socket.c 208 KB

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