socket.c 188 KB

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