socket.c 183 KB

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