socket.c 173 KB

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