socket.c 163 KB

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