socket.c 163 KB

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