socket.c 194 KB

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