socket.c 199 KB

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