socket.c 187 KB

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