socket.c 162 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001-2003 Intel Corp.
  6. * Copyright (c) 2001-2002 Nokia, Inc.
  7. * Copyright (c) 2001 La Monte H.P. Yarroll
  8. *
  9. * This file is part of the SCTP kernel reference Implementation
  10. *
  11. * These functions interface with the sockets layer to implement the
  12. * SCTP Extensions for the Sockets API.
  13. *
  14. * Note that the descriptions from the specification are USER level
  15. * functions--this file is the functions which populate the struct proto
  16. * for SCTP which is the BOTTOM of the sockets interface.
  17. *
  18. * The SCTP reference implementation is free software;
  19. * you can redistribute it and/or modify it under the terms of
  20. * the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2, or (at your option)
  22. * any later version.
  23. *
  24. * The SCTP reference implementation is distributed in the hope that it
  25. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  26. * ************************
  27. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  28. * See the GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with GNU CC; see the file COPYING. If not, write to
  32. * the Free Software Foundation, 59 Temple Place - Suite 330,
  33. * Boston, MA 02111-1307, USA.
  34. *
  35. * Please send any bug reports or fixes you make to the
  36. * email address(es):
  37. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  38. *
  39. * Or submit a bug report through the following website:
  40. * http://www.sf.net/projects/lksctp
  41. *
  42. * Written or modified by:
  43. * La Monte H.P. Yarroll <piggy@acm.org>
  44. * Narasimha Budihal <narsi@refcode.org>
  45. * Karl Knutson <karl@athena.chicago.il.us>
  46. * Jon Grimm <jgrimm@us.ibm.com>
  47. * Xingang Guo <xingang.guo@intel.com>
  48. * Daisy Chang <daisyc@us.ibm.com>
  49. * Sridhar Samudrala <samudrala@us.ibm.com>
  50. * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
  51. * Ardelle Fan <ardelle.fan@intel.com>
  52. * Ryan Layer <rmlayer@us.ibm.com>
  53. * Anup Pemmaiah <pemmaiah@cc.usu.edu>
  54. * Kevin Gao <kevin.gao@intel.com>
  55. *
  56. * Any bugs reported given to us we will try to fix... any fixes shared will
  57. * be incorporated into the next SCTP release.
  58. */
  59. #include <linux/types.h>
  60. #include <linux/kernel.h>
  61. #include <linux/wait.h>
  62. #include <linux/time.h>
  63. #include <linux/ip.h>
  64. #include <linux/capability.h>
  65. #include <linux/fcntl.h>
  66. #include <linux/poll.h>
  67. #include <linux/init.h>
  68. #include <linux/crypto.h>
  69. #include <net/ip.h>
  70. #include <net/icmp.h>
  71. #include <net/route.h>
  72. #include <net/ipv6.h>
  73. #include <net/inet_common.h>
  74. #include <linux/socket.h> /* for sa_family_t */
  75. #include <net/sock.h>
  76. #include <net/sctp/sctp.h>
  77. #include <net/sctp/sm.h>
  78. /* WARNING: Please do not remove the SCTP_STATIC attribute to
  79. * any of the functions below as they are used to export functions
  80. * used by a project regression testsuite.
  81. */
  82. /* Forward declarations for internal helper functions. */
  83. static int sctp_writeable(struct sock *sk);
  84. static void sctp_wfree(struct sk_buff *skb);
  85. static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
  86. size_t msg_len);
  87. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
  88. static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
  89. static int sctp_wait_for_accept(struct sock *sk, long timeo);
  90. static void sctp_wait_for_close(struct sock *sk, long timeo);
  91. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  92. union sctp_addr *addr, int len);
  93. static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
  94. static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
  95. static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
  96. static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
  97. static int sctp_send_asconf(struct sctp_association *asoc,
  98. struct sctp_chunk *chunk);
  99. static int sctp_do_bind(struct sock *, union sctp_addr *, int);
  100. static int sctp_autobind(struct sock *sk);
  101. static void sctp_sock_migrate(struct sock *, struct sock *,
  102. struct sctp_association *, sctp_socket_type_t);
  103. static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
  104. extern kmem_cache_t *sctp_bucket_cachep;
  105. /* Get the sndbuf space available at the time on the association. */
  106. static inline int sctp_wspace(struct sctp_association *asoc)
  107. {
  108. struct sock *sk = asoc->base.sk;
  109. int amt = 0;
  110. if (asoc->ep->sndbuf_policy) {
  111. /* make sure that no association uses more than sk_sndbuf */
  112. amt = sk->sk_sndbuf - asoc->sndbuf_used;
  113. } else {
  114. /* do socket level accounting */
  115. amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
  116. }
  117. if (amt < 0)
  118. amt = 0;
  119. return amt;
  120. }
  121. /* Increment the used sndbuf space count of the corresponding association by
  122. * the size of the outgoing data chunk.
  123. * Also, set the skb destructor for sndbuf accounting later.
  124. *
  125. * Since it is always 1-1 between chunk and skb, and also a new skb is always
  126. * allocated for chunk bundling in sctp_packet_transmit(), we can use the
  127. * destructor in the data chunk skb for the purpose of the sndbuf space
  128. * tracking.
  129. */
  130. static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
  131. {
  132. struct sctp_association *asoc = chunk->asoc;
  133. struct sock *sk = asoc->base.sk;
  134. /* The sndbuf space is tracked per association. */
  135. sctp_association_hold(asoc);
  136. skb_set_owner_w(chunk->skb, sk);
  137. chunk->skb->destructor = sctp_wfree;
  138. /* Save the chunk pointer in skb for sctp_wfree to use later. */
  139. *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
  140. asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
  141. sizeof(struct sk_buff) +
  142. sizeof(struct sctp_chunk);
  143. atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  144. }
  145. /* Verify that this is a valid address. */
  146. static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
  147. int len)
  148. {
  149. struct sctp_af *af;
  150. /* Verify basic sockaddr. */
  151. af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
  152. if (!af)
  153. return -EINVAL;
  154. /* Is this a valid SCTP address? */
  155. if (!af->addr_valid(addr, sctp_sk(sk), NULL))
  156. return -EINVAL;
  157. if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
  158. return -EINVAL;
  159. return 0;
  160. }
  161. /* Look up the association by its id. If this is not a UDP-style
  162. * socket, the ID field is always ignored.
  163. */
  164. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
  165. {
  166. struct sctp_association *asoc = NULL;
  167. /* If this is not a UDP-style socket, assoc id should be ignored. */
  168. if (!sctp_style(sk, UDP)) {
  169. /* Return NULL if the socket state is not ESTABLISHED. It
  170. * could be a TCP-style listening socket or a socket which
  171. * hasn't yet called connect() to establish an association.
  172. */
  173. if (!sctp_sstate(sk, ESTABLISHED))
  174. return NULL;
  175. /* Get the first and the only association from the list. */
  176. if (!list_empty(&sctp_sk(sk)->ep->asocs))
  177. asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
  178. struct sctp_association, asocs);
  179. return asoc;
  180. }
  181. /* Otherwise this is a UDP-style socket. */
  182. if (!id || (id == (sctp_assoc_t)-1))
  183. return NULL;
  184. spin_lock_bh(&sctp_assocs_id_lock);
  185. asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
  186. spin_unlock_bh(&sctp_assocs_id_lock);
  187. if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
  188. return NULL;
  189. return asoc;
  190. }
  191. /* Look up the transport from an address and an assoc id. If both address and
  192. * id are specified, the associations matching the address and the id should be
  193. * the same.
  194. */
  195. static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
  196. struct sockaddr_storage *addr,
  197. sctp_assoc_t id)
  198. {
  199. struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
  200. struct sctp_transport *transport;
  201. union sctp_addr *laddr = (union sctp_addr *)addr;
  202. laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
  203. addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
  204. (union sctp_addr *)addr,
  205. &transport);
  206. laddr->v4.sin_port = htons(laddr->v4.sin_port);
  207. if (!addr_asoc)
  208. return NULL;
  209. id_asoc = sctp_id2assoc(sk, id);
  210. if (id_asoc && (id_asoc != addr_asoc))
  211. return NULL;
  212. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  213. (union sctp_addr *)addr);
  214. return transport;
  215. }
  216. /* API 3.1.2 bind() - UDP Style Syntax
  217. * The syntax of bind() is,
  218. *
  219. * ret = bind(int sd, struct sockaddr *addr, int addrlen);
  220. *
  221. * sd - the socket descriptor returned by socket().
  222. * addr - the address structure (struct sockaddr_in or struct
  223. * sockaddr_in6 [RFC 2553]),
  224. * addr_len - the size of the address structure.
  225. */
  226. SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
  227. {
  228. int retval = 0;
  229. sctp_lock_sock(sk);
  230. SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
  231. sk, addr, addr_len);
  232. /* Disallow binding twice. */
  233. if (!sctp_sk(sk)->ep->base.bind_addr.port)
  234. retval = sctp_do_bind(sk, (union sctp_addr *)addr,
  235. addr_len);
  236. else
  237. retval = -EINVAL;
  238. sctp_release_sock(sk);
  239. return retval;
  240. }
  241. static long sctp_get_port_local(struct sock *, union sctp_addr *);
  242. /* Verify this is a valid sockaddr. */
  243. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  244. union sctp_addr *addr, int len)
  245. {
  246. struct sctp_af *af;
  247. /* Check minimum size. */
  248. if (len < sizeof (struct sockaddr))
  249. return NULL;
  250. /* Does this PF support this AF? */
  251. if (!opt->pf->af_supported(addr->sa.sa_family, opt))
  252. return NULL;
  253. /* If we get this far, af is valid. */
  254. af = sctp_get_af_specific(addr->sa.sa_family);
  255. if (len < af->sockaddr_len)
  256. return NULL;
  257. return af;
  258. }
  259. /* Bind a local address either to an endpoint or to an association. */
  260. SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
  261. {
  262. struct sctp_sock *sp = sctp_sk(sk);
  263. struct sctp_endpoint *ep = sp->ep;
  264. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  265. struct sctp_af *af;
  266. unsigned short snum;
  267. int ret = 0;
  268. /* Common sockaddr verification. */
  269. af = sctp_sockaddr_af(sp, addr, len);
  270. if (!af) {
  271. SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
  272. sk, addr, len);
  273. return -EINVAL;
  274. }
  275. snum = ntohs(addr->v4.sin_port);
  276. SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
  277. ", port: %d, new port: %d, len: %d)\n",
  278. sk,
  279. addr,
  280. bp->port, snum,
  281. len);
  282. /* PF specific bind() address verification. */
  283. if (!sp->pf->bind_verify(sp, addr))
  284. return -EADDRNOTAVAIL;
  285. /* We must either be unbound, or bind to the same port. */
  286. if (bp->port && (snum != bp->port)) {
  287. SCTP_DEBUG_PRINTK("sctp_do_bind:"
  288. " New port %d does not match existing port "
  289. "%d.\n", snum, bp->port);
  290. return -EINVAL;
  291. }
  292. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  293. return -EACCES;
  294. /* Make sure we are allowed to bind here.
  295. * The function sctp_get_port_local() does duplicate address
  296. * detection.
  297. */
  298. if ((ret = sctp_get_port_local(sk, addr))) {
  299. if (ret == (long) sk) {
  300. /* This endpoint has a conflicting address. */
  301. return -EINVAL;
  302. } else {
  303. return -EADDRINUSE;
  304. }
  305. }
  306. /* Refresh ephemeral port. */
  307. if (!bp->port)
  308. bp->port = inet_sk(sk)->num;
  309. /* Add the address to the bind address list. */
  310. sctp_local_bh_disable();
  311. sctp_write_lock(&ep->base.addr_lock);
  312. /* Use GFP_ATOMIC since BHs are disabled. */
  313. addr->v4.sin_port = ntohs(addr->v4.sin_port);
  314. ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
  315. addr->v4.sin_port = htons(addr->v4.sin_port);
  316. sctp_write_unlock(&ep->base.addr_lock);
  317. sctp_local_bh_enable();
  318. /* Copy back into socket for getsockname() use. */
  319. if (!ret) {
  320. inet_sk(sk)->sport = htons(inet_sk(sk)->num);
  321. af->to_sk_saddr(addr, sk);
  322. }
  323. return ret;
  324. }
  325. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  326. *
  327. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  328. * at any one time. If a sender, after sending an ASCONF chunk, decides
  329. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  330. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  331. * subsequent ASCONF. Note this restriction binds each side, so at any
  332. * time two ASCONF may be in-transit on any given association (one sent
  333. * from each endpoint).
  334. */
  335. static int sctp_send_asconf(struct sctp_association *asoc,
  336. struct sctp_chunk *chunk)
  337. {
  338. int retval = 0;
  339. /* If there is an outstanding ASCONF chunk, queue it for later
  340. * transmission.
  341. */
  342. if (asoc->addip_last_asconf) {
  343. list_add_tail(&chunk->list, &asoc->addip_chunk_list);
  344. goto out;
  345. }
  346. /* Hold the chunk until an ASCONF_ACK is received. */
  347. sctp_chunk_hold(chunk);
  348. retval = sctp_primitive_ASCONF(asoc, chunk);
  349. if (retval)
  350. sctp_chunk_free(chunk);
  351. else
  352. asoc->addip_last_asconf = chunk;
  353. out:
  354. return retval;
  355. }
  356. /* Add a list of addresses as bind addresses to local endpoint or
  357. * association.
  358. *
  359. * Basically run through each address specified in the addrs/addrcnt
  360. * array/length pair, determine if it is IPv6 or IPv4 and call
  361. * sctp_do_bind() on it.
  362. *
  363. * If any of them fails, then the operation will be reversed and the
  364. * ones that were added will be removed.
  365. *
  366. * Only sctp_setsockopt_bindx() is supposed to call this function.
  367. */
  368. int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  369. {
  370. int cnt;
  371. int retval = 0;
  372. void *addr_buf;
  373. struct sockaddr *sa_addr;
  374. struct sctp_af *af;
  375. SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
  376. sk, addrs, addrcnt);
  377. addr_buf = addrs;
  378. for (cnt = 0; cnt < addrcnt; cnt++) {
  379. /* The list may contain either IPv4 or IPv6 address;
  380. * determine the address length for walking thru the list.
  381. */
  382. sa_addr = (struct sockaddr *)addr_buf;
  383. af = sctp_get_af_specific(sa_addr->sa_family);
  384. if (!af) {
  385. retval = -EINVAL;
  386. goto err_bindx_add;
  387. }
  388. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  389. af->sockaddr_len);
  390. addr_buf += af->sockaddr_len;
  391. err_bindx_add:
  392. if (retval < 0) {
  393. /* Failed. Cleanup the ones that have been added */
  394. if (cnt > 0)
  395. sctp_bindx_rem(sk, addrs, cnt);
  396. return retval;
  397. }
  398. }
  399. return retval;
  400. }
  401. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  402. * associations that are part of the endpoint indicating that a list of local
  403. * addresses are added to the endpoint.
  404. *
  405. * If any of the addresses is already in the bind address list of the
  406. * association, we do not send the chunk for that association. But it will not
  407. * affect other associations.
  408. *
  409. * Only sctp_setsockopt_bindx() is supposed to call this function.
  410. */
  411. static int sctp_send_asconf_add_ip(struct sock *sk,
  412. struct sockaddr *addrs,
  413. int addrcnt)
  414. {
  415. struct sctp_sock *sp;
  416. struct sctp_endpoint *ep;
  417. struct sctp_association *asoc;
  418. struct sctp_bind_addr *bp;
  419. struct sctp_chunk *chunk;
  420. struct sctp_sockaddr_entry *laddr;
  421. union sctp_addr *addr;
  422. void *addr_buf;
  423. struct sctp_af *af;
  424. struct list_head *pos;
  425. struct list_head *p;
  426. int i;
  427. int retval = 0;
  428. if (!sctp_addip_enable)
  429. return retval;
  430. sp = sctp_sk(sk);
  431. ep = sp->ep;
  432. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  433. __FUNCTION__, sk, addrs, addrcnt);
  434. list_for_each(pos, &ep->asocs) {
  435. asoc = list_entry(pos, struct sctp_association, asocs);
  436. if (!asoc->peer.asconf_capable)
  437. continue;
  438. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  439. continue;
  440. if (!sctp_state(asoc, ESTABLISHED))
  441. continue;
  442. /* Check if any address in the packed array of addresses is
  443. * in the bind address list of the association. If so,
  444. * do not send the asconf chunk to its peer, but continue with
  445. * other associations.
  446. */
  447. addr_buf = addrs;
  448. for (i = 0; i < addrcnt; i++) {
  449. addr = (union sctp_addr *)addr_buf;
  450. af = sctp_get_af_specific(addr->v4.sin_family);
  451. if (!af) {
  452. retval = -EINVAL;
  453. goto out;
  454. }
  455. if (sctp_assoc_lookup_laddr(asoc, addr))
  456. break;
  457. addr_buf += af->sockaddr_len;
  458. }
  459. if (i < addrcnt)
  460. continue;
  461. /* Use the first address in bind addr list of association as
  462. * Address Parameter of ASCONF CHUNK.
  463. */
  464. sctp_read_lock(&asoc->base.addr_lock);
  465. bp = &asoc->base.bind_addr;
  466. p = bp->address_list.next;
  467. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  468. sctp_read_unlock(&asoc->base.addr_lock);
  469. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  470. addrcnt, SCTP_PARAM_ADD_IP);
  471. if (!chunk) {
  472. retval = -ENOMEM;
  473. goto out;
  474. }
  475. retval = sctp_send_asconf(asoc, chunk);
  476. /* FIXME: After sending the add address ASCONF chunk, we
  477. * cannot append the address to the association's binding
  478. * address list, because the new address may be used as the
  479. * source of a message sent to the peer before the ASCONF
  480. * chunk is received by the peer. So we should wait until
  481. * ASCONF_ACK is received.
  482. */
  483. }
  484. out:
  485. return retval;
  486. }
  487. /* Remove a list of addresses from bind addresses list. Do not remove the
  488. * last address.
  489. *
  490. * Basically run through each address specified in the addrs/addrcnt
  491. * array/length pair, determine if it is IPv6 or IPv4 and call
  492. * sctp_del_bind() on it.
  493. *
  494. * If any of them fails, then the operation will be reversed and the
  495. * ones that were removed will be added back.
  496. *
  497. * At least one address has to be left; if only one address is
  498. * available, the operation will return -EBUSY.
  499. *
  500. * Only sctp_setsockopt_bindx() is supposed to call this function.
  501. */
  502. int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  503. {
  504. struct sctp_sock *sp = sctp_sk(sk);
  505. struct sctp_endpoint *ep = sp->ep;
  506. int cnt;
  507. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  508. int retval = 0;
  509. union sctp_addr saveaddr;
  510. void *addr_buf;
  511. struct sockaddr *sa_addr;
  512. struct sctp_af *af;
  513. SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
  514. sk, addrs, addrcnt);
  515. addr_buf = addrs;
  516. for (cnt = 0; cnt < addrcnt; cnt++) {
  517. /* If the bind address list is empty or if there is only one
  518. * bind address, there is nothing more to be removed (we need
  519. * at least one address here).
  520. */
  521. if (list_empty(&bp->address_list) ||
  522. (sctp_list_single_entry(&bp->address_list))) {
  523. retval = -EBUSY;
  524. goto err_bindx_rem;
  525. }
  526. /* The list may contain either IPv4 or IPv6 address;
  527. * determine the address length to copy the address to
  528. * saveaddr.
  529. */
  530. sa_addr = (struct sockaddr *)addr_buf;
  531. af = sctp_get_af_specific(sa_addr->sa_family);
  532. if (!af) {
  533. retval = -EINVAL;
  534. goto err_bindx_rem;
  535. }
  536. memcpy(&saveaddr, sa_addr, af->sockaddr_len);
  537. saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
  538. if (saveaddr.v4.sin_port != bp->port) {
  539. retval = -EINVAL;
  540. goto err_bindx_rem;
  541. }
  542. /* FIXME - There is probably a need to check if sk->sk_saddr and
  543. * sk->sk_rcv_addr are currently set to one of the addresses to
  544. * be removed. This is something which needs to be looked into
  545. * when we are fixing the outstanding issues with multi-homing
  546. * socket routing and failover schemes. Refer to comments in
  547. * sctp_do_bind(). -daisy
  548. */
  549. sctp_local_bh_disable();
  550. sctp_write_lock(&ep->base.addr_lock);
  551. retval = sctp_del_bind_addr(bp, &saveaddr);
  552. sctp_write_unlock(&ep->base.addr_lock);
  553. sctp_local_bh_enable();
  554. addr_buf += af->sockaddr_len;
  555. err_bindx_rem:
  556. if (retval < 0) {
  557. /* Failed. Add the ones that has been removed back */
  558. if (cnt > 0)
  559. sctp_bindx_add(sk, addrs, cnt);
  560. return retval;
  561. }
  562. }
  563. return retval;
  564. }
  565. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  566. * the associations that are part of the endpoint indicating that a list of
  567. * local addresses are removed from the endpoint.
  568. *
  569. * If any of the addresses is already in the bind address list of the
  570. * association, we do not send the chunk for that association. But it will not
  571. * affect other associations.
  572. *
  573. * Only sctp_setsockopt_bindx() is supposed to call this function.
  574. */
  575. static int sctp_send_asconf_del_ip(struct sock *sk,
  576. struct sockaddr *addrs,
  577. int addrcnt)
  578. {
  579. struct sctp_sock *sp;
  580. struct sctp_endpoint *ep;
  581. struct sctp_association *asoc;
  582. struct sctp_bind_addr *bp;
  583. struct sctp_chunk *chunk;
  584. union sctp_addr *laddr;
  585. void *addr_buf;
  586. struct sctp_af *af;
  587. struct list_head *pos;
  588. int i;
  589. int retval = 0;
  590. if (!sctp_addip_enable)
  591. return retval;
  592. sp = sctp_sk(sk);
  593. ep = sp->ep;
  594. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  595. __FUNCTION__, sk, addrs, addrcnt);
  596. list_for_each(pos, &ep->asocs) {
  597. asoc = list_entry(pos, struct sctp_association, asocs);
  598. if (!asoc->peer.asconf_capable)
  599. continue;
  600. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  601. continue;
  602. if (!sctp_state(asoc, ESTABLISHED))
  603. continue;
  604. /* Check if any address in the packed array of addresses is
  605. * not present in the bind address list of the association.
  606. * If so, do not send the asconf chunk to its peer, but
  607. * continue with other associations.
  608. */
  609. addr_buf = addrs;
  610. for (i = 0; i < addrcnt; i++) {
  611. laddr = (union sctp_addr *)addr_buf;
  612. af = sctp_get_af_specific(laddr->v4.sin_family);
  613. if (!af) {
  614. retval = -EINVAL;
  615. goto out;
  616. }
  617. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  618. break;
  619. addr_buf += af->sockaddr_len;
  620. }
  621. if (i < addrcnt)
  622. continue;
  623. /* Find one address in the association's bind address list
  624. * that is not in the packed array of addresses. This is to
  625. * make sure that we do not delete all the addresses in the
  626. * association.
  627. */
  628. sctp_read_lock(&asoc->base.addr_lock);
  629. bp = &asoc->base.bind_addr;
  630. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  631. addrcnt, sp);
  632. sctp_read_unlock(&asoc->base.addr_lock);
  633. if (!laddr)
  634. continue;
  635. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  636. SCTP_PARAM_DEL_IP);
  637. if (!chunk) {
  638. retval = -ENOMEM;
  639. goto out;
  640. }
  641. retval = sctp_send_asconf(asoc, chunk);
  642. /* FIXME: After sending the delete address ASCONF chunk, we
  643. * cannot remove the addresses from the association's bind
  644. * address list, because there maybe some packet send to
  645. * the delete addresses, so we should wait until ASCONF_ACK
  646. * packet is received.
  647. */
  648. }
  649. out:
  650. return retval;
  651. }
  652. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  653. *
  654. * API 8.1
  655. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  656. * int flags);
  657. *
  658. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  659. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  660. * or IPv6 addresses.
  661. *
  662. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  663. * Section 3.1.2 for this usage.
  664. *
  665. * addrs is a pointer to an array of one or more socket addresses. Each
  666. * address is contained in its appropriate structure (i.e. struct
  667. * sockaddr_in or struct sockaddr_in6) the family of the address type
  668. * must be used to distengish the address length (note that this
  669. * representation is termed a "packed array" of addresses). The caller
  670. * specifies the number of addresses in the array with addrcnt.
  671. *
  672. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  673. * -1, and sets errno to the appropriate error code.
  674. *
  675. * For SCTP, the port given in each socket address must be the same, or
  676. * sctp_bindx() will fail, setting errno to EINVAL.
  677. *
  678. * The flags parameter is formed from the bitwise OR of zero or more of
  679. * the following currently defined flags:
  680. *
  681. * SCTP_BINDX_ADD_ADDR
  682. *
  683. * SCTP_BINDX_REM_ADDR
  684. *
  685. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  686. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  687. * addresses from the association. The two flags are mutually exclusive;
  688. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  689. * not remove all addresses from an association; sctp_bindx() will
  690. * reject such an attempt with EINVAL.
  691. *
  692. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  693. * additional addresses with an endpoint after calling bind(). Or use
  694. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  695. * socket is associated with so that no new association accepted will be
  696. * associated with those addresses. If the endpoint supports dynamic
  697. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  698. * endpoint to send the appropriate message to the peer to change the
  699. * peers address lists.
  700. *
  701. * Adding and removing addresses from a connected association is
  702. * optional functionality. Implementations that do not support this
  703. * functionality should return EOPNOTSUPP.
  704. *
  705. * Basically do nothing but copying the addresses from user to kernel
  706. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  707. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
  708. * from userspace.
  709. *
  710. * We don't use copy_from_user() for optimization: we first do the
  711. * sanity checks (buffer size -fast- and access check-healthy
  712. * pointer); if all of those succeed, then we can alloc the memory
  713. * (expensive operation) needed to copy the data to kernel. Then we do
  714. * the copying without checking the user space area
  715. * (__copy_from_user()).
  716. *
  717. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  718. * it.
  719. *
  720. * sk The sk of the socket
  721. * addrs The pointer to the addresses in user land
  722. * addrssize Size of the addrs buffer
  723. * op Operation to perform (add or remove, see the flags of
  724. * sctp_bindx)
  725. *
  726. * Returns 0 if ok, <0 errno code on error.
  727. */
  728. SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
  729. struct sockaddr __user *addrs,
  730. int addrs_size, int op)
  731. {
  732. struct sockaddr *kaddrs;
  733. int err;
  734. int addrcnt = 0;
  735. int walk_size = 0;
  736. struct sockaddr *sa_addr;
  737. void *addr_buf;
  738. struct sctp_af *af;
  739. SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
  740. " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
  741. if (unlikely(addrs_size <= 0))
  742. return -EINVAL;
  743. /* Check the user passed a healthy pointer. */
  744. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  745. return -EFAULT;
  746. /* Alloc space for the address array in kernel memory. */
  747. kaddrs = kmalloc(addrs_size, GFP_KERNEL);
  748. if (unlikely(!kaddrs))
  749. return -ENOMEM;
  750. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  751. kfree(kaddrs);
  752. return -EFAULT;
  753. }
  754. /* Walk through the addrs buffer and count the number of addresses. */
  755. addr_buf = kaddrs;
  756. while (walk_size < addrs_size) {
  757. sa_addr = (struct sockaddr *)addr_buf;
  758. af = sctp_get_af_specific(sa_addr->sa_family);
  759. /* If the address family is not supported or if this address
  760. * causes the address buffer to overflow return EINVAL.
  761. */
  762. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  763. kfree(kaddrs);
  764. return -EINVAL;
  765. }
  766. addrcnt++;
  767. addr_buf += af->sockaddr_len;
  768. walk_size += af->sockaddr_len;
  769. }
  770. /* Do the work. */
  771. switch (op) {
  772. case SCTP_BINDX_ADD_ADDR:
  773. err = sctp_bindx_add(sk, kaddrs, addrcnt);
  774. if (err)
  775. goto out;
  776. err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
  777. break;
  778. case SCTP_BINDX_REM_ADDR:
  779. err = sctp_bindx_rem(sk, kaddrs, addrcnt);
  780. if (err)
  781. goto out;
  782. err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
  783. break;
  784. default:
  785. err = -EINVAL;
  786. break;
  787. };
  788. out:
  789. kfree(kaddrs);
  790. return err;
  791. }
  792. /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
  793. *
  794. * Common routine for handling connect() and sctp_connectx().
  795. * Connect will come in with just a single address.
  796. */
  797. static int __sctp_connect(struct sock* sk,
  798. struct sockaddr *kaddrs,
  799. int addrs_size)
  800. {
  801. struct sctp_sock *sp;
  802. struct sctp_endpoint *ep;
  803. struct sctp_association *asoc = NULL;
  804. struct sctp_association *asoc2;
  805. struct sctp_transport *transport;
  806. union sctp_addr to;
  807. struct sctp_af *af;
  808. sctp_scope_t scope;
  809. long timeo;
  810. int err = 0;
  811. int addrcnt = 0;
  812. int walk_size = 0;
  813. struct sockaddr *sa_addr;
  814. void *addr_buf;
  815. sp = sctp_sk(sk);
  816. ep = sp->ep;
  817. /* connect() cannot be done on a socket that is already in ESTABLISHED
  818. * state - UDP-style peeled off socket or a TCP-style socket that
  819. * is already connected.
  820. * It cannot be done even on a TCP-style listening socket.
  821. */
  822. if (sctp_sstate(sk, ESTABLISHED) ||
  823. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
  824. err = -EISCONN;
  825. goto out_free;
  826. }
  827. /* Walk through the addrs buffer and count the number of addresses. */
  828. addr_buf = kaddrs;
  829. while (walk_size < addrs_size) {
  830. sa_addr = (struct sockaddr *)addr_buf;
  831. af = sctp_get_af_specific(sa_addr->sa_family);
  832. /* If the address family is not supported or if this address
  833. * causes the address buffer to overflow return EINVAL.
  834. */
  835. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  836. err = -EINVAL;
  837. goto out_free;
  838. }
  839. err = sctp_verify_addr(sk, (union sctp_addr *)sa_addr,
  840. af->sockaddr_len);
  841. if (err)
  842. goto out_free;
  843. memcpy(&to, sa_addr, af->sockaddr_len);
  844. to.v4.sin_port = ntohs(to.v4.sin_port);
  845. /* Check if there already is a matching association on the
  846. * endpoint (other than the one created here).
  847. */
  848. asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  849. if (asoc2 && asoc2 != asoc) {
  850. if (asoc2->state >= SCTP_STATE_ESTABLISHED)
  851. err = -EISCONN;
  852. else
  853. err = -EALREADY;
  854. goto out_free;
  855. }
  856. /* If we could not find a matching association on the endpoint,
  857. * make sure that there is no peeled-off association matching
  858. * the peer address even on another socket.
  859. */
  860. if (sctp_endpoint_is_peeled_off(ep, &to)) {
  861. err = -EADDRNOTAVAIL;
  862. goto out_free;
  863. }
  864. if (!asoc) {
  865. /* If a bind() or sctp_bindx() is not called prior to
  866. * an sctp_connectx() call, the system picks an
  867. * ephemeral port and will choose an address set
  868. * equivalent to binding with a wildcard address.
  869. */
  870. if (!ep->base.bind_addr.port) {
  871. if (sctp_autobind(sk)) {
  872. err = -EAGAIN;
  873. goto out_free;
  874. }
  875. } else {
  876. /*
  877. * If an unprivileged user inherits a 1-many
  878. * style socket with open associations on a
  879. * privileged port, it MAY be permitted to
  880. * accept new associations, but it SHOULD NOT
  881. * be permitted to open new associations.
  882. */
  883. if (ep->base.bind_addr.port < PROT_SOCK &&
  884. !capable(CAP_NET_BIND_SERVICE)) {
  885. err = -EACCES;
  886. goto out_free;
  887. }
  888. }
  889. scope = sctp_scope(&to);
  890. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  891. if (!asoc) {
  892. err = -ENOMEM;
  893. goto out_free;
  894. }
  895. }
  896. /* Prime the peer's transport structures. */
  897. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
  898. SCTP_UNKNOWN);
  899. if (!transport) {
  900. err = -ENOMEM;
  901. goto out_free;
  902. }
  903. addrcnt++;
  904. addr_buf += af->sockaddr_len;
  905. walk_size += af->sockaddr_len;
  906. }
  907. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  908. if (err < 0) {
  909. goto out_free;
  910. }
  911. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  912. if (err < 0) {
  913. goto out_free;
  914. }
  915. /* Initialize sk's dport and daddr for getpeername() */
  916. inet_sk(sk)->dport = htons(asoc->peer.port);
  917. af = sctp_get_af_specific(to.sa.sa_family);
  918. af->to_sk_daddr(&to, sk);
  919. sk->sk_err = 0;
  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 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. continue;
  1088. }
  1089. }
  1090. if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)
  1091. sctp_primitive_ABORT(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. __u32 path_sum = 0;
  2218. int paths = 0;
  2219. struct list_head *pos;
  2220. struct sctp_transport *peer_addr;
  2221. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2222. peer_addr = list_entry(pos,
  2223. struct sctp_transport,
  2224. transports);
  2225. path_sum += peer_addr->pathmaxrxt;
  2226. paths++;
  2227. }
  2228. /* Only validate asocmaxrxt if we have more then
  2229. * one path/transport. We do this because path
  2230. * retransmissions are only counted when we have more
  2231. * then one path.
  2232. */
  2233. if (paths > 1 &&
  2234. assocparams.sasoc_asocmaxrxt > path_sum)
  2235. return -EINVAL;
  2236. asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
  2237. }
  2238. if (assocparams.sasoc_cookie_life != 0) {
  2239. asoc->cookie_life.tv_sec =
  2240. assocparams.sasoc_cookie_life / 1000;
  2241. asoc->cookie_life.tv_usec =
  2242. (assocparams.sasoc_cookie_life % 1000)
  2243. * 1000;
  2244. }
  2245. } else {
  2246. /* Set the values to the endpoint */
  2247. struct sctp_sock *sp = sctp_sk(sk);
  2248. if (assocparams.sasoc_asocmaxrxt != 0)
  2249. sp->assocparams.sasoc_asocmaxrxt =
  2250. assocparams.sasoc_asocmaxrxt;
  2251. if (assocparams.sasoc_cookie_life != 0)
  2252. sp->assocparams.sasoc_cookie_life =
  2253. assocparams.sasoc_cookie_life;
  2254. }
  2255. return 0;
  2256. }
  2257. /*
  2258. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  2259. *
  2260. * This socket option is a boolean flag which turns on or off mapped V4
  2261. * addresses. If this option is turned on and the socket is type
  2262. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  2263. * If this option is turned off, then no mapping will be done of V4
  2264. * addresses and a user will receive both PF_INET6 and PF_INET type
  2265. * addresses on the socket.
  2266. */
  2267. static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
  2268. {
  2269. int val;
  2270. struct sctp_sock *sp = sctp_sk(sk);
  2271. if (optlen < sizeof(int))
  2272. return -EINVAL;
  2273. if (get_user(val, (int __user *)optval))
  2274. return -EFAULT;
  2275. if (val)
  2276. sp->v4mapped = 1;
  2277. else
  2278. sp->v4mapped = 0;
  2279. return 0;
  2280. }
  2281. /*
  2282. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  2283. *
  2284. * This socket option specifies the maximum size to put in any outgoing
  2285. * SCTP chunk. If a message is larger than this size it will be
  2286. * fragmented by SCTP into the specified size. Note that the underlying
  2287. * SCTP implementation may fragment into smaller sized chunks when the
  2288. * PMTU of the underlying association is smaller than the value set by
  2289. * the user.
  2290. */
  2291. static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
  2292. {
  2293. struct sctp_association *asoc;
  2294. struct list_head *pos;
  2295. struct sctp_sock *sp = sctp_sk(sk);
  2296. int val;
  2297. if (optlen < sizeof(int))
  2298. return -EINVAL;
  2299. if (get_user(val, (int __user *)optval))
  2300. return -EFAULT;
  2301. if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
  2302. return -EINVAL;
  2303. sp->user_frag = val;
  2304. /* Update the frag_point of the existing associations. */
  2305. list_for_each(pos, &(sp->ep->asocs)) {
  2306. asoc = list_entry(pos, struct sctp_association, asocs);
  2307. asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
  2308. }
  2309. return 0;
  2310. }
  2311. /*
  2312. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  2313. *
  2314. * Requests that the peer mark the enclosed address as the association
  2315. * primary. The enclosed address must be one of the association's
  2316. * locally bound addresses. The following structure is used to make a
  2317. * set primary request:
  2318. */
  2319. static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
  2320. int optlen)
  2321. {
  2322. struct sctp_sock *sp;
  2323. struct sctp_endpoint *ep;
  2324. struct sctp_association *asoc = NULL;
  2325. struct sctp_setpeerprim prim;
  2326. struct sctp_chunk *chunk;
  2327. int err;
  2328. sp = sctp_sk(sk);
  2329. ep = sp->ep;
  2330. if (!sctp_addip_enable)
  2331. return -EPERM;
  2332. if (optlen != sizeof(struct sctp_setpeerprim))
  2333. return -EINVAL;
  2334. if (copy_from_user(&prim, optval, optlen))
  2335. return -EFAULT;
  2336. asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
  2337. if (!asoc)
  2338. return -EINVAL;
  2339. if (!asoc->peer.asconf_capable)
  2340. return -EPERM;
  2341. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  2342. return -EPERM;
  2343. if (!sctp_state(asoc, ESTABLISHED))
  2344. return -ENOTCONN;
  2345. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
  2346. return -EADDRNOTAVAIL;
  2347. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  2348. chunk = sctp_make_asconf_set_prim(asoc,
  2349. (union sctp_addr *)&prim.sspp_addr);
  2350. if (!chunk)
  2351. return -ENOMEM;
  2352. err = sctp_send_asconf(asoc, chunk);
  2353. SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
  2354. return err;
  2355. }
  2356. static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
  2357. int optlen)
  2358. {
  2359. struct sctp_setadaption adaption;
  2360. if (optlen != sizeof(struct sctp_setadaption))
  2361. return -EINVAL;
  2362. if (copy_from_user(&adaption, optval, optlen))
  2363. return -EFAULT;
  2364. sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
  2365. return 0;
  2366. }
  2367. /* API 6.2 setsockopt(), getsockopt()
  2368. *
  2369. * Applications use setsockopt() and getsockopt() to set or retrieve
  2370. * socket options. Socket options are used to change the default
  2371. * behavior of sockets calls. They are described in Section 7.
  2372. *
  2373. * The syntax is:
  2374. *
  2375. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  2376. * int __user *optlen);
  2377. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  2378. * int optlen);
  2379. *
  2380. * sd - the socket descript.
  2381. * level - set to IPPROTO_SCTP for all SCTP options.
  2382. * optname - the option name.
  2383. * optval - the buffer to store the value of the option.
  2384. * optlen - the size of the buffer.
  2385. */
  2386. SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
  2387. char __user *optval, int optlen)
  2388. {
  2389. int retval = 0;
  2390. SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
  2391. sk, optname);
  2392. /* I can hardly begin to describe how wrong this is. This is
  2393. * so broken as to be worse than useless. The API draft
  2394. * REALLY is NOT helpful here... I am not convinced that the
  2395. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  2396. * are at all well-founded.
  2397. */
  2398. if (level != SOL_SCTP) {
  2399. struct sctp_af *af = sctp_sk(sk)->pf->af;
  2400. retval = af->setsockopt(sk, level, optname, optval, optlen);
  2401. goto out_nounlock;
  2402. }
  2403. sctp_lock_sock(sk);
  2404. switch (optname) {
  2405. case SCTP_SOCKOPT_BINDX_ADD:
  2406. /* 'optlen' is the size of the addresses buffer. */
  2407. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  2408. optlen, SCTP_BINDX_ADD_ADDR);
  2409. break;
  2410. case SCTP_SOCKOPT_BINDX_REM:
  2411. /* 'optlen' is the size of the addresses buffer. */
  2412. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  2413. optlen, SCTP_BINDX_REM_ADDR);
  2414. break;
  2415. case SCTP_SOCKOPT_CONNECTX:
  2416. /* 'optlen' is the size of the addresses buffer. */
  2417. retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
  2418. optlen);
  2419. break;
  2420. case SCTP_DISABLE_FRAGMENTS:
  2421. retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
  2422. break;
  2423. case SCTP_EVENTS:
  2424. retval = sctp_setsockopt_events(sk, optval, optlen);
  2425. break;
  2426. case SCTP_AUTOCLOSE:
  2427. retval = sctp_setsockopt_autoclose(sk, optval, optlen);
  2428. break;
  2429. case SCTP_PEER_ADDR_PARAMS:
  2430. retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
  2431. break;
  2432. case SCTP_DELAYED_ACK_TIME:
  2433. retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
  2434. break;
  2435. case SCTP_INITMSG:
  2436. retval = sctp_setsockopt_initmsg(sk, optval, optlen);
  2437. break;
  2438. case SCTP_DEFAULT_SEND_PARAM:
  2439. retval = sctp_setsockopt_default_send_param(sk, optval,
  2440. optlen);
  2441. break;
  2442. case SCTP_PRIMARY_ADDR:
  2443. retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
  2444. break;
  2445. case SCTP_SET_PEER_PRIMARY_ADDR:
  2446. retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
  2447. break;
  2448. case SCTP_NODELAY:
  2449. retval = sctp_setsockopt_nodelay(sk, optval, optlen);
  2450. break;
  2451. case SCTP_RTOINFO:
  2452. retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
  2453. break;
  2454. case SCTP_ASSOCINFO:
  2455. retval = sctp_setsockopt_associnfo(sk, optval, optlen);
  2456. break;
  2457. case SCTP_I_WANT_MAPPED_V4_ADDR:
  2458. retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
  2459. break;
  2460. case SCTP_MAXSEG:
  2461. retval = sctp_setsockopt_maxseg(sk, optval, optlen);
  2462. break;
  2463. case SCTP_ADAPTION_LAYER:
  2464. retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
  2465. break;
  2466. default:
  2467. retval = -ENOPROTOOPT;
  2468. break;
  2469. };
  2470. sctp_release_sock(sk);
  2471. out_nounlock:
  2472. return retval;
  2473. }
  2474. /* API 3.1.6 connect() - UDP Style Syntax
  2475. *
  2476. * An application may use the connect() call in the UDP model to initiate an
  2477. * association without sending data.
  2478. *
  2479. * The syntax is:
  2480. *
  2481. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  2482. *
  2483. * sd: the socket descriptor to have a new association added to.
  2484. *
  2485. * nam: the address structure (either struct sockaddr_in or struct
  2486. * sockaddr_in6 defined in RFC2553 [7]).
  2487. *
  2488. * len: the size of the address.
  2489. */
  2490. SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
  2491. int addr_len)
  2492. {
  2493. int err = 0;
  2494. struct sctp_af *af;
  2495. sctp_lock_sock(sk);
  2496. SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
  2497. __FUNCTION__, sk, addr, addr_len);
  2498. /* Validate addr_len before calling common connect/connectx routine. */
  2499. af = sctp_get_af_specific(addr->sa_family);
  2500. if (!af || addr_len < af->sockaddr_len) {
  2501. err = -EINVAL;
  2502. } else {
  2503. /* Pass correct addr len to common routine (so it knows there
  2504. * is only one address being passed.
  2505. */
  2506. err = __sctp_connect(sk, addr, af->sockaddr_len);
  2507. }
  2508. sctp_release_sock(sk);
  2509. return err;
  2510. }
  2511. /* FIXME: Write comments. */
  2512. SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
  2513. {
  2514. return -EOPNOTSUPP; /* STUB */
  2515. }
  2516. /* 4.1.4 accept() - TCP Style Syntax
  2517. *
  2518. * Applications use accept() call to remove an established SCTP
  2519. * association from the accept queue of the endpoint. A new socket
  2520. * descriptor will be returned from accept() to represent the newly
  2521. * formed association.
  2522. */
  2523. SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
  2524. {
  2525. struct sctp_sock *sp;
  2526. struct sctp_endpoint *ep;
  2527. struct sock *newsk = NULL;
  2528. struct sctp_association *asoc;
  2529. long timeo;
  2530. int error = 0;
  2531. sctp_lock_sock(sk);
  2532. sp = sctp_sk(sk);
  2533. ep = sp->ep;
  2534. if (!sctp_style(sk, TCP)) {
  2535. error = -EOPNOTSUPP;
  2536. goto out;
  2537. }
  2538. if (!sctp_sstate(sk, LISTENING)) {
  2539. error = -EINVAL;
  2540. goto out;
  2541. }
  2542. timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
  2543. error = sctp_wait_for_accept(sk, timeo);
  2544. if (error)
  2545. goto out;
  2546. /* We treat the list of associations on the endpoint as the accept
  2547. * queue and pick the first association on the list.
  2548. */
  2549. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  2550. newsk = sp->pf->create_accept_sk(sk, asoc);
  2551. if (!newsk) {
  2552. error = -ENOMEM;
  2553. goto out;
  2554. }
  2555. /* Populate the fields of the newsk from the oldsk and migrate the
  2556. * asoc to the newsk.
  2557. */
  2558. sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  2559. out:
  2560. sctp_release_sock(sk);
  2561. *err = error;
  2562. return newsk;
  2563. }
  2564. /* The SCTP ioctl handler. */
  2565. SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  2566. {
  2567. return -ENOIOCTLCMD;
  2568. }
  2569. /* This is the function which gets called during socket creation to
  2570. * initialized the SCTP-specific portion of the sock.
  2571. * The sock structure should already be zero-filled memory.
  2572. */
  2573. SCTP_STATIC int sctp_init_sock(struct sock *sk)
  2574. {
  2575. struct sctp_endpoint *ep;
  2576. struct sctp_sock *sp;
  2577. SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
  2578. sp = sctp_sk(sk);
  2579. /* Initialize the SCTP per socket area. */
  2580. switch (sk->sk_type) {
  2581. case SOCK_SEQPACKET:
  2582. sp->type = SCTP_SOCKET_UDP;
  2583. break;
  2584. case SOCK_STREAM:
  2585. sp->type = SCTP_SOCKET_TCP;
  2586. break;
  2587. default:
  2588. return -ESOCKTNOSUPPORT;
  2589. }
  2590. /* Initialize default send parameters. These parameters can be
  2591. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  2592. */
  2593. sp->default_stream = 0;
  2594. sp->default_ppid = 0;
  2595. sp->default_flags = 0;
  2596. sp->default_context = 0;
  2597. sp->default_timetolive = 0;
  2598. /* Initialize default setup parameters. These parameters
  2599. * can be modified with the SCTP_INITMSG socket option or
  2600. * overridden by the SCTP_INIT CMSG.
  2601. */
  2602. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  2603. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  2604. sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
  2605. sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
  2606. /* Initialize default RTO related parameters. These parameters can
  2607. * be modified for with the SCTP_RTOINFO socket option.
  2608. */
  2609. sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
  2610. sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
  2611. sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
  2612. /* Initialize default association related parameters. These parameters
  2613. * can be modified with the SCTP_ASSOCINFO socket option.
  2614. */
  2615. sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
  2616. sp->assocparams.sasoc_number_peer_destinations = 0;
  2617. sp->assocparams.sasoc_peer_rwnd = 0;
  2618. sp->assocparams.sasoc_local_rwnd = 0;
  2619. sp->assocparams.sasoc_cookie_life =
  2620. jiffies_to_msecs(sctp_valid_cookie_life);
  2621. /* Initialize default event subscriptions. By default, all the
  2622. * options are off.
  2623. */
  2624. memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
  2625. /* Default Peer Address Parameters. These defaults can
  2626. * be modified via SCTP_PEER_ADDR_PARAMS
  2627. */
  2628. sp->hbinterval = jiffies_to_msecs(sctp_hb_interval);
  2629. sp->pathmaxrxt = sctp_max_retrans_path;
  2630. sp->pathmtu = 0; // allow default discovery
  2631. sp->sackdelay = jiffies_to_msecs(sctp_sack_timeout);
  2632. sp->param_flags = SPP_HB_ENABLE |
  2633. SPP_PMTUD_ENABLE |
  2634. SPP_SACKDELAY_ENABLE;
  2635. /* If enabled no SCTP message fragmentation will be performed.
  2636. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  2637. */
  2638. sp->disable_fragments = 0;
  2639. /* Turn on/off any Nagle-like algorithm. */
  2640. sp->nodelay = 1;
  2641. /* Enable by default. */
  2642. sp->v4mapped = 1;
  2643. /* Auto-close idle associations after the configured
  2644. * number of seconds. A value of 0 disables this
  2645. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  2646. * for UDP-style sockets only.
  2647. */
  2648. sp->autoclose = 0;
  2649. /* User specified fragmentation limit. */
  2650. sp->user_frag = 0;
  2651. sp->adaption_ind = 0;
  2652. sp->pf = sctp_get_pf_specific(sk->sk_family);
  2653. /* Control variables for partial data delivery. */
  2654. sp->pd_mode = 0;
  2655. skb_queue_head_init(&sp->pd_lobby);
  2656. /* Create a per socket endpoint structure. Even if we
  2657. * change the data structure relationships, this may still
  2658. * be useful for storing pre-connect address information.
  2659. */
  2660. ep = sctp_endpoint_new(sk, GFP_KERNEL);
  2661. if (!ep)
  2662. return -ENOMEM;
  2663. sp->ep = ep;
  2664. sp->hmac = NULL;
  2665. SCTP_DBG_OBJCNT_INC(sock);
  2666. return 0;
  2667. }
  2668. /* Cleanup any SCTP per socket resources. */
  2669. SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
  2670. {
  2671. struct sctp_endpoint *ep;
  2672. SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
  2673. /* Release our hold on the endpoint. */
  2674. ep = sctp_sk(sk)->ep;
  2675. sctp_endpoint_free(ep);
  2676. return 0;
  2677. }
  2678. /* API 4.1.7 shutdown() - TCP Style Syntax
  2679. * int shutdown(int socket, int how);
  2680. *
  2681. * sd - the socket descriptor of the association to be closed.
  2682. * how - Specifies the type of shutdown. The values are
  2683. * as follows:
  2684. * SHUT_RD
  2685. * Disables further receive operations. No SCTP
  2686. * protocol action is taken.
  2687. * SHUT_WR
  2688. * Disables further send operations, and initiates
  2689. * the SCTP shutdown sequence.
  2690. * SHUT_RDWR
  2691. * Disables further send and receive operations
  2692. * and initiates the SCTP shutdown sequence.
  2693. */
  2694. SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
  2695. {
  2696. struct sctp_endpoint *ep;
  2697. struct sctp_association *asoc;
  2698. if (!sctp_style(sk, TCP))
  2699. return;
  2700. if (how & SEND_SHUTDOWN) {
  2701. ep = sctp_sk(sk)->ep;
  2702. if (!list_empty(&ep->asocs)) {
  2703. asoc = list_entry(ep->asocs.next,
  2704. struct sctp_association, asocs);
  2705. sctp_primitive_SHUTDOWN(asoc, NULL);
  2706. }
  2707. }
  2708. }
  2709. /* 7.2.1 Association Status (SCTP_STATUS)
  2710. * Applications can retrieve current status information about an
  2711. * association, including association state, peer receiver window size,
  2712. * number of unacked data chunks, and number of data chunks pending
  2713. * receipt. This information is read-only.
  2714. */
  2715. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  2716. char __user *optval,
  2717. int __user *optlen)
  2718. {
  2719. struct sctp_status status;
  2720. struct sctp_association *asoc = NULL;
  2721. struct sctp_transport *transport;
  2722. sctp_assoc_t associd;
  2723. int retval = 0;
  2724. if (len != sizeof(status)) {
  2725. retval = -EINVAL;
  2726. goto out;
  2727. }
  2728. if (copy_from_user(&status, optval, sizeof(status))) {
  2729. retval = -EFAULT;
  2730. goto out;
  2731. }
  2732. associd = status.sstat_assoc_id;
  2733. asoc = sctp_id2assoc(sk, associd);
  2734. if (!asoc) {
  2735. retval = -EINVAL;
  2736. goto out;
  2737. }
  2738. transport = asoc->peer.primary_path;
  2739. status.sstat_assoc_id = sctp_assoc2id(asoc);
  2740. status.sstat_state = asoc->state;
  2741. status.sstat_rwnd = asoc->peer.rwnd;
  2742. status.sstat_unackdata = asoc->unack_data;
  2743. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  2744. status.sstat_instrms = asoc->c.sinit_max_instreams;
  2745. status.sstat_outstrms = asoc->c.sinit_num_ostreams;
  2746. status.sstat_fragmentation_point = asoc->frag_point;
  2747. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  2748. memcpy(&status.sstat_primary.spinfo_address,
  2749. &(transport->ipaddr), sizeof(union sctp_addr));
  2750. /* Map ipv4 address into v4-mapped-on-v6 address. */
  2751. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  2752. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  2753. status.sstat_primary.spinfo_state = transport->state;
  2754. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  2755. status.sstat_primary.spinfo_srtt = transport->srtt;
  2756. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  2757. status.sstat_primary.spinfo_mtu = transport->pathmtu;
  2758. if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
  2759. status.sstat_primary.spinfo_state = SCTP_ACTIVE;
  2760. if (put_user(len, optlen)) {
  2761. retval = -EFAULT;
  2762. goto out;
  2763. }
  2764. SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
  2765. len, status.sstat_state, status.sstat_rwnd,
  2766. status.sstat_assoc_id);
  2767. if (copy_to_user(optval, &status, len)) {
  2768. retval = -EFAULT;
  2769. goto out;
  2770. }
  2771. out:
  2772. return (retval);
  2773. }
  2774. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  2775. *
  2776. * Applications can retrieve information about a specific peer address
  2777. * of an association, including its reachability state, congestion
  2778. * window, and retransmission timer values. This information is
  2779. * read-only.
  2780. */
  2781. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  2782. char __user *optval,
  2783. int __user *optlen)
  2784. {
  2785. struct sctp_paddrinfo pinfo;
  2786. struct sctp_transport *transport;
  2787. int retval = 0;
  2788. if (len != sizeof(pinfo)) {
  2789. retval = -EINVAL;
  2790. goto out;
  2791. }
  2792. if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
  2793. retval = -EFAULT;
  2794. goto out;
  2795. }
  2796. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  2797. pinfo.spinfo_assoc_id);
  2798. if (!transport)
  2799. return -EINVAL;
  2800. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  2801. pinfo.spinfo_state = transport->state;
  2802. pinfo.spinfo_cwnd = transport->cwnd;
  2803. pinfo.spinfo_srtt = transport->srtt;
  2804. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  2805. pinfo.spinfo_mtu = transport->pathmtu;
  2806. if (pinfo.spinfo_state == SCTP_UNKNOWN)
  2807. pinfo.spinfo_state = SCTP_ACTIVE;
  2808. if (put_user(len, optlen)) {
  2809. retval = -EFAULT;
  2810. goto out;
  2811. }
  2812. if (copy_to_user(optval, &pinfo, len)) {
  2813. retval = -EFAULT;
  2814. goto out;
  2815. }
  2816. out:
  2817. return (retval);
  2818. }
  2819. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  2820. *
  2821. * This option is a on/off flag. If enabled no SCTP message
  2822. * fragmentation will be performed. Instead if a message being sent
  2823. * exceeds the current PMTU size, the message will NOT be sent and
  2824. * instead a error will be indicated to the user.
  2825. */
  2826. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  2827. char __user *optval, int __user *optlen)
  2828. {
  2829. int val;
  2830. if (len < sizeof(int))
  2831. return -EINVAL;
  2832. len = sizeof(int);
  2833. val = (sctp_sk(sk)->disable_fragments == 1);
  2834. if (put_user(len, optlen))
  2835. return -EFAULT;
  2836. if (copy_to_user(optval, &val, len))
  2837. return -EFAULT;
  2838. return 0;
  2839. }
  2840. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  2841. *
  2842. * This socket option is used to specify various notifications and
  2843. * ancillary data the user wishes to receive.
  2844. */
  2845. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  2846. int __user *optlen)
  2847. {
  2848. if (len != sizeof(struct sctp_event_subscribe))
  2849. return -EINVAL;
  2850. if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
  2851. return -EFAULT;
  2852. return 0;
  2853. }
  2854. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  2855. *
  2856. * This socket option is applicable to the UDP-style socket only. When
  2857. * set it will cause associations that are idle for more than the
  2858. * specified number of seconds to automatically close. An association
  2859. * being idle is defined an association that has NOT sent or received
  2860. * user data. The special value of '0' indicates that no automatic
  2861. * close of any associations should be performed. The option expects an
  2862. * integer defining the number of seconds of idle time before an
  2863. * association is closed.
  2864. */
  2865. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2866. {
  2867. /* Applicable to UDP-style socket only */
  2868. if (sctp_style(sk, TCP))
  2869. return -EOPNOTSUPP;
  2870. if (len != sizeof(int))
  2871. return -EINVAL;
  2872. if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
  2873. return -EFAULT;
  2874. return 0;
  2875. }
  2876. /* Helper routine to branch off an association to a new socket. */
  2877. SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
  2878. struct socket **sockp)
  2879. {
  2880. struct sock *sk = asoc->base.sk;
  2881. struct socket *sock;
  2882. int err = 0;
  2883. /* An association cannot be branched off from an already peeled-off
  2884. * socket, nor is this supported for tcp style sockets.
  2885. */
  2886. if (!sctp_style(sk, UDP))
  2887. return -EINVAL;
  2888. /* Create a new socket. */
  2889. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  2890. if (err < 0)
  2891. return err;
  2892. /* Populate the fields of the newsk from the oldsk and migrate the
  2893. * asoc to the newsk.
  2894. */
  2895. sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  2896. *sockp = sock;
  2897. return err;
  2898. }
  2899. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2900. {
  2901. sctp_peeloff_arg_t peeloff;
  2902. struct socket *newsock;
  2903. int retval = 0;
  2904. struct sctp_association *asoc;
  2905. if (len != sizeof(sctp_peeloff_arg_t))
  2906. return -EINVAL;
  2907. if (copy_from_user(&peeloff, optval, len))
  2908. return -EFAULT;
  2909. asoc = sctp_id2assoc(sk, peeloff.associd);
  2910. if (!asoc) {
  2911. retval = -EINVAL;
  2912. goto out;
  2913. }
  2914. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
  2915. retval = sctp_do_peeloff(asoc, &newsock);
  2916. if (retval < 0)
  2917. goto out;
  2918. /* Map the socket to an unused fd that can be returned to the user. */
  2919. retval = sock_map_fd(newsock);
  2920. if (retval < 0) {
  2921. sock_release(newsock);
  2922. goto out;
  2923. }
  2924. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
  2925. __FUNCTION__, sk, asoc, newsock->sk, retval);
  2926. /* Return the fd mapped to the new socket. */
  2927. peeloff.sd = retval;
  2928. if (copy_to_user(optval, &peeloff, len))
  2929. retval = -EFAULT;
  2930. out:
  2931. return retval;
  2932. }
  2933. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  2934. *
  2935. * Applications can enable or disable heartbeats for any peer address of
  2936. * an association, modify an address's heartbeat interval, force a
  2937. * heartbeat to be sent immediately, and adjust the address's maximum
  2938. * number of retransmissions sent before an address is considered
  2939. * unreachable. The following structure is used to access and modify an
  2940. * address's parameters:
  2941. *
  2942. * struct sctp_paddrparams {
  2943. * sctp_assoc_t spp_assoc_id;
  2944. * struct sockaddr_storage spp_address;
  2945. * uint32_t spp_hbinterval;
  2946. * uint16_t spp_pathmaxrxt;
  2947. * uint32_t spp_pathmtu;
  2948. * uint32_t spp_sackdelay;
  2949. * uint32_t spp_flags;
  2950. * };
  2951. *
  2952. * spp_assoc_id - (one-to-many style socket) This is filled in the
  2953. * application, and identifies the association for
  2954. * this query.
  2955. * spp_address - This specifies which address is of interest.
  2956. * spp_hbinterval - This contains the value of the heartbeat interval,
  2957. * in milliseconds. If a value of zero
  2958. * is present in this field then no changes are to
  2959. * be made to this parameter.
  2960. * spp_pathmaxrxt - This contains the maximum number of
  2961. * retransmissions before this address shall be
  2962. * considered unreachable. If a value of zero
  2963. * is present in this field then no changes are to
  2964. * be made to this parameter.
  2965. * spp_pathmtu - When Path MTU discovery is disabled the value
  2966. * specified here will be the "fixed" path mtu.
  2967. * Note that if the spp_address field is empty
  2968. * then all associations on this address will
  2969. * have this fixed path mtu set upon them.
  2970. *
  2971. * spp_sackdelay - When delayed sack is enabled, this value specifies
  2972. * the number of milliseconds that sacks will be delayed
  2973. * for. This value will apply to all addresses of an
  2974. * association if the spp_address field is empty. Note
  2975. * also, that if delayed sack is enabled and this
  2976. * value is set to 0, no change is made to the last
  2977. * recorded delayed sack timer value.
  2978. *
  2979. * spp_flags - These flags are used to control various features
  2980. * on an association. The flag field may contain
  2981. * zero or more of the following options.
  2982. *
  2983. * SPP_HB_ENABLE - Enable heartbeats on the
  2984. * specified address. Note that if the address
  2985. * field is empty all addresses for the association
  2986. * have heartbeats enabled upon them.
  2987. *
  2988. * SPP_HB_DISABLE - Disable heartbeats on the
  2989. * speicifed address. Note that if the address
  2990. * field is empty all addresses for the association
  2991. * will have their heartbeats disabled. Note also
  2992. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  2993. * mutually exclusive, only one of these two should
  2994. * be specified. Enabling both fields will have
  2995. * undetermined results.
  2996. *
  2997. * SPP_HB_DEMAND - Request a user initiated heartbeat
  2998. * to be made immediately.
  2999. *
  3000. * SPP_PMTUD_ENABLE - This field will enable PMTU
  3001. * discovery upon the specified address. Note that
  3002. * if the address feild is empty then all addresses
  3003. * on the association are effected.
  3004. *
  3005. * SPP_PMTUD_DISABLE - This field will disable PMTU
  3006. * discovery upon the specified address. Note that
  3007. * if the address feild is empty then all addresses
  3008. * on the association are effected. Not also that
  3009. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  3010. * exclusive. Enabling both will have undetermined
  3011. * results.
  3012. *
  3013. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  3014. * on delayed sack. The time specified in spp_sackdelay
  3015. * is used to specify the sack delay for this address. Note
  3016. * that if spp_address is empty then all addresses will
  3017. * enable delayed sack and take on the sack delay
  3018. * value specified in spp_sackdelay.
  3019. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  3020. * off delayed sack. If the spp_address field is blank then
  3021. * delayed sack is disabled for the entire association. Note
  3022. * also that this field is mutually exclusive to
  3023. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  3024. * results.
  3025. */
  3026. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  3027. char __user *optval, int __user *optlen)
  3028. {
  3029. struct sctp_paddrparams params;
  3030. struct sctp_transport *trans = NULL;
  3031. struct sctp_association *asoc = NULL;
  3032. struct sctp_sock *sp = sctp_sk(sk);
  3033. if (len != sizeof(struct sctp_paddrparams))
  3034. return -EINVAL;
  3035. if (copy_from_user(&params, optval, len))
  3036. return -EFAULT;
  3037. /* If an address other than INADDR_ANY is specified, and
  3038. * no transport is found, then the request is invalid.
  3039. */
  3040. if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
  3041. trans = sctp_addr_id2transport(sk, &params.spp_address,
  3042. params.spp_assoc_id);
  3043. if (!trans) {
  3044. SCTP_DEBUG_PRINTK("Failed no transport\n");
  3045. return -EINVAL;
  3046. }
  3047. }
  3048. /* Get association, if assoc_id != 0 and the socket is a one
  3049. * to many style socket, and an association was not found, then
  3050. * the id was invalid.
  3051. */
  3052. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  3053. if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
  3054. SCTP_DEBUG_PRINTK("Failed no association\n");
  3055. return -EINVAL;
  3056. }
  3057. if (trans) {
  3058. /* Fetch transport values. */
  3059. params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
  3060. params.spp_pathmtu = trans->pathmtu;
  3061. params.spp_pathmaxrxt = trans->pathmaxrxt;
  3062. params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
  3063. /*draft-11 doesn't say what to return in spp_flags*/
  3064. params.spp_flags = trans->param_flags;
  3065. } else if (asoc) {
  3066. /* Fetch association values. */
  3067. params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
  3068. params.spp_pathmtu = asoc->pathmtu;
  3069. params.spp_pathmaxrxt = asoc->pathmaxrxt;
  3070. params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
  3071. /*draft-11 doesn't say what to return in spp_flags*/
  3072. params.spp_flags = asoc->param_flags;
  3073. } else {
  3074. /* Fetch socket values. */
  3075. params.spp_hbinterval = sp->hbinterval;
  3076. params.spp_pathmtu = sp->pathmtu;
  3077. params.spp_sackdelay = sp->sackdelay;
  3078. params.spp_pathmaxrxt = sp->pathmaxrxt;
  3079. /*draft-11 doesn't say what to return in spp_flags*/
  3080. params.spp_flags = sp->param_flags;
  3081. }
  3082. if (copy_to_user(optval, &params, len))
  3083. return -EFAULT;
  3084. if (put_user(len, optlen))
  3085. return -EFAULT;
  3086. return 0;
  3087. }
  3088. /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
  3089. *
  3090. * This options will get or set the delayed ack timer. The time is set
  3091. * in milliseconds. If the assoc_id is 0, then this sets or gets the
  3092. * endpoints default delayed ack timer value. If the assoc_id field is
  3093. * non-zero, then the set or get effects the specified association.
  3094. *
  3095. * struct sctp_assoc_value {
  3096. * sctp_assoc_t assoc_id;
  3097. * uint32_t assoc_value;
  3098. * };
  3099. *
  3100. * assoc_id - This parameter, indicates which association the
  3101. * user is preforming an action upon. Note that if
  3102. * this field's value is zero then the endpoints
  3103. * default value is changed (effecting future
  3104. * associations only).
  3105. *
  3106. * assoc_value - This parameter contains the number of milliseconds
  3107. * that the user is requesting the delayed ACK timer
  3108. * be set to. Note that this value is defined in
  3109. * the standard to be between 200 and 500 milliseconds.
  3110. *
  3111. * Note: a value of zero will leave the value alone,
  3112. * but disable SACK delay. A non-zero value will also
  3113. * enable SACK delay.
  3114. */
  3115. static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
  3116. char __user *optval,
  3117. int __user *optlen)
  3118. {
  3119. struct sctp_assoc_value params;
  3120. struct sctp_association *asoc = NULL;
  3121. struct sctp_sock *sp = sctp_sk(sk);
  3122. if (len != sizeof(struct sctp_assoc_value))
  3123. return - EINVAL;
  3124. if (copy_from_user(&params, optval, len))
  3125. return -EFAULT;
  3126. /* Get association, if assoc_id != 0 and the socket is a one
  3127. * to many style socket, and an association was not found, then
  3128. * the id was invalid.
  3129. */
  3130. asoc = sctp_id2assoc(sk, params.assoc_id);
  3131. if (!asoc && params.assoc_id && sctp_style(sk, UDP))
  3132. return -EINVAL;
  3133. if (asoc) {
  3134. /* Fetch association values. */
  3135. if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
  3136. params.assoc_value = jiffies_to_msecs(
  3137. asoc->sackdelay);
  3138. else
  3139. params.assoc_value = 0;
  3140. } else {
  3141. /* Fetch socket values. */
  3142. if (sp->param_flags & SPP_SACKDELAY_ENABLE)
  3143. params.assoc_value = sp->sackdelay;
  3144. else
  3145. params.assoc_value = 0;
  3146. }
  3147. if (copy_to_user(optval, &params, len))
  3148. return -EFAULT;
  3149. if (put_user(len, optlen))
  3150. return -EFAULT;
  3151. return 0;
  3152. }
  3153. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  3154. *
  3155. * Applications can specify protocol parameters for the default association
  3156. * initialization. The option name argument to setsockopt() and getsockopt()
  3157. * is SCTP_INITMSG.
  3158. *
  3159. * Setting initialization parameters is effective only on an unconnected
  3160. * socket (for UDP-style sockets only future associations are effected
  3161. * by the change). With TCP-style sockets, this option is inherited by
  3162. * sockets derived from a listener socket.
  3163. */
  3164. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  3165. {
  3166. if (len != sizeof(struct sctp_initmsg))
  3167. return -EINVAL;
  3168. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  3169. return -EFAULT;
  3170. return 0;
  3171. }
  3172. static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
  3173. char __user *optval,
  3174. int __user *optlen)
  3175. {
  3176. sctp_assoc_t id;
  3177. struct sctp_association *asoc;
  3178. struct list_head *pos;
  3179. int cnt = 0;
  3180. if (len != sizeof(sctp_assoc_t))
  3181. return -EINVAL;
  3182. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  3183. return -EFAULT;
  3184. /* For UDP-style sockets, id specifies the association to query. */
  3185. asoc = sctp_id2assoc(sk, id);
  3186. if (!asoc)
  3187. return -EINVAL;
  3188. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3189. cnt ++;
  3190. }
  3191. return cnt;
  3192. }
  3193. /*
  3194. * Old API for getting list of peer addresses. Does not work for 32-bit
  3195. * programs running on a 64-bit kernel
  3196. */
  3197. static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
  3198. char __user *optval,
  3199. int __user *optlen)
  3200. {
  3201. struct sctp_association *asoc;
  3202. struct list_head *pos;
  3203. int cnt = 0;
  3204. struct sctp_getaddrs_old getaddrs;
  3205. struct sctp_transport *from;
  3206. void __user *to;
  3207. union sctp_addr temp;
  3208. struct sctp_sock *sp = sctp_sk(sk);
  3209. int addrlen;
  3210. if (len != sizeof(struct sctp_getaddrs_old))
  3211. return -EINVAL;
  3212. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
  3213. return -EFAULT;
  3214. if (getaddrs.addr_num <= 0) return -EINVAL;
  3215. /* For UDP-style sockets, id specifies the association to query. */
  3216. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3217. if (!asoc)
  3218. return -EINVAL;
  3219. to = (void __user *)getaddrs.addrs;
  3220. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3221. from = list_entry(pos, struct sctp_transport, transports);
  3222. memcpy(&temp, &from->ipaddr, sizeof(temp));
  3223. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3224. addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
  3225. temp.v4.sin_port = htons(temp.v4.sin_port);
  3226. if (copy_to_user(to, &temp, addrlen))
  3227. return -EFAULT;
  3228. to += addrlen ;
  3229. cnt ++;
  3230. if (cnt >= getaddrs.addr_num) break;
  3231. }
  3232. getaddrs.addr_num = cnt;
  3233. if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
  3234. return -EFAULT;
  3235. return 0;
  3236. }
  3237. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  3238. char __user *optval, int __user *optlen)
  3239. {
  3240. struct sctp_association *asoc;
  3241. struct list_head *pos;
  3242. int cnt = 0;
  3243. struct sctp_getaddrs getaddrs;
  3244. struct sctp_transport *from;
  3245. void __user *to;
  3246. union sctp_addr temp;
  3247. struct sctp_sock *sp = sctp_sk(sk);
  3248. int addrlen;
  3249. size_t space_left;
  3250. int bytes_copied;
  3251. if (len < sizeof(struct sctp_getaddrs))
  3252. return -EINVAL;
  3253. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  3254. return -EFAULT;
  3255. /* For UDP-style sockets, id specifies the association to query. */
  3256. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3257. if (!asoc)
  3258. return -EINVAL;
  3259. to = optval + offsetof(struct sctp_getaddrs,addrs);
  3260. space_left = len - sizeof(struct sctp_getaddrs) -
  3261. offsetof(struct sctp_getaddrs,addrs);
  3262. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3263. from = list_entry(pos, struct sctp_transport, transports);
  3264. memcpy(&temp, &from->ipaddr, sizeof(temp));
  3265. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3266. addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
  3267. if(space_left < addrlen)
  3268. return -ENOMEM;
  3269. temp.v4.sin_port = htons(temp.v4.sin_port);
  3270. if (copy_to_user(to, &temp, addrlen))
  3271. return -EFAULT;
  3272. to += addrlen;
  3273. cnt++;
  3274. space_left -= addrlen;
  3275. }
  3276. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  3277. return -EFAULT;
  3278. bytes_copied = ((char __user *)to) - optval;
  3279. if (put_user(bytes_copied, optlen))
  3280. return -EFAULT;
  3281. return 0;
  3282. }
  3283. static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
  3284. char __user *optval,
  3285. int __user *optlen)
  3286. {
  3287. sctp_assoc_t id;
  3288. struct sctp_bind_addr *bp;
  3289. struct sctp_association *asoc;
  3290. struct list_head *pos;
  3291. struct sctp_sockaddr_entry *addr;
  3292. rwlock_t *addr_lock;
  3293. unsigned long flags;
  3294. int cnt = 0;
  3295. if (len != sizeof(sctp_assoc_t))
  3296. return -EINVAL;
  3297. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  3298. return -EFAULT;
  3299. /*
  3300. * For UDP-style sockets, id specifies the association to query.
  3301. * If the id field is set to the value '0' then the locally bound
  3302. * addresses are returned without regard to any particular
  3303. * association.
  3304. */
  3305. if (0 == id) {
  3306. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3307. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  3308. } else {
  3309. asoc = sctp_id2assoc(sk, id);
  3310. if (!asoc)
  3311. return -EINVAL;
  3312. bp = &asoc->base.bind_addr;
  3313. addr_lock = &asoc->base.addr_lock;
  3314. }
  3315. sctp_read_lock(addr_lock);
  3316. /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
  3317. * addresses from the global local address list.
  3318. */
  3319. if (sctp_list_single_entry(&bp->address_list)) {
  3320. addr = list_entry(bp->address_list.next,
  3321. struct sctp_sockaddr_entry, list);
  3322. if (sctp_is_any(&addr->a)) {
  3323. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  3324. list_for_each(pos, &sctp_local_addr_list) {
  3325. addr = list_entry(pos,
  3326. struct sctp_sockaddr_entry,
  3327. list);
  3328. if ((PF_INET == sk->sk_family) &&
  3329. (AF_INET6 == addr->a.sa.sa_family))
  3330. continue;
  3331. cnt++;
  3332. }
  3333. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  3334. flags);
  3335. } else {
  3336. cnt = 1;
  3337. }
  3338. goto done;
  3339. }
  3340. list_for_each(pos, &bp->address_list) {
  3341. cnt ++;
  3342. }
  3343. done:
  3344. sctp_read_unlock(addr_lock);
  3345. return cnt;
  3346. }
  3347. /* Helper function that copies local addresses to user and returns the number
  3348. * of addresses copied.
  3349. */
  3350. static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
  3351. void __user *to)
  3352. {
  3353. struct list_head *pos;
  3354. struct sctp_sockaddr_entry *addr;
  3355. unsigned long flags;
  3356. union sctp_addr temp;
  3357. int cnt = 0;
  3358. int addrlen;
  3359. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  3360. list_for_each(pos, &sctp_local_addr_list) {
  3361. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  3362. if ((PF_INET == sk->sk_family) &&
  3363. (AF_INET6 == addr->a.sa.sa_family))
  3364. continue;
  3365. memcpy(&temp, &addr->a, sizeof(temp));
  3366. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  3367. &temp);
  3368. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3369. temp.v4.sin_port = htons(port);
  3370. if (copy_to_user(to, &temp, addrlen)) {
  3371. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  3372. flags);
  3373. return -EFAULT;
  3374. }
  3375. to += addrlen;
  3376. cnt ++;
  3377. if (cnt >= max_addrs) break;
  3378. }
  3379. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
  3380. return cnt;
  3381. }
  3382. static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
  3383. void __user **to, size_t space_left)
  3384. {
  3385. struct list_head *pos;
  3386. struct sctp_sockaddr_entry *addr;
  3387. unsigned long flags;
  3388. union sctp_addr temp;
  3389. int cnt = 0;
  3390. int addrlen;
  3391. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  3392. list_for_each(pos, &sctp_local_addr_list) {
  3393. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  3394. if ((PF_INET == sk->sk_family) &&
  3395. (AF_INET6 == addr->a.sa.sa_family))
  3396. continue;
  3397. memcpy(&temp, &addr->a, sizeof(temp));
  3398. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  3399. &temp);
  3400. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3401. if(space_left<addrlen)
  3402. return -ENOMEM;
  3403. temp.v4.sin_port = htons(port);
  3404. if (copy_to_user(*to, &temp, addrlen)) {
  3405. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  3406. flags);
  3407. return -EFAULT;
  3408. }
  3409. *to += addrlen;
  3410. cnt ++;
  3411. space_left -= addrlen;
  3412. }
  3413. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
  3414. return cnt;
  3415. }
  3416. /* Old API for getting list of local addresses. Does not work for 32-bit
  3417. * programs running on a 64-bit kernel
  3418. */
  3419. static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
  3420. char __user *optval, int __user *optlen)
  3421. {
  3422. struct sctp_bind_addr *bp;
  3423. struct sctp_association *asoc;
  3424. struct list_head *pos;
  3425. int cnt = 0;
  3426. struct sctp_getaddrs_old getaddrs;
  3427. struct sctp_sockaddr_entry *addr;
  3428. void __user *to;
  3429. union sctp_addr temp;
  3430. struct sctp_sock *sp = sctp_sk(sk);
  3431. int addrlen;
  3432. rwlock_t *addr_lock;
  3433. int err = 0;
  3434. if (len != sizeof(struct sctp_getaddrs_old))
  3435. return -EINVAL;
  3436. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
  3437. return -EFAULT;
  3438. if (getaddrs.addr_num <= 0) return -EINVAL;
  3439. /*
  3440. * For UDP-style sockets, id specifies the association to query.
  3441. * If the id field is set to the value '0' then the locally bound
  3442. * addresses are returned without regard to any particular
  3443. * association.
  3444. */
  3445. if (0 == getaddrs.assoc_id) {
  3446. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3447. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  3448. } else {
  3449. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3450. if (!asoc)
  3451. return -EINVAL;
  3452. bp = &asoc->base.bind_addr;
  3453. addr_lock = &asoc->base.addr_lock;
  3454. }
  3455. to = getaddrs.addrs;
  3456. sctp_read_lock(addr_lock);
  3457. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  3458. * addresses from the global local address list.
  3459. */
  3460. if (sctp_list_single_entry(&bp->address_list)) {
  3461. addr = list_entry(bp->address_list.next,
  3462. struct sctp_sockaddr_entry, list);
  3463. if (sctp_is_any(&addr->a)) {
  3464. cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
  3465. getaddrs.addr_num,
  3466. to);
  3467. if (cnt < 0) {
  3468. err = cnt;
  3469. goto unlock;
  3470. }
  3471. goto copy_getaddrs;
  3472. }
  3473. }
  3474. list_for_each(pos, &bp->address_list) {
  3475. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  3476. memcpy(&temp, &addr->a, sizeof(temp));
  3477. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3478. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3479. temp.v4.sin_port = htons(temp.v4.sin_port);
  3480. if (copy_to_user(to, &temp, addrlen)) {
  3481. err = -EFAULT;
  3482. goto unlock;
  3483. }
  3484. to += addrlen;
  3485. cnt ++;
  3486. if (cnt >= getaddrs.addr_num) break;
  3487. }
  3488. copy_getaddrs:
  3489. getaddrs.addr_num = cnt;
  3490. if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
  3491. err = -EFAULT;
  3492. unlock:
  3493. sctp_read_unlock(addr_lock);
  3494. return err;
  3495. }
  3496. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  3497. char __user *optval, int __user *optlen)
  3498. {
  3499. struct sctp_bind_addr *bp;
  3500. struct sctp_association *asoc;
  3501. struct list_head *pos;
  3502. int cnt = 0;
  3503. struct sctp_getaddrs getaddrs;
  3504. struct sctp_sockaddr_entry *addr;
  3505. void __user *to;
  3506. union sctp_addr temp;
  3507. struct sctp_sock *sp = sctp_sk(sk);
  3508. int addrlen;
  3509. rwlock_t *addr_lock;
  3510. int err = 0;
  3511. size_t space_left;
  3512. int bytes_copied;
  3513. if (len <= sizeof(struct sctp_getaddrs))
  3514. return -EINVAL;
  3515. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  3516. return -EFAULT;
  3517. /*
  3518. * For UDP-style sockets, id specifies the association to query.
  3519. * If the id field is set to the value '0' then the locally bound
  3520. * addresses are returned without regard to any particular
  3521. * association.
  3522. */
  3523. if (0 == getaddrs.assoc_id) {
  3524. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3525. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  3526. } else {
  3527. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3528. if (!asoc)
  3529. return -EINVAL;
  3530. bp = &asoc->base.bind_addr;
  3531. addr_lock = &asoc->base.addr_lock;
  3532. }
  3533. to = optval + offsetof(struct sctp_getaddrs,addrs);
  3534. space_left = len - sizeof(struct sctp_getaddrs) -
  3535. offsetof(struct sctp_getaddrs,addrs);
  3536. sctp_read_lock(addr_lock);
  3537. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  3538. * addresses from the global local address list.
  3539. */
  3540. if (sctp_list_single_entry(&bp->address_list)) {
  3541. addr = list_entry(bp->address_list.next,
  3542. struct sctp_sockaddr_entry, list);
  3543. if (sctp_is_any(&addr->a)) {
  3544. cnt = sctp_copy_laddrs_to_user(sk, bp->port,
  3545. &to, space_left);
  3546. if (cnt < 0) {
  3547. err = cnt;
  3548. goto unlock;
  3549. }
  3550. goto copy_getaddrs;
  3551. }
  3552. }
  3553. list_for_each(pos, &bp->address_list) {
  3554. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  3555. memcpy(&temp, &addr->a, sizeof(temp));
  3556. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3557. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3558. if(space_left < addrlen)
  3559. return -ENOMEM; /*fixme: right error?*/
  3560. temp.v4.sin_port = htons(temp.v4.sin_port);
  3561. if (copy_to_user(to, &temp, addrlen)) {
  3562. err = -EFAULT;
  3563. goto unlock;
  3564. }
  3565. to += addrlen;
  3566. cnt ++;
  3567. space_left -= addrlen;
  3568. }
  3569. copy_getaddrs:
  3570. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  3571. return -EFAULT;
  3572. bytes_copied = ((char __user *)to) - optval;
  3573. if (put_user(bytes_copied, optlen))
  3574. return -EFAULT;
  3575. unlock:
  3576. sctp_read_unlock(addr_lock);
  3577. return err;
  3578. }
  3579. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  3580. *
  3581. * Requests that the local SCTP stack use the enclosed peer address as
  3582. * the association primary. The enclosed address must be one of the
  3583. * association peer's addresses.
  3584. */
  3585. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  3586. char __user *optval, int __user *optlen)
  3587. {
  3588. struct sctp_prim prim;
  3589. struct sctp_association *asoc;
  3590. struct sctp_sock *sp = sctp_sk(sk);
  3591. if (len != sizeof(struct sctp_prim))
  3592. return -EINVAL;
  3593. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  3594. return -EFAULT;
  3595. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  3596. if (!asoc)
  3597. return -EINVAL;
  3598. if (!asoc->peer.primary_path)
  3599. return -ENOTCONN;
  3600. asoc->peer.primary_path->ipaddr.v4.sin_port =
  3601. htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
  3602. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  3603. sizeof(union sctp_addr));
  3604. asoc->peer.primary_path->ipaddr.v4.sin_port =
  3605. ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
  3606. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
  3607. (union sctp_addr *)&prim.ssp_addr);
  3608. if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
  3609. return -EFAULT;
  3610. return 0;
  3611. }
  3612. /*
  3613. * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
  3614. *
  3615. * Requests that the local endpoint set the specified Adaption Layer
  3616. * Indication parameter for all future INIT and INIT-ACK exchanges.
  3617. */
  3618. static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
  3619. char __user *optval, int __user *optlen)
  3620. {
  3621. struct sctp_setadaption adaption;
  3622. if (len != sizeof(struct sctp_setadaption))
  3623. return -EINVAL;
  3624. adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
  3625. if (copy_to_user(optval, &adaption, len))
  3626. return -EFAULT;
  3627. return 0;
  3628. }
  3629. /*
  3630. *
  3631. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  3632. *
  3633. * Applications that wish to use the sendto() system call may wish to
  3634. * specify a default set of parameters that would normally be supplied
  3635. * through the inclusion of ancillary data. This socket option allows
  3636. * such an application to set the default sctp_sndrcvinfo structure.
  3637. * The application that wishes to use this socket option simply passes
  3638. * in to this call the sctp_sndrcvinfo structure defined in Section
  3639. * 5.2.2) The input parameters accepted by this call include
  3640. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  3641. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  3642. * to this call if the caller is using the UDP model.
  3643. *
  3644. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  3645. */
  3646. static int sctp_getsockopt_default_send_param(struct sock *sk,
  3647. int len, char __user *optval,
  3648. int __user *optlen)
  3649. {
  3650. struct sctp_sndrcvinfo info;
  3651. struct sctp_association *asoc;
  3652. struct sctp_sock *sp = sctp_sk(sk);
  3653. if (len != sizeof(struct sctp_sndrcvinfo))
  3654. return -EINVAL;
  3655. if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
  3656. return -EFAULT;
  3657. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  3658. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  3659. return -EINVAL;
  3660. if (asoc) {
  3661. info.sinfo_stream = asoc->default_stream;
  3662. info.sinfo_flags = asoc->default_flags;
  3663. info.sinfo_ppid = asoc->default_ppid;
  3664. info.sinfo_context = asoc->default_context;
  3665. info.sinfo_timetolive = asoc->default_timetolive;
  3666. } else {
  3667. info.sinfo_stream = sp->default_stream;
  3668. info.sinfo_flags = sp->default_flags;
  3669. info.sinfo_ppid = sp->default_ppid;
  3670. info.sinfo_context = sp->default_context;
  3671. info.sinfo_timetolive = sp->default_timetolive;
  3672. }
  3673. if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
  3674. return -EFAULT;
  3675. return 0;
  3676. }
  3677. /*
  3678. *
  3679. * 7.1.5 SCTP_NODELAY
  3680. *
  3681. * Turn on/off any Nagle-like algorithm. This means that packets are
  3682. * generally sent as soon as possible and no unnecessary delays are
  3683. * introduced, at the cost of more packets in the network. Expects an
  3684. * integer boolean flag.
  3685. */
  3686. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  3687. char __user *optval, int __user *optlen)
  3688. {
  3689. int val;
  3690. if (len < sizeof(int))
  3691. return -EINVAL;
  3692. len = sizeof(int);
  3693. val = (sctp_sk(sk)->nodelay == 1);
  3694. if (put_user(len, optlen))
  3695. return -EFAULT;
  3696. if (copy_to_user(optval, &val, len))
  3697. return -EFAULT;
  3698. return 0;
  3699. }
  3700. /*
  3701. *
  3702. * 7.1.1 SCTP_RTOINFO
  3703. *
  3704. * The protocol parameters used to initialize and bound retransmission
  3705. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  3706. * and modify these parameters.
  3707. * All parameters are time values, in milliseconds. A value of 0, when
  3708. * modifying the parameters, indicates that the current value should not
  3709. * be changed.
  3710. *
  3711. */
  3712. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  3713. char __user *optval,
  3714. int __user *optlen) {
  3715. struct sctp_rtoinfo rtoinfo;
  3716. struct sctp_association *asoc;
  3717. if (len != sizeof (struct sctp_rtoinfo))
  3718. return -EINVAL;
  3719. if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
  3720. return -EFAULT;
  3721. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  3722. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  3723. return -EINVAL;
  3724. /* Values corresponding to the specific association. */
  3725. if (asoc) {
  3726. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  3727. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  3728. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  3729. } else {
  3730. /* Values corresponding to the endpoint. */
  3731. struct sctp_sock *sp = sctp_sk(sk);
  3732. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  3733. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  3734. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  3735. }
  3736. if (put_user(len, optlen))
  3737. return -EFAULT;
  3738. if (copy_to_user(optval, &rtoinfo, len))
  3739. return -EFAULT;
  3740. return 0;
  3741. }
  3742. /*
  3743. *
  3744. * 7.1.2 SCTP_ASSOCINFO
  3745. *
  3746. * This option is used to tune the the maximum retransmission attempts
  3747. * of the association.
  3748. * Returns an error if the new association retransmission value is
  3749. * greater than the sum of the retransmission value of the peer.
  3750. * See [SCTP] for more information.
  3751. *
  3752. */
  3753. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  3754. char __user *optval,
  3755. int __user *optlen)
  3756. {
  3757. struct sctp_assocparams assocparams;
  3758. struct sctp_association *asoc;
  3759. struct list_head *pos;
  3760. int cnt = 0;
  3761. if (len != sizeof (struct sctp_assocparams))
  3762. return -EINVAL;
  3763. if (copy_from_user(&assocparams, optval,
  3764. sizeof (struct sctp_assocparams)))
  3765. return -EFAULT;
  3766. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  3767. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  3768. return -EINVAL;
  3769. /* Values correspoinding to the specific association */
  3770. if (asoc) {
  3771. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  3772. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  3773. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  3774. assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
  3775. * 1000) +
  3776. (asoc->cookie_life.tv_usec
  3777. / 1000);
  3778. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3779. cnt ++;
  3780. }
  3781. assocparams.sasoc_number_peer_destinations = cnt;
  3782. } else {
  3783. /* Values corresponding to the endpoint */
  3784. struct sctp_sock *sp = sctp_sk(sk);
  3785. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  3786. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  3787. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  3788. assocparams.sasoc_cookie_life =
  3789. sp->assocparams.sasoc_cookie_life;
  3790. assocparams.sasoc_number_peer_destinations =
  3791. sp->assocparams.
  3792. sasoc_number_peer_destinations;
  3793. }
  3794. if (put_user(len, optlen))
  3795. return -EFAULT;
  3796. if (copy_to_user(optval, &assocparams, len))
  3797. return -EFAULT;
  3798. return 0;
  3799. }
  3800. /*
  3801. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  3802. *
  3803. * This socket option is a boolean flag which turns on or off mapped V4
  3804. * addresses. If this option is turned on and the socket is type
  3805. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  3806. * If this option is turned off, then no mapping will be done of V4
  3807. * addresses and a user will receive both PF_INET6 and PF_INET type
  3808. * addresses on the socket.
  3809. */
  3810. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  3811. char __user *optval, int __user *optlen)
  3812. {
  3813. int val;
  3814. struct sctp_sock *sp = sctp_sk(sk);
  3815. if (len < sizeof(int))
  3816. return -EINVAL;
  3817. len = sizeof(int);
  3818. val = sp->v4mapped;
  3819. if (put_user(len, optlen))
  3820. return -EFAULT;
  3821. if (copy_to_user(optval, &val, len))
  3822. return -EFAULT;
  3823. return 0;
  3824. }
  3825. /*
  3826. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  3827. *
  3828. * This socket option specifies the maximum size to put in any outgoing
  3829. * SCTP chunk. If a message is larger than this size it will be
  3830. * fragmented by SCTP into the specified size. Note that the underlying
  3831. * SCTP implementation may fragment into smaller sized chunks when the
  3832. * PMTU of the underlying association is smaller than the value set by
  3833. * the user.
  3834. */
  3835. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  3836. char __user *optval, int __user *optlen)
  3837. {
  3838. int val;
  3839. if (len < sizeof(int))
  3840. return -EINVAL;
  3841. len = sizeof(int);
  3842. val = sctp_sk(sk)->user_frag;
  3843. if (put_user(len, optlen))
  3844. return -EFAULT;
  3845. if (copy_to_user(optval, &val, len))
  3846. return -EFAULT;
  3847. return 0;
  3848. }
  3849. SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
  3850. char __user *optval, int __user *optlen)
  3851. {
  3852. int retval = 0;
  3853. int len;
  3854. SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
  3855. sk, optname);
  3856. /* I can hardly begin to describe how wrong this is. This is
  3857. * so broken as to be worse than useless. The API draft
  3858. * REALLY is NOT helpful here... I am not convinced that the
  3859. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  3860. * are at all well-founded.
  3861. */
  3862. if (level != SOL_SCTP) {
  3863. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3864. retval = af->getsockopt(sk, level, optname, optval, optlen);
  3865. return retval;
  3866. }
  3867. if (get_user(len, optlen))
  3868. return -EFAULT;
  3869. sctp_lock_sock(sk);
  3870. switch (optname) {
  3871. case SCTP_STATUS:
  3872. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  3873. break;
  3874. case SCTP_DISABLE_FRAGMENTS:
  3875. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  3876. optlen);
  3877. break;
  3878. case SCTP_EVENTS:
  3879. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  3880. break;
  3881. case SCTP_AUTOCLOSE:
  3882. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  3883. break;
  3884. case SCTP_SOCKOPT_PEELOFF:
  3885. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  3886. break;
  3887. case SCTP_PEER_ADDR_PARAMS:
  3888. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  3889. optlen);
  3890. break;
  3891. case SCTP_DELAYED_ACK_TIME:
  3892. retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
  3893. optlen);
  3894. break;
  3895. case SCTP_INITMSG:
  3896. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  3897. break;
  3898. case SCTP_GET_PEER_ADDRS_NUM_OLD:
  3899. retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
  3900. optlen);
  3901. break;
  3902. case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
  3903. retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
  3904. optlen);
  3905. break;
  3906. case SCTP_GET_PEER_ADDRS_OLD:
  3907. retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
  3908. optlen);
  3909. break;
  3910. case SCTP_GET_LOCAL_ADDRS_OLD:
  3911. retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
  3912. optlen);
  3913. break;
  3914. case SCTP_GET_PEER_ADDRS:
  3915. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  3916. optlen);
  3917. break;
  3918. case SCTP_GET_LOCAL_ADDRS:
  3919. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  3920. optlen);
  3921. break;
  3922. case SCTP_DEFAULT_SEND_PARAM:
  3923. retval = sctp_getsockopt_default_send_param(sk, len,
  3924. optval, optlen);
  3925. break;
  3926. case SCTP_PRIMARY_ADDR:
  3927. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  3928. break;
  3929. case SCTP_NODELAY:
  3930. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  3931. break;
  3932. case SCTP_RTOINFO:
  3933. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  3934. break;
  3935. case SCTP_ASSOCINFO:
  3936. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  3937. break;
  3938. case SCTP_I_WANT_MAPPED_V4_ADDR:
  3939. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  3940. break;
  3941. case SCTP_MAXSEG:
  3942. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  3943. break;
  3944. case SCTP_GET_PEER_ADDR_INFO:
  3945. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  3946. optlen);
  3947. break;
  3948. case SCTP_ADAPTION_LAYER:
  3949. retval = sctp_getsockopt_adaption_layer(sk, len, optval,
  3950. optlen);
  3951. break;
  3952. default:
  3953. retval = -ENOPROTOOPT;
  3954. break;
  3955. };
  3956. sctp_release_sock(sk);
  3957. return retval;
  3958. }
  3959. static void sctp_hash(struct sock *sk)
  3960. {
  3961. /* STUB */
  3962. }
  3963. static void sctp_unhash(struct sock *sk)
  3964. {
  3965. /* STUB */
  3966. }
  3967. /* Check if port is acceptable. Possibly find first available port.
  3968. *
  3969. * The port hash table (contained in the 'global' SCTP protocol storage
  3970. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  3971. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  3972. * list (the list number is the port number hashed out, so as you
  3973. * would expect from a hash function, all the ports in a given list have
  3974. * such a number that hashes out to the same list number; you were
  3975. * expecting that, right?); so each list has a set of ports, with a
  3976. * link to the socket (struct sock) that uses it, the port number and
  3977. * a fastreuse flag (FIXME: NPI ipg).
  3978. */
  3979. static struct sctp_bind_bucket *sctp_bucket_create(
  3980. struct sctp_bind_hashbucket *head, unsigned short snum);
  3981. static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  3982. {
  3983. struct sctp_bind_hashbucket *head; /* hash list */
  3984. struct sctp_bind_bucket *pp; /* hash list port iterator */
  3985. unsigned short snum;
  3986. int ret;
  3987. /* NOTE: Remember to put this back to net order. */
  3988. addr->v4.sin_port = ntohs(addr->v4.sin_port);
  3989. snum = addr->v4.sin_port;
  3990. SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
  3991. sctp_local_bh_disable();
  3992. if (snum == 0) {
  3993. /* Search for an available port.
  3994. *
  3995. * 'sctp_port_rover' was the last port assigned, so
  3996. * we start to search from 'sctp_port_rover +
  3997. * 1'. What we do is first check if port 'rover' is
  3998. * already in the hash table; if not, we use that; if
  3999. * it is, we try next.
  4000. */
  4001. int low = sysctl_local_port_range[0];
  4002. int high = sysctl_local_port_range[1];
  4003. int remaining = (high - low) + 1;
  4004. int rover;
  4005. int index;
  4006. sctp_spin_lock(&sctp_port_alloc_lock);
  4007. rover = sctp_port_rover;
  4008. do {
  4009. rover++;
  4010. if ((rover < low) || (rover > high))
  4011. rover = low;
  4012. index = sctp_phashfn(rover);
  4013. head = &sctp_port_hashtable[index];
  4014. sctp_spin_lock(&head->lock);
  4015. for (pp = head->chain; pp; pp = pp->next)
  4016. if (pp->port == rover)
  4017. goto next;
  4018. break;
  4019. next:
  4020. sctp_spin_unlock(&head->lock);
  4021. } while (--remaining > 0);
  4022. sctp_port_rover = rover;
  4023. sctp_spin_unlock(&sctp_port_alloc_lock);
  4024. /* Exhausted local port range during search? */
  4025. ret = 1;
  4026. if (remaining <= 0)
  4027. goto fail;
  4028. /* OK, here is the one we will use. HEAD (the port
  4029. * hash table list entry) is non-NULL and we hold it's
  4030. * mutex.
  4031. */
  4032. snum = rover;
  4033. } else {
  4034. /* We are given an specific port number; we verify
  4035. * that it is not being used. If it is used, we will
  4036. * exahust the search in the hash list corresponding
  4037. * to the port number (snum) - we detect that with the
  4038. * port iterator, pp being NULL.
  4039. */
  4040. head = &sctp_port_hashtable[sctp_phashfn(snum)];
  4041. sctp_spin_lock(&head->lock);
  4042. for (pp = head->chain; pp; pp = pp->next) {
  4043. if (pp->port == snum)
  4044. goto pp_found;
  4045. }
  4046. }
  4047. pp = NULL;
  4048. goto pp_not_found;
  4049. pp_found:
  4050. if (!hlist_empty(&pp->owner)) {
  4051. /* We had a port hash table hit - there is an
  4052. * available port (pp != NULL) and it is being
  4053. * used by other socket (pp->owner not empty); that other
  4054. * socket is going to be sk2.
  4055. */
  4056. int reuse = sk->sk_reuse;
  4057. struct sock *sk2;
  4058. struct hlist_node *node;
  4059. SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
  4060. if (pp->fastreuse && sk->sk_reuse)
  4061. goto success;
  4062. /* Run through the list of sockets bound to the port
  4063. * (pp->port) [via the pointers bind_next and
  4064. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  4065. * we get the endpoint they describe and run through
  4066. * the endpoint's list of IP (v4 or v6) addresses,
  4067. * comparing each of the addresses with the address of
  4068. * the socket sk. If we find a match, then that means
  4069. * that this port/socket (sk) combination are already
  4070. * in an endpoint.
  4071. */
  4072. sk_for_each_bound(sk2, node, &pp->owner) {
  4073. struct sctp_endpoint *ep2;
  4074. ep2 = sctp_sk(sk2)->ep;
  4075. if (reuse && sk2->sk_reuse)
  4076. continue;
  4077. if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
  4078. sctp_sk(sk))) {
  4079. ret = (long)sk2;
  4080. goto fail_unlock;
  4081. }
  4082. }
  4083. SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
  4084. }
  4085. pp_not_found:
  4086. /* If there was a hash table miss, create a new port. */
  4087. ret = 1;
  4088. if (!pp && !(pp = sctp_bucket_create(head, snum)))
  4089. goto fail_unlock;
  4090. /* In either case (hit or miss), make sure fastreuse is 1 only
  4091. * if sk->sk_reuse is too (that is, if the caller requested
  4092. * SO_REUSEADDR on this socket -sk-).
  4093. */
  4094. if (hlist_empty(&pp->owner))
  4095. pp->fastreuse = sk->sk_reuse ? 1 : 0;
  4096. else if (pp->fastreuse && !sk->sk_reuse)
  4097. pp->fastreuse = 0;
  4098. /* We are set, so fill up all the data in the hash table
  4099. * entry, tie the socket list information with the rest of the
  4100. * sockets FIXME: Blurry, NPI (ipg).
  4101. */
  4102. success:
  4103. inet_sk(sk)->num = snum;
  4104. if (!sctp_sk(sk)->bind_hash) {
  4105. sk_add_bind_node(sk, &pp->owner);
  4106. sctp_sk(sk)->bind_hash = pp;
  4107. }
  4108. ret = 0;
  4109. fail_unlock:
  4110. sctp_spin_unlock(&head->lock);
  4111. fail:
  4112. sctp_local_bh_enable();
  4113. addr->v4.sin_port = htons(addr->v4.sin_port);
  4114. return ret;
  4115. }
  4116. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  4117. * port is requested.
  4118. */
  4119. static int sctp_get_port(struct sock *sk, unsigned short snum)
  4120. {
  4121. long ret;
  4122. union sctp_addr addr;
  4123. struct sctp_af *af = sctp_sk(sk)->pf->af;
  4124. /* Set up a dummy address struct from the sk. */
  4125. af->from_sk(&addr, sk);
  4126. addr.v4.sin_port = htons(snum);
  4127. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  4128. ret = sctp_get_port_local(sk, &addr);
  4129. return (ret ? 1 : 0);
  4130. }
  4131. /*
  4132. * 3.1.3 listen() - UDP Style Syntax
  4133. *
  4134. * By default, new associations are not accepted for UDP style sockets.
  4135. * An application uses listen() to mark a socket as being able to
  4136. * accept new associations.
  4137. */
  4138. SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
  4139. {
  4140. struct sctp_sock *sp = sctp_sk(sk);
  4141. struct sctp_endpoint *ep = sp->ep;
  4142. /* Only UDP style sockets that are not peeled off are allowed to
  4143. * listen().
  4144. */
  4145. if (!sctp_style(sk, UDP))
  4146. return -EINVAL;
  4147. /* If backlog is zero, disable listening. */
  4148. if (!backlog) {
  4149. if (sctp_sstate(sk, CLOSED))
  4150. return 0;
  4151. sctp_unhash_endpoint(ep);
  4152. sk->sk_state = SCTP_SS_CLOSED;
  4153. }
  4154. /* Return if we are already listening. */
  4155. if (sctp_sstate(sk, LISTENING))
  4156. return 0;
  4157. /*
  4158. * If a bind() or sctp_bindx() is not called prior to a listen()
  4159. * call that allows new associations to be accepted, the system
  4160. * picks an ephemeral port and will choose an address set equivalent
  4161. * to binding with a wildcard address.
  4162. *
  4163. * This is not currently spelled out in the SCTP sockets
  4164. * extensions draft, but follows the practice as seen in TCP
  4165. * sockets.
  4166. */
  4167. if (!ep->base.bind_addr.port) {
  4168. if (sctp_autobind(sk))
  4169. return -EAGAIN;
  4170. }
  4171. sk->sk_state = SCTP_SS_LISTENING;
  4172. sctp_hash_endpoint(ep);
  4173. return 0;
  4174. }
  4175. /*
  4176. * 4.1.3 listen() - TCP Style Syntax
  4177. *
  4178. * Applications uses listen() to ready the SCTP endpoint for accepting
  4179. * inbound associations.
  4180. */
  4181. SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
  4182. {
  4183. struct sctp_sock *sp = sctp_sk(sk);
  4184. struct sctp_endpoint *ep = sp->ep;
  4185. /* If backlog is zero, disable listening. */
  4186. if (!backlog) {
  4187. if (sctp_sstate(sk, CLOSED))
  4188. return 0;
  4189. sctp_unhash_endpoint(ep);
  4190. sk->sk_state = SCTP_SS_CLOSED;
  4191. }
  4192. if (sctp_sstate(sk, LISTENING))
  4193. return 0;
  4194. /*
  4195. * If a bind() or sctp_bindx() is not called prior to a listen()
  4196. * call that allows new associations to be accepted, the system
  4197. * picks an ephemeral port and will choose an address set equivalent
  4198. * to binding with a wildcard address.
  4199. *
  4200. * This is not currently spelled out in the SCTP sockets
  4201. * extensions draft, but follows the practice as seen in TCP
  4202. * sockets.
  4203. */
  4204. if (!ep->base.bind_addr.port) {
  4205. if (sctp_autobind(sk))
  4206. return -EAGAIN;
  4207. }
  4208. sk->sk_state = SCTP_SS_LISTENING;
  4209. sk->sk_max_ack_backlog = backlog;
  4210. sctp_hash_endpoint(ep);
  4211. return 0;
  4212. }
  4213. /*
  4214. * Move a socket to LISTENING state.
  4215. */
  4216. int sctp_inet_listen(struct socket *sock, int backlog)
  4217. {
  4218. struct sock *sk = sock->sk;
  4219. struct crypto_tfm *tfm=NULL;
  4220. int err = -EINVAL;
  4221. if (unlikely(backlog < 0))
  4222. goto out;
  4223. sctp_lock_sock(sk);
  4224. if (sock->state != SS_UNCONNECTED)
  4225. goto out;
  4226. /* Allocate HMAC for generating cookie. */
  4227. if (sctp_hmac_alg) {
  4228. tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
  4229. if (!tfm) {
  4230. err = -ENOSYS;
  4231. goto out;
  4232. }
  4233. }
  4234. switch (sock->type) {
  4235. case SOCK_SEQPACKET:
  4236. err = sctp_seqpacket_listen(sk, backlog);
  4237. break;
  4238. case SOCK_STREAM:
  4239. err = sctp_stream_listen(sk, backlog);
  4240. break;
  4241. default:
  4242. break;
  4243. };
  4244. if (err)
  4245. goto cleanup;
  4246. /* Store away the transform reference. */
  4247. sctp_sk(sk)->hmac = tfm;
  4248. out:
  4249. sctp_release_sock(sk);
  4250. return err;
  4251. cleanup:
  4252. sctp_crypto_free_tfm(tfm);
  4253. goto out;
  4254. }
  4255. /*
  4256. * This function is done by modeling the current datagram_poll() and the
  4257. * tcp_poll(). Note that, based on these implementations, we don't
  4258. * lock the socket in this function, even though it seems that,
  4259. * ideally, locking or some other mechanisms can be used to ensure
  4260. * the integrity of the counters (sndbuf and wmem_alloc) used
  4261. * in this place. We assume that we don't need locks either until proven
  4262. * otherwise.
  4263. *
  4264. * Another thing to note is that we include the Async I/O support
  4265. * here, again, by modeling the current TCP/UDP code. We don't have
  4266. * a good way to test with it yet.
  4267. */
  4268. unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  4269. {
  4270. struct sock *sk = sock->sk;
  4271. struct sctp_sock *sp = sctp_sk(sk);
  4272. unsigned int mask;
  4273. poll_wait(file, sk->sk_sleep, wait);
  4274. /* A TCP-style listening socket becomes readable when the accept queue
  4275. * is not empty.
  4276. */
  4277. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  4278. return (!list_empty(&sp->ep->asocs)) ?
  4279. (POLLIN | POLLRDNORM) : 0;
  4280. mask = 0;
  4281. /* Is there any exceptional events? */
  4282. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  4283. mask |= POLLERR;
  4284. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4285. mask |= POLLRDHUP;
  4286. if (sk->sk_shutdown == SHUTDOWN_MASK)
  4287. mask |= POLLHUP;
  4288. /* Is it readable? Reconsider this code with TCP-style support. */
  4289. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  4290. (sk->sk_shutdown & RCV_SHUTDOWN))
  4291. mask |= POLLIN | POLLRDNORM;
  4292. /* The association is either gone or not ready. */
  4293. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  4294. return mask;
  4295. /* Is it writable? */
  4296. if (sctp_writeable(sk)) {
  4297. mask |= POLLOUT | POLLWRNORM;
  4298. } else {
  4299. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  4300. /*
  4301. * Since the socket is not locked, the buffer
  4302. * might be made available after the writeable check and
  4303. * before the bit is set. This could cause a lost I/O
  4304. * signal. tcp_poll() has a race breaker for this race
  4305. * condition. Based on their implementation, we put
  4306. * in the following code to cover it as well.
  4307. */
  4308. if (sctp_writeable(sk))
  4309. mask |= POLLOUT | POLLWRNORM;
  4310. }
  4311. return mask;
  4312. }
  4313. /********************************************************************
  4314. * 2nd Level Abstractions
  4315. ********************************************************************/
  4316. static struct sctp_bind_bucket *sctp_bucket_create(
  4317. struct sctp_bind_hashbucket *head, unsigned short snum)
  4318. {
  4319. struct sctp_bind_bucket *pp;
  4320. pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
  4321. SCTP_DBG_OBJCNT_INC(bind_bucket);
  4322. if (pp) {
  4323. pp->port = snum;
  4324. pp->fastreuse = 0;
  4325. INIT_HLIST_HEAD(&pp->owner);
  4326. if ((pp->next = head->chain) != NULL)
  4327. pp->next->pprev = &pp->next;
  4328. head->chain = pp;
  4329. pp->pprev = &head->chain;
  4330. }
  4331. return pp;
  4332. }
  4333. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  4334. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  4335. {
  4336. if (hlist_empty(&pp->owner)) {
  4337. if (pp->next)
  4338. pp->next->pprev = pp->pprev;
  4339. *(pp->pprev) = pp->next;
  4340. kmem_cache_free(sctp_bucket_cachep, pp);
  4341. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  4342. }
  4343. }
  4344. /* Release this socket's reference to a local port. */
  4345. static inline void __sctp_put_port(struct sock *sk)
  4346. {
  4347. struct sctp_bind_hashbucket *head =
  4348. &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
  4349. struct sctp_bind_bucket *pp;
  4350. sctp_spin_lock(&head->lock);
  4351. pp = sctp_sk(sk)->bind_hash;
  4352. __sk_del_bind_node(sk);
  4353. sctp_sk(sk)->bind_hash = NULL;
  4354. inet_sk(sk)->num = 0;
  4355. sctp_bucket_destroy(pp);
  4356. sctp_spin_unlock(&head->lock);
  4357. }
  4358. void sctp_put_port(struct sock *sk)
  4359. {
  4360. sctp_local_bh_disable();
  4361. __sctp_put_port(sk);
  4362. sctp_local_bh_enable();
  4363. }
  4364. /*
  4365. * The system picks an ephemeral port and choose an address set equivalent
  4366. * to binding with a wildcard address.
  4367. * One of those addresses will be the primary address for the association.
  4368. * This automatically enables the multihoming capability of SCTP.
  4369. */
  4370. static int sctp_autobind(struct sock *sk)
  4371. {
  4372. union sctp_addr autoaddr;
  4373. struct sctp_af *af;
  4374. unsigned short port;
  4375. /* Initialize a local sockaddr structure to INADDR_ANY. */
  4376. af = sctp_sk(sk)->pf->af;
  4377. port = htons(inet_sk(sk)->num);
  4378. af->inaddr_any(&autoaddr, port);
  4379. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  4380. }
  4381. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  4382. *
  4383. * From RFC 2292
  4384. * 4.2 The cmsghdr Structure *
  4385. *
  4386. * When ancillary data is sent or received, any number of ancillary data
  4387. * objects can be specified by the msg_control and msg_controllen members of
  4388. * the msghdr structure, because each object is preceded by
  4389. * a cmsghdr structure defining the object's length (the cmsg_len member).
  4390. * Historically Berkeley-derived implementations have passed only one object
  4391. * at a time, but this API allows multiple objects to be
  4392. * passed in a single call to sendmsg() or recvmsg(). The following example
  4393. * shows two ancillary data objects in a control buffer.
  4394. *
  4395. * |<--------------------------- msg_controllen -------------------------->|
  4396. * | |
  4397. *
  4398. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  4399. *
  4400. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  4401. * | | |
  4402. *
  4403. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  4404. *
  4405. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  4406. * | | | | |
  4407. *
  4408. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  4409. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  4410. *
  4411. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  4412. *
  4413. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  4414. * ^
  4415. * |
  4416. *
  4417. * msg_control
  4418. * points here
  4419. */
  4420. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
  4421. sctp_cmsgs_t *cmsgs)
  4422. {
  4423. struct cmsghdr *cmsg;
  4424. for (cmsg = CMSG_FIRSTHDR(msg);
  4425. cmsg != NULL;
  4426. cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
  4427. if (!CMSG_OK(msg, cmsg))
  4428. return -EINVAL;
  4429. /* Should we parse this header or ignore? */
  4430. if (cmsg->cmsg_level != IPPROTO_SCTP)
  4431. continue;
  4432. /* Strictly check lengths following example in SCM code. */
  4433. switch (cmsg->cmsg_type) {
  4434. case SCTP_INIT:
  4435. /* SCTP Socket API Extension
  4436. * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
  4437. *
  4438. * This cmsghdr structure provides information for
  4439. * initializing new SCTP associations with sendmsg().
  4440. * The SCTP_INITMSG socket option uses this same data
  4441. * structure. This structure is not used for
  4442. * recvmsg().
  4443. *
  4444. * cmsg_level cmsg_type cmsg_data[]
  4445. * ------------ ------------ ----------------------
  4446. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  4447. */
  4448. if (cmsg->cmsg_len !=
  4449. CMSG_LEN(sizeof(struct sctp_initmsg)))
  4450. return -EINVAL;
  4451. cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
  4452. break;
  4453. case SCTP_SNDRCV:
  4454. /* SCTP Socket API Extension
  4455. * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
  4456. *
  4457. * This cmsghdr structure specifies SCTP options for
  4458. * sendmsg() and describes SCTP header information
  4459. * about a received message through recvmsg().
  4460. *
  4461. * cmsg_level cmsg_type cmsg_data[]
  4462. * ------------ ------------ ----------------------
  4463. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  4464. */
  4465. if (cmsg->cmsg_len !=
  4466. CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  4467. return -EINVAL;
  4468. cmsgs->info =
  4469. (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
  4470. /* Minimally, validate the sinfo_flags. */
  4471. if (cmsgs->info->sinfo_flags &
  4472. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  4473. SCTP_ABORT | SCTP_EOF))
  4474. return -EINVAL;
  4475. break;
  4476. default:
  4477. return -EINVAL;
  4478. };
  4479. }
  4480. return 0;
  4481. }
  4482. /*
  4483. * Wait for a packet..
  4484. * Note: This function is the same function as in core/datagram.c
  4485. * with a few modifications to make lksctp work.
  4486. */
  4487. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
  4488. {
  4489. int error;
  4490. DEFINE_WAIT(wait);
  4491. prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  4492. /* Socket errors? */
  4493. error = sock_error(sk);
  4494. if (error)
  4495. goto out;
  4496. if (!skb_queue_empty(&sk->sk_receive_queue))
  4497. goto ready;
  4498. /* Socket shut down? */
  4499. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4500. goto out;
  4501. /* Sequenced packets can come disconnected. If so we report the
  4502. * problem.
  4503. */
  4504. error = -ENOTCONN;
  4505. /* Is there a good reason to think that we may receive some data? */
  4506. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  4507. goto out;
  4508. /* Handle signals. */
  4509. if (signal_pending(current))
  4510. goto interrupted;
  4511. /* Let another process have a go. Since we are going to sleep
  4512. * anyway. Note: This may cause odd behaviors if the message
  4513. * does not fit in the user's buffer, but this seems to be the
  4514. * only way to honor MSG_DONTWAIT realistically.
  4515. */
  4516. sctp_release_sock(sk);
  4517. *timeo_p = schedule_timeout(*timeo_p);
  4518. sctp_lock_sock(sk);
  4519. ready:
  4520. finish_wait(sk->sk_sleep, &wait);
  4521. return 0;
  4522. interrupted:
  4523. error = sock_intr_errno(*timeo_p);
  4524. out:
  4525. finish_wait(sk->sk_sleep, &wait);
  4526. *err = error;
  4527. return error;
  4528. }
  4529. /* Receive a datagram.
  4530. * Note: This is pretty much the same routine as in core/datagram.c
  4531. * with a few changes to make lksctp work.
  4532. */
  4533. static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  4534. int noblock, int *err)
  4535. {
  4536. int error;
  4537. struct sk_buff *skb;
  4538. long timeo;
  4539. timeo = sock_rcvtimeo(sk, noblock);
  4540. SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
  4541. timeo, MAX_SCHEDULE_TIMEOUT);
  4542. do {
  4543. /* Again only user level code calls this function,
  4544. * so nothing interrupt level
  4545. * will suddenly eat the receive_queue.
  4546. *
  4547. * Look at current nfs client by the way...
  4548. * However, this function was corrent in any case. 8)
  4549. */
  4550. if (flags & MSG_PEEK) {
  4551. spin_lock_bh(&sk->sk_receive_queue.lock);
  4552. skb = skb_peek(&sk->sk_receive_queue);
  4553. if (skb)
  4554. atomic_inc(&skb->users);
  4555. spin_unlock_bh(&sk->sk_receive_queue.lock);
  4556. } else {
  4557. skb = skb_dequeue(&sk->sk_receive_queue);
  4558. }
  4559. if (skb)
  4560. return skb;
  4561. /* Caller is allowed not to check sk->sk_err before calling. */
  4562. error = sock_error(sk);
  4563. if (error)
  4564. goto no_packet;
  4565. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4566. break;
  4567. /* User doesn't want to wait. */
  4568. error = -EAGAIN;
  4569. if (!timeo)
  4570. goto no_packet;
  4571. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  4572. return NULL;
  4573. no_packet:
  4574. *err = error;
  4575. return NULL;
  4576. }
  4577. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  4578. static void __sctp_write_space(struct sctp_association *asoc)
  4579. {
  4580. struct sock *sk = asoc->base.sk;
  4581. struct socket *sock = sk->sk_socket;
  4582. if ((sctp_wspace(asoc) > 0) && sock) {
  4583. if (waitqueue_active(&asoc->wait))
  4584. wake_up_interruptible(&asoc->wait);
  4585. if (sctp_writeable(sk)) {
  4586. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  4587. wake_up_interruptible(sk->sk_sleep);
  4588. /* Note that we try to include the Async I/O support
  4589. * here by modeling from the current TCP/UDP code.
  4590. * We have not tested with it yet.
  4591. */
  4592. if (sock->fasync_list &&
  4593. !(sk->sk_shutdown & SEND_SHUTDOWN))
  4594. sock_wake_async(sock, 2, POLL_OUT);
  4595. }
  4596. }
  4597. }
  4598. /* Do accounting for the sndbuf space.
  4599. * Decrement the used sndbuf space of the corresponding association by the
  4600. * data size which was just transmitted(freed).
  4601. */
  4602. static void sctp_wfree(struct sk_buff *skb)
  4603. {
  4604. struct sctp_association *asoc;
  4605. struct sctp_chunk *chunk;
  4606. struct sock *sk;
  4607. /* Get the saved chunk pointer. */
  4608. chunk = *((struct sctp_chunk **)(skb->cb));
  4609. asoc = chunk->asoc;
  4610. sk = asoc->base.sk;
  4611. asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
  4612. sizeof(struct sk_buff) +
  4613. sizeof(struct sctp_chunk);
  4614. atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  4615. sock_wfree(skb);
  4616. __sctp_write_space(asoc);
  4617. sctp_association_put(asoc);
  4618. }
  4619. /* Helper function to wait for space in the sndbuf. */
  4620. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  4621. size_t msg_len)
  4622. {
  4623. struct sock *sk = asoc->base.sk;
  4624. int err = 0;
  4625. long current_timeo = *timeo_p;
  4626. DEFINE_WAIT(wait);
  4627. SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
  4628. asoc, (long)(*timeo_p), msg_len);
  4629. /* Increment the association's refcnt. */
  4630. sctp_association_hold(asoc);
  4631. /* Wait on the association specific sndbuf space. */
  4632. for (;;) {
  4633. prepare_to_wait_exclusive(&asoc->wait, &wait,
  4634. TASK_INTERRUPTIBLE);
  4635. if (!*timeo_p)
  4636. goto do_nonblock;
  4637. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  4638. asoc->base.dead)
  4639. goto do_error;
  4640. if (signal_pending(current))
  4641. goto do_interrupted;
  4642. if (msg_len <= sctp_wspace(asoc))
  4643. break;
  4644. /* Let another process have a go. Since we are going
  4645. * to sleep anyway.
  4646. */
  4647. sctp_release_sock(sk);
  4648. current_timeo = schedule_timeout(current_timeo);
  4649. BUG_ON(sk != asoc->base.sk);
  4650. sctp_lock_sock(sk);
  4651. *timeo_p = current_timeo;
  4652. }
  4653. out:
  4654. finish_wait(&asoc->wait, &wait);
  4655. /* Release the association's refcnt. */
  4656. sctp_association_put(asoc);
  4657. return err;
  4658. do_error:
  4659. err = -EPIPE;
  4660. goto out;
  4661. do_interrupted:
  4662. err = sock_intr_errno(*timeo_p);
  4663. goto out;
  4664. do_nonblock:
  4665. err = -EAGAIN;
  4666. goto out;
  4667. }
  4668. /* If socket sndbuf has changed, wake up all per association waiters. */
  4669. void sctp_write_space(struct sock *sk)
  4670. {
  4671. struct sctp_association *asoc;
  4672. struct list_head *pos;
  4673. /* Wake up the tasks in each wait queue. */
  4674. list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
  4675. asoc = list_entry(pos, struct sctp_association, asocs);
  4676. __sctp_write_space(asoc);
  4677. }
  4678. }
  4679. /* Is there any sndbuf space available on the socket?
  4680. *
  4681. * Note that sk_wmem_alloc is the sum of the send buffers on all of the
  4682. * associations on the same socket. For a UDP-style socket with
  4683. * multiple associations, it is possible for it to be "unwriteable"
  4684. * prematurely. I assume that this is acceptable because
  4685. * a premature "unwriteable" is better than an accidental "writeable" which
  4686. * would cause an unwanted block under certain circumstances. For the 1-1
  4687. * UDP-style sockets or TCP-style sockets, this code should work.
  4688. * - Daisy
  4689. */
  4690. static int sctp_writeable(struct sock *sk)
  4691. {
  4692. int amt = 0;
  4693. amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
  4694. if (amt < 0)
  4695. amt = 0;
  4696. return amt;
  4697. }
  4698. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  4699. * returns immediately with EINPROGRESS.
  4700. */
  4701. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  4702. {
  4703. struct sock *sk = asoc->base.sk;
  4704. int err = 0;
  4705. long current_timeo = *timeo_p;
  4706. DEFINE_WAIT(wait);
  4707. SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
  4708. (long)(*timeo_p));
  4709. /* Increment the association's refcnt. */
  4710. sctp_association_hold(asoc);
  4711. for (;;) {
  4712. prepare_to_wait_exclusive(&asoc->wait, &wait,
  4713. TASK_INTERRUPTIBLE);
  4714. if (!*timeo_p)
  4715. goto do_nonblock;
  4716. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4717. break;
  4718. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  4719. asoc->base.dead)
  4720. goto do_error;
  4721. if (signal_pending(current))
  4722. goto do_interrupted;
  4723. if (sctp_state(asoc, ESTABLISHED))
  4724. break;
  4725. /* Let another process have a go. Since we are going
  4726. * to sleep anyway.
  4727. */
  4728. sctp_release_sock(sk);
  4729. current_timeo = schedule_timeout(current_timeo);
  4730. sctp_lock_sock(sk);
  4731. *timeo_p = current_timeo;
  4732. }
  4733. out:
  4734. finish_wait(&asoc->wait, &wait);
  4735. /* Release the association's refcnt. */
  4736. sctp_association_put(asoc);
  4737. return err;
  4738. do_error:
  4739. if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
  4740. err = -ETIMEDOUT;
  4741. else
  4742. err = -ECONNREFUSED;
  4743. goto out;
  4744. do_interrupted:
  4745. err = sock_intr_errno(*timeo_p);
  4746. goto out;
  4747. do_nonblock:
  4748. err = -EINPROGRESS;
  4749. goto out;
  4750. }
  4751. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  4752. {
  4753. struct sctp_endpoint *ep;
  4754. int err = 0;
  4755. DEFINE_WAIT(wait);
  4756. ep = sctp_sk(sk)->ep;
  4757. for (;;) {
  4758. prepare_to_wait_exclusive(sk->sk_sleep, &wait,
  4759. TASK_INTERRUPTIBLE);
  4760. if (list_empty(&ep->asocs)) {
  4761. sctp_release_sock(sk);
  4762. timeo = schedule_timeout(timeo);
  4763. sctp_lock_sock(sk);
  4764. }
  4765. err = -EINVAL;
  4766. if (!sctp_sstate(sk, LISTENING))
  4767. break;
  4768. err = 0;
  4769. if (!list_empty(&ep->asocs))
  4770. break;
  4771. err = sock_intr_errno(timeo);
  4772. if (signal_pending(current))
  4773. break;
  4774. err = -EAGAIN;
  4775. if (!timeo)
  4776. break;
  4777. }
  4778. finish_wait(sk->sk_sleep, &wait);
  4779. return err;
  4780. }
  4781. void sctp_wait_for_close(struct sock *sk, long timeout)
  4782. {
  4783. DEFINE_WAIT(wait);
  4784. do {
  4785. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  4786. if (list_empty(&sctp_sk(sk)->ep->asocs))
  4787. break;
  4788. sctp_release_sock(sk);
  4789. timeout = schedule_timeout(timeout);
  4790. sctp_lock_sock(sk);
  4791. } while (!signal_pending(current) && timeout);
  4792. finish_wait(sk->sk_sleep, &wait);
  4793. }
  4794. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  4795. * and its messages to the newsk.
  4796. */
  4797. static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  4798. struct sctp_association *assoc,
  4799. sctp_socket_type_t type)
  4800. {
  4801. struct sctp_sock *oldsp = sctp_sk(oldsk);
  4802. struct sctp_sock *newsp = sctp_sk(newsk);
  4803. struct sctp_bind_bucket *pp; /* hash list port iterator */
  4804. struct sctp_endpoint *newep = newsp->ep;
  4805. struct sk_buff *skb, *tmp;
  4806. struct sctp_ulpevent *event;
  4807. int flags = 0;
  4808. /* Migrate socket buffer sizes and all the socket level options to the
  4809. * new socket.
  4810. */
  4811. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  4812. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  4813. /* Brute force copy old sctp opt. */
  4814. inet_sk_copy_descendant(newsk, oldsk);
  4815. /* Restore the ep value that was overwritten with the above structure
  4816. * copy.
  4817. */
  4818. newsp->ep = newep;
  4819. newsp->hmac = NULL;
  4820. /* Hook this new socket in to the bind_hash list. */
  4821. pp = sctp_sk(oldsk)->bind_hash;
  4822. sk_add_bind_node(newsk, &pp->owner);
  4823. sctp_sk(newsk)->bind_hash = pp;
  4824. inet_sk(newsk)->num = inet_sk(oldsk)->num;
  4825. /* Copy the bind_addr list from the original endpoint to the new
  4826. * endpoint so that we can handle restarts properly
  4827. */
  4828. if (assoc->peer.ipv4_address)
  4829. flags |= SCTP_ADDR4_PEERSUPP;
  4830. if (assoc->peer.ipv6_address)
  4831. flags |= SCTP_ADDR6_PEERSUPP;
  4832. sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
  4833. &oldsp->ep->base.bind_addr,
  4834. SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
  4835. /* Move any messages in the old socket's receive queue that are for the
  4836. * peeled off association to the new socket's receive queue.
  4837. */
  4838. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  4839. event = sctp_skb2event(skb);
  4840. if (event->asoc == assoc) {
  4841. sock_rfree(skb);
  4842. __skb_unlink(skb, &oldsk->sk_receive_queue);
  4843. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  4844. skb_set_owner_r(skb, newsk);
  4845. }
  4846. }
  4847. /* Clean up any messages pending delivery due to partial
  4848. * delivery. Three cases:
  4849. * 1) No partial deliver; no work.
  4850. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  4851. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  4852. */
  4853. skb_queue_head_init(&newsp->pd_lobby);
  4854. sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
  4855. if (sctp_sk(oldsk)->pd_mode) {
  4856. struct sk_buff_head *queue;
  4857. /* Decide which queue to move pd_lobby skbs to. */
  4858. if (assoc->ulpq.pd_mode) {
  4859. queue = &newsp->pd_lobby;
  4860. } else
  4861. queue = &newsk->sk_receive_queue;
  4862. /* Walk through the pd_lobby, looking for skbs that
  4863. * need moved to the new socket.
  4864. */
  4865. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  4866. event = sctp_skb2event(skb);
  4867. if (event->asoc == assoc) {
  4868. sock_rfree(skb);
  4869. __skb_unlink(skb, &oldsp->pd_lobby);
  4870. __skb_queue_tail(queue, skb);
  4871. skb_set_owner_r(skb, newsk);
  4872. }
  4873. }
  4874. /* Clear up any skbs waiting for the partial
  4875. * delivery to finish.
  4876. */
  4877. if (assoc->ulpq.pd_mode)
  4878. sctp_clear_pd(oldsk);
  4879. }
  4880. /* Set the type of socket to indicate that it is peeled off from the
  4881. * original UDP-style socket or created with the accept() call on a
  4882. * TCP-style socket..
  4883. */
  4884. newsp->type = type;
  4885. /* Mark the new socket "in-use" by the user so that any packets
  4886. * that may arrive on the association after we've moved it are
  4887. * queued to the backlog. This prevents a potential race between
  4888. * backlog processing on the old socket and new-packet processing
  4889. * on the new socket.
  4890. */
  4891. sctp_lock_sock(newsk);
  4892. sctp_assoc_migrate(assoc, newsk);
  4893. /* If the association on the newsk is already closed before accept()
  4894. * is called, set RCV_SHUTDOWN flag.
  4895. */
  4896. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
  4897. newsk->sk_shutdown |= RCV_SHUTDOWN;
  4898. newsk->sk_state = SCTP_SS_ESTABLISHED;
  4899. sctp_release_sock(newsk);
  4900. }
  4901. /* This proto struct describes the ULP interface for SCTP. */
  4902. struct proto sctp_prot = {
  4903. .name = "SCTP",
  4904. .owner = THIS_MODULE,
  4905. .close = sctp_close,
  4906. .connect = sctp_connect,
  4907. .disconnect = sctp_disconnect,
  4908. .accept = sctp_accept,
  4909. .ioctl = sctp_ioctl,
  4910. .init = sctp_init_sock,
  4911. .destroy = sctp_destroy_sock,
  4912. .shutdown = sctp_shutdown,
  4913. .setsockopt = sctp_setsockopt,
  4914. .getsockopt = sctp_getsockopt,
  4915. .sendmsg = sctp_sendmsg,
  4916. .recvmsg = sctp_recvmsg,
  4917. .bind = sctp_bind,
  4918. .backlog_rcv = sctp_backlog_rcv,
  4919. .hash = sctp_hash,
  4920. .unhash = sctp_unhash,
  4921. .get_port = sctp_get_port,
  4922. .obj_size = sizeof(struct sctp_sock),
  4923. };
  4924. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  4925. struct proto sctpv6_prot = {
  4926. .name = "SCTPv6",
  4927. .owner = THIS_MODULE,
  4928. .close = sctp_close,
  4929. .connect = sctp_connect,
  4930. .disconnect = sctp_disconnect,
  4931. .accept = sctp_accept,
  4932. .ioctl = sctp_ioctl,
  4933. .init = sctp_init_sock,
  4934. .destroy = sctp_destroy_sock,
  4935. .shutdown = sctp_shutdown,
  4936. .setsockopt = sctp_setsockopt,
  4937. .getsockopt = sctp_getsockopt,
  4938. .sendmsg = sctp_sendmsg,
  4939. .recvmsg = sctp_recvmsg,
  4940. .bind = sctp_bind,
  4941. .backlog_rcv = sctp_backlog_rcv,
  4942. .hash = sctp_hash,
  4943. .unhash = sctp_unhash,
  4944. .get_port = sctp_get_port,
  4945. .obj_size = sizeof(struct sctp6_sock),
  4946. };
  4947. #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */