socket.c 162 KB

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