socket.c 155 KB

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