socket.c 161 KB

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