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