socket.c 194 KB

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