socket.c 265 KB

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