socket.c 165 KB

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