socket.c 175 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221
  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/types.h>
  60. #include <linux/kernel.h>
  61. #include <linux/wait.h>
  62. #include <linux/time.h>
  63. #include <linux/ip.h>
  64. #include <linux/capability.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 struct kmem_cache *sctp_bucket_cachep;
  105. extern int sysctl_sctp_mem[3];
  106. extern int sysctl_sctp_rmem[3];
  107. extern int sysctl_sctp_wmem[3];
  108. static int sctp_memory_pressure;
  109. static atomic_t sctp_memory_allocated;
  110. static atomic_t sctp_sockets_allocated;
  111. static void sctp_enter_memory_pressure(void)
  112. {
  113. sctp_memory_pressure = 1;
  114. }
  115. /* Get the sndbuf space available at the time on the association. */
  116. static inline int sctp_wspace(struct sctp_association *asoc)
  117. {
  118. int amt;
  119. if (asoc->ep->sndbuf_policy)
  120. amt = asoc->sndbuf_used;
  121. else
  122. amt = atomic_read(&asoc->base.sk->sk_wmem_alloc);
  123. if (amt >= asoc->base.sk->sk_sndbuf) {
  124. if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
  125. amt = 0;
  126. else {
  127. amt = sk_stream_wspace(asoc->base.sk);
  128. if (amt < 0)
  129. amt = 0;
  130. }
  131. } else {
  132. amt = asoc->base.sk->sk_sndbuf - amt;
  133. }
  134. return amt;
  135. }
  136. /* Increment the used sndbuf space count of the corresponding association by
  137. * the size of the outgoing data chunk.
  138. * Also, set the skb destructor for sndbuf accounting later.
  139. *
  140. * Since it is always 1-1 between chunk and skb, and also a new skb is always
  141. * allocated for chunk bundling in sctp_packet_transmit(), we can use the
  142. * destructor in the data chunk skb for the purpose of the sndbuf space
  143. * tracking.
  144. */
  145. static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
  146. {
  147. struct sctp_association *asoc = chunk->asoc;
  148. struct sock *sk = asoc->base.sk;
  149. /* The sndbuf space is tracked per association. */
  150. sctp_association_hold(asoc);
  151. skb_set_owner_w(chunk->skb, sk);
  152. chunk->skb->destructor = sctp_wfree;
  153. /* Save the chunk pointer in skb for sctp_wfree to use later. */
  154. *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
  155. asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
  156. sizeof(struct sk_buff) +
  157. sizeof(struct sctp_chunk);
  158. atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  159. sk_charge_skb(sk, chunk->skb);
  160. }
  161. /* Verify that this is a valid address. */
  162. static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
  163. int len)
  164. {
  165. struct sctp_af *af;
  166. /* Verify basic sockaddr. */
  167. af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
  168. if (!af)
  169. return -EINVAL;
  170. /* Is this a valid SCTP address? */
  171. if (!af->addr_valid(addr, sctp_sk(sk), NULL))
  172. return -EINVAL;
  173. if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
  174. return -EINVAL;
  175. return 0;
  176. }
  177. /* Look up the association by its id. If this is not a UDP-style
  178. * socket, the ID field is always ignored.
  179. */
  180. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
  181. {
  182. struct sctp_association *asoc = NULL;
  183. /* If this is not a UDP-style socket, assoc id should be ignored. */
  184. if (!sctp_style(sk, UDP)) {
  185. /* Return NULL if the socket state is not ESTABLISHED. It
  186. * could be a TCP-style listening socket or a socket which
  187. * hasn't yet called connect() to establish an association.
  188. */
  189. if (!sctp_sstate(sk, ESTABLISHED))
  190. return NULL;
  191. /* Get the first and the only association from the list. */
  192. if (!list_empty(&sctp_sk(sk)->ep->asocs))
  193. asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
  194. struct sctp_association, asocs);
  195. return asoc;
  196. }
  197. /* Otherwise this is a UDP-style socket. */
  198. if (!id || (id == (sctp_assoc_t)-1))
  199. return NULL;
  200. spin_lock_bh(&sctp_assocs_id_lock);
  201. asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
  202. spin_unlock_bh(&sctp_assocs_id_lock);
  203. if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
  204. return NULL;
  205. return asoc;
  206. }
  207. /* Look up the transport from an address and an assoc id. If both address and
  208. * id are specified, the associations matching the address and the id should be
  209. * the same.
  210. */
  211. static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
  212. struct sockaddr_storage *addr,
  213. sctp_assoc_t id)
  214. {
  215. struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
  216. struct sctp_transport *transport;
  217. union sctp_addr *laddr = (union sctp_addr *)addr;
  218. addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
  219. laddr,
  220. &transport);
  221. if (!addr_asoc)
  222. return NULL;
  223. id_asoc = sctp_id2assoc(sk, id);
  224. if (id_asoc && (id_asoc != addr_asoc))
  225. return NULL;
  226. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  227. (union sctp_addr *)addr);
  228. return transport;
  229. }
  230. /* API 3.1.2 bind() - UDP Style Syntax
  231. * The syntax of bind() is,
  232. *
  233. * ret = bind(int sd, struct sockaddr *addr, int addrlen);
  234. *
  235. * sd - the socket descriptor returned by socket().
  236. * addr - the address structure (struct sockaddr_in or struct
  237. * sockaddr_in6 [RFC 2553]),
  238. * addr_len - the size of the address structure.
  239. */
  240. SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
  241. {
  242. int retval = 0;
  243. sctp_lock_sock(sk);
  244. SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
  245. sk, addr, addr_len);
  246. /* Disallow binding twice. */
  247. if (!sctp_sk(sk)->ep->base.bind_addr.port)
  248. retval = sctp_do_bind(sk, (union sctp_addr *)addr,
  249. addr_len);
  250. else
  251. retval = -EINVAL;
  252. sctp_release_sock(sk);
  253. return retval;
  254. }
  255. static long sctp_get_port_local(struct sock *, union sctp_addr *);
  256. /* Verify this is a valid sockaddr. */
  257. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  258. union sctp_addr *addr, int len)
  259. {
  260. struct sctp_af *af;
  261. /* Check minimum size. */
  262. if (len < sizeof (struct sockaddr))
  263. return NULL;
  264. /* Does this PF support this AF? */
  265. if (!opt->pf->af_supported(addr->sa.sa_family, opt))
  266. return NULL;
  267. /* If we get this far, af is valid. */
  268. af = sctp_get_af_specific(addr->sa.sa_family);
  269. if (len < af->sockaddr_len)
  270. return NULL;
  271. return af;
  272. }
  273. /* Bind a local address either to an endpoint or to an association. */
  274. SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
  275. {
  276. struct sctp_sock *sp = sctp_sk(sk);
  277. struct sctp_endpoint *ep = sp->ep;
  278. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  279. struct sctp_af *af;
  280. unsigned short snum;
  281. int ret = 0;
  282. /* Common sockaddr verification. */
  283. af = sctp_sockaddr_af(sp, addr, len);
  284. if (!af) {
  285. SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
  286. sk, addr, len);
  287. return -EINVAL;
  288. }
  289. snum = ntohs(addr->v4.sin_port);
  290. SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
  291. ", port: %d, new port: %d, len: %d)\n",
  292. sk,
  293. addr,
  294. bp->port, snum,
  295. len);
  296. /* PF specific bind() address verification. */
  297. if (!sp->pf->bind_verify(sp, addr))
  298. return -EADDRNOTAVAIL;
  299. /* We must either be unbound, or bind to the same port.
  300. * It's OK to allow 0 ports if we are already bound.
  301. * We'll just inhert an already bound port in this case
  302. */
  303. if (bp->port) {
  304. if (!snum)
  305. snum = bp->port;
  306. else if (snum != bp->port) {
  307. SCTP_DEBUG_PRINTK("sctp_do_bind:"
  308. " New port %d does not match existing port "
  309. "%d.\n", snum, bp->port);
  310. return -EINVAL;
  311. }
  312. }
  313. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  314. return -EACCES;
  315. /* Make sure we are allowed to bind here.
  316. * The function sctp_get_port_local() does duplicate address
  317. * detection.
  318. */
  319. addr->v4.sin_port = htons(snum);
  320. if ((ret = sctp_get_port_local(sk, addr))) {
  321. if (ret == (long) sk) {
  322. /* This endpoint has a conflicting address. */
  323. return -EINVAL;
  324. } else {
  325. return -EADDRINUSE;
  326. }
  327. }
  328. /* Refresh ephemeral port. */
  329. if (!bp->port)
  330. bp->port = inet_sk(sk)->num;
  331. /* Add the address to the bind address list.
  332. * Use GFP_ATOMIC since BHs will be disabled.
  333. */
  334. ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
  335. /* Copy back into socket for getsockname() use. */
  336. if (!ret) {
  337. inet_sk(sk)->sport = htons(inet_sk(sk)->num);
  338. af->to_sk_saddr(addr, sk);
  339. }
  340. return ret;
  341. }
  342. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  343. *
  344. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  345. * at any one time. If a sender, after sending an ASCONF chunk, decides
  346. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  347. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  348. * subsequent ASCONF. Note this restriction binds each side, so at any
  349. * time two ASCONF may be in-transit on any given association (one sent
  350. * from each endpoint).
  351. */
  352. static int sctp_send_asconf(struct sctp_association *asoc,
  353. struct sctp_chunk *chunk)
  354. {
  355. int retval = 0;
  356. /* If there is an outstanding ASCONF chunk, queue it for later
  357. * transmission.
  358. */
  359. if (asoc->addip_last_asconf) {
  360. list_add_tail(&chunk->list, &asoc->addip_chunk_list);
  361. goto out;
  362. }
  363. /* Hold the chunk until an ASCONF_ACK is received. */
  364. sctp_chunk_hold(chunk);
  365. retval = sctp_primitive_ASCONF(asoc, chunk);
  366. if (retval)
  367. sctp_chunk_free(chunk);
  368. else
  369. asoc->addip_last_asconf = chunk;
  370. out:
  371. return retval;
  372. }
  373. /* Add a list of addresses as bind addresses to local endpoint or
  374. * association.
  375. *
  376. * Basically run through each address specified in the addrs/addrcnt
  377. * array/length pair, determine if it is IPv6 or IPv4 and call
  378. * sctp_do_bind() on it.
  379. *
  380. * If any of them fails, then the operation will be reversed and the
  381. * ones that were added will be removed.
  382. *
  383. * Only sctp_setsockopt_bindx() is supposed to call this function.
  384. */
  385. static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  386. {
  387. int cnt;
  388. int retval = 0;
  389. void *addr_buf;
  390. struct sockaddr *sa_addr;
  391. struct sctp_af *af;
  392. SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
  393. sk, addrs, addrcnt);
  394. addr_buf = addrs;
  395. for (cnt = 0; cnt < addrcnt; cnt++) {
  396. /* The list may contain either IPv4 or IPv6 address;
  397. * determine the address length for walking thru the list.
  398. */
  399. sa_addr = (struct sockaddr *)addr_buf;
  400. af = sctp_get_af_specific(sa_addr->sa_family);
  401. if (!af) {
  402. retval = -EINVAL;
  403. goto err_bindx_add;
  404. }
  405. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  406. af->sockaddr_len);
  407. addr_buf += af->sockaddr_len;
  408. err_bindx_add:
  409. if (retval < 0) {
  410. /* Failed. Cleanup the ones that have been added */
  411. if (cnt > 0)
  412. sctp_bindx_rem(sk, addrs, cnt);
  413. return retval;
  414. }
  415. }
  416. return retval;
  417. }
  418. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  419. * associations that are part of the endpoint indicating that a list of local
  420. * addresses are added to the endpoint.
  421. *
  422. * If any of the addresses is already in the bind address list of the
  423. * association, we do not send the chunk for that association. But it will not
  424. * affect other associations.
  425. *
  426. * Only sctp_setsockopt_bindx() is supposed to call this function.
  427. */
  428. static int sctp_send_asconf_add_ip(struct sock *sk,
  429. struct sockaddr *addrs,
  430. int addrcnt)
  431. {
  432. struct sctp_sock *sp;
  433. struct sctp_endpoint *ep;
  434. struct sctp_association *asoc;
  435. struct sctp_bind_addr *bp;
  436. struct sctp_chunk *chunk;
  437. struct sctp_sockaddr_entry *laddr;
  438. union sctp_addr *addr;
  439. union sctp_addr saveaddr;
  440. void *addr_buf;
  441. struct sctp_af *af;
  442. struct list_head *pos;
  443. struct list_head *p;
  444. int i;
  445. int retval = 0;
  446. if (!sctp_addip_enable)
  447. return retval;
  448. sp = sctp_sk(sk);
  449. ep = sp->ep;
  450. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  451. __FUNCTION__, sk, addrs, addrcnt);
  452. list_for_each(pos, &ep->asocs) {
  453. asoc = list_entry(pos, struct sctp_association, asocs);
  454. if (!asoc->peer.asconf_capable)
  455. continue;
  456. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  457. continue;
  458. if (!sctp_state(asoc, ESTABLISHED))
  459. continue;
  460. /* Check if any address in the packed array of addresses is
  461. * in the bind address list of the association. If so,
  462. * do not send the asconf chunk to its peer, but continue with
  463. * other associations.
  464. */
  465. addr_buf = addrs;
  466. for (i = 0; i < addrcnt; i++) {
  467. addr = (union sctp_addr *)addr_buf;
  468. af = sctp_get_af_specific(addr->v4.sin_family);
  469. if (!af) {
  470. retval = -EINVAL;
  471. goto out;
  472. }
  473. if (sctp_assoc_lookup_laddr(asoc, addr))
  474. break;
  475. addr_buf += af->sockaddr_len;
  476. }
  477. if (i < addrcnt)
  478. continue;
  479. /* Use the first valid address in bind addr list of
  480. * association as Address Parameter of ASCONF CHUNK.
  481. */
  482. bp = &asoc->base.bind_addr;
  483. p = bp->address_list.next;
  484. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  485. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  486. addrcnt, SCTP_PARAM_ADD_IP);
  487. if (!chunk) {
  488. retval = -ENOMEM;
  489. goto out;
  490. }
  491. retval = sctp_send_asconf(asoc, chunk);
  492. if (retval)
  493. goto out;
  494. /* Add the new addresses to the bind address list with
  495. * use_as_src set to 0.
  496. */
  497. addr_buf = addrs;
  498. for (i = 0; i < addrcnt; i++) {
  499. addr = (union sctp_addr *)addr_buf;
  500. af = sctp_get_af_specific(addr->v4.sin_family);
  501. memcpy(&saveaddr, addr, af->sockaddr_len);
  502. retval = sctp_add_bind_addr(bp, &saveaddr, 0,
  503. GFP_ATOMIC);
  504. addr_buf += af->sockaddr_len;
  505. }
  506. }
  507. out:
  508. return retval;
  509. }
  510. /* Remove a list of addresses from bind addresses list. Do not remove the
  511. * last address.
  512. *
  513. * Basically run through each address specified in the addrs/addrcnt
  514. * array/length pair, determine if it is IPv6 or IPv4 and call
  515. * sctp_del_bind() on it.
  516. *
  517. * If any of them fails, then the operation will be reversed and the
  518. * ones that were removed will be added back.
  519. *
  520. * At least one address has to be left; if only one address is
  521. * available, the operation will return -EBUSY.
  522. *
  523. * Only sctp_setsockopt_bindx() is supposed to call this function.
  524. */
  525. static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  526. {
  527. struct sctp_sock *sp = sctp_sk(sk);
  528. struct sctp_endpoint *ep = sp->ep;
  529. int cnt;
  530. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  531. int retval = 0;
  532. void *addr_buf;
  533. union sctp_addr *sa_addr;
  534. struct sctp_af *af;
  535. SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
  536. sk, addrs, addrcnt);
  537. addr_buf = addrs;
  538. for (cnt = 0; cnt < addrcnt; cnt++) {
  539. /* If the bind address list is empty or if there is only one
  540. * bind address, there is nothing more to be removed (we need
  541. * at least one address here).
  542. */
  543. if (list_empty(&bp->address_list) ||
  544. (sctp_list_single_entry(&bp->address_list))) {
  545. retval = -EBUSY;
  546. goto err_bindx_rem;
  547. }
  548. sa_addr = (union sctp_addr *)addr_buf;
  549. af = sctp_get_af_specific(sa_addr->sa.sa_family);
  550. if (!af) {
  551. retval = -EINVAL;
  552. goto err_bindx_rem;
  553. }
  554. if (!af->addr_valid(sa_addr, sp, NULL)) {
  555. retval = -EADDRNOTAVAIL;
  556. goto err_bindx_rem;
  557. }
  558. if (sa_addr->v4.sin_port != htons(bp->port)) {
  559. retval = -EINVAL;
  560. goto err_bindx_rem;
  561. }
  562. /* FIXME - There is probably a need to check if sk->sk_saddr and
  563. * sk->sk_rcv_addr are currently set to one of the addresses to
  564. * be removed. This is something which needs to be looked into
  565. * when we are fixing the outstanding issues with multi-homing
  566. * socket routing and failover schemes. Refer to comments in
  567. * sctp_do_bind(). -daisy
  568. */
  569. retval = sctp_del_bind_addr(bp, sa_addr, call_rcu);
  570. addr_buf += af->sockaddr_len;
  571. err_bindx_rem:
  572. if (retval < 0) {
  573. /* Failed. Add the ones that has been removed back */
  574. if (cnt > 0)
  575. sctp_bindx_add(sk, addrs, cnt);
  576. return retval;
  577. }
  578. }
  579. return retval;
  580. }
  581. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  582. * the associations that are part of the endpoint indicating that a list of
  583. * local addresses are removed from the endpoint.
  584. *
  585. * If any of the addresses is already in the bind address list of the
  586. * association, we do not send the chunk for that association. But it will not
  587. * affect other associations.
  588. *
  589. * Only sctp_setsockopt_bindx() is supposed to call this function.
  590. */
  591. static int sctp_send_asconf_del_ip(struct sock *sk,
  592. struct sockaddr *addrs,
  593. int addrcnt)
  594. {
  595. struct sctp_sock *sp;
  596. struct sctp_endpoint *ep;
  597. struct sctp_association *asoc;
  598. struct sctp_transport *transport;
  599. struct sctp_bind_addr *bp;
  600. struct sctp_chunk *chunk;
  601. union sctp_addr *laddr;
  602. void *addr_buf;
  603. struct sctp_af *af;
  604. struct list_head *pos, *pos1;
  605. struct sctp_sockaddr_entry *saddr;
  606. int i;
  607. int retval = 0;
  608. if (!sctp_addip_enable)
  609. return retval;
  610. sp = sctp_sk(sk);
  611. ep = sp->ep;
  612. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  613. __FUNCTION__, sk, addrs, addrcnt);
  614. list_for_each(pos, &ep->asocs) {
  615. asoc = list_entry(pos, struct sctp_association, asocs);
  616. if (!asoc->peer.asconf_capable)
  617. continue;
  618. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  619. continue;
  620. if (!sctp_state(asoc, ESTABLISHED))
  621. continue;
  622. /* Check if any address in the packed array of addresses is
  623. * not present in the bind address list of the association.
  624. * If so, do not send the asconf chunk to its peer, but
  625. * continue with other associations.
  626. */
  627. addr_buf = addrs;
  628. for (i = 0; i < addrcnt; i++) {
  629. laddr = (union sctp_addr *)addr_buf;
  630. af = sctp_get_af_specific(laddr->v4.sin_family);
  631. if (!af) {
  632. retval = -EINVAL;
  633. goto out;
  634. }
  635. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  636. break;
  637. addr_buf += af->sockaddr_len;
  638. }
  639. if (i < addrcnt)
  640. continue;
  641. /* Find one address in the association's bind address list
  642. * that is not in the packed array of addresses. This is to
  643. * make sure that we do not delete all the addresses in the
  644. * association.
  645. */
  646. bp = &asoc->base.bind_addr;
  647. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  648. addrcnt, sp);
  649. if (!laddr)
  650. continue;
  651. /* We do not need RCU protection throughout this loop
  652. * because this is done under a socket lock from the
  653. * setsockopt call.
  654. */
  655. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  656. SCTP_PARAM_DEL_IP);
  657. if (!chunk) {
  658. retval = -ENOMEM;
  659. goto out;
  660. }
  661. /* Reset use_as_src flag for the addresses in the bind address
  662. * list that are to be deleted.
  663. */
  664. addr_buf = addrs;
  665. for (i = 0; i < addrcnt; i++) {
  666. laddr = (union sctp_addr *)addr_buf;
  667. af = sctp_get_af_specific(laddr->v4.sin_family);
  668. list_for_each_entry(saddr, &bp->address_list, list) {
  669. if (sctp_cmp_addr_exact(&saddr->a, laddr))
  670. saddr->use_as_src = 0;
  671. }
  672. addr_buf += af->sockaddr_len;
  673. }
  674. /* Update the route and saddr entries for all the transports
  675. * as some of the addresses in the bind address list are
  676. * about to be deleted and cannot be used as source addresses.
  677. */
  678. list_for_each(pos1, &asoc->peer.transport_addr_list) {
  679. transport = list_entry(pos1, struct sctp_transport,
  680. transports);
  681. dst_release(transport->dst);
  682. sctp_transport_route(transport, NULL,
  683. sctp_sk(asoc->base.sk));
  684. }
  685. retval = sctp_send_asconf(asoc, chunk);
  686. }
  687. out:
  688. return retval;
  689. }
  690. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  691. *
  692. * API 8.1
  693. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  694. * int flags);
  695. *
  696. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  697. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  698. * or IPv6 addresses.
  699. *
  700. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  701. * Section 3.1.2 for this usage.
  702. *
  703. * addrs is a pointer to an array of one or more socket addresses. Each
  704. * address is contained in its appropriate structure (i.e. struct
  705. * sockaddr_in or struct sockaddr_in6) the family of the address type
  706. * must be used to distinguish the address length (note that this
  707. * representation is termed a "packed array" of addresses). The caller
  708. * specifies the number of addresses in the array with addrcnt.
  709. *
  710. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  711. * -1, and sets errno to the appropriate error code.
  712. *
  713. * For SCTP, the port given in each socket address must be the same, or
  714. * sctp_bindx() will fail, setting errno to EINVAL.
  715. *
  716. * The flags parameter is formed from the bitwise OR of zero or more of
  717. * the following currently defined flags:
  718. *
  719. * SCTP_BINDX_ADD_ADDR
  720. *
  721. * SCTP_BINDX_REM_ADDR
  722. *
  723. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  724. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  725. * addresses from the association. The two flags are mutually exclusive;
  726. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  727. * not remove all addresses from an association; sctp_bindx() will
  728. * reject such an attempt with EINVAL.
  729. *
  730. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  731. * additional addresses with an endpoint after calling bind(). Or use
  732. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  733. * socket is associated with so that no new association accepted will be
  734. * associated with those addresses. If the endpoint supports dynamic
  735. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  736. * endpoint to send the appropriate message to the peer to change the
  737. * peers address lists.
  738. *
  739. * Adding and removing addresses from a connected association is
  740. * optional functionality. Implementations that do not support this
  741. * functionality should return EOPNOTSUPP.
  742. *
  743. * Basically do nothing but copying the addresses from user to kernel
  744. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  745. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
  746. * from userspace.
  747. *
  748. * We don't use copy_from_user() for optimization: we first do the
  749. * sanity checks (buffer size -fast- and access check-healthy
  750. * pointer); if all of those succeed, then we can alloc the memory
  751. * (expensive operation) needed to copy the data to kernel. Then we do
  752. * the copying without checking the user space area
  753. * (__copy_from_user()).
  754. *
  755. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  756. * it.
  757. *
  758. * sk The sk of the socket
  759. * addrs The pointer to the addresses in user land
  760. * addrssize Size of the addrs buffer
  761. * op Operation to perform (add or remove, see the flags of
  762. * sctp_bindx)
  763. *
  764. * Returns 0 if ok, <0 errno code on error.
  765. */
  766. SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
  767. struct sockaddr __user *addrs,
  768. int addrs_size, int op)
  769. {
  770. struct sockaddr *kaddrs;
  771. int err;
  772. int addrcnt = 0;
  773. int walk_size = 0;
  774. struct sockaddr *sa_addr;
  775. void *addr_buf;
  776. struct sctp_af *af;
  777. SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
  778. " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
  779. if (unlikely(addrs_size <= 0))
  780. return -EINVAL;
  781. /* Check the user passed a healthy pointer. */
  782. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  783. return -EFAULT;
  784. /* Alloc space for the address array in kernel memory. */
  785. kaddrs = kmalloc(addrs_size, GFP_KERNEL);
  786. if (unlikely(!kaddrs))
  787. return -ENOMEM;
  788. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  789. kfree(kaddrs);
  790. return -EFAULT;
  791. }
  792. /* Walk through the addrs buffer and count the number of addresses. */
  793. addr_buf = kaddrs;
  794. while (walk_size < addrs_size) {
  795. sa_addr = (struct sockaddr *)addr_buf;
  796. af = sctp_get_af_specific(sa_addr->sa_family);
  797. /* If the address family is not supported or if this address
  798. * causes the address buffer to overflow return EINVAL.
  799. */
  800. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  801. kfree(kaddrs);
  802. return -EINVAL;
  803. }
  804. addrcnt++;
  805. addr_buf += af->sockaddr_len;
  806. walk_size += af->sockaddr_len;
  807. }
  808. /* Do the work. */
  809. switch (op) {
  810. case SCTP_BINDX_ADD_ADDR:
  811. err = sctp_bindx_add(sk, kaddrs, addrcnt);
  812. if (err)
  813. goto out;
  814. err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
  815. break;
  816. case SCTP_BINDX_REM_ADDR:
  817. err = sctp_bindx_rem(sk, kaddrs, addrcnt);
  818. if (err)
  819. goto out;
  820. err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
  821. break;
  822. default:
  823. err = -EINVAL;
  824. break;
  825. }
  826. out:
  827. kfree(kaddrs);
  828. return err;
  829. }
  830. /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
  831. *
  832. * Common routine for handling connect() and sctp_connectx().
  833. * Connect will come in with just a single address.
  834. */
  835. static int __sctp_connect(struct sock* sk,
  836. struct sockaddr *kaddrs,
  837. int addrs_size)
  838. {
  839. struct sctp_sock *sp;
  840. struct sctp_endpoint *ep;
  841. struct sctp_association *asoc = NULL;
  842. struct sctp_association *asoc2;
  843. struct sctp_transport *transport;
  844. union sctp_addr to;
  845. struct sctp_af *af;
  846. sctp_scope_t scope;
  847. long timeo;
  848. int err = 0;
  849. int addrcnt = 0;
  850. int walk_size = 0;
  851. union sctp_addr *sa_addr = NULL;
  852. void *addr_buf;
  853. unsigned short port;
  854. unsigned int f_flags = 0;
  855. sp = sctp_sk(sk);
  856. ep = sp->ep;
  857. /* connect() cannot be done on a socket that is already in ESTABLISHED
  858. * state - UDP-style peeled off socket or a TCP-style socket that
  859. * is already connected.
  860. * It cannot be done even on a TCP-style listening socket.
  861. */
  862. if (sctp_sstate(sk, ESTABLISHED) ||
  863. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
  864. err = -EISCONN;
  865. goto out_free;
  866. }
  867. /* Walk through the addrs buffer and count the number of addresses. */
  868. addr_buf = kaddrs;
  869. while (walk_size < addrs_size) {
  870. sa_addr = (union sctp_addr *)addr_buf;
  871. af = sctp_get_af_specific(sa_addr->sa.sa_family);
  872. port = ntohs(sa_addr->v4.sin_port);
  873. /* If the address family is not supported or if this address
  874. * causes the address buffer to overflow return EINVAL.
  875. */
  876. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  877. err = -EINVAL;
  878. goto out_free;
  879. }
  880. /* Save current address so we can work with it */
  881. memcpy(&to, sa_addr, af->sockaddr_len);
  882. err = sctp_verify_addr(sk, &to, af->sockaddr_len);
  883. if (err)
  884. goto out_free;
  885. /* Make sure the destination port is correctly set
  886. * in all addresses.
  887. */
  888. if (asoc && asoc->peer.port && asoc->peer.port != port)
  889. goto out_free;
  890. /* Check if there already is a matching association on the
  891. * endpoint (other than the one created here).
  892. */
  893. asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  894. if (asoc2 && asoc2 != asoc) {
  895. if (asoc2->state >= SCTP_STATE_ESTABLISHED)
  896. err = -EISCONN;
  897. else
  898. err = -EALREADY;
  899. goto out_free;
  900. }
  901. /* If we could not find a matching association on the endpoint,
  902. * make sure that there is no peeled-off association matching
  903. * the peer address even on another socket.
  904. */
  905. if (sctp_endpoint_is_peeled_off(ep, &to)) {
  906. err = -EADDRNOTAVAIL;
  907. goto out_free;
  908. }
  909. if (!asoc) {
  910. /* If a bind() or sctp_bindx() is not called prior to
  911. * an sctp_connectx() call, the system picks an
  912. * ephemeral port and will choose an address set
  913. * equivalent to binding with a wildcard address.
  914. */
  915. if (!ep->base.bind_addr.port) {
  916. if (sctp_autobind(sk)) {
  917. err = -EAGAIN;
  918. goto out_free;
  919. }
  920. } else {
  921. /*
  922. * If an unprivileged user inherits a 1-many
  923. * style socket with open associations on a
  924. * privileged port, it MAY be permitted to
  925. * accept new associations, but it SHOULD NOT
  926. * be permitted to open new associations.
  927. */
  928. if (ep->base.bind_addr.port < PROT_SOCK &&
  929. !capable(CAP_NET_BIND_SERVICE)) {
  930. err = -EACCES;
  931. goto out_free;
  932. }
  933. }
  934. scope = sctp_scope(&to);
  935. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  936. if (!asoc) {
  937. err = -ENOMEM;
  938. goto out_free;
  939. }
  940. }
  941. /* Prime the peer's transport structures. */
  942. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
  943. SCTP_UNKNOWN);
  944. if (!transport) {
  945. err = -ENOMEM;
  946. goto out_free;
  947. }
  948. addrcnt++;
  949. addr_buf += af->sockaddr_len;
  950. walk_size += af->sockaddr_len;
  951. }
  952. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  953. if (err < 0) {
  954. goto out_free;
  955. }
  956. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  957. if (err < 0) {
  958. goto out_free;
  959. }
  960. /* Initialize sk's dport and daddr for getpeername() */
  961. inet_sk(sk)->dport = htons(asoc->peer.port);
  962. af = sctp_get_af_specific(sa_addr->sa.sa_family);
  963. af->to_sk_daddr(sa_addr, sk);
  964. sk->sk_err = 0;
  965. /* in-kernel sockets don't generally have a file allocated to them
  966. * if all they do is call sock_create_kern().
  967. */
  968. if (sk->sk_socket->file)
  969. f_flags = sk->sk_socket->file->f_flags;
  970. timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
  971. err = sctp_wait_for_connect(asoc, &timeo);
  972. /* Don't free association on exit. */
  973. asoc = NULL;
  974. out_free:
  975. SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
  976. " kaddrs: %p err: %d\n",
  977. asoc, kaddrs, err);
  978. if (asoc)
  979. sctp_association_free(asoc);
  980. return err;
  981. }
  982. /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
  983. *
  984. * API 8.9
  985. * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
  986. *
  987. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  988. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  989. * or IPv6 addresses.
  990. *
  991. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  992. * Section 3.1.2 for this usage.
  993. *
  994. * addrs is a pointer to an array of one or more socket addresses. Each
  995. * address is contained in its appropriate structure (i.e. struct
  996. * sockaddr_in or struct sockaddr_in6) the family of the address type
  997. * must be used to distengish the address length (note that this
  998. * representation is termed a "packed array" of addresses). The caller
  999. * specifies the number of addresses in the array with addrcnt.
  1000. *
  1001. * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
  1002. * -1, and sets errno to the appropriate error code.
  1003. *
  1004. * For SCTP, the port given in each socket address must be the same, or
  1005. * sctp_connectx() will fail, setting errno to EINVAL.
  1006. *
  1007. * An application can use sctp_connectx to initiate an association with
  1008. * an endpoint that is multi-homed. Much like sctp_bindx() this call
  1009. * allows a caller to specify multiple addresses at which a peer can be
  1010. * reached. The way the SCTP stack uses the list of addresses to set up
  1011. * the association is implementation dependant. This function only
  1012. * specifies that the stack will try to make use of all the addresses in
  1013. * the list when needed.
  1014. *
  1015. * Note that the list of addresses passed in is only used for setting up
  1016. * the association. It does not necessarily equal the set of addresses
  1017. * the peer uses for the resulting association. If the caller wants to
  1018. * find out the set of peer addresses, it must use sctp_getpaddrs() to
  1019. * retrieve them after the association has been set up.
  1020. *
  1021. * Basically do nothing but copying the addresses from user to kernel
  1022. * land and invoking either sctp_connectx(). This is used for tunneling
  1023. * the sctp_connectx() request through sctp_setsockopt() from userspace.
  1024. *
  1025. * We don't use copy_from_user() for optimization: we first do the
  1026. * sanity checks (buffer size -fast- and access check-healthy
  1027. * pointer); if all of those succeed, then we can alloc the memory
  1028. * (expensive operation) needed to copy the data to kernel. Then we do
  1029. * the copying without checking the user space area
  1030. * (__copy_from_user()).
  1031. *
  1032. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  1033. * it.
  1034. *
  1035. * sk The sk of the socket
  1036. * addrs The pointer to the addresses in user land
  1037. * addrssize Size of the addrs buffer
  1038. *
  1039. * Returns 0 if ok, <0 errno code on error.
  1040. */
  1041. SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
  1042. struct sockaddr __user *addrs,
  1043. int addrs_size)
  1044. {
  1045. int err = 0;
  1046. struct sockaddr *kaddrs;
  1047. SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
  1048. __FUNCTION__, sk, addrs, addrs_size);
  1049. if (unlikely(addrs_size <= 0))
  1050. return -EINVAL;
  1051. /* Check the user passed a healthy pointer. */
  1052. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  1053. return -EFAULT;
  1054. /* Alloc space for the address array in kernel memory. */
  1055. kaddrs = kmalloc(addrs_size, GFP_KERNEL);
  1056. if (unlikely(!kaddrs))
  1057. return -ENOMEM;
  1058. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  1059. err = -EFAULT;
  1060. } else {
  1061. err = __sctp_connect(sk, kaddrs, addrs_size);
  1062. }
  1063. kfree(kaddrs);
  1064. return err;
  1065. }
  1066. /* API 3.1.4 close() - UDP Style Syntax
  1067. * Applications use close() to perform graceful shutdown (as described in
  1068. * Section 10.1 of [SCTP]) on ALL the associations currently represented
  1069. * by a UDP-style socket.
  1070. *
  1071. * The syntax is
  1072. *
  1073. * ret = close(int sd);
  1074. *
  1075. * sd - the socket descriptor of the associations to be closed.
  1076. *
  1077. * To gracefully shutdown a specific association represented by the
  1078. * UDP-style socket, an application should use the sendmsg() call,
  1079. * passing no user data, but including the appropriate flag in the
  1080. * ancillary data (see Section xxxx).
  1081. *
  1082. * If sd in the close() call is a branched-off socket representing only
  1083. * one association, the shutdown is performed on that association only.
  1084. *
  1085. * 4.1.6 close() - TCP Style Syntax
  1086. *
  1087. * Applications use close() to gracefully close down an association.
  1088. *
  1089. * The syntax is:
  1090. *
  1091. * int close(int sd);
  1092. *
  1093. * sd - the socket descriptor of the association to be closed.
  1094. *
  1095. * After an application calls close() on a socket descriptor, no further
  1096. * socket operations will succeed on that descriptor.
  1097. *
  1098. * API 7.1.4 SO_LINGER
  1099. *
  1100. * An application using the TCP-style socket can use this option to
  1101. * perform the SCTP ABORT primitive. The linger option structure is:
  1102. *
  1103. * struct linger {
  1104. * int l_onoff; // option on/off
  1105. * int l_linger; // linger time
  1106. * };
  1107. *
  1108. * To enable the option, set l_onoff to 1. If the l_linger value is set
  1109. * to 0, calling close() is the same as the ABORT primitive. If the
  1110. * value is set to a negative value, the setsockopt() call will return
  1111. * an error. If the value is set to a positive value linger_time, the
  1112. * close() can be blocked for at most linger_time ms. If the graceful
  1113. * shutdown phase does not finish during this period, close() will
  1114. * return but the graceful shutdown phase continues in the system.
  1115. */
  1116. SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
  1117. {
  1118. struct sctp_endpoint *ep;
  1119. struct sctp_association *asoc;
  1120. struct list_head *pos, *temp;
  1121. SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
  1122. sctp_lock_sock(sk);
  1123. sk->sk_shutdown = SHUTDOWN_MASK;
  1124. ep = sctp_sk(sk)->ep;
  1125. /* Walk all associations on an endpoint. */
  1126. list_for_each_safe(pos, temp, &ep->asocs) {
  1127. asoc = list_entry(pos, struct sctp_association, asocs);
  1128. if (sctp_style(sk, TCP)) {
  1129. /* A closed association can still be in the list if
  1130. * it belongs to a TCP-style listening socket that is
  1131. * not yet accepted. If so, free it. If not, send an
  1132. * ABORT or SHUTDOWN based on the linger options.
  1133. */
  1134. if (sctp_state(asoc, CLOSED)) {
  1135. sctp_unhash_established(asoc);
  1136. sctp_association_free(asoc);
  1137. continue;
  1138. }
  1139. }
  1140. if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
  1141. struct sctp_chunk *chunk;
  1142. chunk = sctp_make_abort_user(asoc, NULL, 0);
  1143. if (chunk)
  1144. sctp_primitive_ABORT(asoc, chunk);
  1145. } else
  1146. sctp_primitive_SHUTDOWN(asoc, NULL);
  1147. }
  1148. /* Clean up any skbs sitting on the receive queue. */
  1149. sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
  1150. sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
  1151. /* On a TCP-style socket, block for at most linger_time if set. */
  1152. if (sctp_style(sk, TCP) && timeout)
  1153. sctp_wait_for_close(sk, timeout);
  1154. /* This will run the backlog queue. */
  1155. sctp_release_sock(sk);
  1156. /* Supposedly, no process has access to the socket, but
  1157. * the net layers still may.
  1158. */
  1159. sctp_local_bh_disable();
  1160. sctp_bh_lock_sock(sk);
  1161. /* Hold the sock, since sk_common_release() will put sock_put()
  1162. * and we have just a little more cleanup.
  1163. */
  1164. sock_hold(sk);
  1165. sk_common_release(sk);
  1166. sctp_bh_unlock_sock(sk);
  1167. sctp_local_bh_enable();
  1168. sock_put(sk);
  1169. SCTP_DBG_OBJCNT_DEC(sock);
  1170. }
  1171. /* Handle EPIPE error. */
  1172. static int sctp_error(struct sock *sk, int flags, int err)
  1173. {
  1174. if (err == -EPIPE)
  1175. err = sock_error(sk) ? : -EPIPE;
  1176. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  1177. send_sig(SIGPIPE, current, 0);
  1178. return err;
  1179. }
  1180. /* API 3.1.3 sendmsg() - UDP Style Syntax
  1181. *
  1182. * An application uses sendmsg() and recvmsg() calls to transmit data to
  1183. * and receive data from its peer.
  1184. *
  1185. * ssize_t sendmsg(int socket, const struct msghdr *message,
  1186. * int flags);
  1187. *
  1188. * socket - the socket descriptor of the endpoint.
  1189. * message - pointer to the msghdr structure which contains a single
  1190. * user message and possibly some ancillary data.
  1191. *
  1192. * See Section 5 for complete description of the data
  1193. * structures.
  1194. *
  1195. * flags - flags sent or received with the user message, see Section
  1196. * 5 for complete description of the flags.
  1197. *
  1198. * Note: This function could use a rewrite especially when explicit
  1199. * connect support comes in.
  1200. */
  1201. /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
  1202. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
  1203. SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
  1204. struct msghdr *msg, size_t msg_len)
  1205. {
  1206. struct sctp_sock *sp;
  1207. struct sctp_endpoint *ep;
  1208. struct sctp_association *new_asoc=NULL, *asoc=NULL;
  1209. struct sctp_transport *transport, *chunk_tp;
  1210. struct sctp_chunk *chunk;
  1211. union sctp_addr to;
  1212. struct sockaddr *msg_name = NULL;
  1213. struct sctp_sndrcvinfo default_sinfo = { 0 };
  1214. struct sctp_sndrcvinfo *sinfo;
  1215. struct sctp_initmsg *sinit;
  1216. sctp_assoc_t associd = 0;
  1217. sctp_cmsgs_t cmsgs = { NULL };
  1218. int err;
  1219. sctp_scope_t scope;
  1220. long timeo;
  1221. __u16 sinfo_flags = 0;
  1222. struct sctp_datamsg *datamsg;
  1223. struct list_head *pos;
  1224. int msg_flags = msg->msg_flags;
  1225. SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
  1226. sk, msg, msg_len);
  1227. err = 0;
  1228. sp = sctp_sk(sk);
  1229. ep = sp->ep;
  1230. SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
  1231. /* We cannot send a message over a TCP-style listening socket. */
  1232. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
  1233. err = -EPIPE;
  1234. goto out_nounlock;
  1235. }
  1236. /* Parse out the SCTP CMSGs. */
  1237. err = sctp_msghdr_parse(msg, &cmsgs);
  1238. if (err) {
  1239. SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
  1240. goto out_nounlock;
  1241. }
  1242. /* Fetch the destination address for this packet. This
  1243. * address only selects the association--it is not necessarily
  1244. * the address we will send to.
  1245. * For a peeled-off socket, msg_name is ignored.
  1246. */
  1247. if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
  1248. int msg_namelen = msg->msg_namelen;
  1249. err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
  1250. msg_namelen);
  1251. if (err)
  1252. return err;
  1253. if (msg_namelen > sizeof(to))
  1254. msg_namelen = sizeof(to);
  1255. memcpy(&to, msg->msg_name, msg_namelen);
  1256. msg_name = msg->msg_name;
  1257. }
  1258. sinfo = cmsgs.info;
  1259. sinit = cmsgs.init;
  1260. /* Did the user specify SNDRCVINFO? */
  1261. if (sinfo) {
  1262. sinfo_flags = sinfo->sinfo_flags;
  1263. associd = sinfo->sinfo_assoc_id;
  1264. }
  1265. SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
  1266. msg_len, sinfo_flags);
  1267. /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
  1268. if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
  1269. err = -EINVAL;
  1270. goto out_nounlock;
  1271. }
  1272. /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
  1273. * length messages when SCTP_EOF|SCTP_ABORT is not set.
  1274. * If SCTP_ABORT is set, the message length could be non zero with
  1275. * the msg_iov set to the user abort reason.
  1276. */
  1277. if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
  1278. (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
  1279. err = -EINVAL;
  1280. goto out_nounlock;
  1281. }
  1282. /* If SCTP_ADDR_OVER is set, there must be an address
  1283. * specified in msg_name.
  1284. */
  1285. if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
  1286. err = -EINVAL;
  1287. goto out_nounlock;
  1288. }
  1289. transport = NULL;
  1290. SCTP_DEBUG_PRINTK("About to look up association.\n");
  1291. sctp_lock_sock(sk);
  1292. /* If a msg_name has been specified, assume this is to be used. */
  1293. if (msg_name) {
  1294. /* Look for a matching association on the endpoint. */
  1295. asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  1296. if (!asoc) {
  1297. /* If we could not find a matching association on the
  1298. * endpoint, make sure that it is not a TCP-style
  1299. * socket that already has an association or there is
  1300. * no peeled-off association on another socket.
  1301. */
  1302. if ((sctp_style(sk, TCP) &&
  1303. sctp_sstate(sk, ESTABLISHED)) ||
  1304. sctp_endpoint_is_peeled_off(ep, &to)) {
  1305. err = -EADDRNOTAVAIL;
  1306. goto out_unlock;
  1307. }
  1308. }
  1309. } else {
  1310. asoc = sctp_id2assoc(sk, associd);
  1311. if (!asoc) {
  1312. err = -EPIPE;
  1313. goto out_unlock;
  1314. }
  1315. }
  1316. if (asoc) {
  1317. SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
  1318. /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
  1319. * socket that has an association in CLOSED state. This can
  1320. * happen when an accepted socket has an association that is
  1321. * already CLOSED.
  1322. */
  1323. if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
  1324. err = -EPIPE;
  1325. goto out_unlock;
  1326. }
  1327. if (sinfo_flags & SCTP_EOF) {
  1328. SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
  1329. asoc);
  1330. sctp_primitive_SHUTDOWN(asoc, NULL);
  1331. err = 0;
  1332. goto out_unlock;
  1333. }
  1334. if (sinfo_flags & SCTP_ABORT) {
  1335. chunk = sctp_make_abort_user(asoc, msg, msg_len);
  1336. if (!chunk) {
  1337. err = -ENOMEM;
  1338. goto out_unlock;
  1339. }
  1340. SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
  1341. sctp_primitive_ABORT(asoc, chunk);
  1342. err = 0;
  1343. goto out_unlock;
  1344. }
  1345. }
  1346. /* Do we need to create the association? */
  1347. if (!asoc) {
  1348. SCTP_DEBUG_PRINTK("There is no association yet.\n");
  1349. if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
  1350. err = -EINVAL;
  1351. goto out_unlock;
  1352. }
  1353. /* Check for invalid stream against the stream counts,
  1354. * either the default or the user specified stream counts.
  1355. */
  1356. if (sinfo) {
  1357. if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
  1358. /* Check against the defaults. */
  1359. if (sinfo->sinfo_stream >=
  1360. sp->initmsg.sinit_num_ostreams) {
  1361. err = -EINVAL;
  1362. goto out_unlock;
  1363. }
  1364. } else {
  1365. /* Check against the requested. */
  1366. if (sinfo->sinfo_stream >=
  1367. sinit->sinit_num_ostreams) {
  1368. err = -EINVAL;
  1369. goto out_unlock;
  1370. }
  1371. }
  1372. }
  1373. /*
  1374. * API 3.1.2 bind() - UDP Style Syntax
  1375. * If a bind() or sctp_bindx() is not called prior to a
  1376. * sendmsg() call that initiates a new association, the
  1377. * system picks an ephemeral port and will choose an address
  1378. * set equivalent to binding with a wildcard address.
  1379. */
  1380. if (!ep->base.bind_addr.port) {
  1381. if (sctp_autobind(sk)) {
  1382. err = -EAGAIN;
  1383. goto out_unlock;
  1384. }
  1385. } else {
  1386. /*
  1387. * If an unprivileged user inherits a one-to-many
  1388. * style socket with open associations on a privileged
  1389. * port, it MAY be permitted to accept new associations,
  1390. * but it SHOULD NOT be permitted to open new
  1391. * associations.
  1392. */
  1393. if (ep->base.bind_addr.port < PROT_SOCK &&
  1394. !capable(CAP_NET_BIND_SERVICE)) {
  1395. err = -EACCES;
  1396. goto out_unlock;
  1397. }
  1398. }
  1399. scope = sctp_scope(&to);
  1400. new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  1401. if (!new_asoc) {
  1402. err = -ENOMEM;
  1403. goto out_unlock;
  1404. }
  1405. asoc = new_asoc;
  1406. /* If the SCTP_INIT ancillary data is specified, set all
  1407. * the association init values accordingly.
  1408. */
  1409. if (sinit) {
  1410. if (sinit->sinit_num_ostreams) {
  1411. asoc->c.sinit_num_ostreams =
  1412. sinit->sinit_num_ostreams;
  1413. }
  1414. if (sinit->sinit_max_instreams) {
  1415. asoc->c.sinit_max_instreams =
  1416. sinit->sinit_max_instreams;
  1417. }
  1418. if (sinit->sinit_max_attempts) {
  1419. asoc->max_init_attempts
  1420. = sinit->sinit_max_attempts;
  1421. }
  1422. if (sinit->sinit_max_init_timeo) {
  1423. asoc->max_init_timeo =
  1424. msecs_to_jiffies(sinit->sinit_max_init_timeo);
  1425. }
  1426. }
  1427. /* Prime the peer's transport structures. */
  1428. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
  1429. if (!transport) {
  1430. err = -ENOMEM;
  1431. goto out_free;
  1432. }
  1433. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  1434. if (err < 0) {
  1435. err = -ENOMEM;
  1436. goto out_free;
  1437. }
  1438. }
  1439. /* ASSERT: we have a valid association at this point. */
  1440. SCTP_DEBUG_PRINTK("We have a valid association.\n");
  1441. if (!sinfo) {
  1442. /* If the user didn't specify SNDRCVINFO, make up one with
  1443. * some defaults.
  1444. */
  1445. default_sinfo.sinfo_stream = asoc->default_stream;
  1446. default_sinfo.sinfo_flags = asoc->default_flags;
  1447. default_sinfo.sinfo_ppid = asoc->default_ppid;
  1448. default_sinfo.sinfo_context = asoc->default_context;
  1449. default_sinfo.sinfo_timetolive = asoc->default_timetolive;
  1450. default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
  1451. sinfo = &default_sinfo;
  1452. }
  1453. /* API 7.1.7, the sndbuf size per association bounds the
  1454. * maximum size of data that can be sent in a single send call.
  1455. */
  1456. if (msg_len > sk->sk_sndbuf) {
  1457. err = -EMSGSIZE;
  1458. goto out_free;
  1459. }
  1460. if (asoc->pmtu_pending)
  1461. sctp_assoc_pending_pmtu(asoc);
  1462. /* If fragmentation is disabled and the message length exceeds the
  1463. * association fragmentation point, return EMSGSIZE. The I-D
  1464. * does not specify what this error is, but this looks like
  1465. * a great fit.
  1466. */
  1467. if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
  1468. err = -EMSGSIZE;
  1469. goto out_free;
  1470. }
  1471. if (sinfo) {
  1472. /* Check for invalid stream. */
  1473. if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
  1474. err = -EINVAL;
  1475. goto out_free;
  1476. }
  1477. }
  1478. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1479. if (!sctp_wspace(asoc)) {
  1480. err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
  1481. if (err)
  1482. goto out_free;
  1483. }
  1484. /* If an address is passed with the sendto/sendmsg call, it is used
  1485. * to override the primary destination address in the TCP model, or
  1486. * when SCTP_ADDR_OVER flag is set in the UDP model.
  1487. */
  1488. if ((sctp_style(sk, TCP) && msg_name) ||
  1489. (sinfo_flags & SCTP_ADDR_OVER)) {
  1490. chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
  1491. if (!chunk_tp) {
  1492. err = -EINVAL;
  1493. goto out_free;
  1494. }
  1495. } else
  1496. chunk_tp = NULL;
  1497. /* Auto-connect, if we aren't connected already. */
  1498. if (sctp_state(asoc, CLOSED)) {
  1499. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  1500. if (err < 0)
  1501. goto out_free;
  1502. SCTP_DEBUG_PRINTK("We associated primitively.\n");
  1503. }
  1504. /* Break the message into multiple chunks of maximum size. */
  1505. datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
  1506. if (!datamsg) {
  1507. err = -ENOMEM;
  1508. goto out_free;
  1509. }
  1510. /* Now send the (possibly) fragmented message. */
  1511. list_for_each(pos, &datamsg->chunks) {
  1512. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  1513. sctp_datamsg_track(chunk);
  1514. /* Do accounting for the write space. */
  1515. sctp_set_owner_w(chunk);
  1516. chunk->transport = chunk_tp;
  1517. /* Send it to the lower layers. Note: all chunks
  1518. * must either fail or succeed. The lower layer
  1519. * works that way today. Keep it that way or this
  1520. * breaks.
  1521. */
  1522. err = sctp_primitive_SEND(asoc, chunk);
  1523. /* Did the lower layer accept the chunk? */
  1524. if (err)
  1525. sctp_chunk_free(chunk);
  1526. SCTP_DEBUG_PRINTK("We sent primitively.\n");
  1527. }
  1528. sctp_datamsg_free(datamsg);
  1529. if (err)
  1530. goto out_free;
  1531. else
  1532. err = msg_len;
  1533. /* If we are already past ASSOCIATE, the lower
  1534. * layers are responsible for association cleanup.
  1535. */
  1536. goto out_unlock;
  1537. out_free:
  1538. if (new_asoc)
  1539. sctp_association_free(asoc);
  1540. out_unlock:
  1541. sctp_release_sock(sk);
  1542. out_nounlock:
  1543. return sctp_error(sk, msg_flags, err);
  1544. #if 0
  1545. do_sock_err:
  1546. if (msg_len)
  1547. err = msg_len;
  1548. else
  1549. err = sock_error(sk);
  1550. goto out;
  1551. do_interrupted:
  1552. if (msg_len)
  1553. err = msg_len;
  1554. goto out;
  1555. #endif /* 0 */
  1556. }
  1557. /* This is an extended version of skb_pull() that removes the data from the
  1558. * start of a skb even when data is spread across the list of skb's in the
  1559. * frag_list. len specifies the total amount of data that needs to be removed.
  1560. * when 'len' bytes could be removed from the skb, it returns 0.
  1561. * If 'len' exceeds the total skb length, it returns the no. of bytes that
  1562. * could not be removed.
  1563. */
  1564. static int sctp_skb_pull(struct sk_buff *skb, int len)
  1565. {
  1566. struct sk_buff *list;
  1567. int skb_len = skb_headlen(skb);
  1568. int rlen;
  1569. if (len <= skb_len) {
  1570. __skb_pull(skb, len);
  1571. return 0;
  1572. }
  1573. len -= skb_len;
  1574. __skb_pull(skb, skb_len);
  1575. for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
  1576. rlen = sctp_skb_pull(list, len);
  1577. skb->len -= (len-rlen);
  1578. skb->data_len -= (len-rlen);
  1579. if (!rlen)
  1580. return 0;
  1581. len = rlen;
  1582. }
  1583. return len;
  1584. }
  1585. /* API 3.1.3 recvmsg() - UDP Style Syntax
  1586. *
  1587. * ssize_t recvmsg(int socket, struct msghdr *message,
  1588. * int flags);
  1589. *
  1590. * socket - the socket descriptor of the endpoint.
  1591. * message - pointer to the msghdr structure which contains a single
  1592. * user message and possibly some ancillary data.
  1593. *
  1594. * See Section 5 for complete description of the data
  1595. * structures.
  1596. *
  1597. * flags - flags sent or received with the user message, see Section
  1598. * 5 for complete description of the flags.
  1599. */
  1600. static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
  1601. SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
  1602. struct msghdr *msg, size_t len, int noblock,
  1603. int flags, int *addr_len)
  1604. {
  1605. struct sctp_ulpevent *event = NULL;
  1606. struct sctp_sock *sp = sctp_sk(sk);
  1607. struct sk_buff *skb;
  1608. int copied;
  1609. int err = 0;
  1610. int skb_len;
  1611. SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
  1612. "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
  1613. "len", len, "knoblauch", noblock,
  1614. "flags", flags, "addr_len", addr_len);
  1615. sctp_lock_sock(sk);
  1616. if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
  1617. err = -ENOTCONN;
  1618. goto out;
  1619. }
  1620. skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
  1621. if (!skb)
  1622. goto out;
  1623. /* Get the total length of the skb including any skb's in the
  1624. * frag_list.
  1625. */
  1626. skb_len = skb->len;
  1627. copied = skb_len;
  1628. if (copied > len)
  1629. copied = len;
  1630. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1631. event = sctp_skb2event(skb);
  1632. if (err)
  1633. goto out_free;
  1634. sock_recv_timestamp(msg, sk, skb);
  1635. if (sctp_ulpevent_is_notification(event)) {
  1636. msg->msg_flags |= MSG_NOTIFICATION;
  1637. sp->pf->event_msgname(event, msg->msg_name, addr_len);
  1638. } else {
  1639. sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
  1640. }
  1641. /* Check if we allow SCTP_SNDRCVINFO. */
  1642. if (sp->subscribe.sctp_data_io_event)
  1643. sctp_ulpevent_read_sndrcvinfo(event, msg);
  1644. #if 0
  1645. /* FIXME: we should be calling IP/IPv6 layers. */
  1646. if (sk->sk_protinfo.af_inet.cmsg_flags)
  1647. ip_cmsg_recv(msg, skb);
  1648. #endif
  1649. err = copied;
  1650. /* If skb's length exceeds the user's buffer, update the skb and
  1651. * push it back to the receive_queue so that the next call to
  1652. * recvmsg() will return the remaining data. Don't set MSG_EOR.
  1653. */
  1654. if (skb_len > copied) {
  1655. msg->msg_flags &= ~MSG_EOR;
  1656. if (flags & MSG_PEEK)
  1657. goto out_free;
  1658. sctp_skb_pull(skb, copied);
  1659. skb_queue_head(&sk->sk_receive_queue, skb);
  1660. /* When only partial message is copied to the user, increase
  1661. * rwnd by that amount. If all the data in the skb is read,
  1662. * rwnd is updated when the event is freed.
  1663. */
  1664. sctp_assoc_rwnd_increase(event->asoc, copied);
  1665. goto out;
  1666. } else if ((event->msg_flags & MSG_NOTIFICATION) ||
  1667. (event->msg_flags & MSG_EOR))
  1668. msg->msg_flags |= MSG_EOR;
  1669. else
  1670. msg->msg_flags &= ~MSG_EOR;
  1671. out_free:
  1672. if (flags & MSG_PEEK) {
  1673. /* Release the skb reference acquired after peeking the skb in
  1674. * sctp_skb_recv_datagram().
  1675. */
  1676. kfree_skb(skb);
  1677. } else {
  1678. /* Free the event which includes releasing the reference to
  1679. * the owner of the skb, freeing the skb and updating the
  1680. * rwnd.
  1681. */
  1682. sctp_ulpevent_free(event);
  1683. }
  1684. out:
  1685. sctp_release_sock(sk);
  1686. return err;
  1687. }
  1688. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  1689. *
  1690. * This option is a on/off flag. If enabled no SCTP message
  1691. * fragmentation will be performed. Instead if a message being sent
  1692. * exceeds the current PMTU size, the message will NOT be sent and
  1693. * instead a error will be indicated to the user.
  1694. */
  1695. static int sctp_setsockopt_disable_fragments(struct sock *sk,
  1696. char __user *optval, int optlen)
  1697. {
  1698. int val;
  1699. if (optlen < sizeof(int))
  1700. return -EINVAL;
  1701. if (get_user(val, (int __user *)optval))
  1702. return -EFAULT;
  1703. sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
  1704. return 0;
  1705. }
  1706. static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
  1707. int optlen)
  1708. {
  1709. if (optlen != sizeof(struct sctp_event_subscribe))
  1710. return -EINVAL;
  1711. if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
  1712. return -EFAULT;
  1713. return 0;
  1714. }
  1715. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  1716. *
  1717. * This socket option is applicable to the UDP-style socket only. When
  1718. * set it will cause associations that are idle for more than the
  1719. * specified number of seconds to automatically close. An association
  1720. * being idle is defined an association that has NOT sent or received
  1721. * user data. The special value of '0' indicates that no automatic
  1722. * close of any associations should be performed. The option expects an
  1723. * integer defining the number of seconds of idle time before an
  1724. * association is closed.
  1725. */
  1726. static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
  1727. int optlen)
  1728. {
  1729. struct sctp_sock *sp = sctp_sk(sk);
  1730. /* Applicable to UDP-style socket only */
  1731. if (sctp_style(sk, TCP))
  1732. return -EOPNOTSUPP;
  1733. if (optlen != sizeof(int))
  1734. return -EINVAL;
  1735. if (copy_from_user(&sp->autoclose, optval, optlen))
  1736. return -EFAULT;
  1737. return 0;
  1738. }
  1739. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  1740. *
  1741. * Applications can enable or disable heartbeats for any peer address of
  1742. * an association, modify an address's heartbeat interval, force a
  1743. * heartbeat to be sent immediately, and adjust the address's maximum
  1744. * number of retransmissions sent before an address is considered
  1745. * unreachable. The following structure is used to access and modify an
  1746. * address's parameters:
  1747. *
  1748. * struct sctp_paddrparams {
  1749. * sctp_assoc_t spp_assoc_id;
  1750. * struct sockaddr_storage spp_address;
  1751. * uint32_t spp_hbinterval;
  1752. * uint16_t spp_pathmaxrxt;
  1753. * uint32_t spp_pathmtu;
  1754. * uint32_t spp_sackdelay;
  1755. * uint32_t spp_flags;
  1756. * };
  1757. *
  1758. * spp_assoc_id - (one-to-many style socket) This is filled in the
  1759. * application, and identifies the association for
  1760. * this query.
  1761. * spp_address - This specifies which address is of interest.
  1762. * spp_hbinterval - This contains the value of the heartbeat interval,
  1763. * in milliseconds. If a value of zero
  1764. * is present in this field then no changes are to
  1765. * be made to this parameter.
  1766. * spp_pathmaxrxt - This contains the maximum number of
  1767. * retransmissions before this address shall be
  1768. * considered unreachable. If a value of zero
  1769. * is present in this field then no changes are to
  1770. * be made to this parameter.
  1771. * spp_pathmtu - When Path MTU discovery is disabled the value
  1772. * specified here will be the "fixed" path mtu.
  1773. * Note that if the spp_address field is empty
  1774. * then all associations on this address will
  1775. * have this fixed path mtu set upon them.
  1776. *
  1777. * spp_sackdelay - When delayed sack is enabled, this value specifies
  1778. * the number of milliseconds that sacks will be delayed
  1779. * for. This value will apply to all addresses of an
  1780. * association if the spp_address field is empty. Note
  1781. * also, that if delayed sack is enabled and this
  1782. * value is set to 0, no change is made to the last
  1783. * recorded delayed sack timer value.
  1784. *
  1785. * spp_flags - These flags are used to control various features
  1786. * on an association. The flag field may contain
  1787. * zero or more of the following options.
  1788. *
  1789. * SPP_HB_ENABLE - Enable heartbeats on the
  1790. * specified address. Note that if the address
  1791. * field is empty all addresses for the association
  1792. * have heartbeats enabled upon them.
  1793. *
  1794. * SPP_HB_DISABLE - Disable heartbeats on the
  1795. * speicifed address. Note that if the address
  1796. * field is empty all addresses for the association
  1797. * will have their heartbeats disabled. Note also
  1798. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  1799. * mutually exclusive, only one of these two should
  1800. * be specified. Enabling both fields will have
  1801. * undetermined results.
  1802. *
  1803. * SPP_HB_DEMAND - Request a user initiated heartbeat
  1804. * to be made immediately.
  1805. *
  1806. * SPP_HB_TIME_IS_ZERO - Specify's that the time for
  1807. * heartbeat delayis to be set to the value of 0
  1808. * milliseconds.
  1809. *
  1810. * SPP_PMTUD_ENABLE - This field will enable PMTU
  1811. * discovery upon the specified address. Note that
  1812. * if the address feild is empty then all addresses
  1813. * on the association are effected.
  1814. *
  1815. * SPP_PMTUD_DISABLE - This field will disable PMTU
  1816. * discovery upon the specified address. Note that
  1817. * if the address feild is empty then all addresses
  1818. * on the association are effected. Not also that
  1819. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  1820. * exclusive. Enabling both will have undetermined
  1821. * results.
  1822. *
  1823. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  1824. * on delayed sack. The time specified in spp_sackdelay
  1825. * is used to specify the sack delay for this address. Note
  1826. * that if spp_address is empty then all addresses will
  1827. * enable delayed sack and take on the sack delay
  1828. * value specified in spp_sackdelay.
  1829. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  1830. * off delayed sack. If the spp_address field is blank then
  1831. * delayed sack is disabled for the entire association. Note
  1832. * also that this field is mutually exclusive to
  1833. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  1834. * results.
  1835. */
  1836. static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
  1837. struct sctp_transport *trans,
  1838. struct sctp_association *asoc,
  1839. struct sctp_sock *sp,
  1840. int hb_change,
  1841. int pmtud_change,
  1842. int sackdelay_change)
  1843. {
  1844. int error;
  1845. if (params->spp_flags & SPP_HB_DEMAND && trans) {
  1846. error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
  1847. if (error)
  1848. return error;
  1849. }
  1850. /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
  1851. * this field is ignored. Note also that a value of zero indicates
  1852. * the current setting should be left unchanged.
  1853. */
  1854. if (params->spp_flags & SPP_HB_ENABLE) {
  1855. /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
  1856. * set. This lets us use 0 value when this flag
  1857. * is set.
  1858. */
  1859. if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
  1860. params->spp_hbinterval = 0;
  1861. if (params->spp_hbinterval ||
  1862. (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
  1863. if (trans) {
  1864. trans->hbinterval =
  1865. msecs_to_jiffies(params->spp_hbinterval);
  1866. } else if (asoc) {
  1867. asoc->hbinterval =
  1868. msecs_to_jiffies(params->spp_hbinterval);
  1869. } else {
  1870. sp->hbinterval = params->spp_hbinterval;
  1871. }
  1872. }
  1873. }
  1874. if (hb_change) {
  1875. if (trans) {
  1876. trans->param_flags =
  1877. (trans->param_flags & ~SPP_HB) | hb_change;
  1878. } else if (asoc) {
  1879. asoc->param_flags =
  1880. (asoc->param_flags & ~SPP_HB) | hb_change;
  1881. } else {
  1882. sp->param_flags =
  1883. (sp->param_flags & ~SPP_HB) | hb_change;
  1884. }
  1885. }
  1886. /* When Path MTU discovery is disabled the value specified here will
  1887. * be the "fixed" path mtu (i.e. the value of the spp_flags field must
  1888. * include the flag SPP_PMTUD_DISABLE for this field to have any
  1889. * effect).
  1890. */
  1891. if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
  1892. if (trans) {
  1893. trans->pathmtu = params->spp_pathmtu;
  1894. sctp_assoc_sync_pmtu(asoc);
  1895. } else if (asoc) {
  1896. asoc->pathmtu = params->spp_pathmtu;
  1897. sctp_frag_point(sp, params->spp_pathmtu);
  1898. } else {
  1899. sp->pathmtu = params->spp_pathmtu;
  1900. }
  1901. }
  1902. if (pmtud_change) {
  1903. if (trans) {
  1904. int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
  1905. (params->spp_flags & SPP_PMTUD_ENABLE);
  1906. trans->param_flags =
  1907. (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
  1908. if (update) {
  1909. sctp_transport_pmtu(trans);
  1910. sctp_assoc_sync_pmtu(asoc);
  1911. }
  1912. } else if (asoc) {
  1913. asoc->param_flags =
  1914. (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
  1915. } else {
  1916. sp->param_flags =
  1917. (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
  1918. }
  1919. }
  1920. /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
  1921. * value of this field is ignored. Note also that a value of zero
  1922. * indicates the current setting should be left unchanged.
  1923. */
  1924. if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
  1925. if (trans) {
  1926. trans->sackdelay =
  1927. msecs_to_jiffies(params->spp_sackdelay);
  1928. } else if (asoc) {
  1929. asoc->sackdelay =
  1930. msecs_to_jiffies(params->spp_sackdelay);
  1931. } else {
  1932. sp->sackdelay = params->spp_sackdelay;
  1933. }
  1934. }
  1935. if (sackdelay_change) {
  1936. if (trans) {
  1937. trans->param_flags =
  1938. (trans->param_flags & ~SPP_SACKDELAY) |
  1939. sackdelay_change;
  1940. } else if (asoc) {
  1941. asoc->param_flags =
  1942. (asoc->param_flags & ~SPP_SACKDELAY) |
  1943. sackdelay_change;
  1944. } else {
  1945. sp->param_flags =
  1946. (sp->param_flags & ~SPP_SACKDELAY) |
  1947. sackdelay_change;
  1948. }
  1949. }
  1950. /* Note that unless the spp_flag is set to SPP_PMTUD_ENABLE the value
  1951. * of this field is ignored. Note also that a value of zero
  1952. * indicates the current setting should be left unchanged.
  1953. */
  1954. if ((params->spp_flags & SPP_PMTUD_ENABLE) && params->spp_pathmaxrxt) {
  1955. if (trans) {
  1956. trans->pathmaxrxt = params->spp_pathmaxrxt;
  1957. } else if (asoc) {
  1958. asoc->pathmaxrxt = params->spp_pathmaxrxt;
  1959. } else {
  1960. sp->pathmaxrxt = params->spp_pathmaxrxt;
  1961. }
  1962. }
  1963. return 0;
  1964. }
  1965. static int sctp_setsockopt_peer_addr_params(struct sock *sk,
  1966. char __user *optval, int optlen)
  1967. {
  1968. struct sctp_paddrparams params;
  1969. struct sctp_transport *trans = NULL;
  1970. struct sctp_association *asoc = NULL;
  1971. struct sctp_sock *sp = sctp_sk(sk);
  1972. int error;
  1973. int hb_change, pmtud_change, sackdelay_change;
  1974. if (optlen != sizeof(struct sctp_paddrparams))
  1975. return - EINVAL;
  1976. if (copy_from_user(&params, optval, optlen))
  1977. return -EFAULT;
  1978. /* Validate flags and value parameters. */
  1979. hb_change = params.spp_flags & SPP_HB;
  1980. pmtud_change = params.spp_flags & SPP_PMTUD;
  1981. sackdelay_change = params.spp_flags & SPP_SACKDELAY;
  1982. if (hb_change == SPP_HB ||
  1983. pmtud_change == SPP_PMTUD ||
  1984. sackdelay_change == SPP_SACKDELAY ||
  1985. params.spp_sackdelay > 500 ||
  1986. (params.spp_pathmtu
  1987. && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
  1988. return -EINVAL;
  1989. /* If an address other than INADDR_ANY is specified, and
  1990. * no transport is found, then the request is invalid.
  1991. */
  1992. if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
  1993. trans = sctp_addr_id2transport(sk, &params.spp_address,
  1994. params.spp_assoc_id);
  1995. if (!trans)
  1996. return -EINVAL;
  1997. }
  1998. /* Get association, if assoc_id != 0 and the socket is a one
  1999. * to many style socket, and an association was not found, then
  2000. * the id was invalid.
  2001. */
  2002. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  2003. if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
  2004. return -EINVAL;
  2005. /* Heartbeat demand can only be sent on a transport or
  2006. * association, but not a socket.
  2007. */
  2008. if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
  2009. return -EINVAL;
  2010. /* Process parameters. */
  2011. error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  2012. hb_change, pmtud_change,
  2013. sackdelay_change);
  2014. if (error)
  2015. return error;
  2016. /* If changes are for association, also apply parameters to each
  2017. * transport.
  2018. */
  2019. if (!trans && asoc) {
  2020. struct list_head *pos;
  2021. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2022. trans = list_entry(pos, struct sctp_transport,
  2023. transports);
  2024. sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  2025. hb_change, pmtud_change,
  2026. sackdelay_change);
  2027. }
  2028. }
  2029. return 0;
  2030. }
  2031. /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
  2032. *
  2033. * This options will get or set the delayed ack timer. The time is set
  2034. * in milliseconds. If the assoc_id is 0, then this sets or gets the
  2035. * endpoints default delayed ack timer value. If the assoc_id field is
  2036. * non-zero, then the set or get effects the specified association.
  2037. *
  2038. * struct sctp_assoc_value {
  2039. * sctp_assoc_t assoc_id;
  2040. * uint32_t assoc_value;
  2041. * };
  2042. *
  2043. * assoc_id - This parameter, indicates which association the
  2044. * user is preforming an action upon. Note that if
  2045. * this field's value is zero then the endpoints
  2046. * default value is changed (effecting future
  2047. * associations only).
  2048. *
  2049. * assoc_value - This parameter contains the number of milliseconds
  2050. * that the user is requesting the delayed ACK timer
  2051. * be set to. Note that this value is defined in
  2052. * the standard to be between 200 and 500 milliseconds.
  2053. *
  2054. * Note: a value of zero will leave the value alone,
  2055. * but disable SACK delay. A non-zero value will also
  2056. * enable SACK delay.
  2057. */
  2058. static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
  2059. char __user *optval, int optlen)
  2060. {
  2061. struct sctp_assoc_value params;
  2062. struct sctp_transport *trans = NULL;
  2063. struct sctp_association *asoc = NULL;
  2064. struct sctp_sock *sp = sctp_sk(sk);
  2065. if (optlen != sizeof(struct sctp_assoc_value))
  2066. return - EINVAL;
  2067. if (copy_from_user(&params, optval, optlen))
  2068. return -EFAULT;
  2069. /* Validate value parameter. */
  2070. if (params.assoc_value > 500)
  2071. return -EINVAL;
  2072. /* Get association, if assoc_id != 0 and the socket is a one
  2073. * to many style socket, and an association was not found, then
  2074. * the id was invalid.
  2075. */
  2076. asoc = sctp_id2assoc(sk, params.assoc_id);
  2077. if (!asoc && params.assoc_id && sctp_style(sk, UDP))
  2078. return -EINVAL;
  2079. if (params.assoc_value) {
  2080. if (asoc) {
  2081. asoc->sackdelay =
  2082. msecs_to_jiffies(params.assoc_value);
  2083. asoc->param_flags =
  2084. (asoc->param_flags & ~SPP_SACKDELAY) |
  2085. SPP_SACKDELAY_ENABLE;
  2086. } else {
  2087. sp->sackdelay = params.assoc_value;
  2088. sp->param_flags =
  2089. (sp->param_flags & ~SPP_SACKDELAY) |
  2090. SPP_SACKDELAY_ENABLE;
  2091. }
  2092. } else {
  2093. if (asoc) {
  2094. asoc->param_flags =
  2095. (asoc->param_flags & ~SPP_SACKDELAY) |
  2096. SPP_SACKDELAY_DISABLE;
  2097. } else {
  2098. sp->param_flags =
  2099. (sp->param_flags & ~SPP_SACKDELAY) |
  2100. SPP_SACKDELAY_DISABLE;
  2101. }
  2102. }
  2103. /* If change is for association, also apply to each transport. */
  2104. if (asoc) {
  2105. struct list_head *pos;
  2106. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2107. trans = list_entry(pos, struct sctp_transport,
  2108. transports);
  2109. if (params.assoc_value) {
  2110. trans->sackdelay =
  2111. msecs_to_jiffies(params.assoc_value);
  2112. trans->param_flags =
  2113. (trans->param_flags & ~SPP_SACKDELAY) |
  2114. SPP_SACKDELAY_ENABLE;
  2115. } else {
  2116. trans->param_flags =
  2117. (trans->param_flags & ~SPP_SACKDELAY) |
  2118. SPP_SACKDELAY_DISABLE;
  2119. }
  2120. }
  2121. }
  2122. return 0;
  2123. }
  2124. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  2125. *
  2126. * Applications can specify protocol parameters for the default association
  2127. * initialization. The option name argument to setsockopt() and getsockopt()
  2128. * is SCTP_INITMSG.
  2129. *
  2130. * Setting initialization parameters is effective only on an unconnected
  2131. * socket (for UDP-style sockets only future associations are effected
  2132. * by the change). With TCP-style sockets, this option is inherited by
  2133. * sockets derived from a listener socket.
  2134. */
  2135. static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
  2136. {
  2137. struct sctp_initmsg sinit;
  2138. struct sctp_sock *sp = sctp_sk(sk);
  2139. if (optlen != sizeof(struct sctp_initmsg))
  2140. return -EINVAL;
  2141. if (copy_from_user(&sinit, optval, optlen))
  2142. return -EFAULT;
  2143. if (sinit.sinit_num_ostreams)
  2144. sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
  2145. if (sinit.sinit_max_instreams)
  2146. sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
  2147. if (sinit.sinit_max_attempts)
  2148. sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
  2149. if (sinit.sinit_max_init_timeo)
  2150. sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
  2151. return 0;
  2152. }
  2153. /*
  2154. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  2155. *
  2156. * Applications that wish to use the sendto() system call may wish to
  2157. * specify a default set of parameters that would normally be supplied
  2158. * through the inclusion of ancillary data. This socket option allows
  2159. * such an application to set the default sctp_sndrcvinfo structure.
  2160. * The application that wishes to use this socket option simply passes
  2161. * in to this call the sctp_sndrcvinfo structure defined in Section
  2162. * 5.2.2) The input parameters accepted by this call include
  2163. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  2164. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  2165. * to this call if the caller is using the UDP model.
  2166. */
  2167. static int sctp_setsockopt_default_send_param(struct sock *sk,
  2168. char __user *optval, int optlen)
  2169. {
  2170. struct sctp_sndrcvinfo info;
  2171. struct sctp_association *asoc;
  2172. struct sctp_sock *sp = sctp_sk(sk);
  2173. if (optlen != sizeof(struct sctp_sndrcvinfo))
  2174. return -EINVAL;
  2175. if (copy_from_user(&info, optval, optlen))
  2176. return -EFAULT;
  2177. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  2178. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  2179. return -EINVAL;
  2180. if (asoc) {
  2181. asoc->default_stream = info.sinfo_stream;
  2182. asoc->default_flags = info.sinfo_flags;
  2183. asoc->default_ppid = info.sinfo_ppid;
  2184. asoc->default_context = info.sinfo_context;
  2185. asoc->default_timetolive = info.sinfo_timetolive;
  2186. } else {
  2187. sp->default_stream = info.sinfo_stream;
  2188. sp->default_flags = info.sinfo_flags;
  2189. sp->default_ppid = info.sinfo_ppid;
  2190. sp->default_context = info.sinfo_context;
  2191. sp->default_timetolive = info.sinfo_timetolive;
  2192. }
  2193. return 0;
  2194. }
  2195. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  2196. *
  2197. * Requests that the local SCTP stack use the enclosed peer address as
  2198. * the association primary. The enclosed address must be one of the
  2199. * association peer's addresses.
  2200. */
  2201. static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
  2202. int optlen)
  2203. {
  2204. struct sctp_prim prim;
  2205. struct sctp_transport *trans;
  2206. if (optlen != sizeof(struct sctp_prim))
  2207. return -EINVAL;
  2208. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  2209. return -EFAULT;
  2210. trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
  2211. if (!trans)
  2212. return -EINVAL;
  2213. sctp_assoc_set_primary(trans->asoc, trans);
  2214. return 0;
  2215. }
  2216. /*
  2217. * 7.1.5 SCTP_NODELAY
  2218. *
  2219. * Turn on/off any Nagle-like algorithm. This means that packets are
  2220. * generally sent as soon as possible and no unnecessary delays are
  2221. * introduced, at the cost of more packets in the network. Expects an
  2222. * integer boolean flag.
  2223. */
  2224. static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
  2225. int optlen)
  2226. {
  2227. int val;
  2228. if (optlen < sizeof(int))
  2229. return -EINVAL;
  2230. if (get_user(val, (int __user *)optval))
  2231. return -EFAULT;
  2232. sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
  2233. return 0;
  2234. }
  2235. /*
  2236. *
  2237. * 7.1.1 SCTP_RTOINFO
  2238. *
  2239. * The protocol parameters used to initialize and bound retransmission
  2240. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  2241. * and modify these parameters.
  2242. * All parameters are time values, in milliseconds. A value of 0, when
  2243. * modifying the parameters, indicates that the current value should not
  2244. * be changed.
  2245. *
  2246. */
  2247. static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
  2248. struct sctp_rtoinfo rtoinfo;
  2249. struct sctp_association *asoc;
  2250. if (optlen != sizeof (struct sctp_rtoinfo))
  2251. return -EINVAL;
  2252. if (copy_from_user(&rtoinfo, optval, optlen))
  2253. return -EFAULT;
  2254. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  2255. /* Set the values to the specific association */
  2256. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  2257. return -EINVAL;
  2258. if (asoc) {
  2259. if (rtoinfo.srto_initial != 0)
  2260. asoc->rto_initial =
  2261. msecs_to_jiffies(rtoinfo.srto_initial);
  2262. if (rtoinfo.srto_max != 0)
  2263. asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
  2264. if (rtoinfo.srto_min != 0)
  2265. asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
  2266. } else {
  2267. /* If there is no association or the association-id = 0
  2268. * set the values to the endpoint.
  2269. */
  2270. struct sctp_sock *sp = sctp_sk(sk);
  2271. if (rtoinfo.srto_initial != 0)
  2272. sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
  2273. if (rtoinfo.srto_max != 0)
  2274. sp->rtoinfo.srto_max = rtoinfo.srto_max;
  2275. if (rtoinfo.srto_min != 0)
  2276. sp->rtoinfo.srto_min = rtoinfo.srto_min;
  2277. }
  2278. return 0;
  2279. }
  2280. /*
  2281. *
  2282. * 7.1.2 SCTP_ASSOCINFO
  2283. *
  2284. * This option is used to tune the maximum retransmission attempts
  2285. * of the association.
  2286. * Returns an error if the new association retransmission value is
  2287. * greater than the sum of the retransmission value of the peer.
  2288. * See [SCTP] for more information.
  2289. *
  2290. */
  2291. static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
  2292. {
  2293. struct sctp_assocparams assocparams;
  2294. struct sctp_association *asoc;
  2295. if (optlen != sizeof(struct sctp_assocparams))
  2296. return -EINVAL;
  2297. if (copy_from_user(&assocparams, optval, optlen))
  2298. return -EFAULT;
  2299. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  2300. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  2301. return -EINVAL;
  2302. /* Set the values to the specific association */
  2303. if (asoc) {
  2304. if (assocparams.sasoc_asocmaxrxt != 0) {
  2305. __u32 path_sum = 0;
  2306. int paths = 0;
  2307. struct list_head *pos;
  2308. struct sctp_transport *peer_addr;
  2309. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2310. peer_addr = list_entry(pos,
  2311. struct sctp_transport,
  2312. transports);
  2313. path_sum += peer_addr->pathmaxrxt;
  2314. paths++;
  2315. }
  2316. /* Only validate asocmaxrxt if we have more then
  2317. * one path/transport. We do this because path
  2318. * retransmissions are only counted when we have more
  2319. * then one path.
  2320. */
  2321. if (paths > 1 &&
  2322. assocparams.sasoc_asocmaxrxt > path_sum)
  2323. return -EINVAL;
  2324. asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
  2325. }
  2326. if (assocparams.sasoc_cookie_life != 0) {
  2327. asoc->cookie_life.tv_sec =
  2328. assocparams.sasoc_cookie_life / 1000;
  2329. asoc->cookie_life.tv_usec =
  2330. (assocparams.sasoc_cookie_life % 1000)
  2331. * 1000;
  2332. }
  2333. } else {
  2334. /* Set the values to the endpoint */
  2335. struct sctp_sock *sp = sctp_sk(sk);
  2336. if (assocparams.sasoc_asocmaxrxt != 0)
  2337. sp->assocparams.sasoc_asocmaxrxt =
  2338. assocparams.sasoc_asocmaxrxt;
  2339. if (assocparams.sasoc_cookie_life != 0)
  2340. sp->assocparams.sasoc_cookie_life =
  2341. assocparams.sasoc_cookie_life;
  2342. }
  2343. return 0;
  2344. }
  2345. /*
  2346. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  2347. *
  2348. * This socket option is a boolean flag which turns on or off mapped V4
  2349. * addresses. If this option is turned on and the socket is type
  2350. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  2351. * If this option is turned off, then no mapping will be done of V4
  2352. * addresses and a user will receive both PF_INET6 and PF_INET type
  2353. * addresses on the socket.
  2354. */
  2355. static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
  2356. {
  2357. int val;
  2358. struct sctp_sock *sp = sctp_sk(sk);
  2359. if (optlen < sizeof(int))
  2360. return -EINVAL;
  2361. if (get_user(val, (int __user *)optval))
  2362. return -EFAULT;
  2363. if (val)
  2364. sp->v4mapped = 1;
  2365. else
  2366. sp->v4mapped = 0;
  2367. return 0;
  2368. }
  2369. /*
  2370. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  2371. *
  2372. * This socket option specifies the maximum size to put in any outgoing
  2373. * SCTP chunk. If a message is larger than this size it will be
  2374. * fragmented by SCTP into the specified size. Note that the underlying
  2375. * SCTP implementation may fragment into smaller sized chunks when the
  2376. * PMTU of the underlying association is smaller than the value set by
  2377. * the user.
  2378. */
  2379. static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
  2380. {
  2381. struct sctp_association *asoc;
  2382. struct list_head *pos;
  2383. struct sctp_sock *sp = sctp_sk(sk);
  2384. int val;
  2385. if (optlen < sizeof(int))
  2386. return -EINVAL;
  2387. if (get_user(val, (int __user *)optval))
  2388. return -EFAULT;
  2389. if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
  2390. return -EINVAL;
  2391. sp->user_frag = val;
  2392. /* Update the frag_point of the existing associations. */
  2393. list_for_each(pos, &(sp->ep->asocs)) {
  2394. asoc = list_entry(pos, struct sctp_association, asocs);
  2395. asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
  2396. }
  2397. return 0;
  2398. }
  2399. /*
  2400. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  2401. *
  2402. * Requests that the peer mark the enclosed address as the association
  2403. * primary. The enclosed address must be one of the association's
  2404. * locally bound addresses. The following structure is used to make a
  2405. * set primary request:
  2406. */
  2407. static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
  2408. int optlen)
  2409. {
  2410. struct sctp_sock *sp;
  2411. struct sctp_endpoint *ep;
  2412. struct sctp_association *asoc = NULL;
  2413. struct sctp_setpeerprim prim;
  2414. struct sctp_chunk *chunk;
  2415. int err;
  2416. sp = sctp_sk(sk);
  2417. ep = sp->ep;
  2418. if (!sctp_addip_enable)
  2419. return -EPERM;
  2420. if (optlen != sizeof(struct sctp_setpeerprim))
  2421. return -EINVAL;
  2422. if (copy_from_user(&prim, optval, optlen))
  2423. return -EFAULT;
  2424. asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
  2425. if (!asoc)
  2426. return -EINVAL;
  2427. if (!asoc->peer.asconf_capable)
  2428. return -EPERM;
  2429. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  2430. return -EPERM;
  2431. if (!sctp_state(asoc, ESTABLISHED))
  2432. return -ENOTCONN;
  2433. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
  2434. return -EADDRNOTAVAIL;
  2435. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  2436. chunk = sctp_make_asconf_set_prim(asoc,
  2437. (union sctp_addr *)&prim.sspp_addr);
  2438. if (!chunk)
  2439. return -ENOMEM;
  2440. err = sctp_send_asconf(asoc, chunk);
  2441. SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
  2442. return err;
  2443. }
  2444. static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
  2445. int optlen)
  2446. {
  2447. struct sctp_setadaptation adaptation;
  2448. if (optlen != sizeof(struct sctp_setadaptation))
  2449. return -EINVAL;
  2450. if (copy_from_user(&adaptation, optval, optlen))
  2451. return -EFAULT;
  2452. sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
  2453. return 0;
  2454. }
  2455. /*
  2456. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  2457. *
  2458. * The context field in the sctp_sndrcvinfo structure is normally only
  2459. * used when a failed message is retrieved holding the value that was
  2460. * sent down on the actual send call. This option allows the setting of
  2461. * a default context on an association basis that will be received on
  2462. * reading messages from the peer. This is especially helpful in the
  2463. * one-2-many model for an application to keep some reference to an
  2464. * internal state machine that is processing messages on the
  2465. * association. Note that the setting of this value only effects
  2466. * received messages from the peer and does not effect the value that is
  2467. * saved with outbound messages.
  2468. */
  2469. static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
  2470. int optlen)
  2471. {
  2472. struct sctp_assoc_value params;
  2473. struct sctp_sock *sp;
  2474. struct sctp_association *asoc;
  2475. if (optlen != sizeof(struct sctp_assoc_value))
  2476. return -EINVAL;
  2477. if (copy_from_user(&params, optval, optlen))
  2478. return -EFAULT;
  2479. sp = sctp_sk(sk);
  2480. if (params.assoc_id != 0) {
  2481. asoc = sctp_id2assoc(sk, params.assoc_id);
  2482. if (!asoc)
  2483. return -EINVAL;
  2484. asoc->default_rcv_context = params.assoc_value;
  2485. } else {
  2486. sp->default_rcv_context = params.assoc_value;
  2487. }
  2488. return 0;
  2489. }
  2490. /*
  2491. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  2492. *
  2493. * This options will at a minimum specify if the implementation is doing
  2494. * fragmented interleave. Fragmented interleave, for a one to many
  2495. * socket, is when subsequent calls to receive a message may return
  2496. * parts of messages from different associations. Some implementations
  2497. * may allow you to turn this value on or off. If so, when turned off,
  2498. * no fragment interleave will occur (which will cause a head of line
  2499. * blocking amongst multiple associations sharing the same one to many
  2500. * socket). When this option is turned on, then each receive call may
  2501. * come from a different association (thus the user must receive data
  2502. * with the extended calls (e.g. sctp_recvmsg) to keep track of which
  2503. * association each receive belongs to.
  2504. *
  2505. * This option takes a boolean value. A non-zero value indicates that
  2506. * fragmented interleave is on. A value of zero indicates that
  2507. * fragmented interleave is off.
  2508. *
  2509. * Note that it is important that an implementation that allows this
  2510. * option to be turned on, have it off by default. Otherwise an unaware
  2511. * application using the one to many model may become confused and act
  2512. * incorrectly.
  2513. */
  2514. static int sctp_setsockopt_fragment_interleave(struct sock *sk,
  2515. char __user *optval,
  2516. int optlen)
  2517. {
  2518. int val;
  2519. if (optlen != sizeof(int))
  2520. return -EINVAL;
  2521. if (get_user(val, (int __user *)optval))
  2522. return -EFAULT;
  2523. sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
  2524. return 0;
  2525. }
  2526. /*
  2527. * 7.1.25. Set or Get the sctp partial delivery point
  2528. * (SCTP_PARTIAL_DELIVERY_POINT)
  2529. * This option will set or get the SCTP partial delivery point. This
  2530. * point is the size of a message where the partial delivery API will be
  2531. * invoked to help free up rwnd space for the peer. Setting this to a
  2532. * lower value will cause partial delivery's to happen more often. The
  2533. * calls argument is an integer that sets or gets the partial delivery
  2534. * point.
  2535. */
  2536. static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
  2537. char __user *optval,
  2538. int optlen)
  2539. {
  2540. u32 val;
  2541. if (optlen != sizeof(u32))
  2542. return -EINVAL;
  2543. if (get_user(val, (int __user *)optval))
  2544. return -EFAULT;
  2545. sctp_sk(sk)->pd_point = val;
  2546. return 0; /* is this the right error code? */
  2547. }
  2548. /*
  2549. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  2550. *
  2551. * This option will allow a user to change the maximum burst of packets
  2552. * that can be emitted by this association. Note that the default value
  2553. * is 4, and some implementations may restrict this setting so that it
  2554. * can only be lowered.
  2555. *
  2556. * NOTE: This text doesn't seem right. Do this on a socket basis with
  2557. * future associations inheriting the socket value.
  2558. */
  2559. static int sctp_setsockopt_maxburst(struct sock *sk,
  2560. char __user *optval,
  2561. int optlen)
  2562. {
  2563. int val;
  2564. if (optlen != sizeof(int))
  2565. return -EINVAL;
  2566. if (get_user(val, (int __user *)optval))
  2567. return -EFAULT;
  2568. if (val < 0)
  2569. return -EINVAL;
  2570. sctp_sk(sk)->max_burst = val;
  2571. return 0;
  2572. }
  2573. /* API 6.2 setsockopt(), getsockopt()
  2574. *
  2575. * Applications use setsockopt() and getsockopt() to set or retrieve
  2576. * socket options. Socket options are used to change the default
  2577. * behavior of sockets calls. They are described in Section 7.
  2578. *
  2579. * The syntax is:
  2580. *
  2581. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  2582. * int __user *optlen);
  2583. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  2584. * int optlen);
  2585. *
  2586. * sd - the socket descript.
  2587. * level - set to IPPROTO_SCTP for all SCTP options.
  2588. * optname - the option name.
  2589. * optval - the buffer to store the value of the option.
  2590. * optlen - the size of the buffer.
  2591. */
  2592. SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
  2593. char __user *optval, int optlen)
  2594. {
  2595. int retval = 0;
  2596. SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
  2597. sk, optname);
  2598. /* I can hardly begin to describe how wrong this is. This is
  2599. * so broken as to be worse than useless. The API draft
  2600. * REALLY is NOT helpful here... I am not convinced that the
  2601. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  2602. * are at all well-founded.
  2603. */
  2604. if (level != SOL_SCTP) {
  2605. struct sctp_af *af = sctp_sk(sk)->pf->af;
  2606. retval = af->setsockopt(sk, level, optname, optval, optlen);
  2607. goto out_nounlock;
  2608. }
  2609. sctp_lock_sock(sk);
  2610. switch (optname) {
  2611. case SCTP_SOCKOPT_BINDX_ADD:
  2612. /* 'optlen' is the size of the addresses buffer. */
  2613. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  2614. optlen, SCTP_BINDX_ADD_ADDR);
  2615. break;
  2616. case SCTP_SOCKOPT_BINDX_REM:
  2617. /* 'optlen' is the size of the addresses buffer. */
  2618. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  2619. optlen, SCTP_BINDX_REM_ADDR);
  2620. break;
  2621. case SCTP_SOCKOPT_CONNECTX:
  2622. /* 'optlen' is the size of the addresses buffer. */
  2623. retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
  2624. optlen);
  2625. break;
  2626. case SCTP_DISABLE_FRAGMENTS:
  2627. retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
  2628. break;
  2629. case SCTP_EVENTS:
  2630. retval = sctp_setsockopt_events(sk, optval, optlen);
  2631. break;
  2632. case SCTP_AUTOCLOSE:
  2633. retval = sctp_setsockopt_autoclose(sk, optval, optlen);
  2634. break;
  2635. case SCTP_PEER_ADDR_PARAMS:
  2636. retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
  2637. break;
  2638. case SCTP_DELAYED_ACK_TIME:
  2639. retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
  2640. break;
  2641. case SCTP_PARTIAL_DELIVERY_POINT:
  2642. retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
  2643. break;
  2644. case SCTP_INITMSG:
  2645. retval = sctp_setsockopt_initmsg(sk, optval, optlen);
  2646. break;
  2647. case SCTP_DEFAULT_SEND_PARAM:
  2648. retval = sctp_setsockopt_default_send_param(sk, optval,
  2649. optlen);
  2650. break;
  2651. case SCTP_PRIMARY_ADDR:
  2652. retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
  2653. break;
  2654. case SCTP_SET_PEER_PRIMARY_ADDR:
  2655. retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
  2656. break;
  2657. case SCTP_NODELAY:
  2658. retval = sctp_setsockopt_nodelay(sk, optval, optlen);
  2659. break;
  2660. case SCTP_RTOINFO:
  2661. retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
  2662. break;
  2663. case SCTP_ASSOCINFO:
  2664. retval = sctp_setsockopt_associnfo(sk, optval, optlen);
  2665. break;
  2666. case SCTP_I_WANT_MAPPED_V4_ADDR:
  2667. retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
  2668. break;
  2669. case SCTP_MAXSEG:
  2670. retval = sctp_setsockopt_maxseg(sk, optval, optlen);
  2671. break;
  2672. case SCTP_ADAPTATION_LAYER:
  2673. retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
  2674. break;
  2675. case SCTP_CONTEXT:
  2676. retval = sctp_setsockopt_context(sk, optval, optlen);
  2677. break;
  2678. case SCTP_FRAGMENT_INTERLEAVE:
  2679. retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
  2680. break;
  2681. case SCTP_MAX_BURST:
  2682. retval = sctp_setsockopt_maxburst(sk, optval, optlen);
  2683. break;
  2684. default:
  2685. retval = -ENOPROTOOPT;
  2686. break;
  2687. }
  2688. sctp_release_sock(sk);
  2689. out_nounlock:
  2690. return retval;
  2691. }
  2692. /* API 3.1.6 connect() - UDP Style Syntax
  2693. *
  2694. * An application may use the connect() call in the UDP model to initiate an
  2695. * association without sending data.
  2696. *
  2697. * The syntax is:
  2698. *
  2699. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  2700. *
  2701. * sd: the socket descriptor to have a new association added to.
  2702. *
  2703. * nam: the address structure (either struct sockaddr_in or struct
  2704. * sockaddr_in6 defined in RFC2553 [7]).
  2705. *
  2706. * len: the size of the address.
  2707. */
  2708. SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
  2709. int addr_len)
  2710. {
  2711. int err = 0;
  2712. struct sctp_af *af;
  2713. sctp_lock_sock(sk);
  2714. SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
  2715. __FUNCTION__, sk, addr, addr_len);
  2716. /* Validate addr_len before calling common connect/connectx routine. */
  2717. af = sctp_get_af_specific(addr->sa_family);
  2718. if (!af || addr_len < af->sockaddr_len) {
  2719. err = -EINVAL;
  2720. } else {
  2721. /* Pass correct addr len to common routine (so it knows there
  2722. * is only one address being passed.
  2723. */
  2724. err = __sctp_connect(sk, addr, af->sockaddr_len);
  2725. }
  2726. sctp_release_sock(sk);
  2727. return err;
  2728. }
  2729. /* FIXME: Write comments. */
  2730. SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
  2731. {
  2732. return -EOPNOTSUPP; /* STUB */
  2733. }
  2734. /* 4.1.4 accept() - TCP Style Syntax
  2735. *
  2736. * Applications use accept() call to remove an established SCTP
  2737. * association from the accept queue of the endpoint. A new socket
  2738. * descriptor will be returned from accept() to represent the newly
  2739. * formed association.
  2740. */
  2741. SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
  2742. {
  2743. struct sctp_sock *sp;
  2744. struct sctp_endpoint *ep;
  2745. struct sock *newsk = NULL;
  2746. struct sctp_association *asoc;
  2747. long timeo;
  2748. int error = 0;
  2749. sctp_lock_sock(sk);
  2750. sp = sctp_sk(sk);
  2751. ep = sp->ep;
  2752. if (!sctp_style(sk, TCP)) {
  2753. error = -EOPNOTSUPP;
  2754. goto out;
  2755. }
  2756. if (!sctp_sstate(sk, LISTENING)) {
  2757. error = -EINVAL;
  2758. goto out;
  2759. }
  2760. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  2761. error = sctp_wait_for_accept(sk, timeo);
  2762. if (error)
  2763. goto out;
  2764. /* We treat the list of associations on the endpoint as the accept
  2765. * queue and pick the first association on the list.
  2766. */
  2767. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  2768. newsk = sp->pf->create_accept_sk(sk, asoc);
  2769. if (!newsk) {
  2770. error = -ENOMEM;
  2771. goto out;
  2772. }
  2773. /* Populate the fields of the newsk from the oldsk and migrate the
  2774. * asoc to the newsk.
  2775. */
  2776. sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  2777. out:
  2778. sctp_release_sock(sk);
  2779. *err = error;
  2780. return newsk;
  2781. }
  2782. /* The SCTP ioctl handler. */
  2783. SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  2784. {
  2785. return -ENOIOCTLCMD;
  2786. }
  2787. /* This is the function which gets called during socket creation to
  2788. * initialized the SCTP-specific portion of the sock.
  2789. * The sock structure should already be zero-filled memory.
  2790. */
  2791. SCTP_STATIC int sctp_init_sock(struct sock *sk)
  2792. {
  2793. struct sctp_endpoint *ep;
  2794. struct sctp_sock *sp;
  2795. SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
  2796. sp = sctp_sk(sk);
  2797. /* Initialize the SCTP per socket area. */
  2798. switch (sk->sk_type) {
  2799. case SOCK_SEQPACKET:
  2800. sp->type = SCTP_SOCKET_UDP;
  2801. break;
  2802. case SOCK_STREAM:
  2803. sp->type = SCTP_SOCKET_TCP;
  2804. break;
  2805. default:
  2806. return -ESOCKTNOSUPPORT;
  2807. }
  2808. /* Initialize default send parameters. These parameters can be
  2809. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  2810. */
  2811. sp->default_stream = 0;
  2812. sp->default_ppid = 0;
  2813. sp->default_flags = 0;
  2814. sp->default_context = 0;
  2815. sp->default_timetolive = 0;
  2816. sp->default_rcv_context = 0;
  2817. sp->max_burst = sctp_max_burst;
  2818. /* Initialize default setup parameters. These parameters
  2819. * can be modified with the SCTP_INITMSG socket option or
  2820. * overridden by the SCTP_INIT CMSG.
  2821. */
  2822. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  2823. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  2824. sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
  2825. sp->initmsg.sinit_max_init_timeo = sctp_rto_max;
  2826. /* Initialize default RTO related parameters. These parameters can
  2827. * be modified for with the SCTP_RTOINFO socket option.
  2828. */
  2829. sp->rtoinfo.srto_initial = sctp_rto_initial;
  2830. sp->rtoinfo.srto_max = sctp_rto_max;
  2831. sp->rtoinfo.srto_min = sctp_rto_min;
  2832. /* Initialize default association related parameters. These parameters
  2833. * can be modified with the SCTP_ASSOCINFO socket option.
  2834. */
  2835. sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
  2836. sp->assocparams.sasoc_number_peer_destinations = 0;
  2837. sp->assocparams.sasoc_peer_rwnd = 0;
  2838. sp->assocparams.sasoc_local_rwnd = 0;
  2839. sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life;
  2840. /* Initialize default event subscriptions. By default, all the
  2841. * options are off.
  2842. */
  2843. memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
  2844. /* Default Peer Address Parameters. These defaults can
  2845. * be modified via SCTP_PEER_ADDR_PARAMS
  2846. */
  2847. sp->hbinterval = sctp_hb_interval;
  2848. sp->pathmaxrxt = sctp_max_retrans_path;
  2849. sp->pathmtu = 0; // allow default discovery
  2850. sp->sackdelay = sctp_sack_timeout;
  2851. sp->param_flags = SPP_HB_ENABLE |
  2852. SPP_PMTUD_ENABLE |
  2853. SPP_SACKDELAY_ENABLE;
  2854. /* If enabled no SCTP message fragmentation will be performed.
  2855. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  2856. */
  2857. sp->disable_fragments = 0;
  2858. /* Enable Nagle algorithm by default. */
  2859. sp->nodelay = 0;
  2860. /* Enable by default. */
  2861. sp->v4mapped = 1;
  2862. /* Auto-close idle associations after the configured
  2863. * number of seconds. A value of 0 disables this
  2864. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  2865. * for UDP-style sockets only.
  2866. */
  2867. sp->autoclose = 0;
  2868. /* User specified fragmentation limit. */
  2869. sp->user_frag = 0;
  2870. sp->adaptation_ind = 0;
  2871. sp->pf = sctp_get_pf_specific(sk->sk_family);
  2872. /* Control variables for partial data delivery. */
  2873. atomic_set(&sp->pd_mode, 0);
  2874. skb_queue_head_init(&sp->pd_lobby);
  2875. sp->frag_interleave = 0;
  2876. /* Create a per socket endpoint structure. Even if we
  2877. * change the data structure relationships, this may still
  2878. * be useful for storing pre-connect address information.
  2879. */
  2880. ep = sctp_endpoint_new(sk, GFP_KERNEL);
  2881. if (!ep)
  2882. return -ENOMEM;
  2883. sp->ep = ep;
  2884. sp->hmac = NULL;
  2885. SCTP_DBG_OBJCNT_INC(sock);
  2886. atomic_inc(&sctp_sockets_allocated);
  2887. return 0;
  2888. }
  2889. /* Cleanup any SCTP per socket resources. */
  2890. SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
  2891. {
  2892. struct sctp_endpoint *ep;
  2893. SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
  2894. /* Release our hold on the endpoint. */
  2895. ep = sctp_sk(sk)->ep;
  2896. sctp_endpoint_free(ep);
  2897. atomic_dec(&sctp_sockets_allocated);
  2898. return 0;
  2899. }
  2900. /* API 4.1.7 shutdown() - TCP Style Syntax
  2901. * int shutdown(int socket, int how);
  2902. *
  2903. * sd - the socket descriptor of the association to be closed.
  2904. * how - Specifies the type of shutdown. The values are
  2905. * as follows:
  2906. * SHUT_RD
  2907. * Disables further receive operations. No SCTP
  2908. * protocol action is taken.
  2909. * SHUT_WR
  2910. * Disables further send operations, and initiates
  2911. * the SCTP shutdown sequence.
  2912. * SHUT_RDWR
  2913. * Disables further send and receive operations
  2914. * and initiates the SCTP shutdown sequence.
  2915. */
  2916. SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
  2917. {
  2918. struct sctp_endpoint *ep;
  2919. struct sctp_association *asoc;
  2920. if (!sctp_style(sk, TCP))
  2921. return;
  2922. if (how & SEND_SHUTDOWN) {
  2923. ep = sctp_sk(sk)->ep;
  2924. if (!list_empty(&ep->asocs)) {
  2925. asoc = list_entry(ep->asocs.next,
  2926. struct sctp_association, asocs);
  2927. sctp_primitive_SHUTDOWN(asoc, NULL);
  2928. }
  2929. }
  2930. }
  2931. /* 7.2.1 Association Status (SCTP_STATUS)
  2932. * Applications can retrieve current status information about an
  2933. * association, including association state, peer receiver window size,
  2934. * number of unacked data chunks, and number of data chunks pending
  2935. * receipt. This information is read-only.
  2936. */
  2937. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  2938. char __user *optval,
  2939. int __user *optlen)
  2940. {
  2941. struct sctp_status status;
  2942. struct sctp_association *asoc = NULL;
  2943. struct sctp_transport *transport;
  2944. sctp_assoc_t associd;
  2945. int retval = 0;
  2946. if (len < sizeof(status)) {
  2947. retval = -EINVAL;
  2948. goto out;
  2949. }
  2950. len = sizeof(status);
  2951. if (copy_from_user(&status, optval, len)) {
  2952. retval = -EFAULT;
  2953. goto out;
  2954. }
  2955. associd = status.sstat_assoc_id;
  2956. asoc = sctp_id2assoc(sk, associd);
  2957. if (!asoc) {
  2958. retval = -EINVAL;
  2959. goto out;
  2960. }
  2961. transport = asoc->peer.primary_path;
  2962. status.sstat_assoc_id = sctp_assoc2id(asoc);
  2963. status.sstat_state = asoc->state;
  2964. status.sstat_rwnd = asoc->peer.rwnd;
  2965. status.sstat_unackdata = asoc->unack_data;
  2966. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  2967. status.sstat_instrms = asoc->c.sinit_max_instreams;
  2968. status.sstat_outstrms = asoc->c.sinit_num_ostreams;
  2969. status.sstat_fragmentation_point = asoc->frag_point;
  2970. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  2971. memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
  2972. transport->af_specific->sockaddr_len);
  2973. /* Map ipv4 address into v4-mapped-on-v6 address. */
  2974. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  2975. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  2976. status.sstat_primary.spinfo_state = transport->state;
  2977. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  2978. status.sstat_primary.spinfo_srtt = transport->srtt;
  2979. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  2980. status.sstat_primary.spinfo_mtu = transport->pathmtu;
  2981. if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
  2982. status.sstat_primary.spinfo_state = SCTP_ACTIVE;
  2983. if (put_user(len, optlen)) {
  2984. retval = -EFAULT;
  2985. goto out;
  2986. }
  2987. SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
  2988. len, status.sstat_state, status.sstat_rwnd,
  2989. status.sstat_assoc_id);
  2990. if (copy_to_user(optval, &status, len)) {
  2991. retval = -EFAULT;
  2992. goto out;
  2993. }
  2994. out:
  2995. return (retval);
  2996. }
  2997. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  2998. *
  2999. * Applications can retrieve information about a specific peer address
  3000. * of an association, including its reachability state, congestion
  3001. * window, and retransmission timer values. This information is
  3002. * read-only.
  3003. */
  3004. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  3005. char __user *optval,
  3006. int __user *optlen)
  3007. {
  3008. struct sctp_paddrinfo pinfo;
  3009. struct sctp_transport *transport;
  3010. int retval = 0;
  3011. if (len < sizeof(pinfo)) {
  3012. retval = -EINVAL;
  3013. goto out;
  3014. }
  3015. len = sizeof(pinfo);
  3016. if (copy_from_user(&pinfo, optval, len)) {
  3017. retval = -EFAULT;
  3018. goto out;
  3019. }
  3020. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  3021. pinfo.spinfo_assoc_id);
  3022. if (!transport)
  3023. return -EINVAL;
  3024. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  3025. pinfo.spinfo_state = transport->state;
  3026. pinfo.spinfo_cwnd = transport->cwnd;
  3027. pinfo.spinfo_srtt = transport->srtt;
  3028. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  3029. pinfo.spinfo_mtu = transport->pathmtu;
  3030. if (pinfo.spinfo_state == SCTP_UNKNOWN)
  3031. pinfo.spinfo_state = SCTP_ACTIVE;
  3032. if (put_user(len, optlen)) {
  3033. retval = -EFAULT;
  3034. goto out;
  3035. }
  3036. if (copy_to_user(optval, &pinfo, len)) {
  3037. retval = -EFAULT;
  3038. goto out;
  3039. }
  3040. out:
  3041. return (retval);
  3042. }
  3043. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  3044. *
  3045. * This option is a on/off flag. If enabled no SCTP message
  3046. * fragmentation will be performed. Instead if a message being sent
  3047. * exceeds the current PMTU size, the message will NOT be sent and
  3048. * instead a error will be indicated to the user.
  3049. */
  3050. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  3051. char __user *optval, int __user *optlen)
  3052. {
  3053. int val;
  3054. if (len < sizeof(int))
  3055. return -EINVAL;
  3056. len = sizeof(int);
  3057. val = (sctp_sk(sk)->disable_fragments == 1);
  3058. if (put_user(len, optlen))
  3059. return -EFAULT;
  3060. if (copy_to_user(optval, &val, len))
  3061. return -EFAULT;
  3062. return 0;
  3063. }
  3064. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  3065. *
  3066. * This socket option is used to specify various notifications and
  3067. * ancillary data the user wishes to receive.
  3068. */
  3069. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  3070. int __user *optlen)
  3071. {
  3072. if (len < sizeof(struct sctp_event_subscribe))
  3073. return -EINVAL;
  3074. len = sizeof(struct sctp_event_subscribe);
  3075. if (put_user(len, optlen))
  3076. return -EFAULT;
  3077. if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
  3078. return -EFAULT;
  3079. return 0;
  3080. }
  3081. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  3082. *
  3083. * This socket option is applicable to the UDP-style socket only. When
  3084. * set it will cause associations that are idle for more than the
  3085. * specified number of seconds to automatically close. An association
  3086. * being idle is defined an association that has NOT sent or received
  3087. * user data. The special value of '0' indicates that no automatic
  3088. * close of any associations should be performed. The option expects an
  3089. * integer defining the number of seconds of idle time before an
  3090. * association is closed.
  3091. */
  3092. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  3093. {
  3094. /* Applicable to UDP-style socket only */
  3095. if (sctp_style(sk, TCP))
  3096. return -EOPNOTSUPP;
  3097. if (len < sizeof(int))
  3098. return -EINVAL;
  3099. len = sizeof(int);
  3100. if (put_user(len, optlen))
  3101. return -EFAULT;
  3102. if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
  3103. return -EFAULT;
  3104. return 0;
  3105. }
  3106. /* Helper routine to branch off an association to a new socket. */
  3107. SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
  3108. struct socket **sockp)
  3109. {
  3110. struct sock *sk = asoc->base.sk;
  3111. struct socket *sock;
  3112. struct inet_sock *inetsk;
  3113. struct sctp_af *af;
  3114. int err = 0;
  3115. /* An association cannot be branched off from an already peeled-off
  3116. * socket, nor is this supported for tcp style sockets.
  3117. */
  3118. if (!sctp_style(sk, UDP))
  3119. return -EINVAL;
  3120. /* Create a new socket. */
  3121. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  3122. if (err < 0)
  3123. return err;
  3124. /* Populate the fields of the newsk from the oldsk and migrate the
  3125. * asoc to the newsk.
  3126. */
  3127. sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  3128. /* Make peeled-off sockets more like 1-1 accepted sockets.
  3129. * Set the daddr and initialize id to something more random
  3130. */
  3131. af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family);
  3132. af->to_sk_daddr(&asoc->peer.primary_addr, sk);
  3133. inetsk = inet_sk(sock->sk);
  3134. inetsk->id = asoc->next_tsn ^ jiffies;
  3135. *sockp = sock;
  3136. return err;
  3137. }
  3138. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  3139. {
  3140. sctp_peeloff_arg_t peeloff;
  3141. struct socket *newsock;
  3142. int retval = 0;
  3143. struct sctp_association *asoc;
  3144. if (len < sizeof(sctp_peeloff_arg_t))
  3145. return -EINVAL;
  3146. len = sizeof(sctp_peeloff_arg_t);
  3147. if (copy_from_user(&peeloff, optval, len))
  3148. return -EFAULT;
  3149. asoc = sctp_id2assoc(sk, peeloff.associd);
  3150. if (!asoc) {
  3151. retval = -EINVAL;
  3152. goto out;
  3153. }
  3154. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
  3155. retval = sctp_do_peeloff(asoc, &newsock);
  3156. if (retval < 0)
  3157. goto out;
  3158. /* Map the socket to an unused fd that can be returned to the user. */
  3159. retval = sock_map_fd(newsock);
  3160. if (retval < 0) {
  3161. sock_release(newsock);
  3162. goto out;
  3163. }
  3164. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
  3165. __FUNCTION__, sk, asoc, newsock->sk, retval);
  3166. /* Return the fd mapped to the new socket. */
  3167. peeloff.sd = retval;
  3168. if (put_user(len, optlen))
  3169. return -EFAULT;
  3170. if (copy_to_user(optval, &peeloff, len))
  3171. retval = -EFAULT;
  3172. out:
  3173. return retval;
  3174. }
  3175. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  3176. *
  3177. * Applications can enable or disable heartbeats for any peer address of
  3178. * an association, modify an address's heartbeat interval, force a
  3179. * heartbeat to be sent immediately, and adjust the address's maximum
  3180. * number of retransmissions sent before an address is considered
  3181. * unreachable. The following structure is used to access and modify an
  3182. * address's parameters:
  3183. *
  3184. * struct sctp_paddrparams {
  3185. * sctp_assoc_t spp_assoc_id;
  3186. * struct sockaddr_storage spp_address;
  3187. * uint32_t spp_hbinterval;
  3188. * uint16_t spp_pathmaxrxt;
  3189. * uint32_t spp_pathmtu;
  3190. * uint32_t spp_sackdelay;
  3191. * uint32_t spp_flags;
  3192. * };
  3193. *
  3194. * spp_assoc_id - (one-to-many style socket) This is filled in the
  3195. * application, and identifies the association for
  3196. * this query.
  3197. * spp_address - This specifies which address is of interest.
  3198. * spp_hbinterval - This contains the value of the heartbeat interval,
  3199. * in milliseconds. If a value of zero
  3200. * is present in this field then no changes are to
  3201. * be made to this parameter.
  3202. * spp_pathmaxrxt - This contains the maximum number of
  3203. * retransmissions before this address shall be
  3204. * considered unreachable. If a value of zero
  3205. * is present in this field then no changes are to
  3206. * be made to this parameter.
  3207. * spp_pathmtu - When Path MTU discovery is disabled the value
  3208. * specified here will be the "fixed" path mtu.
  3209. * Note that if the spp_address field is empty
  3210. * then all associations on this address will
  3211. * have this fixed path mtu set upon them.
  3212. *
  3213. * spp_sackdelay - When delayed sack is enabled, this value specifies
  3214. * the number of milliseconds that sacks will be delayed
  3215. * for. This value will apply to all addresses of an
  3216. * association if the spp_address field is empty. Note
  3217. * also, that if delayed sack is enabled and this
  3218. * value is set to 0, no change is made to the last
  3219. * recorded delayed sack timer value.
  3220. *
  3221. * spp_flags - These flags are used to control various features
  3222. * on an association. The flag field may contain
  3223. * zero or more of the following options.
  3224. *
  3225. * SPP_HB_ENABLE - Enable heartbeats on the
  3226. * specified address. Note that if the address
  3227. * field is empty all addresses for the association
  3228. * have heartbeats enabled upon them.
  3229. *
  3230. * SPP_HB_DISABLE - Disable heartbeats on the
  3231. * speicifed address. Note that if the address
  3232. * field is empty all addresses for the association
  3233. * will have their heartbeats disabled. Note also
  3234. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  3235. * mutually exclusive, only one of these two should
  3236. * be specified. Enabling both fields will have
  3237. * undetermined results.
  3238. *
  3239. * SPP_HB_DEMAND - Request a user initiated heartbeat
  3240. * to be made immediately.
  3241. *
  3242. * SPP_PMTUD_ENABLE - This field will enable PMTU
  3243. * discovery upon the specified address. Note that
  3244. * if the address feild is empty then all addresses
  3245. * on the association are effected.
  3246. *
  3247. * SPP_PMTUD_DISABLE - This field will disable PMTU
  3248. * discovery upon the specified address. Note that
  3249. * if the address feild is empty then all addresses
  3250. * on the association are effected. Not also that
  3251. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  3252. * exclusive. Enabling both will have undetermined
  3253. * results.
  3254. *
  3255. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  3256. * on delayed sack. The time specified in spp_sackdelay
  3257. * is used to specify the sack delay for this address. Note
  3258. * that if spp_address is empty then all addresses will
  3259. * enable delayed sack and take on the sack delay
  3260. * value specified in spp_sackdelay.
  3261. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  3262. * off delayed sack. If the spp_address field is blank then
  3263. * delayed sack is disabled for the entire association. Note
  3264. * also that this field is mutually exclusive to
  3265. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  3266. * results.
  3267. */
  3268. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  3269. char __user *optval, int __user *optlen)
  3270. {
  3271. struct sctp_paddrparams params;
  3272. struct sctp_transport *trans = NULL;
  3273. struct sctp_association *asoc = NULL;
  3274. struct sctp_sock *sp = sctp_sk(sk);
  3275. if (len < sizeof(struct sctp_paddrparams))
  3276. return -EINVAL;
  3277. len = sizeof(struct sctp_paddrparams);
  3278. if (copy_from_user(&params, optval, len))
  3279. return -EFAULT;
  3280. /* If an address other than INADDR_ANY is specified, and
  3281. * no transport is found, then the request is invalid.
  3282. */
  3283. if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
  3284. trans = sctp_addr_id2transport(sk, &params.spp_address,
  3285. params.spp_assoc_id);
  3286. if (!trans) {
  3287. SCTP_DEBUG_PRINTK("Failed no transport\n");
  3288. return -EINVAL;
  3289. }
  3290. }
  3291. /* Get association, if assoc_id != 0 and the socket is a one
  3292. * to many style socket, and an association was not found, then
  3293. * the id was invalid.
  3294. */
  3295. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  3296. if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
  3297. SCTP_DEBUG_PRINTK("Failed no association\n");
  3298. return -EINVAL;
  3299. }
  3300. if (trans) {
  3301. /* Fetch transport values. */
  3302. params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
  3303. params.spp_pathmtu = trans->pathmtu;
  3304. params.spp_pathmaxrxt = trans->pathmaxrxt;
  3305. params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
  3306. /*draft-11 doesn't say what to return in spp_flags*/
  3307. params.spp_flags = trans->param_flags;
  3308. } else if (asoc) {
  3309. /* Fetch association values. */
  3310. params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
  3311. params.spp_pathmtu = asoc->pathmtu;
  3312. params.spp_pathmaxrxt = asoc->pathmaxrxt;
  3313. params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
  3314. /*draft-11 doesn't say what to return in spp_flags*/
  3315. params.spp_flags = asoc->param_flags;
  3316. } else {
  3317. /* Fetch socket values. */
  3318. params.spp_hbinterval = sp->hbinterval;
  3319. params.spp_pathmtu = sp->pathmtu;
  3320. params.spp_sackdelay = sp->sackdelay;
  3321. params.spp_pathmaxrxt = sp->pathmaxrxt;
  3322. /*draft-11 doesn't say what to return in spp_flags*/
  3323. params.spp_flags = sp->param_flags;
  3324. }
  3325. if (copy_to_user(optval, &params, len))
  3326. return -EFAULT;
  3327. if (put_user(len, optlen))
  3328. return -EFAULT;
  3329. return 0;
  3330. }
  3331. /* 7.1.23. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
  3332. *
  3333. * This options will get or set the delayed ack timer. The time is set
  3334. * in milliseconds. If the assoc_id is 0, then this sets or gets the
  3335. * endpoints default delayed ack timer value. If the assoc_id field is
  3336. * non-zero, then the set or get effects the specified association.
  3337. *
  3338. * struct sctp_assoc_value {
  3339. * sctp_assoc_t assoc_id;
  3340. * uint32_t assoc_value;
  3341. * };
  3342. *
  3343. * assoc_id - This parameter, indicates which association the
  3344. * user is preforming an action upon. Note that if
  3345. * this field's value is zero then the endpoints
  3346. * default value is changed (effecting future
  3347. * associations only).
  3348. *
  3349. * assoc_value - This parameter contains the number of milliseconds
  3350. * that the user is requesting the delayed ACK timer
  3351. * be set to. Note that this value is defined in
  3352. * the standard to be between 200 and 500 milliseconds.
  3353. *
  3354. * Note: a value of zero will leave the value alone,
  3355. * but disable SACK delay. A non-zero value will also
  3356. * enable SACK delay.
  3357. */
  3358. static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
  3359. char __user *optval,
  3360. int __user *optlen)
  3361. {
  3362. struct sctp_assoc_value params;
  3363. struct sctp_association *asoc = NULL;
  3364. struct sctp_sock *sp = sctp_sk(sk);
  3365. if (len < sizeof(struct sctp_assoc_value))
  3366. return - EINVAL;
  3367. len = sizeof(struct sctp_assoc_value);
  3368. if (copy_from_user(&params, optval, len))
  3369. return -EFAULT;
  3370. /* Get association, if assoc_id != 0 and the socket is a one
  3371. * to many style socket, and an association was not found, then
  3372. * the id was invalid.
  3373. */
  3374. asoc = sctp_id2assoc(sk, params.assoc_id);
  3375. if (!asoc && params.assoc_id && sctp_style(sk, UDP))
  3376. return -EINVAL;
  3377. if (asoc) {
  3378. /* Fetch association values. */
  3379. if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
  3380. params.assoc_value = jiffies_to_msecs(
  3381. asoc->sackdelay);
  3382. else
  3383. params.assoc_value = 0;
  3384. } else {
  3385. /* Fetch socket values. */
  3386. if (sp->param_flags & SPP_SACKDELAY_ENABLE)
  3387. params.assoc_value = sp->sackdelay;
  3388. else
  3389. params.assoc_value = 0;
  3390. }
  3391. if (copy_to_user(optval, &params, len))
  3392. return -EFAULT;
  3393. if (put_user(len, optlen))
  3394. return -EFAULT;
  3395. return 0;
  3396. }
  3397. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  3398. *
  3399. * Applications can specify protocol parameters for the default association
  3400. * initialization. The option name argument to setsockopt() and getsockopt()
  3401. * is SCTP_INITMSG.
  3402. *
  3403. * Setting initialization parameters is effective only on an unconnected
  3404. * socket (for UDP-style sockets only future associations are effected
  3405. * by the change). With TCP-style sockets, this option is inherited by
  3406. * sockets derived from a listener socket.
  3407. */
  3408. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  3409. {
  3410. if (len < sizeof(struct sctp_initmsg))
  3411. return -EINVAL;
  3412. len = sizeof(struct sctp_initmsg);
  3413. if (put_user(len, optlen))
  3414. return -EFAULT;
  3415. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  3416. return -EFAULT;
  3417. return 0;
  3418. }
  3419. static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
  3420. char __user *optval,
  3421. int __user *optlen)
  3422. {
  3423. sctp_assoc_t id;
  3424. struct sctp_association *asoc;
  3425. struct list_head *pos;
  3426. int cnt = 0;
  3427. if (len < sizeof(sctp_assoc_t))
  3428. return -EINVAL;
  3429. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  3430. return -EFAULT;
  3431. /* For UDP-style sockets, id specifies the association to query. */
  3432. asoc = sctp_id2assoc(sk, id);
  3433. if (!asoc)
  3434. return -EINVAL;
  3435. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3436. cnt ++;
  3437. }
  3438. return cnt;
  3439. }
  3440. /*
  3441. * Old API for getting list of peer addresses. Does not work for 32-bit
  3442. * programs running on a 64-bit kernel
  3443. */
  3444. static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
  3445. char __user *optval,
  3446. int __user *optlen)
  3447. {
  3448. struct sctp_association *asoc;
  3449. struct list_head *pos;
  3450. int cnt = 0;
  3451. struct sctp_getaddrs_old getaddrs;
  3452. struct sctp_transport *from;
  3453. void __user *to;
  3454. union sctp_addr temp;
  3455. struct sctp_sock *sp = sctp_sk(sk);
  3456. int addrlen;
  3457. if (len < sizeof(struct sctp_getaddrs_old))
  3458. return -EINVAL;
  3459. len = sizeof(struct sctp_getaddrs_old);
  3460. if (copy_from_user(&getaddrs, optval, len))
  3461. return -EFAULT;
  3462. if (getaddrs.addr_num <= 0) return -EINVAL;
  3463. /* For UDP-style sockets, id specifies the association to query. */
  3464. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3465. if (!asoc)
  3466. return -EINVAL;
  3467. to = (void __user *)getaddrs.addrs;
  3468. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3469. from = list_entry(pos, struct sctp_transport, transports);
  3470. memcpy(&temp, &from->ipaddr, sizeof(temp));
  3471. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3472. addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
  3473. if (copy_to_user(to, &temp, addrlen))
  3474. return -EFAULT;
  3475. to += addrlen ;
  3476. cnt ++;
  3477. if (cnt >= getaddrs.addr_num) break;
  3478. }
  3479. getaddrs.addr_num = cnt;
  3480. if (put_user(len, optlen))
  3481. return -EFAULT;
  3482. if (copy_to_user(optval, &getaddrs, len))
  3483. return -EFAULT;
  3484. return 0;
  3485. }
  3486. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  3487. char __user *optval, int __user *optlen)
  3488. {
  3489. struct sctp_association *asoc;
  3490. struct list_head *pos;
  3491. int cnt = 0;
  3492. struct sctp_getaddrs getaddrs;
  3493. struct sctp_transport *from;
  3494. void __user *to;
  3495. union sctp_addr temp;
  3496. struct sctp_sock *sp = sctp_sk(sk);
  3497. int addrlen;
  3498. size_t space_left;
  3499. int bytes_copied;
  3500. if (len < sizeof(struct sctp_getaddrs))
  3501. return -EINVAL;
  3502. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  3503. return -EFAULT;
  3504. /* For UDP-style sockets, id specifies the association to query. */
  3505. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3506. if (!asoc)
  3507. return -EINVAL;
  3508. to = optval + offsetof(struct sctp_getaddrs,addrs);
  3509. space_left = len - offsetof(struct sctp_getaddrs,addrs);
  3510. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3511. from = list_entry(pos, struct sctp_transport, transports);
  3512. memcpy(&temp, &from->ipaddr, sizeof(temp));
  3513. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3514. addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
  3515. if (space_left < addrlen)
  3516. return -ENOMEM;
  3517. if (copy_to_user(to, &temp, addrlen))
  3518. return -EFAULT;
  3519. to += addrlen;
  3520. cnt++;
  3521. space_left -= addrlen;
  3522. }
  3523. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  3524. return -EFAULT;
  3525. bytes_copied = ((char __user *)to) - optval;
  3526. if (put_user(bytes_copied, optlen))
  3527. return -EFAULT;
  3528. return 0;
  3529. }
  3530. static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
  3531. char __user *optval,
  3532. int __user *optlen)
  3533. {
  3534. sctp_assoc_t id;
  3535. struct sctp_bind_addr *bp;
  3536. struct sctp_association *asoc;
  3537. struct sctp_sockaddr_entry *addr;
  3538. int cnt = 0;
  3539. if (len < sizeof(sctp_assoc_t))
  3540. return -EINVAL;
  3541. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  3542. return -EFAULT;
  3543. /*
  3544. * For UDP-style sockets, id specifies the association to query.
  3545. * If the id field is set to the value '0' then the locally bound
  3546. * addresses are returned without regard to any particular
  3547. * association.
  3548. */
  3549. if (0 == id) {
  3550. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3551. } else {
  3552. asoc = sctp_id2assoc(sk, id);
  3553. if (!asoc)
  3554. return -EINVAL;
  3555. bp = &asoc->base.bind_addr;
  3556. }
  3557. /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
  3558. * addresses from the global local address list.
  3559. */
  3560. if (sctp_list_single_entry(&bp->address_list)) {
  3561. addr = list_entry(bp->address_list.next,
  3562. struct sctp_sockaddr_entry, list);
  3563. if (sctp_is_any(&addr->a)) {
  3564. rcu_read_lock();
  3565. list_for_each_entry_rcu(addr,
  3566. &sctp_local_addr_list, list) {
  3567. if (!addr->valid)
  3568. continue;
  3569. if ((PF_INET == sk->sk_family) &&
  3570. (AF_INET6 == addr->a.sa.sa_family))
  3571. continue;
  3572. cnt++;
  3573. }
  3574. rcu_read_unlock();
  3575. } else {
  3576. cnt = 1;
  3577. }
  3578. goto done;
  3579. }
  3580. /* Protection on the bound address list is not needed,
  3581. * since in the socket option context we hold the socket lock,
  3582. * so there is no way that the bound address list can change.
  3583. */
  3584. list_for_each_entry(addr, &bp->address_list, list) {
  3585. cnt ++;
  3586. }
  3587. done:
  3588. return cnt;
  3589. }
  3590. /* Helper function that copies local addresses to user and returns the number
  3591. * of addresses copied.
  3592. */
  3593. static int sctp_copy_laddrs_old(struct sock *sk, __u16 port,
  3594. int max_addrs, void *to,
  3595. int *bytes_copied)
  3596. {
  3597. struct sctp_sockaddr_entry *addr;
  3598. union sctp_addr temp;
  3599. int cnt = 0;
  3600. int addrlen;
  3601. rcu_read_lock();
  3602. list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
  3603. if (!addr->valid)
  3604. continue;
  3605. if ((PF_INET == sk->sk_family) &&
  3606. (AF_INET6 == addr->a.sa.sa_family))
  3607. continue;
  3608. memcpy(&temp, &addr->a, sizeof(temp));
  3609. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  3610. &temp);
  3611. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3612. memcpy(to, &temp, addrlen);
  3613. to += addrlen;
  3614. *bytes_copied += addrlen;
  3615. cnt ++;
  3616. if (cnt >= max_addrs) break;
  3617. }
  3618. rcu_read_unlock();
  3619. return cnt;
  3620. }
  3621. static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
  3622. size_t space_left, int *bytes_copied)
  3623. {
  3624. struct sctp_sockaddr_entry *addr;
  3625. union sctp_addr temp;
  3626. int cnt = 0;
  3627. int addrlen;
  3628. rcu_read_lock();
  3629. list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) {
  3630. if (!addr->valid)
  3631. continue;
  3632. if ((PF_INET == sk->sk_family) &&
  3633. (AF_INET6 == addr->a.sa.sa_family))
  3634. continue;
  3635. memcpy(&temp, &addr->a, sizeof(temp));
  3636. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  3637. &temp);
  3638. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3639. if (space_left < addrlen) {
  3640. cnt = -ENOMEM;
  3641. break;
  3642. }
  3643. memcpy(to, &temp, addrlen);
  3644. to += addrlen;
  3645. cnt ++;
  3646. space_left -= addrlen;
  3647. *bytes_copied += addrlen;
  3648. }
  3649. rcu_read_unlock();
  3650. return cnt;
  3651. }
  3652. /* Old API for getting list of local addresses. Does not work for 32-bit
  3653. * programs running on a 64-bit kernel
  3654. */
  3655. static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
  3656. char __user *optval, int __user *optlen)
  3657. {
  3658. struct sctp_bind_addr *bp;
  3659. struct sctp_association *asoc;
  3660. int cnt = 0;
  3661. struct sctp_getaddrs_old getaddrs;
  3662. struct sctp_sockaddr_entry *addr;
  3663. void __user *to;
  3664. union sctp_addr temp;
  3665. struct sctp_sock *sp = sctp_sk(sk);
  3666. int addrlen;
  3667. int err = 0;
  3668. void *addrs;
  3669. void *buf;
  3670. int bytes_copied = 0;
  3671. if (len < sizeof(struct sctp_getaddrs_old))
  3672. return -EINVAL;
  3673. len = sizeof(struct sctp_getaddrs_old);
  3674. if (copy_from_user(&getaddrs, optval, len))
  3675. return -EFAULT;
  3676. if (getaddrs.addr_num <= 0) return -EINVAL;
  3677. /*
  3678. * For UDP-style sockets, id specifies the association to query.
  3679. * If the id field is set to the value '0' then the locally bound
  3680. * addresses are returned without regard to any particular
  3681. * association.
  3682. */
  3683. if (0 == getaddrs.assoc_id) {
  3684. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3685. } else {
  3686. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3687. if (!asoc)
  3688. return -EINVAL;
  3689. bp = &asoc->base.bind_addr;
  3690. }
  3691. to = getaddrs.addrs;
  3692. /* Allocate space for a local instance of packed array to hold all
  3693. * the data. We store addresses here first and then put write them
  3694. * to the user in one shot.
  3695. */
  3696. addrs = kmalloc(sizeof(union sctp_addr) * getaddrs.addr_num,
  3697. GFP_KERNEL);
  3698. if (!addrs)
  3699. return -ENOMEM;
  3700. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  3701. * addresses from the global local address list.
  3702. */
  3703. if (sctp_list_single_entry(&bp->address_list)) {
  3704. addr = list_entry(bp->address_list.next,
  3705. struct sctp_sockaddr_entry, list);
  3706. if (sctp_is_any(&addr->a)) {
  3707. cnt = sctp_copy_laddrs_old(sk, bp->port,
  3708. getaddrs.addr_num,
  3709. addrs, &bytes_copied);
  3710. goto copy_getaddrs;
  3711. }
  3712. }
  3713. buf = addrs;
  3714. /* Protection on the bound address list is not needed since
  3715. * in the socket option context we hold a socket lock and
  3716. * thus the bound address list can't change.
  3717. */
  3718. list_for_each_entry(addr, &bp->address_list, list) {
  3719. memcpy(&temp, &addr->a, sizeof(temp));
  3720. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3721. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3722. memcpy(buf, &temp, addrlen);
  3723. buf += addrlen;
  3724. bytes_copied += addrlen;
  3725. cnt ++;
  3726. if (cnt >= getaddrs.addr_num) break;
  3727. }
  3728. copy_getaddrs:
  3729. /* copy the entire address list into the user provided space */
  3730. if (copy_to_user(to, addrs, bytes_copied)) {
  3731. err = -EFAULT;
  3732. goto error;
  3733. }
  3734. /* copy the leading structure back to user */
  3735. getaddrs.addr_num = cnt;
  3736. if (copy_to_user(optval, &getaddrs, len))
  3737. err = -EFAULT;
  3738. error:
  3739. kfree(addrs);
  3740. return err;
  3741. }
  3742. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  3743. char __user *optval, int __user *optlen)
  3744. {
  3745. struct sctp_bind_addr *bp;
  3746. struct sctp_association *asoc;
  3747. int cnt = 0;
  3748. struct sctp_getaddrs getaddrs;
  3749. struct sctp_sockaddr_entry *addr;
  3750. void __user *to;
  3751. union sctp_addr temp;
  3752. struct sctp_sock *sp = sctp_sk(sk);
  3753. int addrlen;
  3754. int err = 0;
  3755. size_t space_left;
  3756. int bytes_copied = 0;
  3757. void *addrs;
  3758. void *buf;
  3759. if (len < sizeof(struct sctp_getaddrs))
  3760. return -EINVAL;
  3761. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  3762. return -EFAULT;
  3763. /*
  3764. * For UDP-style sockets, id specifies the association to query.
  3765. * If the id field is set to the value '0' then the locally bound
  3766. * addresses are returned without regard to any particular
  3767. * association.
  3768. */
  3769. if (0 == getaddrs.assoc_id) {
  3770. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3771. } else {
  3772. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3773. if (!asoc)
  3774. return -EINVAL;
  3775. bp = &asoc->base.bind_addr;
  3776. }
  3777. to = optval + offsetof(struct sctp_getaddrs,addrs);
  3778. space_left = len - offsetof(struct sctp_getaddrs,addrs);
  3779. addrs = kmalloc(space_left, GFP_KERNEL);
  3780. if (!addrs)
  3781. return -ENOMEM;
  3782. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  3783. * addresses from the global local address list.
  3784. */
  3785. if (sctp_list_single_entry(&bp->address_list)) {
  3786. addr = list_entry(bp->address_list.next,
  3787. struct sctp_sockaddr_entry, list);
  3788. if (sctp_is_any(&addr->a)) {
  3789. cnt = sctp_copy_laddrs(sk, bp->port, addrs,
  3790. space_left, &bytes_copied);
  3791. if (cnt < 0) {
  3792. err = cnt;
  3793. goto out;
  3794. }
  3795. goto copy_getaddrs;
  3796. }
  3797. }
  3798. buf = addrs;
  3799. /* Protection on the bound address list is not needed since
  3800. * in the socket option context we hold a socket lock and
  3801. * thus the bound address list can't change.
  3802. */
  3803. list_for_each_entry(addr, &bp->address_list, list) {
  3804. memcpy(&temp, &addr->a, sizeof(temp));
  3805. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3806. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3807. if (space_left < addrlen) {
  3808. err = -ENOMEM; /*fixme: right error?*/
  3809. goto out;
  3810. }
  3811. memcpy(buf, &temp, addrlen);
  3812. buf += addrlen;
  3813. bytes_copied += addrlen;
  3814. cnt ++;
  3815. space_left -= addrlen;
  3816. }
  3817. copy_getaddrs:
  3818. if (copy_to_user(to, addrs, bytes_copied)) {
  3819. err = -EFAULT;
  3820. goto out;
  3821. }
  3822. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
  3823. err = -EFAULT;
  3824. goto out;
  3825. }
  3826. if (put_user(bytes_copied, optlen))
  3827. err = -EFAULT;
  3828. out:
  3829. kfree(addrs);
  3830. return err;
  3831. }
  3832. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  3833. *
  3834. * Requests that the local SCTP stack use the enclosed peer address as
  3835. * the association primary. The enclosed address must be one of the
  3836. * association peer's addresses.
  3837. */
  3838. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  3839. char __user *optval, int __user *optlen)
  3840. {
  3841. struct sctp_prim prim;
  3842. struct sctp_association *asoc;
  3843. struct sctp_sock *sp = sctp_sk(sk);
  3844. if (len < sizeof(struct sctp_prim))
  3845. return -EINVAL;
  3846. len = sizeof(struct sctp_prim);
  3847. if (copy_from_user(&prim, optval, len))
  3848. return -EFAULT;
  3849. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  3850. if (!asoc)
  3851. return -EINVAL;
  3852. if (!asoc->peer.primary_path)
  3853. return -ENOTCONN;
  3854. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  3855. asoc->peer.primary_path->af_specific->sockaddr_len);
  3856. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
  3857. (union sctp_addr *)&prim.ssp_addr);
  3858. if (put_user(len, optlen))
  3859. return -EFAULT;
  3860. if (copy_to_user(optval, &prim, len))
  3861. return -EFAULT;
  3862. return 0;
  3863. }
  3864. /*
  3865. * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
  3866. *
  3867. * Requests that the local endpoint set the specified Adaptation Layer
  3868. * Indication parameter for all future INIT and INIT-ACK exchanges.
  3869. */
  3870. static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
  3871. char __user *optval, int __user *optlen)
  3872. {
  3873. struct sctp_setadaptation adaptation;
  3874. if (len < sizeof(struct sctp_setadaptation))
  3875. return -EINVAL;
  3876. len = sizeof(struct sctp_setadaptation);
  3877. adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
  3878. if (put_user(len, optlen))
  3879. return -EFAULT;
  3880. if (copy_to_user(optval, &adaptation, len))
  3881. return -EFAULT;
  3882. return 0;
  3883. }
  3884. /*
  3885. *
  3886. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  3887. *
  3888. * Applications that wish to use the sendto() system call may wish to
  3889. * specify a default set of parameters that would normally be supplied
  3890. * through the inclusion of ancillary data. This socket option allows
  3891. * such an application to set the default sctp_sndrcvinfo structure.
  3892. * The application that wishes to use this socket option simply passes
  3893. * in to this call the sctp_sndrcvinfo structure defined in Section
  3894. * 5.2.2) The input parameters accepted by this call include
  3895. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  3896. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  3897. * to this call if the caller is using the UDP model.
  3898. *
  3899. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  3900. */
  3901. static int sctp_getsockopt_default_send_param(struct sock *sk,
  3902. int len, char __user *optval,
  3903. int __user *optlen)
  3904. {
  3905. struct sctp_sndrcvinfo info;
  3906. struct sctp_association *asoc;
  3907. struct sctp_sock *sp = sctp_sk(sk);
  3908. if (len < sizeof(struct sctp_sndrcvinfo))
  3909. return -EINVAL;
  3910. len = sizeof(struct sctp_sndrcvinfo);
  3911. if (copy_from_user(&info, optval, len))
  3912. return -EFAULT;
  3913. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  3914. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  3915. return -EINVAL;
  3916. if (asoc) {
  3917. info.sinfo_stream = asoc->default_stream;
  3918. info.sinfo_flags = asoc->default_flags;
  3919. info.sinfo_ppid = asoc->default_ppid;
  3920. info.sinfo_context = asoc->default_context;
  3921. info.sinfo_timetolive = asoc->default_timetolive;
  3922. } else {
  3923. info.sinfo_stream = sp->default_stream;
  3924. info.sinfo_flags = sp->default_flags;
  3925. info.sinfo_ppid = sp->default_ppid;
  3926. info.sinfo_context = sp->default_context;
  3927. info.sinfo_timetolive = sp->default_timetolive;
  3928. }
  3929. if (put_user(len, optlen))
  3930. return -EFAULT;
  3931. if (copy_to_user(optval, &info, len))
  3932. return -EFAULT;
  3933. return 0;
  3934. }
  3935. /*
  3936. *
  3937. * 7.1.5 SCTP_NODELAY
  3938. *
  3939. * Turn on/off any Nagle-like algorithm. This means that packets are
  3940. * generally sent as soon as possible and no unnecessary delays are
  3941. * introduced, at the cost of more packets in the network. Expects an
  3942. * integer boolean flag.
  3943. */
  3944. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  3945. char __user *optval, int __user *optlen)
  3946. {
  3947. int val;
  3948. if (len < sizeof(int))
  3949. return -EINVAL;
  3950. len = sizeof(int);
  3951. val = (sctp_sk(sk)->nodelay == 1);
  3952. if (put_user(len, optlen))
  3953. return -EFAULT;
  3954. if (copy_to_user(optval, &val, len))
  3955. return -EFAULT;
  3956. return 0;
  3957. }
  3958. /*
  3959. *
  3960. * 7.1.1 SCTP_RTOINFO
  3961. *
  3962. * The protocol parameters used to initialize and bound retransmission
  3963. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  3964. * and modify these parameters.
  3965. * All parameters are time values, in milliseconds. A value of 0, when
  3966. * modifying the parameters, indicates that the current value should not
  3967. * be changed.
  3968. *
  3969. */
  3970. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  3971. char __user *optval,
  3972. int __user *optlen) {
  3973. struct sctp_rtoinfo rtoinfo;
  3974. struct sctp_association *asoc;
  3975. if (len < sizeof (struct sctp_rtoinfo))
  3976. return -EINVAL;
  3977. len = sizeof(struct sctp_rtoinfo);
  3978. if (copy_from_user(&rtoinfo, optval, len))
  3979. return -EFAULT;
  3980. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  3981. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  3982. return -EINVAL;
  3983. /* Values corresponding to the specific association. */
  3984. if (asoc) {
  3985. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  3986. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  3987. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  3988. } else {
  3989. /* Values corresponding to the endpoint. */
  3990. struct sctp_sock *sp = sctp_sk(sk);
  3991. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  3992. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  3993. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  3994. }
  3995. if (put_user(len, optlen))
  3996. return -EFAULT;
  3997. if (copy_to_user(optval, &rtoinfo, len))
  3998. return -EFAULT;
  3999. return 0;
  4000. }
  4001. /*
  4002. *
  4003. * 7.1.2 SCTP_ASSOCINFO
  4004. *
  4005. * This option is used to tune the maximum retransmission attempts
  4006. * of the association.
  4007. * Returns an error if the new association retransmission value is
  4008. * greater than the sum of the retransmission value of the peer.
  4009. * See [SCTP] for more information.
  4010. *
  4011. */
  4012. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  4013. char __user *optval,
  4014. int __user *optlen)
  4015. {
  4016. struct sctp_assocparams assocparams;
  4017. struct sctp_association *asoc;
  4018. struct list_head *pos;
  4019. int cnt = 0;
  4020. if (len < sizeof (struct sctp_assocparams))
  4021. return -EINVAL;
  4022. len = sizeof(struct sctp_assocparams);
  4023. if (copy_from_user(&assocparams, optval, len))
  4024. return -EFAULT;
  4025. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  4026. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  4027. return -EINVAL;
  4028. /* Values correspoinding to the specific association */
  4029. if (asoc) {
  4030. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  4031. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  4032. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  4033. assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
  4034. * 1000) +
  4035. (asoc->cookie_life.tv_usec
  4036. / 1000);
  4037. list_for_each(pos, &asoc->peer.transport_addr_list) {
  4038. cnt ++;
  4039. }
  4040. assocparams.sasoc_number_peer_destinations = cnt;
  4041. } else {
  4042. /* Values corresponding to the endpoint */
  4043. struct sctp_sock *sp = sctp_sk(sk);
  4044. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  4045. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  4046. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  4047. assocparams.sasoc_cookie_life =
  4048. sp->assocparams.sasoc_cookie_life;
  4049. assocparams.sasoc_number_peer_destinations =
  4050. sp->assocparams.
  4051. sasoc_number_peer_destinations;
  4052. }
  4053. if (put_user(len, optlen))
  4054. return -EFAULT;
  4055. if (copy_to_user(optval, &assocparams, len))
  4056. return -EFAULT;
  4057. return 0;
  4058. }
  4059. /*
  4060. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  4061. *
  4062. * This socket option is a boolean flag which turns on or off mapped V4
  4063. * addresses. If this option is turned on and the socket is type
  4064. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  4065. * If this option is turned off, then no mapping will be done of V4
  4066. * addresses and a user will receive both PF_INET6 and PF_INET type
  4067. * addresses on the socket.
  4068. */
  4069. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  4070. char __user *optval, int __user *optlen)
  4071. {
  4072. int val;
  4073. struct sctp_sock *sp = sctp_sk(sk);
  4074. if (len < sizeof(int))
  4075. return -EINVAL;
  4076. len = sizeof(int);
  4077. val = sp->v4mapped;
  4078. if (put_user(len, optlen))
  4079. return -EFAULT;
  4080. if (copy_to_user(optval, &val, len))
  4081. return -EFAULT;
  4082. return 0;
  4083. }
  4084. /*
  4085. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  4086. * (chapter and verse is quoted at sctp_setsockopt_context())
  4087. */
  4088. static int sctp_getsockopt_context(struct sock *sk, int len,
  4089. char __user *optval, int __user *optlen)
  4090. {
  4091. struct sctp_assoc_value params;
  4092. struct sctp_sock *sp;
  4093. struct sctp_association *asoc;
  4094. if (len < sizeof(struct sctp_assoc_value))
  4095. return -EINVAL;
  4096. len = sizeof(struct sctp_assoc_value);
  4097. if (copy_from_user(&params, optval, len))
  4098. return -EFAULT;
  4099. sp = sctp_sk(sk);
  4100. if (params.assoc_id != 0) {
  4101. asoc = sctp_id2assoc(sk, params.assoc_id);
  4102. if (!asoc)
  4103. return -EINVAL;
  4104. params.assoc_value = asoc->default_rcv_context;
  4105. } else {
  4106. params.assoc_value = sp->default_rcv_context;
  4107. }
  4108. if (put_user(len, optlen))
  4109. return -EFAULT;
  4110. if (copy_to_user(optval, &params, len))
  4111. return -EFAULT;
  4112. return 0;
  4113. }
  4114. /*
  4115. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  4116. *
  4117. * This socket option specifies the maximum size to put in any outgoing
  4118. * SCTP chunk. If a message is larger than this size it will be
  4119. * fragmented by SCTP into the specified size. Note that the underlying
  4120. * SCTP implementation may fragment into smaller sized chunks when the
  4121. * PMTU of the underlying association is smaller than the value set by
  4122. * the user.
  4123. */
  4124. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  4125. char __user *optval, int __user *optlen)
  4126. {
  4127. int val;
  4128. if (len < sizeof(int))
  4129. return -EINVAL;
  4130. len = sizeof(int);
  4131. val = sctp_sk(sk)->user_frag;
  4132. if (put_user(len, optlen))
  4133. return -EFAULT;
  4134. if (copy_to_user(optval, &val, len))
  4135. return -EFAULT;
  4136. return 0;
  4137. }
  4138. /*
  4139. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  4140. * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
  4141. */
  4142. static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
  4143. char __user *optval, int __user *optlen)
  4144. {
  4145. int val;
  4146. if (len < sizeof(int))
  4147. return -EINVAL;
  4148. len = sizeof(int);
  4149. val = sctp_sk(sk)->frag_interleave;
  4150. if (put_user(len, optlen))
  4151. return -EFAULT;
  4152. if (copy_to_user(optval, &val, len))
  4153. return -EFAULT;
  4154. return 0;
  4155. }
  4156. /*
  4157. * 7.1.25. Set or Get the sctp partial delivery point
  4158. * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
  4159. */
  4160. static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
  4161. char __user *optval,
  4162. int __user *optlen)
  4163. {
  4164. u32 val;
  4165. if (len < sizeof(u32))
  4166. return -EINVAL;
  4167. len = sizeof(u32);
  4168. val = sctp_sk(sk)->pd_point;
  4169. if (put_user(len, optlen))
  4170. return -EFAULT;
  4171. if (copy_to_user(optval, &val, len))
  4172. return -EFAULT;
  4173. return -ENOTSUPP;
  4174. }
  4175. /*
  4176. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  4177. * (chapter and verse is quoted at sctp_setsockopt_maxburst())
  4178. */
  4179. static int sctp_getsockopt_maxburst(struct sock *sk, int len,
  4180. char __user *optval,
  4181. int __user *optlen)
  4182. {
  4183. int val;
  4184. if (len < sizeof(int))
  4185. return -EINVAL;
  4186. len = sizeof(int);
  4187. val = sctp_sk(sk)->max_burst;
  4188. if (put_user(len, optlen))
  4189. return -EFAULT;
  4190. if (copy_to_user(optval, &val, len))
  4191. return -EFAULT;
  4192. return -ENOTSUPP;
  4193. }
  4194. SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
  4195. char __user *optval, int __user *optlen)
  4196. {
  4197. int retval = 0;
  4198. int len;
  4199. SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
  4200. sk, optname);
  4201. /* I can hardly begin to describe how wrong this is. This is
  4202. * so broken as to be worse than useless. The API draft
  4203. * REALLY is NOT helpful here... I am not convinced that the
  4204. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  4205. * are at all well-founded.
  4206. */
  4207. if (level != SOL_SCTP) {
  4208. struct sctp_af *af = sctp_sk(sk)->pf->af;
  4209. retval = af->getsockopt(sk, level, optname, optval, optlen);
  4210. return retval;
  4211. }
  4212. if (get_user(len, optlen))
  4213. return -EFAULT;
  4214. sctp_lock_sock(sk);
  4215. switch (optname) {
  4216. case SCTP_STATUS:
  4217. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  4218. break;
  4219. case SCTP_DISABLE_FRAGMENTS:
  4220. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  4221. optlen);
  4222. break;
  4223. case SCTP_EVENTS:
  4224. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  4225. break;
  4226. case SCTP_AUTOCLOSE:
  4227. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  4228. break;
  4229. case SCTP_SOCKOPT_PEELOFF:
  4230. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  4231. break;
  4232. case SCTP_PEER_ADDR_PARAMS:
  4233. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  4234. optlen);
  4235. break;
  4236. case SCTP_DELAYED_ACK_TIME:
  4237. retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
  4238. optlen);
  4239. break;
  4240. case SCTP_INITMSG:
  4241. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  4242. break;
  4243. case SCTP_GET_PEER_ADDRS_NUM_OLD:
  4244. retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
  4245. optlen);
  4246. break;
  4247. case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
  4248. retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
  4249. optlen);
  4250. break;
  4251. case SCTP_GET_PEER_ADDRS_OLD:
  4252. retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
  4253. optlen);
  4254. break;
  4255. case SCTP_GET_LOCAL_ADDRS_OLD:
  4256. retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
  4257. optlen);
  4258. break;
  4259. case SCTP_GET_PEER_ADDRS:
  4260. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  4261. optlen);
  4262. break;
  4263. case SCTP_GET_LOCAL_ADDRS:
  4264. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  4265. optlen);
  4266. break;
  4267. case SCTP_DEFAULT_SEND_PARAM:
  4268. retval = sctp_getsockopt_default_send_param(sk, len,
  4269. optval, optlen);
  4270. break;
  4271. case SCTP_PRIMARY_ADDR:
  4272. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  4273. break;
  4274. case SCTP_NODELAY:
  4275. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  4276. break;
  4277. case SCTP_RTOINFO:
  4278. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  4279. break;
  4280. case SCTP_ASSOCINFO:
  4281. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  4282. break;
  4283. case SCTP_I_WANT_MAPPED_V4_ADDR:
  4284. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  4285. break;
  4286. case SCTP_MAXSEG:
  4287. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  4288. break;
  4289. case SCTP_GET_PEER_ADDR_INFO:
  4290. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  4291. optlen);
  4292. break;
  4293. case SCTP_ADAPTATION_LAYER:
  4294. retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
  4295. optlen);
  4296. break;
  4297. case SCTP_CONTEXT:
  4298. retval = sctp_getsockopt_context(sk, len, optval, optlen);
  4299. break;
  4300. case SCTP_FRAGMENT_INTERLEAVE:
  4301. retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
  4302. optlen);
  4303. break;
  4304. case SCTP_PARTIAL_DELIVERY_POINT:
  4305. retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
  4306. optlen);
  4307. break;
  4308. case SCTP_MAX_BURST:
  4309. retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
  4310. break;
  4311. default:
  4312. retval = -ENOPROTOOPT;
  4313. break;
  4314. }
  4315. sctp_release_sock(sk);
  4316. return retval;
  4317. }
  4318. static void sctp_hash(struct sock *sk)
  4319. {
  4320. /* STUB */
  4321. }
  4322. static void sctp_unhash(struct sock *sk)
  4323. {
  4324. /* STUB */
  4325. }
  4326. /* Check if port is acceptable. Possibly find first available port.
  4327. *
  4328. * The port hash table (contained in the 'global' SCTP protocol storage
  4329. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  4330. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  4331. * list (the list number is the port number hashed out, so as you
  4332. * would expect from a hash function, all the ports in a given list have
  4333. * such a number that hashes out to the same list number; you were
  4334. * expecting that, right?); so each list has a set of ports, with a
  4335. * link to the socket (struct sock) that uses it, the port number and
  4336. * a fastreuse flag (FIXME: NPI ipg).
  4337. */
  4338. static struct sctp_bind_bucket *sctp_bucket_create(
  4339. struct sctp_bind_hashbucket *head, unsigned short snum);
  4340. static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  4341. {
  4342. struct sctp_bind_hashbucket *head; /* hash list */
  4343. struct sctp_bind_bucket *pp; /* hash list port iterator */
  4344. unsigned short snum;
  4345. int ret;
  4346. snum = ntohs(addr->v4.sin_port);
  4347. SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
  4348. sctp_local_bh_disable();
  4349. if (snum == 0) {
  4350. /* Search for an available port.
  4351. *
  4352. * 'sctp_port_rover' was the last port assigned, so
  4353. * we start to search from 'sctp_port_rover +
  4354. * 1'. What we do is first check if port 'rover' is
  4355. * already in the hash table; if not, we use that; if
  4356. * it is, we try next.
  4357. */
  4358. int low = sysctl_local_port_range[0];
  4359. int high = sysctl_local_port_range[1];
  4360. int remaining = (high - low) + 1;
  4361. int rover;
  4362. int index;
  4363. sctp_spin_lock(&sctp_port_alloc_lock);
  4364. rover = sctp_port_rover;
  4365. do {
  4366. rover++;
  4367. if ((rover < low) || (rover > high))
  4368. rover = low;
  4369. index = sctp_phashfn(rover);
  4370. head = &sctp_port_hashtable[index];
  4371. sctp_spin_lock(&head->lock);
  4372. for (pp = head->chain; pp; pp = pp->next)
  4373. if (pp->port == rover)
  4374. goto next;
  4375. break;
  4376. next:
  4377. sctp_spin_unlock(&head->lock);
  4378. } while (--remaining > 0);
  4379. sctp_port_rover = rover;
  4380. sctp_spin_unlock(&sctp_port_alloc_lock);
  4381. /* Exhausted local port range during search? */
  4382. ret = 1;
  4383. if (remaining <= 0)
  4384. goto fail;
  4385. /* OK, here is the one we will use. HEAD (the port
  4386. * hash table list entry) is non-NULL and we hold it's
  4387. * mutex.
  4388. */
  4389. snum = rover;
  4390. } else {
  4391. /* We are given an specific port number; we verify
  4392. * that it is not being used. If it is used, we will
  4393. * exahust the search in the hash list corresponding
  4394. * to the port number (snum) - we detect that with the
  4395. * port iterator, pp being NULL.
  4396. */
  4397. head = &sctp_port_hashtable[sctp_phashfn(snum)];
  4398. sctp_spin_lock(&head->lock);
  4399. for (pp = head->chain; pp; pp = pp->next) {
  4400. if (pp->port == snum)
  4401. goto pp_found;
  4402. }
  4403. }
  4404. pp = NULL;
  4405. goto pp_not_found;
  4406. pp_found:
  4407. if (!hlist_empty(&pp->owner)) {
  4408. /* We had a port hash table hit - there is an
  4409. * available port (pp != NULL) and it is being
  4410. * used by other socket (pp->owner not empty); that other
  4411. * socket is going to be sk2.
  4412. */
  4413. int reuse = sk->sk_reuse;
  4414. struct sock *sk2;
  4415. struct hlist_node *node;
  4416. SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
  4417. if (pp->fastreuse && sk->sk_reuse &&
  4418. sk->sk_state != SCTP_SS_LISTENING)
  4419. goto success;
  4420. /* Run through the list of sockets bound to the port
  4421. * (pp->port) [via the pointers bind_next and
  4422. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  4423. * we get the endpoint they describe and run through
  4424. * the endpoint's list of IP (v4 or v6) addresses,
  4425. * comparing each of the addresses with the address of
  4426. * the socket sk. If we find a match, then that means
  4427. * that this port/socket (sk) combination are already
  4428. * in an endpoint.
  4429. */
  4430. sk_for_each_bound(sk2, node, &pp->owner) {
  4431. struct sctp_endpoint *ep2;
  4432. ep2 = sctp_sk(sk2)->ep;
  4433. if (reuse && sk2->sk_reuse &&
  4434. sk2->sk_state != SCTP_SS_LISTENING)
  4435. continue;
  4436. if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
  4437. sctp_sk(sk))) {
  4438. ret = (long)sk2;
  4439. goto fail_unlock;
  4440. }
  4441. }
  4442. SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
  4443. }
  4444. pp_not_found:
  4445. /* If there was a hash table miss, create a new port. */
  4446. ret = 1;
  4447. if (!pp && !(pp = sctp_bucket_create(head, snum)))
  4448. goto fail_unlock;
  4449. /* In either case (hit or miss), make sure fastreuse is 1 only
  4450. * if sk->sk_reuse is too (that is, if the caller requested
  4451. * SO_REUSEADDR on this socket -sk-).
  4452. */
  4453. if (hlist_empty(&pp->owner)) {
  4454. if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
  4455. pp->fastreuse = 1;
  4456. else
  4457. pp->fastreuse = 0;
  4458. } else if (pp->fastreuse &&
  4459. (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
  4460. pp->fastreuse = 0;
  4461. /* We are set, so fill up all the data in the hash table
  4462. * entry, tie the socket list information with the rest of the
  4463. * sockets FIXME: Blurry, NPI (ipg).
  4464. */
  4465. success:
  4466. if (!sctp_sk(sk)->bind_hash) {
  4467. inet_sk(sk)->num = snum;
  4468. sk_add_bind_node(sk, &pp->owner);
  4469. sctp_sk(sk)->bind_hash = pp;
  4470. }
  4471. ret = 0;
  4472. fail_unlock:
  4473. sctp_spin_unlock(&head->lock);
  4474. fail:
  4475. sctp_local_bh_enable();
  4476. return ret;
  4477. }
  4478. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  4479. * port is requested.
  4480. */
  4481. static int sctp_get_port(struct sock *sk, unsigned short snum)
  4482. {
  4483. long ret;
  4484. union sctp_addr addr;
  4485. struct sctp_af *af = sctp_sk(sk)->pf->af;
  4486. /* Set up a dummy address struct from the sk. */
  4487. af->from_sk(&addr, sk);
  4488. addr.v4.sin_port = htons(snum);
  4489. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  4490. ret = sctp_get_port_local(sk, &addr);
  4491. return (ret ? 1 : 0);
  4492. }
  4493. /*
  4494. * 3.1.3 listen() - UDP Style Syntax
  4495. *
  4496. * By default, new associations are not accepted for UDP style sockets.
  4497. * An application uses listen() to mark a socket as being able to
  4498. * accept new associations.
  4499. */
  4500. SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
  4501. {
  4502. struct sctp_sock *sp = sctp_sk(sk);
  4503. struct sctp_endpoint *ep = sp->ep;
  4504. /* Only UDP style sockets that are not peeled off are allowed to
  4505. * listen().
  4506. */
  4507. if (!sctp_style(sk, UDP))
  4508. return -EINVAL;
  4509. /* If backlog is zero, disable listening. */
  4510. if (!backlog) {
  4511. if (sctp_sstate(sk, CLOSED))
  4512. return 0;
  4513. sctp_unhash_endpoint(ep);
  4514. sk->sk_state = SCTP_SS_CLOSED;
  4515. return 0;
  4516. }
  4517. /* Return if we are already listening. */
  4518. if (sctp_sstate(sk, LISTENING))
  4519. return 0;
  4520. /*
  4521. * If a bind() or sctp_bindx() is not called prior to a listen()
  4522. * call that allows new associations to be accepted, the system
  4523. * picks an ephemeral port and will choose an address set equivalent
  4524. * to binding with a wildcard address.
  4525. *
  4526. * This is not currently spelled out in the SCTP sockets
  4527. * extensions draft, but follows the practice as seen in TCP
  4528. * sockets.
  4529. *
  4530. * Additionally, turn off fastreuse flag since we are not listening
  4531. */
  4532. sk->sk_state = SCTP_SS_LISTENING;
  4533. if (!ep->base.bind_addr.port) {
  4534. if (sctp_autobind(sk))
  4535. return -EAGAIN;
  4536. } else
  4537. sctp_sk(sk)->bind_hash->fastreuse = 0;
  4538. sctp_hash_endpoint(ep);
  4539. return 0;
  4540. }
  4541. /*
  4542. * 4.1.3 listen() - TCP Style Syntax
  4543. *
  4544. * Applications uses listen() to ready the SCTP endpoint for accepting
  4545. * inbound associations.
  4546. */
  4547. SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
  4548. {
  4549. struct sctp_sock *sp = sctp_sk(sk);
  4550. struct sctp_endpoint *ep = sp->ep;
  4551. /* If backlog is zero, disable listening. */
  4552. if (!backlog) {
  4553. if (sctp_sstate(sk, CLOSED))
  4554. return 0;
  4555. sctp_unhash_endpoint(ep);
  4556. sk->sk_state = SCTP_SS_CLOSED;
  4557. return 0;
  4558. }
  4559. if (sctp_sstate(sk, LISTENING))
  4560. return 0;
  4561. /*
  4562. * If a bind() or sctp_bindx() is not called prior to a listen()
  4563. * call that allows new associations to be accepted, the system
  4564. * picks an ephemeral port and will choose an address set equivalent
  4565. * to binding with a wildcard address.
  4566. *
  4567. * This is not currently spelled out in the SCTP sockets
  4568. * extensions draft, but follows the practice as seen in TCP
  4569. * sockets.
  4570. */
  4571. sk->sk_state = SCTP_SS_LISTENING;
  4572. if (!ep->base.bind_addr.port) {
  4573. if (sctp_autobind(sk))
  4574. return -EAGAIN;
  4575. } else
  4576. sctp_sk(sk)->bind_hash->fastreuse = 0;
  4577. sk->sk_max_ack_backlog = backlog;
  4578. sctp_hash_endpoint(ep);
  4579. return 0;
  4580. }
  4581. /*
  4582. * Move a socket to LISTENING state.
  4583. */
  4584. int sctp_inet_listen(struct socket *sock, int backlog)
  4585. {
  4586. struct sock *sk = sock->sk;
  4587. struct crypto_hash *tfm = NULL;
  4588. int err = -EINVAL;
  4589. if (unlikely(backlog < 0))
  4590. goto out;
  4591. sctp_lock_sock(sk);
  4592. if (sock->state != SS_UNCONNECTED)
  4593. goto out;
  4594. /* Allocate HMAC for generating cookie. */
  4595. if (sctp_hmac_alg) {
  4596. tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC);
  4597. if (IS_ERR(tfm)) {
  4598. if (net_ratelimit()) {
  4599. printk(KERN_INFO
  4600. "SCTP: failed to load transform for %s: %ld\n",
  4601. sctp_hmac_alg, PTR_ERR(tfm));
  4602. }
  4603. err = -ENOSYS;
  4604. goto out;
  4605. }
  4606. }
  4607. switch (sock->type) {
  4608. case SOCK_SEQPACKET:
  4609. err = sctp_seqpacket_listen(sk, backlog);
  4610. break;
  4611. case SOCK_STREAM:
  4612. err = sctp_stream_listen(sk, backlog);
  4613. break;
  4614. default:
  4615. break;
  4616. }
  4617. if (err)
  4618. goto cleanup;
  4619. /* Store away the transform reference. */
  4620. sctp_sk(sk)->hmac = tfm;
  4621. out:
  4622. sctp_release_sock(sk);
  4623. return err;
  4624. cleanup:
  4625. crypto_free_hash(tfm);
  4626. goto out;
  4627. }
  4628. /*
  4629. * This function is done by modeling the current datagram_poll() and the
  4630. * tcp_poll(). Note that, based on these implementations, we don't
  4631. * lock the socket in this function, even though it seems that,
  4632. * ideally, locking or some other mechanisms can be used to ensure
  4633. * the integrity of the counters (sndbuf and wmem_alloc) used
  4634. * in this place. We assume that we don't need locks either until proven
  4635. * otherwise.
  4636. *
  4637. * Another thing to note is that we include the Async I/O support
  4638. * here, again, by modeling the current TCP/UDP code. We don't have
  4639. * a good way to test with it yet.
  4640. */
  4641. unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  4642. {
  4643. struct sock *sk = sock->sk;
  4644. struct sctp_sock *sp = sctp_sk(sk);
  4645. unsigned int mask;
  4646. poll_wait(file, sk->sk_sleep, wait);
  4647. /* A TCP-style listening socket becomes readable when the accept queue
  4648. * is not empty.
  4649. */
  4650. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  4651. return (!list_empty(&sp->ep->asocs)) ?
  4652. (POLLIN | POLLRDNORM) : 0;
  4653. mask = 0;
  4654. /* Is there any exceptional events? */
  4655. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  4656. mask |= POLLERR;
  4657. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4658. mask |= POLLRDHUP;
  4659. if (sk->sk_shutdown == SHUTDOWN_MASK)
  4660. mask |= POLLHUP;
  4661. /* Is it readable? Reconsider this code with TCP-style support. */
  4662. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  4663. (sk->sk_shutdown & RCV_SHUTDOWN))
  4664. mask |= POLLIN | POLLRDNORM;
  4665. /* The association is either gone or not ready. */
  4666. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  4667. return mask;
  4668. /* Is it writable? */
  4669. if (sctp_writeable(sk)) {
  4670. mask |= POLLOUT | POLLWRNORM;
  4671. } else {
  4672. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  4673. /*
  4674. * Since the socket is not locked, the buffer
  4675. * might be made available after the writeable check and
  4676. * before the bit is set. This could cause a lost I/O
  4677. * signal. tcp_poll() has a race breaker for this race
  4678. * condition. Based on their implementation, we put
  4679. * in the following code to cover it as well.
  4680. */
  4681. if (sctp_writeable(sk))
  4682. mask |= POLLOUT | POLLWRNORM;
  4683. }
  4684. return mask;
  4685. }
  4686. /********************************************************************
  4687. * 2nd Level Abstractions
  4688. ********************************************************************/
  4689. static struct sctp_bind_bucket *sctp_bucket_create(
  4690. struct sctp_bind_hashbucket *head, unsigned short snum)
  4691. {
  4692. struct sctp_bind_bucket *pp;
  4693. pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
  4694. SCTP_DBG_OBJCNT_INC(bind_bucket);
  4695. if (pp) {
  4696. pp->port = snum;
  4697. pp->fastreuse = 0;
  4698. INIT_HLIST_HEAD(&pp->owner);
  4699. if ((pp->next = head->chain) != NULL)
  4700. pp->next->pprev = &pp->next;
  4701. head->chain = pp;
  4702. pp->pprev = &head->chain;
  4703. }
  4704. return pp;
  4705. }
  4706. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  4707. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  4708. {
  4709. if (pp && hlist_empty(&pp->owner)) {
  4710. if (pp->next)
  4711. pp->next->pprev = pp->pprev;
  4712. *(pp->pprev) = pp->next;
  4713. kmem_cache_free(sctp_bucket_cachep, pp);
  4714. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  4715. }
  4716. }
  4717. /* Release this socket's reference to a local port. */
  4718. static inline void __sctp_put_port(struct sock *sk)
  4719. {
  4720. struct sctp_bind_hashbucket *head =
  4721. &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
  4722. struct sctp_bind_bucket *pp;
  4723. sctp_spin_lock(&head->lock);
  4724. pp = sctp_sk(sk)->bind_hash;
  4725. __sk_del_bind_node(sk);
  4726. sctp_sk(sk)->bind_hash = NULL;
  4727. inet_sk(sk)->num = 0;
  4728. sctp_bucket_destroy(pp);
  4729. sctp_spin_unlock(&head->lock);
  4730. }
  4731. void sctp_put_port(struct sock *sk)
  4732. {
  4733. sctp_local_bh_disable();
  4734. __sctp_put_port(sk);
  4735. sctp_local_bh_enable();
  4736. }
  4737. /*
  4738. * The system picks an ephemeral port and choose an address set equivalent
  4739. * to binding with a wildcard address.
  4740. * One of those addresses will be the primary address for the association.
  4741. * This automatically enables the multihoming capability of SCTP.
  4742. */
  4743. static int sctp_autobind(struct sock *sk)
  4744. {
  4745. union sctp_addr autoaddr;
  4746. struct sctp_af *af;
  4747. __be16 port;
  4748. /* Initialize a local sockaddr structure to INADDR_ANY. */
  4749. af = sctp_sk(sk)->pf->af;
  4750. port = htons(inet_sk(sk)->num);
  4751. af->inaddr_any(&autoaddr, port);
  4752. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  4753. }
  4754. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  4755. *
  4756. * From RFC 2292
  4757. * 4.2 The cmsghdr Structure *
  4758. *
  4759. * When ancillary data is sent or received, any number of ancillary data
  4760. * objects can be specified by the msg_control and msg_controllen members of
  4761. * the msghdr structure, because each object is preceded by
  4762. * a cmsghdr structure defining the object's length (the cmsg_len member).
  4763. * Historically Berkeley-derived implementations have passed only one object
  4764. * at a time, but this API allows multiple objects to be
  4765. * passed in a single call to sendmsg() or recvmsg(). The following example
  4766. * shows two ancillary data objects in a control buffer.
  4767. *
  4768. * |<--------------------------- msg_controllen -------------------------->|
  4769. * | |
  4770. *
  4771. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  4772. *
  4773. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  4774. * | | |
  4775. *
  4776. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  4777. *
  4778. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  4779. * | | | | |
  4780. *
  4781. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  4782. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  4783. *
  4784. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  4785. *
  4786. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  4787. * ^
  4788. * |
  4789. *
  4790. * msg_control
  4791. * points here
  4792. */
  4793. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
  4794. sctp_cmsgs_t *cmsgs)
  4795. {
  4796. struct cmsghdr *cmsg;
  4797. for (cmsg = CMSG_FIRSTHDR(msg);
  4798. cmsg != NULL;
  4799. cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
  4800. if (!CMSG_OK(msg, cmsg))
  4801. return -EINVAL;
  4802. /* Should we parse this header or ignore? */
  4803. if (cmsg->cmsg_level != IPPROTO_SCTP)
  4804. continue;
  4805. /* Strictly check lengths following example in SCM code. */
  4806. switch (cmsg->cmsg_type) {
  4807. case SCTP_INIT:
  4808. /* SCTP Socket API Extension
  4809. * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
  4810. *
  4811. * This cmsghdr structure provides information for
  4812. * initializing new SCTP associations with sendmsg().
  4813. * The SCTP_INITMSG socket option uses this same data
  4814. * structure. This structure is not used for
  4815. * recvmsg().
  4816. *
  4817. * cmsg_level cmsg_type cmsg_data[]
  4818. * ------------ ------------ ----------------------
  4819. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  4820. */
  4821. if (cmsg->cmsg_len !=
  4822. CMSG_LEN(sizeof(struct sctp_initmsg)))
  4823. return -EINVAL;
  4824. cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
  4825. break;
  4826. case SCTP_SNDRCV:
  4827. /* SCTP Socket API Extension
  4828. * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
  4829. *
  4830. * This cmsghdr structure specifies SCTP options for
  4831. * sendmsg() and describes SCTP header information
  4832. * about a received message through recvmsg().
  4833. *
  4834. * cmsg_level cmsg_type cmsg_data[]
  4835. * ------------ ------------ ----------------------
  4836. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  4837. */
  4838. if (cmsg->cmsg_len !=
  4839. CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  4840. return -EINVAL;
  4841. cmsgs->info =
  4842. (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
  4843. /* Minimally, validate the sinfo_flags. */
  4844. if (cmsgs->info->sinfo_flags &
  4845. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  4846. SCTP_ABORT | SCTP_EOF))
  4847. return -EINVAL;
  4848. break;
  4849. default:
  4850. return -EINVAL;
  4851. }
  4852. }
  4853. return 0;
  4854. }
  4855. /*
  4856. * Wait for a packet..
  4857. * Note: This function is the same function as in core/datagram.c
  4858. * with a few modifications to make lksctp work.
  4859. */
  4860. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
  4861. {
  4862. int error;
  4863. DEFINE_WAIT(wait);
  4864. prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  4865. /* Socket errors? */
  4866. error = sock_error(sk);
  4867. if (error)
  4868. goto out;
  4869. if (!skb_queue_empty(&sk->sk_receive_queue))
  4870. goto ready;
  4871. /* Socket shut down? */
  4872. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4873. goto out;
  4874. /* Sequenced packets can come disconnected. If so we report the
  4875. * problem.
  4876. */
  4877. error = -ENOTCONN;
  4878. /* Is there a good reason to think that we may receive some data? */
  4879. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  4880. goto out;
  4881. /* Handle signals. */
  4882. if (signal_pending(current))
  4883. goto interrupted;
  4884. /* Let another process have a go. Since we are going to sleep
  4885. * anyway. Note: This may cause odd behaviors if the message
  4886. * does not fit in the user's buffer, but this seems to be the
  4887. * only way to honor MSG_DONTWAIT realistically.
  4888. */
  4889. sctp_release_sock(sk);
  4890. *timeo_p = schedule_timeout(*timeo_p);
  4891. sctp_lock_sock(sk);
  4892. ready:
  4893. finish_wait(sk->sk_sleep, &wait);
  4894. return 0;
  4895. interrupted:
  4896. error = sock_intr_errno(*timeo_p);
  4897. out:
  4898. finish_wait(sk->sk_sleep, &wait);
  4899. *err = error;
  4900. return error;
  4901. }
  4902. /* Receive a datagram.
  4903. * Note: This is pretty much the same routine as in core/datagram.c
  4904. * with a few changes to make lksctp work.
  4905. */
  4906. static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  4907. int noblock, int *err)
  4908. {
  4909. int error;
  4910. struct sk_buff *skb;
  4911. long timeo;
  4912. timeo = sock_rcvtimeo(sk, noblock);
  4913. SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
  4914. timeo, MAX_SCHEDULE_TIMEOUT);
  4915. do {
  4916. /* Again only user level code calls this function,
  4917. * so nothing interrupt level
  4918. * will suddenly eat the receive_queue.
  4919. *
  4920. * Look at current nfs client by the way...
  4921. * However, this function was corrent in any case. 8)
  4922. */
  4923. if (flags & MSG_PEEK) {
  4924. spin_lock_bh(&sk->sk_receive_queue.lock);
  4925. skb = skb_peek(&sk->sk_receive_queue);
  4926. if (skb)
  4927. atomic_inc(&skb->users);
  4928. spin_unlock_bh(&sk->sk_receive_queue.lock);
  4929. } else {
  4930. skb = skb_dequeue(&sk->sk_receive_queue);
  4931. }
  4932. if (skb)
  4933. return skb;
  4934. /* Caller is allowed not to check sk->sk_err before calling. */
  4935. error = sock_error(sk);
  4936. if (error)
  4937. goto no_packet;
  4938. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4939. break;
  4940. /* User doesn't want to wait. */
  4941. error = -EAGAIN;
  4942. if (!timeo)
  4943. goto no_packet;
  4944. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  4945. return NULL;
  4946. no_packet:
  4947. *err = error;
  4948. return NULL;
  4949. }
  4950. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  4951. static void __sctp_write_space(struct sctp_association *asoc)
  4952. {
  4953. struct sock *sk = asoc->base.sk;
  4954. struct socket *sock = sk->sk_socket;
  4955. if ((sctp_wspace(asoc) > 0) && sock) {
  4956. if (waitqueue_active(&asoc->wait))
  4957. wake_up_interruptible(&asoc->wait);
  4958. if (sctp_writeable(sk)) {
  4959. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  4960. wake_up_interruptible(sk->sk_sleep);
  4961. /* Note that we try to include the Async I/O support
  4962. * here by modeling from the current TCP/UDP code.
  4963. * We have not tested with it yet.
  4964. */
  4965. if (sock->fasync_list &&
  4966. !(sk->sk_shutdown & SEND_SHUTDOWN))
  4967. sock_wake_async(sock, 2, POLL_OUT);
  4968. }
  4969. }
  4970. }
  4971. /* Do accounting for the sndbuf space.
  4972. * Decrement the used sndbuf space of the corresponding association by the
  4973. * data size which was just transmitted(freed).
  4974. */
  4975. static void sctp_wfree(struct sk_buff *skb)
  4976. {
  4977. struct sctp_association *asoc;
  4978. struct sctp_chunk *chunk;
  4979. struct sock *sk;
  4980. /* Get the saved chunk pointer. */
  4981. chunk = *((struct sctp_chunk **)(skb->cb));
  4982. asoc = chunk->asoc;
  4983. sk = asoc->base.sk;
  4984. asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
  4985. sizeof(struct sk_buff) +
  4986. sizeof(struct sctp_chunk);
  4987. atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  4988. /*
  4989. * This undoes what is done via sk_charge_skb
  4990. */
  4991. sk->sk_wmem_queued -= skb->truesize;
  4992. sk->sk_forward_alloc += skb->truesize;
  4993. sock_wfree(skb);
  4994. __sctp_write_space(asoc);
  4995. sctp_association_put(asoc);
  4996. }
  4997. /* Do accounting for the receive space on the socket.
  4998. * Accounting for the association is done in ulpevent.c
  4999. * We set this as a destructor for the cloned data skbs so that
  5000. * accounting is done at the correct time.
  5001. */
  5002. void sctp_sock_rfree(struct sk_buff *skb)
  5003. {
  5004. struct sock *sk = skb->sk;
  5005. struct sctp_ulpevent *event = sctp_skb2event(skb);
  5006. atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
  5007. /*
  5008. * Mimic the behavior of sk_stream_rfree
  5009. */
  5010. sk->sk_forward_alloc += event->rmem_len;
  5011. }
  5012. /* Helper function to wait for space in the sndbuf. */
  5013. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  5014. size_t msg_len)
  5015. {
  5016. struct sock *sk = asoc->base.sk;
  5017. int err = 0;
  5018. long current_timeo = *timeo_p;
  5019. DEFINE_WAIT(wait);
  5020. SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
  5021. asoc, (long)(*timeo_p), msg_len);
  5022. /* Increment the association's refcnt. */
  5023. sctp_association_hold(asoc);
  5024. /* Wait on the association specific sndbuf space. */
  5025. for (;;) {
  5026. prepare_to_wait_exclusive(&asoc->wait, &wait,
  5027. TASK_INTERRUPTIBLE);
  5028. if (!*timeo_p)
  5029. goto do_nonblock;
  5030. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  5031. asoc->base.dead)
  5032. goto do_error;
  5033. if (signal_pending(current))
  5034. goto do_interrupted;
  5035. if (msg_len <= sctp_wspace(asoc))
  5036. break;
  5037. /* Let another process have a go. Since we are going
  5038. * to sleep anyway.
  5039. */
  5040. sctp_release_sock(sk);
  5041. current_timeo = schedule_timeout(current_timeo);
  5042. BUG_ON(sk != asoc->base.sk);
  5043. sctp_lock_sock(sk);
  5044. *timeo_p = current_timeo;
  5045. }
  5046. out:
  5047. finish_wait(&asoc->wait, &wait);
  5048. /* Release the association's refcnt. */
  5049. sctp_association_put(asoc);
  5050. return err;
  5051. do_error:
  5052. err = -EPIPE;
  5053. goto out;
  5054. do_interrupted:
  5055. err = sock_intr_errno(*timeo_p);
  5056. goto out;
  5057. do_nonblock:
  5058. err = -EAGAIN;
  5059. goto out;
  5060. }
  5061. /* If socket sndbuf has changed, wake up all per association waiters. */
  5062. void sctp_write_space(struct sock *sk)
  5063. {
  5064. struct sctp_association *asoc;
  5065. struct list_head *pos;
  5066. /* Wake up the tasks in each wait queue. */
  5067. list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
  5068. asoc = list_entry(pos, struct sctp_association, asocs);
  5069. __sctp_write_space(asoc);
  5070. }
  5071. }
  5072. /* Is there any sndbuf space available on the socket?
  5073. *
  5074. * Note that sk_wmem_alloc is the sum of the send buffers on all of the
  5075. * associations on the same socket. For a UDP-style socket with
  5076. * multiple associations, it is possible for it to be "unwriteable"
  5077. * prematurely. I assume that this is acceptable because
  5078. * a premature "unwriteable" is better than an accidental "writeable" which
  5079. * would cause an unwanted block under certain circumstances. For the 1-1
  5080. * UDP-style sockets or TCP-style sockets, this code should work.
  5081. * - Daisy
  5082. */
  5083. static int sctp_writeable(struct sock *sk)
  5084. {
  5085. int amt = 0;
  5086. amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
  5087. if (amt < 0)
  5088. amt = 0;
  5089. return amt;
  5090. }
  5091. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  5092. * returns immediately with EINPROGRESS.
  5093. */
  5094. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  5095. {
  5096. struct sock *sk = asoc->base.sk;
  5097. int err = 0;
  5098. long current_timeo = *timeo_p;
  5099. DEFINE_WAIT(wait);
  5100. SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
  5101. (long)(*timeo_p));
  5102. /* Increment the association's refcnt. */
  5103. sctp_association_hold(asoc);
  5104. for (;;) {
  5105. prepare_to_wait_exclusive(&asoc->wait, &wait,
  5106. TASK_INTERRUPTIBLE);
  5107. if (!*timeo_p)
  5108. goto do_nonblock;
  5109. if (sk->sk_shutdown & RCV_SHUTDOWN)
  5110. break;
  5111. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  5112. asoc->base.dead)
  5113. goto do_error;
  5114. if (signal_pending(current))
  5115. goto do_interrupted;
  5116. if (sctp_state(asoc, ESTABLISHED))
  5117. break;
  5118. /* Let another process have a go. Since we are going
  5119. * to sleep anyway.
  5120. */
  5121. sctp_release_sock(sk);
  5122. current_timeo = schedule_timeout(current_timeo);
  5123. sctp_lock_sock(sk);
  5124. *timeo_p = current_timeo;
  5125. }
  5126. out:
  5127. finish_wait(&asoc->wait, &wait);
  5128. /* Release the association's refcnt. */
  5129. sctp_association_put(asoc);
  5130. return err;
  5131. do_error:
  5132. if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
  5133. err = -ETIMEDOUT;
  5134. else
  5135. err = -ECONNREFUSED;
  5136. goto out;
  5137. do_interrupted:
  5138. err = sock_intr_errno(*timeo_p);
  5139. goto out;
  5140. do_nonblock:
  5141. err = -EINPROGRESS;
  5142. goto out;
  5143. }
  5144. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  5145. {
  5146. struct sctp_endpoint *ep;
  5147. int err = 0;
  5148. DEFINE_WAIT(wait);
  5149. ep = sctp_sk(sk)->ep;
  5150. for (;;) {
  5151. prepare_to_wait_exclusive(sk->sk_sleep, &wait,
  5152. TASK_INTERRUPTIBLE);
  5153. if (list_empty(&ep->asocs)) {
  5154. sctp_release_sock(sk);
  5155. timeo = schedule_timeout(timeo);
  5156. sctp_lock_sock(sk);
  5157. }
  5158. err = -EINVAL;
  5159. if (!sctp_sstate(sk, LISTENING))
  5160. break;
  5161. err = 0;
  5162. if (!list_empty(&ep->asocs))
  5163. break;
  5164. err = sock_intr_errno(timeo);
  5165. if (signal_pending(current))
  5166. break;
  5167. err = -EAGAIN;
  5168. if (!timeo)
  5169. break;
  5170. }
  5171. finish_wait(sk->sk_sleep, &wait);
  5172. return err;
  5173. }
  5174. static void sctp_wait_for_close(struct sock *sk, long timeout)
  5175. {
  5176. DEFINE_WAIT(wait);
  5177. do {
  5178. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  5179. if (list_empty(&sctp_sk(sk)->ep->asocs))
  5180. break;
  5181. sctp_release_sock(sk);
  5182. timeout = schedule_timeout(timeout);
  5183. sctp_lock_sock(sk);
  5184. } while (!signal_pending(current) && timeout);
  5185. finish_wait(sk->sk_sleep, &wait);
  5186. }
  5187. static void sctp_sock_rfree_frag(struct sk_buff *skb)
  5188. {
  5189. struct sk_buff *frag;
  5190. if (!skb->data_len)
  5191. goto done;
  5192. /* Don't forget the fragments. */
  5193. for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
  5194. sctp_sock_rfree_frag(frag);
  5195. done:
  5196. sctp_sock_rfree(skb);
  5197. }
  5198. static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
  5199. {
  5200. struct sk_buff *frag;
  5201. if (!skb->data_len)
  5202. goto done;
  5203. /* Don't forget the fragments. */
  5204. for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
  5205. sctp_skb_set_owner_r_frag(frag, sk);
  5206. done:
  5207. sctp_skb_set_owner_r(skb, sk);
  5208. }
  5209. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  5210. * and its messages to the newsk.
  5211. */
  5212. static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  5213. struct sctp_association *assoc,
  5214. sctp_socket_type_t type)
  5215. {
  5216. struct sctp_sock *oldsp = sctp_sk(oldsk);
  5217. struct sctp_sock *newsp = sctp_sk(newsk);
  5218. struct sctp_bind_bucket *pp; /* hash list port iterator */
  5219. struct sctp_endpoint *newep = newsp->ep;
  5220. struct sk_buff *skb, *tmp;
  5221. struct sctp_ulpevent *event;
  5222. int flags = 0;
  5223. /* Migrate socket buffer sizes and all the socket level options to the
  5224. * new socket.
  5225. */
  5226. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  5227. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  5228. /* Brute force copy old sctp opt. */
  5229. inet_sk_copy_descendant(newsk, oldsk);
  5230. /* Restore the ep value that was overwritten with the above structure
  5231. * copy.
  5232. */
  5233. newsp->ep = newep;
  5234. newsp->hmac = NULL;
  5235. /* Hook this new socket in to the bind_hash list. */
  5236. pp = sctp_sk(oldsk)->bind_hash;
  5237. sk_add_bind_node(newsk, &pp->owner);
  5238. sctp_sk(newsk)->bind_hash = pp;
  5239. inet_sk(newsk)->num = inet_sk(oldsk)->num;
  5240. /* Copy the bind_addr list from the original endpoint to the new
  5241. * endpoint so that we can handle restarts properly
  5242. */
  5243. if (PF_INET6 == assoc->base.sk->sk_family)
  5244. flags = SCTP_ADDR6_ALLOWED;
  5245. if (assoc->peer.ipv4_address)
  5246. flags |= SCTP_ADDR4_PEERSUPP;
  5247. if (assoc->peer.ipv6_address)
  5248. flags |= SCTP_ADDR6_PEERSUPP;
  5249. sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
  5250. &oldsp->ep->base.bind_addr,
  5251. SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
  5252. /* Move any messages in the old socket's receive queue that are for the
  5253. * peeled off association to the new socket's receive queue.
  5254. */
  5255. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  5256. event = sctp_skb2event(skb);
  5257. if (event->asoc == assoc) {
  5258. sctp_sock_rfree_frag(skb);
  5259. __skb_unlink(skb, &oldsk->sk_receive_queue);
  5260. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  5261. sctp_skb_set_owner_r_frag(skb, newsk);
  5262. }
  5263. }
  5264. /* Clean up any messages pending delivery due to partial
  5265. * delivery. Three cases:
  5266. * 1) No partial deliver; no work.
  5267. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  5268. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  5269. */
  5270. skb_queue_head_init(&newsp->pd_lobby);
  5271. atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
  5272. if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
  5273. struct sk_buff_head *queue;
  5274. /* Decide which queue to move pd_lobby skbs to. */
  5275. if (assoc->ulpq.pd_mode) {
  5276. queue = &newsp->pd_lobby;
  5277. } else
  5278. queue = &newsk->sk_receive_queue;
  5279. /* Walk through the pd_lobby, looking for skbs that
  5280. * need moved to the new socket.
  5281. */
  5282. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  5283. event = sctp_skb2event(skb);
  5284. if (event->asoc == assoc) {
  5285. sctp_sock_rfree_frag(skb);
  5286. __skb_unlink(skb, &oldsp->pd_lobby);
  5287. __skb_queue_tail(queue, skb);
  5288. sctp_skb_set_owner_r_frag(skb, newsk);
  5289. }
  5290. }
  5291. /* Clear up any skbs waiting for the partial
  5292. * delivery to finish.
  5293. */
  5294. if (assoc->ulpq.pd_mode)
  5295. sctp_clear_pd(oldsk, NULL);
  5296. }
  5297. sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) {
  5298. sctp_sock_rfree_frag(skb);
  5299. sctp_skb_set_owner_r_frag(skb, newsk);
  5300. }
  5301. sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) {
  5302. sctp_sock_rfree_frag(skb);
  5303. sctp_skb_set_owner_r_frag(skb, newsk);
  5304. }
  5305. /* Set the type of socket to indicate that it is peeled off from the
  5306. * original UDP-style socket or created with the accept() call on a
  5307. * TCP-style socket..
  5308. */
  5309. newsp->type = type;
  5310. /* Mark the new socket "in-use" by the user so that any packets
  5311. * that may arrive on the association after we've moved it are
  5312. * queued to the backlog. This prevents a potential race between
  5313. * backlog processing on the old socket and new-packet processing
  5314. * on the new socket.
  5315. *
  5316. * The caller has just allocated newsk so we can guarantee that other
  5317. * paths won't try to lock it and then oldsk.
  5318. */
  5319. lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
  5320. sctp_assoc_migrate(assoc, newsk);
  5321. /* If the association on the newsk is already closed before accept()
  5322. * is called, set RCV_SHUTDOWN flag.
  5323. */
  5324. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
  5325. newsk->sk_shutdown |= RCV_SHUTDOWN;
  5326. newsk->sk_state = SCTP_SS_ESTABLISHED;
  5327. sctp_release_sock(newsk);
  5328. }
  5329. /* This proto struct describes the ULP interface for SCTP. */
  5330. struct proto sctp_prot = {
  5331. .name = "SCTP",
  5332. .owner = THIS_MODULE,
  5333. .close = sctp_close,
  5334. .connect = sctp_connect,
  5335. .disconnect = sctp_disconnect,
  5336. .accept = sctp_accept,
  5337. .ioctl = sctp_ioctl,
  5338. .init = sctp_init_sock,
  5339. .destroy = sctp_destroy_sock,
  5340. .shutdown = sctp_shutdown,
  5341. .setsockopt = sctp_setsockopt,
  5342. .getsockopt = sctp_getsockopt,
  5343. .sendmsg = sctp_sendmsg,
  5344. .recvmsg = sctp_recvmsg,
  5345. .bind = sctp_bind,
  5346. .backlog_rcv = sctp_backlog_rcv,
  5347. .hash = sctp_hash,
  5348. .unhash = sctp_unhash,
  5349. .get_port = sctp_get_port,
  5350. .obj_size = sizeof(struct sctp_sock),
  5351. .sysctl_mem = sysctl_sctp_mem,
  5352. .sysctl_rmem = sysctl_sctp_rmem,
  5353. .sysctl_wmem = sysctl_sctp_wmem,
  5354. .memory_pressure = &sctp_memory_pressure,
  5355. .enter_memory_pressure = sctp_enter_memory_pressure,
  5356. .memory_allocated = &sctp_memory_allocated,
  5357. };
  5358. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  5359. struct proto sctpv6_prot = {
  5360. .name = "SCTPv6",
  5361. .owner = THIS_MODULE,
  5362. .close = sctp_close,
  5363. .connect = sctp_connect,
  5364. .disconnect = sctp_disconnect,
  5365. .accept = sctp_accept,
  5366. .ioctl = sctp_ioctl,
  5367. .init = sctp_init_sock,
  5368. .destroy = sctp_destroy_sock,
  5369. .shutdown = sctp_shutdown,
  5370. .setsockopt = sctp_setsockopt,
  5371. .getsockopt = sctp_getsockopt,
  5372. .sendmsg = sctp_sendmsg,
  5373. .recvmsg = sctp_recvmsg,
  5374. .bind = sctp_bind,
  5375. .backlog_rcv = sctp_backlog_rcv,
  5376. .hash = sctp_hash,
  5377. .unhash = sctp_unhash,
  5378. .get_port = sctp_get_port,
  5379. .obj_size = sizeof(struct sctp6_sock),
  5380. .sysctl_mem = sysctl_sctp_mem,
  5381. .sysctl_rmem = sysctl_sctp_rmem,
  5382. .sysctl_wmem = sysctl_sctp_wmem,
  5383. .memory_pressure = &sctp_memory_pressure,
  5384. .enter_memory_pressure = sctp_enter_memory_pressure,
  5385. .memory_allocated = &sctp_memory_allocated,
  5386. };
  5387. #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */