socket.c 187 KB

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