socket.c 202 KB

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