socket.c 227 KB

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