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