socket.c 234 KB

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