socket.c 188 KB

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