socket.c 161 KB

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