socket.c 247 KB

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