socket.c 163 KB

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