socket.c 200 KB

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