socket.c 164 KB

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