socket.c 200 KB

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