socket.c 193 KB

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