socket.c 221 KB

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