socket.c 232 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528
  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, see
  32. * <http://www.gnu.org/licenses/>.
  33. *
  34. * Please send any bug reports or fixes you make to the
  35. * email address(es):
  36. * lksctp developers <linux-sctp@vger.kernel.org>
  37. *
  38. * Written or modified by:
  39. * La Monte H.P. Yarroll <piggy@acm.org>
  40. * Narasimha Budihal <narsi@refcode.org>
  41. * Karl Knutson <karl@athena.chicago.il.us>
  42. * Jon Grimm <jgrimm@us.ibm.com>
  43. * Xingang Guo <xingang.guo@intel.com>
  44. * Daisy Chang <daisyc@us.ibm.com>
  45. * Sridhar Samudrala <samudrala@us.ibm.com>
  46. * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
  47. * Ardelle Fan <ardelle.fan@intel.com>
  48. * Ryan Layer <rmlayer@us.ibm.com>
  49. * Anup Pemmaiah <pemmaiah@cc.usu.edu>
  50. * Kevin Gao <kevin.gao@intel.com>
  51. */
  52. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  53. #include <crypto/hash.h>
  54. #include <linux/types.h>
  55. #include <linux/kernel.h>
  56. #include <linux/wait.h>
  57. #include <linux/time.h>
  58. #include <linux/sched/signal.h>
  59. #include <linux/ip.h>
  60. #include <linux/capability.h>
  61. #include <linux/fcntl.h>
  62. #include <linux/poll.h>
  63. #include <linux/init.h>
  64. #include <linux/slab.h>
  65. #include <linux/file.h>
  66. #include <linux/compat.h>
  67. #include <net/ip.h>
  68. #include <net/icmp.h>
  69. #include <net/route.h>
  70. #include <net/ipv6.h>
  71. #include <net/inet_common.h>
  72. #include <net/busy_poll.h>
  73. #include <linux/socket.h> /* for sa_family_t */
  74. #include <linux/export.h>
  75. #include <net/sock.h>
  76. #include <net/sctp/sctp.h>
  77. #include <net/sctp/sm.h>
  78. #include <net/sctp/stream_sched.h>
  79. /* Forward declarations for internal helper functions. */
  80. static int sctp_writeable(struct sock *sk);
  81. static void sctp_wfree(struct sk_buff *skb);
  82. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  83. size_t msg_len, struct sock **orig_sk);
  84. static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
  85. static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
  86. static int sctp_wait_for_accept(struct sock *sk, long timeo);
  87. static void sctp_wait_for_close(struct sock *sk, long timeo);
  88. static void sctp_destruct_sock(struct sock *sk);
  89. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  90. union sctp_addr *addr, int len);
  91. static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
  92. static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
  93. static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
  94. static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
  95. static int sctp_send_asconf(struct sctp_association *asoc,
  96. struct sctp_chunk *chunk);
  97. static int sctp_do_bind(struct sock *, union sctp_addr *, int);
  98. static int sctp_autobind(struct sock *sk);
  99. static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  100. struct sctp_association *assoc,
  101. enum sctp_socket_type type);
  102. static unsigned long sctp_memory_pressure;
  103. static atomic_long_t sctp_memory_allocated;
  104. struct percpu_counter sctp_sockets_allocated;
  105. static void sctp_enter_memory_pressure(struct sock *sk)
  106. {
  107. sctp_memory_pressure = 1;
  108. }
  109. /* Get the sndbuf space available at the time on the association. */
  110. static inline int sctp_wspace(struct sctp_association *asoc)
  111. {
  112. int amt;
  113. if (asoc->ep->sndbuf_policy)
  114. amt = asoc->sndbuf_used;
  115. else
  116. amt = sk_wmem_alloc_get(asoc->base.sk);
  117. if (amt >= asoc->base.sk->sk_sndbuf) {
  118. if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK)
  119. amt = 0;
  120. else {
  121. amt = sk_stream_wspace(asoc->base.sk);
  122. if (amt < 0)
  123. amt = 0;
  124. }
  125. } else {
  126. amt = asoc->base.sk->sk_sndbuf - amt;
  127. }
  128. return amt;
  129. }
  130. /* Increment the used sndbuf space count of the corresponding association by
  131. * the size of the outgoing data chunk.
  132. * Also, set the skb destructor for sndbuf accounting later.
  133. *
  134. * Since it is always 1-1 between chunk and skb, and also a new skb is always
  135. * allocated for chunk bundling in sctp_packet_transmit(), we can use the
  136. * destructor in the data chunk skb for the purpose of the sndbuf space
  137. * tracking.
  138. */
  139. static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
  140. {
  141. struct sctp_association *asoc = chunk->asoc;
  142. struct sock *sk = asoc->base.sk;
  143. /* The sndbuf space is tracked per association. */
  144. sctp_association_hold(asoc);
  145. skb_set_owner_w(chunk->skb, sk);
  146. chunk->skb->destructor = sctp_wfree;
  147. /* Save the chunk pointer in skb for sctp_wfree to use later. */
  148. skb_shinfo(chunk->skb)->destructor_arg = chunk;
  149. asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
  150. sizeof(struct sk_buff) +
  151. sizeof(struct sctp_chunk);
  152. refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  153. sk->sk_wmem_queued += chunk->skb->truesize;
  154. sk_mem_charge(sk, chunk->skb->truesize);
  155. }
  156. static void sctp_clear_owner_w(struct sctp_chunk *chunk)
  157. {
  158. skb_orphan(chunk->skb);
  159. }
  160. static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
  161. void (*cb)(struct sctp_chunk *))
  162. {
  163. struct sctp_outq *q = &asoc->outqueue;
  164. struct sctp_transport *t;
  165. struct sctp_chunk *chunk;
  166. list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
  167. list_for_each_entry(chunk, &t->transmitted, transmitted_list)
  168. cb(chunk);
  169. list_for_each_entry(chunk, &q->retransmit, transmitted_list)
  170. cb(chunk);
  171. list_for_each_entry(chunk, &q->sacked, transmitted_list)
  172. cb(chunk);
  173. list_for_each_entry(chunk, &q->abandoned, transmitted_list)
  174. cb(chunk);
  175. list_for_each_entry(chunk, &q->out_chunk_list, list)
  176. cb(chunk);
  177. }
  178. /* Verify that this is a valid address. */
  179. static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
  180. int len)
  181. {
  182. struct sctp_af *af;
  183. /* Verify basic sockaddr. */
  184. af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
  185. if (!af)
  186. return -EINVAL;
  187. /* Is this a valid SCTP address? */
  188. if (!af->addr_valid(addr, sctp_sk(sk), NULL))
  189. return -EINVAL;
  190. if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
  191. return -EINVAL;
  192. return 0;
  193. }
  194. /* Look up the association by its id. If this is not a UDP-style
  195. * socket, the ID field is always ignored.
  196. */
  197. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
  198. {
  199. struct sctp_association *asoc = NULL;
  200. /* If this is not a UDP-style socket, assoc id should be ignored. */
  201. if (!sctp_style(sk, UDP)) {
  202. /* Return NULL if the socket state is not ESTABLISHED. It
  203. * could be a TCP-style listening socket or a socket which
  204. * hasn't yet called connect() to establish an association.
  205. */
  206. if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
  207. return NULL;
  208. /* Get the first and the only association from the list. */
  209. if (!list_empty(&sctp_sk(sk)->ep->asocs))
  210. asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
  211. struct sctp_association, asocs);
  212. return asoc;
  213. }
  214. /* Otherwise this is a UDP-style socket. */
  215. if (!id || (id == (sctp_assoc_t)-1))
  216. return NULL;
  217. spin_lock_bh(&sctp_assocs_id_lock);
  218. asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
  219. spin_unlock_bh(&sctp_assocs_id_lock);
  220. if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
  221. return NULL;
  222. return asoc;
  223. }
  224. /* Look up the transport from an address and an assoc id. If both address and
  225. * id are specified, the associations matching the address and the id should be
  226. * the same.
  227. */
  228. static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
  229. struct sockaddr_storage *addr,
  230. sctp_assoc_t id)
  231. {
  232. struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
  233. struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
  234. union sctp_addr *laddr = (union sctp_addr *)addr;
  235. struct sctp_transport *transport;
  236. if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
  237. return NULL;
  238. addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
  239. laddr,
  240. &transport);
  241. if (!addr_asoc)
  242. return NULL;
  243. id_asoc = sctp_id2assoc(sk, id);
  244. if (id_asoc && (id_asoc != addr_asoc))
  245. return NULL;
  246. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
  247. (union sctp_addr *)addr);
  248. return transport;
  249. }
  250. /* API 3.1.2 bind() - UDP Style Syntax
  251. * The syntax of bind() is,
  252. *
  253. * ret = bind(int sd, struct sockaddr *addr, int addrlen);
  254. *
  255. * sd - the socket descriptor returned by socket().
  256. * addr - the address structure (struct sockaddr_in or struct
  257. * sockaddr_in6 [RFC 2553]),
  258. * addr_len - the size of the address structure.
  259. */
  260. static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
  261. {
  262. int retval = 0;
  263. lock_sock(sk);
  264. pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
  265. addr, addr_len);
  266. /* Disallow binding twice. */
  267. if (!sctp_sk(sk)->ep->base.bind_addr.port)
  268. retval = sctp_do_bind(sk, (union sctp_addr *)addr,
  269. addr_len);
  270. else
  271. retval = -EINVAL;
  272. release_sock(sk);
  273. return retval;
  274. }
  275. static long sctp_get_port_local(struct sock *, union sctp_addr *);
  276. /* Verify this is a valid sockaddr. */
  277. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  278. union sctp_addr *addr, int len)
  279. {
  280. struct sctp_af *af;
  281. /* Check minimum size. */
  282. if (len < sizeof (struct sockaddr))
  283. return NULL;
  284. /* V4 mapped address are really of AF_INET family */
  285. if (addr->sa.sa_family == AF_INET6 &&
  286. ipv6_addr_v4mapped(&addr->v6.sin6_addr)) {
  287. if (!opt->pf->af_supported(AF_INET, opt))
  288. return NULL;
  289. } else {
  290. /* Does this PF support this AF? */
  291. if (!opt->pf->af_supported(addr->sa.sa_family, opt))
  292. return NULL;
  293. }
  294. /* If we get this far, af is valid. */
  295. af = sctp_get_af_specific(addr->sa.sa_family);
  296. if (len < af->sockaddr_len)
  297. return NULL;
  298. return af;
  299. }
  300. /* Bind a local address either to an endpoint or to an association. */
  301. static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
  302. {
  303. struct net *net = sock_net(sk);
  304. struct sctp_sock *sp = sctp_sk(sk);
  305. struct sctp_endpoint *ep = sp->ep;
  306. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  307. struct sctp_af *af;
  308. unsigned short snum;
  309. int ret = 0;
  310. /* Common sockaddr verification. */
  311. af = sctp_sockaddr_af(sp, addr, len);
  312. if (!af) {
  313. pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
  314. __func__, sk, addr, len);
  315. return -EINVAL;
  316. }
  317. snum = ntohs(addr->v4.sin_port);
  318. pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
  319. __func__, sk, &addr->sa, bp->port, snum, len);
  320. /* PF specific bind() address verification. */
  321. if (!sp->pf->bind_verify(sp, addr))
  322. return -EADDRNOTAVAIL;
  323. /* We must either be unbound, or bind to the same port.
  324. * It's OK to allow 0 ports if we are already bound.
  325. * We'll just inhert an already bound port in this case
  326. */
  327. if (bp->port) {
  328. if (!snum)
  329. snum = bp->port;
  330. else if (snum != bp->port) {
  331. pr_debug("%s: new port %d doesn't match existing port "
  332. "%d\n", __func__, snum, bp->port);
  333. return -EINVAL;
  334. }
  335. }
  336. if (snum && snum < inet_prot_sock(net) &&
  337. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  338. return -EACCES;
  339. /* See if the address matches any of the addresses we may have
  340. * already bound before checking against other endpoints.
  341. */
  342. if (sctp_bind_addr_match(bp, addr, sp))
  343. return -EINVAL;
  344. /* Make sure we are allowed to bind here.
  345. * The function sctp_get_port_local() does duplicate address
  346. * detection.
  347. */
  348. addr->v4.sin_port = htons(snum);
  349. if ((ret = sctp_get_port_local(sk, addr))) {
  350. return -EADDRINUSE;
  351. }
  352. /* Refresh ephemeral port. */
  353. if (!bp->port)
  354. bp->port = inet_sk(sk)->inet_num;
  355. /* Add the address to the bind address list.
  356. * Use GFP_ATOMIC since BHs will be disabled.
  357. */
  358. ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
  359. SCTP_ADDR_SRC, GFP_ATOMIC);
  360. /* Copy back into socket for getsockname() use. */
  361. if (!ret) {
  362. inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
  363. sp->pf->to_sk_saddr(addr, sk);
  364. }
  365. return ret;
  366. }
  367. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  368. *
  369. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  370. * at any one time. If a sender, after sending an ASCONF chunk, decides
  371. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  372. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  373. * subsequent ASCONF. Note this restriction binds each side, so at any
  374. * time two ASCONF may be in-transit on any given association (one sent
  375. * from each endpoint).
  376. */
  377. static int sctp_send_asconf(struct sctp_association *asoc,
  378. struct sctp_chunk *chunk)
  379. {
  380. struct net *net = sock_net(asoc->base.sk);
  381. int retval = 0;
  382. /* If there is an outstanding ASCONF chunk, queue it for later
  383. * transmission.
  384. */
  385. if (asoc->addip_last_asconf) {
  386. list_add_tail(&chunk->list, &asoc->addip_chunk_list);
  387. goto out;
  388. }
  389. /* Hold the chunk until an ASCONF_ACK is received. */
  390. sctp_chunk_hold(chunk);
  391. retval = sctp_primitive_ASCONF(net, asoc, chunk);
  392. if (retval)
  393. sctp_chunk_free(chunk);
  394. else
  395. asoc->addip_last_asconf = chunk;
  396. out:
  397. return retval;
  398. }
  399. /* Add a list of addresses as bind addresses to local endpoint or
  400. * association.
  401. *
  402. * Basically run through each address specified in the addrs/addrcnt
  403. * array/length pair, determine if it is IPv6 or IPv4 and call
  404. * sctp_do_bind() on it.
  405. *
  406. * If any of them fails, then the operation will be reversed and the
  407. * ones that were added will be removed.
  408. *
  409. * Only sctp_setsockopt_bindx() is supposed to call this function.
  410. */
  411. static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  412. {
  413. int cnt;
  414. int retval = 0;
  415. void *addr_buf;
  416. struct sockaddr *sa_addr;
  417. struct sctp_af *af;
  418. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
  419. addrs, addrcnt);
  420. addr_buf = addrs;
  421. for (cnt = 0; cnt < addrcnt; cnt++) {
  422. /* The list may contain either IPv4 or IPv6 address;
  423. * determine the address length for walking thru the list.
  424. */
  425. sa_addr = addr_buf;
  426. af = sctp_get_af_specific(sa_addr->sa_family);
  427. if (!af) {
  428. retval = -EINVAL;
  429. goto err_bindx_add;
  430. }
  431. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  432. af->sockaddr_len);
  433. addr_buf += af->sockaddr_len;
  434. err_bindx_add:
  435. if (retval < 0) {
  436. /* Failed. Cleanup the ones that have been added */
  437. if (cnt > 0)
  438. sctp_bindx_rem(sk, addrs, cnt);
  439. return retval;
  440. }
  441. }
  442. return retval;
  443. }
  444. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  445. * associations that are part of the endpoint indicating that a list of local
  446. * addresses are added to the endpoint.
  447. *
  448. * If any of the addresses is already in the bind address list of the
  449. * association, we do not send the chunk for that association. But it will not
  450. * affect other associations.
  451. *
  452. * Only sctp_setsockopt_bindx() is supposed to call this function.
  453. */
  454. static int sctp_send_asconf_add_ip(struct sock *sk,
  455. struct sockaddr *addrs,
  456. int addrcnt)
  457. {
  458. struct net *net = sock_net(sk);
  459. struct sctp_sock *sp;
  460. struct sctp_endpoint *ep;
  461. struct sctp_association *asoc;
  462. struct sctp_bind_addr *bp;
  463. struct sctp_chunk *chunk;
  464. struct sctp_sockaddr_entry *laddr;
  465. union sctp_addr *addr;
  466. union sctp_addr saveaddr;
  467. void *addr_buf;
  468. struct sctp_af *af;
  469. struct list_head *p;
  470. int i;
  471. int retval = 0;
  472. if (!net->sctp.addip_enable)
  473. return retval;
  474. sp = sctp_sk(sk);
  475. ep = sp->ep;
  476. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  477. __func__, sk, addrs, addrcnt);
  478. list_for_each_entry(asoc, &ep->asocs, asocs) {
  479. if (!asoc->peer.asconf_capable)
  480. continue;
  481. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  482. continue;
  483. if (!sctp_state(asoc, ESTABLISHED))
  484. continue;
  485. /* Check if any address in the packed array of addresses is
  486. * in the bind address list of the association. If so,
  487. * do not send the asconf chunk to its peer, but continue with
  488. * other associations.
  489. */
  490. addr_buf = addrs;
  491. for (i = 0; i < addrcnt; i++) {
  492. addr = addr_buf;
  493. af = sctp_get_af_specific(addr->v4.sin_family);
  494. if (!af) {
  495. retval = -EINVAL;
  496. goto out;
  497. }
  498. if (sctp_assoc_lookup_laddr(asoc, addr))
  499. break;
  500. addr_buf += af->sockaddr_len;
  501. }
  502. if (i < addrcnt)
  503. continue;
  504. /* Use the first valid address in bind addr list of
  505. * association as Address Parameter of ASCONF CHUNK.
  506. */
  507. bp = &asoc->base.bind_addr;
  508. p = bp->address_list.next;
  509. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  510. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  511. addrcnt, SCTP_PARAM_ADD_IP);
  512. if (!chunk) {
  513. retval = -ENOMEM;
  514. goto out;
  515. }
  516. /* Add the new addresses to the bind address list with
  517. * use_as_src set to 0.
  518. */
  519. addr_buf = addrs;
  520. for (i = 0; i < addrcnt; i++) {
  521. addr = addr_buf;
  522. af = sctp_get_af_specific(addr->v4.sin_family);
  523. memcpy(&saveaddr, addr, af->sockaddr_len);
  524. retval = sctp_add_bind_addr(bp, &saveaddr,
  525. sizeof(saveaddr),
  526. SCTP_ADDR_NEW, GFP_ATOMIC);
  527. addr_buf += af->sockaddr_len;
  528. }
  529. if (asoc->src_out_of_asoc_ok) {
  530. struct sctp_transport *trans;
  531. list_for_each_entry(trans,
  532. &asoc->peer.transport_addr_list, transports) {
  533. /* Clear the source and route cache */
  534. sctp_transport_dst_release(trans);
  535. trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
  536. 2*asoc->pathmtu, 4380));
  537. trans->ssthresh = asoc->peer.i.a_rwnd;
  538. trans->rto = asoc->rto_initial;
  539. sctp_max_rto(asoc, trans);
  540. trans->rtt = trans->srtt = trans->rttvar = 0;
  541. sctp_transport_route(trans, NULL,
  542. sctp_sk(asoc->base.sk));
  543. }
  544. }
  545. retval = sctp_send_asconf(asoc, chunk);
  546. }
  547. out:
  548. return retval;
  549. }
  550. /* Remove a list of addresses from bind addresses list. Do not remove the
  551. * last address.
  552. *
  553. * Basically run through each address specified in the addrs/addrcnt
  554. * array/length pair, determine if it is IPv6 or IPv4 and call
  555. * sctp_del_bind() on it.
  556. *
  557. * If any of them fails, then the operation will be reversed and the
  558. * ones that were removed will be added back.
  559. *
  560. * At least one address has to be left; if only one address is
  561. * available, the operation will return -EBUSY.
  562. *
  563. * Only sctp_setsockopt_bindx() is supposed to call this function.
  564. */
  565. static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  566. {
  567. struct sctp_sock *sp = sctp_sk(sk);
  568. struct sctp_endpoint *ep = sp->ep;
  569. int cnt;
  570. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  571. int retval = 0;
  572. void *addr_buf;
  573. union sctp_addr *sa_addr;
  574. struct sctp_af *af;
  575. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  576. __func__, sk, addrs, addrcnt);
  577. addr_buf = addrs;
  578. for (cnt = 0; cnt < addrcnt; cnt++) {
  579. /* If the bind address list is empty or if there is only one
  580. * bind address, there is nothing more to be removed (we need
  581. * at least one address here).
  582. */
  583. if (list_empty(&bp->address_list) ||
  584. (sctp_list_single_entry(&bp->address_list))) {
  585. retval = -EBUSY;
  586. goto err_bindx_rem;
  587. }
  588. sa_addr = addr_buf;
  589. af = sctp_get_af_specific(sa_addr->sa.sa_family);
  590. if (!af) {
  591. retval = -EINVAL;
  592. goto err_bindx_rem;
  593. }
  594. if (!af->addr_valid(sa_addr, sp, NULL)) {
  595. retval = -EADDRNOTAVAIL;
  596. goto err_bindx_rem;
  597. }
  598. if (sa_addr->v4.sin_port &&
  599. sa_addr->v4.sin_port != htons(bp->port)) {
  600. retval = -EINVAL;
  601. goto err_bindx_rem;
  602. }
  603. if (!sa_addr->v4.sin_port)
  604. sa_addr->v4.sin_port = htons(bp->port);
  605. /* FIXME - There is probably a need to check if sk->sk_saddr and
  606. * sk->sk_rcv_addr are currently set to one of the addresses to
  607. * be removed. This is something which needs to be looked into
  608. * when we are fixing the outstanding issues with multi-homing
  609. * socket routing and failover schemes. Refer to comments in
  610. * sctp_do_bind(). -daisy
  611. */
  612. retval = sctp_del_bind_addr(bp, sa_addr);
  613. addr_buf += af->sockaddr_len;
  614. err_bindx_rem:
  615. if (retval < 0) {
  616. /* Failed. Add the ones that has been removed back */
  617. if (cnt > 0)
  618. sctp_bindx_add(sk, addrs, cnt);
  619. return retval;
  620. }
  621. }
  622. return retval;
  623. }
  624. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  625. * the associations that are part of the endpoint indicating that a list of
  626. * local addresses are removed from the endpoint.
  627. *
  628. * If any of the addresses is already in the bind address list of the
  629. * association, we do not send the chunk for that association. But it will not
  630. * affect other associations.
  631. *
  632. * Only sctp_setsockopt_bindx() is supposed to call this function.
  633. */
  634. static int sctp_send_asconf_del_ip(struct sock *sk,
  635. struct sockaddr *addrs,
  636. int addrcnt)
  637. {
  638. struct net *net = sock_net(sk);
  639. struct sctp_sock *sp;
  640. struct sctp_endpoint *ep;
  641. struct sctp_association *asoc;
  642. struct sctp_transport *transport;
  643. struct sctp_bind_addr *bp;
  644. struct sctp_chunk *chunk;
  645. union sctp_addr *laddr;
  646. void *addr_buf;
  647. struct sctp_af *af;
  648. struct sctp_sockaddr_entry *saddr;
  649. int i;
  650. int retval = 0;
  651. int stored = 0;
  652. chunk = NULL;
  653. if (!net->sctp.addip_enable)
  654. return retval;
  655. sp = sctp_sk(sk);
  656. ep = sp->ep;
  657. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  658. __func__, sk, addrs, addrcnt);
  659. list_for_each_entry(asoc, &ep->asocs, asocs) {
  660. if (!asoc->peer.asconf_capable)
  661. continue;
  662. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  663. continue;
  664. if (!sctp_state(asoc, ESTABLISHED))
  665. continue;
  666. /* Check if any address in the packed array of addresses is
  667. * not present in the bind address list of the association.
  668. * If so, do not send the asconf chunk to its peer, but
  669. * continue with other associations.
  670. */
  671. addr_buf = addrs;
  672. for (i = 0; i < addrcnt; i++) {
  673. laddr = addr_buf;
  674. af = sctp_get_af_specific(laddr->v4.sin_family);
  675. if (!af) {
  676. retval = -EINVAL;
  677. goto out;
  678. }
  679. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  680. break;
  681. addr_buf += af->sockaddr_len;
  682. }
  683. if (i < addrcnt)
  684. continue;
  685. /* Find one address in the association's bind address list
  686. * that is not in the packed array of addresses. This is to
  687. * make sure that we do not delete all the addresses in the
  688. * association.
  689. */
  690. bp = &asoc->base.bind_addr;
  691. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  692. addrcnt, sp);
  693. if ((laddr == NULL) && (addrcnt == 1)) {
  694. if (asoc->asconf_addr_del_pending)
  695. continue;
  696. asoc->asconf_addr_del_pending =
  697. kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
  698. if (asoc->asconf_addr_del_pending == NULL) {
  699. retval = -ENOMEM;
  700. goto out;
  701. }
  702. asoc->asconf_addr_del_pending->sa.sa_family =
  703. addrs->sa_family;
  704. asoc->asconf_addr_del_pending->v4.sin_port =
  705. htons(bp->port);
  706. if (addrs->sa_family == AF_INET) {
  707. struct sockaddr_in *sin;
  708. sin = (struct sockaddr_in *)addrs;
  709. asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
  710. } else if (addrs->sa_family == AF_INET6) {
  711. struct sockaddr_in6 *sin6;
  712. sin6 = (struct sockaddr_in6 *)addrs;
  713. asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
  714. }
  715. pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
  716. __func__, asoc, &asoc->asconf_addr_del_pending->sa,
  717. asoc->asconf_addr_del_pending);
  718. asoc->src_out_of_asoc_ok = 1;
  719. stored = 1;
  720. goto skip_mkasconf;
  721. }
  722. if (laddr == NULL)
  723. return -EINVAL;
  724. /* We do not need RCU protection throughout this loop
  725. * because this is done under a socket lock from the
  726. * setsockopt call.
  727. */
  728. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  729. SCTP_PARAM_DEL_IP);
  730. if (!chunk) {
  731. retval = -ENOMEM;
  732. goto out;
  733. }
  734. skip_mkasconf:
  735. /* Reset use_as_src flag for the addresses in the bind address
  736. * list that are to be deleted.
  737. */
  738. addr_buf = addrs;
  739. for (i = 0; i < addrcnt; i++) {
  740. laddr = addr_buf;
  741. af = sctp_get_af_specific(laddr->v4.sin_family);
  742. list_for_each_entry(saddr, &bp->address_list, list) {
  743. if (sctp_cmp_addr_exact(&saddr->a, laddr))
  744. saddr->state = SCTP_ADDR_DEL;
  745. }
  746. addr_buf += af->sockaddr_len;
  747. }
  748. /* Update the route and saddr entries for all the transports
  749. * as some of the addresses in the bind address list are
  750. * about to be deleted and cannot be used as source addresses.
  751. */
  752. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  753. transports) {
  754. sctp_transport_dst_release(transport);
  755. sctp_transport_route(transport, NULL,
  756. sctp_sk(asoc->base.sk));
  757. }
  758. if (stored)
  759. /* We don't need to transmit ASCONF */
  760. continue;
  761. retval = sctp_send_asconf(asoc, chunk);
  762. }
  763. out:
  764. return retval;
  765. }
  766. /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
  767. int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
  768. {
  769. struct sock *sk = sctp_opt2sk(sp);
  770. union sctp_addr *addr;
  771. struct sctp_af *af;
  772. /* It is safe to write port space in caller. */
  773. addr = &addrw->a;
  774. addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
  775. af = sctp_get_af_specific(addr->sa.sa_family);
  776. if (!af)
  777. return -EINVAL;
  778. if (sctp_verify_addr(sk, addr, af->sockaddr_len))
  779. return -EINVAL;
  780. if (addrw->state == SCTP_ADDR_NEW)
  781. return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
  782. else
  783. return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
  784. }
  785. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  786. *
  787. * API 8.1
  788. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  789. * int flags);
  790. *
  791. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  792. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  793. * or IPv6 addresses.
  794. *
  795. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  796. * Section 3.1.2 for this usage.
  797. *
  798. * addrs is a pointer to an array of one or more socket addresses. Each
  799. * address is contained in its appropriate structure (i.e. struct
  800. * sockaddr_in or struct sockaddr_in6) the family of the address type
  801. * must be used to distinguish the address length (note that this
  802. * representation is termed a "packed array" of addresses). The caller
  803. * specifies the number of addresses in the array with addrcnt.
  804. *
  805. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  806. * -1, and sets errno to the appropriate error code.
  807. *
  808. * For SCTP, the port given in each socket address must be the same, or
  809. * sctp_bindx() will fail, setting errno to EINVAL.
  810. *
  811. * The flags parameter is formed from the bitwise OR of zero or more of
  812. * the following currently defined flags:
  813. *
  814. * SCTP_BINDX_ADD_ADDR
  815. *
  816. * SCTP_BINDX_REM_ADDR
  817. *
  818. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  819. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  820. * addresses from the association. The two flags are mutually exclusive;
  821. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  822. * not remove all addresses from an association; sctp_bindx() will
  823. * reject such an attempt with EINVAL.
  824. *
  825. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  826. * additional addresses with an endpoint after calling bind(). Or use
  827. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  828. * socket is associated with so that no new association accepted will be
  829. * associated with those addresses. If the endpoint supports dynamic
  830. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  831. * endpoint to send the appropriate message to the peer to change the
  832. * peers address lists.
  833. *
  834. * Adding and removing addresses from a connected association is
  835. * optional functionality. Implementations that do not support this
  836. * functionality should return EOPNOTSUPP.
  837. *
  838. * Basically do nothing but copying the addresses from user to kernel
  839. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  840. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
  841. * from userspace.
  842. *
  843. * We don't use copy_from_user() for optimization: we first do the
  844. * sanity checks (buffer size -fast- and access check-healthy
  845. * pointer); if all of those succeed, then we can alloc the memory
  846. * (expensive operation) needed to copy the data to kernel. Then we do
  847. * the copying without checking the user space area
  848. * (__copy_from_user()).
  849. *
  850. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  851. * it.
  852. *
  853. * sk The sk of the socket
  854. * addrs The pointer to the addresses in user land
  855. * addrssize Size of the addrs buffer
  856. * op Operation to perform (add or remove, see the flags of
  857. * sctp_bindx)
  858. *
  859. * Returns 0 if ok, <0 errno code on error.
  860. */
  861. static int sctp_setsockopt_bindx(struct sock *sk,
  862. struct sockaddr __user *addrs,
  863. int addrs_size, int op)
  864. {
  865. struct sockaddr *kaddrs;
  866. int err;
  867. int addrcnt = 0;
  868. int walk_size = 0;
  869. struct sockaddr *sa_addr;
  870. void *addr_buf;
  871. struct sctp_af *af;
  872. pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
  873. __func__, sk, addrs, addrs_size, op);
  874. if (unlikely(addrs_size <= 0))
  875. return -EINVAL;
  876. /* Check the user passed a healthy pointer. */
  877. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  878. return -EFAULT;
  879. /* Alloc space for the address array in kernel memory. */
  880. kaddrs = kmalloc(addrs_size, GFP_USER | __GFP_NOWARN);
  881. if (unlikely(!kaddrs))
  882. return -ENOMEM;
  883. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  884. kfree(kaddrs);
  885. return -EFAULT;
  886. }
  887. /* Walk through the addrs buffer and count the number of addresses. */
  888. addr_buf = kaddrs;
  889. while (walk_size < addrs_size) {
  890. if (walk_size + sizeof(sa_family_t) > addrs_size) {
  891. kfree(kaddrs);
  892. return -EINVAL;
  893. }
  894. sa_addr = addr_buf;
  895. af = sctp_get_af_specific(sa_addr->sa_family);
  896. /* If the address family is not supported or if this address
  897. * causes the address buffer to overflow return EINVAL.
  898. */
  899. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  900. kfree(kaddrs);
  901. return -EINVAL;
  902. }
  903. addrcnt++;
  904. addr_buf += af->sockaddr_len;
  905. walk_size += af->sockaddr_len;
  906. }
  907. /* Do the work. */
  908. switch (op) {
  909. case SCTP_BINDX_ADD_ADDR:
  910. err = sctp_bindx_add(sk, kaddrs, addrcnt);
  911. if (err)
  912. goto out;
  913. err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
  914. break;
  915. case SCTP_BINDX_REM_ADDR:
  916. err = sctp_bindx_rem(sk, kaddrs, addrcnt);
  917. if (err)
  918. goto out;
  919. err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
  920. break;
  921. default:
  922. err = -EINVAL;
  923. break;
  924. }
  925. out:
  926. kfree(kaddrs);
  927. return err;
  928. }
  929. /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
  930. *
  931. * Common routine for handling connect() and sctp_connectx().
  932. * Connect will come in with just a single address.
  933. */
  934. static int __sctp_connect(struct sock *sk,
  935. struct sockaddr *kaddrs,
  936. int addrs_size,
  937. sctp_assoc_t *assoc_id)
  938. {
  939. struct net *net = sock_net(sk);
  940. struct sctp_sock *sp;
  941. struct sctp_endpoint *ep;
  942. struct sctp_association *asoc = NULL;
  943. struct sctp_association *asoc2;
  944. struct sctp_transport *transport;
  945. union sctp_addr to;
  946. enum sctp_scope scope;
  947. long timeo;
  948. int err = 0;
  949. int addrcnt = 0;
  950. int walk_size = 0;
  951. union sctp_addr *sa_addr = NULL;
  952. void *addr_buf;
  953. unsigned short port;
  954. unsigned int f_flags = 0;
  955. sp = sctp_sk(sk);
  956. ep = sp->ep;
  957. /* connect() cannot be done on a socket that is already in ESTABLISHED
  958. * state - UDP-style peeled off socket or a TCP-style socket that
  959. * is already connected.
  960. * It cannot be done even on a TCP-style listening socket.
  961. */
  962. if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
  963. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
  964. err = -EISCONN;
  965. goto out_free;
  966. }
  967. /* Walk through the addrs buffer and count the number of addresses. */
  968. addr_buf = kaddrs;
  969. while (walk_size < addrs_size) {
  970. struct sctp_af *af;
  971. if (walk_size + sizeof(sa_family_t) > addrs_size) {
  972. err = -EINVAL;
  973. goto out_free;
  974. }
  975. sa_addr = addr_buf;
  976. af = sctp_get_af_specific(sa_addr->sa.sa_family);
  977. /* If the address family is not supported or if this address
  978. * causes the address buffer to overflow return EINVAL.
  979. */
  980. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  981. err = -EINVAL;
  982. goto out_free;
  983. }
  984. port = ntohs(sa_addr->v4.sin_port);
  985. /* Save current address so we can work with it */
  986. memcpy(&to, sa_addr, af->sockaddr_len);
  987. err = sctp_verify_addr(sk, &to, af->sockaddr_len);
  988. if (err)
  989. goto out_free;
  990. /* Make sure the destination port is correctly set
  991. * in all addresses.
  992. */
  993. if (asoc && asoc->peer.port && asoc->peer.port != port) {
  994. err = -EINVAL;
  995. goto out_free;
  996. }
  997. /* Check if there already is a matching association on the
  998. * endpoint (other than the one created here).
  999. */
  1000. asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  1001. if (asoc2 && asoc2 != asoc) {
  1002. if (asoc2->state >= SCTP_STATE_ESTABLISHED)
  1003. err = -EISCONN;
  1004. else
  1005. err = -EALREADY;
  1006. goto out_free;
  1007. }
  1008. /* If we could not find a matching association on the endpoint,
  1009. * make sure that there is no peeled-off association matching
  1010. * the peer address even on another socket.
  1011. */
  1012. if (sctp_endpoint_is_peeled_off(ep, &to)) {
  1013. err = -EADDRNOTAVAIL;
  1014. goto out_free;
  1015. }
  1016. if (!asoc) {
  1017. /* If a bind() or sctp_bindx() is not called prior to
  1018. * an sctp_connectx() call, the system picks an
  1019. * ephemeral port and will choose an address set
  1020. * equivalent to binding with a wildcard address.
  1021. */
  1022. if (!ep->base.bind_addr.port) {
  1023. if (sctp_autobind(sk)) {
  1024. err = -EAGAIN;
  1025. goto out_free;
  1026. }
  1027. } else {
  1028. /*
  1029. * If an unprivileged user inherits a 1-many
  1030. * style socket with open associations on a
  1031. * privileged port, it MAY be permitted to
  1032. * accept new associations, but it SHOULD NOT
  1033. * be permitted to open new associations.
  1034. */
  1035. if (ep->base.bind_addr.port <
  1036. inet_prot_sock(net) &&
  1037. !ns_capable(net->user_ns,
  1038. CAP_NET_BIND_SERVICE)) {
  1039. err = -EACCES;
  1040. goto out_free;
  1041. }
  1042. }
  1043. scope = sctp_scope(&to);
  1044. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  1045. if (!asoc) {
  1046. err = -ENOMEM;
  1047. goto out_free;
  1048. }
  1049. err = sctp_assoc_set_bind_addr_from_ep(asoc, scope,
  1050. GFP_KERNEL);
  1051. if (err < 0) {
  1052. goto out_free;
  1053. }
  1054. }
  1055. /* Prime the peer's transport structures. */
  1056. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
  1057. SCTP_UNKNOWN);
  1058. if (!transport) {
  1059. err = -ENOMEM;
  1060. goto out_free;
  1061. }
  1062. addrcnt++;
  1063. addr_buf += af->sockaddr_len;
  1064. walk_size += af->sockaddr_len;
  1065. }
  1066. /* In case the user of sctp_connectx() wants an association
  1067. * id back, assign one now.
  1068. */
  1069. if (assoc_id) {
  1070. err = sctp_assoc_set_id(asoc, GFP_KERNEL);
  1071. if (err < 0)
  1072. goto out_free;
  1073. }
  1074. err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
  1075. if (err < 0) {
  1076. goto out_free;
  1077. }
  1078. /* Initialize sk's dport and daddr for getpeername() */
  1079. inet_sk(sk)->inet_dport = htons(asoc->peer.port);
  1080. sp->pf->to_sk_daddr(sa_addr, sk);
  1081. sk->sk_err = 0;
  1082. /* in-kernel sockets don't generally have a file allocated to them
  1083. * if all they do is call sock_create_kern().
  1084. */
  1085. if (sk->sk_socket->file)
  1086. f_flags = sk->sk_socket->file->f_flags;
  1087. timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
  1088. if (assoc_id)
  1089. *assoc_id = asoc->assoc_id;
  1090. err = sctp_wait_for_connect(asoc, &timeo);
  1091. /* Note: the asoc may be freed after the return of
  1092. * sctp_wait_for_connect.
  1093. */
  1094. /* Don't free association on exit. */
  1095. asoc = NULL;
  1096. out_free:
  1097. pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
  1098. __func__, asoc, kaddrs, err);
  1099. if (asoc) {
  1100. /* sctp_primitive_ASSOCIATE may have added this association
  1101. * To the hash table, try to unhash it, just in case, its a noop
  1102. * if it wasn't hashed so we're safe
  1103. */
  1104. sctp_association_free(asoc);
  1105. }
  1106. return err;
  1107. }
  1108. /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
  1109. *
  1110. * API 8.9
  1111. * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
  1112. * sctp_assoc_t *asoc);
  1113. *
  1114. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  1115. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  1116. * or IPv6 addresses.
  1117. *
  1118. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  1119. * Section 3.1.2 for this usage.
  1120. *
  1121. * addrs is a pointer to an array of one or more socket addresses. Each
  1122. * address is contained in its appropriate structure (i.e. struct
  1123. * sockaddr_in or struct sockaddr_in6) the family of the address type
  1124. * must be used to distengish the address length (note that this
  1125. * representation is termed a "packed array" of addresses). The caller
  1126. * specifies the number of addresses in the array with addrcnt.
  1127. *
  1128. * On success, sctp_connectx() returns 0. It also sets the assoc_id to
  1129. * the association id of the new association. On failure, sctp_connectx()
  1130. * returns -1, and sets errno to the appropriate error code. The assoc_id
  1131. * is not touched by the kernel.
  1132. *
  1133. * For SCTP, the port given in each socket address must be the same, or
  1134. * sctp_connectx() will fail, setting errno to EINVAL.
  1135. *
  1136. * An application can use sctp_connectx to initiate an association with
  1137. * an endpoint that is multi-homed. Much like sctp_bindx() this call
  1138. * allows a caller to specify multiple addresses at which a peer can be
  1139. * reached. The way the SCTP stack uses the list of addresses to set up
  1140. * the association is implementation dependent. This function only
  1141. * specifies that the stack will try to make use of all the addresses in
  1142. * the list when needed.
  1143. *
  1144. * Note that the list of addresses passed in is only used for setting up
  1145. * the association. It does not necessarily equal the set of addresses
  1146. * the peer uses for the resulting association. If the caller wants to
  1147. * find out the set of peer addresses, it must use sctp_getpaddrs() to
  1148. * retrieve them after the association has been set up.
  1149. *
  1150. * Basically do nothing but copying the addresses from user to kernel
  1151. * land and invoking either sctp_connectx(). This is used for tunneling
  1152. * the sctp_connectx() request through sctp_setsockopt() from userspace.
  1153. *
  1154. * We don't use copy_from_user() for optimization: we first do the
  1155. * sanity checks (buffer size -fast- and access check-healthy
  1156. * pointer); if all of those succeed, then we can alloc the memory
  1157. * (expensive operation) needed to copy the data to kernel. Then we do
  1158. * the copying without checking the user space area
  1159. * (__copy_from_user()).
  1160. *
  1161. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  1162. * it.
  1163. *
  1164. * sk The sk of the socket
  1165. * addrs The pointer to the addresses in user land
  1166. * addrssize Size of the addrs buffer
  1167. *
  1168. * Returns >=0 if ok, <0 errno code on error.
  1169. */
  1170. static int __sctp_setsockopt_connectx(struct sock *sk,
  1171. struct sockaddr __user *addrs,
  1172. int addrs_size,
  1173. sctp_assoc_t *assoc_id)
  1174. {
  1175. struct sockaddr *kaddrs;
  1176. gfp_t gfp = GFP_KERNEL;
  1177. int err = 0;
  1178. pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
  1179. __func__, sk, addrs, addrs_size);
  1180. if (unlikely(addrs_size <= 0))
  1181. return -EINVAL;
  1182. /* Check the user passed a healthy pointer. */
  1183. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  1184. return -EFAULT;
  1185. /* Alloc space for the address array in kernel memory. */
  1186. if (sk->sk_socket->file)
  1187. gfp = GFP_USER | __GFP_NOWARN;
  1188. kaddrs = kmalloc(addrs_size, gfp);
  1189. if (unlikely(!kaddrs))
  1190. return -ENOMEM;
  1191. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  1192. err = -EFAULT;
  1193. } else {
  1194. err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id);
  1195. }
  1196. kfree(kaddrs);
  1197. return err;
  1198. }
  1199. /*
  1200. * This is an older interface. It's kept for backward compatibility
  1201. * to the option that doesn't provide association id.
  1202. */
  1203. static int sctp_setsockopt_connectx_old(struct sock *sk,
  1204. struct sockaddr __user *addrs,
  1205. int addrs_size)
  1206. {
  1207. return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL);
  1208. }
  1209. /*
  1210. * New interface for the API. The since the API is done with a socket
  1211. * option, to make it simple we feed back the association id is as a return
  1212. * indication to the call. Error is always negative and association id is
  1213. * always positive.
  1214. */
  1215. static int sctp_setsockopt_connectx(struct sock *sk,
  1216. struct sockaddr __user *addrs,
  1217. int addrs_size)
  1218. {
  1219. sctp_assoc_t assoc_id = 0;
  1220. int err = 0;
  1221. err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id);
  1222. if (err)
  1223. return err;
  1224. else
  1225. return assoc_id;
  1226. }
  1227. /*
  1228. * New (hopefully final) interface for the API.
  1229. * We use the sctp_getaddrs_old structure so that use-space library
  1230. * can avoid any unnecessary allocations. The only different part
  1231. * is that we store the actual length of the address buffer into the
  1232. * addrs_num structure member. That way we can re-use the existing
  1233. * code.
  1234. */
  1235. #ifdef CONFIG_COMPAT
  1236. struct compat_sctp_getaddrs_old {
  1237. sctp_assoc_t assoc_id;
  1238. s32 addr_num;
  1239. compat_uptr_t addrs; /* struct sockaddr * */
  1240. };
  1241. #endif
  1242. static int sctp_getsockopt_connectx3(struct sock *sk, int len,
  1243. char __user *optval,
  1244. int __user *optlen)
  1245. {
  1246. struct sctp_getaddrs_old param;
  1247. sctp_assoc_t assoc_id = 0;
  1248. int err = 0;
  1249. #ifdef CONFIG_COMPAT
  1250. if (in_compat_syscall()) {
  1251. struct compat_sctp_getaddrs_old param32;
  1252. if (len < sizeof(param32))
  1253. return -EINVAL;
  1254. if (copy_from_user(&param32, optval, sizeof(param32)))
  1255. return -EFAULT;
  1256. param.assoc_id = param32.assoc_id;
  1257. param.addr_num = param32.addr_num;
  1258. param.addrs = compat_ptr(param32.addrs);
  1259. } else
  1260. #endif
  1261. {
  1262. if (len < sizeof(param))
  1263. return -EINVAL;
  1264. if (copy_from_user(&param, optval, sizeof(param)))
  1265. return -EFAULT;
  1266. }
  1267. err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)
  1268. param.addrs, param.addr_num,
  1269. &assoc_id);
  1270. if (err == 0 || err == -EINPROGRESS) {
  1271. if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
  1272. return -EFAULT;
  1273. if (put_user(sizeof(assoc_id), optlen))
  1274. return -EFAULT;
  1275. }
  1276. return err;
  1277. }
  1278. /* API 3.1.4 close() - UDP Style Syntax
  1279. * Applications use close() to perform graceful shutdown (as described in
  1280. * Section 10.1 of [SCTP]) on ALL the associations currently represented
  1281. * by a UDP-style socket.
  1282. *
  1283. * The syntax is
  1284. *
  1285. * ret = close(int sd);
  1286. *
  1287. * sd - the socket descriptor of the associations to be closed.
  1288. *
  1289. * To gracefully shutdown a specific association represented by the
  1290. * UDP-style socket, an application should use the sendmsg() call,
  1291. * passing no user data, but including the appropriate flag in the
  1292. * ancillary data (see Section xxxx).
  1293. *
  1294. * If sd in the close() call is a branched-off socket representing only
  1295. * one association, the shutdown is performed on that association only.
  1296. *
  1297. * 4.1.6 close() - TCP Style Syntax
  1298. *
  1299. * Applications use close() to gracefully close down an association.
  1300. *
  1301. * The syntax is:
  1302. *
  1303. * int close(int sd);
  1304. *
  1305. * sd - the socket descriptor of the association to be closed.
  1306. *
  1307. * After an application calls close() on a socket descriptor, no further
  1308. * socket operations will succeed on that descriptor.
  1309. *
  1310. * API 7.1.4 SO_LINGER
  1311. *
  1312. * An application using the TCP-style socket can use this option to
  1313. * perform the SCTP ABORT primitive. The linger option structure is:
  1314. *
  1315. * struct linger {
  1316. * int l_onoff; // option on/off
  1317. * int l_linger; // linger time
  1318. * };
  1319. *
  1320. * To enable the option, set l_onoff to 1. If the l_linger value is set
  1321. * to 0, calling close() is the same as the ABORT primitive. If the
  1322. * value is set to a negative value, the setsockopt() call will return
  1323. * an error. If the value is set to a positive value linger_time, the
  1324. * close() can be blocked for at most linger_time ms. If the graceful
  1325. * shutdown phase does not finish during this period, close() will
  1326. * return but the graceful shutdown phase continues in the system.
  1327. */
  1328. static void sctp_close(struct sock *sk, long timeout)
  1329. {
  1330. struct net *net = sock_net(sk);
  1331. struct sctp_endpoint *ep;
  1332. struct sctp_association *asoc;
  1333. struct list_head *pos, *temp;
  1334. unsigned int data_was_unread;
  1335. pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
  1336. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  1337. sk->sk_shutdown = SHUTDOWN_MASK;
  1338. sk->sk_state = SCTP_SS_CLOSING;
  1339. ep = sctp_sk(sk)->ep;
  1340. /* Clean up any skbs sitting on the receive queue. */
  1341. data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
  1342. data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
  1343. /* Walk all associations on an endpoint. */
  1344. list_for_each_safe(pos, temp, &ep->asocs) {
  1345. asoc = list_entry(pos, struct sctp_association, asocs);
  1346. if (sctp_style(sk, TCP)) {
  1347. /* A closed association can still be in the list if
  1348. * it belongs to a TCP-style listening socket that is
  1349. * not yet accepted. If so, free it. If not, send an
  1350. * ABORT or SHUTDOWN based on the linger options.
  1351. */
  1352. if (sctp_state(asoc, CLOSED)) {
  1353. sctp_association_free(asoc);
  1354. continue;
  1355. }
  1356. }
  1357. if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
  1358. !skb_queue_empty(&asoc->ulpq.reasm) ||
  1359. (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
  1360. struct sctp_chunk *chunk;
  1361. chunk = sctp_make_abort_user(asoc, NULL, 0);
  1362. sctp_primitive_ABORT(net, asoc, chunk);
  1363. } else
  1364. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  1365. }
  1366. /* On a TCP-style socket, block for at most linger_time if set. */
  1367. if (sctp_style(sk, TCP) && timeout)
  1368. sctp_wait_for_close(sk, timeout);
  1369. /* This will run the backlog queue. */
  1370. release_sock(sk);
  1371. /* Supposedly, no process has access to the socket, but
  1372. * the net layers still may.
  1373. * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
  1374. * held and that should be grabbed before socket lock.
  1375. */
  1376. spin_lock_bh(&net->sctp.addr_wq_lock);
  1377. bh_lock_sock_nested(sk);
  1378. /* Hold the sock, since sk_common_release() will put sock_put()
  1379. * and we have just a little more cleanup.
  1380. */
  1381. sock_hold(sk);
  1382. sk_common_release(sk);
  1383. bh_unlock_sock(sk);
  1384. spin_unlock_bh(&net->sctp.addr_wq_lock);
  1385. sock_put(sk);
  1386. SCTP_DBG_OBJCNT_DEC(sock);
  1387. }
  1388. /* Handle EPIPE error. */
  1389. static int sctp_error(struct sock *sk, int flags, int err)
  1390. {
  1391. if (err == -EPIPE)
  1392. err = sock_error(sk) ? : -EPIPE;
  1393. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  1394. send_sig(SIGPIPE, current, 0);
  1395. return err;
  1396. }
  1397. /* API 3.1.3 sendmsg() - UDP Style Syntax
  1398. *
  1399. * An application uses sendmsg() and recvmsg() calls to transmit data to
  1400. * and receive data from its peer.
  1401. *
  1402. * ssize_t sendmsg(int socket, const struct msghdr *message,
  1403. * int flags);
  1404. *
  1405. * socket - the socket descriptor of the endpoint.
  1406. * message - pointer to the msghdr structure which contains a single
  1407. * user message and possibly some ancillary data.
  1408. *
  1409. * See Section 5 for complete description of the data
  1410. * structures.
  1411. *
  1412. * flags - flags sent or received with the user message, see Section
  1413. * 5 for complete description of the flags.
  1414. *
  1415. * Note: This function could use a rewrite especially when explicit
  1416. * connect support comes in.
  1417. */
  1418. /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
  1419. static int sctp_msghdr_parse(const struct msghdr *msg,
  1420. struct sctp_cmsgs *cmsgs);
  1421. static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
  1422. {
  1423. struct net *net = sock_net(sk);
  1424. struct sctp_sock *sp;
  1425. struct sctp_endpoint *ep;
  1426. struct sctp_association *new_asoc = NULL, *asoc = NULL;
  1427. struct sctp_transport *transport, *chunk_tp;
  1428. struct sctp_chunk *chunk;
  1429. union sctp_addr to;
  1430. struct sockaddr *msg_name = NULL;
  1431. struct sctp_sndrcvinfo default_sinfo;
  1432. struct sctp_sndrcvinfo *sinfo;
  1433. struct sctp_initmsg *sinit;
  1434. sctp_assoc_t associd = 0;
  1435. struct sctp_cmsgs cmsgs = { NULL };
  1436. enum sctp_scope scope;
  1437. bool fill_sinfo_ttl = false, wait_connect = false;
  1438. struct sctp_datamsg *datamsg;
  1439. int msg_flags = msg->msg_flags;
  1440. __u16 sinfo_flags = 0;
  1441. long timeo;
  1442. int err;
  1443. err = 0;
  1444. sp = sctp_sk(sk);
  1445. ep = sp->ep;
  1446. pr_debug("%s: sk:%p, msg:%p, msg_len:%zu ep:%p\n", __func__, sk,
  1447. msg, msg_len, ep);
  1448. /* We cannot send a message over a TCP-style listening socket. */
  1449. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
  1450. err = -EPIPE;
  1451. goto out_nounlock;
  1452. }
  1453. /* Parse out the SCTP CMSGs. */
  1454. err = sctp_msghdr_parse(msg, &cmsgs);
  1455. if (err) {
  1456. pr_debug("%s: msghdr parse err:%x\n", __func__, err);
  1457. goto out_nounlock;
  1458. }
  1459. /* Fetch the destination address for this packet. This
  1460. * address only selects the association--it is not necessarily
  1461. * the address we will send to.
  1462. * For a peeled-off socket, msg_name is ignored.
  1463. */
  1464. if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
  1465. int msg_namelen = msg->msg_namelen;
  1466. err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
  1467. msg_namelen);
  1468. if (err)
  1469. return err;
  1470. if (msg_namelen > sizeof(to))
  1471. msg_namelen = sizeof(to);
  1472. memcpy(&to, msg->msg_name, msg_namelen);
  1473. msg_name = msg->msg_name;
  1474. }
  1475. sinit = cmsgs.init;
  1476. if (cmsgs.sinfo != NULL) {
  1477. memset(&default_sinfo, 0, sizeof(default_sinfo));
  1478. default_sinfo.sinfo_stream = cmsgs.sinfo->snd_sid;
  1479. default_sinfo.sinfo_flags = cmsgs.sinfo->snd_flags;
  1480. default_sinfo.sinfo_ppid = cmsgs.sinfo->snd_ppid;
  1481. default_sinfo.sinfo_context = cmsgs.sinfo->snd_context;
  1482. default_sinfo.sinfo_assoc_id = cmsgs.sinfo->snd_assoc_id;
  1483. sinfo = &default_sinfo;
  1484. fill_sinfo_ttl = true;
  1485. } else {
  1486. sinfo = cmsgs.srinfo;
  1487. }
  1488. /* Did the user specify SNDINFO/SNDRCVINFO? */
  1489. if (sinfo) {
  1490. sinfo_flags = sinfo->sinfo_flags;
  1491. associd = sinfo->sinfo_assoc_id;
  1492. }
  1493. pr_debug("%s: msg_len:%zu, sinfo_flags:0x%x\n", __func__,
  1494. msg_len, sinfo_flags);
  1495. /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
  1496. if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
  1497. err = -EINVAL;
  1498. goto out_nounlock;
  1499. }
  1500. /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
  1501. * length messages when SCTP_EOF|SCTP_ABORT is not set.
  1502. * If SCTP_ABORT is set, the message length could be non zero with
  1503. * the msg_iov set to the user abort reason.
  1504. */
  1505. if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
  1506. (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
  1507. err = -EINVAL;
  1508. goto out_nounlock;
  1509. }
  1510. /* If SCTP_ADDR_OVER is set, there must be an address
  1511. * specified in msg_name.
  1512. */
  1513. if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
  1514. err = -EINVAL;
  1515. goto out_nounlock;
  1516. }
  1517. transport = NULL;
  1518. pr_debug("%s: about to look up association\n", __func__);
  1519. lock_sock(sk);
  1520. /* If a msg_name has been specified, assume this is to be used. */
  1521. if (msg_name) {
  1522. /* Look for a matching association on the endpoint. */
  1523. asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  1524. /* If we could not find a matching association on the
  1525. * endpoint, make sure that it is not a TCP-style
  1526. * socket that already has an association or there is
  1527. * no peeled-off association on another socket.
  1528. */
  1529. if (!asoc &&
  1530. ((sctp_style(sk, TCP) &&
  1531. (sctp_sstate(sk, ESTABLISHED) ||
  1532. sctp_sstate(sk, CLOSING))) ||
  1533. sctp_endpoint_is_peeled_off(ep, &to))) {
  1534. err = -EADDRNOTAVAIL;
  1535. goto out_unlock;
  1536. }
  1537. } else {
  1538. asoc = sctp_id2assoc(sk, associd);
  1539. if (!asoc) {
  1540. err = -EPIPE;
  1541. goto out_unlock;
  1542. }
  1543. }
  1544. if (asoc) {
  1545. pr_debug("%s: just looked up association:%p\n", __func__, asoc);
  1546. /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
  1547. * socket that has an association in CLOSED state. This can
  1548. * happen when an accepted socket has an association that is
  1549. * already CLOSED.
  1550. */
  1551. if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
  1552. err = -EPIPE;
  1553. goto out_unlock;
  1554. }
  1555. if (sinfo_flags & SCTP_EOF) {
  1556. pr_debug("%s: shutting down association:%p\n",
  1557. __func__, asoc);
  1558. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  1559. err = 0;
  1560. goto out_unlock;
  1561. }
  1562. if (sinfo_flags & SCTP_ABORT) {
  1563. chunk = sctp_make_abort_user(asoc, msg, msg_len);
  1564. if (!chunk) {
  1565. err = -ENOMEM;
  1566. goto out_unlock;
  1567. }
  1568. pr_debug("%s: aborting association:%p\n",
  1569. __func__, asoc);
  1570. sctp_primitive_ABORT(net, asoc, chunk);
  1571. err = 0;
  1572. goto out_unlock;
  1573. }
  1574. }
  1575. /* Do we need to create the association? */
  1576. if (!asoc) {
  1577. pr_debug("%s: there is no association yet\n", __func__);
  1578. if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
  1579. err = -EINVAL;
  1580. goto out_unlock;
  1581. }
  1582. /* Check for invalid stream against the stream counts,
  1583. * either the default or the user specified stream counts.
  1584. */
  1585. if (sinfo) {
  1586. if (!sinit || !sinit->sinit_num_ostreams) {
  1587. /* Check against the defaults. */
  1588. if (sinfo->sinfo_stream >=
  1589. sp->initmsg.sinit_num_ostreams) {
  1590. err = -EINVAL;
  1591. goto out_unlock;
  1592. }
  1593. } else {
  1594. /* Check against the requested. */
  1595. if (sinfo->sinfo_stream >=
  1596. sinit->sinit_num_ostreams) {
  1597. err = -EINVAL;
  1598. goto out_unlock;
  1599. }
  1600. }
  1601. }
  1602. /*
  1603. * API 3.1.2 bind() - UDP Style Syntax
  1604. * If a bind() or sctp_bindx() is not called prior to a
  1605. * sendmsg() call that initiates a new association, the
  1606. * system picks an ephemeral port and will choose an address
  1607. * set equivalent to binding with a wildcard address.
  1608. */
  1609. if (!ep->base.bind_addr.port) {
  1610. if (sctp_autobind(sk)) {
  1611. err = -EAGAIN;
  1612. goto out_unlock;
  1613. }
  1614. } else {
  1615. /*
  1616. * If an unprivileged user inherits a one-to-many
  1617. * style socket with open associations on a privileged
  1618. * port, it MAY be permitted to accept new associations,
  1619. * but it SHOULD NOT be permitted to open new
  1620. * associations.
  1621. */
  1622. if (ep->base.bind_addr.port < inet_prot_sock(net) &&
  1623. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) {
  1624. err = -EACCES;
  1625. goto out_unlock;
  1626. }
  1627. }
  1628. scope = sctp_scope(&to);
  1629. new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  1630. if (!new_asoc) {
  1631. err = -ENOMEM;
  1632. goto out_unlock;
  1633. }
  1634. asoc = new_asoc;
  1635. err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
  1636. if (err < 0) {
  1637. err = -ENOMEM;
  1638. goto out_free;
  1639. }
  1640. /* If the SCTP_INIT ancillary data is specified, set all
  1641. * the association init values accordingly.
  1642. */
  1643. if (sinit) {
  1644. if (sinit->sinit_num_ostreams) {
  1645. asoc->c.sinit_num_ostreams =
  1646. sinit->sinit_num_ostreams;
  1647. }
  1648. if (sinit->sinit_max_instreams) {
  1649. asoc->c.sinit_max_instreams =
  1650. sinit->sinit_max_instreams;
  1651. }
  1652. if (sinit->sinit_max_attempts) {
  1653. asoc->max_init_attempts
  1654. = sinit->sinit_max_attempts;
  1655. }
  1656. if (sinit->sinit_max_init_timeo) {
  1657. asoc->max_init_timeo =
  1658. msecs_to_jiffies(sinit->sinit_max_init_timeo);
  1659. }
  1660. }
  1661. /* Prime the peer's transport structures. */
  1662. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
  1663. if (!transport) {
  1664. err = -ENOMEM;
  1665. goto out_free;
  1666. }
  1667. }
  1668. /* ASSERT: we have a valid association at this point. */
  1669. pr_debug("%s: we have a valid association\n", __func__);
  1670. if (!sinfo) {
  1671. /* If the user didn't specify SNDINFO/SNDRCVINFO, make up
  1672. * one with some defaults.
  1673. */
  1674. memset(&default_sinfo, 0, sizeof(default_sinfo));
  1675. default_sinfo.sinfo_stream = asoc->default_stream;
  1676. default_sinfo.sinfo_flags = asoc->default_flags;
  1677. default_sinfo.sinfo_ppid = asoc->default_ppid;
  1678. default_sinfo.sinfo_context = asoc->default_context;
  1679. default_sinfo.sinfo_timetolive = asoc->default_timetolive;
  1680. default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
  1681. sinfo = &default_sinfo;
  1682. } else if (fill_sinfo_ttl) {
  1683. /* In case SNDINFO was specified, we still need to fill
  1684. * it with a default ttl from the assoc here.
  1685. */
  1686. sinfo->sinfo_timetolive = asoc->default_timetolive;
  1687. }
  1688. /* API 7.1.7, the sndbuf size per association bounds the
  1689. * maximum size of data that can be sent in a single send call.
  1690. */
  1691. if (msg_len > sk->sk_sndbuf) {
  1692. err = -EMSGSIZE;
  1693. goto out_free;
  1694. }
  1695. if (asoc->pmtu_pending)
  1696. sctp_assoc_pending_pmtu(asoc);
  1697. /* If fragmentation is disabled and the message length exceeds the
  1698. * association fragmentation point, return EMSGSIZE. The I-D
  1699. * does not specify what this error is, but this looks like
  1700. * a great fit.
  1701. */
  1702. if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
  1703. err = -EMSGSIZE;
  1704. goto out_free;
  1705. }
  1706. /* Check for invalid stream. */
  1707. if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
  1708. err = -EINVAL;
  1709. goto out_free;
  1710. }
  1711. /* Allocate sctp_stream_out_ext if not already done */
  1712. if (unlikely(!asoc->stream.out[sinfo->sinfo_stream].ext)) {
  1713. err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
  1714. if (err)
  1715. goto out_free;
  1716. }
  1717. if (sctp_wspace(asoc) < msg_len)
  1718. sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
  1719. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1720. if (!sctp_wspace(asoc)) {
  1721. /* sk can be changed by peel off when waiting for buf. */
  1722. err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len, &sk);
  1723. if (err) {
  1724. if (err == -ESRCH) {
  1725. /* asoc is already dead. */
  1726. new_asoc = NULL;
  1727. err = -EPIPE;
  1728. }
  1729. goto out_free;
  1730. }
  1731. }
  1732. /* If an address is passed with the sendto/sendmsg call, it is used
  1733. * to override the primary destination address in the TCP model, or
  1734. * when SCTP_ADDR_OVER flag is set in the UDP model.
  1735. */
  1736. if ((sctp_style(sk, TCP) && msg_name) ||
  1737. (sinfo_flags & SCTP_ADDR_OVER)) {
  1738. chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
  1739. if (!chunk_tp) {
  1740. err = -EINVAL;
  1741. goto out_free;
  1742. }
  1743. } else
  1744. chunk_tp = NULL;
  1745. /* Auto-connect, if we aren't connected already. */
  1746. if (sctp_state(asoc, CLOSED)) {
  1747. err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
  1748. if (err < 0)
  1749. goto out_free;
  1750. wait_connect = true;
  1751. pr_debug("%s: we associated primitively\n", __func__);
  1752. }
  1753. /* Break the message into multiple chunks of maximum size. */
  1754. datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
  1755. if (IS_ERR(datamsg)) {
  1756. err = PTR_ERR(datamsg);
  1757. goto out_free;
  1758. }
  1759. asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
  1760. /* Now send the (possibly) fragmented message. */
  1761. list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
  1762. sctp_chunk_hold(chunk);
  1763. /* Do accounting for the write space. */
  1764. sctp_set_owner_w(chunk);
  1765. chunk->transport = chunk_tp;
  1766. }
  1767. /* Send it to the lower layers. Note: all chunks
  1768. * must either fail or succeed. The lower layer
  1769. * works that way today. Keep it that way or this
  1770. * breaks.
  1771. */
  1772. err = sctp_primitive_SEND(net, asoc, datamsg);
  1773. /* Did the lower layer accept the chunk? */
  1774. if (err) {
  1775. sctp_datamsg_free(datamsg);
  1776. goto out_free;
  1777. }
  1778. pr_debug("%s: we sent primitively\n", __func__);
  1779. sctp_datamsg_put(datamsg);
  1780. err = msg_len;
  1781. if (unlikely(wait_connect)) {
  1782. timeo = sock_sndtimeo(sk, msg_flags & MSG_DONTWAIT);
  1783. sctp_wait_for_connect(asoc, &timeo);
  1784. }
  1785. /* If we are already past ASSOCIATE, the lower
  1786. * layers are responsible for association cleanup.
  1787. */
  1788. goto out_unlock;
  1789. out_free:
  1790. if (new_asoc)
  1791. sctp_association_free(asoc);
  1792. out_unlock:
  1793. release_sock(sk);
  1794. out_nounlock:
  1795. return sctp_error(sk, msg_flags, err);
  1796. #if 0
  1797. do_sock_err:
  1798. if (msg_len)
  1799. err = msg_len;
  1800. else
  1801. err = sock_error(sk);
  1802. goto out;
  1803. do_interrupted:
  1804. if (msg_len)
  1805. err = msg_len;
  1806. goto out;
  1807. #endif /* 0 */
  1808. }
  1809. /* This is an extended version of skb_pull() that removes the data from the
  1810. * start of a skb even when data is spread across the list of skb's in the
  1811. * frag_list. len specifies the total amount of data that needs to be removed.
  1812. * when 'len' bytes could be removed from the skb, it returns 0.
  1813. * If 'len' exceeds the total skb length, it returns the no. of bytes that
  1814. * could not be removed.
  1815. */
  1816. static int sctp_skb_pull(struct sk_buff *skb, int len)
  1817. {
  1818. struct sk_buff *list;
  1819. int skb_len = skb_headlen(skb);
  1820. int rlen;
  1821. if (len <= skb_len) {
  1822. __skb_pull(skb, len);
  1823. return 0;
  1824. }
  1825. len -= skb_len;
  1826. __skb_pull(skb, skb_len);
  1827. skb_walk_frags(skb, list) {
  1828. rlen = sctp_skb_pull(list, len);
  1829. skb->len -= (len-rlen);
  1830. skb->data_len -= (len-rlen);
  1831. if (!rlen)
  1832. return 0;
  1833. len = rlen;
  1834. }
  1835. return len;
  1836. }
  1837. /* API 3.1.3 recvmsg() - UDP Style Syntax
  1838. *
  1839. * ssize_t recvmsg(int socket, struct msghdr *message,
  1840. * int flags);
  1841. *
  1842. * socket - the socket descriptor of the endpoint.
  1843. * message - pointer to the msghdr structure which contains a single
  1844. * user message and possibly some ancillary data.
  1845. *
  1846. * See Section 5 for complete description of the data
  1847. * structures.
  1848. *
  1849. * flags - flags sent or received with the user message, see Section
  1850. * 5 for complete description of the flags.
  1851. */
  1852. static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  1853. int noblock, int flags, int *addr_len)
  1854. {
  1855. struct sctp_ulpevent *event = NULL;
  1856. struct sctp_sock *sp = sctp_sk(sk);
  1857. struct sk_buff *skb, *head_skb;
  1858. int copied;
  1859. int err = 0;
  1860. int skb_len;
  1861. pr_debug("%s: sk:%p, msghdr:%p, len:%zd, noblock:%d, flags:0x%x, "
  1862. "addr_len:%p)\n", __func__, sk, msg, len, noblock, flags,
  1863. addr_len);
  1864. lock_sock(sk);
  1865. if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
  1866. !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
  1867. err = -ENOTCONN;
  1868. goto out;
  1869. }
  1870. skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
  1871. if (!skb)
  1872. goto out;
  1873. /* Get the total length of the skb including any skb's in the
  1874. * frag_list.
  1875. */
  1876. skb_len = skb->len;
  1877. copied = skb_len;
  1878. if (copied > len)
  1879. copied = len;
  1880. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  1881. event = sctp_skb2event(skb);
  1882. if (err)
  1883. goto out_free;
  1884. if (event->chunk && event->chunk->head_skb)
  1885. head_skb = event->chunk->head_skb;
  1886. else
  1887. head_skb = skb;
  1888. sock_recv_ts_and_drops(msg, sk, head_skb);
  1889. if (sctp_ulpevent_is_notification(event)) {
  1890. msg->msg_flags |= MSG_NOTIFICATION;
  1891. sp->pf->event_msgname(event, msg->msg_name, addr_len);
  1892. } else {
  1893. sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
  1894. }
  1895. /* Check if we allow SCTP_NXTINFO. */
  1896. if (sp->recvnxtinfo)
  1897. sctp_ulpevent_read_nxtinfo(event, msg, sk);
  1898. /* Check if we allow SCTP_RCVINFO. */
  1899. if (sp->recvrcvinfo)
  1900. sctp_ulpevent_read_rcvinfo(event, msg);
  1901. /* Check if we allow SCTP_SNDRCVINFO. */
  1902. if (sp->subscribe.sctp_data_io_event)
  1903. sctp_ulpevent_read_sndrcvinfo(event, msg);
  1904. err = copied;
  1905. /* If skb's length exceeds the user's buffer, update the skb and
  1906. * push it back to the receive_queue so that the next call to
  1907. * recvmsg() will return the remaining data. Don't set MSG_EOR.
  1908. */
  1909. if (skb_len > copied) {
  1910. msg->msg_flags &= ~MSG_EOR;
  1911. if (flags & MSG_PEEK)
  1912. goto out_free;
  1913. sctp_skb_pull(skb, copied);
  1914. skb_queue_head(&sk->sk_receive_queue, skb);
  1915. /* When only partial message is copied to the user, increase
  1916. * rwnd by that amount. If all the data in the skb is read,
  1917. * rwnd is updated when the event is freed.
  1918. */
  1919. if (!sctp_ulpevent_is_notification(event))
  1920. sctp_assoc_rwnd_increase(event->asoc, copied);
  1921. goto out;
  1922. } else if ((event->msg_flags & MSG_NOTIFICATION) ||
  1923. (event->msg_flags & MSG_EOR))
  1924. msg->msg_flags |= MSG_EOR;
  1925. else
  1926. msg->msg_flags &= ~MSG_EOR;
  1927. out_free:
  1928. if (flags & MSG_PEEK) {
  1929. /* Release the skb reference acquired after peeking the skb in
  1930. * sctp_skb_recv_datagram().
  1931. */
  1932. kfree_skb(skb);
  1933. } else {
  1934. /* Free the event which includes releasing the reference to
  1935. * the owner of the skb, freeing the skb and updating the
  1936. * rwnd.
  1937. */
  1938. sctp_ulpevent_free(event);
  1939. }
  1940. out:
  1941. release_sock(sk);
  1942. return err;
  1943. }
  1944. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  1945. *
  1946. * This option is a on/off flag. If enabled no SCTP message
  1947. * fragmentation will be performed. Instead if a message being sent
  1948. * exceeds the current PMTU size, the message will NOT be sent and
  1949. * instead a error will be indicated to the user.
  1950. */
  1951. static int sctp_setsockopt_disable_fragments(struct sock *sk,
  1952. char __user *optval,
  1953. unsigned int optlen)
  1954. {
  1955. int val;
  1956. if (optlen < sizeof(int))
  1957. return -EINVAL;
  1958. if (get_user(val, (int __user *)optval))
  1959. return -EFAULT;
  1960. sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
  1961. return 0;
  1962. }
  1963. static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
  1964. unsigned int optlen)
  1965. {
  1966. struct sctp_association *asoc;
  1967. struct sctp_ulpevent *event;
  1968. if (optlen > sizeof(struct sctp_event_subscribe))
  1969. return -EINVAL;
  1970. if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
  1971. return -EFAULT;
  1972. /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
  1973. * if there is no data to be sent or retransmit, the stack will
  1974. * immediately send up this notification.
  1975. */
  1976. if (sctp_ulpevent_type_enabled(SCTP_SENDER_DRY_EVENT,
  1977. &sctp_sk(sk)->subscribe)) {
  1978. asoc = sctp_id2assoc(sk, 0);
  1979. if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
  1980. event = sctp_ulpevent_make_sender_dry_event(asoc,
  1981. GFP_ATOMIC);
  1982. if (!event)
  1983. return -ENOMEM;
  1984. sctp_ulpq_tail_event(&asoc->ulpq, event);
  1985. }
  1986. }
  1987. return 0;
  1988. }
  1989. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  1990. *
  1991. * This socket option is applicable to the UDP-style socket only. When
  1992. * set it will cause associations that are idle for more than the
  1993. * specified number of seconds to automatically close. An association
  1994. * being idle is defined an association that has NOT sent or received
  1995. * user data. The special value of '0' indicates that no automatic
  1996. * close of any associations should be performed. The option expects an
  1997. * integer defining the number of seconds of idle time before an
  1998. * association is closed.
  1999. */
  2000. static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
  2001. unsigned int optlen)
  2002. {
  2003. struct sctp_sock *sp = sctp_sk(sk);
  2004. struct net *net = sock_net(sk);
  2005. /* Applicable to UDP-style socket only */
  2006. if (sctp_style(sk, TCP))
  2007. return -EOPNOTSUPP;
  2008. if (optlen != sizeof(int))
  2009. return -EINVAL;
  2010. if (copy_from_user(&sp->autoclose, optval, optlen))
  2011. return -EFAULT;
  2012. if (sp->autoclose > net->sctp.max_autoclose)
  2013. sp->autoclose = net->sctp.max_autoclose;
  2014. return 0;
  2015. }
  2016. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  2017. *
  2018. * Applications can enable or disable heartbeats for any peer address of
  2019. * an association, modify an address's heartbeat interval, force a
  2020. * heartbeat to be sent immediately, and adjust the address's maximum
  2021. * number of retransmissions sent before an address is considered
  2022. * unreachable. The following structure is used to access and modify an
  2023. * address's parameters:
  2024. *
  2025. * struct sctp_paddrparams {
  2026. * sctp_assoc_t spp_assoc_id;
  2027. * struct sockaddr_storage spp_address;
  2028. * uint32_t spp_hbinterval;
  2029. * uint16_t spp_pathmaxrxt;
  2030. * uint32_t spp_pathmtu;
  2031. * uint32_t spp_sackdelay;
  2032. * uint32_t spp_flags;
  2033. * };
  2034. *
  2035. * spp_assoc_id - (one-to-many style socket) This is filled in the
  2036. * application, and identifies the association for
  2037. * this query.
  2038. * spp_address - This specifies which address is of interest.
  2039. * spp_hbinterval - This contains the value of the heartbeat interval,
  2040. * in milliseconds. If a value of zero
  2041. * is present in this field then no changes are to
  2042. * be made to this parameter.
  2043. * spp_pathmaxrxt - This contains the maximum number of
  2044. * retransmissions before this address shall be
  2045. * considered unreachable. If a value of zero
  2046. * is present in this field then no changes are to
  2047. * be made to this parameter.
  2048. * spp_pathmtu - When Path MTU discovery is disabled the value
  2049. * specified here will be the "fixed" path mtu.
  2050. * Note that if the spp_address field is empty
  2051. * then all associations on this address will
  2052. * have this fixed path mtu set upon them.
  2053. *
  2054. * spp_sackdelay - When delayed sack is enabled, this value specifies
  2055. * the number of milliseconds that sacks will be delayed
  2056. * for. This value will apply to all addresses of an
  2057. * association if the spp_address field is empty. Note
  2058. * also, that if delayed sack is enabled and this
  2059. * value is set to 0, no change is made to the last
  2060. * recorded delayed sack timer value.
  2061. *
  2062. * spp_flags - These flags are used to control various features
  2063. * on an association. The flag field may contain
  2064. * zero or more of the following options.
  2065. *
  2066. * SPP_HB_ENABLE - Enable heartbeats on the
  2067. * specified address. Note that if the address
  2068. * field is empty all addresses for the association
  2069. * have heartbeats enabled upon them.
  2070. *
  2071. * SPP_HB_DISABLE - Disable heartbeats on the
  2072. * speicifed address. Note that if the address
  2073. * field is empty all addresses for the association
  2074. * will have their heartbeats disabled. Note also
  2075. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  2076. * mutually exclusive, only one of these two should
  2077. * be specified. Enabling both fields will have
  2078. * undetermined results.
  2079. *
  2080. * SPP_HB_DEMAND - Request a user initiated heartbeat
  2081. * to be made immediately.
  2082. *
  2083. * SPP_HB_TIME_IS_ZERO - Specify's that the time for
  2084. * heartbeat delayis to be set to the value of 0
  2085. * milliseconds.
  2086. *
  2087. * SPP_PMTUD_ENABLE - This field will enable PMTU
  2088. * discovery upon the specified address. Note that
  2089. * if the address feild is empty then all addresses
  2090. * on the association are effected.
  2091. *
  2092. * SPP_PMTUD_DISABLE - This field will disable PMTU
  2093. * discovery upon the specified address. Note that
  2094. * if the address feild is empty then all addresses
  2095. * on the association are effected. Not also that
  2096. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  2097. * exclusive. Enabling both will have undetermined
  2098. * results.
  2099. *
  2100. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  2101. * on delayed sack. The time specified in spp_sackdelay
  2102. * is used to specify the sack delay for this address. Note
  2103. * that if spp_address is empty then all addresses will
  2104. * enable delayed sack and take on the sack delay
  2105. * value specified in spp_sackdelay.
  2106. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  2107. * off delayed sack. If the spp_address field is blank then
  2108. * delayed sack is disabled for the entire association. Note
  2109. * also that this field is mutually exclusive to
  2110. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  2111. * results.
  2112. */
  2113. static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
  2114. struct sctp_transport *trans,
  2115. struct sctp_association *asoc,
  2116. struct sctp_sock *sp,
  2117. int hb_change,
  2118. int pmtud_change,
  2119. int sackdelay_change)
  2120. {
  2121. int error;
  2122. if (params->spp_flags & SPP_HB_DEMAND && trans) {
  2123. struct net *net = sock_net(trans->asoc->base.sk);
  2124. error = sctp_primitive_REQUESTHEARTBEAT(net, trans->asoc, trans);
  2125. if (error)
  2126. return error;
  2127. }
  2128. /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
  2129. * this field is ignored. Note also that a value of zero indicates
  2130. * the current setting should be left unchanged.
  2131. */
  2132. if (params->spp_flags & SPP_HB_ENABLE) {
  2133. /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
  2134. * set. This lets us use 0 value when this flag
  2135. * is set.
  2136. */
  2137. if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
  2138. params->spp_hbinterval = 0;
  2139. if (params->spp_hbinterval ||
  2140. (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
  2141. if (trans) {
  2142. trans->hbinterval =
  2143. msecs_to_jiffies(params->spp_hbinterval);
  2144. } else if (asoc) {
  2145. asoc->hbinterval =
  2146. msecs_to_jiffies(params->spp_hbinterval);
  2147. } else {
  2148. sp->hbinterval = params->spp_hbinterval;
  2149. }
  2150. }
  2151. }
  2152. if (hb_change) {
  2153. if (trans) {
  2154. trans->param_flags =
  2155. (trans->param_flags & ~SPP_HB) | hb_change;
  2156. } else if (asoc) {
  2157. asoc->param_flags =
  2158. (asoc->param_flags & ~SPP_HB) | hb_change;
  2159. } else {
  2160. sp->param_flags =
  2161. (sp->param_flags & ~SPP_HB) | hb_change;
  2162. }
  2163. }
  2164. /* When Path MTU discovery is disabled the value specified here will
  2165. * be the "fixed" path mtu (i.e. the value of the spp_flags field must
  2166. * include the flag SPP_PMTUD_DISABLE for this field to have any
  2167. * effect).
  2168. */
  2169. if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
  2170. if (trans) {
  2171. trans->pathmtu = params->spp_pathmtu;
  2172. sctp_assoc_sync_pmtu(asoc);
  2173. } else if (asoc) {
  2174. asoc->pathmtu = params->spp_pathmtu;
  2175. } else {
  2176. sp->pathmtu = params->spp_pathmtu;
  2177. }
  2178. }
  2179. if (pmtud_change) {
  2180. if (trans) {
  2181. int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
  2182. (params->spp_flags & SPP_PMTUD_ENABLE);
  2183. trans->param_flags =
  2184. (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
  2185. if (update) {
  2186. sctp_transport_pmtu(trans, sctp_opt2sk(sp));
  2187. sctp_assoc_sync_pmtu(asoc);
  2188. }
  2189. } else if (asoc) {
  2190. asoc->param_flags =
  2191. (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
  2192. } else {
  2193. sp->param_flags =
  2194. (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
  2195. }
  2196. }
  2197. /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
  2198. * value of this field is ignored. Note also that a value of zero
  2199. * indicates the current setting should be left unchanged.
  2200. */
  2201. if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
  2202. if (trans) {
  2203. trans->sackdelay =
  2204. msecs_to_jiffies(params->spp_sackdelay);
  2205. } else if (asoc) {
  2206. asoc->sackdelay =
  2207. msecs_to_jiffies(params->spp_sackdelay);
  2208. } else {
  2209. sp->sackdelay = params->spp_sackdelay;
  2210. }
  2211. }
  2212. if (sackdelay_change) {
  2213. if (trans) {
  2214. trans->param_flags =
  2215. (trans->param_flags & ~SPP_SACKDELAY) |
  2216. sackdelay_change;
  2217. } else if (asoc) {
  2218. asoc->param_flags =
  2219. (asoc->param_flags & ~SPP_SACKDELAY) |
  2220. sackdelay_change;
  2221. } else {
  2222. sp->param_flags =
  2223. (sp->param_flags & ~SPP_SACKDELAY) |
  2224. sackdelay_change;
  2225. }
  2226. }
  2227. /* Note that a value of zero indicates the current setting should be
  2228. left unchanged.
  2229. */
  2230. if (params->spp_pathmaxrxt) {
  2231. if (trans) {
  2232. trans->pathmaxrxt = params->spp_pathmaxrxt;
  2233. } else if (asoc) {
  2234. asoc->pathmaxrxt = params->spp_pathmaxrxt;
  2235. } else {
  2236. sp->pathmaxrxt = params->spp_pathmaxrxt;
  2237. }
  2238. }
  2239. return 0;
  2240. }
  2241. static int sctp_setsockopt_peer_addr_params(struct sock *sk,
  2242. char __user *optval,
  2243. unsigned int optlen)
  2244. {
  2245. struct sctp_paddrparams params;
  2246. struct sctp_transport *trans = NULL;
  2247. struct sctp_association *asoc = NULL;
  2248. struct sctp_sock *sp = sctp_sk(sk);
  2249. int error;
  2250. int hb_change, pmtud_change, sackdelay_change;
  2251. if (optlen != sizeof(struct sctp_paddrparams))
  2252. return -EINVAL;
  2253. if (copy_from_user(&params, optval, optlen))
  2254. return -EFAULT;
  2255. /* Validate flags and value parameters. */
  2256. hb_change = params.spp_flags & SPP_HB;
  2257. pmtud_change = params.spp_flags & SPP_PMTUD;
  2258. sackdelay_change = params.spp_flags & SPP_SACKDELAY;
  2259. if (hb_change == SPP_HB ||
  2260. pmtud_change == SPP_PMTUD ||
  2261. sackdelay_change == SPP_SACKDELAY ||
  2262. params.spp_sackdelay > 500 ||
  2263. (params.spp_pathmtu &&
  2264. params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
  2265. return -EINVAL;
  2266. /* If an address other than INADDR_ANY is specified, and
  2267. * no transport is found, then the request is invalid.
  2268. */
  2269. if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
  2270. trans = sctp_addr_id2transport(sk, &params.spp_address,
  2271. params.spp_assoc_id);
  2272. if (!trans)
  2273. return -EINVAL;
  2274. }
  2275. /* Get association, if assoc_id != 0 and the socket is a one
  2276. * to many style socket, and an association was not found, then
  2277. * the id was invalid.
  2278. */
  2279. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  2280. if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
  2281. return -EINVAL;
  2282. /* Heartbeat demand can only be sent on a transport or
  2283. * association, but not a socket.
  2284. */
  2285. if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
  2286. return -EINVAL;
  2287. /* Process parameters. */
  2288. error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  2289. hb_change, pmtud_change,
  2290. sackdelay_change);
  2291. if (error)
  2292. return error;
  2293. /* If changes are for association, also apply parameters to each
  2294. * transport.
  2295. */
  2296. if (!trans && asoc) {
  2297. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2298. transports) {
  2299. sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  2300. hb_change, pmtud_change,
  2301. sackdelay_change);
  2302. }
  2303. }
  2304. return 0;
  2305. }
  2306. static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
  2307. {
  2308. return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
  2309. }
  2310. static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
  2311. {
  2312. return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
  2313. }
  2314. /*
  2315. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  2316. *
  2317. * This option will effect the way delayed acks are performed. This
  2318. * option allows you to get or set the delayed ack time, in
  2319. * milliseconds. It also allows changing the delayed ack frequency.
  2320. * Changing the frequency to 1 disables the delayed sack algorithm. If
  2321. * the assoc_id is 0, then this sets or gets the endpoints default
  2322. * values. If the assoc_id field is non-zero, then the set or get
  2323. * effects the specified association for the one to many model (the
  2324. * assoc_id field is ignored by the one to one model). Note that if
  2325. * sack_delay or sack_freq are 0 when setting this option, then the
  2326. * current values will remain unchanged.
  2327. *
  2328. * struct sctp_sack_info {
  2329. * sctp_assoc_t sack_assoc_id;
  2330. * uint32_t sack_delay;
  2331. * uint32_t sack_freq;
  2332. * };
  2333. *
  2334. * sack_assoc_id - This parameter, indicates which association the user
  2335. * is performing an action upon. Note that if this field's value is
  2336. * zero then the endpoints default value is changed (effecting future
  2337. * associations only).
  2338. *
  2339. * sack_delay - This parameter contains the number of milliseconds that
  2340. * the user is requesting the delayed ACK timer be set to. Note that
  2341. * this value is defined in the standard to be between 200 and 500
  2342. * milliseconds.
  2343. *
  2344. * sack_freq - This parameter contains the number of packets that must
  2345. * be received before a sack is sent without waiting for the delay
  2346. * timer to expire. The default value for this is 2, setting this
  2347. * value to 1 will disable the delayed sack algorithm.
  2348. */
  2349. static int sctp_setsockopt_delayed_ack(struct sock *sk,
  2350. char __user *optval, unsigned int optlen)
  2351. {
  2352. struct sctp_sack_info params;
  2353. struct sctp_transport *trans = NULL;
  2354. struct sctp_association *asoc = NULL;
  2355. struct sctp_sock *sp = sctp_sk(sk);
  2356. if (optlen == sizeof(struct sctp_sack_info)) {
  2357. if (copy_from_user(&params, optval, optlen))
  2358. return -EFAULT;
  2359. if (params.sack_delay == 0 && params.sack_freq == 0)
  2360. return 0;
  2361. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2362. pr_warn_ratelimited(DEPRECATED
  2363. "%s (pid %d) "
  2364. "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
  2365. "Use struct sctp_sack_info instead\n",
  2366. current->comm, task_pid_nr(current));
  2367. if (copy_from_user(&params, optval, optlen))
  2368. return -EFAULT;
  2369. if (params.sack_delay == 0)
  2370. params.sack_freq = 1;
  2371. else
  2372. params.sack_freq = 0;
  2373. } else
  2374. return -EINVAL;
  2375. /* Validate value parameter. */
  2376. if (params.sack_delay > 500)
  2377. return -EINVAL;
  2378. /* Get association, if sack_assoc_id != 0 and the socket is a one
  2379. * to many style socket, and an association was not found, then
  2380. * the id was invalid.
  2381. */
  2382. asoc = sctp_id2assoc(sk, params.sack_assoc_id);
  2383. if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
  2384. return -EINVAL;
  2385. if (params.sack_delay) {
  2386. if (asoc) {
  2387. asoc->sackdelay =
  2388. msecs_to_jiffies(params.sack_delay);
  2389. asoc->param_flags =
  2390. sctp_spp_sackdelay_enable(asoc->param_flags);
  2391. } else {
  2392. sp->sackdelay = params.sack_delay;
  2393. sp->param_flags =
  2394. sctp_spp_sackdelay_enable(sp->param_flags);
  2395. }
  2396. }
  2397. if (params.sack_freq == 1) {
  2398. if (asoc) {
  2399. asoc->param_flags =
  2400. sctp_spp_sackdelay_disable(asoc->param_flags);
  2401. } else {
  2402. sp->param_flags =
  2403. sctp_spp_sackdelay_disable(sp->param_flags);
  2404. }
  2405. } else if (params.sack_freq > 1) {
  2406. if (asoc) {
  2407. asoc->sackfreq = params.sack_freq;
  2408. asoc->param_flags =
  2409. sctp_spp_sackdelay_enable(asoc->param_flags);
  2410. } else {
  2411. sp->sackfreq = params.sack_freq;
  2412. sp->param_flags =
  2413. sctp_spp_sackdelay_enable(sp->param_flags);
  2414. }
  2415. }
  2416. /* If change is for association, also apply to each transport. */
  2417. if (asoc) {
  2418. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2419. transports) {
  2420. if (params.sack_delay) {
  2421. trans->sackdelay =
  2422. msecs_to_jiffies(params.sack_delay);
  2423. trans->param_flags =
  2424. sctp_spp_sackdelay_enable(trans->param_flags);
  2425. }
  2426. if (params.sack_freq == 1) {
  2427. trans->param_flags =
  2428. sctp_spp_sackdelay_disable(trans->param_flags);
  2429. } else if (params.sack_freq > 1) {
  2430. trans->sackfreq = params.sack_freq;
  2431. trans->param_flags =
  2432. sctp_spp_sackdelay_enable(trans->param_flags);
  2433. }
  2434. }
  2435. }
  2436. return 0;
  2437. }
  2438. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  2439. *
  2440. * Applications can specify protocol parameters for the default association
  2441. * initialization. The option name argument to setsockopt() and getsockopt()
  2442. * is SCTP_INITMSG.
  2443. *
  2444. * Setting initialization parameters is effective only on an unconnected
  2445. * socket (for UDP-style sockets only future associations are effected
  2446. * by the change). With TCP-style sockets, this option is inherited by
  2447. * sockets derived from a listener socket.
  2448. */
  2449. static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen)
  2450. {
  2451. struct sctp_initmsg sinit;
  2452. struct sctp_sock *sp = sctp_sk(sk);
  2453. if (optlen != sizeof(struct sctp_initmsg))
  2454. return -EINVAL;
  2455. if (copy_from_user(&sinit, optval, optlen))
  2456. return -EFAULT;
  2457. if (sinit.sinit_num_ostreams)
  2458. sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
  2459. if (sinit.sinit_max_instreams)
  2460. sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
  2461. if (sinit.sinit_max_attempts)
  2462. sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
  2463. if (sinit.sinit_max_init_timeo)
  2464. sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
  2465. return 0;
  2466. }
  2467. /*
  2468. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  2469. *
  2470. * Applications that wish to use the sendto() system call may wish to
  2471. * specify a default set of parameters that would normally be supplied
  2472. * through the inclusion of ancillary data. This socket option allows
  2473. * such an application to set the default sctp_sndrcvinfo structure.
  2474. * The application that wishes to use this socket option simply passes
  2475. * in to this call the sctp_sndrcvinfo structure defined in Section
  2476. * 5.2.2) The input parameters accepted by this call include
  2477. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  2478. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  2479. * to this call if the caller is using the UDP model.
  2480. */
  2481. static int sctp_setsockopt_default_send_param(struct sock *sk,
  2482. char __user *optval,
  2483. unsigned int optlen)
  2484. {
  2485. struct sctp_sock *sp = sctp_sk(sk);
  2486. struct sctp_association *asoc;
  2487. struct sctp_sndrcvinfo info;
  2488. if (optlen != sizeof(info))
  2489. return -EINVAL;
  2490. if (copy_from_user(&info, optval, optlen))
  2491. return -EFAULT;
  2492. if (info.sinfo_flags &
  2493. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  2494. SCTP_ABORT | SCTP_EOF))
  2495. return -EINVAL;
  2496. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  2497. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  2498. return -EINVAL;
  2499. if (asoc) {
  2500. asoc->default_stream = info.sinfo_stream;
  2501. asoc->default_flags = info.sinfo_flags;
  2502. asoc->default_ppid = info.sinfo_ppid;
  2503. asoc->default_context = info.sinfo_context;
  2504. asoc->default_timetolive = info.sinfo_timetolive;
  2505. } else {
  2506. sp->default_stream = info.sinfo_stream;
  2507. sp->default_flags = info.sinfo_flags;
  2508. sp->default_ppid = info.sinfo_ppid;
  2509. sp->default_context = info.sinfo_context;
  2510. sp->default_timetolive = info.sinfo_timetolive;
  2511. }
  2512. return 0;
  2513. }
  2514. /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
  2515. * (SCTP_DEFAULT_SNDINFO)
  2516. */
  2517. static int sctp_setsockopt_default_sndinfo(struct sock *sk,
  2518. char __user *optval,
  2519. unsigned int optlen)
  2520. {
  2521. struct sctp_sock *sp = sctp_sk(sk);
  2522. struct sctp_association *asoc;
  2523. struct sctp_sndinfo info;
  2524. if (optlen != sizeof(info))
  2525. return -EINVAL;
  2526. if (copy_from_user(&info, optval, optlen))
  2527. return -EFAULT;
  2528. if (info.snd_flags &
  2529. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  2530. SCTP_ABORT | SCTP_EOF))
  2531. return -EINVAL;
  2532. asoc = sctp_id2assoc(sk, info.snd_assoc_id);
  2533. if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
  2534. return -EINVAL;
  2535. if (asoc) {
  2536. asoc->default_stream = info.snd_sid;
  2537. asoc->default_flags = info.snd_flags;
  2538. asoc->default_ppid = info.snd_ppid;
  2539. asoc->default_context = info.snd_context;
  2540. } else {
  2541. sp->default_stream = info.snd_sid;
  2542. sp->default_flags = info.snd_flags;
  2543. sp->default_ppid = info.snd_ppid;
  2544. sp->default_context = info.snd_context;
  2545. }
  2546. return 0;
  2547. }
  2548. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  2549. *
  2550. * Requests that the local SCTP stack use the enclosed peer address as
  2551. * the association primary. The enclosed address must be one of the
  2552. * association peer's addresses.
  2553. */
  2554. static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
  2555. unsigned int optlen)
  2556. {
  2557. struct sctp_prim prim;
  2558. struct sctp_transport *trans;
  2559. if (optlen != sizeof(struct sctp_prim))
  2560. return -EINVAL;
  2561. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  2562. return -EFAULT;
  2563. trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
  2564. if (!trans)
  2565. return -EINVAL;
  2566. sctp_assoc_set_primary(trans->asoc, trans);
  2567. return 0;
  2568. }
  2569. /*
  2570. * 7.1.5 SCTP_NODELAY
  2571. *
  2572. * Turn on/off any Nagle-like algorithm. This means that packets are
  2573. * generally sent as soon as possible and no unnecessary delays are
  2574. * introduced, at the cost of more packets in the network. Expects an
  2575. * integer boolean flag.
  2576. */
  2577. static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
  2578. unsigned int optlen)
  2579. {
  2580. int val;
  2581. if (optlen < sizeof(int))
  2582. return -EINVAL;
  2583. if (get_user(val, (int __user *)optval))
  2584. return -EFAULT;
  2585. sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
  2586. return 0;
  2587. }
  2588. /*
  2589. *
  2590. * 7.1.1 SCTP_RTOINFO
  2591. *
  2592. * The protocol parameters used to initialize and bound retransmission
  2593. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  2594. * and modify these parameters.
  2595. * All parameters are time values, in milliseconds. A value of 0, when
  2596. * modifying the parameters, indicates that the current value should not
  2597. * be changed.
  2598. *
  2599. */
  2600. static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen)
  2601. {
  2602. struct sctp_rtoinfo rtoinfo;
  2603. struct sctp_association *asoc;
  2604. unsigned long rto_min, rto_max;
  2605. struct sctp_sock *sp = sctp_sk(sk);
  2606. if (optlen != sizeof (struct sctp_rtoinfo))
  2607. return -EINVAL;
  2608. if (copy_from_user(&rtoinfo, optval, optlen))
  2609. return -EFAULT;
  2610. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  2611. /* Set the values to the specific association */
  2612. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  2613. return -EINVAL;
  2614. rto_max = rtoinfo.srto_max;
  2615. rto_min = rtoinfo.srto_min;
  2616. if (rto_max)
  2617. rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
  2618. else
  2619. rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
  2620. if (rto_min)
  2621. rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
  2622. else
  2623. rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
  2624. if (rto_min > rto_max)
  2625. return -EINVAL;
  2626. if (asoc) {
  2627. if (rtoinfo.srto_initial != 0)
  2628. asoc->rto_initial =
  2629. msecs_to_jiffies(rtoinfo.srto_initial);
  2630. asoc->rto_max = rto_max;
  2631. asoc->rto_min = rto_min;
  2632. } else {
  2633. /* If there is no association or the association-id = 0
  2634. * set the values to the endpoint.
  2635. */
  2636. if (rtoinfo.srto_initial != 0)
  2637. sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
  2638. sp->rtoinfo.srto_max = rto_max;
  2639. sp->rtoinfo.srto_min = rto_min;
  2640. }
  2641. return 0;
  2642. }
  2643. /*
  2644. *
  2645. * 7.1.2 SCTP_ASSOCINFO
  2646. *
  2647. * This option is used to tune the maximum retransmission attempts
  2648. * of the association.
  2649. * Returns an error if the new association retransmission value is
  2650. * greater than the sum of the retransmission value of the peer.
  2651. * See [SCTP] for more information.
  2652. *
  2653. */
  2654. static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen)
  2655. {
  2656. struct sctp_assocparams assocparams;
  2657. struct sctp_association *asoc;
  2658. if (optlen != sizeof(struct sctp_assocparams))
  2659. return -EINVAL;
  2660. if (copy_from_user(&assocparams, optval, optlen))
  2661. return -EFAULT;
  2662. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  2663. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  2664. return -EINVAL;
  2665. /* Set the values to the specific association */
  2666. if (asoc) {
  2667. if (assocparams.sasoc_asocmaxrxt != 0) {
  2668. __u32 path_sum = 0;
  2669. int paths = 0;
  2670. struct sctp_transport *peer_addr;
  2671. list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
  2672. transports) {
  2673. path_sum += peer_addr->pathmaxrxt;
  2674. paths++;
  2675. }
  2676. /* Only validate asocmaxrxt if we have more than
  2677. * one path/transport. We do this because path
  2678. * retransmissions are only counted when we have more
  2679. * then one path.
  2680. */
  2681. if (paths > 1 &&
  2682. assocparams.sasoc_asocmaxrxt > path_sum)
  2683. return -EINVAL;
  2684. asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
  2685. }
  2686. if (assocparams.sasoc_cookie_life != 0)
  2687. asoc->cookie_life = ms_to_ktime(assocparams.sasoc_cookie_life);
  2688. } else {
  2689. /* Set the values to the endpoint */
  2690. struct sctp_sock *sp = sctp_sk(sk);
  2691. if (assocparams.sasoc_asocmaxrxt != 0)
  2692. sp->assocparams.sasoc_asocmaxrxt =
  2693. assocparams.sasoc_asocmaxrxt;
  2694. if (assocparams.sasoc_cookie_life != 0)
  2695. sp->assocparams.sasoc_cookie_life =
  2696. assocparams.sasoc_cookie_life;
  2697. }
  2698. return 0;
  2699. }
  2700. /*
  2701. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  2702. *
  2703. * This socket option is a boolean flag which turns on or off mapped V4
  2704. * addresses. If this option is turned on and the socket is type
  2705. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  2706. * If this option is turned off, then no mapping will be done of V4
  2707. * addresses and a user will receive both PF_INET6 and PF_INET type
  2708. * addresses on the socket.
  2709. */
  2710. static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen)
  2711. {
  2712. int val;
  2713. struct sctp_sock *sp = sctp_sk(sk);
  2714. if (optlen < sizeof(int))
  2715. return -EINVAL;
  2716. if (get_user(val, (int __user *)optval))
  2717. return -EFAULT;
  2718. if (val)
  2719. sp->v4mapped = 1;
  2720. else
  2721. sp->v4mapped = 0;
  2722. return 0;
  2723. }
  2724. /*
  2725. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  2726. * This option will get or set the maximum size to put in any outgoing
  2727. * SCTP DATA chunk. If a message is larger than this size it will be
  2728. * fragmented by SCTP into the specified size. Note that the underlying
  2729. * SCTP implementation may fragment into smaller sized chunks when the
  2730. * PMTU of the underlying association is smaller than the value set by
  2731. * the user. The default value for this option is '0' which indicates
  2732. * the user is NOT limiting fragmentation and only the PMTU will effect
  2733. * SCTP's choice of DATA chunk size. Note also that values set larger
  2734. * than the maximum size of an IP datagram will effectively let SCTP
  2735. * control fragmentation (i.e. the same as setting this option to 0).
  2736. *
  2737. * The following structure is used to access and modify this parameter:
  2738. *
  2739. * struct sctp_assoc_value {
  2740. * sctp_assoc_t assoc_id;
  2741. * uint32_t assoc_value;
  2742. * };
  2743. *
  2744. * assoc_id: This parameter is ignored for one-to-one style sockets.
  2745. * For one-to-many style sockets this parameter indicates which
  2746. * association the user is performing an action upon. Note that if
  2747. * this field's value is zero then the endpoints default value is
  2748. * changed (effecting future associations only).
  2749. * assoc_value: This parameter specifies the maximum size in bytes.
  2750. */
  2751. static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen)
  2752. {
  2753. struct sctp_sock *sp = sctp_sk(sk);
  2754. struct sctp_assoc_value params;
  2755. struct sctp_association *asoc;
  2756. int val;
  2757. if (optlen == sizeof(int)) {
  2758. pr_warn_ratelimited(DEPRECATED
  2759. "%s (pid %d) "
  2760. "Use of int in maxseg socket option.\n"
  2761. "Use struct sctp_assoc_value instead\n",
  2762. current->comm, task_pid_nr(current));
  2763. if (copy_from_user(&val, optval, optlen))
  2764. return -EFAULT;
  2765. params.assoc_id = 0;
  2766. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2767. if (copy_from_user(&params, optval, optlen))
  2768. return -EFAULT;
  2769. val = params.assoc_value;
  2770. } else {
  2771. return -EINVAL;
  2772. }
  2773. if (val) {
  2774. int min_len, max_len;
  2775. min_len = SCTP_DEFAULT_MINSEGMENT - sp->pf->af->net_header_len;
  2776. min_len -= sizeof(struct sctphdr) +
  2777. sizeof(struct sctp_data_chunk);
  2778. max_len = SCTP_MAX_CHUNK_LEN - sizeof(struct sctp_data_chunk);
  2779. if (val < min_len || val > max_len)
  2780. return -EINVAL;
  2781. }
  2782. asoc = sctp_id2assoc(sk, params.assoc_id);
  2783. if (asoc) {
  2784. if (val == 0) {
  2785. val = asoc->pathmtu - sp->pf->af->net_header_len;
  2786. val -= sizeof(struct sctphdr) +
  2787. sizeof(struct sctp_data_chunk);
  2788. }
  2789. asoc->user_frag = val;
  2790. asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu);
  2791. } else {
  2792. if (params.assoc_id && sctp_style(sk, UDP))
  2793. return -EINVAL;
  2794. sp->user_frag = val;
  2795. }
  2796. return 0;
  2797. }
  2798. /*
  2799. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  2800. *
  2801. * Requests that the peer mark the enclosed address as the association
  2802. * primary. The enclosed address must be one of the association's
  2803. * locally bound addresses. The following structure is used to make a
  2804. * set primary request:
  2805. */
  2806. static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
  2807. unsigned int optlen)
  2808. {
  2809. struct net *net = sock_net(sk);
  2810. struct sctp_sock *sp;
  2811. struct sctp_association *asoc = NULL;
  2812. struct sctp_setpeerprim prim;
  2813. struct sctp_chunk *chunk;
  2814. struct sctp_af *af;
  2815. int err;
  2816. sp = sctp_sk(sk);
  2817. if (!net->sctp.addip_enable)
  2818. return -EPERM;
  2819. if (optlen != sizeof(struct sctp_setpeerprim))
  2820. return -EINVAL;
  2821. if (copy_from_user(&prim, optval, optlen))
  2822. return -EFAULT;
  2823. asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
  2824. if (!asoc)
  2825. return -EINVAL;
  2826. if (!asoc->peer.asconf_capable)
  2827. return -EPERM;
  2828. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  2829. return -EPERM;
  2830. if (!sctp_state(asoc, ESTABLISHED))
  2831. return -ENOTCONN;
  2832. af = sctp_get_af_specific(prim.sspp_addr.ss_family);
  2833. if (!af)
  2834. return -EINVAL;
  2835. if (!af->addr_valid((union sctp_addr *)&prim.sspp_addr, sp, NULL))
  2836. return -EADDRNOTAVAIL;
  2837. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
  2838. return -EADDRNOTAVAIL;
  2839. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  2840. chunk = sctp_make_asconf_set_prim(asoc,
  2841. (union sctp_addr *)&prim.sspp_addr);
  2842. if (!chunk)
  2843. return -ENOMEM;
  2844. err = sctp_send_asconf(asoc, chunk);
  2845. pr_debug("%s: we set peer primary addr primitively\n", __func__);
  2846. return err;
  2847. }
  2848. static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval,
  2849. unsigned int optlen)
  2850. {
  2851. struct sctp_setadaptation adaptation;
  2852. if (optlen != sizeof(struct sctp_setadaptation))
  2853. return -EINVAL;
  2854. if (copy_from_user(&adaptation, optval, optlen))
  2855. return -EFAULT;
  2856. sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind;
  2857. return 0;
  2858. }
  2859. /*
  2860. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  2861. *
  2862. * The context field in the sctp_sndrcvinfo structure is normally only
  2863. * used when a failed message is retrieved holding the value that was
  2864. * sent down on the actual send call. This option allows the setting of
  2865. * a default context on an association basis that will be received on
  2866. * reading messages from the peer. This is especially helpful in the
  2867. * one-2-many model for an application to keep some reference to an
  2868. * internal state machine that is processing messages on the
  2869. * association. Note that the setting of this value only effects
  2870. * received messages from the peer and does not effect the value that is
  2871. * saved with outbound messages.
  2872. */
  2873. static int sctp_setsockopt_context(struct sock *sk, char __user *optval,
  2874. unsigned int optlen)
  2875. {
  2876. struct sctp_assoc_value params;
  2877. struct sctp_sock *sp;
  2878. struct sctp_association *asoc;
  2879. if (optlen != sizeof(struct sctp_assoc_value))
  2880. return -EINVAL;
  2881. if (copy_from_user(&params, optval, optlen))
  2882. return -EFAULT;
  2883. sp = sctp_sk(sk);
  2884. if (params.assoc_id != 0) {
  2885. asoc = sctp_id2assoc(sk, params.assoc_id);
  2886. if (!asoc)
  2887. return -EINVAL;
  2888. asoc->default_rcv_context = params.assoc_value;
  2889. } else {
  2890. sp->default_rcv_context = params.assoc_value;
  2891. }
  2892. return 0;
  2893. }
  2894. /*
  2895. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  2896. *
  2897. * This options will at a minimum specify if the implementation is doing
  2898. * fragmented interleave. Fragmented interleave, for a one to many
  2899. * socket, is when subsequent calls to receive a message may return
  2900. * parts of messages from different associations. Some implementations
  2901. * may allow you to turn this value on or off. If so, when turned off,
  2902. * no fragment interleave will occur (which will cause a head of line
  2903. * blocking amongst multiple associations sharing the same one to many
  2904. * socket). When this option is turned on, then each receive call may
  2905. * come from a different association (thus the user must receive data
  2906. * with the extended calls (e.g. sctp_recvmsg) to keep track of which
  2907. * association each receive belongs to.
  2908. *
  2909. * This option takes a boolean value. A non-zero value indicates that
  2910. * fragmented interleave is on. A value of zero indicates that
  2911. * fragmented interleave is off.
  2912. *
  2913. * Note that it is important that an implementation that allows this
  2914. * option to be turned on, have it off by default. Otherwise an unaware
  2915. * application using the one to many model may become confused and act
  2916. * incorrectly.
  2917. */
  2918. static int sctp_setsockopt_fragment_interleave(struct sock *sk,
  2919. char __user *optval,
  2920. unsigned int optlen)
  2921. {
  2922. int val;
  2923. if (optlen != sizeof(int))
  2924. return -EINVAL;
  2925. if (get_user(val, (int __user *)optval))
  2926. return -EFAULT;
  2927. sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1;
  2928. return 0;
  2929. }
  2930. /*
  2931. * 8.1.21. Set or Get the SCTP Partial Delivery Point
  2932. * (SCTP_PARTIAL_DELIVERY_POINT)
  2933. *
  2934. * This option will set or get the SCTP partial delivery point. This
  2935. * point is the size of a message where the partial delivery API will be
  2936. * invoked to help free up rwnd space for the peer. Setting this to a
  2937. * lower value will cause partial deliveries to happen more often. The
  2938. * calls argument is an integer that sets or gets the partial delivery
  2939. * point. Note also that the call will fail if the user attempts to set
  2940. * this value larger than the socket receive buffer size.
  2941. *
  2942. * Note that any single message having a length smaller than or equal to
  2943. * the SCTP partial delivery point will be delivered in one single read
  2944. * call as long as the user provided buffer is large enough to hold the
  2945. * message.
  2946. */
  2947. static int sctp_setsockopt_partial_delivery_point(struct sock *sk,
  2948. char __user *optval,
  2949. unsigned int optlen)
  2950. {
  2951. u32 val;
  2952. if (optlen != sizeof(u32))
  2953. return -EINVAL;
  2954. if (get_user(val, (int __user *)optval))
  2955. return -EFAULT;
  2956. /* Note: We double the receive buffer from what the user sets
  2957. * it to be, also initial rwnd is based on rcvbuf/2.
  2958. */
  2959. if (val > (sk->sk_rcvbuf >> 1))
  2960. return -EINVAL;
  2961. sctp_sk(sk)->pd_point = val;
  2962. return 0; /* is this the right error code? */
  2963. }
  2964. /*
  2965. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  2966. *
  2967. * This option will allow a user to change the maximum burst of packets
  2968. * that can be emitted by this association. Note that the default value
  2969. * is 4, and some implementations may restrict this setting so that it
  2970. * can only be lowered.
  2971. *
  2972. * NOTE: This text doesn't seem right. Do this on a socket basis with
  2973. * future associations inheriting the socket value.
  2974. */
  2975. static int sctp_setsockopt_maxburst(struct sock *sk,
  2976. char __user *optval,
  2977. unsigned int optlen)
  2978. {
  2979. struct sctp_assoc_value params;
  2980. struct sctp_sock *sp;
  2981. struct sctp_association *asoc;
  2982. int val;
  2983. int assoc_id = 0;
  2984. if (optlen == sizeof(int)) {
  2985. pr_warn_ratelimited(DEPRECATED
  2986. "%s (pid %d) "
  2987. "Use of int in max_burst socket option deprecated.\n"
  2988. "Use struct sctp_assoc_value instead\n",
  2989. current->comm, task_pid_nr(current));
  2990. if (copy_from_user(&val, optval, optlen))
  2991. return -EFAULT;
  2992. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2993. if (copy_from_user(&params, optval, optlen))
  2994. return -EFAULT;
  2995. val = params.assoc_value;
  2996. assoc_id = params.assoc_id;
  2997. } else
  2998. return -EINVAL;
  2999. sp = sctp_sk(sk);
  3000. if (assoc_id != 0) {
  3001. asoc = sctp_id2assoc(sk, assoc_id);
  3002. if (!asoc)
  3003. return -EINVAL;
  3004. asoc->max_burst = val;
  3005. } else
  3006. sp->max_burst = val;
  3007. return 0;
  3008. }
  3009. /*
  3010. * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
  3011. *
  3012. * This set option adds a chunk type that the user is requesting to be
  3013. * received only in an authenticated way. Changes to the list of chunks
  3014. * will only effect future associations on the socket.
  3015. */
  3016. static int sctp_setsockopt_auth_chunk(struct sock *sk,
  3017. char __user *optval,
  3018. unsigned int optlen)
  3019. {
  3020. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3021. struct sctp_authchunk val;
  3022. if (!ep->auth_enable)
  3023. return -EACCES;
  3024. if (optlen != sizeof(struct sctp_authchunk))
  3025. return -EINVAL;
  3026. if (copy_from_user(&val, optval, optlen))
  3027. return -EFAULT;
  3028. switch (val.sauth_chunk) {
  3029. case SCTP_CID_INIT:
  3030. case SCTP_CID_INIT_ACK:
  3031. case SCTP_CID_SHUTDOWN_COMPLETE:
  3032. case SCTP_CID_AUTH:
  3033. return -EINVAL;
  3034. }
  3035. /* add this chunk id to the endpoint */
  3036. return sctp_auth_ep_add_chunkid(ep, val.sauth_chunk);
  3037. }
  3038. /*
  3039. * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
  3040. *
  3041. * This option gets or sets the list of HMAC algorithms that the local
  3042. * endpoint requires the peer to use.
  3043. */
  3044. static int sctp_setsockopt_hmac_ident(struct sock *sk,
  3045. char __user *optval,
  3046. unsigned int optlen)
  3047. {
  3048. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3049. struct sctp_hmacalgo *hmacs;
  3050. u32 idents;
  3051. int err;
  3052. if (!ep->auth_enable)
  3053. return -EACCES;
  3054. if (optlen < sizeof(struct sctp_hmacalgo))
  3055. return -EINVAL;
  3056. hmacs = memdup_user(optval, optlen);
  3057. if (IS_ERR(hmacs))
  3058. return PTR_ERR(hmacs);
  3059. idents = hmacs->shmac_num_idents;
  3060. if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
  3061. (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) {
  3062. err = -EINVAL;
  3063. goto out;
  3064. }
  3065. err = sctp_auth_ep_set_hmacs(ep, hmacs);
  3066. out:
  3067. kfree(hmacs);
  3068. return err;
  3069. }
  3070. /*
  3071. * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
  3072. *
  3073. * This option will set a shared secret key which is used to build an
  3074. * association shared key.
  3075. */
  3076. static int sctp_setsockopt_auth_key(struct sock *sk,
  3077. char __user *optval,
  3078. unsigned int optlen)
  3079. {
  3080. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3081. struct sctp_authkey *authkey;
  3082. struct sctp_association *asoc;
  3083. int ret;
  3084. if (!ep->auth_enable)
  3085. return -EACCES;
  3086. if (optlen <= sizeof(struct sctp_authkey))
  3087. return -EINVAL;
  3088. authkey = memdup_user(optval, optlen);
  3089. if (IS_ERR(authkey))
  3090. return PTR_ERR(authkey);
  3091. if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) {
  3092. ret = -EINVAL;
  3093. goto out;
  3094. }
  3095. asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
  3096. if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) {
  3097. ret = -EINVAL;
  3098. goto out;
  3099. }
  3100. ret = sctp_auth_set_key(ep, asoc, authkey);
  3101. out:
  3102. kzfree(authkey);
  3103. return ret;
  3104. }
  3105. /*
  3106. * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
  3107. *
  3108. * This option will get or set the active shared key to be used to build
  3109. * the association shared key.
  3110. */
  3111. static int sctp_setsockopt_active_key(struct sock *sk,
  3112. char __user *optval,
  3113. unsigned int optlen)
  3114. {
  3115. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3116. struct sctp_authkeyid val;
  3117. struct sctp_association *asoc;
  3118. if (!ep->auth_enable)
  3119. return -EACCES;
  3120. if (optlen != sizeof(struct sctp_authkeyid))
  3121. return -EINVAL;
  3122. if (copy_from_user(&val, optval, optlen))
  3123. return -EFAULT;
  3124. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  3125. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  3126. return -EINVAL;
  3127. return sctp_auth_set_active_key(ep, asoc, val.scact_keynumber);
  3128. }
  3129. /*
  3130. * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
  3131. *
  3132. * This set option will delete a shared secret key from use.
  3133. */
  3134. static int sctp_setsockopt_del_key(struct sock *sk,
  3135. char __user *optval,
  3136. unsigned int optlen)
  3137. {
  3138. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3139. struct sctp_authkeyid val;
  3140. struct sctp_association *asoc;
  3141. if (!ep->auth_enable)
  3142. return -EACCES;
  3143. if (optlen != sizeof(struct sctp_authkeyid))
  3144. return -EINVAL;
  3145. if (copy_from_user(&val, optval, optlen))
  3146. return -EFAULT;
  3147. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  3148. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  3149. return -EINVAL;
  3150. return sctp_auth_del_key_id(ep, asoc, val.scact_keynumber);
  3151. }
  3152. /*
  3153. * 8.1.23 SCTP_AUTO_ASCONF
  3154. *
  3155. * This option will enable or disable the use of the automatic generation of
  3156. * ASCONF chunks to add and delete addresses to an existing association. Note
  3157. * that this option has two caveats namely: a) it only affects sockets that
  3158. * are bound to all addresses available to the SCTP stack, and b) the system
  3159. * administrator may have an overriding control that turns the ASCONF feature
  3160. * off no matter what setting the socket option may have.
  3161. * This option expects an integer boolean flag, where a non-zero value turns on
  3162. * the option, and a zero value turns off the option.
  3163. * Note. In this implementation, socket operation overrides default parameter
  3164. * being set by sysctl as well as FreeBSD implementation
  3165. */
  3166. static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
  3167. unsigned int optlen)
  3168. {
  3169. int val;
  3170. struct sctp_sock *sp = sctp_sk(sk);
  3171. if (optlen < sizeof(int))
  3172. return -EINVAL;
  3173. if (get_user(val, (int __user *)optval))
  3174. return -EFAULT;
  3175. if (!sctp_is_ep_boundall(sk) && val)
  3176. return -EINVAL;
  3177. if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
  3178. return 0;
  3179. spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
  3180. if (val == 0 && sp->do_auto_asconf) {
  3181. list_del(&sp->auto_asconf_list);
  3182. sp->do_auto_asconf = 0;
  3183. } else if (val && !sp->do_auto_asconf) {
  3184. list_add_tail(&sp->auto_asconf_list,
  3185. &sock_net(sk)->sctp.auto_asconf_splist);
  3186. sp->do_auto_asconf = 1;
  3187. }
  3188. spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
  3189. return 0;
  3190. }
  3191. /*
  3192. * SCTP_PEER_ADDR_THLDS
  3193. *
  3194. * This option allows us to alter the partially failed threshold for one or all
  3195. * transports in an association. See Section 6.1 of:
  3196. * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
  3197. */
  3198. static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
  3199. char __user *optval,
  3200. unsigned int optlen)
  3201. {
  3202. struct sctp_paddrthlds val;
  3203. struct sctp_transport *trans;
  3204. struct sctp_association *asoc;
  3205. if (optlen < sizeof(struct sctp_paddrthlds))
  3206. return -EINVAL;
  3207. if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
  3208. sizeof(struct sctp_paddrthlds)))
  3209. return -EFAULT;
  3210. if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
  3211. asoc = sctp_id2assoc(sk, val.spt_assoc_id);
  3212. if (!asoc)
  3213. return -ENOENT;
  3214. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  3215. transports) {
  3216. if (val.spt_pathmaxrxt)
  3217. trans->pathmaxrxt = val.spt_pathmaxrxt;
  3218. trans->pf_retrans = val.spt_pathpfthld;
  3219. }
  3220. if (val.spt_pathmaxrxt)
  3221. asoc->pathmaxrxt = val.spt_pathmaxrxt;
  3222. asoc->pf_retrans = val.spt_pathpfthld;
  3223. } else {
  3224. trans = sctp_addr_id2transport(sk, &val.spt_address,
  3225. val.spt_assoc_id);
  3226. if (!trans)
  3227. return -ENOENT;
  3228. if (val.spt_pathmaxrxt)
  3229. trans->pathmaxrxt = val.spt_pathmaxrxt;
  3230. trans->pf_retrans = val.spt_pathpfthld;
  3231. }
  3232. return 0;
  3233. }
  3234. static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
  3235. char __user *optval,
  3236. unsigned int optlen)
  3237. {
  3238. int val;
  3239. if (optlen < sizeof(int))
  3240. return -EINVAL;
  3241. if (get_user(val, (int __user *) optval))
  3242. return -EFAULT;
  3243. sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
  3244. return 0;
  3245. }
  3246. static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
  3247. char __user *optval,
  3248. unsigned int optlen)
  3249. {
  3250. int val;
  3251. if (optlen < sizeof(int))
  3252. return -EINVAL;
  3253. if (get_user(val, (int __user *) optval))
  3254. return -EFAULT;
  3255. sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
  3256. return 0;
  3257. }
  3258. static int sctp_setsockopt_pr_supported(struct sock *sk,
  3259. char __user *optval,
  3260. unsigned int optlen)
  3261. {
  3262. struct sctp_assoc_value params;
  3263. struct sctp_association *asoc;
  3264. int retval = -EINVAL;
  3265. if (optlen != sizeof(params))
  3266. goto out;
  3267. if (copy_from_user(&params, optval, optlen)) {
  3268. retval = -EFAULT;
  3269. goto out;
  3270. }
  3271. asoc = sctp_id2assoc(sk, params.assoc_id);
  3272. if (asoc) {
  3273. asoc->prsctp_enable = !!params.assoc_value;
  3274. } else if (!params.assoc_id) {
  3275. struct sctp_sock *sp = sctp_sk(sk);
  3276. sp->ep->prsctp_enable = !!params.assoc_value;
  3277. } else {
  3278. goto out;
  3279. }
  3280. retval = 0;
  3281. out:
  3282. return retval;
  3283. }
  3284. static int sctp_setsockopt_default_prinfo(struct sock *sk,
  3285. char __user *optval,
  3286. unsigned int optlen)
  3287. {
  3288. struct sctp_default_prinfo info;
  3289. struct sctp_association *asoc;
  3290. int retval = -EINVAL;
  3291. if (optlen != sizeof(info))
  3292. goto out;
  3293. if (copy_from_user(&info, optval, sizeof(info))) {
  3294. retval = -EFAULT;
  3295. goto out;
  3296. }
  3297. if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
  3298. goto out;
  3299. if (info.pr_policy == SCTP_PR_SCTP_NONE)
  3300. info.pr_value = 0;
  3301. asoc = sctp_id2assoc(sk, info.pr_assoc_id);
  3302. if (asoc) {
  3303. SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
  3304. asoc->default_timetolive = info.pr_value;
  3305. } else if (!info.pr_assoc_id) {
  3306. struct sctp_sock *sp = sctp_sk(sk);
  3307. SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
  3308. sp->default_timetolive = info.pr_value;
  3309. } else {
  3310. goto out;
  3311. }
  3312. retval = 0;
  3313. out:
  3314. return retval;
  3315. }
  3316. static int sctp_setsockopt_reconfig_supported(struct sock *sk,
  3317. char __user *optval,
  3318. unsigned int optlen)
  3319. {
  3320. struct sctp_assoc_value params;
  3321. struct sctp_association *asoc;
  3322. int retval = -EINVAL;
  3323. if (optlen != sizeof(params))
  3324. goto out;
  3325. if (copy_from_user(&params, optval, optlen)) {
  3326. retval = -EFAULT;
  3327. goto out;
  3328. }
  3329. asoc = sctp_id2assoc(sk, params.assoc_id);
  3330. if (asoc) {
  3331. asoc->reconf_enable = !!params.assoc_value;
  3332. } else if (!params.assoc_id) {
  3333. struct sctp_sock *sp = sctp_sk(sk);
  3334. sp->ep->reconf_enable = !!params.assoc_value;
  3335. } else {
  3336. goto out;
  3337. }
  3338. retval = 0;
  3339. out:
  3340. return retval;
  3341. }
  3342. static int sctp_setsockopt_enable_strreset(struct sock *sk,
  3343. char __user *optval,
  3344. unsigned int optlen)
  3345. {
  3346. struct sctp_assoc_value params;
  3347. struct sctp_association *asoc;
  3348. int retval = -EINVAL;
  3349. if (optlen != sizeof(params))
  3350. goto out;
  3351. if (copy_from_user(&params, optval, optlen)) {
  3352. retval = -EFAULT;
  3353. goto out;
  3354. }
  3355. if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
  3356. goto out;
  3357. asoc = sctp_id2assoc(sk, params.assoc_id);
  3358. if (asoc) {
  3359. asoc->strreset_enable = params.assoc_value;
  3360. } else if (!params.assoc_id) {
  3361. struct sctp_sock *sp = sctp_sk(sk);
  3362. sp->ep->strreset_enable = params.assoc_value;
  3363. } else {
  3364. goto out;
  3365. }
  3366. retval = 0;
  3367. out:
  3368. return retval;
  3369. }
  3370. static int sctp_setsockopt_reset_streams(struct sock *sk,
  3371. char __user *optval,
  3372. unsigned int optlen)
  3373. {
  3374. struct sctp_reset_streams *params;
  3375. struct sctp_association *asoc;
  3376. int retval = -EINVAL;
  3377. if (optlen < sizeof(struct sctp_reset_streams))
  3378. return -EINVAL;
  3379. params = memdup_user(optval, optlen);
  3380. if (IS_ERR(params))
  3381. return PTR_ERR(params);
  3382. asoc = sctp_id2assoc(sk, params->srs_assoc_id);
  3383. if (!asoc)
  3384. goto out;
  3385. retval = sctp_send_reset_streams(asoc, params);
  3386. out:
  3387. kfree(params);
  3388. return retval;
  3389. }
  3390. static int sctp_setsockopt_reset_assoc(struct sock *sk,
  3391. char __user *optval,
  3392. unsigned int optlen)
  3393. {
  3394. struct sctp_association *asoc;
  3395. sctp_assoc_t associd;
  3396. int retval = -EINVAL;
  3397. if (optlen != sizeof(associd))
  3398. goto out;
  3399. if (copy_from_user(&associd, optval, optlen)) {
  3400. retval = -EFAULT;
  3401. goto out;
  3402. }
  3403. asoc = sctp_id2assoc(sk, associd);
  3404. if (!asoc)
  3405. goto out;
  3406. retval = sctp_send_reset_assoc(asoc);
  3407. out:
  3408. return retval;
  3409. }
  3410. static int sctp_setsockopt_add_streams(struct sock *sk,
  3411. char __user *optval,
  3412. unsigned int optlen)
  3413. {
  3414. struct sctp_association *asoc;
  3415. struct sctp_add_streams params;
  3416. int retval = -EINVAL;
  3417. if (optlen != sizeof(params))
  3418. goto out;
  3419. if (copy_from_user(&params, optval, optlen)) {
  3420. retval = -EFAULT;
  3421. goto out;
  3422. }
  3423. asoc = sctp_id2assoc(sk, params.sas_assoc_id);
  3424. if (!asoc)
  3425. goto out;
  3426. retval = sctp_send_add_streams(asoc, &params);
  3427. out:
  3428. return retval;
  3429. }
  3430. static int sctp_setsockopt_scheduler(struct sock *sk,
  3431. char __user *optval,
  3432. unsigned int optlen)
  3433. {
  3434. struct sctp_association *asoc;
  3435. struct sctp_assoc_value params;
  3436. int retval = -EINVAL;
  3437. if (optlen < sizeof(params))
  3438. goto out;
  3439. optlen = sizeof(params);
  3440. if (copy_from_user(&params, optval, optlen)) {
  3441. retval = -EFAULT;
  3442. goto out;
  3443. }
  3444. if (params.assoc_value > SCTP_SS_MAX)
  3445. goto out;
  3446. asoc = sctp_id2assoc(sk, params.assoc_id);
  3447. if (!asoc)
  3448. goto out;
  3449. retval = sctp_sched_set_sched(asoc, params.assoc_value);
  3450. out:
  3451. return retval;
  3452. }
  3453. static int sctp_setsockopt_scheduler_value(struct sock *sk,
  3454. char __user *optval,
  3455. unsigned int optlen)
  3456. {
  3457. struct sctp_association *asoc;
  3458. struct sctp_stream_value params;
  3459. int retval = -EINVAL;
  3460. if (optlen < sizeof(params))
  3461. goto out;
  3462. optlen = sizeof(params);
  3463. if (copy_from_user(&params, optval, optlen)) {
  3464. retval = -EFAULT;
  3465. goto out;
  3466. }
  3467. asoc = sctp_id2assoc(sk, params.assoc_id);
  3468. if (!asoc)
  3469. goto out;
  3470. retval = sctp_sched_set_value(asoc, params.stream_id,
  3471. params.stream_value, GFP_KERNEL);
  3472. out:
  3473. return retval;
  3474. }
  3475. /* API 6.2 setsockopt(), getsockopt()
  3476. *
  3477. * Applications use setsockopt() and getsockopt() to set or retrieve
  3478. * socket options. Socket options are used to change the default
  3479. * behavior of sockets calls. They are described in Section 7.
  3480. *
  3481. * The syntax is:
  3482. *
  3483. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  3484. * int __user *optlen);
  3485. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  3486. * int optlen);
  3487. *
  3488. * sd - the socket descript.
  3489. * level - set to IPPROTO_SCTP for all SCTP options.
  3490. * optname - the option name.
  3491. * optval - the buffer to store the value of the option.
  3492. * optlen - the size of the buffer.
  3493. */
  3494. static int sctp_setsockopt(struct sock *sk, int level, int optname,
  3495. char __user *optval, unsigned int optlen)
  3496. {
  3497. int retval = 0;
  3498. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  3499. /* I can hardly begin to describe how wrong this is. This is
  3500. * so broken as to be worse than useless. The API draft
  3501. * REALLY is NOT helpful here... I am not convinced that the
  3502. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  3503. * are at all well-founded.
  3504. */
  3505. if (level != SOL_SCTP) {
  3506. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3507. retval = af->setsockopt(sk, level, optname, optval, optlen);
  3508. goto out_nounlock;
  3509. }
  3510. lock_sock(sk);
  3511. switch (optname) {
  3512. case SCTP_SOCKOPT_BINDX_ADD:
  3513. /* 'optlen' is the size of the addresses buffer. */
  3514. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  3515. optlen, SCTP_BINDX_ADD_ADDR);
  3516. break;
  3517. case SCTP_SOCKOPT_BINDX_REM:
  3518. /* 'optlen' is the size of the addresses buffer. */
  3519. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  3520. optlen, SCTP_BINDX_REM_ADDR);
  3521. break;
  3522. case SCTP_SOCKOPT_CONNECTX_OLD:
  3523. /* 'optlen' is the size of the addresses buffer. */
  3524. retval = sctp_setsockopt_connectx_old(sk,
  3525. (struct sockaddr __user *)optval,
  3526. optlen);
  3527. break;
  3528. case SCTP_SOCKOPT_CONNECTX:
  3529. /* 'optlen' is the size of the addresses buffer. */
  3530. retval = sctp_setsockopt_connectx(sk,
  3531. (struct sockaddr __user *)optval,
  3532. optlen);
  3533. break;
  3534. case SCTP_DISABLE_FRAGMENTS:
  3535. retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
  3536. break;
  3537. case SCTP_EVENTS:
  3538. retval = sctp_setsockopt_events(sk, optval, optlen);
  3539. break;
  3540. case SCTP_AUTOCLOSE:
  3541. retval = sctp_setsockopt_autoclose(sk, optval, optlen);
  3542. break;
  3543. case SCTP_PEER_ADDR_PARAMS:
  3544. retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
  3545. break;
  3546. case SCTP_DELAYED_SACK:
  3547. retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
  3548. break;
  3549. case SCTP_PARTIAL_DELIVERY_POINT:
  3550. retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
  3551. break;
  3552. case SCTP_INITMSG:
  3553. retval = sctp_setsockopt_initmsg(sk, optval, optlen);
  3554. break;
  3555. case SCTP_DEFAULT_SEND_PARAM:
  3556. retval = sctp_setsockopt_default_send_param(sk, optval,
  3557. optlen);
  3558. break;
  3559. case SCTP_DEFAULT_SNDINFO:
  3560. retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
  3561. break;
  3562. case SCTP_PRIMARY_ADDR:
  3563. retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
  3564. break;
  3565. case SCTP_SET_PEER_PRIMARY_ADDR:
  3566. retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
  3567. break;
  3568. case SCTP_NODELAY:
  3569. retval = sctp_setsockopt_nodelay(sk, optval, optlen);
  3570. break;
  3571. case SCTP_RTOINFO:
  3572. retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
  3573. break;
  3574. case SCTP_ASSOCINFO:
  3575. retval = sctp_setsockopt_associnfo(sk, optval, optlen);
  3576. break;
  3577. case SCTP_I_WANT_MAPPED_V4_ADDR:
  3578. retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
  3579. break;
  3580. case SCTP_MAXSEG:
  3581. retval = sctp_setsockopt_maxseg(sk, optval, optlen);
  3582. break;
  3583. case SCTP_ADAPTATION_LAYER:
  3584. retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
  3585. break;
  3586. case SCTP_CONTEXT:
  3587. retval = sctp_setsockopt_context(sk, optval, optlen);
  3588. break;
  3589. case SCTP_FRAGMENT_INTERLEAVE:
  3590. retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
  3591. break;
  3592. case SCTP_MAX_BURST:
  3593. retval = sctp_setsockopt_maxburst(sk, optval, optlen);
  3594. break;
  3595. case SCTP_AUTH_CHUNK:
  3596. retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
  3597. break;
  3598. case SCTP_HMAC_IDENT:
  3599. retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
  3600. break;
  3601. case SCTP_AUTH_KEY:
  3602. retval = sctp_setsockopt_auth_key(sk, optval, optlen);
  3603. break;
  3604. case SCTP_AUTH_ACTIVE_KEY:
  3605. retval = sctp_setsockopt_active_key(sk, optval, optlen);
  3606. break;
  3607. case SCTP_AUTH_DELETE_KEY:
  3608. retval = sctp_setsockopt_del_key(sk, optval, optlen);
  3609. break;
  3610. case SCTP_AUTO_ASCONF:
  3611. retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
  3612. break;
  3613. case SCTP_PEER_ADDR_THLDS:
  3614. retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
  3615. break;
  3616. case SCTP_RECVRCVINFO:
  3617. retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
  3618. break;
  3619. case SCTP_RECVNXTINFO:
  3620. retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
  3621. break;
  3622. case SCTP_PR_SUPPORTED:
  3623. retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
  3624. break;
  3625. case SCTP_DEFAULT_PRINFO:
  3626. retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
  3627. break;
  3628. case SCTP_RECONFIG_SUPPORTED:
  3629. retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
  3630. break;
  3631. case SCTP_ENABLE_STREAM_RESET:
  3632. retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
  3633. break;
  3634. case SCTP_RESET_STREAMS:
  3635. retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
  3636. break;
  3637. case SCTP_RESET_ASSOC:
  3638. retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
  3639. break;
  3640. case SCTP_ADD_STREAMS:
  3641. retval = sctp_setsockopt_add_streams(sk, optval, optlen);
  3642. break;
  3643. case SCTP_STREAM_SCHEDULER:
  3644. retval = sctp_setsockopt_scheduler(sk, optval, optlen);
  3645. break;
  3646. case SCTP_STREAM_SCHEDULER_VALUE:
  3647. retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
  3648. break;
  3649. default:
  3650. retval = -ENOPROTOOPT;
  3651. break;
  3652. }
  3653. release_sock(sk);
  3654. out_nounlock:
  3655. return retval;
  3656. }
  3657. /* API 3.1.6 connect() - UDP Style Syntax
  3658. *
  3659. * An application may use the connect() call in the UDP model to initiate an
  3660. * association without sending data.
  3661. *
  3662. * The syntax is:
  3663. *
  3664. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  3665. *
  3666. * sd: the socket descriptor to have a new association added to.
  3667. *
  3668. * nam: the address structure (either struct sockaddr_in or struct
  3669. * sockaddr_in6 defined in RFC2553 [7]).
  3670. *
  3671. * len: the size of the address.
  3672. */
  3673. static int sctp_connect(struct sock *sk, struct sockaddr *addr,
  3674. int addr_len)
  3675. {
  3676. int err = 0;
  3677. struct sctp_af *af;
  3678. lock_sock(sk);
  3679. pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
  3680. addr, addr_len);
  3681. /* Validate addr_len before calling common connect/connectx routine. */
  3682. af = sctp_get_af_specific(addr->sa_family);
  3683. if (!af || addr_len < af->sockaddr_len) {
  3684. err = -EINVAL;
  3685. } else {
  3686. /* Pass correct addr len to common routine (so it knows there
  3687. * is only one address being passed.
  3688. */
  3689. err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
  3690. }
  3691. release_sock(sk);
  3692. return err;
  3693. }
  3694. /* FIXME: Write comments. */
  3695. static int sctp_disconnect(struct sock *sk, int flags)
  3696. {
  3697. return -EOPNOTSUPP; /* STUB */
  3698. }
  3699. /* 4.1.4 accept() - TCP Style Syntax
  3700. *
  3701. * Applications use accept() call to remove an established SCTP
  3702. * association from the accept queue of the endpoint. A new socket
  3703. * descriptor will be returned from accept() to represent the newly
  3704. * formed association.
  3705. */
  3706. static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
  3707. {
  3708. struct sctp_sock *sp;
  3709. struct sctp_endpoint *ep;
  3710. struct sock *newsk = NULL;
  3711. struct sctp_association *asoc;
  3712. long timeo;
  3713. int error = 0;
  3714. lock_sock(sk);
  3715. sp = sctp_sk(sk);
  3716. ep = sp->ep;
  3717. if (!sctp_style(sk, TCP)) {
  3718. error = -EOPNOTSUPP;
  3719. goto out;
  3720. }
  3721. if (!sctp_sstate(sk, LISTENING)) {
  3722. error = -EINVAL;
  3723. goto out;
  3724. }
  3725. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  3726. error = sctp_wait_for_accept(sk, timeo);
  3727. if (error)
  3728. goto out;
  3729. /* We treat the list of associations on the endpoint as the accept
  3730. * queue and pick the first association on the list.
  3731. */
  3732. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  3733. newsk = sp->pf->create_accept_sk(sk, asoc, kern);
  3734. if (!newsk) {
  3735. error = -ENOMEM;
  3736. goto out;
  3737. }
  3738. /* Populate the fields of the newsk from the oldsk and migrate the
  3739. * asoc to the newsk.
  3740. */
  3741. sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  3742. out:
  3743. release_sock(sk);
  3744. *err = error;
  3745. return newsk;
  3746. }
  3747. /* The SCTP ioctl handler. */
  3748. static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  3749. {
  3750. int rc = -ENOTCONN;
  3751. lock_sock(sk);
  3752. /*
  3753. * SEQPACKET-style sockets in LISTENING state are valid, for
  3754. * SCTP, so only discard TCP-style sockets in LISTENING state.
  3755. */
  3756. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  3757. goto out;
  3758. switch (cmd) {
  3759. case SIOCINQ: {
  3760. struct sk_buff *skb;
  3761. unsigned int amount = 0;
  3762. skb = skb_peek(&sk->sk_receive_queue);
  3763. if (skb != NULL) {
  3764. /*
  3765. * We will only return the amount of this packet since
  3766. * that is all that will be read.
  3767. */
  3768. amount = skb->len;
  3769. }
  3770. rc = put_user(amount, (int __user *)arg);
  3771. break;
  3772. }
  3773. default:
  3774. rc = -ENOIOCTLCMD;
  3775. break;
  3776. }
  3777. out:
  3778. release_sock(sk);
  3779. return rc;
  3780. }
  3781. /* This is the function which gets called during socket creation to
  3782. * initialized the SCTP-specific portion of the sock.
  3783. * The sock structure should already be zero-filled memory.
  3784. */
  3785. static int sctp_init_sock(struct sock *sk)
  3786. {
  3787. struct net *net = sock_net(sk);
  3788. struct sctp_sock *sp;
  3789. pr_debug("%s: sk:%p\n", __func__, sk);
  3790. sp = sctp_sk(sk);
  3791. /* Initialize the SCTP per socket area. */
  3792. switch (sk->sk_type) {
  3793. case SOCK_SEQPACKET:
  3794. sp->type = SCTP_SOCKET_UDP;
  3795. break;
  3796. case SOCK_STREAM:
  3797. sp->type = SCTP_SOCKET_TCP;
  3798. break;
  3799. default:
  3800. return -ESOCKTNOSUPPORT;
  3801. }
  3802. sk->sk_gso_type = SKB_GSO_SCTP;
  3803. /* Initialize default send parameters. These parameters can be
  3804. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  3805. */
  3806. sp->default_stream = 0;
  3807. sp->default_ppid = 0;
  3808. sp->default_flags = 0;
  3809. sp->default_context = 0;
  3810. sp->default_timetolive = 0;
  3811. sp->default_rcv_context = 0;
  3812. sp->max_burst = net->sctp.max_burst;
  3813. sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
  3814. /* Initialize default setup parameters. These parameters
  3815. * can be modified with the SCTP_INITMSG socket option or
  3816. * overridden by the SCTP_INIT CMSG.
  3817. */
  3818. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  3819. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  3820. sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
  3821. sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
  3822. /* Initialize default RTO related parameters. These parameters can
  3823. * be modified for with the SCTP_RTOINFO socket option.
  3824. */
  3825. sp->rtoinfo.srto_initial = net->sctp.rto_initial;
  3826. sp->rtoinfo.srto_max = net->sctp.rto_max;
  3827. sp->rtoinfo.srto_min = net->sctp.rto_min;
  3828. /* Initialize default association related parameters. These parameters
  3829. * can be modified with the SCTP_ASSOCINFO socket option.
  3830. */
  3831. sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
  3832. sp->assocparams.sasoc_number_peer_destinations = 0;
  3833. sp->assocparams.sasoc_peer_rwnd = 0;
  3834. sp->assocparams.sasoc_local_rwnd = 0;
  3835. sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
  3836. /* Initialize default event subscriptions. By default, all the
  3837. * options are off.
  3838. */
  3839. memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
  3840. /* Default Peer Address Parameters. These defaults can
  3841. * be modified via SCTP_PEER_ADDR_PARAMS
  3842. */
  3843. sp->hbinterval = net->sctp.hb_interval;
  3844. sp->pathmaxrxt = net->sctp.max_retrans_path;
  3845. sp->pathmtu = 0; /* allow default discovery */
  3846. sp->sackdelay = net->sctp.sack_timeout;
  3847. sp->sackfreq = 2;
  3848. sp->param_flags = SPP_HB_ENABLE |
  3849. SPP_PMTUD_ENABLE |
  3850. SPP_SACKDELAY_ENABLE;
  3851. /* If enabled no SCTP message fragmentation will be performed.
  3852. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  3853. */
  3854. sp->disable_fragments = 0;
  3855. /* Enable Nagle algorithm by default. */
  3856. sp->nodelay = 0;
  3857. sp->recvrcvinfo = 0;
  3858. sp->recvnxtinfo = 0;
  3859. /* Enable by default. */
  3860. sp->v4mapped = 1;
  3861. /* Auto-close idle associations after the configured
  3862. * number of seconds. A value of 0 disables this
  3863. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  3864. * for UDP-style sockets only.
  3865. */
  3866. sp->autoclose = 0;
  3867. /* User specified fragmentation limit. */
  3868. sp->user_frag = 0;
  3869. sp->adaptation_ind = 0;
  3870. sp->pf = sctp_get_pf_specific(sk->sk_family);
  3871. /* Control variables for partial data delivery. */
  3872. atomic_set(&sp->pd_mode, 0);
  3873. skb_queue_head_init(&sp->pd_lobby);
  3874. sp->frag_interleave = 0;
  3875. /* Create a per socket endpoint structure. Even if we
  3876. * change the data structure relationships, this may still
  3877. * be useful for storing pre-connect address information.
  3878. */
  3879. sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
  3880. if (!sp->ep)
  3881. return -ENOMEM;
  3882. sp->hmac = NULL;
  3883. sk->sk_destruct = sctp_destruct_sock;
  3884. SCTP_DBG_OBJCNT_INC(sock);
  3885. local_bh_disable();
  3886. percpu_counter_inc(&sctp_sockets_allocated);
  3887. sock_prot_inuse_add(net, sk->sk_prot, 1);
  3888. /* Nothing can fail after this block, otherwise
  3889. * sctp_destroy_sock() will be called without addr_wq_lock held
  3890. */
  3891. if (net->sctp.default_auto_asconf) {
  3892. spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
  3893. list_add_tail(&sp->auto_asconf_list,
  3894. &net->sctp.auto_asconf_splist);
  3895. sp->do_auto_asconf = 1;
  3896. spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
  3897. } else {
  3898. sp->do_auto_asconf = 0;
  3899. }
  3900. local_bh_enable();
  3901. return 0;
  3902. }
  3903. /* Cleanup any SCTP per socket resources. Must be called with
  3904. * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
  3905. */
  3906. static void sctp_destroy_sock(struct sock *sk)
  3907. {
  3908. struct sctp_sock *sp;
  3909. pr_debug("%s: sk:%p\n", __func__, sk);
  3910. /* Release our hold on the endpoint. */
  3911. sp = sctp_sk(sk);
  3912. /* This could happen during socket init, thus we bail out
  3913. * early, since the rest of the below is not setup either.
  3914. */
  3915. if (sp->ep == NULL)
  3916. return;
  3917. if (sp->do_auto_asconf) {
  3918. sp->do_auto_asconf = 0;
  3919. list_del(&sp->auto_asconf_list);
  3920. }
  3921. sctp_endpoint_free(sp->ep);
  3922. local_bh_disable();
  3923. percpu_counter_dec(&sctp_sockets_allocated);
  3924. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  3925. local_bh_enable();
  3926. }
  3927. /* Triggered when there are no references on the socket anymore */
  3928. static void sctp_destruct_sock(struct sock *sk)
  3929. {
  3930. struct sctp_sock *sp = sctp_sk(sk);
  3931. /* Free up the HMAC transform. */
  3932. crypto_free_shash(sp->hmac);
  3933. inet_sock_destruct(sk);
  3934. }
  3935. /* API 4.1.7 shutdown() - TCP Style Syntax
  3936. * int shutdown(int socket, int how);
  3937. *
  3938. * sd - the socket descriptor of the association to be closed.
  3939. * how - Specifies the type of shutdown. The values are
  3940. * as follows:
  3941. * SHUT_RD
  3942. * Disables further receive operations. No SCTP
  3943. * protocol action is taken.
  3944. * SHUT_WR
  3945. * Disables further send operations, and initiates
  3946. * the SCTP shutdown sequence.
  3947. * SHUT_RDWR
  3948. * Disables further send and receive operations
  3949. * and initiates the SCTP shutdown sequence.
  3950. */
  3951. static void sctp_shutdown(struct sock *sk, int how)
  3952. {
  3953. struct net *net = sock_net(sk);
  3954. struct sctp_endpoint *ep;
  3955. if (!sctp_style(sk, TCP))
  3956. return;
  3957. ep = sctp_sk(sk)->ep;
  3958. if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
  3959. struct sctp_association *asoc;
  3960. sk->sk_state = SCTP_SS_CLOSING;
  3961. asoc = list_entry(ep->asocs.next,
  3962. struct sctp_association, asocs);
  3963. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  3964. }
  3965. }
  3966. int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
  3967. struct sctp_info *info)
  3968. {
  3969. struct sctp_transport *prim;
  3970. struct list_head *pos;
  3971. int mask;
  3972. memset(info, 0, sizeof(*info));
  3973. if (!asoc) {
  3974. struct sctp_sock *sp = sctp_sk(sk);
  3975. info->sctpi_s_autoclose = sp->autoclose;
  3976. info->sctpi_s_adaptation_ind = sp->adaptation_ind;
  3977. info->sctpi_s_pd_point = sp->pd_point;
  3978. info->sctpi_s_nodelay = sp->nodelay;
  3979. info->sctpi_s_disable_fragments = sp->disable_fragments;
  3980. info->sctpi_s_v4mapped = sp->v4mapped;
  3981. info->sctpi_s_frag_interleave = sp->frag_interleave;
  3982. info->sctpi_s_type = sp->type;
  3983. return 0;
  3984. }
  3985. info->sctpi_tag = asoc->c.my_vtag;
  3986. info->sctpi_state = asoc->state;
  3987. info->sctpi_rwnd = asoc->a_rwnd;
  3988. info->sctpi_unackdata = asoc->unack_data;
  3989. info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  3990. info->sctpi_instrms = asoc->stream.incnt;
  3991. info->sctpi_outstrms = asoc->stream.outcnt;
  3992. list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
  3993. info->sctpi_inqueue++;
  3994. list_for_each(pos, &asoc->outqueue.out_chunk_list)
  3995. info->sctpi_outqueue++;
  3996. info->sctpi_overall_error = asoc->overall_error_count;
  3997. info->sctpi_max_burst = asoc->max_burst;
  3998. info->sctpi_maxseg = asoc->frag_point;
  3999. info->sctpi_peer_rwnd = asoc->peer.rwnd;
  4000. info->sctpi_peer_tag = asoc->c.peer_vtag;
  4001. mask = asoc->peer.ecn_capable << 1;
  4002. mask = (mask | asoc->peer.ipv4_address) << 1;
  4003. mask = (mask | asoc->peer.ipv6_address) << 1;
  4004. mask = (mask | asoc->peer.hostname_address) << 1;
  4005. mask = (mask | asoc->peer.asconf_capable) << 1;
  4006. mask = (mask | asoc->peer.prsctp_capable) << 1;
  4007. mask = (mask | asoc->peer.auth_capable);
  4008. info->sctpi_peer_capable = mask;
  4009. mask = asoc->peer.sack_needed << 1;
  4010. mask = (mask | asoc->peer.sack_generation) << 1;
  4011. mask = (mask | asoc->peer.zero_window_announced);
  4012. info->sctpi_peer_sack = mask;
  4013. info->sctpi_isacks = asoc->stats.isacks;
  4014. info->sctpi_osacks = asoc->stats.osacks;
  4015. info->sctpi_opackets = asoc->stats.opackets;
  4016. info->sctpi_ipackets = asoc->stats.ipackets;
  4017. info->sctpi_rtxchunks = asoc->stats.rtxchunks;
  4018. info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
  4019. info->sctpi_idupchunks = asoc->stats.idupchunks;
  4020. info->sctpi_gapcnt = asoc->stats.gapcnt;
  4021. info->sctpi_ouodchunks = asoc->stats.ouodchunks;
  4022. info->sctpi_iuodchunks = asoc->stats.iuodchunks;
  4023. info->sctpi_oodchunks = asoc->stats.oodchunks;
  4024. info->sctpi_iodchunks = asoc->stats.iodchunks;
  4025. info->sctpi_octrlchunks = asoc->stats.octrlchunks;
  4026. info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
  4027. prim = asoc->peer.primary_path;
  4028. memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
  4029. info->sctpi_p_state = prim->state;
  4030. info->sctpi_p_cwnd = prim->cwnd;
  4031. info->sctpi_p_srtt = prim->srtt;
  4032. info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
  4033. info->sctpi_p_hbinterval = prim->hbinterval;
  4034. info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
  4035. info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
  4036. info->sctpi_p_ssthresh = prim->ssthresh;
  4037. info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
  4038. info->sctpi_p_flight_size = prim->flight_size;
  4039. info->sctpi_p_error = prim->error_count;
  4040. return 0;
  4041. }
  4042. EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
  4043. /* use callback to avoid exporting the core structure */
  4044. int sctp_transport_walk_start(struct rhashtable_iter *iter)
  4045. {
  4046. int err;
  4047. rhltable_walk_enter(&sctp_transport_hashtable, iter);
  4048. err = rhashtable_walk_start(iter);
  4049. if (err && err != -EAGAIN) {
  4050. rhashtable_walk_stop(iter);
  4051. rhashtable_walk_exit(iter);
  4052. return err;
  4053. }
  4054. return 0;
  4055. }
  4056. void sctp_transport_walk_stop(struct rhashtable_iter *iter)
  4057. {
  4058. rhashtable_walk_stop(iter);
  4059. rhashtable_walk_exit(iter);
  4060. }
  4061. struct sctp_transport *sctp_transport_get_next(struct net *net,
  4062. struct rhashtable_iter *iter)
  4063. {
  4064. struct sctp_transport *t;
  4065. t = rhashtable_walk_next(iter);
  4066. for (; t; t = rhashtable_walk_next(iter)) {
  4067. if (IS_ERR(t)) {
  4068. if (PTR_ERR(t) == -EAGAIN)
  4069. continue;
  4070. break;
  4071. }
  4072. if (net_eq(sock_net(t->asoc->base.sk), net) &&
  4073. t->asoc->peer.primary_path == t)
  4074. break;
  4075. }
  4076. return t;
  4077. }
  4078. struct sctp_transport *sctp_transport_get_idx(struct net *net,
  4079. struct rhashtable_iter *iter,
  4080. int pos)
  4081. {
  4082. void *obj = SEQ_START_TOKEN;
  4083. while (pos && (obj = sctp_transport_get_next(net, iter)) &&
  4084. !IS_ERR(obj))
  4085. pos--;
  4086. return obj;
  4087. }
  4088. int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
  4089. void *p) {
  4090. int err = 0;
  4091. int hash = 0;
  4092. struct sctp_ep_common *epb;
  4093. struct sctp_hashbucket *head;
  4094. for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
  4095. hash++, head++) {
  4096. read_lock_bh(&head->lock);
  4097. sctp_for_each_hentry(epb, &head->chain) {
  4098. err = cb(sctp_ep(epb), p);
  4099. if (err)
  4100. break;
  4101. }
  4102. read_unlock_bh(&head->lock);
  4103. }
  4104. return err;
  4105. }
  4106. EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
  4107. int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
  4108. struct net *net,
  4109. const union sctp_addr *laddr,
  4110. const union sctp_addr *paddr, void *p)
  4111. {
  4112. struct sctp_transport *transport;
  4113. int err;
  4114. rcu_read_lock();
  4115. transport = sctp_addrs_lookup_transport(net, laddr, paddr);
  4116. rcu_read_unlock();
  4117. if (!transport)
  4118. return -ENOENT;
  4119. err = cb(transport, p);
  4120. sctp_transport_put(transport);
  4121. return err;
  4122. }
  4123. EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
  4124. int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
  4125. int (*cb_done)(struct sctp_transport *, void *),
  4126. struct net *net, int *pos, void *p) {
  4127. struct rhashtable_iter hti;
  4128. struct sctp_transport *tsp;
  4129. int ret;
  4130. again:
  4131. ret = sctp_transport_walk_start(&hti);
  4132. if (ret)
  4133. return ret;
  4134. tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
  4135. for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
  4136. if (!sctp_transport_hold(tsp))
  4137. continue;
  4138. ret = cb(tsp, p);
  4139. if (ret)
  4140. break;
  4141. (*pos)++;
  4142. sctp_transport_put(tsp);
  4143. }
  4144. sctp_transport_walk_stop(&hti);
  4145. if (ret) {
  4146. if (cb_done && !cb_done(tsp, p)) {
  4147. (*pos)++;
  4148. sctp_transport_put(tsp);
  4149. goto again;
  4150. }
  4151. sctp_transport_put(tsp);
  4152. }
  4153. return ret;
  4154. }
  4155. EXPORT_SYMBOL_GPL(sctp_for_each_transport);
  4156. /* 7.2.1 Association Status (SCTP_STATUS)
  4157. * Applications can retrieve current status information about an
  4158. * association, including association state, peer receiver window size,
  4159. * number of unacked data chunks, and number of data chunks pending
  4160. * receipt. This information is read-only.
  4161. */
  4162. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  4163. char __user *optval,
  4164. int __user *optlen)
  4165. {
  4166. struct sctp_status status;
  4167. struct sctp_association *asoc = NULL;
  4168. struct sctp_transport *transport;
  4169. sctp_assoc_t associd;
  4170. int retval = 0;
  4171. if (len < sizeof(status)) {
  4172. retval = -EINVAL;
  4173. goto out;
  4174. }
  4175. len = sizeof(status);
  4176. if (copy_from_user(&status, optval, len)) {
  4177. retval = -EFAULT;
  4178. goto out;
  4179. }
  4180. associd = status.sstat_assoc_id;
  4181. asoc = sctp_id2assoc(sk, associd);
  4182. if (!asoc) {
  4183. retval = -EINVAL;
  4184. goto out;
  4185. }
  4186. transport = asoc->peer.primary_path;
  4187. status.sstat_assoc_id = sctp_assoc2id(asoc);
  4188. status.sstat_state = sctp_assoc_to_state(asoc);
  4189. status.sstat_rwnd = asoc->peer.rwnd;
  4190. status.sstat_unackdata = asoc->unack_data;
  4191. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  4192. status.sstat_instrms = asoc->stream.incnt;
  4193. status.sstat_outstrms = asoc->stream.outcnt;
  4194. status.sstat_fragmentation_point = asoc->frag_point;
  4195. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  4196. memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
  4197. transport->af_specific->sockaddr_len);
  4198. /* Map ipv4 address into v4-mapped-on-v6 address. */
  4199. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
  4200. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  4201. status.sstat_primary.spinfo_state = transport->state;
  4202. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  4203. status.sstat_primary.spinfo_srtt = transport->srtt;
  4204. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  4205. status.sstat_primary.spinfo_mtu = transport->pathmtu;
  4206. if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
  4207. status.sstat_primary.spinfo_state = SCTP_ACTIVE;
  4208. if (put_user(len, optlen)) {
  4209. retval = -EFAULT;
  4210. goto out;
  4211. }
  4212. pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
  4213. __func__, len, status.sstat_state, status.sstat_rwnd,
  4214. status.sstat_assoc_id);
  4215. if (copy_to_user(optval, &status, len)) {
  4216. retval = -EFAULT;
  4217. goto out;
  4218. }
  4219. out:
  4220. return retval;
  4221. }
  4222. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  4223. *
  4224. * Applications can retrieve information about a specific peer address
  4225. * of an association, including its reachability state, congestion
  4226. * window, and retransmission timer values. This information is
  4227. * read-only.
  4228. */
  4229. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  4230. char __user *optval,
  4231. int __user *optlen)
  4232. {
  4233. struct sctp_paddrinfo pinfo;
  4234. struct sctp_transport *transport;
  4235. int retval = 0;
  4236. if (len < sizeof(pinfo)) {
  4237. retval = -EINVAL;
  4238. goto out;
  4239. }
  4240. len = sizeof(pinfo);
  4241. if (copy_from_user(&pinfo, optval, len)) {
  4242. retval = -EFAULT;
  4243. goto out;
  4244. }
  4245. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  4246. pinfo.spinfo_assoc_id);
  4247. if (!transport)
  4248. return -EINVAL;
  4249. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  4250. pinfo.spinfo_state = transport->state;
  4251. pinfo.spinfo_cwnd = transport->cwnd;
  4252. pinfo.spinfo_srtt = transport->srtt;
  4253. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  4254. pinfo.spinfo_mtu = transport->pathmtu;
  4255. if (pinfo.spinfo_state == SCTP_UNKNOWN)
  4256. pinfo.spinfo_state = SCTP_ACTIVE;
  4257. if (put_user(len, optlen)) {
  4258. retval = -EFAULT;
  4259. goto out;
  4260. }
  4261. if (copy_to_user(optval, &pinfo, len)) {
  4262. retval = -EFAULT;
  4263. goto out;
  4264. }
  4265. out:
  4266. return retval;
  4267. }
  4268. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  4269. *
  4270. * This option is a on/off flag. If enabled no SCTP message
  4271. * fragmentation will be performed. Instead if a message being sent
  4272. * exceeds the current PMTU size, the message will NOT be sent and
  4273. * instead a error will be indicated to the user.
  4274. */
  4275. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  4276. char __user *optval, int __user *optlen)
  4277. {
  4278. int val;
  4279. if (len < sizeof(int))
  4280. return -EINVAL;
  4281. len = sizeof(int);
  4282. val = (sctp_sk(sk)->disable_fragments == 1);
  4283. if (put_user(len, optlen))
  4284. return -EFAULT;
  4285. if (copy_to_user(optval, &val, len))
  4286. return -EFAULT;
  4287. return 0;
  4288. }
  4289. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  4290. *
  4291. * This socket option is used to specify various notifications and
  4292. * ancillary data the user wishes to receive.
  4293. */
  4294. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  4295. int __user *optlen)
  4296. {
  4297. if (len == 0)
  4298. return -EINVAL;
  4299. if (len > sizeof(struct sctp_event_subscribe))
  4300. len = sizeof(struct sctp_event_subscribe);
  4301. if (put_user(len, optlen))
  4302. return -EFAULT;
  4303. if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
  4304. return -EFAULT;
  4305. return 0;
  4306. }
  4307. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  4308. *
  4309. * This socket option is applicable to the UDP-style socket only. When
  4310. * set it will cause associations that are idle for more than the
  4311. * specified number of seconds to automatically close. An association
  4312. * being idle is defined an association that has NOT sent or received
  4313. * user data. The special value of '0' indicates that no automatic
  4314. * close of any associations should be performed. The option expects an
  4315. * integer defining the number of seconds of idle time before an
  4316. * association is closed.
  4317. */
  4318. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4319. {
  4320. /* Applicable to UDP-style socket only */
  4321. if (sctp_style(sk, TCP))
  4322. return -EOPNOTSUPP;
  4323. if (len < sizeof(int))
  4324. return -EINVAL;
  4325. len = sizeof(int);
  4326. if (put_user(len, optlen))
  4327. return -EFAULT;
  4328. if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int)))
  4329. return -EFAULT;
  4330. return 0;
  4331. }
  4332. /* Helper routine to branch off an association to a new socket. */
  4333. int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
  4334. {
  4335. struct sctp_association *asoc = sctp_id2assoc(sk, id);
  4336. struct sctp_sock *sp = sctp_sk(sk);
  4337. struct socket *sock;
  4338. int err = 0;
  4339. /* Do not peel off from one netns to another one. */
  4340. if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
  4341. return -EINVAL;
  4342. if (!asoc)
  4343. return -EINVAL;
  4344. /* An association cannot be branched off from an already peeled-off
  4345. * socket, nor is this supported for tcp style sockets.
  4346. */
  4347. if (!sctp_style(sk, UDP))
  4348. return -EINVAL;
  4349. /* Create a new socket. */
  4350. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  4351. if (err < 0)
  4352. return err;
  4353. sctp_copy_sock(sock->sk, sk, asoc);
  4354. /* Make peeled-off sockets more like 1-1 accepted sockets.
  4355. * Set the daddr and initialize id to something more random
  4356. */
  4357. sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
  4358. /* Populate the fields of the newsk from the oldsk and migrate the
  4359. * asoc to the newsk.
  4360. */
  4361. sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  4362. *sockp = sock;
  4363. return err;
  4364. }
  4365. EXPORT_SYMBOL(sctp_do_peeloff);
  4366. static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
  4367. struct file **newfile, unsigned flags)
  4368. {
  4369. struct socket *newsock;
  4370. int retval;
  4371. retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
  4372. if (retval < 0)
  4373. goto out;
  4374. /* Map the socket to an unused fd that can be returned to the user. */
  4375. retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
  4376. if (retval < 0) {
  4377. sock_release(newsock);
  4378. goto out;
  4379. }
  4380. *newfile = sock_alloc_file(newsock, 0, NULL);
  4381. if (IS_ERR(*newfile)) {
  4382. put_unused_fd(retval);
  4383. sock_release(newsock);
  4384. retval = PTR_ERR(*newfile);
  4385. *newfile = NULL;
  4386. return retval;
  4387. }
  4388. pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
  4389. retval);
  4390. peeloff->sd = retval;
  4391. if (flags & SOCK_NONBLOCK)
  4392. (*newfile)->f_flags |= O_NONBLOCK;
  4393. out:
  4394. return retval;
  4395. }
  4396. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4397. {
  4398. sctp_peeloff_arg_t peeloff;
  4399. struct file *newfile = NULL;
  4400. int retval = 0;
  4401. if (len < sizeof(sctp_peeloff_arg_t))
  4402. return -EINVAL;
  4403. len = sizeof(sctp_peeloff_arg_t);
  4404. if (copy_from_user(&peeloff, optval, len))
  4405. return -EFAULT;
  4406. retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
  4407. if (retval < 0)
  4408. goto out;
  4409. /* Return the fd mapped to the new socket. */
  4410. if (put_user(len, optlen)) {
  4411. fput(newfile);
  4412. put_unused_fd(retval);
  4413. return -EFAULT;
  4414. }
  4415. if (copy_to_user(optval, &peeloff, len)) {
  4416. fput(newfile);
  4417. put_unused_fd(retval);
  4418. return -EFAULT;
  4419. }
  4420. fd_install(retval, newfile);
  4421. out:
  4422. return retval;
  4423. }
  4424. static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
  4425. char __user *optval, int __user *optlen)
  4426. {
  4427. sctp_peeloff_flags_arg_t peeloff;
  4428. struct file *newfile = NULL;
  4429. int retval = 0;
  4430. if (len < sizeof(sctp_peeloff_flags_arg_t))
  4431. return -EINVAL;
  4432. len = sizeof(sctp_peeloff_flags_arg_t);
  4433. if (copy_from_user(&peeloff, optval, len))
  4434. return -EFAULT;
  4435. retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
  4436. &newfile, peeloff.flags);
  4437. if (retval < 0)
  4438. goto out;
  4439. /* Return the fd mapped to the new socket. */
  4440. if (put_user(len, optlen)) {
  4441. fput(newfile);
  4442. put_unused_fd(retval);
  4443. return -EFAULT;
  4444. }
  4445. if (copy_to_user(optval, &peeloff, len)) {
  4446. fput(newfile);
  4447. put_unused_fd(retval);
  4448. return -EFAULT;
  4449. }
  4450. fd_install(retval, newfile);
  4451. out:
  4452. return retval;
  4453. }
  4454. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  4455. *
  4456. * Applications can enable or disable heartbeats for any peer address of
  4457. * an association, modify an address's heartbeat interval, force a
  4458. * heartbeat to be sent immediately, and adjust the address's maximum
  4459. * number of retransmissions sent before an address is considered
  4460. * unreachable. The following structure is used to access and modify an
  4461. * address's parameters:
  4462. *
  4463. * struct sctp_paddrparams {
  4464. * sctp_assoc_t spp_assoc_id;
  4465. * struct sockaddr_storage spp_address;
  4466. * uint32_t spp_hbinterval;
  4467. * uint16_t spp_pathmaxrxt;
  4468. * uint32_t spp_pathmtu;
  4469. * uint32_t spp_sackdelay;
  4470. * uint32_t spp_flags;
  4471. * };
  4472. *
  4473. * spp_assoc_id - (one-to-many style socket) This is filled in the
  4474. * application, and identifies the association for
  4475. * this query.
  4476. * spp_address - This specifies which address is of interest.
  4477. * spp_hbinterval - This contains the value of the heartbeat interval,
  4478. * in milliseconds. If a value of zero
  4479. * is present in this field then no changes are to
  4480. * be made to this parameter.
  4481. * spp_pathmaxrxt - This contains the maximum number of
  4482. * retransmissions before this address shall be
  4483. * considered unreachable. If a value of zero
  4484. * is present in this field then no changes are to
  4485. * be made to this parameter.
  4486. * spp_pathmtu - When Path MTU discovery is disabled the value
  4487. * specified here will be the "fixed" path mtu.
  4488. * Note that if the spp_address field is empty
  4489. * then all associations on this address will
  4490. * have this fixed path mtu set upon them.
  4491. *
  4492. * spp_sackdelay - When delayed sack is enabled, this value specifies
  4493. * the number of milliseconds that sacks will be delayed
  4494. * for. This value will apply to all addresses of an
  4495. * association if the spp_address field is empty. Note
  4496. * also, that if delayed sack is enabled and this
  4497. * value is set to 0, no change is made to the last
  4498. * recorded delayed sack timer value.
  4499. *
  4500. * spp_flags - These flags are used to control various features
  4501. * on an association. The flag field may contain
  4502. * zero or more of the following options.
  4503. *
  4504. * SPP_HB_ENABLE - Enable heartbeats on the
  4505. * specified address. Note that if the address
  4506. * field is empty all addresses for the association
  4507. * have heartbeats enabled upon them.
  4508. *
  4509. * SPP_HB_DISABLE - Disable heartbeats on the
  4510. * speicifed address. Note that if the address
  4511. * field is empty all addresses for the association
  4512. * will have their heartbeats disabled. Note also
  4513. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  4514. * mutually exclusive, only one of these two should
  4515. * be specified. Enabling both fields will have
  4516. * undetermined results.
  4517. *
  4518. * SPP_HB_DEMAND - Request a user initiated heartbeat
  4519. * to be made immediately.
  4520. *
  4521. * SPP_PMTUD_ENABLE - This field will enable PMTU
  4522. * discovery upon the specified address. Note that
  4523. * if the address feild is empty then all addresses
  4524. * on the association are effected.
  4525. *
  4526. * SPP_PMTUD_DISABLE - This field will disable PMTU
  4527. * discovery upon the specified address. Note that
  4528. * if the address feild is empty then all addresses
  4529. * on the association are effected. Not also that
  4530. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  4531. * exclusive. Enabling both will have undetermined
  4532. * results.
  4533. *
  4534. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  4535. * on delayed sack. The time specified in spp_sackdelay
  4536. * is used to specify the sack delay for this address. Note
  4537. * that if spp_address is empty then all addresses will
  4538. * enable delayed sack and take on the sack delay
  4539. * value specified in spp_sackdelay.
  4540. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  4541. * off delayed sack. If the spp_address field is blank then
  4542. * delayed sack is disabled for the entire association. Note
  4543. * also that this field is mutually exclusive to
  4544. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  4545. * results.
  4546. */
  4547. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  4548. char __user *optval, int __user *optlen)
  4549. {
  4550. struct sctp_paddrparams params;
  4551. struct sctp_transport *trans = NULL;
  4552. struct sctp_association *asoc = NULL;
  4553. struct sctp_sock *sp = sctp_sk(sk);
  4554. if (len < sizeof(struct sctp_paddrparams))
  4555. return -EINVAL;
  4556. len = sizeof(struct sctp_paddrparams);
  4557. if (copy_from_user(&params, optval, len))
  4558. return -EFAULT;
  4559. /* If an address other than INADDR_ANY is specified, and
  4560. * no transport is found, then the request is invalid.
  4561. */
  4562. if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
  4563. trans = sctp_addr_id2transport(sk, &params.spp_address,
  4564. params.spp_assoc_id);
  4565. if (!trans) {
  4566. pr_debug("%s: failed no transport\n", __func__);
  4567. return -EINVAL;
  4568. }
  4569. }
  4570. /* Get association, if assoc_id != 0 and the socket is a one
  4571. * to many style socket, and an association was not found, then
  4572. * the id was invalid.
  4573. */
  4574. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  4575. if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
  4576. pr_debug("%s: failed no association\n", __func__);
  4577. return -EINVAL;
  4578. }
  4579. if (trans) {
  4580. /* Fetch transport values. */
  4581. params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
  4582. params.spp_pathmtu = trans->pathmtu;
  4583. params.spp_pathmaxrxt = trans->pathmaxrxt;
  4584. params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
  4585. /*draft-11 doesn't say what to return in spp_flags*/
  4586. params.spp_flags = trans->param_flags;
  4587. } else if (asoc) {
  4588. /* Fetch association values. */
  4589. params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
  4590. params.spp_pathmtu = asoc->pathmtu;
  4591. params.spp_pathmaxrxt = asoc->pathmaxrxt;
  4592. params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
  4593. /*draft-11 doesn't say what to return in spp_flags*/
  4594. params.spp_flags = asoc->param_flags;
  4595. } else {
  4596. /* Fetch socket values. */
  4597. params.spp_hbinterval = sp->hbinterval;
  4598. params.spp_pathmtu = sp->pathmtu;
  4599. params.spp_sackdelay = sp->sackdelay;
  4600. params.spp_pathmaxrxt = sp->pathmaxrxt;
  4601. /*draft-11 doesn't say what to return in spp_flags*/
  4602. params.spp_flags = sp->param_flags;
  4603. }
  4604. if (copy_to_user(optval, &params, len))
  4605. return -EFAULT;
  4606. if (put_user(len, optlen))
  4607. return -EFAULT;
  4608. return 0;
  4609. }
  4610. /*
  4611. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  4612. *
  4613. * This option will effect the way delayed acks are performed. This
  4614. * option allows you to get or set the delayed ack time, in
  4615. * milliseconds. It also allows changing the delayed ack frequency.
  4616. * Changing the frequency to 1 disables the delayed sack algorithm. If
  4617. * the assoc_id is 0, then this sets or gets the endpoints default
  4618. * values. If the assoc_id field is non-zero, then the set or get
  4619. * effects the specified association for the one to many model (the
  4620. * assoc_id field is ignored by the one to one model). Note that if
  4621. * sack_delay or sack_freq are 0 when setting this option, then the
  4622. * current values will remain unchanged.
  4623. *
  4624. * struct sctp_sack_info {
  4625. * sctp_assoc_t sack_assoc_id;
  4626. * uint32_t sack_delay;
  4627. * uint32_t sack_freq;
  4628. * };
  4629. *
  4630. * sack_assoc_id - This parameter, indicates which association the user
  4631. * is performing an action upon. Note that if this field's value is
  4632. * zero then the endpoints default value is changed (effecting future
  4633. * associations only).
  4634. *
  4635. * sack_delay - This parameter contains the number of milliseconds that
  4636. * the user is requesting the delayed ACK timer be set to. Note that
  4637. * this value is defined in the standard to be between 200 and 500
  4638. * milliseconds.
  4639. *
  4640. * sack_freq - This parameter contains the number of packets that must
  4641. * be received before a sack is sent without waiting for the delay
  4642. * timer to expire. The default value for this is 2, setting this
  4643. * value to 1 will disable the delayed sack algorithm.
  4644. */
  4645. static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
  4646. char __user *optval,
  4647. int __user *optlen)
  4648. {
  4649. struct sctp_sack_info params;
  4650. struct sctp_association *asoc = NULL;
  4651. struct sctp_sock *sp = sctp_sk(sk);
  4652. if (len >= sizeof(struct sctp_sack_info)) {
  4653. len = sizeof(struct sctp_sack_info);
  4654. if (copy_from_user(&params, optval, len))
  4655. return -EFAULT;
  4656. } else if (len == sizeof(struct sctp_assoc_value)) {
  4657. pr_warn_ratelimited(DEPRECATED
  4658. "%s (pid %d) "
  4659. "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
  4660. "Use struct sctp_sack_info instead\n",
  4661. current->comm, task_pid_nr(current));
  4662. if (copy_from_user(&params, optval, len))
  4663. return -EFAULT;
  4664. } else
  4665. return -EINVAL;
  4666. /* Get association, if sack_assoc_id != 0 and the socket is a one
  4667. * to many style socket, and an association was not found, then
  4668. * the id was invalid.
  4669. */
  4670. asoc = sctp_id2assoc(sk, params.sack_assoc_id);
  4671. if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
  4672. return -EINVAL;
  4673. if (asoc) {
  4674. /* Fetch association values. */
  4675. if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
  4676. params.sack_delay = jiffies_to_msecs(
  4677. asoc->sackdelay);
  4678. params.sack_freq = asoc->sackfreq;
  4679. } else {
  4680. params.sack_delay = 0;
  4681. params.sack_freq = 1;
  4682. }
  4683. } else {
  4684. /* Fetch socket values. */
  4685. if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
  4686. params.sack_delay = sp->sackdelay;
  4687. params.sack_freq = sp->sackfreq;
  4688. } else {
  4689. params.sack_delay = 0;
  4690. params.sack_freq = 1;
  4691. }
  4692. }
  4693. if (copy_to_user(optval, &params, len))
  4694. return -EFAULT;
  4695. if (put_user(len, optlen))
  4696. return -EFAULT;
  4697. return 0;
  4698. }
  4699. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  4700. *
  4701. * Applications can specify protocol parameters for the default association
  4702. * initialization. The option name argument to setsockopt() and getsockopt()
  4703. * is SCTP_INITMSG.
  4704. *
  4705. * Setting initialization parameters is effective only on an unconnected
  4706. * socket (for UDP-style sockets only future associations are effected
  4707. * by the change). With TCP-style sockets, this option is inherited by
  4708. * sockets derived from a listener socket.
  4709. */
  4710. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4711. {
  4712. if (len < sizeof(struct sctp_initmsg))
  4713. return -EINVAL;
  4714. len = sizeof(struct sctp_initmsg);
  4715. if (put_user(len, optlen))
  4716. return -EFAULT;
  4717. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  4718. return -EFAULT;
  4719. return 0;
  4720. }
  4721. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  4722. char __user *optval, int __user *optlen)
  4723. {
  4724. struct sctp_association *asoc;
  4725. int cnt = 0;
  4726. struct sctp_getaddrs getaddrs;
  4727. struct sctp_transport *from;
  4728. void __user *to;
  4729. union sctp_addr temp;
  4730. struct sctp_sock *sp = sctp_sk(sk);
  4731. int addrlen;
  4732. size_t space_left;
  4733. int bytes_copied;
  4734. if (len < sizeof(struct sctp_getaddrs))
  4735. return -EINVAL;
  4736. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  4737. return -EFAULT;
  4738. /* For UDP-style sockets, id specifies the association to query. */
  4739. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  4740. if (!asoc)
  4741. return -EINVAL;
  4742. to = optval + offsetof(struct sctp_getaddrs, addrs);
  4743. space_left = len - offsetof(struct sctp_getaddrs, addrs);
  4744. list_for_each_entry(from, &asoc->peer.transport_addr_list,
  4745. transports) {
  4746. memcpy(&temp, &from->ipaddr, sizeof(temp));
  4747. addrlen = sctp_get_pf_specific(sk->sk_family)
  4748. ->addr_to_user(sp, &temp);
  4749. if (space_left < addrlen)
  4750. return -ENOMEM;
  4751. if (copy_to_user(to, &temp, addrlen))
  4752. return -EFAULT;
  4753. to += addrlen;
  4754. cnt++;
  4755. space_left -= addrlen;
  4756. }
  4757. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  4758. return -EFAULT;
  4759. bytes_copied = ((char __user *)to) - optval;
  4760. if (put_user(bytes_copied, optlen))
  4761. return -EFAULT;
  4762. return 0;
  4763. }
  4764. static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
  4765. size_t space_left, int *bytes_copied)
  4766. {
  4767. struct sctp_sockaddr_entry *addr;
  4768. union sctp_addr temp;
  4769. int cnt = 0;
  4770. int addrlen;
  4771. struct net *net = sock_net(sk);
  4772. rcu_read_lock();
  4773. list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
  4774. if (!addr->valid)
  4775. continue;
  4776. if ((PF_INET == sk->sk_family) &&
  4777. (AF_INET6 == addr->a.sa.sa_family))
  4778. continue;
  4779. if ((PF_INET6 == sk->sk_family) &&
  4780. inet_v6_ipv6only(sk) &&
  4781. (AF_INET == addr->a.sa.sa_family))
  4782. continue;
  4783. memcpy(&temp, &addr->a, sizeof(temp));
  4784. if (!temp.v4.sin_port)
  4785. temp.v4.sin_port = htons(port);
  4786. addrlen = sctp_get_pf_specific(sk->sk_family)
  4787. ->addr_to_user(sctp_sk(sk), &temp);
  4788. if (space_left < addrlen) {
  4789. cnt = -ENOMEM;
  4790. break;
  4791. }
  4792. memcpy(to, &temp, addrlen);
  4793. to += addrlen;
  4794. cnt++;
  4795. space_left -= addrlen;
  4796. *bytes_copied += addrlen;
  4797. }
  4798. rcu_read_unlock();
  4799. return cnt;
  4800. }
  4801. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  4802. char __user *optval, int __user *optlen)
  4803. {
  4804. struct sctp_bind_addr *bp;
  4805. struct sctp_association *asoc;
  4806. int cnt = 0;
  4807. struct sctp_getaddrs getaddrs;
  4808. struct sctp_sockaddr_entry *addr;
  4809. void __user *to;
  4810. union sctp_addr temp;
  4811. struct sctp_sock *sp = sctp_sk(sk);
  4812. int addrlen;
  4813. int err = 0;
  4814. size_t space_left;
  4815. int bytes_copied = 0;
  4816. void *addrs;
  4817. void *buf;
  4818. if (len < sizeof(struct sctp_getaddrs))
  4819. return -EINVAL;
  4820. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  4821. return -EFAULT;
  4822. /*
  4823. * For UDP-style sockets, id specifies the association to query.
  4824. * If the id field is set to the value '0' then the locally bound
  4825. * addresses are returned without regard to any particular
  4826. * association.
  4827. */
  4828. if (0 == getaddrs.assoc_id) {
  4829. bp = &sctp_sk(sk)->ep->base.bind_addr;
  4830. } else {
  4831. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  4832. if (!asoc)
  4833. return -EINVAL;
  4834. bp = &asoc->base.bind_addr;
  4835. }
  4836. to = optval + offsetof(struct sctp_getaddrs, addrs);
  4837. space_left = len - offsetof(struct sctp_getaddrs, addrs);
  4838. addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
  4839. if (!addrs)
  4840. return -ENOMEM;
  4841. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  4842. * addresses from the global local address list.
  4843. */
  4844. if (sctp_list_single_entry(&bp->address_list)) {
  4845. addr = list_entry(bp->address_list.next,
  4846. struct sctp_sockaddr_entry, list);
  4847. if (sctp_is_any(sk, &addr->a)) {
  4848. cnt = sctp_copy_laddrs(sk, bp->port, addrs,
  4849. space_left, &bytes_copied);
  4850. if (cnt < 0) {
  4851. err = cnt;
  4852. goto out;
  4853. }
  4854. goto copy_getaddrs;
  4855. }
  4856. }
  4857. buf = addrs;
  4858. /* Protection on the bound address list is not needed since
  4859. * in the socket option context we hold a socket lock and
  4860. * thus the bound address list can't change.
  4861. */
  4862. list_for_each_entry(addr, &bp->address_list, list) {
  4863. memcpy(&temp, &addr->a, sizeof(temp));
  4864. addrlen = sctp_get_pf_specific(sk->sk_family)
  4865. ->addr_to_user(sp, &temp);
  4866. if (space_left < addrlen) {
  4867. err = -ENOMEM; /*fixme: right error?*/
  4868. goto out;
  4869. }
  4870. memcpy(buf, &temp, addrlen);
  4871. buf += addrlen;
  4872. bytes_copied += addrlen;
  4873. cnt++;
  4874. space_left -= addrlen;
  4875. }
  4876. copy_getaddrs:
  4877. if (copy_to_user(to, addrs, bytes_copied)) {
  4878. err = -EFAULT;
  4879. goto out;
  4880. }
  4881. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
  4882. err = -EFAULT;
  4883. goto out;
  4884. }
  4885. if (put_user(bytes_copied, optlen))
  4886. err = -EFAULT;
  4887. out:
  4888. kfree(addrs);
  4889. return err;
  4890. }
  4891. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  4892. *
  4893. * Requests that the local SCTP stack use the enclosed peer address as
  4894. * the association primary. The enclosed address must be one of the
  4895. * association peer's addresses.
  4896. */
  4897. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  4898. char __user *optval, int __user *optlen)
  4899. {
  4900. struct sctp_prim prim;
  4901. struct sctp_association *asoc;
  4902. struct sctp_sock *sp = sctp_sk(sk);
  4903. if (len < sizeof(struct sctp_prim))
  4904. return -EINVAL;
  4905. len = sizeof(struct sctp_prim);
  4906. if (copy_from_user(&prim, optval, len))
  4907. return -EFAULT;
  4908. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  4909. if (!asoc)
  4910. return -EINVAL;
  4911. if (!asoc->peer.primary_path)
  4912. return -ENOTCONN;
  4913. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  4914. asoc->peer.primary_path->af_specific->sockaddr_len);
  4915. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
  4916. (union sctp_addr *)&prim.ssp_addr);
  4917. if (put_user(len, optlen))
  4918. return -EFAULT;
  4919. if (copy_to_user(optval, &prim, len))
  4920. return -EFAULT;
  4921. return 0;
  4922. }
  4923. /*
  4924. * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
  4925. *
  4926. * Requests that the local endpoint set the specified Adaptation Layer
  4927. * Indication parameter for all future INIT and INIT-ACK exchanges.
  4928. */
  4929. static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
  4930. char __user *optval, int __user *optlen)
  4931. {
  4932. struct sctp_setadaptation adaptation;
  4933. if (len < sizeof(struct sctp_setadaptation))
  4934. return -EINVAL;
  4935. len = sizeof(struct sctp_setadaptation);
  4936. adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
  4937. if (put_user(len, optlen))
  4938. return -EFAULT;
  4939. if (copy_to_user(optval, &adaptation, len))
  4940. return -EFAULT;
  4941. return 0;
  4942. }
  4943. /*
  4944. *
  4945. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  4946. *
  4947. * Applications that wish to use the sendto() system call may wish to
  4948. * specify a default set of parameters that would normally be supplied
  4949. * through the inclusion of ancillary data. This socket option allows
  4950. * such an application to set the default sctp_sndrcvinfo structure.
  4951. * The application that wishes to use this socket option simply passes
  4952. * in to this call the sctp_sndrcvinfo structure defined in Section
  4953. * 5.2.2) The input parameters accepted by this call include
  4954. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  4955. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  4956. * to this call if the caller is using the UDP model.
  4957. *
  4958. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  4959. */
  4960. static int sctp_getsockopt_default_send_param(struct sock *sk,
  4961. int len, char __user *optval,
  4962. int __user *optlen)
  4963. {
  4964. struct sctp_sock *sp = sctp_sk(sk);
  4965. struct sctp_association *asoc;
  4966. struct sctp_sndrcvinfo info;
  4967. if (len < sizeof(info))
  4968. return -EINVAL;
  4969. len = sizeof(info);
  4970. if (copy_from_user(&info, optval, len))
  4971. return -EFAULT;
  4972. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  4973. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  4974. return -EINVAL;
  4975. if (asoc) {
  4976. info.sinfo_stream = asoc->default_stream;
  4977. info.sinfo_flags = asoc->default_flags;
  4978. info.sinfo_ppid = asoc->default_ppid;
  4979. info.sinfo_context = asoc->default_context;
  4980. info.sinfo_timetolive = asoc->default_timetolive;
  4981. } else {
  4982. info.sinfo_stream = sp->default_stream;
  4983. info.sinfo_flags = sp->default_flags;
  4984. info.sinfo_ppid = sp->default_ppid;
  4985. info.sinfo_context = sp->default_context;
  4986. info.sinfo_timetolive = sp->default_timetolive;
  4987. }
  4988. if (put_user(len, optlen))
  4989. return -EFAULT;
  4990. if (copy_to_user(optval, &info, len))
  4991. return -EFAULT;
  4992. return 0;
  4993. }
  4994. /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
  4995. * (SCTP_DEFAULT_SNDINFO)
  4996. */
  4997. static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
  4998. char __user *optval,
  4999. int __user *optlen)
  5000. {
  5001. struct sctp_sock *sp = sctp_sk(sk);
  5002. struct sctp_association *asoc;
  5003. struct sctp_sndinfo info;
  5004. if (len < sizeof(info))
  5005. return -EINVAL;
  5006. len = sizeof(info);
  5007. if (copy_from_user(&info, optval, len))
  5008. return -EFAULT;
  5009. asoc = sctp_id2assoc(sk, info.snd_assoc_id);
  5010. if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
  5011. return -EINVAL;
  5012. if (asoc) {
  5013. info.snd_sid = asoc->default_stream;
  5014. info.snd_flags = asoc->default_flags;
  5015. info.snd_ppid = asoc->default_ppid;
  5016. info.snd_context = asoc->default_context;
  5017. } else {
  5018. info.snd_sid = sp->default_stream;
  5019. info.snd_flags = sp->default_flags;
  5020. info.snd_ppid = sp->default_ppid;
  5021. info.snd_context = sp->default_context;
  5022. }
  5023. if (put_user(len, optlen))
  5024. return -EFAULT;
  5025. if (copy_to_user(optval, &info, len))
  5026. return -EFAULT;
  5027. return 0;
  5028. }
  5029. /*
  5030. *
  5031. * 7.1.5 SCTP_NODELAY
  5032. *
  5033. * Turn on/off any Nagle-like algorithm. This means that packets are
  5034. * generally sent as soon as possible and no unnecessary delays are
  5035. * introduced, at the cost of more packets in the network. Expects an
  5036. * integer boolean flag.
  5037. */
  5038. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  5039. char __user *optval, int __user *optlen)
  5040. {
  5041. int val;
  5042. if (len < sizeof(int))
  5043. return -EINVAL;
  5044. len = sizeof(int);
  5045. val = (sctp_sk(sk)->nodelay == 1);
  5046. if (put_user(len, optlen))
  5047. return -EFAULT;
  5048. if (copy_to_user(optval, &val, len))
  5049. return -EFAULT;
  5050. return 0;
  5051. }
  5052. /*
  5053. *
  5054. * 7.1.1 SCTP_RTOINFO
  5055. *
  5056. * The protocol parameters used to initialize and bound retransmission
  5057. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  5058. * and modify these parameters.
  5059. * All parameters are time values, in milliseconds. A value of 0, when
  5060. * modifying the parameters, indicates that the current value should not
  5061. * be changed.
  5062. *
  5063. */
  5064. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  5065. char __user *optval,
  5066. int __user *optlen) {
  5067. struct sctp_rtoinfo rtoinfo;
  5068. struct sctp_association *asoc;
  5069. if (len < sizeof (struct sctp_rtoinfo))
  5070. return -EINVAL;
  5071. len = sizeof(struct sctp_rtoinfo);
  5072. if (copy_from_user(&rtoinfo, optval, len))
  5073. return -EFAULT;
  5074. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  5075. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  5076. return -EINVAL;
  5077. /* Values corresponding to the specific association. */
  5078. if (asoc) {
  5079. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  5080. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  5081. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  5082. } else {
  5083. /* Values corresponding to the endpoint. */
  5084. struct sctp_sock *sp = sctp_sk(sk);
  5085. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  5086. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  5087. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  5088. }
  5089. if (put_user(len, optlen))
  5090. return -EFAULT;
  5091. if (copy_to_user(optval, &rtoinfo, len))
  5092. return -EFAULT;
  5093. return 0;
  5094. }
  5095. /*
  5096. *
  5097. * 7.1.2 SCTP_ASSOCINFO
  5098. *
  5099. * This option is used to tune the maximum retransmission attempts
  5100. * of the association.
  5101. * Returns an error if the new association retransmission value is
  5102. * greater than the sum of the retransmission value of the peer.
  5103. * See [SCTP] for more information.
  5104. *
  5105. */
  5106. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  5107. char __user *optval,
  5108. int __user *optlen)
  5109. {
  5110. struct sctp_assocparams assocparams;
  5111. struct sctp_association *asoc;
  5112. struct list_head *pos;
  5113. int cnt = 0;
  5114. if (len < sizeof (struct sctp_assocparams))
  5115. return -EINVAL;
  5116. len = sizeof(struct sctp_assocparams);
  5117. if (copy_from_user(&assocparams, optval, len))
  5118. return -EFAULT;
  5119. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  5120. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  5121. return -EINVAL;
  5122. /* Values correspoinding to the specific association */
  5123. if (asoc) {
  5124. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  5125. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  5126. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  5127. assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
  5128. list_for_each(pos, &asoc->peer.transport_addr_list) {
  5129. cnt++;
  5130. }
  5131. assocparams.sasoc_number_peer_destinations = cnt;
  5132. } else {
  5133. /* Values corresponding to the endpoint */
  5134. struct sctp_sock *sp = sctp_sk(sk);
  5135. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  5136. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  5137. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  5138. assocparams.sasoc_cookie_life =
  5139. sp->assocparams.sasoc_cookie_life;
  5140. assocparams.sasoc_number_peer_destinations =
  5141. sp->assocparams.
  5142. sasoc_number_peer_destinations;
  5143. }
  5144. if (put_user(len, optlen))
  5145. return -EFAULT;
  5146. if (copy_to_user(optval, &assocparams, len))
  5147. return -EFAULT;
  5148. return 0;
  5149. }
  5150. /*
  5151. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  5152. *
  5153. * This socket option is a boolean flag which turns on or off mapped V4
  5154. * addresses. If this option is turned on and the socket is type
  5155. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  5156. * If this option is turned off, then no mapping will be done of V4
  5157. * addresses and a user will receive both PF_INET6 and PF_INET type
  5158. * addresses on the socket.
  5159. */
  5160. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  5161. char __user *optval, int __user *optlen)
  5162. {
  5163. int val;
  5164. struct sctp_sock *sp = sctp_sk(sk);
  5165. if (len < sizeof(int))
  5166. return -EINVAL;
  5167. len = sizeof(int);
  5168. val = sp->v4mapped;
  5169. if (put_user(len, optlen))
  5170. return -EFAULT;
  5171. if (copy_to_user(optval, &val, len))
  5172. return -EFAULT;
  5173. return 0;
  5174. }
  5175. /*
  5176. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  5177. * (chapter and verse is quoted at sctp_setsockopt_context())
  5178. */
  5179. static int sctp_getsockopt_context(struct sock *sk, int len,
  5180. char __user *optval, int __user *optlen)
  5181. {
  5182. struct sctp_assoc_value params;
  5183. struct sctp_sock *sp;
  5184. struct sctp_association *asoc;
  5185. if (len < sizeof(struct sctp_assoc_value))
  5186. return -EINVAL;
  5187. len = sizeof(struct sctp_assoc_value);
  5188. if (copy_from_user(&params, optval, len))
  5189. return -EFAULT;
  5190. sp = sctp_sk(sk);
  5191. if (params.assoc_id != 0) {
  5192. asoc = sctp_id2assoc(sk, params.assoc_id);
  5193. if (!asoc)
  5194. return -EINVAL;
  5195. params.assoc_value = asoc->default_rcv_context;
  5196. } else {
  5197. params.assoc_value = sp->default_rcv_context;
  5198. }
  5199. if (put_user(len, optlen))
  5200. return -EFAULT;
  5201. if (copy_to_user(optval, &params, len))
  5202. return -EFAULT;
  5203. return 0;
  5204. }
  5205. /*
  5206. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  5207. * This option will get or set the maximum size to put in any outgoing
  5208. * SCTP DATA chunk. If a message is larger than this size it will be
  5209. * fragmented by SCTP into the specified size. Note that the underlying
  5210. * SCTP implementation may fragment into smaller sized chunks when the
  5211. * PMTU of the underlying association is smaller than the value set by
  5212. * the user. The default value for this option is '0' which indicates
  5213. * the user is NOT limiting fragmentation and only the PMTU will effect
  5214. * SCTP's choice of DATA chunk size. Note also that values set larger
  5215. * than the maximum size of an IP datagram will effectively let SCTP
  5216. * control fragmentation (i.e. the same as setting this option to 0).
  5217. *
  5218. * The following structure is used to access and modify this parameter:
  5219. *
  5220. * struct sctp_assoc_value {
  5221. * sctp_assoc_t assoc_id;
  5222. * uint32_t assoc_value;
  5223. * };
  5224. *
  5225. * assoc_id: This parameter is ignored for one-to-one style sockets.
  5226. * For one-to-many style sockets this parameter indicates which
  5227. * association the user is performing an action upon. Note that if
  5228. * this field's value is zero then the endpoints default value is
  5229. * changed (effecting future associations only).
  5230. * assoc_value: This parameter specifies the maximum size in bytes.
  5231. */
  5232. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  5233. char __user *optval, int __user *optlen)
  5234. {
  5235. struct sctp_assoc_value params;
  5236. struct sctp_association *asoc;
  5237. if (len == sizeof(int)) {
  5238. pr_warn_ratelimited(DEPRECATED
  5239. "%s (pid %d) "
  5240. "Use of int in maxseg socket option.\n"
  5241. "Use struct sctp_assoc_value instead\n",
  5242. current->comm, task_pid_nr(current));
  5243. params.assoc_id = 0;
  5244. } else if (len >= sizeof(struct sctp_assoc_value)) {
  5245. len = sizeof(struct sctp_assoc_value);
  5246. if (copy_from_user(&params, optval, sizeof(params)))
  5247. return -EFAULT;
  5248. } else
  5249. return -EINVAL;
  5250. asoc = sctp_id2assoc(sk, params.assoc_id);
  5251. if (!asoc && params.assoc_id && sctp_style(sk, UDP))
  5252. return -EINVAL;
  5253. if (asoc)
  5254. params.assoc_value = asoc->frag_point;
  5255. else
  5256. params.assoc_value = sctp_sk(sk)->user_frag;
  5257. if (put_user(len, optlen))
  5258. return -EFAULT;
  5259. if (len == sizeof(int)) {
  5260. if (copy_to_user(optval, &params.assoc_value, len))
  5261. return -EFAULT;
  5262. } else {
  5263. if (copy_to_user(optval, &params, len))
  5264. return -EFAULT;
  5265. }
  5266. return 0;
  5267. }
  5268. /*
  5269. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  5270. * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
  5271. */
  5272. static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
  5273. char __user *optval, int __user *optlen)
  5274. {
  5275. int val;
  5276. if (len < sizeof(int))
  5277. return -EINVAL;
  5278. len = sizeof(int);
  5279. val = sctp_sk(sk)->frag_interleave;
  5280. if (put_user(len, optlen))
  5281. return -EFAULT;
  5282. if (copy_to_user(optval, &val, len))
  5283. return -EFAULT;
  5284. return 0;
  5285. }
  5286. /*
  5287. * 7.1.25. Set or Get the sctp partial delivery point
  5288. * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
  5289. */
  5290. static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
  5291. char __user *optval,
  5292. int __user *optlen)
  5293. {
  5294. u32 val;
  5295. if (len < sizeof(u32))
  5296. return -EINVAL;
  5297. len = sizeof(u32);
  5298. val = sctp_sk(sk)->pd_point;
  5299. if (put_user(len, optlen))
  5300. return -EFAULT;
  5301. if (copy_to_user(optval, &val, len))
  5302. return -EFAULT;
  5303. return 0;
  5304. }
  5305. /*
  5306. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  5307. * (chapter and verse is quoted at sctp_setsockopt_maxburst())
  5308. */
  5309. static int sctp_getsockopt_maxburst(struct sock *sk, int len,
  5310. char __user *optval,
  5311. int __user *optlen)
  5312. {
  5313. struct sctp_assoc_value params;
  5314. struct sctp_sock *sp;
  5315. struct sctp_association *asoc;
  5316. if (len == sizeof(int)) {
  5317. pr_warn_ratelimited(DEPRECATED
  5318. "%s (pid %d) "
  5319. "Use of int in max_burst socket option.\n"
  5320. "Use struct sctp_assoc_value instead\n",
  5321. current->comm, task_pid_nr(current));
  5322. params.assoc_id = 0;
  5323. } else if (len >= sizeof(struct sctp_assoc_value)) {
  5324. len = sizeof(struct sctp_assoc_value);
  5325. if (copy_from_user(&params, optval, len))
  5326. return -EFAULT;
  5327. } else
  5328. return -EINVAL;
  5329. sp = sctp_sk(sk);
  5330. if (params.assoc_id != 0) {
  5331. asoc = sctp_id2assoc(sk, params.assoc_id);
  5332. if (!asoc)
  5333. return -EINVAL;
  5334. params.assoc_value = asoc->max_burst;
  5335. } else
  5336. params.assoc_value = sp->max_burst;
  5337. if (len == sizeof(int)) {
  5338. if (copy_to_user(optval, &params.assoc_value, len))
  5339. return -EFAULT;
  5340. } else {
  5341. if (copy_to_user(optval, &params, len))
  5342. return -EFAULT;
  5343. }
  5344. return 0;
  5345. }
  5346. static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
  5347. char __user *optval, int __user *optlen)
  5348. {
  5349. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5350. struct sctp_hmacalgo __user *p = (void __user *)optval;
  5351. struct sctp_hmac_algo_param *hmacs;
  5352. __u16 data_len = 0;
  5353. u32 num_idents;
  5354. int i;
  5355. if (!ep->auth_enable)
  5356. return -EACCES;
  5357. hmacs = ep->auth_hmacs_list;
  5358. data_len = ntohs(hmacs->param_hdr.length) -
  5359. sizeof(struct sctp_paramhdr);
  5360. if (len < sizeof(struct sctp_hmacalgo) + data_len)
  5361. return -EINVAL;
  5362. len = sizeof(struct sctp_hmacalgo) + data_len;
  5363. num_idents = data_len / sizeof(u16);
  5364. if (put_user(len, optlen))
  5365. return -EFAULT;
  5366. if (put_user(num_idents, &p->shmac_num_idents))
  5367. return -EFAULT;
  5368. for (i = 0; i < num_idents; i++) {
  5369. __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
  5370. if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
  5371. return -EFAULT;
  5372. }
  5373. return 0;
  5374. }
  5375. static int sctp_getsockopt_active_key(struct sock *sk, int len,
  5376. char __user *optval, int __user *optlen)
  5377. {
  5378. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5379. struct sctp_authkeyid val;
  5380. struct sctp_association *asoc;
  5381. if (!ep->auth_enable)
  5382. return -EACCES;
  5383. if (len < sizeof(struct sctp_authkeyid))
  5384. return -EINVAL;
  5385. if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid)))
  5386. return -EFAULT;
  5387. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  5388. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  5389. return -EINVAL;
  5390. if (asoc)
  5391. val.scact_keynumber = asoc->active_key_id;
  5392. else
  5393. val.scact_keynumber = ep->active_key_id;
  5394. len = sizeof(struct sctp_authkeyid);
  5395. if (put_user(len, optlen))
  5396. return -EFAULT;
  5397. if (copy_to_user(optval, &val, len))
  5398. return -EFAULT;
  5399. return 0;
  5400. }
  5401. static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
  5402. char __user *optval, int __user *optlen)
  5403. {
  5404. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5405. struct sctp_authchunks __user *p = (void __user *)optval;
  5406. struct sctp_authchunks val;
  5407. struct sctp_association *asoc;
  5408. struct sctp_chunks_param *ch;
  5409. u32 num_chunks = 0;
  5410. char __user *to;
  5411. if (!ep->auth_enable)
  5412. return -EACCES;
  5413. if (len < sizeof(struct sctp_authchunks))
  5414. return -EINVAL;
  5415. if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
  5416. return -EFAULT;
  5417. to = p->gauth_chunks;
  5418. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  5419. if (!asoc)
  5420. return -EINVAL;
  5421. ch = asoc->peer.peer_chunks;
  5422. if (!ch)
  5423. goto num;
  5424. /* See if the user provided enough room for all the data */
  5425. num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
  5426. if (len < num_chunks)
  5427. return -EINVAL;
  5428. if (copy_to_user(to, ch->chunks, num_chunks))
  5429. return -EFAULT;
  5430. num:
  5431. len = sizeof(struct sctp_authchunks) + num_chunks;
  5432. if (put_user(len, optlen))
  5433. return -EFAULT;
  5434. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  5435. return -EFAULT;
  5436. return 0;
  5437. }
  5438. static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
  5439. char __user *optval, int __user *optlen)
  5440. {
  5441. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5442. struct sctp_authchunks __user *p = (void __user *)optval;
  5443. struct sctp_authchunks val;
  5444. struct sctp_association *asoc;
  5445. struct sctp_chunks_param *ch;
  5446. u32 num_chunks = 0;
  5447. char __user *to;
  5448. if (!ep->auth_enable)
  5449. return -EACCES;
  5450. if (len < sizeof(struct sctp_authchunks))
  5451. return -EINVAL;
  5452. if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks)))
  5453. return -EFAULT;
  5454. to = p->gauth_chunks;
  5455. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  5456. if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
  5457. return -EINVAL;
  5458. if (asoc)
  5459. ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
  5460. else
  5461. ch = ep->auth_chunk_list;
  5462. if (!ch)
  5463. goto num;
  5464. num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
  5465. if (len < sizeof(struct sctp_authchunks) + num_chunks)
  5466. return -EINVAL;
  5467. if (copy_to_user(to, ch->chunks, num_chunks))
  5468. return -EFAULT;
  5469. num:
  5470. len = sizeof(struct sctp_authchunks) + num_chunks;
  5471. if (put_user(len, optlen))
  5472. return -EFAULT;
  5473. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  5474. return -EFAULT;
  5475. return 0;
  5476. }
  5477. /*
  5478. * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
  5479. * This option gets the current number of associations that are attached
  5480. * to a one-to-many style socket. The option value is an uint32_t.
  5481. */
  5482. static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
  5483. char __user *optval, int __user *optlen)
  5484. {
  5485. struct sctp_sock *sp = sctp_sk(sk);
  5486. struct sctp_association *asoc;
  5487. u32 val = 0;
  5488. if (sctp_style(sk, TCP))
  5489. return -EOPNOTSUPP;
  5490. if (len < sizeof(u32))
  5491. return -EINVAL;
  5492. len = sizeof(u32);
  5493. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  5494. val++;
  5495. }
  5496. if (put_user(len, optlen))
  5497. return -EFAULT;
  5498. if (copy_to_user(optval, &val, len))
  5499. return -EFAULT;
  5500. return 0;
  5501. }
  5502. /*
  5503. * 8.1.23 SCTP_AUTO_ASCONF
  5504. * See the corresponding setsockopt entry as description
  5505. */
  5506. static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
  5507. char __user *optval, int __user *optlen)
  5508. {
  5509. int val = 0;
  5510. if (len < sizeof(int))
  5511. return -EINVAL;
  5512. len = sizeof(int);
  5513. if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
  5514. val = 1;
  5515. if (put_user(len, optlen))
  5516. return -EFAULT;
  5517. if (copy_to_user(optval, &val, len))
  5518. return -EFAULT;
  5519. return 0;
  5520. }
  5521. /*
  5522. * 8.2.6. Get the Current Identifiers of Associations
  5523. * (SCTP_GET_ASSOC_ID_LIST)
  5524. *
  5525. * This option gets the current list of SCTP association identifiers of
  5526. * the SCTP associations handled by a one-to-many style socket.
  5527. */
  5528. static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
  5529. char __user *optval, int __user *optlen)
  5530. {
  5531. struct sctp_sock *sp = sctp_sk(sk);
  5532. struct sctp_association *asoc;
  5533. struct sctp_assoc_ids *ids;
  5534. u32 num = 0;
  5535. if (sctp_style(sk, TCP))
  5536. return -EOPNOTSUPP;
  5537. if (len < sizeof(struct sctp_assoc_ids))
  5538. return -EINVAL;
  5539. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  5540. num++;
  5541. }
  5542. if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
  5543. return -EINVAL;
  5544. len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
  5545. ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
  5546. if (unlikely(!ids))
  5547. return -ENOMEM;
  5548. ids->gaids_number_of_ids = num;
  5549. num = 0;
  5550. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  5551. ids->gaids_assoc_id[num++] = asoc->assoc_id;
  5552. }
  5553. if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
  5554. kfree(ids);
  5555. return -EFAULT;
  5556. }
  5557. kfree(ids);
  5558. return 0;
  5559. }
  5560. /*
  5561. * SCTP_PEER_ADDR_THLDS
  5562. *
  5563. * This option allows us to fetch the partially failed threshold for one or all
  5564. * transports in an association. See Section 6.1 of:
  5565. * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
  5566. */
  5567. static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
  5568. char __user *optval,
  5569. int len,
  5570. int __user *optlen)
  5571. {
  5572. struct sctp_paddrthlds val;
  5573. struct sctp_transport *trans;
  5574. struct sctp_association *asoc;
  5575. if (len < sizeof(struct sctp_paddrthlds))
  5576. return -EINVAL;
  5577. len = sizeof(struct sctp_paddrthlds);
  5578. if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
  5579. return -EFAULT;
  5580. if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
  5581. asoc = sctp_id2assoc(sk, val.spt_assoc_id);
  5582. if (!asoc)
  5583. return -ENOENT;
  5584. val.spt_pathpfthld = asoc->pf_retrans;
  5585. val.spt_pathmaxrxt = asoc->pathmaxrxt;
  5586. } else {
  5587. trans = sctp_addr_id2transport(sk, &val.spt_address,
  5588. val.spt_assoc_id);
  5589. if (!trans)
  5590. return -ENOENT;
  5591. val.spt_pathmaxrxt = trans->pathmaxrxt;
  5592. val.spt_pathpfthld = trans->pf_retrans;
  5593. }
  5594. if (put_user(len, optlen) || copy_to_user(optval, &val, len))
  5595. return -EFAULT;
  5596. return 0;
  5597. }
  5598. /*
  5599. * SCTP_GET_ASSOC_STATS
  5600. *
  5601. * This option retrieves local per endpoint statistics. It is modeled
  5602. * after OpenSolaris' implementation
  5603. */
  5604. static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
  5605. char __user *optval,
  5606. int __user *optlen)
  5607. {
  5608. struct sctp_assoc_stats sas;
  5609. struct sctp_association *asoc = NULL;
  5610. /* User must provide at least the assoc id */
  5611. if (len < sizeof(sctp_assoc_t))
  5612. return -EINVAL;
  5613. /* Allow the struct to grow and fill in as much as possible */
  5614. len = min_t(size_t, len, sizeof(sas));
  5615. if (copy_from_user(&sas, optval, len))
  5616. return -EFAULT;
  5617. asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
  5618. if (!asoc)
  5619. return -EINVAL;
  5620. sas.sas_rtxchunks = asoc->stats.rtxchunks;
  5621. sas.sas_gapcnt = asoc->stats.gapcnt;
  5622. sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
  5623. sas.sas_osacks = asoc->stats.osacks;
  5624. sas.sas_isacks = asoc->stats.isacks;
  5625. sas.sas_octrlchunks = asoc->stats.octrlchunks;
  5626. sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
  5627. sas.sas_oodchunks = asoc->stats.oodchunks;
  5628. sas.sas_iodchunks = asoc->stats.iodchunks;
  5629. sas.sas_ouodchunks = asoc->stats.ouodchunks;
  5630. sas.sas_iuodchunks = asoc->stats.iuodchunks;
  5631. sas.sas_idupchunks = asoc->stats.idupchunks;
  5632. sas.sas_opackets = asoc->stats.opackets;
  5633. sas.sas_ipackets = asoc->stats.ipackets;
  5634. /* New high max rto observed, will return 0 if not a single
  5635. * RTO update took place. obs_rto_ipaddr will be bogus
  5636. * in such a case
  5637. */
  5638. sas.sas_maxrto = asoc->stats.max_obs_rto;
  5639. memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
  5640. sizeof(struct sockaddr_storage));
  5641. /* Mark beginning of a new observation period */
  5642. asoc->stats.max_obs_rto = asoc->rto_min;
  5643. if (put_user(len, optlen))
  5644. return -EFAULT;
  5645. pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
  5646. if (copy_to_user(optval, &sas, len))
  5647. return -EFAULT;
  5648. return 0;
  5649. }
  5650. static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
  5651. char __user *optval,
  5652. int __user *optlen)
  5653. {
  5654. int val = 0;
  5655. if (len < sizeof(int))
  5656. return -EINVAL;
  5657. len = sizeof(int);
  5658. if (sctp_sk(sk)->recvrcvinfo)
  5659. val = 1;
  5660. if (put_user(len, optlen))
  5661. return -EFAULT;
  5662. if (copy_to_user(optval, &val, len))
  5663. return -EFAULT;
  5664. return 0;
  5665. }
  5666. static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
  5667. char __user *optval,
  5668. int __user *optlen)
  5669. {
  5670. int val = 0;
  5671. if (len < sizeof(int))
  5672. return -EINVAL;
  5673. len = sizeof(int);
  5674. if (sctp_sk(sk)->recvnxtinfo)
  5675. val = 1;
  5676. if (put_user(len, optlen))
  5677. return -EFAULT;
  5678. if (copy_to_user(optval, &val, len))
  5679. return -EFAULT;
  5680. return 0;
  5681. }
  5682. static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
  5683. char __user *optval,
  5684. int __user *optlen)
  5685. {
  5686. struct sctp_assoc_value params;
  5687. struct sctp_association *asoc;
  5688. int retval = -EFAULT;
  5689. if (len < sizeof(params)) {
  5690. retval = -EINVAL;
  5691. goto out;
  5692. }
  5693. len = sizeof(params);
  5694. if (copy_from_user(&params, optval, len))
  5695. goto out;
  5696. asoc = sctp_id2assoc(sk, params.assoc_id);
  5697. if (asoc) {
  5698. params.assoc_value = asoc->prsctp_enable;
  5699. } else if (!params.assoc_id) {
  5700. struct sctp_sock *sp = sctp_sk(sk);
  5701. params.assoc_value = sp->ep->prsctp_enable;
  5702. } else {
  5703. retval = -EINVAL;
  5704. goto out;
  5705. }
  5706. if (put_user(len, optlen))
  5707. goto out;
  5708. if (copy_to_user(optval, &params, len))
  5709. goto out;
  5710. retval = 0;
  5711. out:
  5712. return retval;
  5713. }
  5714. static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
  5715. char __user *optval,
  5716. int __user *optlen)
  5717. {
  5718. struct sctp_default_prinfo info;
  5719. struct sctp_association *asoc;
  5720. int retval = -EFAULT;
  5721. if (len < sizeof(info)) {
  5722. retval = -EINVAL;
  5723. goto out;
  5724. }
  5725. len = sizeof(info);
  5726. if (copy_from_user(&info, optval, len))
  5727. goto out;
  5728. asoc = sctp_id2assoc(sk, info.pr_assoc_id);
  5729. if (asoc) {
  5730. info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
  5731. info.pr_value = asoc->default_timetolive;
  5732. } else if (!info.pr_assoc_id) {
  5733. struct sctp_sock *sp = sctp_sk(sk);
  5734. info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
  5735. info.pr_value = sp->default_timetolive;
  5736. } else {
  5737. retval = -EINVAL;
  5738. goto out;
  5739. }
  5740. if (put_user(len, optlen))
  5741. goto out;
  5742. if (copy_to_user(optval, &info, len))
  5743. goto out;
  5744. retval = 0;
  5745. out:
  5746. return retval;
  5747. }
  5748. static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
  5749. char __user *optval,
  5750. int __user *optlen)
  5751. {
  5752. struct sctp_prstatus params;
  5753. struct sctp_association *asoc;
  5754. int policy;
  5755. int retval = -EINVAL;
  5756. if (len < sizeof(params))
  5757. goto out;
  5758. len = sizeof(params);
  5759. if (copy_from_user(&params, optval, len)) {
  5760. retval = -EFAULT;
  5761. goto out;
  5762. }
  5763. policy = params.sprstat_policy;
  5764. if (policy & ~SCTP_PR_SCTP_MASK)
  5765. goto out;
  5766. asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
  5767. if (!asoc)
  5768. goto out;
  5769. if (policy == SCTP_PR_SCTP_NONE) {
  5770. params.sprstat_abandoned_unsent = 0;
  5771. params.sprstat_abandoned_sent = 0;
  5772. for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
  5773. params.sprstat_abandoned_unsent +=
  5774. asoc->abandoned_unsent[policy];
  5775. params.sprstat_abandoned_sent +=
  5776. asoc->abandoned_sent[policy];
  5777. }
  5778. } else {
  5779. params.sprstat_abandoned_unsent =
  5780. asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
  5781. params.sprstat_abandoned_sent =
  5782. asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
  5783. }
  5784. if (put_user(len, optlen)) {
  5785. retval = -EFAULT;
  5786. goto out;
  5787. }
  5788. if (copy_to_user(optval, &params, len)) {
  5789. retval = -EFAULT;
  5790. goto out;
  5791. }
  5792. retval = 0;
  5793. out:
  5794. return retval;
  5795. }
  5796. static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
  5797. char __user *optval,
  5798. int __user *optlen)
  5799. {
  5800. struct sctp_stream_out_ext *streamoute;
  5801. struct sctp_association *asoc;
  5802. struct sctp_prstatus params;
  5803. int retval = -EINVAL;
  5804. int policy;
  5805. if (len < sizeof(params))
  5806. goto out;
  5807. len = sizeof(params);
  5808. if (copy_from_user(&params, optval, len)) {
  5809. retval = -EFAULT;
  5810. goto out;
  5811. }
  5812. policy = params.sprstat_policy;
  5813. if (policy & ~SCTP_PR_SCTP_MASK)
  5814. goto out;
  5815. asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
  5816. if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
  5817. goto out;
  5818. streamoute = asoc->stream.out[params.sprstat_sid].ext;
  5819. if (!streamoute) {
  5820. /* Not allocated yet, means all stats are 0 */
  5821. params.sprstat_abandoned_unsent = 0;
  5822. params.sprstat_abandoned_sent = 0;
  5823. retval = 0;
  5824. goto out;
  5825. }
  5826. if (policy == SCTP_PR_SCTP_NONE) {
  5827. params.sprstat_abandoned_unsent = 0;
  5828. params.sprstat_abandoned_sent = 0;
  5829. for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
  5830. params.sprstat_abandoned_unsent +=
  5831. streamoute->abandoned_unsent[policy];
  5832. params.sprstat_abandoned_sent +=
  5833. streamoute->abandoned_sent[policy];
  5834. }
  5835. } else {
  5836. params.sprstat_abandoned_unsent =
  5837. streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
  5838. params.sprstat_abandoned_sent =
  5839. streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
  5840. }
  5841. if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
  5842. retval = -EFAULT;
  5843. goto out;
  5844. }
  5845. retval = 0;
  5846. out:
  5847. return retval;
  5848. }
  5849. static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
  5850. char __user *optval,
  5851. int __user *optlen)
  5852. {
  5853. struct sctp_assoc_value params;
  5854. struct sctp_association *asoc;
  5855. int retval = -EFAULT;
  5856. if (len < sizeof(params)) {
  5857. retval = -EINVAL;
  5858. goto out;
  5859. }
  5860. len = sizeof(params);
  5861. if (copy_from_user(&params, optval, len))
  5862. goto out;
  5863. asoc = sctp_id2assoc(sk, params.assoc_id);
  5864. if (asoc) {
  5865. params.assoc_value = asoc->reconf_enable;
  5866. } else if (!params.assoc_id) {
  5867. struct sctp_sock *sp = sctp_sk(sk);
  5868. params.assoc_value = sp->ep->reconf_enable;
  5869. } else {
  5870. retval = -EINVAL;
  5871. goto out;
  5872. }
  5873. if (put_user(len, optlen))
  5874. goto out;
  5875. if (copy_to_user(optval, &params, len))
  5876. goto out;
  5877. retval = 0;
  5878. out:
  5879. return retval;
  5880. }
  5881. static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
  5882. char __user *optval,
  5883. int __user *optlen)
  5884. {
  5885. struct sctp_assoc_value params;
  5886. struct sctp_association *asoc;
  5887. int retval = -EFAULT;
  5888. if (len < sizeof(params)) {
  5889. retval = -EINVAL;
  5890. goto out;
  5891. }
  5892. len = sizeof(params);
  5893. if (copy_from_user(&params, optval, len))
  5894. goto out;
  5895. asoc = sctp_id2assoc(sk, params.assoc_id);
  5896. if (asoc) {
  5897. params.assoc_value = asoc->strreset_enable;
  5898. } else if (!params.assoc_id) {
  5899. struct sctp_sock *sp = sctp_sk(sk);
  5900. params.assoc_value = sp->ep->strreset_enable;
  5901. } else {
  5902. retval = -EINVAL;
  5903. goto out;
  5904. }
  5905. if (put_user(len, optlen))
  5906. goto out;
  5907. if (copy_to_user(optval, &params, len))
  5908. goto out;
  5909. retval = 0;
  5910. out:
  5911. return retval;
  5912. }
  5913. static int sctp_getsockopt_scheduler(struct sock *sk, int len,
  5914. char __user *optval,
  5915. int __user *optlen)
  5916. {
  5917. struct sctp_assoc_value params;
  5918. struct sctp_association *asoc;
  5919. int retval = -EFAULT;
  5920. if (len < sizeof(params)) {
  5921. retval = -EINVAL;
  5922. goto out;
  5923. }
  5924. len = sizeof(params);
  5925. if (copy_from_user(&params, optval, len))
  5926. goto out;
  5927. asoc = sctp_id2assoc(sk, params.assoc_id);
  5928. if (!asoc) {
  5929. retval = -EINVAL;
  5930. goto out;
  5931. }
  5932. params.assoc_value = sctp_sched_get_sched(asoc);
  5933. if (put_user(len, optlen))
  5934. goto out;
  5935. if (copy_to_user(optval, &params, len))
  5936. goto out;
  5937. retval = 0;
  5938. out:
  5939. return retval;
  5940. }
  5941. static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
  5942. char __user *optval,
  5943. int __user *optlen)
  5944. {
  5945. struct sctp_stream_value params;
  5946. struct sctp_association *asoc;
  5947. int retval = -EFAULT;
  5948. if (len < sizeof(params)) {
  5949. retval = -EINVAL;
  5950. goto out;
  5951. }
  5952. len = sizeof(params);
  5953. if (copy_from_user(&params, optval, len))
  5954. goto out;
  5955. asoc = sctp_id2assoc(sk, params.assoc_id);
  5956. if (!asoc) {
  5957. retval = -EINVAL;
  5958. goto out;
  5959. }
  5960. retval = sctp_sched_get_value(asoc, params.stream_id,
  5961. &params.stream_value);
  5962. if (retval)
  5963. goto out;
  5964. if (put_user(len, optlen)) {
  5965. retval = -EFAULT;
  5966. goto out;
  5967. }
  5968. if (copy_to_user(optval, &params, len)) {
  5969. retval = -EFAULT;
  5970. goto out;
  5971. }
  5972. out:
  5973. return retval;
  5974. }
  5975. static int sctp_getsockopt(struct sock *sk, int level, int optname,
  5976. char __user *optval, int __user *optlen)
  5977. {
  5978. int retval = 0;
  5979. int len;
  5980. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  5981. /* I can hardly begin to describe how wrong this is. This is
  5982. * so broken as to be worse than useless. The API draft
  5983. * REALLY is NOT helpful here... I am not convinced that the
  5984. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  5985. * are at all well-founded.
  5986. */
  5987. if (level != SOL_SCTP) {
  5988. struct sctp_af *af = sctp_sk(sk)->pf->af;
  5989. retval = af->getsockopt(sk, level, optname, optval, optlen);
  5990. return retval;
  5991. }
  5992. if (get_user(len, optlen))
  5993. return -EFAULT;
  5994. if (len < 0)
  5995. return -EINVAL;
  5996. lock_sock(sk);
  5997. switch (optname) {
  5998. case SCTP_STATUS:
  5999. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  6000. break;
  6001. case SCTP_DISABLE_FRAGMENTS:
  6002. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  6003. optlen);
  6004. break;
  6005. case SCTP_EVENTS:
  6006. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  6007. break;
  6008. case SCTP_AUTOCLOSE:
  6009. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  6010. break;
  6011. case SCTP_SOCKOPT_PEELOFF:
  6012. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  6013. break;
  6014. case SCTP_SOCKOPT_PEELOFF_FLAGS:
  6015. retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
  6016. break;
  6017. case SCTP_PEER_ADDR_PARAMS:
  6018. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  6019. optlen);
  6020. break;
  6021. case SCTP_DELAYED_SACK:
  6022. retval = sctp_getsockopt_delayed_ack(sk, len, optval,
  6023. optlen);
  6024. break;
  6025. case SCTP_INITMSG:
  6026. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  6027. break;
  6028. case SCTP_GET_PEER_ADDRS:
  6029. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  6030. optlen);
  6031. break;
  6032. case SCTP_GET_LOCAL_ADDRS:
  6033. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  6034. optlen);
  6035. break;
  6036. case SCTP_SOCKOPT_CONNECTX3:
  6037. retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
  6038. break;
  6039. case SCTP_DEFAULT_SEND_PARAM:
  6040. retval = sctp_getsockopt_default_send_param(sk, len,
  6041. optval, optlen);
  6042. break;
  6043. case SCTP_DEFAULT_SNDINFO:
  6044. retval = sctp_getsockopt_default_sndinfo(sk, len,
  6045. optval, optlen);
  6046. break;
  6047. case SCTP_PRIMARY_ADDR:
  6048. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  6049. break;
  6050. case SCTP_NODELAY:
  6051. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  6052. break;
  6053. case SCTP_RTOINFO:
  6054. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  6055. break;
  6056. case SCTP_ASSOCINFO:
  6057. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  6058. break;
  6059. case SCTP_I_WANT_MAPPED_V4_ADDR:
  6060. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  6061. break;
  6062. case SCTP_MAXSEG:
  6063. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  6064. break;
  6065. case SCTP_GET_PEER_ADDR_INFO:
  6066. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  6067. optlen);
  6068. break;
  6069. case SCTP_ADAPTATION_LAYER:
  6070. retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
  6071. optlen);
  6072. break;
  6073. case SCTP_CONTEXT:
  6074. retval = sctp_getsockopt_context(sk, len, optval, optlen);
  6075. break;
  6076. case SCTP_FRAGMENT_INTERLEAVE:
  6077. retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
  6078. optlen);
  6079. break;
  6080. case SCTP_PARTIAL_DELIVERY_POINT:
  6081. retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
  6082. optlen);
  6083. break;
  6084. case SCTP_MAX_BURST:
  6085. retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
  6086. break;
  6087. case SCTP_AUTH_KEY:
  6088. case SCTP_AUTH_CHUNK:
  6089. case SCTP_AUTH_DELETE_KEY:
  6090. retval = -EOPNOTSUPP;
  6091. break;
  6092. case SCTP_HMAC_IDENT:
  6093. retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
  6094. break;
  6095. case SCTP_AUTH_ACTIVE_KEY:
  6096. retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
  6097. break;
  6098. case SCTP_PEER_AUTH_CHUNKS:
  6099. retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
  6100. optlen);
  6101. break;
  6102. case SCTP_LOCAL_AUTH_CHUNKS:
  6103. retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
  6104. optlen);
  6105. break;
  6106. case SCTP_GET_ASSOC_NUMBER:
  6107. retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
  6108. break;
  6109. case SCTP_GET_ASSOC_ID_LIST:
  6110. retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
  6111. break;
  6112. case SCTP_AUTO_ASCONF:
  6113. retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
  6114. break;
  6115. case SCTP_PEER_ADDR_THLDS:
  6116. retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
  6117. break;
  6118. case SCTP_GET_ASSOC_STATS:
  6119. retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
  6120. break;
  6121. case SCTP_RECVRCVINFO:
  6122. retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
  6123. break;
  6124. case SCTP_RECVNXTINFO:
  6125. retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
  6126. break;
  6127. case SCTP_PR_SUPPORTED:
  6128. retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
  6129. break;
  6130. case SCTP_DEFAULT_PRINFO:
  6131. retval = sctp_getsockopt_default_prinfo(sk, len, optval,
  6132. optlen);
  6133. break;
  6134. case SCTP_PR_ASSOC_STATUS:
  6135. retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
  6136. optlen);
  6137. break;
  6138. case SCTP_PR_STREAM_STATUS:
  6139. retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
  6140. optlen);
  6141. break;
  6142. case SCTP_RECONFIG_SUPPORTED:
  6143. retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
  6144. optlen);
  6145. break;
  6146. case SCTP_ENABLE_STREAM_RESET:
  6147. retval = sctp_getsockopt_enable_strreset(sk, len, optval,
  6148. optlen);
  6149. break;
  6150. case SCTP_STREAM_SCHEDULER:
  6151. retval = sctp_getsockopt_scheduler(sk, len, optval,
  6152. optlen);
  6153. break;
  6154. case SCTP_STREAM_SCHEDULER_VALUE:
  6155. retval = sctp_getsockopt_scheduler_value(sk, len, optval,
  6156. optlen);
  6157. break;
  6158. default:
  6159. retval = -ENOPROTOOPT;
  6160. break;
  6161. }
  6162. release_sock(sk);
  6163. return retval;
  6164. }
  6165. static int sctp_hash(struct sock *sk)
  6166. {
  6167. /* STUB */
  6168. return 0;
  6169. }
  6170. static void sctp_unhash(struct sock *sk)
  6171. {
  6172. /* STUB */
  6173. }
  6174. /* Check if port is acceptable. Possibly find first available port.
  6175. *
  6176. * The port hash table (contained in the 'global' SCTP protocol storage
  6177. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  6178. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  6179. * list (the list number is the port number hashed out, so as you
  6180. * would expect from a hash function, all the ports in a given list have
  6181. * such a number that hashes out to the same list number; you were
  6182. * expecting that, right?); so each list has a set of ports, with a
  6183. * link to the socket (struct sock) that uses it, the port number and
  6184. * a fastreuse flag (FIXME: NPI ipg).
  6185. */
  6186. static struct sctp_bind_bucket *sctp_bucket_create(
  6187. struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
  6188. static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  6189. {
  6190. struct sctp_bind_hashbucket *head; /* hash list */
  6191. struct sctp_bind_bucket *pp;
  6192. unsigned short snum;
  6193. int ret;
  6194. snum = ntohs(addr->v4.sin_port);
  6195. pr_debug("%s: begins, snum:%d\n", __func__, snum);
  6196. local_bh_disable();
  6197. if (snum == 0) {
  6198. /* Search for an available port. */
  6199. int low, high, remaining, index;
  6200. unsigned int rover;
  6201. struct net *net = sock_net(sk);
  6202. inet_get_local_port_range(net, &low, &high);
  6203. remaining = (high - low) + 1;
  6204. rover = prandom_u32() % remaining + low;
  6205. do {
  6206. rover++;
  6207. if ((rover < low) || (rover > high))
  6208. rover = low;
  6209. if (inet_is_local_reserved_port(net, rover))
  6210. continue;
  6211. index = sctp_phashfn(sock_net(sk), rover);
  6212. head = &sctp_port_hashtable[index];
  6213. spin_lock(&head->lock);
  6214. sctp_for_each_hentry(pp, &head->chain)
  6215. if ((pp->port == rover) &&
  6216. net_eq(sock_net(sk), pp->net))
  6217. goto next;
  6218. break;
  6219. next:
  6220. spin_unlock(&head->lock);
  6221. } while (--remaining > 0);
  6222. /* Exhausted local port range during search? */
  6223. ret = 1;
  6224. if (remaining <= 0)
  6225. goto fail;
  6226. /* OK, here is the one we will use. HEAD (the port
  6227. * hash table list entry) is non-NULL and we hold it's
  6228. * mutex.
  6229. */
  6230. snum = rover;
  6231. } else {
  6232. /* We are given an specific port number; we verify
  6233. * that it is not being used. If it is used, we will
  6234. * exahust the search in the hash list corresponding
  6235. * to the port number (snum) - we detect that with the
  6236. * port iterator, pp being NULL.
  6237. */
  6238. head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
  6239. spin_lock(&head->lock);
  6240. sctp_for_each_hentry(pp, &head->chain) {
  6241. if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
  6242. goto pp_found;
  6243. }
  6244. }
  6245. pp = NULL;
  6246. goto pp_not_found;
  6247. pp_found:
  6248. if (!hlist_empty(&pp->owner)) {
  6249. /* We had a port hash table hit - there is an
  6250. * available port (pp != NULL) and it is being
  6251. * used by other socket (pp->owner not empty); that other
  6252. * socket is going to be sk2.
  6253. */
  6254. int reuse = sk->sk_reuse;
  6255. struct sock *sk2;
  6256. pr_debug("%s: found a possible match\n", __func__);
  6257. if (pp->fastreuse && sk->sk_reuse &&
  6258. sk->sk_state != SCTP_SS_LISTENING)
  6259. goto success;
  6260. /* Run through the list of sockets bound to the port
  6261. * (pp->port) [via the pointers bind_next and
  6262. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  6263. * we get the endpoint they describe and run through
  6264. * the endpoint's list of IP (v4 or v6) addresses,
  6265. * comparing each of the addresses with the address of
  6266. * the socket sk. If we find a match, then that means
  6267. * that this port/socket (sk) combination are already
  6268. * in an endpoint.
  6269. */
  6270. sk_for_each_bound(sk2, &pp->owner) {
  6271. struct sctp_endpoint *ep2;
  6272. ep2 = sctp_sk(sk2)->ep;
  6273. if (sk == sk2 ||
  6274. (reuse && sk2->sk_reuse &&
  6275. sk2->sk_state != SCTP_SS_LISTENING))
  6276. continue;
  6277. if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
  6278. sctp_sk(sk2), sctp_sk(sk))) {
  6279. ret = (long)sk2;
  6280. goto fail_unlock;
  6281. }
  6282. }
  6283. pr_debug("%s: found a match\n", __func__);
  6284. }
  6285. pp_not_found:
  6286. /* If there was a hash table miss, create a new port. */
  6287. ret = 1;
  6288. if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
  6289. goto fail_unlock;
  6290. /* In either case (hit or miss), make sure fastreuse is 1 only
  6291. * if sk->sk_reuse is too (that is, if the caller requested
  6292. * SO_REUSEADDR on this socket -sk-).
  6293. */
  6294. if (hlist_empty(&pp->owner)) {
  6295. if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
  6296. pp->fastreuse = 1;
  6297. else
  6298. pp->fastreuse = 0;
  6299. } else if (pp->fastreuse &&
  6300. (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
  6301. pp->fastreuse = 0;
  6302. /* We are set, so fill up all the data in the hash table
  6303. * entry, tie the socket list information with the rest of the
  6304. * sockets FIXME: Blurry, NPI (ipg).
  6305. */
  6306. success:
  6307. if (!sctp_sk(sk)->bind_hash) {
  6308. inet_sk(sk)->inet_num = snum;
  6309. sk_add_bind_node(sk, &pp->owner);
  6310. sctp_sk(sk)->bind_hash = pp;
  6311. }
  6312. ret = 0;
  6313. fail_unlock:
  6314. spin_unlock(&head->lock);
  6315. fail:
  6316. local_bh_enable();
  6317. return ret;
  6318. }
  6319. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  6320. * port is requested.
  6321. */
  6322. static int sctp_get_port(struct sock *sk, unsigned short snum)
  6323. {
  6324. union sctp_addr addr;
  6325. struct sctp_af *af = sctp_sk(sk)->pf->af;
  6326. /* Set up a dummy address struct from the sk. */
  6327. af->from_sk(&addr, sk);
  6328. addr.v4.sin_port = htons(snum);
  6329. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  6330. return !!sctp_get_port_local(sk, &addr);
  6331. }
  6332. /*
  6333. * Move a socket to LISTENING state.
  6334. */
  6335. static int sctp_listen_start(struct sock *sk, int backlog)
  6336. {
  6337. struct sctp_sock *sp = sctp_sk(sk);
  6338. struct sctp_endpoint *ep = sp->ep;
  6339. struct crypto_shash *tfm = NULL;
  6340. char alg[32];
  6341. /* Allocate HMAC for generating cookie. */
  6342. if (!sp->hmac && sp->sctp_hmac_alg) {
  6343. sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
  6344. tfm = crypto_alloc_shash(alg, 0, 0);
  6345. if (IS_ERR(tfm)) {
  6346. net_info_ratelimited("failed to load transform for %s: %ld\n",
  6347. sp->sctp_hmac_alg, PTR_ERR(tfm));
  6348. return -ENOSYS;
  6349. }
  6350. sctp_sk(sk)->hmac = tfm;
  6351. }
  6352. /*
  6353. * If a bind() or sctp_bindx() is not called prior to a listen()
  6354. * call that allows new associations to be accepted, the system
  6355. * picks an ephemeral port and will choose an address set equivalent
  6356. * to binding with a wildcard address.
  6357. *
  6358. * This is not currently spelled out in the SCTP sockets
  6359. * extensions draft, but follows the practice as seen in TCP
  6360. * sockets.
  6361. *
  6362. */
  6363. sk->sk_state = SCTP_SS_LISTENING;
  6364. if (!ep->base.bind_addr.port) {
  6365. if (sctp_autobind(sk))
  6366. return -EAGAIN;
  6367. } else {
  6368. if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
  6369. sk->sk_state = SCTP_SS_CLOSED;
  6370. return -EADDRINUSE;
  6371. }
  6372. }
  6373. sk->sk_max_ack_backlog = backlog;
  6374. sctp_hash_endpoint(ep);
  6375. return 0;
  6376. }
  6377. /*
  6378. * 4.1.3 / 5.1.3 listen()
  6379. *
  6380. * By default, new associations are not accepted for UDP style sockets.
  6381. * An application uses listen() to mark a socket as being able to
  6382. * accept new associations.
  6383. *
  6384. * On TCP style sockets, applications use listen() to ready the SCTP
  6385. * endpoint for accepting inbound associations.
  6386. *
  6387. * On both types of endpoints a backlog of '0' disables listening.
  6388. *
  6389. * Move a socket to LISTENING state.
  6390. */
  6391. int sctp_inet_listen(struct socket *sock, int backlog)
  6392. {
  6393. struct sock *sk = sock->sk;
  6394. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  6395. int err = -EINVAL;
  6396. if (unlikely(backlog < 0))
  6397. return err;
  6398. lock_sock(sk);
  6399. /* Peeled-off sockets are not allowed to listen(). */
  6400. if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
  6401. goto out;
  6402. if (sock->state != SS_UNCONNECTED)
  6403. goto out;
  6404. if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
  6405. goto out;
  6406. /* If backlog is zero, disable listening. */
  6407. if (!backlog) {
  6408. if (sctp_sstate(sk, CLOSED))
  6409. goto out;
  6410. err = 0;
  6411. sctp_unhash_endpoint(ep);
  6412. sk->sk_state = SCTP_SS_CLOSED;
  6413. if (sk->sk_reuse)
  6414. sctp_sk(sk)->bind_hash->fastreuse = 1;
  6415. goto out;
  6416. }
  6417. /* If we are already listening, just update the backlog */
  6418. if (sctp_sstate(sk, LISTENING))
  6419. sk->sk_max_ack_backlog = backlog;
  6420. else {
  6421. err = sctp_listen_start(sk, backlog);
  6422. if (err)
  6423. goto out;
  6424. }
  6425. err = 0;
  6426. out:
  6427. release_sock(sk);
  6428. return err;
  6429. }
  6430. /*
  6431. * This function is done by modeling the current datagram_poll() and the
  6432. * tcp_poll(). Note that, based on these implementations, we don't
  6433. * lock the socket in this function, even though it seems that,
  6434. * ideally, locking or some other mechanisms can be used to ensure
  6435. * the integrity of the counters (sndbuf and wmem_alloc) used
  6436. * in this place. We assume that we don't need locks either until proven
  6437. * otherwise.
  6438. *
  6439. * Another thing to note is that we include the Async I/O support
  6440. * here, again, by modeling the current TCP/UDP code. We don't have
  6441. * a good way to test with it yet.
  6442. */
  6443. unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  6444. {
  6445. struct sock *sk = sock->sk;
  6446. struct sctp_sock *sp = sctp_sk(sk);
  6447. unsigned int mask;
  6448. poll_wait(file, sk_sleep(sk), wait);
  6449. sock_rps_record_flow(sk);
  6450. /* A TCP-style listening socket becomes readable when the accept queue
  6451. * is not empty.
  6452. */
  6453. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  6454. return (!list_empty(&sp->ep->asocs)) ?
  6455. (POLLIN | POLLRDNORM) : 0;
  6456. mask = 0;
  6457. /* Is there any exceptional events? */
  6458. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  6459. mask |= POLLERR |
  6460. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? POLLPRI : 0);
  6461. if (sk->sk_shutdown & RCV_SHUTDOWN)
  6462. mask |= POLLRDHUP | POLLIN | POLLRDNORM;
  6463. if (sk->sk_shutdown == SHUTDOWN_MASK)
  6464. mask |= POLLHUP;
  6465. /* Is it readable? Reconsider this code with TCP-style support. */
  6466. if (!skb_queue_empty(&sk->sk_receive_queue))
  6467. mask |= POLLIN | POLLRDNORM;
  6468. /* The association is either gone or not ready. */
  6469. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  6470. return mask;
  6471. /* Is it writable? */
  6472. if (sctp_writeable(sk)) {
  6473. mask |= POLLOUT | POLLWRNORM;
  6474. } else {
  6475. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  6476. /*
  6477. * Since the socket is not locked, the buffer
  6478. * might be made available after the writeable check and
  6479. * before the bit is set. This could cause a lost I/O
  6480. * signal. tcp_poll() has a race breaker for this race
  6481. * condition. Based on their implementation, we put
  6482. * in the following code to cover it as well.
  6483. */
  6484. if (sctp_writeable(sk))
  6485. mask |= POLLOUT | POLLWRNORM;
  6486. }
  6487. return mask;
  6488. }
  6489. /********************************************************************
  6490. * 2nd Level Abstractions
  6491. ********************************************************************/
  6492. static struct sctp_bind_bucket *sctp_bucket_create(
  6493. struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
  6494. {
  6495. struct sctp_bind_bucket *pp;
  6496. pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
  6497. if (pp) {
  6498. SCTP_DBG_OBJCNT_INC(bind_bucket);
  6499. pp->port = snum;
  6500. pp->fastreuse = 0;
  6501. INIT_HLIST_HEAD(&pp->owner);
  6502. pp->net = net;
  6503. hlist_add_head(&pp->node, &head->chain);
  6504. }
  6505. return pp;
  6506. }
  6507. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  6508. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  6509. {
  6510. if (pp && hlist_empty(&pp->owner)) {
  6511. __hlist_del(&pp->node);
  6512. kmem_cache_free(sctp_bucket_cachep, pp);
  6513. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  6514. }
  6515. }
  6516. /* Release this socket's reference to a local port. */
  6517. static inline void __sctp_put_port(struct sock *sk)
  6518. {
  6519. struct sctp_bind_hashbucket *head =
  6520. &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
  6521. inet_sk(sk)->inet_num)];
  6522. struct sctp_bind_bucket *pp;
  6523. spin_lock(&head->lock);
  6524. pp = sctp_sk(sk)->bind_hash;
  6525. __sk_del_bind_node(sk);
  6526. sctp_sk(sk)->bind_hash = NULL;
  6527. inet_sk(sk)->inet_num = 0;
  6528. sctp_bucket_destroy(pp);
  6529. spin_unlock(&head->lock);
  6530. }
  6531. void sctp_put_port(struct sock *sk)
  6532. {
  6533. local_bh_disable();
  6534. __sctp_put_port(sk);
  6535. local_bh_enable();
  6536. }
  6537. /*
  6538. * The system picks an ephemeral port and choose an address set equivalent
  6539. * to binding with a wildcard address.
  6540. * One of those addresses will be the primary address for the association.
  6541. * This automatically enables the multihoming capability of SCTP.
  6542. */
  6543. static int sctp_autobind(struct sock *sk)
  6544. {
  6545. union sctp_addr autoaddr;
  6546. struct sctp_af *af;
  6547. __be16 port;
  6548. /* Initialize a local sockaddr structure to INADDR_ANY. */
  6549. af = sctp_sk(sk)->pf->af;
  6550. port = htons(inet_sk(sk)->inet_num);
  6551. af->inaddr_any(&autoaddr, port);
  6552. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  6553. }
  6554. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  6555. *
  6556. * From RFC 2292
  6557. * 4.2 The cmsghdr Structure *
  6558. *
  6559. * When ancillary data is sent or received, any number of ancillary data
  6560. * objects can be specified by the msg_control and msg_controllen members of
  6561. * the msghdr structure, because each object is preceded by
  6562. * a cmsghdr structure defining the object's length (the cmsg_len member).
  6563. * Historically Berkeley-derived implementations have passed only one object
  6564. * at a time, but this API allows multiple objects to be
  6565. * passed in a single call to sendmsg() or recvmsg(). The following example
  6566. * shows two ancillary data objects in a control buffer.
  6567. *
  6568. * |<--------------------------- msg_controllen -------------------------->|
  6569. * | |
  6570. *
  6571. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  6572. *
  6573. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  6574. * | | |
  6575. *
  6576. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  6577. *
  6578. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  6579. * | | | | |
  6580. *
  6581. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  6582. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  6583. *
  6584. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  6585. *
  6586. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  6587. * ^
  6588. * |
  6589. *
  6590. * msg_control
  6591. * points here
  6592. */
  6593. static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
  6594. {
  6595. struct msghdr *my_msg = (struct msghdr *)msg;
  6596. struct cmsghdr *cmsg;
  6597. for_each_cmsghdr(cmsg, my_msg) {
  6598. if (!CMSG_OK(my_msg, cmsg))
  6599. return -EINVAL;
  6600. /* Should we parse this header or ignore? */
  6601. if (cmsg->cmsg_level != IPPROTO_SCTP)
  6602. continue;
  6603. /* Strictly check lengths following example in SCM code. */
  6604. switch (cmsg->cmsg_type) {
  6605. case SCTP_INIT:
  6606. /* SCTP Socket API Extension
  6607. * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
  6608. *
  6609. * This cmsghdr structure provides information for
  6610. * initializing new SCTP associations with sendmsg().
  6611. * The SCTP_INITMSG socket option uses this same data
  6612. * structure. This structure is not used for
  6613. * recvmsg().
  6614. *
  6615. * cmsg_level cmsg_type cmsg_data[]
  6616. * ------------ ------------ ----------------------
  6617. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  6618. */
  6619. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
  6620. return -EINVAL;
  6621. cmsgs->init = CMSG_DATA(cmsg);
  6622. break;
  6623. case SCTP_SNDRCV:
  6624. /* SCTP Socket API Extension
  6625. * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
  6626. *
  6627. * This cmsghdr structure specifies SCTP options for
  6628. * sendmsg() and describes SCTP header information
  6629. * about a received message through recvmsg().
  6630. *
  6631. * cmsg_level cmsg_type cmsg_data[]
  6632. * ------------ ------------ ----------------------
  6633. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  6634. */
  6635. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  6636. return -EINVAL;
  6637. cmsgs->srinfo = CMSG_DATA(cmsg);
  6638. if (cmsgs->srinfo->sinfo_flags &
  6639. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  6640. SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
  6641. SCTP_ABORT | SCTP_EOF))
  6642. return -EINVAL;
  6643. break;
  6644. case SCTP_SNDINFO:
  6645. /* SCTP Socket API Extension
  6646. * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
  6647. *
  6648. * This cmsghdr structure specifies SCTP options for
  6649. * sendmsg(). This structure and SCTP_RCVINFO replaces
  6650. * SCTP_SNDRCV which has been deprecated.
  6651. *
  6652. * cmsg_level cmsg_type cmsg_data[]
  6653. * ------------ ------------ ---------------------
  6654. * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
  6655. */
  6656. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
  6657. return -EINVAL;
  6658. cmsgs->sinfo = CMSG_DATA(cmsg);
  6659. if (cmsgs->sinfo->snd_flags &
  6660. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  6661. SCTP_SACK_IMMEDIATELY | SCTP_PR_SCTP_MASK |
  6662. SCTP_ABORT | SCTP_EOF))
  6663. return -EINVAL;
  6664. break;
  6665. default:
  6666. return -EINVAL;
  6667. }
  6668. }
  6669. return 0;
  6670. }
  6671. /*
  6672. * Wait for a packet..
  6673. * Note: This function is the same function as in core/datagram.c
  6674. * with a few modifications to make lksctp work.
  6675. */
  6676. static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
  6677. {
  6678. int error;
  6679. DEFINE_WAIT(wait);
  6680. prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  6681. /* Socket errors? */
  6682. error = sock_error(sk);
  6683. if (error)
  6684. goto out;
  6685. if (!skb_queue_empty(&sk->sk_receive_queue))
  6686. goto ready;
  6687. /* Socket shut down? */
  6688. if (sk->sk_shutdown & RCV_SHUTDOWN)
  6689. goto out;
  6690. /* Sequenced packets can come disconnected. If so we report the
  6691. * problem.
  6692. */
  6693. error = -ENOTCONN;
  6694. /* Is there a good reason to think that we may receive some data? */
  6695. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  6696. goto out;
  6697. /* Handle signals. */
  6698. if (signal_pending(current))
  6699. goto interrupted;
  6700. /* Let another process have a go. Since we are going to sleep
  6701. * anyway. Note: This may cause odd behaviors if the message
  6702. * does not fit in the user's buffer, but this seems to be the
  6703. * only way to honor MSG_DONTWAIT realistically.
  6704. */
  6705. release_sock(sk);
  6706. *timeo_p = schedule_timeout(*timeo_p);
  6707. lock_sock(sk);
  6708. ready:
  6709. finish_wait(sk_sleep(sk), &wait);
  6710. return 0;
  6711. interrupted:
  6712. error = sock_intr_errno(*timeo_p);
  6713. out:
  6714. finish_wait(sk_sleep(sk), &wait);
  6715. *err = error;
  6716. return error;
  6717. }
  6718. /* Receive a datagram.
  6719. * Note: This is pretty much the same routine as in core/datagram.c
  6720. * with a few changes to make lksctp work.
  6721. */
  6722. struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  6723. int noblock, int *err)
  6724. {
  6725. int error;
  6726. struct sk_buff *skb;
  6727. long timeo;
  6728. timeo = sock_rcvtimeo(sk, noblock);
  6729. pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
  6730. MAX_SCHEDULE_TIMEOUT);
  6731. do {
  6732. /* Again only user level code calls this function,
  6733. * so nothing interrupt level
  6734. * will suddenly eat the receive_queue.
  6735. *
  6736. * Look at current nfs client by the way...
  6737. * However, this function was correct in any case. 8)
  6738. */
  6739. if (flags & MSG_PEEK) {
  6740. skb = skb_peek(&sk->sk_receive_queue);
  6741. if (skb)
  6742. refcount_inc(&skb->users);
  6743. } else {
  6744. skb = __skb_dequeue(&sk->sk_receive_queue);
  6745. }
  6746. if (skb)
  6747. return skb;
  6748. /* Caller is allowed not to check sk->sk_err before calling. */
  6749. error = sock_error(sk);
  6750. if (error)
  6751. goto no_packet;
  6752. if (sk->sk_shutdown & RCV_SHUTDOWN)
  6753. break;
  6754. if (sk_can_busy_loop(sk)) {
  6755. sk_busy_loop(sk, noblock);
  6756. if (!skb_queue_empty(&sk->sk_receive_queue))
  6757. continue;
  6758. }
  6759. /* User doesn't want to wait. */
  6760. error = -EAGAIN;
  6761. if (!timeo)
  6762. goto no_packet;
  6763. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  6764. return NULL;
  6765. no_packet:
  6766. *err = error;
  6767. return NULL;
  6768. }
  6769. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  6770. static void __sctp_write_space(struct sctp_association *asoc)
  6771. {
  6772. struct sock *sk = asoc->base.sk;
  6773. if (sctp_wspace(asoc) <= 0)
  6774. return;
  6775. if (waitqueue_active(&asoc->wait))
  6776. wake_up_interruptible(&asoc->wait);
  6777. if (sctp_writeable(sk)) {
  6778. struct socket_wq *wq;
  6779. rcu_read_lock();
  6780. wq = rcu_dereference(sk->sk_wq);
  6781. if (wq) {
  6782. if (waitqueue_active(&wq->wait))
  6783. wake_up_interruptible(&wq->wait);
  6784. /* Note that we try to include the Async I/O support
  6785. * here by modeling from the current TCP/UDP code.
  6786. * We have not tested with it yet.
  6787. */
  6788. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  6789. sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
  6790. }
  6791. rcu_read_unlock();
  6792. }
  6793. }
  6794. static void sctp_wake_up_waiters(struct sock *sk,
  6795. struct sctp_association *asoc)
  6796. {
  6797. struct sctp_association *tmp = asoc;
  6798. /* We do accounting for the sndbuf space per association,
  6799. * so we only need to wake our own association.
  6800. */
  6801. if (asoc->ep->sndbuf_policy)
  6802. return __sctp_write_space(asoc);
  6803. /* If association goes down and is just flushing its
  6804. * outq, then just normally notify others.
  6805. */
  6806. if (asoc->base.dead)
  6807. return sctp_write_space(sk);
  6808. /* Accounting for the sndbuf space is per socket, so we
  6809. * need to wake up others, try to be fair and in case of
  6810. * other associations, let them have a go first instead
  6811. * of just doing a sctp_write_space() call.
  6812. *
  6813. * Note that we reach sctp_wake_up_waiters() only when
  6814. * associations free up queued chunks, thus we are under
  6815. * lock and the list of associations on a socket is
  6816. * guaranteed not to change.
  6817. */
  6818. for (tmp = list_next_entry(tmp, asocs); 1;
  6819. tmp = list_next_entry(tmp, asocs)) {
  6820. /* Manually skip the head element. */
  6821. if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
  6822. continue;
  6823. /* Wake up association. */
  6824. __sctp_write_space(tmp);
  6825. /* We've reached the end. */
  6826. if (tmp == asoc)
  6827. break;
  6828. }
  6829. }
  6830. /* Do accounting for the sndbuf space.
  6831. * Decrement the used sndbuf space of the corresponding association by the
  6832. * data size which was just transmitted(freed).
  6833. */
  6834. static void sctp_wfree(struct sk_buff *skb)
  6835. {
  6836. struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
  6837. struct sctp_association *asoc = chunk->asoc;
  6838. struct sock *sk = asoc->base.sk;
  6839. asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
  6840. sizeof(struct sk_buff) +
  6841. sizeof(struct sctp_chunk);
  6842. WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
  6843. /*
  6844. * This undoes what is done via sctp_set_owner_w and sk_mem_charge
  6845. */
  6846. sk->sk_wmem_queued -= skb->truesize;
  6847. sk_mem_uncharge(sk, skb->truesize);
  6848. sock_wfree(skb);
  6849. sctp_wake_up_waiters(sk, asoc);
  6850. sctp_association_put(asoc);
  6851. }
  6852. /* Do accounting for the receive space on the socket.
  6853. * Accounting for the association is done in ulpevent.c
  6854. * We set this as a destructor for the cloned data skbs so that
  6855. * accounting is done at the correct time.
  6856. */
  6857. void sctp_sock_rfree(struct sk_buff *skb)
  6858. {
  6859. struct sock *sk = skb->sk;
  6860. struct sctp_ulpevent *event = sctp_skb2event(skb);
  6861. atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
  6862. /*
  6863. * Mimic the behavior of sock_rfree
  6864. */
  6865. sk_mem_uncharge(sk, event->rmem_len);
  6866. }
  6867. /* Helper function to wait for space in the sndbuf. */
  6868. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  6869. size_t msg_len, struct sock **orig_sk)
  6870. {
  6871. struct sock *sk = asoc->base.sk;
  6872. int err = 0;
  6873. long current_timeo = *timeo_p;
  6874. DEFINE_WAIT(wait);
  6875. pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
  6876. *timeo_p, msg_len);
  6877. /* Increment the association's refcnt. */
  6878. sctp_association_hold(asoc);
  6879. /* Wait on the association specific sndbuf space. */
  6880. for (;;) {
  6881. prepare_to_wait_exclusive(&asoc->wait, &wait,
  6882. TASK_INTERRUPTIBLE);
  6883. if (asoc->base.dead)
  6884. goto do_dead;
  6885. if (!*timeo_p)
  6886. goto do_nonblock;
  6887. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
  6888. goto do_error;
  6889. if (signal_pending(current))
  6890. goto do_interrupted;
  6891. if (msg_len <= sctp_wspace(asoc))
  6892. break;
  6893. /* Let another process have a go. Since we are going
  6894. * to sleep anyway.
  6895. */
  6896. release_sock(sk);
  6897. current_timeo = schedule_timeout(current_timeo);
  6898. lock_sock(sk);
  6899. if (sk != asoc->base.sk) {
  6900. release_sock(sk);
  6901. sk = asoc->base.sk;
  6902. lock_sock(sk);
  6903. }
  6904. *timeo_p = current_timeo;
  6905. }
  6906. out:
  6907. *orig_sk = sk;
  6908. finish_wait(&asoc->wait, &wait);
  6909. /* Release the association's refcnt. */
  6910. sctp_association_put(asoc);
  6911. return err;
  6912. do_dead:
  6913. err = -ESRCH;
  6914. goto out;
  6915. do_error:
  6916. err = -EPIPE;
  6917. goto out;
  6918. do_interrupted:
  6919. err = sock_intr_errno(*timeo_p);
  6920. goto out;
  6921. do_nonblock:
  6922. err = -EAGAIN;
  6923. goto out;
  6924. }
  6925. void sctp_data_ready(struct sock *sk)
  6926. {
  6927. struct socket_wq *wq;
  6928. rcu_read_lock();
  6929. wq = rcu_dereference(sk->sk_wq);
  6930. if (skwq_has_sleeper(wq))
  6931. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  6932. POLLRDNORM | POLLRDBAND);
  6933. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  6934. rcu_read_unlock();
  6935. }
  6936. /* If socket sndbuf has changed, wake up all per association waiters. */
  6937. void sctp_write_space(struct sock *sk)
  6938. {
  6939. struct sctp_association *asoc;
  6940. /* Wake up the tasks in each wait queue. */
  6941. list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
  6942. __sctp_write_space(asoc);
  6943. }
  6944. }
  6945. /* Is there any sndbuf space available on the socket?
  6946. *
  6947. * Note that sk_wmem_alloc is the sum of the send buffers on all of the
  6948. * associations on the same socket. For a UDP-style socket with
  6949. * multiple associations, it is possible for it to be "unwriteable"
  6950. * prematurely. I assume that this is acceptable because
  6951. * a premature "unwriteable" is better than an accidental "writeable" which
  6952. * would cause an unwanted block under certain circumstances. For the 1-1
  6953. * UDP-style sockets or TCP-style sockets, this code should work.
  6954. * - Daisy
  6955. */
  6956. static int sctp_writeable(struct sock *sk)
  6957. {
  6958. int amt = 0;
  6959. amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  6960. if (amt < 0)
  6961. amt = 0;
  6962. return amt;
  6963. }
  6964. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  6965. * returns immediately with EINPROGRESS.
  6966. */
  6967. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  6968. {
  6969. struct sock *sk = asoc->base.sk;
  6970. int err = 0;
  6971. long current_timeo = *timeo_p;
  6972. DEFINE_WAIT(wait);
  6973. pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
  6974. /* Increment the association's refcnt. */
  6975. sctp_association_hold(asoc);
  6976. for (;;) {
  6977. prepare_to_wait_exclusive(&asoc->wait, &wait,
  6978. TASK_INTERRUPTIBLE);
  6979. if (!*timeo_p)
  6980. goto do_nonblock;
  6981. if (sk->sk_shutdown & RCV_SHUTDOWN)
  6982. break;
  6983. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  6984. asoc->base.dead)
  6985. goto do_error;
  6986. if (signal_pending(current))
  6987. goto do_interrupted;
  6988. if (sctp_state(asoc, ESTABLISHED))
  6989. break;
  6990. /* Let another process have a go. Since we are going
  6991. * to sleep anyway.
  6992. */
  6993. release_sock(sk);
  6994. current_timeo = schedule_timeout(current_timeo);
  6995. lock_sock(sk);
  6996. *timeo_p = current_timeo;
  6997. }
  6998. out:
  6999. finish_wait(&asoc->wait, &wait);
  7000. /* Release the association's refcnt. */
  7001. sctp_association_put(asoc);
  7002. return err;
  7003. do_error:
  7004. if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
  7005. err = -ETIMEDOUT;
  7006. else
  7007. err = -ECONNREFUSED;
  7008. goto out;
  7009. do_interrupted:
  7010. err = sock_intr_errno(*timeo_p);
  7011. goto out;
  7012. do_nonblock:
  7013. err = -EINPROGRESS;
  7014. goto out;
  7015. }
  7016. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  7017. {
  7018. struct sctp_endpoint *ep;
  7019. int err = 0;
  7020. DEFINE_WAIT(wait);
  7021. ep = sctp_sk(sk)->ep;
  7022. for (;;) {
  7023. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  7024. TASK_INTERRUPTIBLE);
  7025. if (list_empty(&ep->asocs)) {
  7026. release_sock(sk);
  7027. timeo = schedule_timeout(timeo);
  7028. lock_sock(sk);
  7029. }
  7030. err = -EINVAL;
  7031. if (!sctp_sstate(sk, LISTENING))
  7032. break;
  7033. err = 0;
  7034. if (!list_empty(&ep->asocs))
  7035. break;
  7036. err = sock_intr_errno(timeo);
  7037. if (signal_pending(current))
  7038. break;
  7039. err = -EAGAIN;
  7040. if (!timeo)
  7041. break;
  7042. }
  7043. finish_wait(sk_sleep(sk), &wait);
  7044. return err;
  7045. }
  7046. static void sctp_wait_for_close(struct sock *sk, long timeout)
  7047. {
  7048. DEFINE_WAIT(wait);
  7049. do {
  7050. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  7051. if (list_empty(&sctp_sk(sk)->ep->asocs))
  7052. break;
  7053. release_sock(sk);
  7054. timeout = schedule_timeout(timeout);
  7055. lock_sock(sk);
  7056. } while (!signal_pending(current) && timeout);
  7057. finish_wait(sk_sleep(sk), &wait);
  7058. }
  7059. static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
  7060. {
  7061. struct sk_buff *frag;
  7062. if (!skb->data_len)
  7063. goto done;
  7064. /* Don't forget the fragments. */
  7065. skb_walk_frags(skb, frag)
  7066. sctp_skb_set_owner_r_frag(frag, sk);
  7067. done:
  7068. sctp_skb_set_owner_r(skb, sk);
  7069. }
  7070. void sctp_copy_sock(struct sock *newsk, struct sock *sk,
  7071. struct sctp_association *asoc)
  7072. {
  7073. struct inet_sock *inet = inet_sk(sk);
  7074. struct inet_sock *newinet;
  7075. newsk->sk_type = sk->sk_type;
  7076. newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
  7077. newsk->sk_flags = sk->sk_flags;
  7078. newsk->sk_tsflags = sk->sk_tsflags;
  7079. newsk->sk_no_check_tx = sk->sk_no_check_tx;
  7080. newsk->sk_no_check_rx = sk->sk_no_check_rx;
  7081. newsk->sk_reuse = sk->sk_reuse;
  7082. newsk->sk_shutdown = sk->sk_shutdown;
  7083. newsk->sk_destruct = sctp_destruct_sock;
  7084. newsk->sk_family = sk->sk_family;
  7085. newsk->sk_protocol = IPPROTO_SCTP;
  7086. newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  7087. newsk->sk_sndbuf = sk->sk_sndbuf;
  7088. newsk->sk_rcvbuf = sk->sk_rcvbuf;
  7089. newsk->sk_lingertime = sk->sk_lingertime;
  7090. newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
  7091. newsk->sk_sndtimeo = sk->sk_sndtimeo;
  7092. newsk->sk_rxhash = sk->sk_rxhash;
  7093. newinet = inet_sk(newsk);
  7094. /* Initialize sk's sport, dport, rcv_saddr and daddr for
  7095. * getsockname() and getpeername()
  7096. */
  7097. newinet->inet_sport = inet->inet_sport;
  7098. newinet->inet_saddr = inet->inet_saddr;
  7099. newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
  7100. newinet->inet_dport = htons(asoc->peer.port);
  7101. newinet->pmtudisc = inet->pmtudisc;
  7102. newinet->inet_id = asoc->next_tsn ^ jiffies;
  7103. newinet->uc_ttl = inet->uc_ttl;
  7104. newinet->mc_loop = 1;
  7105. newinet->mc_ttl = 1;
  7106. newinet->mc_index = 0;
  7107. newinet->mc_list = NULL;
  7108. if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
  7109. net_enable_timestamp();
  7110. security_sk_clone(sk, newsk);
  7111. }
  7112. static inline void sctp_copy_descendant(struct sock *sk_to,
  7113. const struct sock *sk_from)
  7114. {
  7115. int ancestor_size = sizeof(struct inet_sock) +
  7116. sizeof(struct sctp_sock) -
  7117. offsetof(struct sctp_sock, auto_asconf_list);
  7118. if (sk_from->sk_family == PF_INET6)
  7119. ancestor_size += sizeof(struct ipv6_pinfo);
  7120. __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
  7121. }
  7122. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  7123. * and its messages to the newsk.
  7124. */
  7125. static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  7126. struct sctp_association *assoc,
  7127. enum sctp_socket_type type)
  7128. {
  7129. struct sctp_sock *oldsp = sctp_sk(oldsk);
  7130. struct sctp_sock *newsp = sctp_sk(newsk);
  7131. struct sctp_bind_bucket *pp; /* hash list port iterator */
  7132. struct sctp_endpoint *newep = newsp->ep;
  7133. struct sk_buff *skb, *tmp;
  7134. struct sctp_ulpevent *event;
  7135. struct sctp_bind_hashbucket *head;
  7136. /* Migrate socket buffer sizes and all the socket level options to the
  7137. * new socket.
  7138. */
  7139. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  7140. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  7141. /* Brute force copy old sctp opt. */
  7142. sctp_copy_descendant(newsk, oldsk);
  7143. /* Restore the ep value that was overwritten with the above structure
  7144. * copy.
  7145. */
  7146. newsp->ep = newep;
  7147. newsp->hmac = NULL;
  7148. /* Hook this new socket in to the bind_hash list. */
  7149. head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
  7150. inet_sk(oldsk)->inet_num)];
  7151. spin_lock_bh(&head->lock);
  7152. pp = sctp_sk(oldsk)->bind_hash;
  7153. sk_add_bind_node(newsk, &pp->owner);
  7154. sctp_sk(newsk)->bind_hash = pp;
  7155. inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
  7156. spin_unlock_bh(&head->lock);
  7157. /* Copy the bind_addr list from the original endpoint to the new
  7158. * endpoint so that we can handle restarts properly
  7159. */
  7160. sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
  7161. &oldsp->ep->base.bind_addr, GFP_KERNEL);
  7162. /* Move any messages in the old socket's receive queue that are for the
  7163. * peeled off association to the new socket's receive queue.
  7164. */
  7165. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  7166. event = sctp_skb2event(skb);
  7167. if (event->asoc == assoc) {
  7168. __skb_unlink(skb, &oldsk->sk_receive_queue);
  7169. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  7170. sctp_skb_set_owner_r_frag(skb, newsk);
  7171. }
  7172. }
  7173. /* Clean up any messages pending delivery due to partial
  7174. * delivery. Three cases:
  7175. * 1) No partial deliver; no work.
  7176. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  7177. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  7178. */
  7179. skb_queue_head_init(&newsp->pd_lobby);
  7180. atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
  7181. if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
  7182. struct sk_buff_head *queue;
  7183. /* Decide which queue to move pd_lobby skbs to. */
  7184. if (assoc->ulpq.pd_mode) {
  7185. queue = &newsp->pd_lobby;
  7186. } else
  7187. queue = &newsk->sk_receive_queue;
  7188. /* Walk through the pd_lobby, looking for skbs that
  7189. * need moved to the new socket.
  7190. */
  7191. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  7192. event = sctp_skb2event(skb);
  7193. if (event->asoc == assoc) {
  7194. __skb_unlink(skb, &oldsp->pd_lobby);
  7195. __skb_queue_tail(queue, skb);
  7196. sctp_skb_set_owner_r_frag(skb, newsk);
  7197. }
  7198. }
  7199. /* Clear up any skbs waiting for the partial
  7200. * delivery to finish.
  7201. */
  7202. if (assoc->ulpq.pd_mode)
  7203. sctp_clear_pd(oldsk, NULL);
  7204. }
  7205. sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp)
  7206. sctp_skb_set_owner_r_frag(skb, newsk);
  7207. sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp)
  7208. sctp_skb_set_owner_r_frag(skb, newsk);
  7209. /* Set the type of socket to indicate that it is peeled off from the
  7210. * original UDP-style socket or created with the accept() call on a
  7211. * TCP-style socket..
  7212. */
  7213. newsp->type = type;
  7214. /* Mark the new socket "in-use" by the user so that any packets
  7215. * that may arrive on the association after we've moved it are
  7216. * queued to the backlog. This prevents a potential race between
  7217. * backlog processing on the old socket and new-packet processing
  7218. * on the new socket.
  7219. *
  7220. * The caller has just allocated newsk so we can guarantee that other
  7221. * paths won't try to lock it and then oldsk.
  7222. */
  7223. lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
  7224. sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
  7225. sctp_assoc_migrate(assoc, newsk);
  7226. sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
  7227. /* If the association on the newsk is already closed before accept()
  7228. * is called, set RCV_SHUTDOWN flag.
  7229. */
  7230. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
  7231. newsk->sk_state = SCTP_SS_CLOSED;
  7232. newsk->sk_shutdown |= RCV_SHUTDOWN;
  7233. } else {
  7234. newsk->sk_state = SCTP_SS_ESTABLISHED;
  7235. }
  7236. release_sock(newsk);
  7237. }
  7238. /* This proto struct describes the ULP interface for SCTP. */
  7239. struct proto sctp_prot = {
  7240. .name = "SCTP",
  7241. .owner = THIS_MODULE,
  7242. .close = sctp_close,
  7243. .connect = sctp_connect,
  7244. .disconnect = sctp_disconnect,
  7245. .accept = sctp_accept,
  7246. .ioctl = sctp_ioctl,
  7247. .init = sctp_init_sock,
  7248. .destroy = sctp_destroy_sock,
  7249. .shutdown = sctp_shutdown,
  7250. .setsockopt = sctp_setsockopt,
  7251. .getsockopt = sctp_getsockopt,
  7252. .sendmsg = sctp_sendmsg,
  7253. .recvmsg = sctp_recvmsg,
  7254. .bind = sctp_bind,
  7255. .backlog_rcv = sctp_backlog_rcv,
  7256. .hash = sctp_hash,
  7257. .unhash = sctp_unhash,
  7258. .get_port = sctp_get_port,
  7259. .obj_size = sizeof(struct sctp_sock),
  7260. .useroffset = offsetof(struct sctp_sock, subscribe),
  7261. .usersize = offsetof(struct sctp_sock, initmsg) -
  7262. offsetof(struct sctp_sock, subscribe) +
  7263. sizeof_field(struct sctp_sock, initmsg),
  7264. .sysctl_mem = sysctl_sctp_mem,
  7265. .sysctl_rmem = sysctl_sctp_rmem,
  7266. .sysctl_wmem = sysctl_sctp_wmem,
  7267. .memory_pressure = &sctp_memory_pressure,
  7268. .enter_memory_pressure = sctp_enter_memory_pressure,
  7269. .memory_allocated = &sctp_memory_allocated,
  7270. .sockets_allocated = &sctp_sockets_allocated,
  7271. };
  7272. #if IS_ENABLED(CONFIG_IPV6)
  7273. #include <net/transp_v6.h>
  7274. static void sctp_v6_destroy_sock(struct sock *sk)
  7275. {
  7276. sctp_destroy_sock(sk);
  7277. inet6_destroy_sock(sk);
  7278. }
  7279. struct proto sctpv6_prot = {
  7280. .name = "SCTPv6",
  7281. .owner = THIS_MODULE,
  7282. .close = sctp_close,
  7283. .connect = sctp_connect,
  7284. .disconnect = sctp_disconnect,
  7285. .accept = sctp_accept,
  7286. .ioctl = sctp_ioctl,
  7287. .init = sctp_init_sock,
  7288. .destroy = sctp_v6_destroy_sock,
  7289. .shutdown = sctp_shutdown,
  7290. .setsockopt = sctp_setsockopt,
  7291. .getsockopt = sctp_getsockopt,
  7292. .sendmsg = sctp_sendmsg,
  7293. .recvmsg = sctp_recvmsg,
  7294. .bind = sctp_bind,
  7295. .backlog_rcv = sctp_backlog_rcv,
  7296. .hash = sctp_hash,
  7297. .unhash = sctp_unhash,
  7298. .get_port = sctp_get_port,
  7299. .obj_size = sizeof(struct sctp6_sock),
  7300. .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
  7301. .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
  7302. offsetof(struct sctp6_sock, sctp.subscribe) +
  7303. sizeof_field(struct sctp6_sock, sctp.initmsg),
  7304. .sysctl_mem = sysctl_sctp_mem,
  7305. .sysctl_rmem = sysctl_sctp_rmem,
  7306. .sysctl_wmem = sysctl_sctp_wmem,
  7307. .memory_pressure = &sctp_memory_pressure,
  7308. .enter_memory_pressure = sctp_enter_memory_pressure,
  7309. .memory_allocated = &sctp_memory_allocated,
  7310. .sockets_allocated = &sctp_sockets_allocated,
  7311. };
  7312. #endif /* IS_ENABLED(CONFIG_IPV6) */