socket.c 267 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * (C) Copyright IBM Corp. 2001, 2004
  4. * Copyright (c) 1999-2000 Cisco, Inc.
  5. * Copyright (c) 1999-2001 Motorola, Inc.
  6. * Copyright (c) 2001-2003 Intel Corp.
  7. * Copyright (c) 2001-2002 Nokia, Inc.
  8. * Copyright (c) 2001 La Monte H.P. Yarroll
  9. *
  10. * This file is part of the SCTP kernel implementation
  11. *
  12. * These functions interface with the sockets layer to implement the
  13. * SCTP Extensions for the Sockets API.
  14. *
  15. * Note that the descriptions from the specification are USER level
  16. * functions--this file is the functions which populate the struct proto
  17. * for SCTP which is the BOTTOM of the sockets interface.
  18. *
  19. * Please send any bug reports or fixes you make to the
  20. * email address(es):
  21. * lksctp developers <linux-sctp@vger.kernel.org>
  22. *
  23. * Written or modified by:
  24. * La Monte H.P. Yarroll <piggy@acm.org>
  25. * Narasimha Budihal <narsi@refcode.org>
  26. * Karl Knutson <karl@athena.chicago.il.us>
  27. * Jon Grimm <jgrimm@us.ibm.com>
  28. * Xingang Guo <xingang.guo@intel.com>
  29. * Daisy Chang <daisyc@us.ibm.com>
  30. * Sridhar Samudrala <samudrala@us.ibm.com>
  31. * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
  32. * Ardelle Fan <ardelle.fan@intel.com>
  33. * Ryan Layer <rmlayer@us.ibm.com>
  34. * Anup Pemmaiah <pemmaiah@cc.usu.edu>
  35. * Kevin Gao <kevin.gao@intel.com>
  36. */
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #include <crypto/hash.h>
  39. #include <linux/types.h>
  40. #include <linux/kernel.h>
  41. #include <linux/wait.h>
  42. #include <linux/time.h>
  43. #include <linux/sched/signal.h>
  44. #include <linux/ip.h>
  45. #include <linux/capability.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/poll.h>
  48. #include <linux/init.h>
  49. #include <linux/slab.h>
  50. #include <linux/file.h>
  51. #include <linux/compat.h>
  52. #include <linux/rhashtable.h>
  53. #include <net/ip.h>
  54. #include <net/icmp.h>
  55. #include <net/route.h>
  56. #include <net/ipv6.h>
  57. #include <net/inet_common.h>
  58. #include <net/busy_poll.h>
  59. #include <trace/events/sock.h>
  60. #include <linux/socket.h> /* for sa_family_t */
  61. #include <linux/export.h>
  62. #include <net/sock.h>
  63. #include <net/sctp/sctp.h>
  64. #include <net/sctp/sm.h>
  65. #include <net/sctp/stream_sched.h>
  66. #include <net/rps.h>
  67. /* Forward declarations for internal helper functions. */
  68. static bool sctp_writeable(const struct sock *sk);
  69. static void sctp_wfree(struct sk_buff *skb);
  70. static int sctp_wait_for_sndbuf(struct sctp_association *asoc,
  71. struct sctp_transport *transport,
  72. long *timeo_p, size_t msg_len);
  73. static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p);
  74. static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
  75. static int sctp_wait_for_accept(struct sock *sk, long timeo);
  76. static void sctp_wait_for_close(struct sock *sk, long timeo);
  77. static void sctp_destruct_sock(struct sock *sk);
  78. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  79. union sctp_addr *addr, int len);
  80. static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
  81. static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
  82. static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
  83. static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
  84. static int sctp_send_asconf(struct sctp_association *asoc,
  85. struct sctp_chunk *chunk);
  86. static int sctp_do_bind(struct sock *, union sctp_addr *, int);
  87. static int sctp_autobind(struct sock *sk);
  88. static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  89. struct sctp_association *assoc,
  90. enum sctp_socket_type type);
  91. static unsigned long sctp_memory_pressure;
  92. static atomic_long_t sctp_memory_allocated;
  93. static DEFINE_PER_CPU(int, sctp_memory_per_cpu_fw_alloc);
  94. struct percpu_counter sctp_sockets_allocated;
  95. static void sctp_enter_memory_pressure(struct sock *sk)
  96. {
  97. WRITE_ONCE(sctp_memory_pressure, 1);
  98. }
  99. /* Get the sndbuf space available at the time on the association. */
  100. static inline int sctp_wspace(struct sctp_association *asoc)
  101. {
  102. struct sock *sk = asoc->base.sk;
  103. return asoc->ep->sndbuf_policy ? sk->sk_sndbuf - asoc->sndbuf_used
  104. : sk_stream_wspace(sk);
  105. }
  106. /* Increment the used sndbuf space count of the corresponding association by
  107. * the size of the outgoing data chunk.
  108. * Also, set the skb destructor for sndbuf accounting later.
  109. *
  110. * Since it is always 1-1 between chunk and skb, and also a new skb is always
  111. * allocated for chunk bundling in sctp_packet_transmit(), we can use the
  112. * destructor in the data chunk skb for the purpose of the sndbuf space
  113. * tracking.
  114. */
  115. static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
  116. {
  117. struct sctp_association *asoc = chunk->asoc;
  118. struct sock *sk = asoc->base.sk;
  119. /* The sndbuf space is tracked per association. */
  120. sctp_association_hold(asoc);
  121. if (chunk->shkey)
  122. sctp_auth_shkey_hold(chunk->shkey);
  123. skb_set_owner_w(chunk->skb, sk);
  124. chunk->skb->destructor = sctp_wfree;
  125. /* Save the chunk pointer in skb for sctp_wfree to use later. */
  126. skb_shinfo(chunk->skb)->destructor_arg = chunk;
  127. refcount_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  128. asoc->sndbuf_used += chunk->skb->truesize + sizeof(struct sctp_chunk);
  129. sk_wmem_queued_add(sk, chunk->skb->truesize + sizeof(struct sctp_chunk));
  130. sk_mem_charge(sk, chunk->skb->truesize);
  131. }
  132. static void sctp_clear_owner_w(struct sctp_chunk *chunk)
  133. {
  134. skb_orphan(chunk->skb);
  135. }
  136. #define traverse_and_process() \
  137. do { \
  138. msg = chunk->msg; \
  139. if (msg == prev_msg) \
  140. continue; \
  141. list_for_each_entry(c, &msg->chunks, frag_list) { \
  142. if ((clear && asoc->base.sk == c->skb->sk) || \
  143. (!clear && asoc->base.sk != c->skb->sk)) \
  144. cb(c); \
  145. } \
  146. prev_msg = msg; \
  147. } while (0)
  148. static void sctp_for_each_tx_datachunk(struct sctp_association *asoc,
  149. bool clear,
  150. void (*cb)(struct sctp_chunk *))
  151. {
  152. struct sctp_datamsg *msg, *prev_msg = NULL;
  153. struct sctp_outq *q = &asoc->outqueue;
  154. struct sctp_chunk *chunk, *c;
  155. struct sctp_transport *t;
  156. list_for_each_entry(t, &asoc->peer.transport_addr_list, transports)
  157. list_for_each_entry(chunk, &t->transmitted, transmitted_list)
  158. traverse_and_process();
  159. list_for_each_entry(chunk, &q->retransmit, transmitted_list)
  160. traverse_and_process();
  161. list_for_each_entry(chunk, &q->sacked, transmitted_list)
  162. traverse_and_process();
  163. list_for_each_entry(chunk, &q->abandoned, transmitted_list)
  164. traverse_and_process();
  165. list_for_each_entry(chunk, &q->out_chunk_list, list)
  166. traverse_and_process();
  167. }
  168. static void sctp_for_each_rx_skb(struct sctp_association *asoc, struct sock *sk,
  169. void (*cb)(struct sk_buff *, struct sock *))
  170. {
  171. struct sk_buff *skb, *tmp;
  172. sctp_skb_for_each(skb, &asoc->ulpq.lobby, tmp)
  173. cb(skb, sk);
  174. sctp_skb_for_each(skb, &asoc->ulpq.reasm, tmp)
  175. cb(skb, sk);
  176. sctp_skb_for_each(skb, &asoc->ulpq.reasm_uo, tmp)
  177. cb(skb, sk);
  178. }
  179. /* Verify that this is a valid address. */
  180. static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
  181. int len)
  182. {
  183. struct sctp_af *af;
  184. /* Verify basic sockaddr. */
  185. af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
  186. if (!af)
  187. return -EINVAL;
  188. /* Is this a valid SCTP address? */
  189. if (!af->addr_valid(addr, sctp_sk(sk), NULL))
  190. return -EINVAL;
  191. if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
  192. return -EINVAL;
  193. return 0;
  194. }
  195. /* Look up the association by its id. If this is not a UDP-style
  196. * socket, the ID field is always ignored.
  197. */
  198. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
  199. {
  200. struct sctp_association *asoc = NULL;
  201. /* If this is not a UDP-style socket, assoc id should be ignored. */
  202. if (!sctp_style(sk, UDP)) {
  203. /* Return NULL if the socket state is not ESTABLISHED. It
  204. * could be a TCP-style listening socket or a socket which
  205. * hasn't yet called connect() to establish an association.
  206. */
  207. if (!sctp_sstate(sk, ESTABLISHED) && !sctp_sstate(sk, CLOSING))
  208. return NULL;
  209. /* Get the first and the only association from the list. */
  210. if (!list_empty(&sctp_sk(sk)->ep->asocs))
  211. asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
  212. struct sctp_association, asocs);
  213. return asoc;
  214. }
  215. /* Otherwise this is a UDP-style socket. */
  216. if (id <= SCTP_ALL_ASSOC)
  217. return NULL;
  218. spin_lock_bh(&sctp_assocs_id_lock);
  219. asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
  220. if (asoc && (asoc->base.sk != sk || asoc->base.dead))
  221. asoc = NULL;
  222. spin_unlock_bh(&sctp_assocs_id_lock);
  223. return asoc;
  224. }
  225. /* Look up the transport from an address and an assoc id. If both address and
  226. * id are specified, the associations matching the address and the id should be
  227. * the same.
  228. */
  229. static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
  230. struct sockaddr_storage *addr,
  231. sctp_assoc_t id)
  232. {
  233. struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
  234. struct sctp_af *af = sctp_get_af_specific(addr->ss_family);
  235. union sctp_addr *laddr = (union sctp_addr *)addr;
  236. struct sctp_transport *transport;
  237. if (!af || sctp_verify_addr(sk, laddr, af->sockaddr_len))
  238. return NULL;
  239. addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
  240. laddr,
  241. &transport);
  242. if (!addr_asoc)
  243. return NULL;
  244. id_asoc = sctp_id2assoc(sk, id);
  245. if (id_asoc && (id_asoc != addr_asoc))
  246. return NULL;
  247. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
  248. (union sctp_addr *)addr);
  249. return transport;
  250. }
  251. /* API 3.1.2 bind() - UDP Style Syntax
  252. * The syntax of bind() is,
  253. *
  254. * ret = bind(int sd, struct sockaddr *addr, int addrlen);
  255. *
  256. * sd - the socket descriptor returned by socket().
  257. * addr - the address structure (struct sockaddr_in or struct
  258. * sockaddr_in6 [RFC 2553]),
  259. * addr_len - the size of the address structure.
  260. */
  261. static int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
  262. {
  263. int retval = 0;
  264. lock_sock(sk);
  265. pr_debug("%s: sk:%p, addr:%p, addr_len:%d\n", __func__, sk,
  266. addr, addr_len);
  267. /* Disallow binding twice. */
  268. if (!sctp_sk(sk)->ep->base.bind_addr.port)
  269. retval = sctp_do_bind(sk, (union sctp_addr *)addr,
  270. addr_len);
  271. else
  272. retval = -EINVAL;
  273. release_sock(sk);
  274. return retval;
  275. }
  276. static int sctp_get_port_local(struct sock *, union sctp_addr *);
  277. /* Verify this is a valid sockaddr. */
  278. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  279. union sctp_addr *addr, int len)
  280. {
  281. struct sctp_af *af;
  282. /* Check minimum size. */
  283. if (len < sizeof (struct sockaddr))
  284. return NULL;
  285. if (!opt->pf->af_supported(addr->sa.sa_family, opt))
  286. return NULL;
  287. if (addr->sa.sa_family == AF_INET6) {
  288. if (len < SIN6_LEN_RFC2133)
  289. return NULL;
  290. /* V4 mapped address are really of AF_INET family */
  291. if (ipv6_addr_v4mapped(&addr->v6.sin6_addr) &&
  292. !opt->pf->af_supported(AF_INET, opt))
  293. return NULL;
  294. }
  295. /* If we get this far, af is valid. */
  296. af = sctp_get_af_specific(addr->sa.sa_family);
  297. if (len < af->sockaddr_len)
  298. return NULL;
  299. return af;
  300. }
  301. static void sctp_auto_asconf_init(struct sctp_sock *sp)
  302. {
  303. struct net *net = sock_net(&sp->inet.sk);
  304. if (net->sctp.default_auto_asconf) {
  305. spin_lock_bh(&net->sctp.addr_wq_lock);
  306. list_add_tail(&sp->auto_asconf_list, &net->sctp.auto_asconf_splist);
  307. spin_unlock_bh(&net->sctp.addr_wq_lock);
  308. sp->do_auto_asconf = 1;
  309. }
  310. }
  311. /* Bind a local address either to an endpoint or to an association. */
  312. static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
  313. {
  314. struct net *net = sock_net(sk);
  315. struct sctp_sock *sp = sctp_sk(sk);
  316. struct sctp_endpoint *ep = sp->ep;
  317. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  318. struct sctp_af *af;
  319. unsigned short snum;
  320. int ret = 0;
  321. /* Common sockaddr verification. */
  322. af = sctp_sockaddr_af(sp, addr, len);
  323. if (!af) {
  324. pr_debug("%s: sk:%p, newaddr:%p, len:%d EINVAL\n",
  325. __func__, sk, addr, len);
  326. return -EINVAL;
  327. }
  328. snum = ntohs(addr->v4.sin_port);
  329. pr_debug("%s: sk:%p, new addr:%pISc, port:%d, new port:%d, len:%d\n",
  330. __func__, sk, &addr->sa, bp->port, snum, len);
  331. /* PF specific bind() address verification. */
  332. if (!sp->pf->bind_verify(sp, addr))
  333. return -EADDRNOTAVAIL;
  334. /* We must either be unbound, or bind to the same port.
  335. * It's OK to allow 0 ports if we are already bound.
  336. * We'll just inhert an already bound port in this case
  337. */
  338. if (bp->port) {
  339. if (!snum)
  340. snum = bp->port;
  341. else if (snum != bp->port) {
  342. pr_debug("%s: new port %d doesn't match existing port "
  343. "%d\n", __func__, snum, bp->port);
  344. return -EINVAL;
  345. }
  346. }
  347. if (snum && inet_port_requires_bind_service(net, snum) &&
  348. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  349. return -EACCES;
  350. /* See if the address matches any of the addresses we may have
  351. * already bound before checking against other endpoints.
  352. */
  353. if (sctp_bind_addr_match(bp, addr, sp))
  354. return -EINVAL;
  355. /* Make sure we are allowed to bind here.
  356. * The function sctp_get_port_local() does duplicate address
  357. * detection.
  358. */
  359. addr->v4.sin_port = htons(snum);
  360. if (sctp_get_port_local(sk, addr))
  361. return -EADDRINUSE;
  362. /* Refresh ephemeral port. */
  363. if (!bp->port) {
  364. bp->port = inet_sk(sk)->inet_num;
  365. sctp_auto_asconf_init(sp);
  366. }
  367. /* Add the address to the bind address list.
  368. * Use GFP_ATOMIC since BHs will be disabled.
  369. */
  370. ret = sctp_add_bind_addr(bp, addr, af->sockaddr_len,
  371. SCTP_ADDR_SRC, GFP_ATOMIC);
  372. if (ret) {
  373. sctp_put_port(sk);
  374. return ret;
  375. }
  376. /* Copy back into socket for getsockname() use. */
  377. inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num);
  378. sp->pf->to_sk_saddr(addr, sk);
  379. return ret;
  380. }
  381. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  382. *
  383. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  384. * at any one time. If a sender, after sending an ASCONF chunk, decides
  385. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  386. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  387. * subsequent ASCONF. Note this restriction binds each side, so at any
  388. * time two ASCONF may be in-transit on any given association (one sent
  389. * from each endpoint).
  390. */
  391. static int sctp_send_asconf(struct sctp_association *asoc,
  392. struct sctp_chunk *chunk)
  393. {
  394. int retval = 0;
  395. /* If there is an outstanding ASCONF chunk, queue it for later
  396. * transmission.
  397. */
  398. if (asoc->addip_last_asconf) {
  399. list_add_tail(&chunk->list, &asoc->addip_chunk_list);
  400. goto out;
  401. }
  402. /* Hold the chunk until an ASCONF_ACK is received. */
  403. sctp_chunk_hold(chunk);
  404. retval = sctp_primitive_ASCONF(asoc->base.net, asoc, chunk);
  405. if (retval)
  406. sctp_chunk_free(chunk);
  407. else
  408. asoc->addip_last_asconf = chunk;
  409. out:
  410. return retval;
  411. }
  412. /* Add a list of addresses as bind addresses to local endpoint or
  413. * association.
  414. *
  415. * Basically run through each address specified in the addrs/addrcnt
  416. * array/length pair, determine if it is IPv6 or IPv4 and call
  417. * sctp_do_bind() on it.
  418. *
  419. * If any of them fails, then the operation will be reversed and the
  420. * ones that were added will be removed.
  421. *
  422. * Only sctp_setsockopt_bindx() is supposed to call this function.
  423. */
  424. static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  425. {
  426. int cnt;
  427. int retval = 0;
  428. void *addr_buf;
  429. struct sockaddr *sa_addr;
  430. struct sctp_af *af;
  431. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n", __func__, sk,
  432. addrs, addrcnt);
  433. addr_buf = addrs;
  434. for (cnt = 0; cnt < addrcnt; cnt++) {
  435. /* The list may contain either IPv4 or IPv6 address;
  436. * determine the address length for walking thru the list.
  437. */
  438. sa_addr = addr_buf;
  439. af = sctp_get_af_specific(sa_addr->sa_family);
  440. if (!af) {
  441. retval = -EINVAL;
  442. goto err_bindx_add;
  443. }
  444. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  445. af->sockaddr_len);
  446. addr_buf += af->sockaddr_len;
  447. err_bindx_add:
  448. if (retval < 0) {
  449. /* Failed. Cleanup the ones that have been added */
  450. if (cnt > 0)
  451. sctp_bindx_rem(sk, addrs, cnt);
  452. return retval;
  453. }
  454. }
  455. return retval;
  456. }
  457. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  458. * associations that are part of the endpoint indicating that a list of local
  459. * addresses are added to the endpoint.
  460. *
  461. * If any of the addresses is already in the bind address list of the
  462. * association, we do not send the chunk for that association. But it will not
  463. * affect other associations.
  464. *
  465. * Only sctp_setsockopt_bindx() is supposed to call this function.
  466. */
  467. static int sctp_send_asconf_add_ip(struct sock *sk,
  468. struct sockaddr *addrs,
  469. int addrcnt)
  470. {
  471. struct sctp_sock *sp;
  472. struct sctp_endpoint *ep;
  473. struct sctp_association *asoc;
  474. struct sctp_bind_addr *bp;
  475. struct sctp_chunk *chunk;
  476. struct sctp_sockaddr_entry *laddr;
  477. union sctp_addr *addr;
  478. union sctp_addr saveaddr;
  479. void *addr_buf;
  480. struct sctp_af *af;
  481. struct list_head *p;
  482. int i;
  483. int retval = 0;
  484. sp = sctp_sk(sk);
  485. ep = sp->ep;
  486. if (!ep->asconf_enable)
  487. return retval;
  488. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  489. __func__, sk, addrs, addrcnt);
  490. list_for_each_entry(asoc, &ep->asocs, asocs) {
  491. if (!asoc->peer.asconf_capable)
  492. continue;
  493. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  494. continue;
  495. if (!sctp_state(asoc, ESTABLISHED))
  496. continue;
  497. /* Check if any address in the packed array of addresses is
  498. * in the bind address list of the association. If so,
  499. * do not send the asconf chunk to its peer, but continue with
  500. * other associations.
  501. */
  502. addr_buf = addrs;
  503. for (i = 0; i < addrcnt; i++) {
  504. addr = addr_buf;
  505. af = sctp_get_af_specific(addr->v4.sin_family);
  506. if (!af) {
  507. retval = -EINVAL;
  508. goto out;
  509. }
  510. if (sctp_assoc_lookup_laddr(asoc, addr))
  511. break;
  512. addr_buf += af->sockaddr_len;
  513. }
  514. if (i < addrcnt)
  515. continue;
  516. /* Use the first valid address in bind addr list of
  517. * association as Address Parameter of ASCONF CHUNK.
  518. */
  519. bp = &asoc->base.bind_addr;
  520. p = bp->address_list.next;
  521. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  522. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  523. addrcnt, SCTP_PARAM_ADD_IP);
  524. if (!chunk) {
  525. retval = -ENOMEM;
  526. goto out;
  527. }
  528. /* Add the new addresses to the bind address list with
  529. * use_as_src set to 0.
  530. */
  531. addr_buf = addrs;
  532. for (i = 0; i < addrcnt; i++) {
  533. addr = addr_buf;
  534. af = sctp_get_af_specific(addr->v4.sin_family);
  535. memcpy(&saveaddr, addr, af->sockaddr_len);
  536. retval = sctp_add_bind_addr(bp, &saveaddr,
  537. sizeof(saveaddr),
  538. SCTP_ADDR_NEW, GFP_ATOMIC);
  539. addr_buf += af->sockaddr_len;
  540. }
  541. if (asoc->src_out_of_asoc_ok) {
  542. struct sctp_transport *trans;
  543. list_for_each_entry(trans,
  544. &asoc->peer.transport_addr_list, transports) {
  545. trans->cwnd = min(4*asoc->pathmtu, max_t(__u32,
  546. 2*asoc->pathmtu, 4380));
  547. trans->ssthresh = asoc->peer.i.a_rwnd;
  548. trans->rto = asoc->rto_initial;
  549. sctp_max_rto(asoc, trans);
  550. trans->rtt = trans->srtt = trans->rttvar = 0;
  551. /* Clear the source and route cache */
  552. sctp_transport_route(trans, NULL,
  553. sctp_sk(asoc->base.sk));
  554. }
  555. }
  556. retval = sctp_send_asconf(asoc, chunk);
  557. }
  558. out:
  559. return retval;
  560. }
  561. /* Remove a list of addresses from bind addresses list. Do not remove the
  562. * last address.
  563. *
  564. * Basically run through each address specified in the addrs/addrcnt
  565. * array/length pair, determine if it is IPv6 or IPv4 and call
  566. * sctp_del_bind() on it.
  567. *
  568. * If any of them fails, then the operation will be reversed and the
  569. * ones that were removed will be added back.
  570. *
  571. * At least one address has to be left; if only one address is
  572. * available, the operation will return -EBUSY.
  573. *
  574. * Only sctp_setsockopt_bindx() is supposed to call this function.
  575. */
  576. static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  577. {
  578. struct sctp_sock *sp = sctp_sk(sk);
  579. struct sctp_endpoint *ep = sp->ep;
  580. int cnt;
  581. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  582. int retval = 0;
  583. void *addr_buf;
  584. union sctp_addr *sa_addr;
  585. struct sctp_af *af;
  586. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  587. __func__, sk, addrs, addrcnt);
  588. addr_buf = addrs;
  589. for (cnt = 0; cnt < addrcnt; cnt++) {
  590. /* If the bind address list is empty or if there is only one
  591. * bind address, there is nothing more to be removed (we need
  592. * at least one address here).
  593. */
  594. if (list_empty(&bp->address_list) ||
  595. (sctp_list_single_entry(&bp->address_list))) {
  596. retval = -EBUSY;
  597. goto err_bindx_rem;
  598. }
  599. sa_addr = addr_buf;
  600. af = sctp_get_af_specific(sa_addr->sa.sa_family);
  601. if (!af) {
  602. retval = -EINVAL;
  603. goto err_bindx_rem;
  604. }
  605. if (!af->addr_valid(sa_addr, sp, NULL)) {
  606. retval = -EADDRNOTAVAIL;
  607. goto err_bindx_rem;
  608. }
  609. if (sa_addr->v4.sin_port &&
  610. sa_addr->v4.sin_port != htons(bp->port)) {
  611. retval = -EINVAL;
  612. goto err_bindx_rem;
  613. }
  614. if (!sa_addr->v4.sin_port)
  615. sa_addr->v4.sin_port = htons(bp->port);
  616. /* FIXME - There is probably a need to check if sk->sk_saddr and
  617. * sk->sk_rcv_addr are currently set to one of the addresses to
  618. * be removed. This is something which needs to be looked into
  619. * when we are fixing the outstanding issues with multi-homing
  620. * socket routing and failover schemes. Refer to comments in
  621. * sctp_do_bind(). -daisy
  622. */
  623. retval = sctp_del_bind_addr(bp, sa_addr);
  624. addr_buf += af->sockaddr_len;
  625. err_bindx_rem:
  626. if (retval < 0) {
  627. /* Failed. Add the ones that has been removed back */
  628. if (cnt > 0)
  629. sctp_bindx_add(sk, addrs, cnt);
  630. return retval;
  631. }
  632. }
  633. return retval;
  634. }
  635. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  636. * the associations that are part of the endpoint indicating that a list of
  637. * local addresses are removed from the endpoint.
  638. *
  639. * If any of the addresses is already in the bind address list of the
  640. * association, we do not send the chunk for that association. But it will not
  641. * affect other associations.
  642. *
  643. * Only sctp_setsockopt_bindx() is supposed to call this function.
  644. */
  645. static int sctp_send_asconf_del_ip(struct sock *sk,
  646. struct sockaddr *addrs,
  647. int addrcnt)
  648. {
  649. struct sctp_sock *sp;
  650. struct sctp_endpoint *ep;
  651. struct sctp_association *asoc;
  652. struct sctp_transport *transport;
  653. struct sctp_bind_addr *bp;
  654. struct sctp_chunk *chunk;
  655. union sctp_addr *laddr;
  656. void *addr_buf;
  657. struct sctp_af *af;
  658. struct sctp_sockaddr_entry *saddr;
  659. int i;
  660. int retval = 0;
  661. int stored = 0;
  662. chunk = NULL;
  663. sp = sctp_sk(sk);
  664. ep = sp->ep;
  665. if (!ep->asconf_enable)
  666. return retval;
  667. pr_debug("%s: sk:%p, addrs:%p, addrcnt:%d\n",
  668. __func__, sk, addrs, addrcnt);
  669. list_for_each_entry(asoc, &ep->asocs, asocs) {
  670. if (!asoc->peer.asconf_capable)
  671. continue;
  672. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  673. continue;
  674. if (!sctp_state(asoc, ESTABLISHED))
  675. continue;
  676. /* Check if any address in the packed array of addresses is
  677. * not present in the bind address list of the association.
  678. * If so, do not send the asconf chunk to its peer, but
  679. * continue with other associations.
  680. */
  681. addr_buf = addrs;
  682. for (i = 0; i < addrcnt; i++) {
  683. laddr = addr_buf;
  684. af = sctp_get_af_specific(laddr->v4.sin_family);
  685. if (!af) {
  686. retval = -EINVAL;
  687. goto out;
  688. }
  689. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  690. break;
  691. addr_buf += af->sockaddr_len;
  692. }
  693. if (i < addrcnt)
  694. continue;
  695. /* Find one address in the association's bind address list
  696. * that is not in the packed array of addresses. This is to
  697. * make sure that we do not delete all the addresses in the
  698. * association.
  699. */
  700. bp = &asoc->base.bind_addr;
  701. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  702. addrcnt, sp);
  703. if ((laddr == NULL) && (addrcnt == 1)) {
  704. if (asoc->asconf_addr_del_pending)
  705. continue;
  706. asoc->asconf_addr_del_pending =
  707. kzalloc(sizeof(union sctp_addr), GFP_ATOMIC);
  708. if (asoc->asconf_addr_del_pending == NULL) {
  709. retval = -ENOMEM;
  710. goto out;
  711. }
  712. asoc->asconf_addr_del_pending->sa.sa_family =
  713. addrs->sa_family;
  714. asoc->asconf_addr_del_pending->v4.sin_port =
  715. htons(bp->port);
  716. if (addrs->sa_family == AF_INET) {
  717. struct sockaddr_in *sin;
  718. sin = (struct sockaddr_in *)addrs;
  719. asoc->asconf_addr_del_pending->v4.sin_addr.s_addr = sin->sin_addr.s_addr;
  720. } else if (addrs->sa_family == AF_INET6) {
  721. struct sockaddr_in6 *sin6;
  722. sin6 = (struct sockaddr_in6 *)addrs;
  723. asoc->asconf_addr_del_pending->v6.sin6_addr = sin6->sin6_addr;
  724. }
  725. pr_debug("%s: keep the last address asoc:%p %pISc at %p\n",
  726. __func__, asoc, &asoc->asconf_addr_del_pending->sa,
  727. asoc->asconf_addr_del_pending);
  728. asoc->src_out_of_asoc_ok = 1;
  729. stored = 1;
  730. goto skip_mkasconf;
  731. }
  732. if (laddr == NULL)
  733. return -EINVAL;
  734. /* We do not need RCU protection throughout this loop
  735. * because this is done under a socket lock from the
  736. * setsockopt call.
  737. */
  738. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  739. SCTP_PARAM_DEL_IP);
  740. if (!chunk) {
  741. retval = -ENOMEM;
  742. goto out;
  743. }
  744. skip_mkasconf:
  745. /* Reset use_as_src flag for the addresses in the bind address
  746. * list that are to be deleted.
  747. */
  748. addr_buf = addrs;
  749. for (i = 0; i < addrcnt; i++) {
  750. laddr = addr_buf;
  751. af = sctp_get_af_specific(laddr->v4.sin_family);
  752. list_for_each_entry(saddr, &bp->address_list, list) {
  753. if (sctp_cmp_addr_exact(&saddr->a, laddr))
  754. saddr->state = SCTP_ADDR_DEL;
  755. }
  756. addr_buf += af->sockaddr_len;
  757. }
  758. /* Update the route and saddr entries for all the transports
  759. * as some of the addresses in the bind address list are
  760. * about to be deleted and cannot be used as source addresses.
  761. */
  762. list_for_each_entry(transport, &asoc->peer.transport_addr_list,
  763. transports) {
  764. sctp_transport_route(transport, NULL,
  765. sctp_sk(asoc->base.sk));
  766. }
  767. if (stored)
  768. /* We don't need to transmit ASCONF */
  769. continue;
  770. retval = sctp_send_asconf(asoc, chunk);
  771. }
  772. out:
  773. return retval;
  774. }
  775. /* set addr events to assocs in the endpoint. ep and addr_wq must be locked */
  776. int sctp_asconf_mgmt(struct sctp_sock *sp, struct sctp_sockaddr_entry *addrw)
  777. {
  778. struct sock *sk = sctp_opt2sk(sp);
  779. union sctp_addr *addr;
  780. struct sctp_af *af;
  781. /* It is safe to write port space in caller. */
  782. addr = &addrw->a;
  783. addr->v4.sin_port = htons(sp->ep->base.bind_addr.port);
  784. af = sctp_get_af_specific(addr->sa.sa_family);
  785. if (!af)
  786. return -EINVAL;
  787. if (sctp_verify_addr(sk, addr, af->sockaddr_len))
  788. return -EINVAL;
  789. if (addrw->state == SCTP_ADDR_NEW)
  790. return sctp_send_asconf_add_ip(sk, (struct sockaddr *)addr, 1);
  791. else
  792. return sctp_send_asconf_del_ip(sk, (struct sockaddr *)addr, 1);
  793. }
  794. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  795. *
  796. * API 8.1
  797. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  798. * int flags);
  799. *
  800. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  801. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  802. * or IPv6 addresses.
  803. *
  804. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  805. * Section 3.1.2 for this usage.
  806. *
  807. * addrs is a pointer to an array of one or more socket addresses. Each
  808. * address is contained in its appropriate structure (i.e. struct
  809. * sockaddr_in or struct sockaddr_in6) the family of the address type
  810. * must be used to distinguish the address length (note that this
  811. * representation is termed a "packed array" of addresses). The caller
  812. * specifies the number of addresses in the array with addrcnt.
  813. *
  814. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  815. * -1, and sets errno to the appropriate error code.
  816. *
  817. * For SCTP, the port given in each socket address must be the same, or
  818. * sctp_bindx() will fail, setting errno to EINVAL.
  819. *
  820. * The flags parameter is formed from the bitwise OR of zero or more of
  821. * the following currently defined flags:
  822. *
  823. * SCTP_BINDX_ADD_ADDR
  824. *
  825. * SCTP_BINDX_REM_ADDR
  826. *
  827. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  828. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  829. * addresses from the association. The two flags are mutually exclusive;
  830. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  831. * not remove all addresses from an association; sctp_bindx() will
  832. * reject such an attempt with EINVAL.
  833. *
  834. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  835. * additional addresses with an endpoint after calling bind(). Or use
  836. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  837. * socket is associated with so that no new association accepted will be
  838. * associated with those addresses. If the endpoint supports dynamic
  839. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  840. * endpoint to send the appropriate message to the peer to change the
  841. * peers address lists.
  842. *
  843. * Adding and removing addresses from a connected association is
  844. * optional functionality. Implementations that do not support this
  845. * functionality should return EOPNOTSUPP.
  846. *
  847. * Basically do nothing but copying the addresses from user to kernel
  848. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  849. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
  850. * from userspace.
  851. *
  852. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  853. * it.
  854. *
  855. * sk The sk of the socket
  856. * addrs The pointer to the addresses
  857. * addrssize Size of the addrs buffer
  858. * op Operation to perform (add or remove, see the flags of
  859. * sctp_bindx)
  860. *
  861. * Returns 0 if ok, <0 errno code on error.
  862. */
  863. static int sctp_setsockopt_bindx(struct sock *sk, struct sockaddr *addrs,
  864. int addrs_size, int op)
  865. {
  866. int err;
  867. int addrcnt = 0;
  868. int walk_size = 0;
  869. struct sockaddr *sa_addr;
  870. void *addr_buf = addrs;
  871. struct sctp_af *af;
  872. pr_debug("%s: sk:%p addrs:%p addrs_size:%d opt:%d\n",
  873. __func__, sk, addr_buf, addrs_size, op);
  874. if (unlikely(addrs_size <= 0))
  875. return -EINVAL;
  876. /* Walk through the addrs buffer and count the number of addresses. */
  877. while (walk_size < addrs_size) {
  878. if (walk_size + sizeof(sa_family_t) > addrs_size)
  879. return -EINVAL;
  880. sa_addr = addr_buf;
  881. af = sctp_get_af_specific(sa_addr->sa_family);
  882. /* If the address family is not supported or if this address
  883. * causes the address buffer to overflow return EINVAL.
  884. */
  885. if (!af || (walk_size + af->sockaddr_len) > addrs_size)
  886. return -EINVAL;
  887. addrcnt++;
  888. addr_buf += af->sockaddr_len;
  889. walk_size += af->sockaddr_len;
  890. }
  891. /* Do the work. */
  892. switch (op) {
  893. case SCTP_BINDX_ADD_ADDR:
  894. /* Allow security module to validate bindx addresses. */
  895. err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_BINDX_ADD,
  896. addrs, addrs_size);
  897. if (err)
  898. return err;
  899. err = sctp_bindx_add(sk, addrs, addrcnt);
  900. if (err)
  901. return err;
  902. return sctp_send_asconf_add_ip(sk, addrs, addrcnt);
  903. case SCTP_BINDX_REM_ADDR:
  904. err = sctp_bindx_rem(sk, addrs, addrcnt);
  905. if (err)
  906. return err;
  907. return sctp_send_asconf_del_ip(sk, addrs, addrcnt);
  908. default:
  909. return -EINVAL;
  910. }
  911. }
  912. static int sctp_bind_add(struct sock *sk, struct sockaddr *addrs,
  913. int addrlen)
  914. {
  915. int err;
  916. lock_sock(sk);
  917. err = sctp_setsockopt_bindx(sk, addrs, addrlen, SCTP_BINDX_ADD_ADDR);
  918. release_sock(sk);
  919. return err;
  920. }
  921. static int sctp_connect_new_asoc(struct sctp_endpoint *ep,
  922. const union sctp_addr *daddr,
  923. const struct sctp_initmsg *init,
  924. struct sctp_transport **tp)
  925. {
  926. struct sctp_association *asoc;
  927. struct sock *sk = ep->base.sk;
  928. struct net *net = sock_net(sk);
  929. enum sctp_scope scope;
  930. int err;
  931. if (sctp_endpoint_is_peeled_off(ep, daddr))
  932. return -EADDRNOTAVAIL;
  933. if (!ep->base.bind_addr.port) {
  934. if (sctp_autobind(sk))
  935. return -EAGAIN;
  936. } else {
  937. if (inet_port_requires_bind_service(net, ep->base.bind_addr.port) &&
  938. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  939. return -EACCES;
  940. }
  941. scope = sctp_scope(daddr);
  942. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  943. if (!asoc)
  944. return -ENOMEM;
  945. err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL);
  946. if (err < 0)
  947. goto free;
  948. *tp = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
  949. if (!*tp) {
  950. err = -ENOMEM;
  951. goto free;
  952. }
  953. if (!init)
  954. return 0;
  955. if (init->sinit_num_ostreams) {
  956. __u16 outcnt = init->sinit_num_ostreams;
  957. asoc->c.sinit_num_ostreams = outcnt;
  958. /* outcnt has been changed, need to re-init stream */
  959. err = sctp_stream_init(&asoc->stream, outcnt, 0, GFP_KERNEL);
  960. if (err)
  961. goto free;
  962. }
  963. if (init->sinit_max_instreams)
  964. asoc->c.sinit_max_instreams = init->sinit_max_instreams;
  965. if (init->sinit_max_attempts)
  966. asoc->max_init_attempts = init->sinit_max_attempts;
  967. if (init->sinit_max_init_timeo)
  968. asoc->max_init_timeo =
  969. msecs_to_jiffies(init->sinit_max_init_timeo);
  970. return 0;
  971. free:
  972. sctp_association_free(asoc);
  973. return err;
  974. }
  975. static int sctp_connect_add_peer(struct sctp_association *asoc,
  976. union sctp_addr *daddr, int addr_len)
  977. {
  978. struct sctp_endpoint *ep = asoc->ep;
  979. struct sctp_association *old;
  980. struct sctp_transport *t;
  981. int err;
  982. err = sctp_verify_addr(ep->base.sk, daddr, addr_len);
  983. if (err)
  984. return err;
  985. old = sctp_endpoint_lookup_assoc(ep, daddr, &t);
  986. if (old && old != asoc)
  987. return old->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
  988. : -EALREADY;
  989. if (sctp_endpoint_is_peeled_off(ep, daddr))
  990. return -EADDRNOTAVAIL;
  991. t = sctp_assoc_add_peer(asoc, daddr, GFP_KERNEL, SCTP_UNKNOWN);
  992. if (!t)
  993. return -ENOMEM;
  994. return 0;
  995. }
  996. /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
  997. *
  998. * Common routine for handling connect() and sctp_connectx().
  999. * Connect will come in with just a single address.
  1000. */
  1001. static int __sctp_connect(struct sock *sk, struct sockaddr *kaddrs,
  1002. int addrs_size, int flags, sctp_assoc_t *assoc_id)
  1003. {
  1004. struct sctp_sock *sp = sctp_sk(sk);
  1005. struct sctp_endpoint *ep = sp->ep;
  1006. struct sctp_transport *transport;
  1007. struct sctp_association *asoc;
  1008. void *addr_buf = kaddrs;
  1009. union sctp_addr *daddr;
  1010. struct sctp_af *af;
  1011. int walk_size, err;
  1012. long timeo;
  1013. if (sctp_sstate(sk, ESTABLISHED) || sctp_sstate(sk, CLOSING) ||
  1014. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)))
  1015. return -EISCONN;
  1016. daddr = addr_buf;
  1017. af = sctp_get_af_specific(daddr->sa.sa_family);
  1018. if (!af || af->sockaddr_len > addrs_size)
  1019. return -EINVAL;
  1020. err = sctp_verify_addr(sk, daddr, af->sockaddr_len);
  1021. if (err)
  1022. return err;
  1023. asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
  1024. if (asoc)
  1025. return asoc->state >= SCTP_STATE_ESTABLISHED ? -EISCONN
  1026. : -EALREADY;
  1027. err = sctp_connect_new_asoc(ep, daddr, NULL, &transport);
  1028. if (err)
  1029. return err;
  1030. asoc = transport->asoc;
  1031. addr_buf += af->sockaddr_len;
  1032. walk_size = af->sockaddr_len;
  1033. while (walk_size < addrs_size) {
  1034. err = -EINVAL;
  1035. if (walk_size + sizeof(sa_family_t) > addrs_size)
  1036. goto out_free;
  1037. daddr = addr_buf;
  1038. af = sctp_get_af_specific(daddr->sa.sa_family);
  1039. if (!af || af->sockaddr_len + walk_size > addrs_size)
  1040. goto out_free;
  1041. if (asoc->peer.port != ntohs(daddr->v4.sin_port))
  1042. goto out_free;
  1043. err = sctp_connect_add_peer(asoc, daddr, af->sockaddr_len);
  1044. if (err)
  1045. goto out_free;
  1046. addr_buf += af->sockaddr_len;
  1047. walk_size += af->sockaddr_len;
  1048. }
  1049. /* In case the user of sctp_connectx() wants an association
  1050. * id back, assign one now.
  1051. */
  1052. if (assoc_id) {
  1053. err = sctp_assoc_set_id(asoc, GFP_KERNEL);
  1054. if (err < 0)
  1055. goto out_free;
  1056. }
  1057. err = sctp_primitive_ASSOCIATE(sock_net(sk), asoc, NULL);
  1058. if (err < 0)
  1059. goto out_free;
  1060. /* Initialize sk's dport and daddr for getpeername() */
  1061. inet_sk(sk)->inet_dport = htons(asoc->peer.port);
  1062. sp->pf->to_sk_daddr(daddr, sk);
  1063. sk->sk_err = 0;
  1064. if (assoc_id)
  1065. *assoc_id = asoc->assoc_id;
  1066. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  1067. return sctp_wait_for_connect(asoc, &timeo);
  1068. out_free:
  1069. pr_debug("%s: took out_free path with asoc:%p kaddrs:%p err:%d\n",
  1070. __func__, asoc, kaddrs, err);
  1071. sctp_association_free(asoc);
  1072. return err;
  1073. }
  1074. /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
  1075. *
  1076. * API 8.9
  1077. * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt,
  1078. * sctp_assoc_t *asoc);
  1079. *
  1080. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  1081. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  1082. * or IPv6 addresses.
  1083. *
  1084. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  1085. * Section 3.1.2 for this usage.
  1086. *
  1087. * addrs is a pointer to an array of one or more socket addresses. Each
  1088. * address is contained in its appropriate structure (i.e. struct
  1089. * sockaddr_in or struct sockaddr_in6) the family of the address type
  1090. * must be used to distengish the address length (note that this
  1091. * representation is termed a "packed array" of addresses). The caller
  1092. * specifies the number of addresses in the array with addrcnt.
  1093. *
  1094. * On success, sctp_connectx() returns 0. It also sets the assoc_id to
  1095. * the association id of the new association. On failure, sctp_connectx()
  1096. * returns -1, and sets errno to the appropriate error code. The assoc_id
  1097. * is not touched by the kernel.
  1098. *
  1099. * For SCTP, the port given in each socket address must be the same, or
  1100. * sctp_connectx() will fail, setting errno to EINVAL.
  1101. *
  1102. * An application can use sctp_connectx to initiate an association with
  1103. * an endpoint that is multi-homed. Much like sctp_bindx() this call
  1104. * allows a caller to specify multiple addresses at which a peer can be
  1105. * reached. The way the SCTP stack uses the list of addresses to set up
  1106. * the association is implementation dependent. This function only
  1107. * specifies that the stack will try to make use of all the addresses in
  1108. * the list when needed.
  1109. *
  1110. * Note that the list of addresses passed in is only used for setting up
  1111. * the association. It does not necessarily equal the set of addresses
  1112. * the peer uses for the resulting association. If the caller wants to
  1113. * find out the set of peer addresses, it must use sctp_getpaddrs() to
  1114. * retrieve them after the association has been set up.
  1115. *
  1116. * Basically do nothing but copying the addresses from user to kernel
  1117. * land and invoking either sctp_connectx(). This is used for tunneling
  1118. * the sctp_connectx() request through sctp_setsockopt() from userspace.
  1119. *
  1120. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  1121. * it.
  1122. *
  1123. * sk The sk of the socket
  1124. * addrs The pointer to the addresses
  1125. * addrssize Size of the addrs buffer
  1126. *
  1127. * Returns >=0 if ok, <0 errno code on error.
  1128. */
  1129. static int __sctp_setsockopt_connectx(struct sock *sk, struct sockaddr *kaddrs,
  1130. int addrs_size, sctp_assoc_t *assoc_id)
  1131. {
  1132. int err = 0, flags = 0;
  1133. pr_debug("%s: sk:%p addrs:%p addrs_size:%d\n",
  1134. __func__, sk, kaddrs, addrs_size);
  1135. /* make sure the 1st addr's sa_family is accessible later */
  1136. if (unlikely(addrs_size < sizeof(sa_family_t)))
  1137. return -EINVAL;
  1138. /* Allow security module to validate connectx addresses. */
  1139. err = security_sctp_bind_connect(sk, SCTP_SOCKOPT_CONNECTX,
  1140. (struct sockaddr *)kaddrs,
  1141. addrs_size);
  1142. if (err)
  1143. return err;
  1144. /* in-kernel sockets don't generally have a file allocated to them
  1145. * if all they do is call sock_create_kern().
  1146. */
  1147. if (sk->sk_socket->file)
  1148. flags = sk->sk_socket->file->f_flags;
  1149. return __sctp_connect(sk, kaddrs, addrs_size, flags, assoc_id);
  1150. }
  1151. /*
  1152. * This is an older interface. It's kept for backward compatibility
  1153. * to the option that doesn't provide association id.
  1154. */
  1155. static int sctp_setsockopt_connectx_old(struct sock *sk,
  1156. struct sockaddr *kaddrs,
  1157. int addrs_size)
  1158. {
  1159. return __sctp_setsockopt_connectx(sk, kaddrs, addrs_size, NULL);
  1160. }
  1161. /*
  1162. * New interface for the API. The since the API is done with a socket
  1163. * option, to make it simple we feed back the association id is as a return
  1164. * indication to the call. Error is always negative and association id is
  1165. * always positive.
  1166. */
  1167. static int sctp_setsockopt_connectx(struct sock *sk,
  1168. struct sockaddr *kaddrs,
  1169. int addrs_size)
  1170. {
  1171. sctp_assoc_t assoc_id = 0;
  1172. int err = 0;
  1173. err = __sctp_setsockopt_connectx(sk, kaddrs, addrs_size, &assoc_id);
  1174. if (err)
  1175. return err;
  1176. else
  1177. return assoc_id;
  1178. }
  1179. /*
  1180. * New (hopefully final) interface for the API.
  1181. * We use the sctp_getaddrs_old structure so that use-space library
  1182. * can avoid any unnecessary allocations. The only different part
  1183. * is that we store the actual length of the address buffer into the
  1184. * addrs_num structure member. That way we can re-use the existing
  1185. * code.
  1186. */
  1187. #ifdef CONFIG_COMPAT
  1188. struct compat_sctp_getaddrs_old {
  1189. sctp_assoc_t assoc_id;
  1190. s32 addr_num;
  1191. compat_uptr_t addrs; /* struct sockaddr * */
  1192. };
  1193. #endif
  1194. static int sctp_getsockopt_connectx3(struct sock *sk, int len,
  1195. char __user *optval,
  1196. int __user *optlen)
  1197. {
  1198. struct sctp_getaddrs_old param;
  1199. sctp_assoc_t assoc_id = 0;
  1200. struct sockaddr *kaddrs;
  1201. int err = 0;
  1202. #ifdef CONFIG_COMPAT
  1203. if (in_compat_syscall()) {
  1204. struct compat_sctp_getaddrs_old param32;
  1205. if (len < sizeof(param32))
  1206. return -EINVAL;
  1207. if (copy_from_user(&param32, optval, sizeof(param32)))
  1208. return -EFAULT;
  1209. param.assoc_id = param32.assoc_id;
  1210. param.addr_num = param32.addr_num;
  1211. param.addrs = compat_ptr(param32.addrs);
  1212. } else
  1213. #endif
  1214. {
  1215. if (len < sizeof(param))
  1216. return -EINVAL;
  1217. if (copy_from_user(&param, optval, sizeof(param)))
  1218. return -EFAULT;
  1219. }
  1220. kaddrs = memdup_user(param.addrs, param.addr_num);
  1221. if (IS_ERR(kaddrs))
  1222. return PTR_ERR(kaddrs);
  1223. err = __sctp_setsockopt_connectx(sk, kaddrs, param.addr_num, &assoc_id);
  1224. kfree(kaddrs);
  1225. if (err == 0 || err == -EINPROGRESS) {
  1226. if (copy_to_user(optval, &assoc_id, sizeof(assoc_id)))
  1227. return -EFAULT;
  1228. if (put_user(sizeof(assoc_id), optlen))
  1229. return -EFAULT;
  1230. }
  1231. return err;
  1232. }
  1233. /* API 3.1.4 close() - UDP Style Syntax
  1234. * Applications use close() to perform graceful shutdown (as described in
  1235. * Section 10.1 of [SCTP]) on ALL the associations currently represented
  1236. * by a UDP-style socket.
  1237. *
  1238. * The syntax is
  1239. *
  1240. * ret = close(int sd);
  1241. *
  1242. * sd - the socket descriptor of the associations to be closed.
  1243. *
  1244. * To gracefully shutdown a specific association represented by the
  1245. * UDP-style socket, an application should use the sendmsg() call,
  1246. * passing no user data, but including the appropriate flag in the
  1247. * ancillary data (see Section xxxx).
  1248. *
  1249. * If sd in the close() call is a branched-off socket representing only
  1250. * one association, the shutdown is performed on that association only.
  1251. *
  1252. * 4.1.6 close() - TCP Style Syntax
  1253. *
  1254. * Applications use close() to gracefully close down an association.
  1255. *
  1256. * The syntax is:
  1257. *
  1258. * int close(int sd);
  1259. *
  1260. * sd - the socket descriptor of the association to be closed.
  1261. *
  1262. * After an application calls close() on a socket descriptor, no further
  1263. * socket operations will succeed on that descriptor.
  1264. *
  1265. * API 7.1.4 SO_LINGER
  1266. *
  1267. * An application using the TCP-style socket can use this option to
  1268. * perform the SCTP ABORT primitive. The linger option structure is:
  1269. *
  1270. * struct linger {
  1271. * int l_onoff; // option on/off
  1272. * int l_linger; // linger time
  1273. * };
  1274. *
  1275. * To enable the option, set l_onoff to 1. If the l_linger value is set
  1276. * to 0, calling close() is the same as the ABORT primitive. If the
  1277. * value is set to a negative value, the setsockopt() call will return
  1278. * an error. If the value is set to a positive value linger_time, the
  1279. * close() can be blocked for at most linger_time ms. If the graceful
  1280. * shutdown phase does not finish during this period, close() will
  1281. * return but the graceful shutdown phase continues in the system.
  1282. */
  1283. static void sctp_close(struct sock *sk, long timeout)
  1284. {
  1285. struct net *net = sock_net(sk);
  1286. struct sctp_endpoint *ep;
  1287. struct sctp_association *asoc;
  1288. struct list_head *pos, *temp;
  1289. unsigned int data_was_unread;
  1290. pr_debug("%s: sk:%p, timeout:%ld\n", __func__, sk, timeout);
  1291. lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
  1292. sk->sk_shutdown = SHUTDOWN_MASK;
  1293. inet_sk_set_state(sk, SCTP_SS_CLOSING);
  1294. ep = sctp_sk(sk)->ep;
  1295. /* Clean up any skbs sitting on the receive queue. */
  1296. data_was_unread = sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
  1297. data_was_unread += sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
  1298. /* Walk all associations on an endpoint. */
  1299. list_for_each_safe(pos, temp, &ep->asocs) {
  1300. asoc = list_entry(pos, struct sctp_association, asocs);
  1301. if (sctp_style(sk, TCP)) {
  1302. /* A closed association can still be in the list if
  1303. * it belongs to a TCP-style listening socket that is
  1304. * not yet accepted. If so, free it. If not, send an
  1305. * ABORT or SHUTDOWN based on the linger options.
  1306. */
  1307. if (sctp_state(asoc, CLOSED)) {
  1308. sctp_association_free(asoc);
  1309. continue;
  1310. }
  1311. }
  1312. if (data_was_unread || !skb_queue_empty(&asoc->ulpq.lobby) ||
  1313. !skb_queue_empty(&asoc->ulpq.reasm) ||
  1314. !skb_queue_empty(&asoc->ulpq.reasm_uo) ||
  1315. (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime)) {
  1316. struct sctp_chunk *chunk;
  1317. chunk = sctp_make_abort_user(asoc, NULL, 0);
  1318. sctp_primitive_ABORT(net, asoc, chunk);
  1319. } else
  1320. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  1321. }
  1322. /* On a TCP-style socket, block for at most linger_time if set. */
  1323. if (sctp_style(sk, TCP) && timeout)
  1324. sctp_wait_for_close(sk, timeout);
  1325. /* This will run the backlog queue. */
  1326. release_sock(sk);
  1327. /* Supposedly, no process has access to the socket, but
  1328. * the net layers still may.
  1329. * Also, sctp_destroy_sock() needs to be called with addr_wq_lock
  1330. * held and that should be grabbed before socket lock.
  1331. */
  1332. spin_lock_bh(&net->sctp.addr_wq_lock);
  1333. bh_lock_sock_nested(sk);
  1334. /* Hold the sock, since sk_common_release() will put sock_put()
  1335. * and we have just a little more cleanup.
  1336. */
  1337. sock_hold(sk);
  1338. sk_common_release(sk);
  1339. bh_unlock_sock(sk);
  1340. spin_unlock_bh(&net->sctp.addr_wq_lock);
  1341. sock_put(sk);
  1342. SCTP_DBG_OBJCNT_DEC(sock);
  1343. }
  1344. /* Handle EPIPE error. */
  1345. static int sctp_error(struct sock *sk, int flags, int err)
  1346. {
  1347. if (err == -EPIPE)
  1348. err = sock_error(sk) ? : -EPIPE;
  1349. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  1350. send_sig(SIGPIPE, current, 0);
  1351. return err;
  1352. }
  1353. /* API 3.1.3 sendmsg() - UDP Style Syntax
  1354. *
  1355. * An application uses sendmsg() and recvmsg() calls to transmit data to
  1356. * and receive data from its peer.
  1357. *
  1358. * ssize_t sendmsg(int socket, const struct msghdr *message,
  1359. * int flags);
  1360. *
  1361. * socket - the socket descriptor of the endpoint.
  1362. * message - pointer to the msghdr structure which contains a single
  1363. * user message and possibly some ancillary data.
  1364. *
  1365. * See Section 5 for complete description of the data
  1366. * structures.
  1367. *
  1368. * flags - flags sent or received with the user message, see Section
  1369. * 5 for complete description of the flags.
  1370. *
  1371. * Note: This function could use a rewrite especially when explicit
  1372. * connect support comes in.
  1373. */
  1374. /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
  1375. static int sctp_msghdr_parse(const struct msghdr *msg,
  1376. struct sctp_cmsgs *cmsgs);
  1377. static int sctp_sendmsg_parse(struct sock *sk, struct sctp_cmsgs *cmsgs,
  1378. struct sctp_sndrcvinfo *srinfo,
  1379. const struct msghdr *msg, size_t msg_len)
  1380. {
  1381. __u16 sflags;
  1382. int err;
  1383. if (sctp_sstate(sk, LISTENING) && sctp_style(sk, TCP))
  1384. return -EPIPE;
  1385. if (msg_len > sk->sk_sndbuf)
  1386. return -EMSGSIZE;
  1387. memset(cmsgs, 0, sizeof(*cmsgs));
  1388. err = sctp_msghdr_parse(msg, cmsgs);
  1389. if (err) {
  1390. pr_debug("%s: msghdr parse err:%x\n", __func__, err);
  1391. return err;
  1392. }
  1393. memset(srinfo, 0, sizeof(*srinfo));
  1394. if (cmsgs->srinfo) {
  1395. srinfo->sinfo_stream = cmsgs->srinfo->sinfo_stream;
  1396. srinfo->sinfo_flags = cmsgs->srinfo->sinfo_flags;
  1397. srinfo->sinfo_ppid = cmsgs->srinfo->sinfo_ppid;
  1398. srinfo->sinfo_context = cmsgs->srinfo->sinfo_context;
  1399. srinfo->sinfo_assoc_id = cmsgs->srinfo->sinfo_assoc_id;
  1400. srinfo->sinfo_timetolive = cmsgs->srinfo->sinfo_timetolive;
  1401. }
  1402. if (cmsgs->sinfo) {
  1403. srinfo->sinfo_stream = cmsgs->sinfo->snd_sid;
  1404. srinfo->sinfo_flags = cmsgs->sinfo->snd_flags;
  1405. srinfo->sinfo_ppid = cmsgs->sinfo->snd_ppid;
  1406. srinfo->sinfo_context = cmsgs->sinfo->snd_context;
  1407. srinfo->sinfo_assoc_id = cmsgs->sinfo->snd_assoc_id;
  1408. }
  1409. if (cmsgs->prinfo) {
  1410. srinfo->sinfo_timetolive = cmsgs->prinfo->pr_value;
  1411. SCTP_PR_SET_POLICY(srinfo->sinfo_flags,
  1412. cmsgs->prinfo->pr_policy);
  1413. }
  1414. sflags = srinfo->sinfo_flags;
  1415. if (!sflags && msg_len)
  1416. return 0;
  1417. if (sctp_style(sk, TCP) && (sflags & (SCTP_EOF | SCTP_ABORT)))
  1418. return -EINVAL;
  1419. if (((sflags & SCTP_EOF) && msg_len > 0) ||
  1420. (!(sflags & (SCTP_EOF | SCTP_ABORT)) && msg_len == 0))
  1421. return -EINVAL;
  1422. if ((sflags & SCTP_ADDR_OVER) && !msg->msg_name)
  1423. return -EINVAL;
  1424. return 0;
  1425. }
  1426. static int sctp_sendmsg_new_asoc(struct sock *sk, __u16 sflags,
  1427. struct sctp_cmsgs *cmsgs,
  1428. union sctp_addr *daddr,
  1429. struct sctp_transport **tp)
  1430. {
  1431. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  1432. struct sctp_association *asoc;
  1433. struct cmsghdr *cmsg;
  1434. __be32 flowinfo = 0;
  1435. struct sctp_af *af;
  1436. int err;
  1437. *tp = NULL;
  1438. if (sflags & (SCTP_EOF | SCTP_ABORT))
  1439. return -EINVAL;
  1440. if (sctp_style(sk, TCP) && (sctp_sstate(sk, ESTABLISHED) ||
  1441. sctp_sstate(sk, CLOSING)))
  1442. return -EADDRNOTAVAIL;
  1443. /* Label connection socket for first association 1-to-many
  1444. * style for client sequence socket()->sendmsg(). This
  1445. * needs to be done before sctp_assoc_add_peer() as that will
  1446. * set up the initial packet that needs to account for any
  1447. * security ip options (CIPSO/CALIPSO) added to the packet.
  1448. */
  1449. af = sctp_get_af_specific(daddr->sa.sa_family);
  1450. if (!af)
  1451. return -EINVAL;
  1452. err = security_sctp_bind_connect(sk, SCTP_SENDMSG_CONNECT,
  1453. (struct sockaddr *)daddr,
  1454. af->sockaddr_len);
  1455. if (err < 0)
  1456. return err;
  1457. err = sctp_connect_new_asoc(ep, daddr, cmsgs->init, tp);
  1458. if (err)
  1459. return err;
  1460. asoc = (*tp)->asoc;
  1461. if (!cmsgs->addrs_msg)
  1462. return 0;
  1463. if (daddr->sa.sa_family == AF_INET6)
  1464. flowinfo = daddr->v6.sin6_flowinfo;
  1465. /* sendv addr list parse */
  1466. for_each_cmsghdr(cmsg, cmsgs->addrs_msg) {
  1467. union sctp_addr _daddr;
  1468. int dlen;
  1469. if (cmsg->cmsg_level != IPPROTO_SCTP ||
  1470. (cmsg->cmsg_type != SCTP_DSTADDRV4 &&
  1471. cmsg->cmsg_type != SCTP_DSTADDRV6))
  1472. continue;
  1473. daddr = &_daddr;
  1474. memset(daddr, 0, sizeof(*daddr));
  1475. dlen = cmsg->cmsg_len - sizeof(struct cmsghdr);
  1476. if (cmsg->cmsg_type == SCTP_DSTADDRV4) {
  1477. if (dlen < sizeof(struct in_addr)) {
  1478. err = -EINVAL;
  1479. goto free;
  1480. }
  1481. dlen = sizeof(struct in_addr);
  1482. daddr->v4.sin_family = AF_INET;
  1483. daddr->v4.sin_port = htons(asoc->peer.port);
  1484. memcpy(&daddr->v4.sin_addr, CMSG_DATA(cmsg), dlen);
  1485. } else {
  1486. if (dlen < sizeof(struct in6_addr)) {
  1487. err = -EINVAL;
  1488. goto free;
  1489. }
  1490. dlen = sizeof(struct in6_addr);
  1491. daddr->v6.sin6_flowinfo = flowinfo;
  1492. daddr->v6.sin6_family = AF_INET6;
  1493. daddr->v6.sin6_port = htons(asoc->peer.port);
  1494. memcpy(&daddr->v6.sin6_addr, CMSG_DATA(cmsg), dlen);
  1495. }
  1496. err = sctp_connect_add_peer(asoc, daddr, sizeof(*daddr));
  1497. if (err)
  1498. goto free;
  1499. }
  1500. return 0;
  1501. free:
  1502. sctp_association_free(asoc);
  1503. return err;
  1504. }
  1505. static int sctp_sendmsg_check_sflags(struct sctp_association *asoc,
  1506. __u16 sflags, struct msghdr *msg,
  1507. size_t msg_len)
  1508. {
  1509. struct sock *sk = asoc->base.sk;
  1510. struct net *net = sock_net(sk);
  1511. if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP))
  1512. return -EPIPE;
  1513. if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP) &&
  1514. !sctp_state(asoc, ESTABLISHED))
  1515. return 0;
  1516. if (sflags & SCTP_EOF) {
  1517. pr_debug("%s: shutting down association:%p\n", __func__, asoc);
  1518. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  1519. return 0;
  1520. }
  1521. if (sflags & SCTP_ABORT) {
  1522. struct sctp_chunk *chunk;
  1523. chunk = sctp_make_abort_user(asoc, msg, msg_len);
  1524. if (!chunk)
  1525. return -ENOMEM;
  1526. pr_debug("%s: aborting association:%p\n", __func__, asoc);
  1527. sctp_primitive_ABORT(net, asoc, chunk);
  1528. iov_iter_revert(&msg->msg_iter, msg_len);
  1529. return 0;
  1530. }
  1531. return 1;
  1532. }
  1533. static int sctp_sendmsg_to_asoc(struct sctp_association *asoc,
  1534. struct msghdr *msg, size_t msg_len,
  1535. struct sctp_transport *transport,
  1536. struct sctp_sndrcvinfo *sinfo)
  1537. {
  1538. struct sock *sk = asoc->base.sk;
  1539. struct sctp_sock *sp = sctp_sk(sk);
  1540. struct net *net = sock_net(sk);
  1541. struct sctp_datamsg *datamsg;
  1542. bool wait_connect = false;
  1543. struct sctp_chunk *chunk;
  1544. long timeo;
  1545. int err;
  1546. if (sinfo->sinfo_stream >= asoc->stream.outcnt) {
  1547. err = -EINVAL;
  1548. goto err;
  1549. }
  1550. if (unlikely(!SCTP_SO(&asoc->stream, sinfo->sinfo_stream)->ext)) {
  1551. err = sctp_stream_init_ext(&asoc->stream, sinfo->sinfo_stream);
  1552. if (err)
  1553. goto err;
  1554. }
  1555. if (sp->disable_fragments && msg_len > asoc->frag_point) {
  1556. err = -EMSGSIZE;
  1557. goto err;
  1558. }
  1559. if (asoc->pmtu_pending) {
  1560. if (sp->param_flags & SPP_PMTUD_ENABLE)
  1561. sctp_assoc_sync_pmtu(asoc);
  1562. asoc->pmtu_pending = 0;
  1563. }
  1564. if (sctp_wspace(asoc) < (int)msg_len)
  1565. sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
  1566. if (sctp_wspace(asoc) <= 0 || !sk_wmem_schedule(sk, msg_len)) {
  1567. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1568. err = sctp_wait_for_sndbuf(asoc, transport, &timeo, msg_len);
  1569. if (err)
  1570. goto err;
  1571. if (unlikely(sinfo->sinfo_stream >= asoc->stream.outcnt)) {
  1572. err = -EINVAL;
  1573. goto err;
  1574. }
  1575. }
  1576. if (sctp_state(asoc, CLOSED)) {
  1577. err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
  1578. if (err)
  1579. goto err;
  1580. if (asoc->ep->intl_enable) {
  1581. timeo = sock_sndtimeo(sk, 0);
  1582. err = sctp_wait_for_connect(asoc, &timeo);
  1583. if (err) {
  1584. err = -ESRCH;
  1585. goto err;
  1586. }
  1587. } else {
  1588. wait_connect = true;
  1589. }
  1590. pr_debug("%s: we associated primitively\n", __func__);
  1591. }
  1592. datamsg = sctp_datamsg_from_user(asoc, sinfo, &msg->msg_iter);
  1593. if (IS_ERR(datamsg)) {
  1594. err = PTR_ERR(datamsg);
  1595. goto err;
  1596. }
  1597. asoc->force_delay = !!(msg->msg_flags & MSG_MORE);
  1598. list_for_each_entry(chunk, &datamsg->chunks, frag_list) {
  1599. sctp_chunk_hold(chunk);
  1600. sctp_set_owner_w(chunk);
  1601. chunk->transport = transport;
  1602. }
  1603. err = sctp_primitive_SEND(net, asoc, datamsg);
  1604. if (err) {
  1605. sctp_datamsg_free(datamsg);
  1606. goto err;
  1607. }
  1608. pr_debug("%s: we sent primitively\n", __func__);
  1609. sctp_datamsg_put(datamsg);
  1610. if (unlikely(wait_connect)) {
  1611. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1612. sctp_wait_for_connect(asoc, &timeo);
  1613. }
  1614. err = msg_len;
  1615. err:
  1616. return err;
  1617. }
  1618. static union sctp_addr *sctp_sendmsg_get_daddr(struct sock *sk,
  1619. const struct msghdr *msg,
  1620. struct sctp_cmsgs *cmsgs)
  1621. {
  1622. union sctp_addr *daddr = NULL;
  1623. int err;
  1624. if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
  1625. int len = msg->msg_namelen;
  1626. if (len > sizeof(*daddr))
  1627. len = sizeof(*daddr);
  1628. daddr = (union sctp_addr *)msg->msg_name;
  1629. err = sctp_verify_addr(sk, daddr, len);
  1630. if (err)
  1631. return ERR_PTR(err);
  1632. }
  1633. return daddr;
  1634. }
  1635. static void sctp_sendmsg_update_sinfo(struct sctp_association *asoc,
  1636. struct sctp_sndrcvinfo *sinfo,
  1637. struct sctp_cmsgs *cmsgs)
  1638. {
  1639. if (!cmsgs->srinfo && !cmsgs->sinfo) {
  1640. sinfo->sinfo_stream = asoc->default_stream;
  1641. sinfo->sinfo_ppid = asoc->default_ppid;
  1642. sinfo->sinfo_context = asoc->default_context;
  1643. sinfo->sinfo_assoc_id = sctp_assoc2id(asoc);
  1644. if (!cmsgs->prinfo)
  1645. sinfo->sinfo_flags = asoc->default_flags;
  1646. }
  1647. if (!cmsgs->srinfo && !cmsgs->prinfo)
  1648. sinfo->sinfo_timetolive = asoc->default_timetolive;
  1649. if (cmsgs->authinfo) {
  1650. /* Reuse sinfo_tsn to indicate that authinfo was set and
  1651. * sinfo_ssn to save the keyid on tx path.
  1652. */
  1653. sinfo->sinfo_tsn = 1;
  1654. sinfo->sinfo_ssn = cmsgs->authinfo->auth_keynumber;
  1655. }
  1656. }
  1657. static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
  1658. {
  1659. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  1660. struct sctp_transport *transport = NULL;
  1661. struct sctp_sndrcvinfo _sinfo, *sinfo;
  1662. struct sctp_association *asoc, *tmp;
  1663. struct sctp_cmsgs cmsgs;
  1664. union sctp_addr *daddr;
  1665. bool new = false;
  1666. __u16 sflags;
  1667. int err;
  1668. /* Parse and get snd_info */
  1669. err = sctp_sendmsg_parse(sk, &cmsgs, &_sinfo, msg, msg_len);
  1670. if (err)
  1671. goto out;
  1672. sinfo = &_sinfo;
  1673. sflags = sinfo->sinfo_flags;
  1674. /* Get daddr from msg */
  1675. daddr = sctp_sendmsg_get_daddr(sk, msg, &cmsgs);
  1676. if (IS_ERR(daddr)) {
  1677. err = PTR_ERR(daddr);
  1678. goto out;
  1679. }
  1680. lock_sock(sk);
  1681. /* SCTP_SENDALL process */
  1682. if ((sflags & SCTP_SENDALL) && sctp_style(sk, UDP)) {
  1683. list_for_each_entry_safe(asoc, tmp, &ep->asocs, asocs) {
  1684. err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
  1685. msg_len);
  1686. if (err == 0)
  1687. continue;
  1688. if (err < 0)
  1689. goto out_unlock;
  1690. sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
  1691. err = sctp_sendmsg_to_asoc(asoc, msg, msg_len,
  1692. NULL, sinfo);
  1693. if (err < 0)
  1694. goto out_unlock;
  1695. iov_iter_revert(&msg->msg_iter, err);
  1696. }
  1697. goto out_unlock;
  1698. }
  1699. /* Get and check or create asoc */
  1700. if (daddr) {
  1701. asoc = sctp_endpoint_lookup_assoc(ep, daddr, &transport);
  1702. if (asoc) {
  1703. err = sctp_sendmsg_check_sflags(asoc, sflags, msg,
  1704. msg_len);
  1705. if (err <= 0)
  1706. goto out_unlock;
  1707. } else {
  1708. err = sctp_sendmsg_new_asoc(sk, sflags, &cmsgs, daddr,
  1709. &transport);
  1710. if (err)
  1711. goto out_unlock;
  1712. asoc = transport->asoc;
  1713. new = true;
  1714. }
  1715. if (!sctp_style(sk, TCP) && !(sflags & SCTP_ADDR_OVER))
  1716. transport = NULL;
  1717. } else {
  1718. asoc = sctp_id2assoc(sk, sinfo->sinfo_assoc_id);
  1719. if (!asoc) {
  1720. err = -EPIPE;
  1721. goto out_unlock;
  1722. }
  1723. err = sctp_sendmsg_check_sflags(asoc, sflags, msg, msg_len);
  1724. if (err <= 0)
  1725. goto out_unlock;
  1726. }
  1727. /* Update snd_info with the asoc */
  1728. sctp_sendmsg_update_sinfo(asoc, sinfo, &cmsgs);
  1729. /* Send msg to the asoc */
  1730. err = sctp_sendmsg_to_asoc(asoc, msg, msg_len, transport, sinfo);
  1731. if (err < 0 && err != -ESRCH && new)
  1732. sctp_association_free(asoc);
  1733. out_unlock:
  1734. release_sock(sk);
  1735. out:
  1736. return sctp_error(sk, msg->msg_flags, err);
  1737. }
  1738. /* This is an extended version of skb_pull() that removes the data from the
  1739. * start of a skb even when data is spread across the list of skb's in the
  1740. * frag_list. len specifies the total amount of data that needs to be removed.
  1741. * when 'len' bytes could be removed from the skb, it returns 0.
  1742. * If 'len' exceeds the total skb length, it returns the no. of bytes that
  1743. * could not be removed.
  1744. */
  1745. static int sctp_skb_pull(struct sk_buff *skb, int len)
  1746. {
  1747. struct sk_buff *list;
  1748. int skb_len = skb_headlen(skb);
  1749. int rlen;
  1750. if (len <= skb_len) {
  1751. __skb_pull(skb, len);
  1752. return 0;
  1753. }
  1754. len -= skb_len;
  1755. __skb_pull(skb, skb_len);
  1756. skb_walk_frags(skb, list) {
  1757. rlen = sctp_skb_pull(list, len);
  1758. skb->len -= (len-rlen);
  1759. skb->data_len -= (len-rlen);
  1760. if (!rlen)
  1761. return 0;
  1762. len = rlen;
  1763. }
  1764. return len;
  1765. }
  1766. /* API 3.1.3 recvmsg() - UDP Style Syntax
  1767. *
  1768. * ssize_t recvmsg(int socket, struct msghdr *message,
  1769. * int flags);
  1770. *
  1771. * socket - the socket descriptor of the endpoint.
  1772. * message - pointer to the msghdr structure which contains a single
  1773. * user message and possibly some ancillary data.
  1774. *
  1775. * See Section 5 for complete description of the data
  1776. * structures.
  1777. *
  1778. * flags - flags sent or received with the user message, see Section
  1779. * 5 for complete description of the flags.
  1780. */
  1781. static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  1782. int flags, int *addr_len)
  1783. {
  1784. struct sctp_ulpevent *event = NULL;
  1785. struct sctp_sock *sp = sctp_sk(sk);
  1786. struct sk_buff *skb, *head_skb;
  1787. int copied;
  1788. int err = 0;
  1789. int skb_len;
  1790. pr_debug("%s: sk:%p, msghdr:%p, len:%zd, flags:0x%x, addr_len:%p)\n",
  1791. __func__, sk, msg, len, flags, addr_len);
  1792. if (unlikely(flags & MSG_ERRQUEUE))
  1793. return inet_recv_error(sk, msg, len, addr_len);
  1794. if (sk_can_busy_loop(sk) &&
  1795. skb_queue_empty_lockless(&sk->sk_receive_queue))
  1796. sk_busy_loop(sk, flags & MSG_DONTWAIT);
  1797. lock_sock(sk);
  1798. if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED) &&
  1799. !sctp_sstate(sk, CLOSING) && !sctp_sstate(sk, CLOSED)) {
  1800. err = -ENOTCONN;
  1801. goto out;
  1802. }
  1803. skb = sctp_skb_recv_datagram(sk, flags, &err);
  1804. if (!skb)
  1805. goto out;
  1806. /* Get the total length of the skb including any skb's in the
  1807. * frag_list.
  1808. */
  1809. skb_len = skb->len;
  1810. copied = skb_len;
  1811. if (copied > len)
  1812. copied = len;
  1813. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  1814. event = sctp_skb2event(skb);
  1815. if (err)
  1816. goto out_free;
  1817. if (event->chunk && event->chunk->head_skb)
  1818. head_skb = event->chunk->head_skb;
  1819. else
  1820. head_skb = skb;
  1821. sock_recv_cmsgs(msg, sk, head_skb);
  1822. if (sctp_ulpevent_is_notification(event)) {
  1823. msg->msg_flags |= MSG_NOTIFICATION;
  1824. sp->pf->event_msgname(event, msg->msg_name, addr_len);
  1825. } else {
  1826. sp->pf->skb_msgname(head_skb, msg->msg_name, addr_len);
  1827. }
  1828. /* Check if we allow SCTP_NXTINFO. */
  1829. if (sp->recvnxtinfo)
  1830. sctp_ulpevent_read_nxtinfo(event, msg, sk);
  1831. /* Check if we allow SCTP_RCVINFO. */
  1832. if (sp->recvrcvinfo)
  1833. sctp_ulpevent_read_rcvinfo(event, msg);
  1834. /* Check if we allow SCTP_SNDRCVINFO. */
  1835. if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_DATA_IO_EVENT))
  1836. sctp_ulpevent_read_sndrcvinfo(event, msg);
  1837. err = copied;
  1838. /* If skb's length exceeds the user's buffer, update the skb and
  1839. * push it back to the receive_queue so that the next call to
  1840. * recvmsg() will return the remaining data. Don't set MSG_EOR.
  1841. */
  1842. if (skb_len > copied) {
  1843. msg->msg_flags &= ~MSG_EOR;
  1844. if (flags & MSG_PEEK)
  1845. goto out_free;
  1846. sctp_skb_pull(skb, copied);
  1847. skb_queue_head(&sk->sk_receive_queue, skb);
  1848. /* When only partial message is copied to the user, increase
  1849. * rwnd by that amount. If all the data in the skb is read,
  1850. * rwnd is updated when the event is freed.
  1851. */
  1852. if (!sctp_ulpevent_is_notification(event))
  1853. sctp_assoc_rwnd_increase(event->asoc, copied);
  1854. goto out;
  1855. } else if ((event->msg_flags & MSG_NOTIFICATION) ||
  1856. (event->msg_flags & MSG_EOR))
  1857. msg->msg_flags |= MSG_EOR;
  1858. else
  1859. msg->msg_flags &= ~MSG_EOR;
  1860. out_free:
  1861. if (flags & MSG_PEEK) {
  1862. /* Release the skb reference acquired after peeking the skb in
  1863. * sctp_skb_recv_datagram().
  1864. */
  1865. kfree_skb(skb);
  1866. } else {
  1867. /* Free the event which includes releasing the reference to
  1868. * the owner of the skb, freeing the skb and updating the
  1869. * rwnd.
  1870. */
  1871. sctp_ulpevent_free(event);
  1872. }
  1873. out:
  1874. release_sock(sk);
  1875. return err;
  1876. }
  1877. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  1878. *
  1879. * This option is a on/off flag. If enabled no SCTP message
  1880. * fragmentation will be performed. Instead if a message being sent
  1881. * exceeds the current PMTU size, the message will NOT be sent and
  1882. * instead a error will be indicated to the user.
  1883. */
  1884. static int sctp_setsockopt_disable_fragments(struct sock *sk, int *val,
  1885. unsigned int optlen)
  1886. {
  1887. if (optlen < sizeof(int))
  1888. return -EINVAL;
  1889. sctp_sk(sk)->disable_fragments = (*val == 0) ? 0 : 1;
  1890. return 0;
  1891. }
  1892. static int sctp_setsockopt_events(struct sock *sk, __u8 *sn_type,
  1893. unsigned int optlen)
  1894. {
  1895. struct sctp_sock *sp = sctp_sk(sk);
  1896. struct sctp_association *asoc;
  1897. int i;
  1898. if (optlen > sizeof(struct sctp_event_subscribe))
  1899. return -EINVAL;
  1900. for (i = 0; i < optlen; i++)
  1901. sctp_ulpevent_type_set(&sp->subscribe, SCTP_SN_TYPE_BASE + i,
  1902. sn_type[i]);
  1903. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  1904. asoc->subscribe = sctp_sk(sk)->subscribe;
  1905. /* At the time when a user app subscribes to SCTP_SENDER_DRY_EVENT,
  1906. * if there is no data to be sent or retransmit, the stack will
  1907. * immediately send up this notification.
  1908. */
  1909. if (sctp_ulpevent_type_enabled(sp->subscribe, SCTP_SENDER_DRY_EVENT)) {
  1910. struct sctp_ulpevent *event;
  1911. asoc = sctp_id2assoc(sk, 0);
  1912. if (asoc && sctp_outq_is_empty(&asoc->outqueue)) {
  1913. event = sctp_ulpevent_make_sender_dry_event(asoc,
  1914. GFP_USER | __GFP_NOWARN);
  1915. if (!event)
  1916. return -ENOMEM;
  1917. asoc->stream.si->enqueue_event(&asoc->ulpq, event);
  1918. }
  1919. }
  1920. return 0;
  1921. }
  1922. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  1923. *
  1924. * This socket option is applicable to the UDP-style socket only. When
  1925. * set it will cause associations that are idle for more than the
  1926. * specified number of seconds to automatically close. An association
  1927. * being idle is defined an association that has NOT sent or received
  1928. * user data. The special value of '0' indicates that no automatic
  1929. * close of any associations should be performed. The option expects an
  1930. * integer defining the number of seconds of idle time before an
  1931. * association is closed.
  1932. */
  1933. static int sctp_setsockopt_autoclose(struct sock *sk, u32 *optval,
  1934. unsigned int optlen)
  1935. {
  1936. struct sctp_sock *sp = sctp_sk(sk);
  1937. struct net *net = sock_net(sk);
  1938. /* Applicable to UDP-style socket only */
  1939. if (sctp_style(sk, TCP))
  1940. return -EOPNOTSUPP;
  1941. if (optlen != sizeof(int))
  1942. return -EINVAL;
  1943. sp->autoclose = *optval;
  1944. if (sp->autoclose > net->sctp.max_autoclose)
  1945. sp->autoclose = net->sctp.max_autoclose;
  1946. return 0;
  1947. }
  1948. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  1949. *
  1950. * Applications can enable or disable heartbeats for any peer address of
  1951. * an association, modify an address's heartbeat interval, force a
  1952. * heartbeat to be sent immediately, and adjust the address's maximum
  1953. * number of retransmissions sent before an address is considered
  1954. * unreachable. The following structure is used to access and modify an
  1955. * address's parameters:
  1956. *
  1957. * struct sctp_paddrparams {
  1958. * sctp_assoc_t spp_assoc_id;
  1959. * struct sockaddr_storage spp_address;
  1960. * uint32_t spp_hbinterval;
  1961. * uint16_t spp_pathmaxrxt;
  1962. * uint32_t spp_pathmtu;
  1963. * uint32_t spp_sackdelay;
  1964. * uint32_t spp_flags;
  1965. * uint32_t spp_ipv6_flowlabel;
  1966. * uint8_t spp_dscp;
  1967. * };
  1968. *
  1969. * spp_assoc_id - (one-to-many style socket) This is filled in the
  1970. * application, and identifies the association for
  1971. * this query.
  1972. * spp_address - This specifies which address is of interest.
  1973. * spp_hbinterval - This contains the value of the heartbeat interval,
  1974. * in milliseconds. If a value of zero
  1975. * is present in this field then no changes are to
  1976. * be made to this parameter.
  1977. * spp_pathmaxrxt - This contains the maximum number of
  1978. * retransmissions before this address shall be
  1979. * considered unreachable. If a value of zero
  1980. * is present in this field then no changes are to
  1981. * be made to this parameter.
  1982. * spp_pathmtu - When Path MTU discovery is disabled the value
  1983. * specified here will be the "fixed" path mtu.
  1984. * Note that if the spp_address field is empty
  1985. * then all associations on this address will
  1986. * have this fixed path mtu set upon them.
  1987. *
  1988. * spp_sackdelay - When delayed sack is enabled, this value specifies
  1989. * the number of milliseconds that sacks will be delayed
  1990. * for. This value will apply to all addresses of an
  1991. * association if the spp_address field is empty. Note
  1992. * also, that if delayed sack is enabled and this
  1993. * value is set to 0, no change is made to the last
  1994. * recorded delayed sack timer value.
  1995. *
  1996. * spp_flags - These flags are used to control various features
  1997. * on an association. The flag field may contain
  1998. * zero or more of the following options.
  1999. *
  2000. * SPP_HB_ENABLE - Enable heartbeats on the
  2001. * specified address. Note that if the address
  2002. * field is empty all addresses for the association
  2003. * have heartbeats enabled upon them.
  2004. *
  2005. * SPP_HB_DISABLE - Disable heartbeats on the
  2006. * speicifed address. Note that if the address
  2007. * field is empty all addresses for the association
  2008. * will have their heartbeats disabled. Note also
  2009. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  2010. * mutually exclusive, only one of these two should
  2011. * be specified. Enabling both fields will have
  2012. * undetermined results.
  2013. *
  2014. * SPP_HB_DEMAND - Request a user initiated heartbeat
  2015. * to be made immediately.
  2016. *
  2017. * SPP_HB_TIME_IS_ZERO - Specify's that the time for
  2018. * heartbeat delayis to be set to the value of 0
  2019. * milliseconds.
  2020. *
  2021. * SPP_PMTUD_ENABLE - This field will enable PMTU
  2022. * discovery upon the specified address. Note that
  2023. * if the address feild is empty then all addresses
  2024. * on the association are effected.
  2025. *
  2026. * SPP_PMTUD_DISABLE - This field will disable PMTU
  2027. * discovery upon the specified address. Note that
  2028. * if the address feild is empty then all addresses
  2029. * on the association are effected. Not also that
  2030. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  2031. * exclusive. Enabling both will have undetermined
  2032. * results.
  2033. *
  2034. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  2035. * on delayed sack. The time specified in spp_sackdelay
  2036. * is used to specify the sack delay for this address. Note
  2037. * that if spp_address is empty then all addresses will
  2038. * enable delayed sack and take on the sack delay
  2039. * value specified in spp_sackdelay.
  2040. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  2041. * off delayed sack. If the spp_address field is blank then
  2042. * delayed sack is disabled for the entire association. Note
  2043. * also that this field is mutually exclusive to
  2044. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  2045. * results.
  2046. *
  2047. * SPP_IPV6_FLOWLABEL: Setting this flag enables the
  2048. * setting of the IPV6 flow label value. The value is
  2049. * contained in the spp_ipv6_flowlabel field.
  2050. * Upon retrieval, this flag will be set to indicate that
  2051. * the spp_ipv6_flowlabel field has a valid value returned.
  2052. * If a specific destination address is set (in the
  2053. * spp_address field), then the value returned is that of
  2054. * the address. If just an association is specified (and
  2055. * no address), then the association's default flow label
  2056. * is returned. If neither an association nor a destination
  2057. * is specified, then the socket's default flow label is
  2058. * returned. For non-IPv6 sockets, this flag will be left
  2059. * cleared.
  2060. *
  2061. * SPP_DSCP: Setting this flag enables the setting of the
  2062. * Differentiated Services Code Point (DSCP) value
  2063. * associated with either the association or a specific
  2064. * address. The value is obtained in the spp_dscp field.
  2065. * Upon retrieval, this flag will be set to indicate that
  2066. * the spp_dscp field has a valid value returned. If a
  2067. * specific destination address is set when called (in the
  2068. * spp_address field), then that specific destination
  2069. * address's DSCP value is returned. If just an association
  2070. * is specified, then the association's default DSCP is
  2071. * returned. If neither an association nor a destination is
  2072. * specified, then the socket's default DSCP is returned.
  2073. *
  2074. * spp_ipv6_flowlabel
  2075. * - This field is used in conjunction with the
  2076. * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
  2077. * The 20 least significant bits are used for the flow
  2078. * label. This setting has precedence over any IPv6-layer
  2079. * setting.
  2080. *
  2081. * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
  2082. * and contains the DSCP. The 6 most significant bits are
  2083. * used for the DSCP. This setting has precedence over any
  2084. * IPv4- or IPv6- layer setting.
  2085. */
  2086. static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
  2087. struct sctp_transport *trans,
  2088. struct sctp_association *asoc,
  2089. struct sctp_sock *sp,
  2090. int hb_change,
  2091. int pmtud_change,
  2092. int sackdelay_change)
  2093. {
  2094. int error;
  2095. if (params->spp_flags & SPP_HB_DEMAND && trans) {
  2096. error = sctp_primitive_REQUESTHEARTBEAT(trans->asoc->base.net,
  2097. trans->asoc, trans);
  2098. if (error)
  2099. return error;
  2100. }
  2101. /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of
  2102. * this field is ignored. Note also that a value of zero indicates
  2103. * the current setting should be left unchanged.
  2104. */
  2105. if (params->spp_flags & SPP_HB_ENABLE) {
  2106. /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is
  2107. * set. This lets us use 0 value when this flag
  2108. * is set.
  2109. */
  2110. if (params->spp_flags & SPP_HB_TIME_IS_ZERO)
  2111. params->spp_hbinterval = 0;
  2112. if (params->spp_hbinterval ||
  2113. (params->spp_flags & SPP_HB_TIME_IS_ZERO)) {
  2114. if (trans) {
  2115. trans->hbinterval =
  2116. msecs_to_jiffies(params->spp_hbinterval);
  2117. sctp_transport_reset_hb_timer(trans);
  2118. } else if (asoc) {
  2119. asoc->hbinterval =
  2120. msecs_to_jiffies(params->spp_hbinterval);
  2121. } else {
  2122. sp->hbinterval = params->spp_hbinterval;
  2123. }
  2124. }
  2125. }
  2126. if (hb_change) {
  2127. if (trans) {
  2128. trans->param_flags =
  2129. (trans->param_flags & ~SPP_HB) | hb_change;
  2130. } else if (asoc) {
  2131. asoc->param_flags =
  2132. (asoc->param_flags & ~SPP_HB) | hb_change;
  2133. } else {
  2134. sp->param_flags =
  2135. (sp->param_flags & ~SPP_HB) | hb_change;
  2136. }
  2137. }
  2138. /* When Path MTU discovery is disabled the value specified here will
  2139. * be the "fixed" path mtu (i.e. the value of the spp_flags field must
  2140. * include the flag SPP_PMTUD_DISABLE for this field to have any
  2141. * effect).
  2142. */
  2143. if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) {
  2144. if (trans) {
  2145. trans->pathmtu = params->spp_pathmtu;
  2146. sctp_assoc_sync_pmtu(asoc);
  2147. } else if (asoc) {
  2148. sctp_assoc_set_pmtu(asoc, params->spp_pathmtu);
  2149. } else {
  2150. sp->pathmtu = params->spp_pathmtu;
  2151. }
  2152. }
  2153. if (pmtud_change) {
  2154. if (trans) {
  2155. int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
  2156. (params->spp_flags & SPP_PMTUD_ENABLE);
  2157. trans->param_flags =
  2158. (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
  2159. if (update) {
  2160. sctp_transport_pmtu(trans, sctp_opt2sk(sp));
  2161. sctp_assoc_sync_pmtu(asoc);
  2162. }
  2163. sctp_transport_pl_reset(trans);
  2164. } else if (asoc) {
  2165. asoc->param_flags =
  2166. (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
  2167. } else {
  2168. sp->param_flags =
  2169. (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
  2170. }
  2171. }
  2172. /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the
  2173. * value of this field is ignored. Note also that a value of zero
  2174. * indicates the current setting should be left unchanged.
  2175. */
  2176. if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) {
  2177. if (trans) {
  2178. trans->sackdelay =
  2179. msecs_to_jiffies(params->spp_sackdelay);
  2180. } else if (asoc) {
  2181. asoc->sackdelay =
  2182. msecs_to_jiffies(params->spp_sackdelay);
  2183. } else {
  2184. sp->sackdelay = params->spp_sackdelay;
  2185. }
  2186. }
  2187. if (sackdelay_change) {
  2188. if (trans) {
  2189. trans->param_flags =
  2190. (trans->param_flags & ~SPP_SACKDELAY) |
  2191. sackdelay_change;
  2192. } else if (asoc) {
  2193. asoc->param_flags =
  2194. (asoc->param_flags & ~SPP_SACKDELAY) |
  2195. sackdelay_change;
  2196. } else {
  2197. sp->param_flags =
  2198. (sp->param_flags & ~SPP_SACKDELAY) |
  2199. sackdelay_change;
  2200. }
  2201. }
  2202. /* Note that a value of zero indicates the current setting should be
  2203. left unchanged.
  2204. */
  2205. if (params->spp_pathmaxrxt) {
  2206. if (trans) {
  2207. trans->pathmaxrxt = params->spp_pathmaxrxt;
  2208. } else if (asoc) {
  2209. asoc->pathmaxrxt = params->spp_pathmaxrxt;
  2210. } else {
  2211. sp->pathmaxrxt = params->spp_pathmaxrxt;
  2212. }
  2213. }
  2214. if (params->spp_flags & SPP_IPV6_FLOWLABEL) {
  2215. if (trans) {
  2216. if (trans->ipaddr.sa.sa_family == AF_INET6) {
  2217. trans->flowlabel = params->spp_ipv6_flowlabel &
  2218. SCTP_FLOWLABEL_VAL_MASK;
  2219. trans->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2220. }
  2221. } else if (asoc) {
  2222. struct sctp_transport *t;
  2223. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  2224. transports) {
  2225. if (t->ipaddr.sa.sa_family != AF_INET6)
  2226. continue;
  2227. t->flowlabel = params->spp_ipv6_flowlabel &
  2228. SCTP_FLOWLABEL_VAL_MASK;
  2229. t->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2230. }
  2231. asoc->flowlabel = params->spp_ipv6_flowlabel &
  2232. SCTP_FLOWLABEL_VAL_MASK;
  2233. asoc->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2234. } else if (sctp_opt2sk(sp)->sk_family == AF_INET6) {
  2235. sp->flowlabel = params->spp_ipv6_flowlabel &
  2236. SCTP_FLOWLABEL_VAL_MASK;
  2237. sp->flowlabel |= SCTP_FLOWLABEL_SET_MASK;
  2238. }
  2239. }
  2240. if (params->spp_flags & SPP_DSCP) {
  2241. if (trans) {
  2242. trans->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
  2243. trans->dscp |= SCTP_DSCP_SET_MASK;
  2244. } else if (asoc) {
  2245. struct sctp_transport *t;
  2246. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  2247. transports) {
  2248. t->dscp = params->spp_dscp &
  2249. SCTP_DSCP_VAL_MASK;
  2250. t->dscp |= SCTP_DSCP_SET_MASK;
  2251. }
  2252. asoc->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
  2253. asoc->dscp |= SCTP_DSCP_SET_MASK;
  2254. } else {
  2255. sp->dscp = params->spp_dscp & SCTP_DSCP_VAL_MASK;
  2256. sp->dscp |= SCTP_DSCP_SET_MASK;
  2257. }
  2258. }
  2259. return 0;
  2260. }
  2261. static int sctp_setsockopt_peer_addr_params(struct sock *sk,
  2262. struct sctp_paddrparams *params,
  2263. unsigned int optlen)
  2264. {
  2265. struct sctp_transport *trans = NULL;
  2266. struct sctp_association *asoc = NULL;
  2267. struct sctp_sock *sp = sctp_sk(sk);
  2268. int error;
  2269. int hb_change, pmtud_change, sackdelay_change;
  2270. if (optlen == ALIGN(offsetof(struct sctp_paddrparams,
  2271. spp_ipv6_flowlabel), 4)) {
  2272. if (params->spp_flags & (SPP_DSCP | SPP_IPV6_FLOWLABEL))
  2273. return -EINVAL;
  2274. } else if (optlen != sizeof(*params)) {
  2275. return -EINVAL;
  2276. }
  2277. /* Validate flags and value parameters. */
  2278. hb_change = params->spp_flags & SPP_HB;
  2279. pmtud_change = params->spp_flags & SPP_PMTUD;
  2280. sackdelay_change = params->spp_flags & SPP_SACKDELAY;
  2281. if (hb_change == SPP_HB ||
  2282. pmtud_change == SPP_PMTUD ||
  2283. sackdelay_change == SPP_SACKDELAY ||
  2284. params->spp_sackdelay > 500 ||
  2285. (params->spp_pathmtu &&
  2286. params->spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
  2287. return -EINVAL;
  2288. /* If an address other than INADDR_ANY is specified, and
  2289. * no transport is found, then the request is invalid.
  2290. */
  2291. if (!sctp_is_any(sk, (union sctp_addr *)&params->spp_address)) {
  2292. trans = sctp_addr_id2transport(sk, &params->spp_address,
  2293. params->spp_assoc_id);
  2294. if (!trans)
  2295. return -EINVAL;
  2296. }
  2297. /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
  2298. * socket is a one to many style socket, and an association
  2299. * was not found, then the id was invalid.
  2300. */
  2301. asoc = sctp_id2assoc(sk, params->spp_assoc_id);
  2302. if (!asoc && params->spp_assoc_id != SCTP_FUTURE_ASSOC &&
  2303. sctp_style(sk, UDP))
  2304. return -EINVAL;
  2305. /* Heartbeat demand can only be sent on a transport or
  2306. * association, but not a socket.
  2307. */
  2308. if (params->spp_flags & SPP_HB_DEMAND && !trans && !asoc)
  2309. return -EINVAL;
  2310. /* Process parameters. */
  2311. error = sctp_apply_peer_addr_params(params, trans, asoc, sp,
  2312. hb_change, pmtud_change,
  2313. sackdelay_change);
  2314. if (error)
  2315. return error;
  2316. /* If changes are for association, also apply parameters to each
  2317. * transport.
  2318. */
  2319. if (!trans && asoc) {
  2320. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2321. transports) {
  2322. sctp_apply_peer_addr_params(params, trans, asoc, sp,
  2323. hb_change, pmtud_change,
  2324. sackdelay_change);
  2325. }
  2326. }
  2327. return 0;
  2328. }
  2329. static inline __u32 sctp_spp_sackdelay_enable(__u32 param_flags)
  2330. {
  2331. return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_ENABLE;
  2332. }
  2333. static inline __u32 sctp_spp_sackdelay_disable(__u32 param_flags)
  2334. {
  2335. return (param_flags & ~SPP_SACKDELAY) | SPP_SACKDELAY_DISABLE;
  2336. }
  2337. static void sctp_apply_asoc_delayed_ack(struct sctp_sack_info *params,
  2338. struct sctp_association *asoc)
  2339. {
  2340. struct sctp_transport *trans;
  2341. if (params->sack_delay) {
  2342. asoc->sackdelay = msecs_to_jiffies(params->sack_delay);
  2343. asoc->param_flags =
  2344. sctp_spp_sackdelay_enable(asoc->param_flags);
  2345. }
  2346. if (params->sack_freq == 1) {
  2347. asoc->param_flags =
  2348. sctp_spp_sackdelay_disable(asoc->param_flags);
  2349. } else if (params->sack_freq > 1) {
  2350. asoc->sackfreq = params->sack_freq;
  2351. asoc->param_flags =
  2352. sctp_spp_sackdelay_enable(asoc->param_flags);
  2353. }
  2354. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  2355. transports) {
  2356. if (params->sack_delay) {
  2357. trans->sackdelay = msecs_to_jiffies(params->sack_delay);
  2358. trans->param_flags =
  2359. sctp_spp_sackdelay_enable(trans->param_flags);
  2360. }
  2361. if (params->sack_freq == 1) {
  2362. trans->param_flags =
  2363. sctp_spp_sackdelay_disable(trans->param_flags);
  2364. } else if (params->sack_freq > 1) {
  2365. trans->sackfreq = params->sack_freq;
  2366. trans->param_flags =
  2367. sctp_spp_sackdelay_enable(trans->param_flags);
  2368. }
  2369. }
  2370. }
  2371. /*
  2372. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  2373. *
  2374. * This option will effect the way delayed acks are performed. This
  2375. * option allows you to get or set the delayed ack time, in
  2376. * milliseconds. It also allows changing the delayed ack frequency.
  2377. * Changing the frequency to 1 disables the delayed sack algorithm. If
  2378. * the assoc_id is 0, then this sets or gets the endpoints default
  2379. * values. If the assoc_id field is non-zero, then the set or get
  2380. * effects the specified association for the one to many model (the
  2381. * assoc_id field is ignored by the one to one model). Note that if
  2382. * sack_delay or sack_freq are 0 when setting this option, then the
  2383. * current values will remain unchanged.
  2384. *
  2385. * struct sctp_sack_info {
  2386. * sctp_assoc_t sack_assoc_id;
  2387. * uint32_t sack_delay;
  2388. * uint32_t sack_freq;
  2389. * };
  2390. *
  2391. * sack_assoc_id - This parameter, indicates which association the user
  2392. * is performing an action upon. Note that if this field's value is
  2393. * zero then the endpoints default value is changed (effecting future
  2394. * associations only).
  2395. *
  2396. * sack_delay - This parameter contains the number of milliseconds that
  2397. * the user is requesting the delayed ACK timer be set to. Note that
  2398. * this value is defined in the standard to be between 200 and 500
  2399. * milliseconds.
  2400. *
  2401. * sack_freq - This parameter contains the number of packets that must
  2402. * be received before a sack is sent without waiting for the delay
  2403. * timer to expire. The default value for this is 2, setting this
  2404. * value to 1 will disable the delayed sack algorithm.
  2405. */
  2406. static int __sctp_setsockopt_delayed_ack(struct sock *sk,
  2407. struct sctp_sack_info *params)
  2408. {
  2409. struct sctp_sock *sp = sctp_sk(sk);
  2410. struct sctp_association *asoc;
  2411. /* Validate value parameter. */
  2412. if (params->sack_delay > 500)
  2413. return -EINVAL;
  2414. /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
  2415. * socket is a one to many style socket, and an association
  2416. * was not found, then the id was invalid.
  2417. */
  2418. asoc = sctp_id2assoc(sk, params->sack_assoc_id);
  2419. if (!asoc && params->sack_assoc_id > SCTP_ALL_ASSOC &&
  2420. sctp_style(sk, UDP))
  2421. return -EINVAL;
  2422. if (asoc) {
  2423. sctp_apply_asoc_delayed_ack(params, asoc);
  2424. return 0;
  2425. }
  2426. if (sctp_style(sk, TCP))
  2427. params->sack_assoc_id = SCTP_FUTURE_ASSOC;
  2428. if (params->sack_assoc_id == SCTP_FUTURE_ASSOC ||
  2429. params->sack_assoc_id == SCTP_ALL_ASSOC) {
  2430. if (params->sack_delay) {
  2431. sp->sackdelay = params->sack_delay;
  2432. sp->param_flags =
  2433. sctp_spp_sackdelay_enable(sp->param_flags);
  2434. }
  2435. if (params->sack_freq == 1) {
  2436. sp->param_flags =
  2437. sctp_spp_sackdelay_disable(sp->param_flags);
  2438. } else if (params->sack_freq > 1) {
  2439. sp->sackfreq = params->sack_freq;
  2440. sp->param_flags =
  2441. sctp_spp_sackdelay_enable(sp->param_flags);
  2442. }
  2443. }
  2444. if (params->sack_assoc_id == SCTP_CURRENT_ASSOC ||
  2445. params->sack_assoc_id == SCTP_ALL_ASSOC)
  2446. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  2447. sctp_apply_asoc_delayed_ack(params, asoc);
  2448. return 0;
  2449. }
  2450. static int sctp_setsockopt_delayed_ack(struct sock *sk,
  2451. struct sctp_sack_info *params,
  2452. unsigned int optlen)
  2453. {
  2454. if (optlen == sizeof(struct sctp_assoc_value)) {
  2455. struct sctp_assoc_value *v = (struct sctp_assoc_value *)params;
  2456. struct sctp_sack_info p;
  2457. pr_warn_ratelimited(DEPRECATED
  2458. "%s (pid %d) "
  2459. "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
  2460. "Use struct sctp_sack_info instead\n",
  2461. current->comm, task_pid_nr(current));
  2462. p.sack_assoc_id = v->assoc_id;
  2463. p.sack_delay = v->assoc_value;
  2464. p.sack_freq = v->assoc_value ? 0 : 1;
  2465. return __sctp_setsockopt_delayed_ack(sk, &p);
  2466. }
  2467. if (optlen != sizeof(struct sctp_sack_info))
  2468. return -EINVAL;
  2469. if (params->sack_delay == 0 && params->sack_freq == 0)
  2470. return 0;
  2471. return __sctp_setsockopt_delayed_ack(sk, params);
  2472. }
  2473. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  2474. *
  2475. * Applications can specify protocol parameters for the default association
  2476. * initialization. The option name argument to setsockopt() and getsockopt()
  2477. * is SCTP_INITMSG.
  2478. *
  2479. * Setting initialization parameters is effective only on an unconnected
  2480. * socket (for UDP-style sockets only future associations are effected
  2481. * by the change). With TCP-style sockets, this option is inherited by
  2482. * sockets derived from a listener socket.
  2483. */
  2484. static int sctp_setsockopt_initmsg(struct sock *sk, struct sctp_initmsg *sinit,
  2485. unsigned int optlen)
  2486. {
  2487. struct sctp_sock *sp = sctp_sk(sk);
  2488. if (optlen != sizeof(struct sctp_initmsg))
  2489. return -EINVAL;
  2490. if (sinit->sinit_num_ostreams)
  2491. sp->initmsg.sinit_num_ostreams = sinit->sinit_num_ostreams;
  2492. if (sinit->sinit_max_instreams)
  2493. sp->initmsg.sinit_max_instreams = sinit->sinit_max_instreams;
  2494. if (sinit->sinit_max_attempts)
  2495. sp->initmsg.sinit_max_attempts = sinit->sinit_max_attempts;
  2496. if (sinit->sinit_max_init_timeo)
  2497. sp->initmsg.sinit_max_init_timeo = sinit->sinit_max_init_timeo;
  2498. return 0;
  2499. }
  2500. /*
  2501. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  2502. *
  2503. * Applications that wish to use the sendto() system call may wish to
  2504. * specify a default set of parameters that would normally be supplied
  2505. * through the inclusion of ancillary data. This socket option allows
  2506. * such an application to set the default sctp_sndrcvinfo structure.
  2507. * The application that wishes to use this socket option simply passes
  2508. * in to this call the sctp_sndrcvinfo structure defined in Section
  2509. * 5.2.2) The input parameters accepted by this call include
  2510. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  2511. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  2512. * to this call if the caller is using the UDP model.
  2513. */
  2514. static int sctp_setsockopt_default_send_param(struct sock *sk,
  2515. struct sctp_sndrcvinfo *info,
  2516. unsigned int optlen)
  2517. {
  2518. struct sctp_sock *sp = sctp_sk(sk);
  2519. struct sctp_association *asoc;
  2520. if (optlen != sizeof(*info))
  2521. return -EINVAL;
  2522. if (info->sinfo_flags &
  2523. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  2524. SCTP_ABORT | SCTP_EOF))
  2525. return -EINVAL;
  2526. asoc = sctp_id2assoc(sk, info->sinfo_assoc_id);
  2527. if (!asoc && info->sinfo_assoc_id > SCTP_ALL_ASSOC &&
  2528. sctp_style(sk, UDP))
  2529. return -EINVAL;
  2530. if (asoc) {
  2531. asoc->default_stream = info->sinfo_stream;
  2532. asoc->default_flags = info->sinfo_flags;
  2533. asoc->default_ppid = info->sinfo_ppid;
  2534. asoc->default_context = info->sinfo_context;
  2535. asoc->default_timetolive = info->sinfo_timetolive;
  2536. return 0;
  2537. }
  2538. if (sctp_style(sk, TCP))
  2539. info->sinfo_assoc_id = SCTP_FUTURE_ASSOC;
  2540. if (info->sinfo_assoc_id == SCTP_FUTURE_ASSOC ||
  2541. info->sinfo_assoc_id == SCTP_ALL_ASSOC) {
  2542. sp->default_stream = info->sinfo_stream;
  2543. sp->default_flags = info->sinfo_flags;
  2544. sp->default_ppid = info->sinfo_ppid;
  2545. sp->default_context = info->sinfo_context;
  2546. sp->default_timetolive = info->sinfo_timetolive;
  2547. }
  2548. if (info->sinfo_assoc_id == SCTP_CURRENT_ASSOC ||
  2549. info->sinfo_assoc_id == SCTP_ALL_ASSOC) {
  2550. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  2551. asoc->default_stream = info->sinfo_stream;
  2552. asoc->default_flags = info->sinfo_flags;
  2553. asoc->default_ppid = info->sinfo_ppid;
  2554. asoc->default_context = info->sinfo_context;
  2555. asoc->default_timetolive = info->sinfo_timetolive;
  2556. }
  2557. }
  2558. return 0;
  2559. }
  2560. /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
  2561. * (SCTP_DEFAULT_SNDINFO)
  2562. */
  2563. static int sctp_setsockopt_default_sndinfo(struct sock *sk,
  2564. struct sctp_sndinfo *info,
  2565. unsigned int optlen)
  2566. {
  2567. struct sctp_sock *sp = sctp_sk(sk);
  2568. struct sctp_association *asoc;
  2569. if (optlen != sizeof(*info))
  2570. return -EINVAL;
  2571. if (info->snd_flags &
  2572. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  2573. SCTP_ABORT | SCTP_EOF))
  2574. return -EINVAL;
  2575. asoc = sctp_id2assoc(sk, info->snd_assoc_id);
  2576. if (!asoc && info->snd_assoc_id > SCTP_ALL_ASSOC &&
  2577. sctp_style(sk, UDP))
  2578. return -EINVAL;
  2579. if (asoc) {
  2580. asoc->default_stream = info->snd_sid;
  2581. asoc->default_flags = info->snd_flags;
  2582. asoc->default_ppid = info->snd_ppid;
  2583. asoc->default_context = info->snd_context;
  2584. return 0;
  2585. }
  2586. if (sctp_style(sk, TCP))
  2587. info->snd_assoc_id = SCTP_FUTURE_ASSOC;
  2588. if (info->snd_assoc_id == SCTP_FUTURE_ASSOC ||
  2589. info->snd_assoc_id == SCTP_ALL_ASSOC) {
  2590. sp->default_stream = info->snd_sid;
  2591. sp->default_flags = info->snd_flags;
  2592. sp->default_ppid = info->snd_ppid;
  2593. sp->default_context = info->snd_context;
  2594. }
  2595. if (info->snd_assoc_id == SCTP_CURRENT_ASSOC ||
  2596. info->snd_assoc_id == SCTP_ALL_ASSOC) {
  2597. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  2598. asoc->default_stream = info->snd_sid;
  2599. asoc->default_flags = info->snd_flags;
  2600. asoc->default_ppid = info->snd_ppid;
  2601. asoc->default_context = info->snd_context;
  2602. }
  2603. }
  2604. return 0;
  2605. }
  2606. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  2607. *
  2608. * Requests that the local SCTP stack use the enclosed peer address as
  2609. * the association primary. The enclosed address must be one of the
  2610. * association peer's addresses.
  2611. */
  2612. static int sctp_setsockopt_primary_addr(struct sock *sk, struct sctp_prim *prim,
  2613. unsigned int optlen)
  2614. {
  2615. struct sctp_transport *trans;
  2616. struct sctp_af *af;
  2617. int err;
  2618. if (optlen != sizeof(struct sctp_prim))
  2619. return -EINVAL;
  2620. /* Allow security module to validate address but need address len. */
  2621. af = sctp_get_af_specific(prim->ssp_addr.ss_family);
  2622. if (!af)
  2623. return -EINVAL;
  2624. err = security_sctp_bind_connect(sk, SCTP_PRIMARY_ADDR,
  2625. (struct sockaddr *)&prim->ssp_addr,
  2626. af->sockaddr_len);
  2627. if (err)
  2628. return err;
  2629. trans = sctp_addr_id2transport(sk, &prim->ssp_addr, prim->ssp_assoc_id);
  2630. if (!trans)
  2631. return -EINVAL;
  2632. sctp_assoc_set_primary(trans->asoc, trans);
  2633. return 0;
  2634. }
  2635. /*
  2636. * 7.1.5 SCTP_NODELAY
  2637. *
  2638. * Turn on/off any Nagle-like algorithm. This means that packets are
  2639. * generally sent as soon as possible and no unnecessary delays are
  2640. * introduced, at the cost of more packets in the network. Expects an
  2641. * integer boolean flag.
  2642. */
  2643. static int sctp_setsockopt_nodelay(struct sock *sk, int *val,
  2644. unsigned int optlen)
  2645. {
  2646. if (optlen < sizeof(int))
  2647. return -EINVAL;
  2648. sctp_sk(sk)->nodelay = (*val == 0) ? 0 : 1;
  2649. return 0;
  2650. }
  2651. /*
  2652. *
  2653. * 7.1.1 SCTP_RTOINFO
  2654. *
  2655. * The protocol parameters used to initialize and bound retransmission
  2656. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  2657. * and modify these parameters.
  2658. * All parameters are time values, in milliseconds. A value of 0, when
  2659. * modifying the parameters, indicates that the current value should not
  2660. * be changed.
  2661. *
  2662. */
  2663. static int sctp_setsockopt_rtoinfo(struct sock *sk,
  2664. struct sctp_rtoinfo *rtoinfo,
  2665. unsigned int optlen)
  2666. {
  2667. struct sctp_association *asoc;
  2668. unsigned long rto_min, rto_max;
  2669. struct sctp_sock *sp = sctp_sk(sk);
  2670. if (optlen != sizeof (struct sctp_rtoinfo))
  2671. return -EINVAL;
  2672. asoc = sctp_id2assoc(sk, rtoinfo->srto_assoc_id);
  2673. /* Set the values to the specific association */
  2674. if (!asoc && rtoinfo->srto_assoc_id != SCTP_FUTURE_ASSOC &&
  2675. sctp_style(sk, UDP))
  2676. return -EINVAL;
  2677. rto_max = rtoinfo->srto_max;
  2678. rto_min = rtoinfo->srto_min;
  2679. if (rto_max)
  2680. rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
  2681. else
  2682. rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
  2683. if (rto_min)
  2684. rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
  2685. else
  2686. rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
  2687. if (rto_min > rto_max)
  2688. return -EINVAL;
  2689. if (asoc) {
  2690. if (rtoinfo->srto_initial != 0)
  2691. asoc->rto_initial =
  2692. msecs_to_jiffies(rtoinfo->srto_initial);
  2693. asoc->rto_max = rto_max;
  2694. asoc->rto_min = rto_min;
  2695. } else {
  2696. /* If there is no association or the association-id = 0
  2697. * set the values to the endpoint.
  2698. */
  2699. if (rtoinfo->srto_initial != 0)
  2700. sp->rtoinfo.srto_initial = rtoinfo->srto_initial;
  2701. sp->rtoinfo.srto_max = rto_max;
  2702. sp->rtoinfo.srto_min = rto_min;
  2703. }
  2704. return 0;
  2705. }
  2706. /*
  2707. *
  2708. * 7.1.2 SCTP_ASSOCINFO
  2709. *
  2710. * This option is used to tune the maximum retransmission attempts
  2711. * of the association.
  2712. * Returns an error if the new association retransmission value is
  2713. * greater than the sum of the retransmission value of the peer.
  2714. * See [SCTP] for more information.
  2715. *
  2716. */
  2717. static int sctp_setsockopt_associnfo(struct sock *sk,
  2718. struct sctp_assocparams *assocparams,
  2719. unsigned int optlen)
  2720. {
  2721. struct sctp_association *asoc;
  2722. if (optlen != sizeof(struct sctp_assocparams))
  2723. return -EINVAL;
  2724. asoc = sctp_id2assoc(sk, assocparams->sasoc_assoc_id);
  2725. if (!asoc && assocparams->sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
  2726. sctp_style(sk, UDP))
  2727. return -EINVAL;
  2728. /* Set the values to the specific association */
  2729. if (asoc) {
  2730. if (assocparams->sasoc_asocmaxrxt != 0) {
  2731. __u32 path_sum = 0;
  2732. int paths = 0;
  2733. struct sctp_transport *peer_addr;
  2734. list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list,
  2735. transports) {
  2736. path_sum += peer_addr->pathmaxrxt;
  2737. paths++;
  2738. }
  2739. /* Only validate asocmaxrxt if we have more than
  2740. * one path/transport. We do this because path
  2741. * retransmissions are only counted when we have more
  2742. * then one path.
  2743. */
  2744. if (paths > 1 &&
  2745. assocparams->sasoc_asocmaxrxt > path_sum)
  2746. return -EINVAL;
  2747. asoc->max_retrans = assocparams->sasoc_asocmaxrxt;
  2748. }
  2749. if (assocparams->sasoc_cookie_life != 0)
  2750. asoc->cookie_life =
  2751. ms_to_ktime(assocparams->sasoc_cookie_life);
  2752. } else {
  2753. /* Set the values to the endpoint */
  2754. struct sctp_sock *sp = sctp_sk(sk);
  2755. if (assocparams->sasoc_asocmaxrxt != 0)
  2756. sp->assocparams.sasoc_asocmaxrxt =
  2757. assocparams->sasoc_asocmaxrxt;
  2758. if (assocparams->sasoc_cookie_life != 0)
  2759. sp->assocparams.sasoc_cookie_life =
  2760. assocparams->sasoc_cookie_life;
  2761. }
  2762. return 0;
  2763. }
  2764. /*
  2765. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  2766. *
  2767. * This socket option is a boolean flag which turns on or off mapped V4
  2768. * addresses. If this option is turned on and the socket is type
  2769. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  2770. * If this option is turned off, then no mapping will be done of V4
  2771. * addresses and a user will receive both PF_INET6 and PF_INET type
  2772. * addresses on the socket.
  2773. */
  2774. static int sctp_setsockopt_mappedv4(struct sock *sk, int *val,
  2775. unsigned int optlen)
  2776. {
  2777. struct sctp_sock *sp = sctp_sk(sk);
  2778. if (optlen < sizeof(int))
  2779. return -EINVAL;
  2780. if (*val)
  2781. sp->v4mapped = 1;
  2782. else
  2783. sp->v4mapped = 0;
  2784. return 0;
  2785. }
  2786. /*
  2787. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  2788. * This option will get or set the maximum size to put in any outgoing
  2789. * SCTP DATA chunk. If a message is larger than this size it will be
  2790. * fragmented by SCTP into the specified size. Note that the underlying
  2791. * SCTP implementation may fragment into smaller sized chunks when the
  2792. * PMTU of the underlying association is smaller than the value set by
  2793. * the user. The default value for this option is '0' which indicates
  2794. * the user is NOT limiting fragmentation and only the PMTU will effect
  2795. * SCTP's choice of DATA chunk size. Note also that values set larger
  2796. * than the maximum size of an IP datagram will effectively let SCTP
  2797. * control fragmentation (i.e. the same as setting this option to 0).
  2798. *
  2799. * The following structure is used to access and modify this parameter:
  2800. *
  2801. * struct sctp_assoc_value {
  2802. * sctp_assoc_t assoc_id;
  2803. * uint32_t assoc_value;
  2804. * };
  2805. *
  2806. * assoc_id: This parameter is ignored for one-to-one style sockets.
  2807. * For one-to-many style sockets this parameter indicates which
  2808. * association the user is performing an action upon. Note that if
  2809. * this field's value is zero then the endpoints default value is
  2810. * changed (effecting future associations only).
  2811. * assoc_value: This parameter specifies the maximum size in bytes.
  2812. */
  2813. static int sctp_setsockopt_maxseg(struct sock *sk,
  2814. struct sctp_assoc_value *params,
  2815. unsigned int optlen)
  2816. {
  2817. struct sctp_sock *sp = sctp_sk(sk);
  2818. struct sctp_association *asoc;
  2819. sctp_assoc_t assoc_id;
  2820. int val;
  2821. if (optlen == sizeof(int)) {
  2822. pr_warn_ratelimited(DEPRECATED
  2823. "%s (pid %d) "
  2824. "Use of int in maxseg socket option.\n"
  2825. "Use struct sctp_assoc_value instead\n",
  2826. current->comm, task_pid_nr(current));
  2827. assoc_id = SCTP_FUTURE_ASSOC;
  2828. val = *(int *)params;
  2829. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  2830. assoc_id = params->assoc_id;
  2831. val = params->assoc_value;
  2832. } else {
  2833. return -EINVAL;
  2834. }
  2835. asoc = sctp_id2assoc(sk, assoc_id);
  2836. if (!asoc && assoc_id != SCTP_FUTURE_ASSOC &&
  2837. sctp_style(sk, UDP))
  2838. return -EINVAL;
  2839. if (val) {
  2840. int min_len, max_len;
  2841. __u16 datasize = asoc ? sctp_datachk_len(&asoc->stream) :
  2842. sizeof(struct sctp_data_chunk);
  2843. min_len = sctp_min_frag_point(sp, datasize);
  2844. max_len = SCTP_MAX_CHUNK_LEN - datasize;
  2845. if (val < min_len || val > max_len)
  2846. return -EINVAL;
  2847. }
  2848. if (asoc) {
  2849. asoc->user_frag = val;
  2850. sctp_assoc_update_frag_point(asoc);
  2851. } else {
  2852. sp->user_frag = val;
  2853. }
  2854. return 0;
  2855. }
  2856. /*
  2857. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  2858. *
  2859. * Requests that the peer mark the enclosed address as the association
  2860. * primary. The enclosed address must be one of the association's
  2861. * locally bound addresses. The following structure is used to make a
  2862. * set primary request:
  2863. */
  2864. static int sctp_setsockopt_peer_primary_addr(struct sock *sk,
  2865. struct sctp_setpeerprim *prim,
  2866. unsigned int optlen)
  2867. {
  2868. struct sctp_sock *sp;
  2869. struct sctp_association *asoc = NULL;
  2870. struct sctp_chunk *chunk;
  2871. struct sctp_af *af;
  2872. int err;
  2873. sp = sctp_sk(sk);
  2874. if (!sp->ep->asconf_enable)
  2875. return -EPERM;
  2876. if (optlen != sizeof(struct sctp_setpeerprim))
  2877. return -EINVAL;
  2878. asoc = sctp_id2assoc(sk, prim->sspp_assoc_id);
  2879. if (!asoc)
  2880. return -EINVAL;
  2881. if (!asoc->peer.asconf_capable)
  2882. return -EPERM;
  2883. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  2884. return -EPERM;
  2885. if (!sctp_state(asoc, ESTABLISHED))
  2886. return -ENOTCONN;
  2887. af = sctp_get_af_specific(prim->sspp_addr.ss_family);
  2888. if (!af)
  2889. return -EINVAL;
  2890. if (!af->addr_valid((union sctp_addr *)&prim->sspp_addr, sp, NULL))
  2891. return -EADDRNOTAVAIL;
  2892. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim->sspp_addr))
  2893. return -EADDRNOTAVAIL;
  2894. /* Allow security module to validate address. */
  2895. err = security_sctp_bind_connect(sk, SCTP_SET_PEER_PRIMARY_ADDR,
  2896. (struct sockaddr *)&prim->sspp_addr,
  2897. af->sockaddr_len);
  2898. if (err)
  2899. return err;
  2900. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  2901. chunk = sctp_make_asconf_set_prim(asoc,
  2902. (union sctp_addr *)&prim->sspp_addr);
  2903. if (!chunk)
  2904. return -ENOMEM;
  2905. err = sctp_send_asconf(asoc, chunk);
  2906. pr_debug("%s: we set peer primary addr primitively\n", __func__);
  2907. return err;
  2908. }
  2909. static int sctp_setsockopt_adaptation_layer(struct sock *sk,
  2910. struct sctp_setadaptation *adapt,
  2911. unsigned int optlen)
  2912. {
  2913. if (optlen != sizeof(struct sctp_setadaptation))
  2914. return -EINVAL;
  2915. sctp_sk(sk)->adaptation_ind = adapt->ssb_adaptation_ind;
  2916. return 0;
  2917. }
  2918. /*
  2919. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  2920. *
  2921. * The context field in the sctp_sndrcvinfo structure is normally only
  2922. * used when a failed message is retrieved holding the value that was
  2923. * sent down on the actual send call. This option allows the setting of
  2924. * a default context on an association basis that will be received on
  2925. * reading messages from the peer. This is especially helpful in the
  2926. * one-2-many model for an application to keep some reference to an
  2927. * internal state machine that is processing messages on the
  2928. * association. Note that the setting of this value only effects
  2929. * received messages from the peer and does not effect the value that is
  2930. * saved with outbound messages.
  2931. */
  2932. static int sctp_setsockopt_context(struct sock *sk,
  2933. struct sctp_assoc_value *params,
  2934. unsigned int optlen)
  2935. {
  2936. struct sctp_sock *sp = sctp_sk(sk);
  2937. struct sctp_association *asoc;
  2938. if (optlen != sizeof(struct sctp_assoc_value))
  2939. return -EINVAL;
  2940. asoc = sctp_id2assoc(sk, params->assoc_id);
  2941. if (!asoc && params->assoc_id > SCTP_ALL_ASSOC &&
  2942. sctp_style(sk, UDP))
  2943. return -EINVAL;
  2944. if (asoc) {
  2945. asoc->default_rcv_context = params->assoc_value;
  2946. return 0;
  2947. }
  2948. if (sctp_style(sk, TCP))
  2949. params->assoc_id = SCTP_FUTURE_ASSOC;
  2950. if (params->assoc_id == SCTP_FUTURE_ASSOC ||
  2951. params->assoc_id == SCTP_ALL_ASSOC)
  2952. sp->default_rcv_context = params->assoc_value;
  2953. if (params->assoc_id == SCTP_CURRENT_ASSOC ||
  2954. params->assoc_id == SCTP_ALL_ASSOC)
  2955. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  2956. asoc->default_rcv_context = params->assoc_value;
  2957. return 0;
  2958. }
  2959. /*
  2960. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  2961. *
  2962. * This options will at a minimum specify if the implementation is doing
  2963. * fragmented interleave. Fragmented interleave, for a one to many
  2964. * socket, is when subsequent calls to receive a message may return
  2965. * parts of messages from different associations. Some implementations
  2966. * may allow you to turn this value on or off. If so, when turned off,
  2967. * no fragment interleave will occur (which will cause a head of line
  2968. * blocking amongst multiple associations sharing the same one to many
  2969. * socket). When this option is turned on, then each receive call may
  2970. * come from a different association (thus the user must receive data
  2971. * with the extended calls (e.g. sctp_recvmsg) to keep track of which
  2972. * association each receive belongs to.
  2973. *
  2974. * This option takes a boolean value. A non-zero value indicates that
  2975. * fragmented interleave is on. A value of zero indicates that
  2976. * fragmented interleave is off.
  2977. *
  2978. * Note that it is important that an implementation that allows this
  2979. * option to be turned on, have it off by default. Otherwise an unaware
  2980. * application using the one to many model may become confused and act
  2981. * incorrectly.
  2982. */
  2983. static int sctp_setsockopt_fragment_interleave(struct sock *sk, int *val,
  2984. unsigned int optlen)
  2985. {
  2986. if (optlen != sizeof(int))
  2987. return -EINVAL;
  2988. sctp_sk(sk)->frag_interleave = !!*val;
  2989. if (!sctp_sk(sk)->frag_interleave)
  2990. sctp_sk(sk)->ep->intl_enable = 0;
  2991. return 0;
  2992. }
  2993. /*
  2994. * 8.1.21. Set or Get the SCTP Partial Delivery Point
  2995. * (SCTP_PARTIAL_DELIVERY_POINT)
  2996. *
  2997. * This option will set or get the SCTP partial delivery point. This
  2998. * point is the size of a message where the partial delivery API will be
  2999. * invoked to help free up rwnd space for the peer. Setting this to a
  3000. * lower value will cause partial deliveries to happen more often. The
  3001. * calls argument is an integer that sets or gets the partial delivery
  3002. * point. Note also that the call will fail if the user attempts to set
  3003. * this value larger than the socket receive buffer size.
  3004. *
  3005. * Note that any single message having a length smaller than or equal to
  3006. * the SCTP partial delivery point will be delivered in one single read
  3007. * call as long as the user provided buffer is large enough to hold the
  3008. * message.
  3009. */
  3010. static int sctp_setsockopt_partial_delivery_point(struct sock *sk, u32 *val,
  3011. unsigned int optlen)
  3012. {
  3013. if (optlen != sizeof(u32))
  3014. return -EINVAL;
  3015. /* Note: We double the receive buffer from what the user sets
  3016. * it to be, also initial rwnd is based on rcvbuf/2.
  3017. */
  3018. if (*val > (sk->sk_rcvbuf >> 1))
  3019. return -EINVAL;
  3020. sctp_sk(sk)->pd_point = *val;
  3021. return 0; /* is this the right error code? */
  3022. }
  3023. /*
  3024. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  3025. *
  3026. * This option will allow a user to change the maximum burst of packets
  3027. * that can be emitted by this association. Note that the default value
  3028. * is 4, and some implementations may restrict this setting so that it
  3029. * can only be lowered.
  3030. *
  3031. * NOTE: This text doesn't seem right. Do this on a socket basis with
  3032. * future associations inheriting the socket value.
  3033. */
  3034. static int sctp_setsockopt_maxburst(struct sock *sk,
  3035. struct sctp_assoc_value *params,
  3036. unsigned int optlen)
  3037. {
  3038. struct sctp_sock *sp = sctp_sk(sk);
  3039. struct sctp_association *asoc;
  3040. sctp_assoc_t assoc_id;
  3041. u32 assoc_value;
  3042. if (optlen == sizeof(int)) {
  3043. pr_warn_ratelimited(DEPRECATED
  3044. "%s (pid %d) "
  3045. "Use of int in max_burst socket option deprecated.\n"
  3046. "Use struct sctp_assoc_value instead\n",
  3047. current->comm, task_pid_nr(current));
  3048. assoc_id = SCTP_FUTURE_ASSOC;
  3049. assoc_value = *((int *)params);
  3050. } else if (optlen == sizeof(struct sctp_assoc_value)) {
  3051. assoc_id = params->assoc_id;
  3052. assoc_value = params->assoc_value;
  3053. } else
  3054. return -EINVAL;
  3055. asoc = sctp_id2assoc(sk, assoc_id);
  3056. if (!asoc && assoc_id > SCTP_ALL_ASSOC && sctp_style(sk, UDP))
  3057. return -EINVAL;
  3058. if (asoc) {
  3059. asoc->max_burst = assoc_value;
  3060. return 0;
  3061. }
  3062. if (sctp_style(sk, TCP))
  3063. assoc_id = SCTP_FUTURE_ASSOC;
  3064. if (assoc_id == SCTP_FUTURE_ASSOC || assoc_id == SCTP_ALL_ASSOC)
  3065. sp->max_burst = assoc_value;
  3066. if (assoc_id == SCTP_CURRENT_ASSOC || assoc_id == SCTP_ALL_ASSOC)
  3067. list_for_each_entry(asoc, &sp->ep->asocs, asocs)
  3068. asoc->max_burst = assoc_value;
  3069. return 0;
  3070. }
  3071. /*
  3072. * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK)
  3073. *
  3074. * This set option adds a chunk type that the user is requesting to be
  3075. * received only in an authenticated way. Changes to the list of chunks
  3076. * will only effect future associations on the socket.
  3077. */
  3078. static int sctp_setsockopt_auth_chunk(struct sock *sk,
  3079. struct sctp_authchunk *val,
  3080. unsigned int optlen)
  3081. {
  3082. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3083. if (!ep->auth_enable)
  3084. return -EACCES;
  3085. if (optlen != sizeof(struct sctp_authchunk))
  3086. return -EINVAL;
  3087. switch (val->sauth_chunk) {
  3088. case SCTP_CID_INIT:
  3089. case SCTP_CID_INIT_ACK:
  3090. case SCTP_CID_SHUTDOWN_COMPLETE:
  3091. case SCTP_CID_AUTH:
  3092. return -EINVAL;
  3093. }
  3094. /* add this chunk id to the endpoint */
  3095. return sctp_auth_ep_add_chunkid(ep, val->sauth_chunk);
  3096. }
  3097. /*
  3098. * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT)
  3099. *
  3100. * This option gets or sets the list of HMAC algorithms that the local
  3101. * endpoint requires the peer to use.
  3102. */
  3103. static int sctp_setsockopt_hmac_ident(struct sock *sk,
  3104. struct sctp_hmacalgo *hmacs,
  3105. unsigned int optlen)
  3106. {
  3107. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3108. u32 idents;
  3109. if (!ep->auth_enable)
  3110. return -EACCES;
  3111. if (optlen < sizeof(struct sctp_hmacalgo))
  3112. return -EINVAL;
  3113. optlen = min_t(unsigned int, optlen, sizeof(struct sctp_hmacalgo) +
  3114. SCTP_AUTH_NUM_HMACS * sizeof(u16));
  3115. idents = hmacs->shmac_num_idents;
  3116. if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS ||
  3117. (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo)))
  3118. return -EINVAL;
  3119. return sctp_auth_ep_set_hmacs(ep, hmacs);
  3120. }
  3121. /*
  3122. * 7.1.20. Set a shared key (SCTP_AUTH_KEY)
  3123. *
  3124. * This option will set a shared secret key which is used to build an
  3125. * association shared key.
  3126. */
  3127. static int sctp_setsockopt_auth_key(struct sock *sk,
  3128. struct sctp_authkey *authkey,
  3129. unsigned int optlen)
  3130. {
  3131. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3132. struct sctp_association *asoc;
  3133. int ret = -EINVAL;
  3134. if (optlen <= sizeof(struct sctp_authkey))
  3135. return -EINVAL;
  3136. /* authkey->sca_keylength is u16, so optlen can't be bigger than
  3137. * this.
  3138. */
  3139. optlen = min_t(unsigned int, optlen, USHRT_MAX + sizeof(*authkey));
  3140. if (authkey->sca_keylength > optlen - sizeof(*authkey))
  3141. goto out;
  3142. asoc = sctp_id2assoc(sk, authkey->sca_assoc_id);
  3143. if (!asoc && authkey->sca_assoc_id > SCTP_ALL_ASSOC &&
  3144. sctp_style(sk, UDP))
  3145. goto out;
  3146. if (asoc) {
  3147. ret = sctp_auth_set_key(ep, asoc, authkey);
  3148. goto out;
  3149. }
  3150. if (sctp_style(sk, TCP))
  3151. authkey->sca_assoc_id = SCTP_FUTURE_ASSOC;
  3152. if (authkey->sca_assoc_id == SCTP_FUTURE_ASSOC ||
  3153. authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
  3154. ret = sctp_auth_set_key(ep, asoc, authkey);
  3155. if (ret)
  3156. goto out;
  3157. }
  3158. ret = 0;
  3159. if (authkey->sca_assoc_id == SCTP_CURRENT_ASSOC ||
  3160. authkey->sca_assoc_id == SCTP_ALL_ASSOC) {
  3161. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3162. int res = sctp_auth_set_key(ep, asoc, authkey);
  3163. if (res && !ret)
  3164. ret = res;
  3165. }
  3166. }
  3167. out:
  3168. memzero_explicit(authkey, optlen);
  3169. return ret;
  3170. }
  3171. /*
  3172. * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY)
  3173. *
  3174. * This option will get or set the active shared key to be used to build
  3175. * the association shared key.
  3176. */
  3177. static int sctp_setsockopt_active_key(struct sock *sk,
  3178. struct sctp_authkeyid *val,
  3179. unsigned int optlen)
  3180. {
  3181. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3182. struct sctp_association *asoc;
  3183. int ret = 0;
  3184. if (optlen != sizeof(struct sctp_authkeyid))
  3185. return -EINVAL;
  3186. asoc = sctp_id2assoc(sk, val->scact_assoc_id);
  3187. if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC &&
  3188. sctp_style(sk, UDP))
  3189. return -EINVAL;
  3190. if (asoc)
  3191. return sctp_auth_set_active_key(ep, asoc, val->scact_keynumber);
  3192. if (sctp_style(sk, TCP))
  3193. val->scact_assoc_id = SCTP_FUTURE_ASSOC;
  3194. if (val->scact_assoc_id == SCTP_FUTURE_ASSOC ||
  3195. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3196. ret = sctp_auth_set_active_key(ep, asoc, val->scact_keynumber);
  3197. if (ret)
  3198. return ret;
  3199. }
  3200. if (val->scact_assoc_id == SCTP_CURRENT_ASSOC ||
  3201. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3202. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3203. int res = sctp_auth_set_active_key(ep, asoc,
  3204. val->scact_keynumber);
  3205. if (res && !ret)
  3206. ret = res;
  3207. }
  3208. }
  3209. return ret;
  3210. }
  3211. /*
  3212. * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY)
  3213. *
  3214. * This set option will delete a shared secret key from use.
  3215. */
  3216. static int sctp_setsockopt_del_key(struct sock *sk,
  3217. struct sctp_authkeyid *val,
  3218. unsigned int optlen)
  3219. {
  3220. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3221. struct sctp_association *asoc;
  3222. int ret = 0;
  3223. if (optlen != sizeof(struct sctp_authkeyid))
  3224. return -EINVAL;
  3225. asoc = sctp_id2assoc(sk, val->scact_assoc_id);
  3226. if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC &&
  3227. sctp_style(sk, UDP))
  3228. return -EINVAL;
  3229. if (asoc)
  3230. return sctp_auth_del_key_id(ep, asoc, val->scact_keynumber);
  3231. if (sctp_style(sk, TCP))
  3232. val->scact_assoc_id = SCTP_FUTURE_ASSOC;
  3233. if (val->scact_assoc_id == SCTP_FUTURE_ASSOC ||
  3234. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3235. ret = sctp_auth_del_key_id(ep, asoc, val->scact_keynumber);
  3236. if (ret)
  3237. return ret;
  3238. }
  3239. if (val->scact_assoc_id == SCTP_CURRENT_ASSOC ||
  3240. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3241. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3242. int res = sctp_auth_del_key_id(ep, asoc,
  3243. val->scact_keynumber);
  3244. if (res && !ret)
  3245. ret = res;
  3246. }
  3247. }
  3248. return ret;
  3249. }
  3250. /*
  3251. * 8.3.4 Deactivate a Shared Key (SCTP_AUTH_DEACTIVATE_KEY)
  3252. *
  3253. * This set option will deactivate a shared secret key.
  3254. */
  3255. static int sctp_setsockopt_deactivate_key(struct sock *sk,
  3256. struct sctp_authkeyid *val,
  3257. unsigned int optlen)
  3258. {
  3259. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3260. struct sctp_association *asoc;
  3261. int ret = 0;
  3262. if (optlen != sizeof(struct sctp_authkeyid))
  3263. return -EINVAL;
  3264. asoc = sctp_id2assoc(sk, val->scact_assoc_id);
  3265. if (!asoc && val->scact_assoc_id > SCTP_ALL_ASSOC &&
  3266. sctp_style(sk, UDP))
  3267. return -EINVAL;
  3268. if (asoc)
  3269. return sctp_auth_deact_key_id(ep, asoc, val->scact_keynumber);
  3270. if (sctp_style(sk, TCP))
  3271. val->scact_assoc_id = SCTP_FUTURE_ASSOC;
  3272. if (val->scact_assoc_id == SCTP_FUTURE_ASSOC ||
  3273. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3274. ret = sctp_auth_deact_key_id(ep, asoc, val->scact_keynumber);
  3275. if (ret)
  3276. return ret;
  3277. }
  3278. if (val->scact_assoc_id == SCTP_CURRENT_ASSOC ||
  3279. val->scact_assoc_id == SCTP_ALL_ASSOC) {
  3280. list_for_each_entry(asoc, &ep->asocs, asocs) {
  3281. int res = sctp_auth_deact_key_id(ep, asoc,
  3282. val->scact_keynumber);
  3283. if (res && !ret)
  3284. ret = res;
  3285. }
  3286. }
  3287. return ret;
  3288. }
  3289. /*
  3290. * 8.1.23 SCTP_AUTO_ASCONF
  3291. *
  3292. * This option will enable or disable the use of the automatic generation of
  3293. * ASCONF chunks to add and delete addresses to an existing association. Note
  3294. * that this option has two caveats namely: a) it only affects sockets that
  3295. * are bound to all addresses available to the SCTP stack, and b) the system
  3296. * administrator may have an overriding control that turns the ASCONF feature
  3297. * off no matter what setting the socket option may have.
  3298. * This option expects an integer boolean flag, where a non-zero value turns on
  3299. * the option, and a zero value turns off the option.
  3300. * Note. In this implementation, socket operation overrides default parameter
  3301. * being set by sysctl as well as FreeBSD implementation
  3302. */
  3303. static int sctp_setsockopt_auto_asconf(struct sock *sk, int *val,
  3304. unsigned int optlen)
  3305. {
  3306. struct sctp_sock *sp = sctp_sk(sk);
  3307. if (optlen < sizeof(int))
  3308. return -EINVAL;
  3309. if (!sctp_is_ep_boundall(sk) && *val)
  3310. return -EINVAL;
  3311. if ((*val && sp->do_auto_asconf) || (!*val && !sp->do_auto_asconf))
  3312. return 0;
  3313. spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
  3314. if (*val == 0 && sp->do_auto_asconf) {
  3315. list_del(&sp->auto_asconf_list);
  3316. sp->do_auto_asconf = 0;
  3317. } else if (*val && !sp->do_auto_asconf) {
  3318. list_add_tail(&sp->auto_asconf_list,
  3319. &sock_net(sk)->sctp.auto_asconf_splist);
  3320. sp->do_auto_asconf = 1;
  3321. }
  3322. spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
  3323. return 0;
  3324. }
  3325. /*
  3326. * SCTP_PEER_ADDR_THLDS
  3327. *
  3328. * This option allows us to alter the partially failed threshold for one or all
  3329. * transports in an association. See Section 6.1 of:
  3330. * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
  3331. */
  3332. static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
  3333. struct sctp_paddrthlds_v2 *val,
  3334. unsigned int optlen, bool v2)
  3335. {
  3336. struct sctp_transport *trans;
  3337. struct sctp_association *asoc;
  3338. int len;
  3339. len = v2 ? sizeof(*val) : sizeof(struct sctp_paddrthlds);
  3340. if (optlen < len)
  3341. return -EINVAL;
  3342. if (v2 && val->spt_pathpfthld > val->spt_pathcpthld)
  3343. return -EINVAL;
  3344. if (!sctp_is_any(sk, (const union sctp_addr *)&val->spt_address)) {
  3345. trans = sctp_addr_id2transport(sk, &val->spt_address,
  3346. val->spt_assoc_id);
  3347. if (!trans)
  3348. return -ENOENT;
  3349. if (val->spt_pathmaxrxt)
  3350. trans->pathmaxrxt = val->spt_pathmaxrxt;
  3351. if (v2)
  3352. trans->ps_retrans = val->spt_pathcpthld;
  3353. trans->pf_retrans = val->spt_pathpfthld;
  3354. return 0;
  3355. }
  3356. asoc = sctp_id2assoc(sk, val->spt_assoc_id);
  3357. if (!asoc && val->spt_assoc_id != SCTP_FUTURE_ASSOC &&
  3358. sctp_style(sk, UDP))
  3359. return -EINVAL;
  3360. if (asoc) {
  3361. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  3362. transports) {
  3363. if (val->spt_pathmaxrxt)
  3364. trans->pathmaxrxt = val->spt_pathmaxrxt;
  3365. if (v2)
  3366. trans->ps_retrans = val->spt_pathcpthld;
  3367. trans->pf_retrans = val->spt_pathpfthld;
  3368. }
  3369. if (val->spt_pathmaxrxt)
  3370. asoc->pathmaxrxt = val->spt_pathmaxrxt;
  3371. if (v2)
  3372. asoc->ps_retrans = val->spt_pathcpthld;
  3373. asoc->pf_retrans = val->spt_pathpfthld;
  3374. } else {
  3375. struct sctp_sock *sp = sctp_sk(sk);
  3376. if (val->spt_pathmaxrxt)
  3377. sp->pathmaxrxt = val->spt_pathmaxrxt;
  3378. if (v2)
  3379. sp->ps_retrans = val->spt_pathcpthld;
  3380. sp->pf_retrans = val->spt_pathpfthld;
  3381. }
  3382. return 0;
  3383. }
  3384. static int sctp_setsockopt_recvrcvinfo(struct sock *sk, int *val,
  3385. unsigned int optlen)
  3386. {
  3387. if (optlen < sizeof(int))
  3388. return -EINVAL;
  3389. sctp_sk(sk)->recvrcvinfo = (*val == 0) ? 0 : 1;
  3390. return 0;
  3391. }
  3392. static int sctp_setsockopt_recvnxtinfo(struct sock *sk, int *val,
  3393. unsigned int optlen)
  3394. {
  3395. if (optlen < sizeof(int))
  3396. return -EINVAL;
  3397. sctp_sk(sk)->recvnxtinfo = (*val == 0) ? 0 : 1;
  3398. return 0;
  3399. }
  3400. static int sctp_setsockopt_pr_supported(struct sock *sk,
  3401. struct sctp_assoc_value *params,
  3402. unsigned int optlen)
  3403. {
  3404. struct sctp_association *asoc;
  3405. if (optlen != sizeof(*params))
  3406. return -EINVAL;
  3407. asoc = sctp_id2assoc(sk, params->assoc_id);
  3408. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3409. sctp_style(sk, UDP))
  3410. return -EINVAL;
  3411. sctp_sk(sk)->ep->prsctp_enable = !!params->assoc_value;
  3412. return 0;
  3413. }
  3414. static int sctp_setsockopt_default_prinfo(struct sock *sk,
  3415. struct sctp_default_prinfo *info,
  3416. unsigned int optlen)
  3417. {
  3418. struct sctp_sock *sp = sctp_sk(sk);
  3419. struct sctp_association *asoc;
  3420. int retval = -EINVAL;
  3421. if (optlen != sizeof(*info))
  3422. goto out;
  3423. if (info->pr_policy & ~SCTP_PR_SCTP_MASK)
  3424. goto out;
  3425. if (info->pr_policy == SCTP_PR_SCTP_NONE)
  3426. info->pr_value = 0;
  3427. asoc = sctp_id2assoc(sk, info->pr_assoc_id);
  3428. if (!asoc && info->pr_assoc_id > SCTP_ALL_ASSOC &&
  3429. sctp_style(sk, UDP))
  3430. goto out;
  3431. retval = 0;
  3432. if (asoc) {
  3433. SCTP_PR_SET_POLICY(asoc->default_flags, info->pr_policy);
  3434. asoc->default_timetolive = info->pr_value;
  3435. goto out;
  3436. }
  3437. if (sctp_style(sk, TCP))
  3438. info->pr_assoc_id = SCTP_FUTURE_ASSOC;
  3439. if (info->pr_assoc_id == SCTP_FUTURE_ASSOC ||
  3440. info->pr_assoc_id == SCTP_ALL_ASSOC) {
  3441. SCTP_PR_SET_POLICY(sp->default_flags, info->pr_policy);
  3442. sp->default_timetolive = info->pr_value;
  3443. }
  3444. if (info->pr_assoc_id == SCTP_CURRENT_ASSOC ||
  3445. info->pr_assoc_id == SCTP_ALL_ASSOC) {
  3446. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  3447. SCTP_PR_SET_POLICY(asoc->default_flags,
  3448. info->pr_policy);
  3449. asoc->default_timetolive = info->pr_value;
  3450. }
  3451. }
  3452. out:
  3453. return retval;
  3454. }
  3455. static int sctp_setsockopt_reconfig_supported(struct sock *sk,
  3456. struct sctp_assoc_value *params,
  3457. unsigned int optlen)
  3458. {
  3459. struct sctp_association *asoc;
  3460. int retval = -EINVAL;
  3461. if (optlen != sizeof(*params))
  3462. goto out;
  3463. asoc = sctp_id2assoc(sk, params->assoc_id);
  3464. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3465. sctp_style(sk, UDP))
  3466. goto out;
  3467. sctp_sk(sk)->ep->reconf_enable = !!params->assoc_value;
  3468. retval = 0;
  3469. out:
  3470. return retval;
  3471. }
  3472. static int sctp_setsockopt_enable_strreset(struct sock *sk,
  3473. struct sctp_assoc_value *params,
  3474. unsigned int optlen)
  3475. {
  3476. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  3477. struct sctp_association *asoc;
  3478. int retval = -EINVAL;
  3479. if (optlen != sizeof(*params))
  3480. goto out;
  3481. if (params->assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
  3482. goto out;
  3483. asoc = sctp_id2assoc(sk, params->assoc_id);
  3484. if (!asoc && params->assoc_id > SCTP_ALL_ASSOC &&
  3485. sctp_style(sk, UDP))
  3486. goto out;
  3487. retval = 0;
  3488. if (asoc) {
  3489. asoc->strreset_enable = params->assoc_value;
  3490. goto out;
  3491. }
  3492. if (sctp_style(sk, TCP))
  3493. params->assoc_id = SCTP_FUTURE_ASSOC;
  3494. if (params->assoc_id == SCTP_FUTURE_ASSOC ||
  3495. params->assoc_id == SCTP_ALL_ASSOC)
  3496. ep->strreset_enable = params->assoc_value;
  3497. if (params->assoc_id == SCTP_CURRENT_ASSOC ||
  3498. params->assoc_id == SCTP_ALL_ASSOC)
  3499. list_for_each_entry(asoc, &ep->asocs, asocs)
  3500. asoc->strreset_enable = params->assoc_value;
  3501. out:
  3502. return retval;
  3503. }
  3504. static int sctp_setsockopt_reset_streams(struct sock *sk,
  3505. struct sctp_reset_streams *params,
  3506. unsigned int optlen)
  3507. {
  3508. struct sctp_association *asoc;
  3509. if (optlen < sizeof(*params))
  3510. return -EINVAL;
  3511. /* srs_number_streams is u16, so optlen can't be bigger than this. */
  3512. optlen = min_t(unsigned int, optlen, USHRT_MAX +
  3513. sizeof(__u16) * sizeof(*params));
  3514. if (params->srs_number_streams * sizeof(__u16) >
  3515. optlen - sizeof(*params))
  3516. return -EINVAL;
  3517. asoc = sctp_id2assoc(sk, params->srs_assoc_id);
  3518. if (!asoc)
  3519. return -EINVAL;
  3520. return sctp_send_reset_streams(asoc, params);
  3521. }
  3522. static int sctp_setsockopt_reset_assoc(struct sock *sk, sctp_assoc_t *associd,
  3523. unsigned int optlen)
  3524. {
  3525. struct sctp_association *asoc;
  3526. if (optlen != sizeof(*associd))
  3527. return -EINVAL;
  3528. asoc = sctp_id2assoc(sk, *associd);
  3529. if (!asoc)
  3530. return -EINVAL;
  3531. return sctp_send_reset_assoc(asoc);
  3532. }
  3533. static int sctp_setsockopt_add_streams(struct sock *sk,
  3534. struct sctp_add_streams *params,
  3535. unsigned int optlen)
  3536. {
  3537. struct sctp_association *asoc;
  3538. if (optlen != sizeof(*params))
  3539. return -EINVAL;
  3540. asoc = sctp_id2assoc(sk, params->sas_assoc_id);
  3541. if (!asoc)
  3542. return -EINVAL;
  3543. return sctp_send_add_streams(asoc, params);
  3544. }
  3545. static int sctp_setsockopt_scheduler(struct sock *sk,
  3546. struct sctp_assoc_value *params,
  3547. unsigned int optlen)
  3548. {
  3549. struct sctp_sock *sp = sctp_sk(sk);
  3550. struct sctp_association *asoc;
  3551. int retval = 0;
  3552. if (optlen < sizeof(*params))
  3553. return -EINVAL;
  3554. if (params->assoc_value > SCTP_SS_MAX)
  3555. return -EINVAL;
  3556. asoc = sctp_id2assoc(sk, params->assoc_id);
  3557. if (!asoc && params->assoc_id > SCTP_ALL_ASSOC &&
  3558. sctp_style(sk, UDP))
  3559. return -EINVAL;
  3560. if (asoc)
  3561. return sctp_sched_set_sched(asoc, params->assoc_value);
  3562. if (sctp_style(sk, TCP))
  3563. params->assoc_id = SCTP_FUTURE_ASSOC;
  3564. if (params->assoc_id == SCTP_FUTURE_ASSOC ||
  3565. params->assoc_id == SCTP_ALL_ASSOC)
  3566. sp->default_ss = params->assoc_value;
  3567. if (params->assoc_id == SCTP_CURRENT_ASSOC ||
  3568. params->assoc_id == SCTP_ALL_ASSOC) {
  3569. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  3570. int ret = sctp_sched_set_sched(asoc,
  3571. params->assoc_value);
  3572. if (ret && !retval)
  3573. retval = ret;
  3574. }
  3575. }
  3576. return retval;
  3577. }
  3578. static int sctp_setsockopt_scheduler_value(struct sock *sk,
  3579. struct sctp_stream_value *params,
  3580. unsigned int optlen)
  3581. {
  3582. struct sctp_association *asoc;
  3583. int retval = -EINVAL;
  3584. if (optlen < sizeof(*params))
  3585. goto out;
  3586. asoc = sctp_id2assoc(sk, params->assoc_id);
  3587. if (!asoc && params->assoc_id != SCTP_CURRENT_ASSOC &&
  3588. sctp_style(sk, UDP))
  3589. goto out;
  3590. if (asoc) {
  3591. retval = sctp_sched_set_value(asoc, params->stream_id,
  3592. params->stream_value, GFP_KERNEL);
  3593. goto out;
  3594. }
  3595. retval = 0;
  3596. list_for_each_entry(asoc, &sctp_sk(sk)->ep->asocs, asocs) {
  3597. int ret = sctp_sched_set_value(asoc, params->stream_id,
  3598. params->stream_value,
  3599. GFP_KERNEL);
  3600. if (ret && !retval) /* try to return the 1st error. */
  3601. retval = ret;
  3602. }
  3603. out:
  3604. return retval;
  3605. }
  3606. static int sctp_setsockopt_interleaving_supported(struct sock *sk,
  3607. struct sctp_assoc_value *p,
  3608. unsigned int optlen)
  3609. {
  3610. struct sctp_sock *sp = sctp_sk(sk);
  3611. struct sctp_association *asoc;
  3612. if (optlen < sizeof(*p))
  3613. return -EINVAL;
  3614. asoc = sctp_id2assoc(sk, p->assoc_id);
  3615. if (!asoc && p->assoc_id != SCTP_FUTURE_ASSOC && sctp_style(sk, UDP))
  3616. return -EINVAL;
  3617. if (!sock_net(sk)->sctp.intl_enable || !sp->frag_interleave) {
  3618. return -EPERM;
  3619. }
  3620. sp->ep->intl_enable = !!p->assoc_value;
  3621. return 0;
  3622. }
  3623. static int sctp_setsockopt_reuse_port(struct sock *sk, int *val,
  3624. unsigned int optlen)
  3625. {
  3626. if (!sctp_style(sk, TCP))
  3627. return -EOPNOTSUPP;
  3628. if (sctp_sk(sk)->ep->base.bind_addr.port)
  3629. return -EFAULT;
  3630. if (optlen < sizeof(int))
  3631. return -EINVAL;
  3632. sctp_sk(sk)->reuse = !!*val;
  3633. return 0;
  3634. }
  3635. static int sctp_assoc_ulpevent_type_set(struct sctp_event *param,
  3636. struct sctp_association *asoc)
  3637. {
  3638. struct sctp_ulpevent *event;
  3639. sctp_ulpevent_type_set(&asoc->subscribe, param->se_type, param->se_on);
  3640. if (param->se_type == SCTP_SENDER_DRY_EVENT && param->se_on) {
  3641. if (sctp_outq_is_empty(&asoc->outqueue)) {
  3642. event = sctp_ulpevent_make_sender_dry_event(asoc,
  3643. GFP_USER | __GFP_NOWARN);
  3644. if (!event)
  3645. return -ENOMEM;
  3646. asoc->stream.si->enqueue_event(&asoc->ulpq, event);
  3647. }
  3648. }
  3649. return 0;
  3650. }
  3651. static int sctp_setsockopt_event(struct sock *sk, struct sctp_event *param,
  3652. unsigned int optlen)
  3653. {
  3654. struct sctp_sock *sp = sctp_sk(sk);
  3655. struct sctp_association *asoc;
  3656. int retval = 0;
  3657. if (optlen < sizeof(*param))
  3658. return -EINVAL;
  3659. if (param->se_type < SCTP_SN_TYPE_BASE ||
  3660. param->se_type > SCTP_SN_TYPE_MAX)
  3661. return -EINVAL;
  3662. asoc = sctp_id2assoc(sk, param->se_assoc_id);
  3663. if (!asoc && param->se_assoc_id > SCTP_ALL_ASSOC &&
  3664. sctp_style(sk, UDP))
  3665. return -EINVAL;
  3666. if (asoc)
  3667. return sctp_assoc_ulpevent_type_set(param, asoc);
  3668. if (sctp_style(sk, TCP))
  3669. param->se_assoc_id = SCTP_FUTURE_ASSOC;
  3670. if (param->se_assoc_id == SCTP_FUTURE_ASSOC ||
  3671. param->se_assoc_id == SCTP_ALL_ASSOC)
  3672. sctp_ulpevent_type_set(&sp->subscribe,
  3673. param->se_type, param->se_on);
  3674. if (param->se_assoc_id == SCTP_CURRENT_ASSOC ||
  3675. param->se_assoc_id == SCTP_ALL_ASSOC) {
  3676. list_for_each_entry(asoc, &sp->ep->asocs, asocs) {
  3677. int ret = sctp_assoc_ulpevent_type_set(param, asoc);
  3678. if (ret && !retval)
  3679. retval = ret;
  3680. }
  3681. }
  3682. return retval;
  3683. }
  3684. static int sctp_setsockopt_asconf_supported(struct sock *sk,
  3685. struct sctp_assoc_value *params,
  3686. unsigned int optlen)
  3687. {
  3688. struct sctp_association *asoc;
  3689. struct sctp_endpoint *ep;
  3690. int retval = -EINVAL;
  3691. if (optlen != sizeof(*params))
  3692. goto out;
  3693. asoc = sctp_id2assoc(sk, params->assoc_id);
  3694. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3695. sctp_style(sk, UDP))
  3696. goto out;
  3697. ep = sctp_sk(sk)->ep;
  3698. ep->asconf_enable = !!params->assoc_value;
  3699. if (ep->asconf_enable && ep->auth_enable) {
  3700. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
  3701. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
  3702. }
  3703. retval = 0;
  3704. out:
  3705. return retval;
  3706. }
  3707. static int sctp_setsockopt_auth_supported(struct sock *sk,
  3708. struct sctp_assoc_value *params,
  3709. unsigned int optlen)
  3710. {
  3711. struct sctp_association *asoc;
  3712. struct sctp_endpoint *ep;
  3713. int retval = -EINVAL;
  3714. if (optlen != sizeof(*params))
  3715. goto out;
  3716. asoc = sctp_id2assoc(sk, params->assoc_id);
  3717. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3718. sctp_style(sk, UDP))
  3719. goto out;
  3720. ep = sctp_sk(sk)->ep;
  3721. if (params->assoc_value) {
  3722. retval = sctp_auth_init(ep, GFP_KERNEL);
  3723. if (retval)
  3724. goto out;
  3725. if (ep->asconf_enable) {
  3726. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
  3727. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
  3728. }
  3729. }
  3730. ep->auth_enable = !!params->assoc_value;
  3731. retval = 0;
  3732. out:
  3733. return retval;
  3734. }
  3735. static int sctp_setsockopt_ecn_supported(struct sock *sk,
  3736. struct sctp_assoc_value *params,
  3737. unsigned int optlen)
  3738. {
  3739. struct sctp_association *asoc;
  3740. int retval = -EINVAL;
  3741. if (optlen != sizeof(*params))
  3742. goto out;
  3743. asoc = sctp_id2assoc(sk, params->assoc_id);
  3744. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3745. sctp_style(sk, UDP))
  3746. goto out;
  3747. sctp_sk(sk)->ep->ecn_enable = !!params->assoc_value;
  3748. retval = 0;
  3749. out:
  3750. return retval;
  3751. }
  3752. static int sctp_setsockopt_pf_expose(struct sock *sk,
  3753. struct sctp_assoc_value *params,
  3754. unsigned int optlen)
  3755. {
  3756. struct sctp_association *asoc;
  3757. int retval = -EINVAL;
  3758. if (optlen != sizeof(*params))
  3759. goto out;
  3760. if (params->assoc_value > SCTP_PF_EXPOSE_MAX)
  3761. goto out;
  3762. asoc = sctp_id2assoc(sk, params->assoc_id);
  3763. if (!asoc && params->assoc_id != SCTP_FUTURE_ASSOC &&
  3764. sctp_style(sk, UDP))
  3765. goto out;
  3766. if (asoc)
  3767. asoc->pf_expose = params->assoc_value;
  3768. else
  3769. sctp_sk(sk)->pf_expose = params->assoc_value;
  3770. retval = 0;
  3771. out:
  3772. return retval;
  3773. }
  3774. static int sctp_setsockopt_encap_port(struct sock *sk,
  3775. struct sctp_udpencaps *encap,
  3776. unsigned int optlen)
  3777. {
  3778. struct sctp_association *asoc;
  3779. struct sctp_transport *t;
  3780. __be16 encap_port;
  3781. if (optlen != sizeof(*encap))
  3782. return -EINVAL;
  3783. /* If an address other than INADDR_ANY is specified, and
  3784. * no transport is found, then the request is invalid.
  3785. */
  3786. encap_port = (__force __be16)encap->sue_port;
  3787. if (!sctp_is_any(sk, (union sctp_addr *)&encap->sue_address)) {
  3788. t = sctp_addr_id2transport(sk, &encap->sue_address,
  3789. encap->sue_assoc_id);
  3790. if (!t)
  3791. return -EINVAL;
  3792. t->encap_port = encap_port;
  3793. return 0;
  3794. }
  3795. /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
  3796. * socket is a one to many style socket, and an association
  3797. * was not found, then the id was invalid.
  3798. */
  3799. asoc = sctp_id2assoc(sk, encap->sue_assoc_id);
  3800. if (!asoc && encap->sue_assoc_id != SCTP_FUTURE_ASSOC &&
  3801. sctp_style(sk, UDP))
  3802. return -EINVAL;
  3803. /* If changes are for association, also apply encap_port to
  3804. * each transport.
  3805. */
  3806. if (asoc) {
  3807. list_for_each_entry(t, &asoc->peer.transport_addr_list,
  3808. transports)
  3809. t->encap_port = encap_port;
  3810. asoc->encap_port = encap_port;
  3811. return 0;
  3812. }
  3813. sctp_sk(sk)->encap_port = encap_port;
  3814. return 0;
  3815. }
  3816. static int sctp_setsockopt_probe_interval(struct sock *sk,
  3817. struct sctp_probeinterval *params,
  3818. unsigned int optlen)
  3819. {
  3820. struct sctp_association *asoc;
  3821. struct sctp_transport *t;
  3822. __u32 probe_interval;
  3823. if (optlen != sizeof(*params))
  3824. return -EINVAL;
  3825. probe_interval = params->spi_interval;
  3826. if (probe_interval && probe_interval < SCTP_PROBE_TIMER_MIN)
  3827. return -EINVAL;
  3828. /* If an address other than INADDR_ANY is specified, and
  3829. * no transport is found, then the request is invalid.
  3830. */
  3831. if (!sctp_is_any(sk, (union sctp_addr *)&params->spi_address)) {
  3832. t = sctp_addr_id2transport(sk, &params->spi_address,
  3833. params->spi_assoc_id);
  3834. if (!t)
  3835. return -EINVAL;
  3836. t->probe_interval = msecs_to_jiffies(probe_interval);
  3837. sctp_transport_pl_reset(t);
  3838. return 0;
  3839. }
  3840. /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
  3841. * socket is a one to many style socket, and an association
  3842. * was not found, then the id was invalid.
  3843. */
  3844. asoc = sctp_id2assoc(sk, params->spi_assoc_id);
  3845. if (!asoc && params->spi_assoc_id != SCTP_FUTURE_ASSOC &&
  3846. sctp_style(sk, UDP))
  3847. return -EINVAL;
  3848. /* If changes are for association, also apply probe_interval to
  3849. * each transport.
  3850. */
  3851. if (asoc) {
  3852. list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) {
  3853. t->probe_interval = msecs_to_jiffies(probe_interval);
  3854. sctp_transport_pl_reset(t);
  3855. }
  3856. asoc->probe_interval = msecs_to_jiffies(probe_interval);
  3857. return 0;
  3858. }
  3859. sctp_sk(sk)->probe_interval = probe_interval;
  3860. return 0;
  3861. }
  3862. /* API 6.2 setsockopt(), getsockopt()
  3863. *
  3864. * Applications use setsockopt() and getsockopt() to set or retrieve
  3865. * socket options. Socket options are used to change the default
  3866. * behavior of sockets calls. They are described in Section 7.
  3867. *
  3868. * The syntax is:
  3869. *
  3870. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  3871. * int __user *optlen);
  3872. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  3873. * int optlen);
  3874. *
  3875. * sd - the socket descript.
  3876. * level - set to IPPROTO_SCTP for all SCTP options.
  3877. * optname - the option name.
  3878. * optval - the buffer to store the value of the option.
  3879. * optlen - the size of the buffer.
  3880. */
  3881. static int sctp_setsockopt(struct sock *sk, int level, int optname,
  3882. sockptr_t optval, unsigned int optlen)
  3883. {
  3884. void *kopt = NULL;
  3885. int retval = 0;
  3886. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  3887. /* I can hardly begin to describe how wrong this is. This is
  3888. * so broken as to be worse than useless. The API draft
  3889. * REALLY is NOT helpful here... I am not convinced that the
  3890. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  3891. * are at all well-founded.
  3892. */
  3893. if (level != SOL_SCTP) {
  3894. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3895. return af->setsockopt(sk, level, optname, optval, optlen);
  3896. }
  3897. if (optlen > 0) {
  3898. /* Trim it to the biggest size sctp sockopt may need if necessary */
  3899. optlen = min_t(unsigned int, optlen,
  3900. PAGE_ALIGN(USHRT_MAX +
  3901. sizeof(__u16) * sizeof(struct sctp_reset_streams)));
  3902. kopt = memdup_sockptr(optval, optlen);
  3903. if (IS_ERR(kopt))
  3904. return PTR_ERR(kopt);
  3905. }
  3906. lock_sock(sk);
  3907. switch (optname) {
  3908. case SCTP_SOCKOPT_BINDX_ADD:
  3909. /* 'optlen' is the size of the addresses buffer. */
  3910. retval = sctp_setsockopt_bindx(sk, kopt, optlen,
  3911. SCTP_BINDX_ADD_ADDR);
  3912. break;
  3913. case SCTP_SOCKOPT_BINDX_REM:
  3914. /* 'optlen' is the size of the addresses buffer. */
  3915. retval = sctp_setsockopt_bindx(sk, kopt, optlen,
  3916. SCTP_BINDX_REM_ADDR);
  3917. break;
  3918. case SCTP_SOCKOPT_CONNECTX_OLD:
  3919. /* 'optlen' is the size of the addresses buffer. */
  3920. retval = sctp_setsockopt_connectx_old(sk, kopt, optlen);
  3921. break;
  3922. case SCTP_SOCKOPT_CONNECTX:
  3923. /* 'optlen' is the size of the addresses buffer. */
  3924. retval = sctp_setsockopt_connectx(sk, kopt, optlen);
  3925. break;
  3926. case SCTP_DISABLE_FRAGMENTS:
  3927. retval = sctp_setsockopt_disable_fragments(sk, kopt, optlen);
  3928. break;
  3929. case SCTP_EVENTS:
  3930. retval = sctp_setsockopt_events(sk, kopt, optlen);
  3931. break;
  3932. case SCTP_AUTOCLOSE:
  3933. retval = sctp_setsockopt_autoclose(sk, kopt, optlen);
  3934. break;
  3935. case SCTP_PEER_ADDR_PARAMS:
  3936. retval = sctp_setsockopt_peer_addr_params(sk, kopt, optlen);
  3937. break;
  3938. case SCTP_DELAYED_SACK:
  3939. retval = sctp_setsockopt_delayed_ack(sk, kopt, optlen);
  3940. break;
  3941. case SCTP_PARTIAL_DELIVERY_POINT:
  3942. retval = sctp_setsockopt_partial_delivery_point(sk, kopt, optlen);
  3943. break;
  3944. case SCTP_INITMSG:
  3945. retval = sctp_setsockopt_initmsg(sk, kopt, optlen);
  3946. break;
  3947. case SCTP_DEFAULT_SEND_PARAM:
  3948. retval = sctp_setsockopt_default_send_param(sk, kopt, optlen);
  3949. break;
  3950. case SCTP_DEFAULT_SNDINFO:
  3951. retval = sctp_setsockopt_default_sndinfo(sk, kopt, optlen);
  3952. break;
  3953. case SCTP_PRIMARY_ADDR:
  3954. retval = sctp_setsockopt_primary_addr(sk, kopt, optlen);
  3955. break;
  3956. case SCTP_SET_PEER_PRIMARY_ADDR:
  3957. retval = sctp_setsockopt_peer_primary_addr(sk, kopt, optlen);
  3958. break;
  3959. case SCTP_NODELAY:
  3960. retval = sctp_setsockopt_nodelay(sk, kopt, optlen);
  3961. break;
  3962. case SCTP_RTOINFO:
  3963. retval = sctp_setsockopt_rtoinfo(sk, kopt, optlen);
  3964. break;
  3965. case SCTP_ASSOCINFO:
  3966. retval = sctp_setsockopt_associnfo(sk, kopt, optlen);
  3967. break;
  3968. case SCTP_I_WANT_MAPPED_V4_ADDR:
  3969. retval = sctp_setsockopt_mappedv4(sk, kopt, optlen);
  3970. break;
  3971. case SCTP_MAXSEG:
  3972. retval = sctp_setsockopt_maxseg(sk, kopt, optlen);
  3973. break;
  3974. case SCTP_ADAPTATION_LAYER:
  3975. retval = sctp_setsockopt_adaptation_layer(sk, kopt, optlen);
  3976. break;
  3977. case SCTP_CONTEXT:
  3978. retval = sctp_setsockopt_context(sk, kopt, optlen);
  3979. break;
  3980. case SCTP_FRAGMENT_INTERLEAVE:
  3981. retval = sctp_setsockopt_fragment_interleave(sk, kopt, optlen);
  3982. break;
  3983. case SCTP_MAX_BURST:
  3984. retval = sctp_setsockopt_maxburst(sk, kopt, optlen);
  3985. break;
  3986. case SCTP_AUTH_CHUNK:
  3987. retval = sctp_setsockopt_auth_chunk(sk, kopt, optlen);
  3988. break;
  3989. case SCTP_HMAC_IDENT:
  3990. retval = sctp_setsockopt_hmac_ident(sk, kopt, optlen);
  3991. break;
  3992. case SCTP_AUTH_KEY:
  3993. retval = sctp_setsockopt_auth_key(sk, kopt, optlen);
  3994. break;
  3995. case SCTP_AUTH_ACTIVE_KEY:
  3996. retval = sctp_setsockopt_active_key(sk, kopt, optlen);
  3997. break;
  3998. case SCTP_AUTH_DELETE_KEY:
  3999. retval = sctp_setsockopt_del_key(sk, kopt, optlen);
  4000. break;
  4001. case SCTP_AUTH_DEACTIVATE_KEY:
  4002. retval = sctp_setsockopt_deactivate_key(sk, kopt, optlen);
  4003. break;
  4004. case SCTP_AUTO_ASCONF:
  4005. retval = sctp_setsockopt_auto_asconf(sk, kopt, optlen);
  4006. break;
  4007. case SCTP_PEER_ADDR_THLDS:
  4008. retval = sctp_setsockopt_paddr_thresholds(sk, kopt, optlen,
  4009. false);
  4010. break;
  4011. case SCTP_PEER_ADDR_THLDS_V2:
  4012. retval = sctp_setsockopt_paddr_thresholds(sk, kopt, optlen,
  4013. true);
  4014. break;
  4015. case SCTP_RECVRCVINFO:
  4016. retval = sctp_setsockopt_recvrcvinfo(sk, kopt, optlen);
  4017. break;
  4018. case SCTP_RECVNXTINFO:
  4019. retval = sctp_setsockopt_recvnxtinfo(sk, kopt, optlen);
  4020. break;
  4021. case SCTP_PR_SUPPORTED:
  4022. retval = sctp_setsockopt_pr_supported(sk, kopt, optlen);
  4023. break;
  4024. case SCTP_DEFAULT_PRINFO:
  4025. retval = sctp_setsockopt_default_prinfo(sk, kopt, optlen);
  4026. break;
  4027. case SCTP_RECONFIG_SUPPORTED:
  4028. retval = sctp_setsockopt_reconfig_supported(sk, kopt, optlen);
  4029. break;
  4030. case SCTP_ENABLE_STREAM_RESET:
  4031. retval = sctp_setsockopt_enable_strreset(sk, kopt, optlen);
  4032. break;
  4033. case SCTP_RESET_STREAMS:
  4034. retval = sctp_setsockopt_reset_streams(sk, kopt, optlen);
  4035. break;
  4036. case SCTP_RESET_ASSOC:
  4037. retval = sctp_setsockopt_reset_assoc(sk, kopt, optlen);
  4038. break;
  4039. case SCTP_ADD_STREAMS:
  4040. retval = sctp_setsockopt_add_streams(sk, kopt, optlen);
  4041. break;
  4042. case SCTP_STREAM_SCHEDULER:
  4043. retval = sctp_setsockopt_scheduler(sk, kopt, optlen);
  4044. break;
  4045. case SCTP_STREAM_SCHEDULER_VALUE:
  4046. retval = sctp_setsockopt_scheduler_value(sk, kopt, optlen);
  4047. break;
  4048. case SCTP_INTERLEAVING_SUPPORTED:
  4049. retval = sctp_setsockopt_interleaving_supported(sk, kopt,
  4050. optlen);
  4051. break;
  4052. case SCTP_REUSE_PORT:
  4053. retval = sctp_setsockopt_reuse_port(sk, kopt, optlen);
  4054. break;
  4055. case SCTP_EVENT:
  4056. retval = sctp_setsockopt_event(sk, kopt, optlen);
  4057. break;
  4058. case SCTP_ASCONF_SUPPORTED:
  4059. retval = sctp_setsockopt_asconf_supported(sk, kopt, optlen);
  4060. break;
  4061. case SCTP_AUTH_SUPPORTED:
  4062. retval = sctp_setsockopt_auth_supported(sk, kopt, optlen);
  4063. break;
  4064. case SCTP_ECN_SUPPORTED:
  4065. retval = sctp_setsockopt_ecn_supported(sk, kopt, optlen);
  4066. break;
  4067. case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE:
  4068. retval = sctp_setsockopt_pf_expose(sk, kopt, optlen);
  4069. break;
  4070. case SCTP_REMOTE_UDP_ENCAPS_PORT:
  4071. retval = sctp_setsockopt_encap_port(sk, kopt, optlen);
  4072. break;
  4073. case SCTP_PLPMTUD_PROBE_INTERVAL:
  4074. retval = sctp_setsockopt_probe_interval(sk, kopt, optlen);
  4075. break;
  4076. default:
  4077. retval = -ENOPROTOOPT;
  4078. break;
  4079. }
  4080. release_sock(sk);
  4081. kfree(kopt);
  4082. return retval;
  4083. }
  4084. /* API 3.1.6 connect() - UDP Style Syntax
  4085. *
  4086. * An application may use the connect() call in the UDP model to initiate an
  4087. * association without sending data.
  4088. *
  4089. * The syntax is:
  4090. *
  4091. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  4092. *
  4093. * sd: the socket descriptor to have a new association added to.
  4094. *
  4095. * nam: the address structure (either struct sockaddr_in or struct
  4096. * sockaddr_in6 defined in RFC2553 [7]).
  4097. *
  4098. * len: the size of the address.
  4099. */
  4100. static int sctp_connect(struct sock *sk, struct sockaddr *addr,
  4101. int addr_len, int flags)
  4102. {
  4103. struct sctp_af *af;
  4104. int err = -EINVAL;
  4105. lock_sock(sk);
  4106. pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
  4107. addr, addr_len);
  4108. /* Validate addr_len before calling common connect/connectx routine. */
  4109. af = sctp_get_af_specific(addr->sa_family);
  4110. if (af && addr_len >= af->sockaddr_len)
  4111. err = __sctp_connect(sk, addr, af->sockaddr_len, flags, NULL);
  4112. release_sock(sk);
  4113. return err;
  4114. }
  4115. int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
  4116. int addr_len, int flags)
  4117. {
  4118. if (addr_len < sizeof(uaddr->sa_family))
  4119. return -EINVAL;
  4120. if (uaddr->sa_family == AF_UNSPEC)
  4121. return -EOPNOTSUPP;
  4122. return sctp_connect(sock->sk, uaddr, addr_len, flags);
  4123. }
  4124. /* Only called when shutdown a listening SCTP socket. */
  4125. static int sctp_disconnect(struct sock *sk, int flags)
  4126. {
  4127. if (!sctp_style(sk, TCP))
  4128. return -EOPNOTSUPP;
  4129. sk->sk_shutdown |= RCV_SHUTDOWN;
  4130. return 0;
  4131. }
  4132. /* 4.1.4 accept() - TCP Style Syntax
  4133. *
  4134. * Applications use accept() call to remove an established SCTP
  4135. * association from the accept queue of the endpoint. A new socket
  4136. * descriptor will be returned from accept() to represent the newly
  4137. * formed association.
  4138. */
  4139. static struct sock *sctp_accept(struct sock *sk, struct proto_accept_arg *arg)
  4140. {
  4141. struct sctp_sock *sp;
  4142. struct sctp_endpoint *ep;
  4143. struct sock *newsk = NULL;
  4144. struct sctp_association *asoc;
  4145. long timeo;
  4146. int error = 0;
  4147. lock_sock(sk);
  4148. sp = sctp_sk(sk);
  4149. ep = sp->ep;
  4150. if (!sctp_style(sk, TCP)) {
  4151. error = -EOPNOTSUPP;
  4152. goto out;
  4153. }
  4154. if (!sctp_sstate(sk, LISTENING) ||
  4155. (sk->sk_shutdown & RCV_SHUTDOWN)) {
  4156. error = -EINVAL;
  4157. goto out;
  4158. }
  4159. timeo = sock_rcvtimeo(sk, arg->flags & O_NONBLOCK);
  4160. error = sctp_wait_for_accept(sk, timeo);
  4161. if (error)
  4162. goto out;
  4163. /* We treat the list of associations on the endpoint as the accept
  4164. * queue and pick the first association on the list.
  4165. */
  4166. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  4167. newsk = sp->pf->create_accept_sk(sk, asoc, arg->kern);
  4168. if (!newsk) {
  4169. error = -ENOMEM;
  4170. goto out;
  4171. }
  4172. /* Populate the fields of the newsk from the oldsk and migrate the
  4173. * asoc to the newsk.
  4174. */
  4175. error = sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  4176. if (error) {
  4177. sk_common_release(newsk);
  4178. newsk = NULL;
  4179. }
  4180. out:
  4181. release_sock(sk);
  4182. arg->err = error;
  4183. return newsk;
  4184. }
  4185. /* The SCTP ioctl handler. */
  4186. static int sctp_ioctl(struct sock *sk, int cmd, int *karg)
  4187. {
  4188. int rc = -ENOTCONN;
  4189. lock_sock(sk);
  4190. /*
  4191. * SEQPACKET-style sockets in LISTENING state are valid, for
  4192. * SCTP, so only discard TCP-style sockets in LISTENING state.
  4193. */
  4194. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  4195. goto out;
  4196. switch (cmd) {
  4197. case SIOCINQ: {
  4198. struct sk_buff *skb;
  4199. *karg = 0;
  4200. skb = skb_peek(&sk->sk_receive_queue);
  4201. if (skb != NULL) {
  4202. /*
  4203. * We will only return the amount of this packet since
  4204. * that is all that will be read.
  4205. */
  4206. *karg = skb->len;
  4207. }
  4208. rc = 0;
  4209. break;
  4210. }
  4211. default:
  4212. rc = -ENOIOCTLCMD;
  4213. break;
  4214. }
  4215. out:
  4216. release_sock(sk);
  4217. return rc;
  4218. }
  4219. /* This is the function which gets called during socket creation to
  4220. * initialized the SCTP-specific portion of the sock.
  4221. * The sock structure should already be zero-filled memory.
  4222. */
  4223. static int sctp_init_sock(struct sock *sk)
  4224. {
  4225. struct net *net = sock_net(sk);
  4226. struct sctp_sock *sp;
  4227. pr_debug("%s: sk:%p\n", __func__, sk);
  4228. sp = sctp_sk(sk);
  4229. /* Initialize the SCTP per socket area. */
  4230. switch (sk->sk_type) {
  4231. case SOCK_SEQPACKET:
  4232. sp->type = SCTP_SOCKET_UDP;
  4233. break;
  4234. case SOCK_STREAM:
  4235. sp->type = SCTP_SOCKET_TCP;
  4236. break;
  4237. default:
  4238. return -ESOCKTNOSUPPORT;
  4239. }
  4240. sk->sk_gso_type = SKB_GSO_SCTP;
  4241. /* Initialize default send parameters. These parameters can be
  4242. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  4243. */
  4244. sp->default_stream = 0;
  4245. sp->default_ppid = 0;
  4246. sp->default_flags = 0;
  4247. sp->default_context = 0;
  4248. sp->default_timetolive = 0;
  4249. sp->default_rcv_context = 0;
  4250. sp->max_burst = net->sctp.max_burst;
  4251. sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
  4252. /* Initialize default setup parameters. These parameters
  4253. * can be modified with the SCTP_INITMSG socket option or
  4254. * overridden by the SCTP_INIT CMSG.
  4255. */
  4256. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  4257. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  4258. sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
  4259. sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
  4260. /* Initialize default RTO related parameters. These parameters can
  4261. * be modified for with the SCTP_RTOINFO socket option.
  4262. */
  4263. sp->rtoinfo.srto_initial = net->sctp.rto_initial;
  4264. sp->rtoinfo.srto_max = net->sctp.rto_max;
  4265. sp->rtoinfo.srto_min = net->sctp.rto_min;
  4266. /* Initialize default association related parameters. These parameters
  4267. * can be modified with the SCTP_ASSOCINFO socket option.
  4268. */
  4269. sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
  4270. sp->assocparams.sasoc_number_peer_destinations = 0;
  4271. sp->assocparams.sasoc_peer_rwnd = 0;
  4272. sp->assocparams.sasoc_local_rwnd = 0;
  4273. sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
  4274. /* Initialize default event subscriptions. By default, all the
  4275. * options are off.
  4276. */
  4277. sp->subscribe = 0;
  4278. /* Default Peer Address Parameters. These defaults can
  4279. * be modified via SCTP_PEER_ADDR_PARAMS
  4280. */
  4281. sp->hbinterval = net->sctp.hb_interval;
  4282. sp->udp_port = htons(net->sctp.udp_port);
  4283. sp->encap_port = htons(net->sctp.encap_port);
  4284. sp->pathmaxrxt = net->sctp.max_retrans_path;
  4285. sp->pf_retrans = net->sctp.pf_retrans;
  4286. sp->ps_retrans = net->sctp.ps_retrans;
  4287. sp->pf_expose = net->sctp.pf_expose;
  4288. sp->pathmtu = 0; /* allow default discovery */
  4289. sp->sackdelay = net->sctp.sack_timeout;
  4290. sp->sackfreq = 2;
  4291. sp->param_flags = SPP_HB_ENABLE |
  4292. SPP_PMTUD_ENABLE |
  4293. SPP_SACKDELAY_ENABLE;
  4294. sp->default_ss = SCTP_SS_DEFAULT;
  4295. /* If enabled no SCTP message fragmentation will be performed.
  4296. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  4297. */
  4298. sp->disable_fragments = 0;
  4299. /* Enable Nagle algorithm by default. */
  4300. sp->nodelay = 0;
  4301. sp->recvrcvinfo = 0;
  4302. sp->recvnxtinfo = 0;
  4303. /* Enable by default. */
  4304. sp->v4mapped = 1;
  4305. /* Auto-close idle associations after the configured
  4306. * number of seconds. A value of 0 disables this
  4307. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  4308. * for UDP-style sockets only.
  4309. */
  4310. sp->autoclose = 0;
  4311. /* User specified fragmentation limit. */
  4312. sp->user_frag = 0;
  4313. sp->adaptation_ind = 0;
  4314. sp->pf = sctp_get_pf_specific(sk->sk_family);
  4315. /* Control variables for partial data delivery. */
  4316. atomic_set(&sp->pd_mode, 0);
  4317. skb_queue_head_init(&sp->pd_lobby);
  4318. sp->frag_interleave = 0;
  4319. sp->probe_interval = net->sctp.probe_interval;
  4320. /* Create a per socket endpoint structure. Even if we
  4321. * change the data structure relationships, this may still
  4322. * be useful for storing pre-connect address information.
  4323. */
  4324. sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
  4325. if (!sp->ep)
  4326. return -ENOMEM;
  4327. sp->hmac = NULL;
  4328. sk->sk_destruct = sctp_destruct_sock;
  4329. SCTP_DBG_OBJCNT_INC(sock);
  4330. sk_sockets_allocated_inc(sk);
  4331. sock_prot_inuse_add(net, sk->sk_prot, 1);
  4332. return 0;
  4333. }
  4334. /* Cleanup any SCTP per socket resources. Must be called with
  4335. * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
  4336. */
  4337. static void sctp_destroy_sock(struct sock *sk)
  4338. {
  4339. struct sctp_sock *sp;
  4340. pr_debug("%s: sk:%p\n", __func__, sk);
  4341. /* Release our hold on the endpoint. */
  4342. sp = sctp_sk(sk);
  4343. /* This could happen during socket init, thus we bail out
  4344. * early, since the rest of the below is not setup either.
  4345. */
  4346. if (sp->ep == NULL)
  4347. return;
  4348. if (sp->do_auto_asconf) {
  4349. sp->do_auto_asconf = 0;
  4350. list_del(&sp->auto_asconf_list);
  4351. }
  4352. sctp_endpoint_free(sp->ep);
  4353. sk_sockets_allocated_dec(sk);
  4354. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  4355. }
  4356. /* Triggered when there are no references on the socket anymore */
  4357. static void sctp_destruct_common(struct sock *sk)
  4358. {
  4359. struct sctp_sock *sp = sctp_sk(sk);
  4360. /* Free up the HMAC transform. */
  4361. crypto_free_shash(sp->hmac);
  4362. }
  4363. static void sctp_destruct_sock(struct sock *sk)
  4364. {
  4365. sctp_destruct_common(sk);
  4366. inet_sock_destruct(sk);
  4367. }
  4368. /* API 4.1.7 shutdown() - TCP Style Syntax
  4369. * int shutdown(int socket, int how);
  4370. *
  4371. * sd - the socket descriptor of the association to be closed.
  4372. * how - Specifies the type of shutdown. The values are
  4373. * as follows:
  4374. * SHUT_RD
  4375. * Disables further receive operations. No SCTP
  4376. * protocol action is taken.
  4377. * SHUT_WR
  4378. * Disables further send operations, and initiates
  4379. * the SCTP shutdown sequence.
  4380. * SHUT_RDWR
  4381. * Disables further send and receive operations
  4382. * and initiates the SCTP shutdown sequence.
  4383. */
  4384. static void sctp_shutdown(struct sock *sk, int how)
  4385. {
  4386. struct net *net = sock_net(sk);
  4387. struct sctp_endpoint *ep;
  4388. if (!sctp_style(sk, TCP))
  4389. return;
  4390. ep = sctp_sk(sk)->ep;
  4391. if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
  4392. struct sctp_association *asoc;
  4393. inet_sk_set_state(sk, SCTP_SS_CLOSING);
  4394. asoc = list_entry(ep->asocs.next,
  4395. struct sctp_association, asocs);
  4396. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  4397. }
  4398. }
  4399. int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
  4400. struct sctp_info *info)
  4401. {
  4402. struct sctp_transport *prim;
  4403. struct list_head *pos;
  4404. int mask;
  4405. memset(info, 0, sizeof(*info));
  4406. if (!asoc) {
  4407. struct sctp_sock *sp = sctp_sk(sk);
  4408. info->sctpi_s_autoclose = sp->autoclose;
  4409. info->sctpi_s_adaptation_ind = sp->adaptation_ind;
  4410. info->sctpi_s_pd_point = sp->pd_point;
  4411. info->sctpi_s_nodelay = sp->nodelay;
  4412. info->sctpi_s_disable_fragments = sp->disable_fragments;
  4413. info->sctpi_s_v4mapped = sp->v4mapped;
  4414. info->sctpi_s_frag_interleave = sp->frag_interleave;
  4415. info->sctpi_s_type = sp->type;
  4416. return 0;
  4417. }
  4418. info->sctpi_tag = asoc->c.my_vtag;
  4419. info->sctpi_state = asoc->state;
  4420. info->sctpi_rwnd = asoc->a_rwnd;
  4421. info->sctpi_unackdata = asoc->unack_data;
  4422. info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  4423. info->sctpi_instrms = asoc->stream.incnt;
  4424. info->sctpi_outstrms = asoc->stream.outcnt;
  4425. list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
  4426. info->sctpi_inqueue++;
  4427. list_for_each(pos, &asoc->outqueue.out_chunk_list)
  4428. info->sctpi_outqueue++;
  4429. info->sctpi_overall_error = asoc->overall_error_count;
  4430. info->sctpi_max_burst = asoc->max_burst;
  4431. info->sctpi_maxseg = asoc->frag_point;
  4432. info->sctpi_peer_rwnd = asoc->peer.rwnd;
  4433. info->sctpi_peer_tag = asoc->c.peer_vtag;
  4434. mask = asoc->peer.intl_capable << 1;
  4435. mask = (mask | asoc->peer.ecn_capable) << 1;
  4436. mask = (mask | asoc->peer.ipv4_address) << 1;
  4437. mask = (mask | asoc->peer.ipv6_address) << 1;
  4438. mask = (mask | asoc->peer.reconf_capable) << 1;
  4439. mask = (mask | asoc->peer.asconf_capable) << 1;
  4440. mask = (mask | asoc->peer.prsctp_capable) << 1;
  4441. mask = (mask | asoc->peer.auth_capable);
  4442. info->sctpi_peer_capable = mask;
  4443. mask = asoc->peer.sack_needed << 1;
  4444. mask = (mask | asoc->peer.sack_generation) << 1;
  4445. mask = (mask | asoc->peer.zero_window_announced);
  4446. info->sctpi_peer_sack = mask;
  4447. info->sctpi_isacks = asoc->stats.isacks;
  4448. info->sctpi_osacks = asoc->stats.osacks;
  4449. info->sctpi_opackets = asoc->stats.opackets;
  4450. info->sctpi_ipackets = asoc->stats.ipackets;
  4451. info->sctpi_rtxchunks = asoc->stats.rtxchunks;
  4452. info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
  4453. info->sctpi_idupchunks = asoc->stats.idupchunks;
  4454. info->sctpi_gapcnt = asoc->stats.gapcnt;
  4455. info->sctpi_ouodchunks = asoc->stats.ouodchunks;
  4456. info->sctpi_iuodchunks = asoc->stats.iuodchunks;
  4457. info->sctpi_oodchunks = asoc->stats.oodchunks;
  4458. info->sctpi_iodchunks = asoc->stats.iodchunks;
  4459. info->sctpi_octrlchunks = asoc->stats.octrlchunks;
  4460. info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
  4461. prim = asoc->peer.primary_path;
  4462. memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
  4463. info->sctpi_p_state = prim->state;
  4464. info->sctpi_p_cwnd = prim->cwnd;
  4465. info->sctpi_p_srtt = prim->srtt;
  4466. info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
  4467. info->sctpi_p_hbinterval = prim->hbinterval;
  4468. info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
  4469. info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
  4470. info->sctpi_p_ssthresh = prim->ssthresh;
  4471. info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
  4472. info->sctpi_p_flight_size = prim->flight_size;
  4473. info->sctpi_p_error = prim->error_count;
  4474. return 0;
  4475. }
  4476. EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
  4477. /* use callback to avoid exporting the core structure */
  4478. void sctp_transport_walk_start(struct rhashtable_iter *iter) __acquires(RCU)
  4479. {
  4480. rhltable_walk_enter(&sctp_transport_hashtable, iter);
  4481. rhashtable_walk_start(iter);
  4482. }
  4483. void sctp_transport_walk_stop(struct rhashtable_iter *iter) __releases(RCU)
  4484. {
  4485. rhashtable_walk_stop(iter);
  4486. rhashtable_walk_exit(iter);
  4487. }
  4488. struct sctp_transport *sctp_transport_get_next(struct net *net,
  4489. struct rhashtable_iter *iter)
  4490. {
  4491. struct sctp_transport *t;
  4492. t = rhashtable_walk_next(iter);
  4493. for (; t; t = rhashtable_walk_next(iter)) {
  4494. if (IS_ERR(t)) {
  4495. if (PTR_ERR(t) == -EAGAIN)
  4496. continue;
  4497. break;
  4498. }
  4499. if (!sctp_transport_hold(t))
  4500. continue;
  4501. if (net_eq(t->asoc->base.net, net) &&
  4502. t->asoc->peer.primary_path == t)
  4503. break;
  4504. sctp_transport_put(t);
  4505. }
  4506. return t;
  4507. }
  4508. struct sctp_transport *sctp_transport_get_idx(struct net *net,
  4509. struct rhashtable_iter *iter,
  4510. int pos)
  4511. {
  4512. struct sctp_transport *t;
  4513. if (!pos)
  4514. return SEQ_START_TOKEN;
  4515. while ((t = sctp_transport_get_next(net, iter)) && !IS_ERR(t)) {
  4516. if (!--pos)
  4517. break;
  4518. sctp_transport_put(t);
  4519. }
  4520. return t;
  4521. }
  4522. int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
  4523. void *p) {
  4524. int err = 0;
  4525. int hash = 0;
  4526. struct sctp_endpoint *ep;
  4527. struct sctp_hashbucket *head;
  4528. for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
  4529. hash++, head++) {
  4530. read_lock_bh(&head->lock);
  4531. sctp_for_each_hentry(ep, &head->chain) {
  4532. err = cb(ep, p);
  4533. if (err)
  4534. break;
  4535. }
  4536. read_unlock_bh(&head->lock);
  4537. }
  4538. return err;
  4539. }
  4540. EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
  4541. int sctp_transport_lookup_process(sctp_callback_t cb, struct net *net,
  4542. const union sctp_addr *laddr,
  4543. const union sctp_addr *paddr, void *p, int dif)
  4544. {
  4545. struct sctp_transport *transport;
  4546. struct sctp_endpoint *ep;
  4547. int err = -ENOENT;
  4548. rcu_read_lock();
  4549. transport = sctp_addrs_lookup_transport(net, laddr, paddr, dif, dif);
  4550. if (!transport) {
  4551. rcu_read_unlock();
  4552. return err;
  4553. }
  4554. ep = transport->asoc->ep;
  4555. if (!sctp_endpoint_hold(ep)) { /* asoc can be peeled off */
  4556. sctp_transport_put(transport);
  4557. rcu_read_unlock();
  4558. return err;
  4559. }
  4560. rcu_read_unlock();
  4561. err = cb(ep, transport, p);
  4562. sctp_endpoint_put(ep);
  4563. sctp_transport_put(transport);
  4564. return err;
  4565. }
  4566. EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
  4567. int sctp_transport_traverse_process(sctp_callback_t cb, sctp_callback_t cb_done,
  4568. struct net *net, int *pos, void *p)
  4569. {
  4570. struct rhashtable_iter hti;
  4571. struct sctp_transport *tsp;
  4572. struct sctp_endpoint *ep;
  4573. int ret;
  4574. again:
  4575. ret = 0;
  4576. sctp_transport_walk_start(&hti);
  4577. tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
  4578. for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
  4579. ep = tsp->asoc->ep;
  4580. if (sctp_endpoint_hold(ep)) { /* asoc can be peeled off */
  4581. ret = cb(ep, tsp, p);
  4582. if (ret)
  4583. break;
  4584. sctp_endpoint_put(ep);
  4585. }
  4586. (*pos)++;
  4587. sctp_transport_put(tsp);
  4588. }
  4589. sctp_transport_walk_stop(&hti);
  4590. if (ret) {
  4591. if (cb_done && !cb_done(ep, tsp, p)) {
  4592. (*pos)++;
  4593. sctp_endpoint_put(ep);
  4594. sctp_transport_put(tsp);
  4595. goto again;
  4596. }
  4597. sctp_endpoint_put(ep);
  4598. sctp_transport_put(tsp);
  4599. }
  4600. return ret;
  4601. }
  4602. EXPORT_SYMBOL_GPL(sctp_transport_traverse_process);
  4603. /* 7.2.1 Association Status (SCTP_STATUS)
  4604. * Applications can retrieve current status information about an
  4605. * association, including association state, peer receiver window size,
  4606. * number of unacked data chunks, and number of data chunks pending
  4607. * receipt. This information is read-only.
  4608. */
  4609. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  4610. char __user *optval,
  4611. int __user *optlen)
  4612. {
  4613. struct sctp_status status;
  4614. struct sctp_association *asoc = NULL;
  4615. struct sctp_transport *transport;
  4616. sctp_assoc_t associd;
  4617. int retval = 0;
  4618. if (len < sizeof(status)) {
  4619. retval = -EINVAL;
  4620. goto out;
  4621. }
  4622. len = sizeof(status);
  4623. if (copy_from_user(&status, optval, len)) {
  4624. retval = -EFAULT;
  4625. goto out;
  4626. }
  4627. associd = status.sstat_assoc_id;
  4628. asoc = sctp_id2assoc(sk, associd);
  4629. if (!asoc) {
  4630. retval = -EINVAL;
  4631. goto out;
  4632. }
  4633. transport = asoc->peer.primary_path;
  4634. status.sstat_assoc_id = sctp_assoc2id(asoc);
  4635. status.sstat_state = sctp_assoc_to_state(asoc);
  4636. status.sstat_rwnd = asoc->peer.rwnd;
  4637. status.sstat_unackdata = asoc->unack_data;
  4638. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  4639. status.sstat_instrms = asoc->stream.incnt;
  4640. status.sstat_outstrms = asoc->stream.outcnt;
  4641. status.sstat_fragmentation_point = asoc->frag_point;
  4642. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  4643. memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
  4644. transport->af_specific->sockaddr_len);
  4645. /* Map ipv4 address into v4-mapped-on-v6 address. */
  4646. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
  4647. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  4648. status.sstat_primary.spinfo_state = transport->state;
  4649. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  4650. status.sstat_primary.spinfo_srtt = transport->srtt;
  4651. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  4652. status.sstat_primary.spinfo_mtu = transport->pathmtu;
  4653. if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
  4654. status.sstat_primary.spinfo_state = SCTP_ACTIVE;
  4655. if (put_user(len, optlen)) {
  4656. retval = -EFAULT;
  4657. goto out;
  4658. }
  4659. pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
  4660. __func__, len, status.sstat_state, status.sstat_rwnd,
  4661. status.sstat_assoc_id);
  4662. if (copy_to_user(optval, &status, len)) {
  4663. retval = -EFAULT;
  4664. goto out;
  4665. }
  4666. out:
  4667. return retval;
  4668. }
  4669. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  4670. *
  4671. * Applications can retrieve information about a specific peer address
  4672. * of an association, including its reachability state, congestion
  4673. * window, and retransmission timer values. This information is
  4674. * read-only.
  4675. */
  4676. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  4677. char __user *optval,
  4678. int __user *optlen)
  4679. {
  4680. struct sctp_paddrinfo pinfo;
  4681. struct sctp_transport *transport;
  4682. int retval = 0;
  4683. if (len < sizeof(pinfo)) {
  4684. retval = -EINVAL;
  4685. goto out;
  4686. }
  4687. len = sizeof(pinfo);
  4688. if (copy_from_user(&pinfo, optval, len)) {
  4689. retval = -EFAULT;
  4690. goto out;
  4691. }
  4692. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  4693. pinfo.spinfo_assoc_id);
  4694. if (!transport) {
  4695. retval = -EINVAL;
  4696. goto out;
  4697. }
  4698. if (transport->state == SCTP_PF &&
  4699. transport->asoc->pf_expose == SCTP_PF_EXPOSE_DISABLE) {
  4700. retval = -EACCES;
  4701. goto out;
  4702. }
  4703. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  4704. pinfo.spinfo_state = transport->state;
  4705. pinfo.spinfo_cwnd = transport->cwnd;
  4706. pinfo.spinfo_srtt = transport->srtt;
  4707. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  4708. pinfo.spinfo_mtu = transport->pathmtu;
  4709. if (pinfo.spinfo_state == SCTP_UNKNOWN)
  4710. pinfo.spinfo_state = SCTP_ACTIVE;
  4711. if (put_user(len, optlen)) {
  4712. retval = -EFAULT;
  4713. goto out;
  4714. }
  4715. if (copy_to_user(optval, &pinfo, len)) {
  4716. retval = -EFAULT;
  4717. goto out;
  4718. }
  4719. out:
  4720. return retval;
  4721. }
  4722. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  4723. *
  4724. * This option is a on/off flag. If enabled no SCTP message
  4725. * fragmentation will be performed. Instead if a message being sent
  4726. * exceeds the current PMTU size, the message will NOT be sent and
  4727. * instead a error will be indicated to the user.
  4728. */
  4729. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  4730. char __user *optval, int __user *optlen)
  4731. {
  4732. int val;
  4733. if (len < sizeof(int))
  4734. return -EINVAL;
  4735. len = sizeof(int);
  4736. val = (sctp_sk(sk)->disable_fragments == 1);
  4737. if (put_user(len, optlen))
  4738. return -EFAULT;
  4739. if (copy_to_user(optval, &val, len))
  4740. return -EFAULT;
  4741. return 0;
  4742. }
  4743. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  4744. *
  4745. * This socket option is used to specify various notifications and
  4746. * ancillary data the user wishes to receive.
  4747. */
  4748. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  4749. int __user *optlen)
  4750. {
  4751. struct sctp_event_subscribe subscribe;
  4752. __u8 *sn_type = (__u8 *)&subscribe;
  4753. int i;
  4754. if (len == 0)
  4755. return -EINVAL;
  4756. if (len > sizeof(struct sctp_event_subscribe))
  4757. len = sizeof(struct sctp_event_subscribe);
  4758. if (put_user(len, optlen))
  4759. return -EFAULT;
  4760. for (i = 0; i < len; i++)
  4761. sn_type[i] = sctp_ulpevent_type_enabled(sctp_sk(sk)->subscribe,
  4762. SCTP_SN_TYPE_BASE + i);
  4763. if (copy_to_user(optval, &subscribe, len))
  4764. return -EFAULT;
  4765. return 0;
  4766. }
  4767. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  4768. *
  4769. * This socket option is applicable to the UDP-style socket only. When
  4770. * set it will cause associations that are idle for more than the
  4771. * specified number of seconds to automatically close. An association
  4772. * being idle is defined an association that has NOT sent or received
  4773. * user data. The special value of '0' indicates that no automatic
  4774. * close of any associations should be performed. The option expects an
  4775. * integer defining the number of seconds of idle time before an
  4776. * association is closed.
  4777. */
  4778. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4779. {
  4780. /* Applicable to UDP-style socket only */
  4781. if (sctp_style(sk, TCP))
  4782. return -EOPNOTSUPP;
  4783. if (len < sizeof(int))
  4784. return -EINVAL;
  4785. len = sizeof(int);
  4786. if (put_user(len, optlen))
  4787. return -EFAULT;
  4788. if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
  4789. return -EFAULT;
  4790. return 0;
  4791. }
  4792. /* Helper routine to branch off an association to a new socket. */
  4793. int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
  4794. {
  4795. struct sctp_association *asoc = sctp_id2assoc(sk, id);
  4796. struct sctp_sock *sp = sctp_sk(sk);
  4797. struct socket *sock;
  4798. int err = 0;
  4799. /* Do not peel off from one netns to another one. */
  4800. if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
  4801. return -EINVAL;
  4802. if (!asoc)
  4803. return -EINVAL;
  4804. /* An association cannot be branched off from an already peeled-off
  4805. * socket, nor is this supported for tcp style sockets.
  4806. */
  4807. if (!sctp_style(sk, UDP))
  4808. return -EINVAL;
  4809. /* Create a new socket. */
  4810. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  4811. if (err < 0)
  4812. return err;
  4813. sctp_copy_sock(sock->sk, sk, asoc);
  4814. /* Make peeled-off sockets more like 1-1 accepted sockets.
  4815. * Set the daddr and initialize id to something more random and also
  4816. * copy over any ip options.
  4817. */
  4818. sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sock->sk);
  4819. sp->pf->copy_ip_options(sk, sock->sk);
  4820. /* Populate the fields of the newsk from the oldsk and migrate the
  4821. * asoc to the newsk.
  4822. */
  4823. err = sctp_sock_migrate(sk, sock->sk, asoc,
  4824. SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  4825. if (err) {
  4826. sock_release(sock);
  4827. sock = NULL;
  4828. }
  4829. *sockp = sock;
  4830. return err;
  4831. }
  4832. EXPORT_SYMBOL(sctp_do_peeloff);
  4833. static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
  4834. struct file **newfile, unsigned flags)
  4835. {
  4836. struct socket *newsock;
  4837. int retval;
  4838. retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
  4839. if (retval < 0)
  4840. goto out;
  4841. /* Map the socket to an unused fd that can be returned to the user. */
  4842. retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
  4843. if (retval < 0) {
  4844. sock_release(newsock);
  4845. goto out;
  4846. }
  4847. *newfile = sock_alloc_file(newsock, 0, NULL);
  4848. if (IS_ERR(*newfile)) {
  4849. put_unused_fd(retval);
  4850. retval = PTR_ERR(*newfile);
  4851. *newfile = NULL;
  4852. return retval;
  4853. }
  4854. pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
  4855. retval);
  4856. peeloff->sd = retval;
  4857. if (flags & SOCK_NONBLOCK)
  4858. (*newfile)->f_flags |= O_NONBLOCK;
  4859. out:
  4860. return retval;
  4861. }
  4862. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4863. {
  4864. sctp_peeloff_arg_t peeloff;
  4865. struct file *newfile = NULL;
  4866. int retval = 0;
  4867. if (len < sizeof(sctp_peeloff_arg_t))
  4868. return -EINVAL;
  4869. len = sizeof(sctp_peeloff_arg_t);
  4870. if (copy_from_user(&peeloff, optval, len))
  4871. return -EFAULT;
  4872. retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
  4873. if (retval < 0)
  4874. goto out;
  4875. /* Return the fd mapped to the new socket. */
  4876. if (put_user(len, optlen)) {
  4877. fput(newfile);
  4878. put_unused_fd(retval);
  4879. return -EFAULT;
  4880. }
  4881. if (copy_to_user(optval, &peeloff, len)) {
  4882. fput(newfile);
  4883. put_unused_fd(retval);
  4884. return -EFAULT;
  4885. }
  4886. fd_install(retval, newfile);
  4887. out:
  4888. return retval;
  4889. }
  4890. static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
  4891. char __user *optval, int __user *optlen)
  4892. {
  4893. sctp_peeloff_flags_arg_t peeloff;
  4894. struct file *newfile = NULL;
  4895. int retval = 0;
  4896. if (len < sizeof(sctp_peeloff_flags_arg_t))
  4897. return -EINVAL;
  4898. len = sizeof(sctp_peeloff_flags_arg_t);
  4899. if (copy_from_user(&peeloff, optval, len))
  4900. return -EFAULT;
  4901. retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
  4902. &newfile, peeloff.flags);
  4903. if (retval < 0)
  4904. goto out;
  4905. /* Return the fd mapped to the new socket. */
  4906. if (put_user(len, optlen)) {
  4907. fput(newfile);
  4908. put_unused_fd(retval);
  4909. return -EFAULT;
  4910. }
  4911. if (copy_to_user(optval, &peeloff, len)) {
  4912. fput(newfile);
  4913. put_unused_fd(retval);
  4914. return -EFAULT;
  4915. }
  4916. fd_install(retval, newfile);
  4917. out:
  4918. return retval;
  4919. }
  4920. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  4921. *
  4922. * Applications can enable or disable heartbeats for any peer address of
  4923. * an association, modify an address's heartbeat interval, force a
  4924. * heartbeat to be sent immediately, and adjust the address's maximum
  4925. * number of retransmissions sent before an address is considered
  4926. * unreachable. The following structure is used to access and modify an
  4927. * address's parameters:
  4928. *
  4929. * struct sctp_paddrparams {
  4930. * sctp_assoc_t spp_assoc_id;
  4931. * struct sockaddr_storage spp_address;
  4932. * uint32_t spp_hbinterval;
  4933. * uint16_t spp_pathmaxrxt;
  4934. * uint32_t spp_pathmtu;
  4935. * uint32_t spp_sackdelay;
  4936. * uint32_t spp_flags;
  4937. * };
  4938. *
  4939. * spp_assoc_id - (one-to-many style socket) This is filled in the
  4940. * application, and identifies the association for
  4941. * this query.
  4942. * spp_address - This specifies which address is of interest.
  4943. * spp_hbinterval - This contains the value of the heartbeat interval,
  4944. * in milliseconds. If a value of zero
  4945. * is present in this field then no changes are to
  4946. * be made to this parameter.
  4947. * spp_pathmaxrxt - This contains the maximum number of
  4948. * retransmissions before this address shall be
  4949. * considered unreachable. If a value of zero
  4950. * is present in this field then no changes are to
  4951. * be made to this parameter.
  4952. * spp_pathmtu - When Path MTU discovery is disabled the value
  4953. * specified here will be the "fixed" path mtu.
  4954. * Note that if the spp_address field is empty
  4955. * then all associations on this address will
  4956. * have this fixed path mtu set upon them.
  4957. *
  4958. * spp_sackdelay - When delayed sack is enabled, this value specifies
  4959. * the number of milliseconds that sacks will be delayed
  4960. * for. This value will apply to all addresses of an
  4961. * association if the spp_address field is empty. Note
  4962. * also, that if delayed sack is enabled and this
  4963. * value is set to 0, no change is made to the last
  4964. * recorded delayed sack timer value.
  4965. *
  4966. * spp_flags - These flags are used to control various features
  4967. * on an association. The flag field may contain
  4968. * zero or more of the following options.
  4969. *
  4970. * SPP_HB_ENABLE - Enable heartbeats on the
  4971. * specified address. Note that if the address
  4972. * field is empty all addresses for the association
  4973. * have heartbeats enabled upon them.
  4974. *
  4975. * SPP_HB_DISABLE - Disable heartbeats on the
  4976. * speicifed address. Note that if the address
  4977. * field is empty all addresses for the association
  4978. * will have their heartbeats disabled. Note also
  4979. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  4980. * mutually exclusive, only one of these two should
  4981. * be specified. Enabling both fields will have
  4982. * undetermined results.
  4983. *
  4984. * SPP_HB_DEMAND - Request a user initiated heartbeat
  4985. * to be made immediately.
  4986. *
  4987. * SPP_PMTUD_ENABLE - This field will enable PMTU
  4988. * discovery upon the specified address. Note that
  4989. * if the address feild is empty then all addresses
  4990. * on the association are effected.
  4991. *
  4992. * SPP_PMTUD_DISABLE - This field will disable PMTU
  4993. * discovery upon the specified address. Note that
  4994. * if the address feild is empty then all addresses
  4995. * on the association are effected. Not also that
  4996. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  4997. * exclusive. Enabling both will have undetermined
  4998. * results.
  4999. *
  5000. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  5001. * on delayed sack. The time specified in spp_sackdelay
  5002. * is used to specify the sack delay for this address. Note
  5003. * that if spp_address is empty then all addresses will
  5004. * enable delayed sack and take on the sack delay
  5005. * value specified in spp_sackdelay.
  5006. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  5007. * off delayed sack. If the spp_address field is blank then
  5008. * delayed sack is disabled for the entire association. Note
  5009. * also that this field is mutually exclusive to
  5010. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  5011. * results.
  5012. *
  5013. * SPP_IPV6_FLOWLABEL: Setting this flag enables the
  5014. * setting of the IPV6 flow label value. The value is
  5015. * contained in the spp_ipv6_flowlabel field.
  5016. * Upon retrieval, this flag will be set to indicate that
  5017. * the spp_ipv6_flowlabel field has a valid value returned.
  5018. * If a specific destination address is set (in the
  5019. * spp_address field), then the value returned is that of
  5020. * the address. If just an association is specified (and
  5021. * no address), then the association's default flow label
  5022. * is returned. If neither an association nor a destination
  5023. * is specified, then the socket's default flow label is
  5024. * returned. For non-IPv6 sockets, this flag will be left
  5025. * cleared.
  5026. *
  5027. * SPP_DSCP: Setting this flag enables the setting of the
  5028. * Differentiated Services Code Point (DSCP) value
  5029. * associated with either the association or a specific
  5030. * address. The value is obtained in the spp_dscp field.
  5031. * Upon retrieval, this flag will be set to indicate that
  5032. * the spp_dscp field has a valid value returned. If a
  5033. * specific destination address is set when called (in the
  5034. * spp_address field), then that specific destination
  5035. * address's DSCP value is returned. If just an association
  5036. * is specified, then the association's default DSCP is
  5037. * returned. If neither an association nor a destination is
  5038. * specified, then the socket's default DSCP is returned.
  5039. *
  5040. * spp_ipv6_flowlabel
  5041. * - This field is used in conjunction with the
  5042. * SPP_IPV6_FLOWLABEL flag and contains the IPv6 flow label.
  5043. * The 20 least significant bits are used for the flow
  5044. * label. This setting has precedence over any IPv6-layer
  5045. * setting.
  5046. *
  5047. * spp_dscp - This field is used in conjunction with the SPP_DSCP flag
  5048. * and contains the DSCP. The 6 most significant bits are
  5049. * used for the DSCP. This setting has precedence over any
  5050. * IPv4- or IPv6- layer setting.
  5051. */
  5052. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  5053. char __user *optval, int __user *optlen)
  5054. {
  5055. struct sctp_paddrparams params;
  5056. struct sctp_transport *trans = NULL;
  5057. struct sctp_association *asoc = NULL;
  5058. struct sctp_sock *sp = sctp_sk(sk);
  5059. if (len >= sizeof(params))
  5060. len = sizeof(params);
  5061. else if (len >= ALIGN(offsetof(struct sctp_paddrparams,
  5062. spp_ipv6_flowlabel), 4))
  5063. len = ALIGN(offsetof(struct sctp_paddrparams,
  5064. spp_ipv6_flowlabel), 4);
  5065. else
  5066. return -EINVAL;
  5067. if (copy_from_user(&params, optval, len))
  5068. return -EFAULT;
  5069. /* If an address other than INADDR_ANY is specified, and
  5070. * no transport is found, then the request is invalid.
  5071. */
  5072. if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
  5073. trans = sctp_addr_id2transport(sk, &params.spp_address,
  5074. params.spp_assoc_id);
  5075. if (!trans) {
  5076. pr_debug("%s: failed no transport\n", __func__);
  5077. return -EINVAL;
  5078. }
  5079. }
  5080. /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
  5081. * socket is a one to many style socket, and an association
  5082. * was not found, then the id was invalid.
  5083. */
  5084. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  5085. if (!asoc && params.spp_assoc_id != SCTP_FUTURE_ASSOC &&
  5086. sctp_style(sk, UDP)) {
  5087. pr_debug("%s: failed no association\n", __func__);
  5088. return -EINVAL;
  5089. }
  5090. if (trans) {
  5091. /* Fetch transport values. */
  5092. params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
  5093. params.spp_pathmtu = trans->pathmtu;
  5094. params.spp_pathmaxrxt = trans->pathmaxrxt;
  5095. params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
  5096. /*draft-11 doesn't say what to return in spp_flags*/
  5097. params.spp_flags = trans->param_flags;
  5098. if (trans->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
  5099. params.spp_ipv6_flowlabel = trans->flowlabel &
  5100. SCTP_FLOWLABEL_VAL_MASK;
  5101. params.spp_flags |= SPP_IPV6_FLOWLABEL;
  5102. }
  5103. if (trans->dscp & SCTP_DSCP_SET_MASK) {
  5104. params.spp_dscp = trans->dscp & SCTP_DSCP_VAL_MASK;
  5105. params.spp_flags |= SPP_DSCP;
  5106. }
  5107. } else if (asoc) {
  5108. /* Fetch association values. */
  5109. params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
  5110. params.spp_pathmtu = asoc->pathmtu;
  5111. params.spp_pathmaxrxt = asoc->pathmaxrxt;
  5112. params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
  5113. /*draft-11 doesn't say what to return in spp_flags*/
  5114. params.spp_flags = asoc->param_flags;
  5115. if (asoc->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
  5116. params.spp_ipv6_flowlabel = asoc->flowlabel &
  5117. SCTP_FLOWLABEL_VAL_MASK;
  5118. params.spp_flags |= SPP_IPV6_FLOWLABEL;
  5119. }
  5120. if (asoc->dscp & SCTP_DSCP_SET_MASK) {
  5121. params.spp_dscp = asoc->dscp & SCTP_DSCP_VAL_MASK;
  5122. params.spp_flags |= SPP_DSCP;
  5123. }
  5124. } else {
  5125. /* Fetch socket values. */
  5126. params.spp_hbinterval = sp->hbinterval;
  5127. params.spp_pathmtu = sp->pathmtu;
  5128. params.spp_sackdelay = sp->sackdelay;
  5129. params.spp_pathmaxrxt = sp->pathmaxrxt;
  5130. /*draft-11 doesn't say what to return in spp_flags*/
  5131. params.spp_flags = sp->param_flags;
  5132. if (sp->flowlabel & SCTP_FLOWLABEL_SET_MASK) {
  5133. params.spp_ipv6_flowlabel = sp->flowlabel &
  5134. SCTP_FLOWLABEL_VAL_MASK;
  5135. params.spp_flags |= SPP_IPV6_FLOWLABEL;
  5136. }
  5137. if (sp->dscp & SCTP_DSCP_SET_MASK) {
  5138. params.spp_dscp = sp->dscp & SCTP_DSCP_VAL_MASK;
  5139. params.spp_flags |= SPP_DSCP;
  5140. }
  5141. }
  5142. if (copy_to_user(optval, &params, len))
  5143. return -EFAULT;
  5144. if (put_user(len, optlen))
  5145. return -EFAULT;
  5146. return 0;
  5147. }
  5148. /*
  5149. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  5150. *
  5151. * This option will effect the way delayed acks are performed. This
  5152. * option allows you to get or set the delayed ack time, in
  5153. * milliseconds. It also allows changing the delayed ack frequency.
  5154. * Changing the frequency to 1 disables the delayed sack algorithm. If
  5155. * the assoc_id is 0, then this sets or gets the endpoints default
  5156. * values. If the assoc_id field is non-zero, then the set or get
  5157. * effects the specified association for the one to many model (the
  5158. * assoc_id field is ignored by the one to one model). Note that if
  5159. * sack_delay or sack_freq are 0 when setting this option, then the
  5160. * current values will remain unchanged.
  5161. *
  5162. * struct sctp_sack_info {
  5163. * sctp_assoc_t sack_assoc_id;
  5164. * uint32_t sack_delay;
  5165. * uint32_t sack_freq;
  5166. * };
  5167. *
  5168. * sack_assoc_id - This parameter, indicates which association the user
  5169. * is performing an action upon. Note that if this field's value is
  5170. * zero then the endpoints default value is changed (effecting future
  5171. * associations only).
  5172. *
  5173. * sack_delay - This parameter contains the number of milliseconds that
  5174. * the user is requesting the delayed ACK timer be set to. Note that
  5175. * this value is defined in the standard to be between 200 and 500
  5176. * milliseconds.
  5177. *
  5178. * sack_freq - This parameter contains the number of packets that must
  5179. * be received before a sack is sent without waiting for the delay
  5180. * timer to expire. The default value for this is 2, setting this
  5181. * value to 1 will disable the delayed sack algorithm.
  5182. */
  5183. static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
  5184. char __user *optval,
  5185. int __user *optlen)
  5186. {
  5187. struct sctp_sack_info params;
  5188. struct sctp_association *asoc = NULL;
  5189. struct sctp_sock *sp = sctp_sk(sk);
  5190. if (len >= sizeof(struct sctp_sack_info)) {
  5191. len = sizeof(struct sctp_sack_info);
  5192. if (copy_from_user(&params, optval, len))
  5193. return -EFAULT;
  5194. } else if (len == sizeof(struct sctp_assoc_value)) {
  5195. pr_warn_ratelimited(DEPRECATED
  5196. "%s (pid %d) "
  5197. "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
  5198. "Use struct sctp_sack_info instead\n",
  5199. current->comm, task_pid_nr(current));
  5200. if (copy_from_user(&params, optval, len))
  5201. return -EFAULT;
  5202. } else
  5203. return -EINVAL;
  5204. /* Get association, if sack_assoc_id != SCTP_FUTURE_ASSOC and the
  5205. * socket is a one to many style socket, and an association
  5206. * was not found, then the id was invalid.
  5207. */
  5208. asoc = sctp_id2assoc(sk, params.sack_assoc_id);
  5209. if (!asoc && params.sack_assoc_id != SCTP_FUTURE_ASSOC &&
  5210. sctp_style(sk, UDP))
  5211. return -EINVAL;
  5212. if (asoc) {
  5213. /* Fetch association values. */
  5214. if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
  5215. params.sack_delay = jiffies_to_msecs(asoc->sackdelay);
  5216. params.sack_freq = asoc->sackfreq;
  5217. } else {
  5218. params.sack_delay = 0;
  5219. params.sack_freq = 1;
  5220. }
  5221. } else {
  5222. /* Fetch socket values. */
  5223. if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
  5224. params.sack_delay = sp->sackdelay;
  5225. params.sack_freq = sp->sackfreq;
  5226. } else {
  5227. params.sack_delay = 0;
  5228. params.sack_freq = 1;
  5229. }
  5230. }
  5231. if (copy_to_user(optval, &params, len))
  5232. return -EFAULT;
  5233. if (put_user(len, optlen))
  5234. return -EFAULT;
  5235. return 0;
  5236. }
  5237. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  5238. *
  5239. * Applications can specify protocol parameters for the default association
  5240. * initialization. The option name argument to setsockopt() and getsockopt()
  5241. * is SCTP_INITMSG.
  5242. *
  5243. * Setting initialization parameters is effective only on an unconnected
  5244. * socket (for UDP-style sockets only future associations are effected
  5245. * by the change). With TCP-style sockets, this option is inherited by
  5246. * sockets derived from a listener socket.
  5247. */
  5248. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  5249. {
  5250. if (len < sizeof(struct sctp_initmsg))
  5251. return -EINVAL;
  5252. len = sizeof(struct sctp_initmsg);
  5253. if (put_user(len, optlen))
  5254. return -EFAULT;
  5255. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  5256. return -EFAULT;
  5257. return 0;
  5258. }
  5259. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  5260. char __user *optval, int __user *optlen)
  5261. {
  5262. struct sctp_association *asoc;
  5263. int cnt = 0;
  5264. struct sctp_getaddrs getaddrs;
  5265. struct sctp_transport *from;
  5266. void __user *to;
  5267. union sctp_addr temp;
  5268. struct sctp_sock *sp = sctp_sk(sk);
  5269. int addrlen;
  5270. size_t space_left;
  5271. int bytes_copied;
  5272. if (len < sizeof(struct sctp_getaddrs))
  5273. return -EINVAL;
  5274. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  5275. return -EFAULT;
  5276. /* For UDP-style sockets, id specifies the association to query. */
  5277. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  5278. if (!asoc)
  5279. return -EINVAL;
  5280. to = optval + offsetof(struct sctp_getaddrs, addrs);
  5281. space_left = len - offsetof(struct sctp_getaddrs, addrs);
  5282. list_for_each_entry(from, &asoc->peer.transport_addr_list,
  5283. transports) {
  5284. memcpy(&temp, &from->ipaddr, sizeof(temp));
  5285. addrlen = sctp_get_pf_specific(sk->sk_family)
  5286. ->addr_to_user(sp, &temp);
  5287. if (space_left < addrlen)
  5288. return -ENOMEM;
  5289. if (copy_to_user(to, &temp, addrlen))
  5290. return -EFAULT;
  5291. to += addrlen;
  5292. cnt++;
  5293. space_left -= addrlen;
  5294. }
  5295. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  5296. return -EFAULT;
  5297. bytes_copied = ((char __user *)to) - optval;
  5298. if (put_user(bytes_copied, optlen))
  5299. return -EFAULT;
  5300. return 0;
  5301. }
  5302. static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
  5303. size_t space_left, int *bytes_copied)
  5304. {
  5305. struct sctp_sockaddr_entry *addr;
  5306. union sctp_addr temp;
  5307. int cnt = 0;
  5308. int addrlen;
  5309. struct net *net = sock_net(sk);
  5310. rcu_read_lock();
  5311. list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
  5312. if (!addr->valid)
  5313. continue;
  5314. if ((PF_INET == sk->sk_family) &&
  5315. (AF_INET6 == addr->a.sa.sa_family))
  5316. continue;
  5317. if ((PF_INET6 == sk->sk_family) &&
  5318. inet_v6_ipv6only(sk) &&
  5319. (AF_INET == addr->a.sa.sa_family))
  5320. continue;
  5321. memcpy(&temp, &addr->a, sizeof(temp));
  5322. if (!temp.v4.sin_port)
  5323. temp.v4.sin_port = htons(port);
  5324. addrlen = sctp_get_pf_specific(sk->sk_family)
  5325. ->addr_to_user(sctp_sk(sk), &temp);
  5326. if (space_left < addrlen) {
  5327. cnt = -ENOMEM;
  5328. break;
  5329. }
  5330. memcpy(to, &temp, addrlen);
  5331. to += addrlen;
  5332. cnt++;
  5333. space_left -= addrlen;
  5334. *bytes_copied += addrlen;
  5335. }
  5336. rcu_read_unlock();
  5337. return cnt;
  5338. }
  5339. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  5340. char __user *optval, int __user *optlen)
  5341. {
  5342. struct sctp_bind_addr *bp;
  5343. struct sctp_association *asoc;
  5344. int cnt = 0;
  5345. struct sctp_getaddrs getaddrs;
  5346. struct sctp_sockaddr_entry *addr;
  5347. void __user *to;
  5348. union sctp_addr temp;
  5349. struct sctp_sock *sp = sctp_sk(sk);
  5350. int addrlen;
  5351. int err = 0;
  5352. size_t space_left;
  5353. int bytes_copied = 0;
  5354. void *addrs;
  5355. void *buf;
  5356. if (len < sizeof(struct sctp_getaddrs))
  5357. return -EINVAL;
  5358. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  5359. return -EFAULT;
  5360. /*
  5361. * For UDP-style sockets, id specifies the association to query.
  5362. * If the id field is set to the value '0' then the locally bound
  5363. * addresses are returned without regard to any particular
  5364. * association.
  5365. */
  5366. if (0 == getaddrs.assoc_id) {
  5367. bp = &sctp_sk(sk)->ep->base.bind_addr;
  5368. } else {
  5369. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  5370. if (!asoc)
  5371. return -EINVAL;
  5372. bp = &asoc->base.bind_addr;
  5373. }
  5374. to = optval + offsetof(struct sctp_getaddrs, addrs);
  5375. space_left = len - offsetof(struct sctp_getaddrs, addrs);
  5376. addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
  5377. if (!addrs)
  5378. return -ENOMEM;
  5379. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  5380. * addresses from the global local address list.
  5381. */
  5382. if (sctp_list_single_entry(&bp->address_list)) {
  5383. addr = list_entry(bp->address_list.next,
  5384. struct sctp_sockaddr_entry, list);
  5385. if (sctp_is_any(sk, &addr->a)) {
  5386. cnt = sctp_copy_laddrs(sk, bp->port, addrs,
  5387. space_left, &bytes_copied);
  5388. if (cnt < 0) {
  5389. err = cnt;
  5390. goto out;
  5391. }
  5392. goto copy_getaddrs;
  5393. }
  5394. }
  5395. buf = addrs;
  5396. /* Protection on the bound address list is not needed since
  5397. * in the socket option context we hold a socket lock and
  5398. * thus the bound address list can't change.
  5399. */
  5400. list_for_each_entry(addr, &bp->address_list, list) {
  5401. memcpy(&temp, &addr->a, sizeof(temp));
  5402. addrlen = sctp_get_pf_specific(sk->sk_family)
  5403. ->addr_to_user(sp, &temp);
  5404. if (space_left < addrlen) {
  5405. err = -ENOMEM; /*fixme: right error?*/
  5406. goto out;
  5407. }
  5408. memcpy(buf, &temp, addrlen);
  5409. buf += addrlen;
  5410. bytes_copied += addrlen;
  5411. cnt++;
  5412. space_left -= addrlen;
  5413. }
  5414. copy_getaddrs:
  5415. if (copy_to_user(to, addrs, bytes_copied)) {
  5416. err = -EFAULT;
  5417. goto out;
  5418. }
  5419. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
  5420. err = -EFAULT;
  5421. goto out;
  5422. }
  5423. /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
  5424. * but we can't change it anymore.
  5425. */
  5426. if (put_user(bytes_copied, optlen))
  5427. err = -EFAULT;
  5428. out:
  5429. kfree(addrs);
  5430. return err;
  5431. }
  5432. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  5433. *
  5434. * Requests that the local SCTP stack use the enclosed peer address as
  5435. * the association primary. The enclosed address must be one of the
  5436. * association peer's addresses.
  5437. */
  5438. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  5439. char __user *optval, int __user *optlen)
  5440. {
  5441. struct sctp_prim prim;
  5442. struct sctp_association *asoc;
  5443. struct sctp_sock *sp = sctp_sk(sk);
  5444. if (len < sizeof(struct sctp_prim))
  5445. return -EINVAL;
  5446. len = sizeof(struct sctp_prim);
  5447. if (copy_from_user(&prim, optval, len))
  5448. return -EFAULT;
  5449. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  5450. if (!asoc)
  5451. return -EINVAL;
  5452. if (!asoc->peer.primary_path)
  5453. return -ENOTCONN;
  5454. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  5455. asoc->peer.primary_path->af_specific->sockaddr_len);
  5456. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
  5457. (union sctp_addr *)&prim.ssp_addr);
  5458. if (put_user(len, optlen))
  5459. return -EFAULT;
  5460. if (copy_to_user(optval, &prim, len))
  5461. return -EFAULT;
  5462. return 0;
  5463. }
  5464. /*
  5465. * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
  5466. *
  5467. * Requests that the local endpoint set the specified Adaptation Layer
  5468. * Indication parameter for all future INIT and INIT-ACK exchanges.
  5469. */
  5470. static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
  5471. char __user *optval, int __user *optlen)
  5472. {
  5473. struct sctp_setadaptation adaptation;
  5474. if (len < sizeof(struct sctp_setadaptation))
  5475. return -EINVAL;
  5476. len = sizeof(struct sctp_setadaptation);
  5477. adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
  5478. if (put_user(len, optlen))
  5479. return -EFAULT;
  5480. if (copy_to_user(optval, &adaptation, len))
  5481. return -EFAULT;
  5482. return 0;
  5483. }
  5484. /*
  5485. *
  5486. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  5487. *
  5488. * Applications that wish to use the sendto() system call may wish to
  5489. * specify a default set of parameters that would normally be supplied
  5490. * through the inclusion of ancillary data. This socket option allows
  5491. * such an application to set the default sctp_sndrcvinfo structure.
  5492. * The application that wishes to use this socket option simply passes
  5493. * in to this call the sctp_sndrcvinfo structure defined in Section
  5494. * 5.2.2) The input parameters accepted by this call include
  5495. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  5496. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  5497. * to this call if the caller is using the UDP model.
  5498. *
  5499. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  5500. */
  5501. static int sctp_getsockopt_default_send_param(struct sock *sk,
  5502. int len, char __user *optval,
  5503. int __user *optlen)
  5504. {
  5505. struct sctp_sock *sp = sctp_sk(sk);
  5506. struct sctp_association *asoc;
  5507. struct sctp_sndrcvinfo info;
  5508. if (len < sizeof(info))
  5509. return -EINVAL;
  5510. len = sizeof(info);
  5511. if (copy_from_user(&info, optval, len))
  5512. return -EFAULT;
  5513. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  5514. if (!asoc && info.sinfo_assoc_id != SCTP_FUTURE_ASSOC &&
  5515. sctp_style(sk, UDP))
  5516. return -EINVAL;
  5517. if (asoc) {
  5518. info.sinfo_stream = asoc->default_stream;
  5519. info.sinfo_flags = asoc->default_flags;
  5520. info.sinfo_ppid = asoc->default_ppid;
  5521. info.sinfo_context = asoc->default_context;
  5522. info.sinfo_timetolive = asoc->default_timetolive;
  5523. } else {
  5524. info.sinfo_stream = sp->default_stream;
  5525. info.sinfo_flags = sp->default_flags;
  5526. info.sinfo_ppid = sp->default_ppid;
  5527. info.sinfo_context = sp->default_context;
  5528. info.sinfo_timetolive = sp->default_timetolive;
  5529. }
  5530. if (put_user(len, optlen))
  5531. return -EFAULT;
  5532. if (copy_to_user(optval, &info, len))
  5533. return -EFAULT;
  5534. return 0;
  5535. }
  5536. /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
  5537. * (SCTP_DEFAULT_SNDINFO)
  5538. */
  5539. static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
  5540. char __user *optval,
  5541. int __user *optlen)
  5542. {
  5543. struct sctp_sock *sp = sctp_sk(sk);
  5544. struct sctp_association *asoc;
  5545. struct sctp_sndinfo info;
  5546. if (len < sizeof(info))
  5547. return -EINVAL;
  5548. len = sizeof(info);
  5549. if (copy_from_user(&info, optval, len))
  5550. return -EFAULT;
  5551. asoc = sctp_id2assoc(sk, info.snd_assoc_id);
  5552. if (!asoc && info.snd_assoc_id != SCTP_FUTURE_ASSOC &&
  5553. sctp_style(sk, UDP))
  5554. return -EINVAL;
  5555. if (asoc) {
  5556. info.snd_sid = asoc->default_stream;
  5557. info.snd_flags = asoc->default_flags;
  5558. info.snd_ppid = asoc->default_ppid;
  5559. info.snd_context = asoc->default_context;
  5560. } else {
  5561. info.snd_sid = sp->default_stream;
  5562. info.snd_flags = sp->default_flags;
  5563. info.snd_ppid = sp->default_ppid;
  5564. info.snd_context = sp->default_context;
  5565. }
  5566. if (put_user(len, optlen))
  5567. return -EFAULT;
  5568. if (copy_to_user(optval, &info, len))
  5569. return -EFAULT;
  5570. return 0;
  5571. }
  5572. /*
  5573. *
  5574. * 7.1.5 SCTP_NODELAY
  5575. *
  5576. * Turn on/off any Nagle-like algorithm. This means that packets are
  5577. * generally sent as soon as possible and no unnecessary delays are
  5578. * introduced, at the cost of more packets in the network. Expects an
  5579. * integer boolean flag.
  5580. */
  5581. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  5582. char __user *optval, int __user *optlen)
  5583. {
  5584. int val;
  5585. if (len < sizeof(int))
  5586. return -EINVAL;
  5587. len = sizeof(int);
  5588. val = (sctp_sk(sk)->nodelay == 1);
  5589. if (put_user(len, optlen))
  5590. return -EFAULT;
  5591. if (copy_to_user(optval, &val, len))
  5592. return -EFAULT;
  5593. return 0;
  5594. }
  5595. /*
  5596. *
  5597. * 7.1.1 SCTP_RTOINFO
  5598. *
  5599. * The protocol parameters used to initialize and bound retransmission
  5600. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  5601. * and modify these parameters.
  5602. * All parameters are time values, in milliseconds. A value of 0, when
  5603. * modifying the parameters, indicates that the current value should not
  5604. * be changed.
  5605. *
  5606. */
  5607. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  5608. char __user *optval,
  5609. int __user *optlen) {
  5610. struct sctp_rtoinfo rtoinfo;
  5611. struct sctp_association *asoc;
  5612. if (len < sizeof (struct sctp_rtoinfo))
  5613. return -EINVAL;
  5614. len = sizeof(struct sctp_rtoinfo);
  5615. if (copy_from_user(&rtoinfo, optval, len))
  5616. return -EFAULT;
  5617. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  5618. if (!asoc && rtoinfo.srto_assoc_id != SCTP_FUTURE_ASSOC &&
  5619. sctp_style(sk, UDP))
  5620. return -EINVAL;
  5621. /* Values corresponding to the specific association. */
  5622. if (asoc) {
  5623. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  5624. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  5625. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  5626. } else {
  5627. /* Values corresponding to the endpoint. */
  5628. struct sctp_sock *sp = sctp_sk(sk);
  5629. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  5630. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  5631. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  5632. }
  5633. if (put_user(len, optlen))
  5634. return -EFAULT;
  5635. if (copy_to_user(optval, &rtoinfo, len))
  5636. return -EFAULT;
  5637. return 0;
  5638. }
  5639. /*
  5640. *
  5641. * 7.1.2 SCTP_ASSOCINFO
  5642. *
  5643. * This option is used to tune the maximum retransmission attempts
  5644. * of the association.
  5645. * Returns an error if the new association retransmission value is
  5646. * greater than the sum of the retransmission value of the peer.
  5647. * See [SCTP] for more information.
  5648. *
  5649. */
  5650. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  5651. char __user *optval,
  5652. int __user *optlen)
  5653. {
  5654. struct sctp_assocparams assocparams;
  5655. struct sctp_association *asoc;
  5656. struct list_head *pos;
  5657. int cnt = 0;
  5658. if (len < sizeof (struct sctp_assocparams))
  5659. return -EINVAL;
  5660. len = sizeof(struct sctp_assocparams);
  5661. if (copy_from_user(&assocparams, optval, len))
  5662. return -EFAULT;
  5663. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  5664. if (!asoc && assocparams.sasoc_assoc_id != SCTP_FUTURE_ASSOC &&
  5665. sctp_style(sk, UDP))
  5666. return -EINVAL;
  5667. /* Values correspoinding to the specific association */
  5668. if (asoc) {
  5669. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  5670. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  5671. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  5672. assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
  5673. list_for_each(pos, &asoc->peer.transport_addr_list) {
  5674. cnt++;
  5675. }
  5676. assocparams.sasoc_number_peer_destinations = cnt;
  5677. } else {
  5678. /* Values corresponding to the endpoint */
  5679. struct sctp_sock *sp = sctp_sk(sk);
  5680. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  5681. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  5682. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  5683. assocparams.sasoc_cookie_life =
  5684. sp->assocparams.sasoc_cookie_life;
  5685. assocparams.sasoc_number_peer_destinations =
  5686. sp->assocparams.
  5687. sasoc_number_peer_destinations;
  5688. }
  5689. if (put_user(len, optlen))
  5690. return -EFAULT;
  5691. if (copy_to_user(optval, &assocparams, len))
  5692. return -EFAULT;
  5693. return 0;
  5694. }
  5695. /*
  5696. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  5697. *
  5698. * This socket option is a boolean flag which turns on or off mapped V4
  5699. * addresses. If this option is turned on and the socket is type
  5700. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  5701. * If this option is turned off, then no mapping will be done of V4
  5702. * addresses and a user will receive both PF_INET6 and PF_INET type
  5703. * addresses on the socket.
  5704. */
  5705. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  5706. char __user *optval, int __user *optlen)
  5707. {
  5708. int val;
  5709. struct sctp_sock *sp = sctp_sk(sk);
  5710. if (len < sizeof(int))
  5711. return -EINVAL;
  5712. len = sizeof(int);
  5713. val = sp->v4mapped;
  5714. if (put_user(len, optlen))
  5715. return -EFAULT;
  5716. if (copy_to_user(optval, &val, len))
  5717. return -EFAULT;
  5718. return 0;
  5719. }
  5720. /*
  5721. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  5722. * (chapter and verse is quoted at sctp_setsockopt_context())
  5723. */
  5724. static int sctp_getsockopt_context(struct sock *sk, int len,
  5725. char __user *optval, int __user *optlen)
  5726. {
  5727. struct sctp_assoc_value params;
  5728. struct sctp_association *asoc;
  5729. if (len < sizeof(struct sctp_assoc_value))
  5730. return -EINVAL;
  5731. len = sizeof(struct sctp_assoc_value);
  5732. if (copy_from_user(&params, optval, len))
  5733. return -EFAULT;
  5734. asoc = sctp_id2assoc(sk, params.assoc_id);
  5735. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  5736. sctp_style(sk, UDP))
  5737. return -EINVAL;
  5738. params.assoc_value = asoc ? asoc->default_rcv_context
  5739. : sctp_sk(sk)->default_rcv_context;
  5740. if (put_user(len, optlen))
  5741. return -EFAULT;
  5742. if (copy_to_user(optval, &params, len))
  5743. return -EFAULT;
  5744. return 0;
  5745. }
  5746. /*
  5747. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  5748. * This option will get or set the maximum size to put in any outgoing
  5749. * SCTP DATA chunk. If a message is larger than this size it will be
  5750. * fragmented by SCTP into the specified size. Note that the underlying
  5751. * SCTP implementation may fragment into smaller sized chunks when the
  5752. * PMTU of the underlying association is smaller than the value set by
  5753. * the user. The default value for this option is '0' which indicates
  5754. * the user is NOT limiting fragmentation and only the PMTU will effect
  5755. * SCTP's choice of DATA chunk size. Note also that values set larger
  5756. * than the maximum size of an IP datagram will effectively let SCTP
  5757. * control fragmentation (i.e. the same as setting this option to 0).
  5758. *
  5759. * The following structure is used to access and modify this parameter:
  5760. *
  5761. * struct sctp_assoc_value {
  5762. * sctp_assoc_t assoc_id;
  5763. * uint32_t assoc_value;
  5764. * };
  5765. *
  5766. * assoc_id: This parameter is ignored for one-to-one style sockets.
  5767. * For one-to-many style sockets this parameter indicates which
  5768. * association the user is performing an action upon. Note that if
  5769. * this field's value is zero then the endpoints default value is
  5770. * changed (effecting future associations only).
  5771. * assoc_value: This parameter specifies the maximum size in bytes.
  5772. */
  5773. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  5774. char __user *optval, int __user *optlen)
  5775. {
  5776. struct sctp_assoc_value params;
  5777. struct sctp_association *asoc;
  5778. if (len == sizeof(int)) {
  5779. pr_warn_ratelimited(DEPRECATED
  5780. "%s (pid %d) "
  5781. "Use of int in maxseg socket option.\n"
  5782. "Use struct sctp_assoc_value instead\n",
  5783. current->comm, task_pid_nr(current));
  5784. params.assoc_id = SCTP_FUTURE_ASSOC;
  5785. } else if (len >= sizeof(struct sctp_assoc_value)) {
  5786. len = sizeof(struct sctp_assoc_value);
  5787. if (copy_from_user(&params, optval, len))
  5788. return -EFAULT;
  5789. } else
  5790. return -EINVAL;
  5791. asoc = sctp_id2assoc(sk, params.assoc_id);
  5792. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  5793. sctp_style(sk, UDP))
  5794. return -EINVAL;
  5795. if (asoc)
  5796. params.assoc_value = asoc->frag_point;
  5797. else
  5798. params.assoc_value = sctp_sk(sk)->user_frag;
  5799. if (put_user(len, optlen))
  5800. return -EFAULT;
  5801. if (len == sizeof(int)) {
  5802. if (copy_to_user(optval, &params.assoc_value, len))
  5803. return -EFAULT;
  5804. } else {
  5805. if (copy_to_user(optval, &params, len))
  5806. return -EFAULT;
  5807. }
  5808. return 0;
  5809. }
  5810. /*
  5811. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  5812. * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
  5813. */
  5814. static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
  5815. char __user *optval, int __user *optlen)
  5816. {
  5817. int val;
  5818. if (len < sizeof(int))
  5819. return -EINVAL;
  5820. len = sizeof(int);
  5821. val = sctp_sk(sk)->frag_interleave;
  5822. if (put_user(len, optlen))
  5823. return -EFAULT;
  5824. if (copy_to_user(optval, &val, len))
  5825. return -EFAULT;
  5826. return 0;
  5827. }
  5828. /*
  5829. * 7.1.25. Set or Get the sctp partial delivery point
  5830. * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
  5831. */
  5832. static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
  5833. char __user *optval,
  5834. int __user *optlen)
  5835. {
  5836. u32 val;
  5837. if (len < sizeof(u32))
  5838. return -EINVAL;
  5839. len = sizeof(u32);
  5840. val = sctp_sk(sk)->pd_point;
  5841. if (put_user(len, optlen))
  5842. return -EFAULT;
  5843. if (copy_to_user(optval, &val, len))
  5844. return -EFAULT;
  5845. return 0;
  5846. }
  5847. /*
  5848. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  5849. * (chapter and verse is quoted at sctp_setsockopt_maxburst())
  5850. */
  5851. static int sctp_getsockopt_maxburst(struct sock *sk, int len,
  5852. char __user *optval,
  5853. int __user *optlen)
  5854. {
  5855. struct sctp_assoc_value params;
  5856. struct sctp_association *asoc;
  5857. if (len == sizeof(int)) {
  5858. pr_warn_ratelimited(DEPRECATED
  5859. "%s (pid %d) "
  5860. "Use of int in max_burst socket option.\n"
  5861. "Use struct sctp_assoc_value instead\n",
  5862. current->comm, task_pid_nr(current));
  5863. params.assoc_id = SCTP_FUTURE_ASSOC;
  5864. } else if (len >= sizeof(struct sctp_assoc_value)) {
  5865. len = sizeof(struct sctp_assoc_value);
  5866. if (copy_from_user(&params, optval, len))
  5867. return -EFAULT;
  5868. } else
  5869. return -EINVAL;
  5870. asoc = sctp_id2assoc(sk, params.assoc_id);
  5871. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  5872. sctp_style(sk, UDP))
  5873. return -EINVAL;
  5874. params.assoc_value = asoc ? asoc->max_burst : sctp_sk(sk)->max_burst;
  5875. if (len == sizeof(int)) {
  5876. if (copy_to_user(optval, &params.assoc_value, len))
  5877. return -EFAULT;
  5878. } else {
  5879. if (copy_to_user(optval, &params, len))
  5880. return -EFAULT;
  5881. }
  5882. return 0;
  5883. }
  5884. static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
  5885. char __user *optval, int __user *optlen)
  5886. {
  5887. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5888. struct sctp_hmacalgo __user *p = (void __user *)optval;
  5889. struct sctp_hmac_algo_param *hmacs;
  5890. __u16 data_len = 0;
  5891. u32 num_idents;
  5892. int i;
  5893. if (!ep->auth_enable)
  5894. return -EACCES;
  5895. hmacs = ep->auth_hmacs_list;
  5896. data_len = ntohs(hmacs->param_hdr.length) -
  5897. sizeof(struct sctp_paramhdr);
  5898. if (len < sizeof(struct sctp_hmacalgo) + data_len)
  5899. return -EINVAL;
  5900. len = sizeof(struct sctp_hmacalgo) + data_len;
  5901. num_idents = data_len / sizeof(u16);
  5902. if (put_user(len, optlen))
  5903. return -EFAULT;
  5904. if (put_user(num_idents, &p->shmac_num_idents))
  5905. return -EFAULT;
  5906. for (i = 0; i < num_idents; i++) {
  5907. __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
  5908. if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
  5909. return -EFAULT;
  5910. }
  5911. return 0;
  5912. }
  5913. static int sctp_getsockopt_active_key(struct sock *sk, int len,
  5914. char __user *optval, int __user *optlen)
  5915. {
  5916. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5917. struct sctp_authkeyid val;
  5918. struct sctp_association *asoc;
  5919. if (len < sizeof(struct sctp_authkeyid))
  5920. return -EINVAL;
  5921. len = sizeof(struct sctp_authkeyid);
  5922. if (copy_from_user(&val, optval, len))
  5923. return -EFAULT;
  5924. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  5925. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  5926. return -EINVAL;
  5927. if (asoc) {
  5928. if (!asoc->peer.auth_capable)
  5929. return -EACCES;
  5930. val.scact_keynumber = asoc->active_key_id;
  5931. } else {
  5932. if (!ep->auth_enable)
  5933. return -EACCES;
  5934. val.scact_keynumber = ep->active_key_id;
  5935. }
  5936. if (put_user(len, optlen))
  5937. return -EFAULT;
  5938. if (copy_to_user(optval, &val, len))
  5939. return -EFAULT;
  5940. return 0;
  5941. }
  5942. static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
  5943. char __user *optval, int __user *optlen)
  5944. {
  5945. struct sctp_authchunks __user *p = (void __user *)optval;
  5946. struct sctp_authchunks val;
  5947. struct sctp_association *asoc;
  5948. struct sctp_chunks_param *ch;
  5949. u32 num_chunks = 0;
  5950. char __user *to;
  5951. if (len < sizeof(struct sctp_authchunks))
  5952. return -EINVAL;
  5953. if (copy_from_user(&val, optval, sizeof(val)))
  5954. return -EFAULT;
  5955. to = p->gauth_chunks;
  5956. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  5957. if (!asoc)
  5958. return -EINVAL;
  5959. if (!asoc->peer.auth_capable)
  5960. return -EACCES;
  5961. ch = asoc->peer.peer_chunks;
  5962. if (!ch)
  5963. goto num;
  5964. /* See if the user provided enough room for all the data */
  5965. num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
  5966. if (len < num_chunks)
  5967. return -EINVAL;
  5968. if (copy_to_user(to, ch->chunks, num_chunks))
  5969. return -EFAULT;
  5970. num:
  5971. len = sizeof(struct sctp_authchunks) + num_chunks;
  5972. if (put_user(len, optlen))
  5973. return -EFAULT;
  5974. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  5975. return -EFAULT;
  5976. return 0;
  5977. }
  5978. static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
  5979. char __user *optval, int __user *optlen)
  5980. {
  5981. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5982. struct sctp_authchunks __user *p = (void __user *)optval;
  5983. struct sctp_authchunks val;
  5984. struct sctp_association *asoc;
  5985. struct sctp_chunks_param *ch;
  5986. u32 num_chunks = 0;
  5987. char __user *to;
  5988. if (len < sizeof(struct sctp_authchunks))
  5989. return -EINVAL;
  5990. if (copy_from_user(&val, optval, sizeof(val)))
  5991. return -EFAULT;
  5992. to = p->gauth_chunks;
  5993. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  5994. if (!asoc && val.gauth_assoc_id != SCTP_FUTURE_ASSOC &&
  5995. sctp_style(sk, UDP))
  5996. return -EINVAL;
  5997. if (asoc) {
  5998. if (!asoc->peer.auth_capable)
  5999. return -EACCES;
  6000. ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
  6001. } else {
  6002. if (!ep->auth_enable)
  6003. return -EACCES;
  6004. ch = ep->auth_chunk_list;
  6005. }
  6006. if (!ch)
  6007. goto num;
  6008. num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
  6009. if (len < sizeof(struct sctp_authchunks) + num_chunks)
  6010. return -EINVAL;
  6011. if (copy_to_user(to, ch->chunks, num_chunks))
  6012. return -EFAULT;
  6013. num:
  6014. len = sizeof(struct sctp_authchunks) + num_chunks;
  6015. if (put_user(len, optlen))
  6016. return -EFAULT;
  6017. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  6018. return -EFAULT;
  6019. return 0;
  6020. }
  6021. /*
  6022. * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
  6023. * This option gets the current number of associations that are attached
  6024. * to a one-to-many style socket. The option value is an uint32_t.
  6025. */
  6026. static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
  6027. char __user *optval, int __user *optlen)
  6028. {
  6029. struct sctp_sock *sp = sctp_sk(sk);
  6030. struct sctp_association *asoc;
  6031. u32 val = 0;
  6032. if (sctp_style(sk, TCP))
  6033. return -EOPNOTSUPP;
  6034. if (len < sizeof(u32))
  6035. return -EINVAL;
  6036. len = sizeof(u32);
  6037. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  6038. val++;
  6039. }
  6040. if (put_user(len, optlen))
  6041. return -EFAULT;
  6042. if (copy_to_user(optval, &val, len))
  6043. return -EFAULT;
  6044. return 0;
  6045. }
  6046. /*
  6047. * 8.1.23 SCTP_AUTO_ASCONF
  6048. * See the corresponding setsockopt entry as description
  6049. */
  6050. static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
  6051. char __user *optval, int __user *optlen)
  6052. {
  6053. int val = 0;
  6054. if (len < sizeof(int))
  6055. return -EINVAL;
  6056. len = sizeof(int);
  6057. if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
  6058. val = 1;
  6059. if (put_user(len, optlen))
  6060. return -EFAULT;
  6061. if (copy_to_user(optval, &val, len))
  6062. return -EFAULT;
  6063. return 0;
  6064. }
  6065. /*
  6066. * 8.2.6. Get the Current Identifiers of Associations
  6067. * (SCTP_GET_ASSOC_ID_LIST)
  6068. *
  6069. * This option gets the current list of SCTP association identifiers of
  6070. * the SCTP associations handled by a one-to-many style socket.
  6071. */
  6072. static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
  6073. char __user *optval, int __user *optlen)
  6074. {
  6075. struct sctp_sock *sp = sctp_sk(sk);
  6076. struct sctp_association *asoc;
  6077. struct sctp_assoc_ids *ids;
  6078. size_t ids_size;
  6079. u32 num = 0;
  6080. if (sctp_style(sk, TCP))
  6081. return -EOPNOTSUPP;
  6082. if (len < sizeof(struct sctp_assoc_ids))
  6083. return -EINVAL;
  6084. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  6085. num++;
  6086. }
  6087. ids_size = struct_size(ids, gaids_assoc_id, num);
  6088. if (len < ids_size)
  6089. return -EINVAL;
  6090. len = ids_size;
  6091. ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
  6092. if (unlikely(!ids))
  6093. return -ENOMEM;
  6094. ids->gaids_number_of_ids = num;
  6095. num = 0;
  6096. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  6097. ids->gaids_assoc_id[num++] = asoc->assoc_id;
  6098. }
  6099. if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
  6100. kfree(ids);
  6101. return -EFAULT;
  6102. }
  6103. kfree(ids);
  6104. return 0;
  6105. }
  6106. /*
  6107. * SCTP_PEER_ADDR_THLDS
  6108. *
  6109. * This option allows us to fetch the partially failed threshold for one or all
  6110. * transports in an association. See Section 6.1 of:
  6111. * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
  6112. */
  6113. static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
  6114. char __user *optval, int len,
  6115. int __user *optlen, bool v2)
  6116. {
  6117. struct sctp_paddrthlds_v2 val;
  6118. struct sctp_transport *trans;
  6119. struct sctp_association *asoc;
  6120. int min;
  6121. min = v2 ? sizeof(val) : sizeof(struct sctp_paddrthlds);
  6122. if (len < min)
  6123. return -EINVAL;
  6124. len = min;
  6125. if (copy_from_user(&val, optval, len))
  6126. return -EFAULT;
  6127. if (!sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
  6128. trans = sctp_addr_id2transport(sk, &val.spt_address,
  6129. val.spt_assoc_id);
  6130. if (!trans)
  6131. return -ENOENT;
  6132. val.spt_pathmaxrxt = trans->pathmaxrxt;
  6133. val.spt_pathpfthld = trans->pf_retrans;
  6134. val.spt_pathcpthld = trans->ps_retrans;
  6135. goto out;
  6136. }
  6137. asoc = sctp_id2assoc(sk, val.spt_assoc_id);
  6138. if (!asoc && val.spt_assoc_id != SCTP_FUTURE_ASSOC &&
  6139. sctp_style(sk, UDP))
  6140. return -EINVAL;
  6141. if (asoc) {
  6142. val.spt_pathpfthld = asoc->pf_retrans;
  6143. val.spt_pathmaxrxt = asoc->pathmaxrxt;
  6144. val.spt_pathcpthld = asoc->ps_retrans;
  6145. } else {
  6146. struct sctp_sock *sp = sctp_sk(sk);
  6147. val.spt_pathpfthld = sp->pf_retrans;
  6148. val.spt_pathmaxrxt = sp->pathmaxrxt;
  6149. val.spt_pathcpthld = sp->ps_retrans;
  6150. }
  6151. out:
  6152. if (put_user(len, optlen) || copy_to_user(optval, &val, len))
  6153. return -EFAULT;
  6154. return 0;
  6155. }
  6156. /*
  6157. * SCTP_GET_ASSOC_STATS
  6158. *
  6159. * This option retrieves local per endpoint statistics. It is modeled
  6160. * after OpenSolaris' implementation
  6161. */
  6162. static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
  6163. char __user *optval,
  6164. int __user *optlen)
  6165. {
  6166. struct sctp_assoc_stats sas;
  6167. struct sctp_association *asoc = NULL;
  6168. /* User must provide at least the assoc id */
  6169. if (len < sizeof(sctp_assoc_t))
  6170. return -EINVAL;
  6171. /* Allow the struct to grow and fill in as much as possible */
  6172. len = min_t(size_t, len, sizeof(sas));
  6173. if (copy_from_user(&sas, optval, len))
  6174. return -EFAULT;
  6175. asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
  6176. if (!asoc)
  6177. return -EINVAL;
  6178. sas.sas_rtxchunks = asoc->stats.rtxchunks;
  6179. sas.sas_gapcnt = asoc->stats.gapcnt;
  6180. sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
  6181. sas.sas_osacks = asoc->stats.osacks;
  6182. sas.sas_isacks = asoc->stats.isacks;
  6183. sas.sas_octrlchunks = asoc->stats.octrlchunks;
  6184. sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
  6185. sas.sas_oodchunks = asoc->stats.oodchunks;
  6186. sas.sas_iodchunks = asoc->stats.iodchunks;
  6187. sas.sas_ouodchunks = asoc->stats.ouodchunks;
  6188. sas.sas_iuodchunks = asoc->stats.iuodchunks;
  6189. sas.sas_idupchunks = asoc->stats.idupchunks;
  6190. sas.sas_opackets = asoc->stats.opackets;
  6191. sas.sas_ipackets = asoc->stats.ipackets;
  6192. /* New high max rto observed, will return 0 if not a single
  6193. * RTO update took place. obs_rto_ipaddr will be bogus
  6194. * in such a case
  6195. */
  6196. sas.sas_maxrto = asoc->stats.max_obs_rto;
  6197. memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
  6198. sizeof(struct sockaddr_storage));
  6199. /* Mark beginning of a new observation period */
  6200. asoc->stats.max_obs_rto = asoc->rto_min;
  6201. if (put_user(len, optlen))
  6202. return -EFAULT;
  6203. pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
  6204. if (copy_to_user(optval, &sas, len))
  6205. return -EFAULT;
  6206. return 0;
  6207. }
  6208. static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
  6209. char __user *optval,
  6210. int __user *optlen)
  6211. {
  6212. int val = 0;
  6213. if (len < sizeof(int))
  6214. return -EINVAL;
  6215. len = sizeof(int);
  6216. if (sctp_sk(sk)->recvrcvinfo)
  6217. val = 1;
  6218. if (put_user(len, optlen))
  6219. return -EFAULT;
  6220. if (copy_to_user(optval, &val, len))
  6221. return -EFAULT;
  6222. return 0;
  6223. }
  6224. static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
  6225. char __user *optval,
  6226. int __user *optlen)
  6227. {
  6228. int val = 0;
  6229. if (len < sizeof(int))
  6230. return -EINVAL;
  6231. len = sizeof(int);
  6232. if (sctp_sk(sk)->recvnxtinfo)
  6233. val = 1;
  6234. if (put_user(len, optlen))
  6235. return -EFAULT;
  6236. if (copy_to_user(optval, &val, len))
  6237. return -EFAULT;
  6238. return 0;
  6239. }
  6240. static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
  6241. char __user *optval,
  6242. int __user *optlen)
  6243. {
  6244. struct sctp_assoc_value params;
  6245. struct sctp_association *asoc;
  6246. int retval = -EFAULT;
  6247. if (len < sizeof(params)) {
  6248. retval = -EINVAL;
  6249. goto out;
  6250. }
  6251. len = sizeof(params);
  6252. if (copy_from_user(&params, optval, len))
  6253. goto out;
  6254. asoc = sctp_id2assoc(sk, params.assoc_id);
  6255. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6256. sctp_style(sk, UDP)) {
  6257. retval = -EINVAL;
  6258. goto out;
  6259. }
  6260. params.assoc_value = asoc ? asoc->peer.prsctp_capable
  6261. : sctp_sk(sk)->ep->prsctp_enable;
  6262. if (put_user(len, optlen))
  6263. goto out;
  6264. if (copy_to_user(optval, &params, len))
  6265. goto out;
  6266. retval = 0;
  6267. out:
  6268. return retval;
  6269. }
  6270. static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
  6271. char __user *optval,
  6272. int __user *optlen)
  6273. {
  6274. struct sctp_default_prinfo info;
  6275. struct sctp_association *asoc;
  6276. int retval = -EFAULT;
  6277. if (len < sizeof(info)) {
  6278. retval = -EINVAL;
  6279. goto out;
  6280. }
  6281. len = sizeof(info);
  6282. if (copy_from_user(&info, optval, len))
  6283. goto out;
  6284. asoc = sctp_id2assoc(sk, info.pr_assoc_id);
  6285. if (!asoc && info.pr_assoc_id != SCTP_FUTURE_ASSOC &&
  6286. sctp_style(sk, UDP)) {
  6287. retval = -EINVAL;
  6288. goto out;
  6289. }
  6290. if (asoc) {
  6291. info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
  6292. info.pr_value = asoc->default_timetolive;
  6293. } else {
  6294. struct sctp_sock *sp = sctp_sk(sk);
  6295. info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
  6296. info.pr_value = sp->default_timetolive;
  6297. }
  6298. if (put_user(len, optlen))
  6299. goto out;
  6300. if (copy_to_user(optval, &info, len))
  6301. goto out;
  6302. retval = 0;
  6303. out:
  6304. return retval;
  6305. }
  6306. static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
  6307. char __user *optval,
  6308. int __user *optlen)
  6309. {
  6310. struct sctp_prstatus params;
  6311. struct sctp_association *asoc;
  6312. int policy;
  6313. int retval = -EINVAL;
  6314. if (len < sizeof(params))
  6315. goto out;
  6316. len = sizeof(params);
  6317. if (copy_from_user(&params, optval, len)) {
  6318. retval = -EFAULT;
  6319. goto out;
  6320. }
  6321. policy = params.sprstat_policy;
  6322. if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
  6323. ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
  6324. goto out;
  6325. asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
  6326. if (!asoc)
  6327. goto out;
  6328. if (policy == SCTP_PR_SCTP_ALL) {
  6329. params.sprstat_abandoned_unsent = 0;
  6330. params.sprstat_abandoned_sent = 0;
  6331. for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
  6332. params.sprstat_abandoned_unsent +=
  6333. asoc->abandoned_unsent[policy];
  6334. params.sprstat_abandoned_sent +=
  6335. asoc->abandoned_sent[policy];
  6336. }
  6337. } else {
  6338. params.sprstat_abandoned_unsent =
  6339. asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
  6340. params.sprstat_abandoned_sent =
  6341. asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
  6342. }
  6343. if (put_user(len, optlen)) {
  6344. retval = -EFAULT;
  6345. goto out;
  6346. }
  6347. if (copy_to_user(optval, &params, len)) {
  6348. retval = -EFAULT;
  6349. goto out;
  6350. }
  6351. retval = 0;
  6352. out:
  6353. return retval;
  6354. }
  6355. static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
  6356. char __user *optval,
  6357. int __user *optlen)
  6358. {
  6359. struct sctp_stream_out_ext *streamoute;
  6360. struct sctp_association *asoc;
  6361. struct sctp_prstatus params;
  6362. int retval = -EINVAL;
  6363. int policy;
  6364. if (len < sizeof(params))
  6365. goto out;
  6366. len = sizeof(params);
  6367. if (copy_from_user(&params, optval, len)) {
  6368. retval = -EFAULT;
  6369. goto out;
  6370. }
  6371. policy = params.sprstat_policy;
  6372. if (!policy || (policy & ~(SCTP_PR_SCTP_MASK | SCTP_PR_SCTP_ALL)) ||
  6373. ((policy & SCTP_PR_SCTP_ALL) && (policy & SCTP_PR_SCTP_MASK)))
  6374. goto out;
  6375. asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
  6376. if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
  6377. goto out;
  6378. streamoute = SCTP_SO(&asoc->stream, params.sprstat_sid)->ext;
  6379. if (!streamoute) {
  6380. /* Not allocated yet, means all stats are 0 */
  6381. params.sprstat_abandoned_unsent = 0;
  6382. params.sprstat_abandoned_sent = 0;
  6383. retval = 0;
  6384. goto out;
  6385. }
  6386. if (policy == SCTP_PR_SCTP_ALL) {
  6387. params.sprstat_abandoned_unsent = 0;
  6388. params.sprstat_abandoned_sent = 0;
  6389. for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
  6390. params.sprstat_abandoned_unsent +=
  6391. streamoute->abandoned_unsent[policy];
  6392. params.sprstat_abandoned_sent +=
  6393. streamoute->abandoned_sent[policy];
  6394. }
  6395. } else {
  6396. params.sprstat_abandoned_unsent =
  6397. streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
  6398. params.sprstat_abandoned_sent =
  6399. streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
  6400. }
  6401. if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
  6402. retval = -EFAULT;
  6403. goto out;
  6404. }
  6405. retval = 0;
  6406. out:
  6407. return retval;
  6408. }
  6409. static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
  6410. char __user *optval,
  6411. int __user *optlen)
  6412. {
  6413. struct sctp_assoc_value params;
  6414. struct sctp_association *asoc;
  6415. int retval = -EFAULT;
  6416. if (len < sizeof(params)) {
  6417. retval = -EINVAL;
  6418. goto out;
  6419. }
  6420. len = sizeof(params);
  6421. if (copy_from_user(&params, optval, len))
  6422. goto out;
  6423. asoc = sctp_id2assoc(sk, params.assoc_id);
  6424. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6425. sctp_style(sk, UDP)) {
  6426. retval = -EINVAL;
  6427. goto out;
  6428. }
  6429. params.assoc_value = asoc ? asoc->peer.reconf_capable
  6430. : sctp_sk(sk)->ep->reconf_enable;
  6431. if (put_user(len, optlen))
  6432. goto out;
  6433. if (copy_to_user(optval, &params, len))
  6434. goto out;
  6435. retval = 0;
  6436. out:
  6437. return retval;
  6438. }
  6439. static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
  6440. char __user *optval,
  6441. int __user *optlen)
  6442. {
  6443. struct sctp_assoc_value params;
  6444. struct sctp_association *asoc;
  6445. int retval = -EFAULT;
  6446. if (len < sizeof(params)) {
  6447. retval = -EINVAL;
  6448. goto out;
  6449. }
  6450. len = sizeof(params);
  6451. if (copy_from_user(&params, optval, len))
  6452. goto out;
  6453. asoc = sctp_id2assoc(sk, params.assoc_id);
  6454. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6455. sctp_style(sk, UDP)) {
  6456. retval = -EINVAL;
  6457. goto out;
  6458. }
  6459. params.assoc_value = asoc ? asoc->strreset_enable
  6460. : sctp_sk(sk)->ep->strreset_enable;
  6461. if (put_user(len, optlen))
  6462. goto out;
  6463. if (copy_to_user(optval, &params, len))
  6464. goto out;
  6465. retval = 0;
  6466. out:
  6467. return retval;
  6468. }
  6469. static int sctp_getsockopt_scheduler(struct sock *sk, int len,
  6470. char __user *optval,
  6471. int __user *optlen)
  6472. {
  6473. struct sctp_assoc_value params;
  6474. struct sctp_association *asoc;
  6475. int retval = -EFAULT;
  6476. if (len < sizeof(params)) {
  6477. retval = -EINVAL;
  6478. goto out;
  6479. }
  6480. len = sizeof(params);
  6481. if (copy_from_user(&params, optval, len))
  6482. goto out;
  6483. asoc = sctp_id2assoc(sk, params.assoc_id);
  6484. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6485. sctp_style(sk, UDP)) {
  6486. retval = -EINVAL;
  6487. goto out;
  6488. }
  6489. params.assoc_value = asoc ? sctp_sched_get_sched(asoc)
  6490. : sctp_sk(sk)->default_ss;
  6491. if (put_user(len, optlen))
  6492. goto out;
  6493. if (copy_to_user(optval, &params, len))
  6494. goto out;
  6495. retval = 0;
  6496. out:
  6497. return retval;
  6498. }
  6499. static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
  6500. char __user *optval,
  6501. int __user *optlen)
  6502. {
  6503. struct sctp_stream_value params;
  6504. struct sctp_association *asoc;
  6505. int retval = -EFAULT;
  6506. if (len < sizeof(params)) {
  6507. retval = -EINVAL;
  6508. goto out;
  6509. }
  6510. len = sizeof(params);
  6511. if (copy_from_user(&params, optval, len))
  6512. goto out;
  6513. asoc = sctp_id2assoc(sk, params.assoc_id);
  6514. if (!asoc) {
  6515. retval = -EINVAL;
  6516. goto out;
  6517. }
  6518. retval = sctp_sched_get_value(asoc, params.stream_id,
  6519. &params.stream_value);
  6520. if (retval)
  6521. goto out;
  6522. if (put_user(len, optlen)) {
  6523. retval = -EFAULT;
  6524. goto out;
  6525. }
  6526. if (copy_to_user(optval, &params, len)) {
  6527. retval = -EFAULT;
  6528. goto out;
  6529. }
  6530. out:
  6531. return retval;
  6532. }
  6533. static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
  6534. char __user *optval,
  6535. int __user *optlen)
  6536. {
  6537. struct sctp_assoc_value params;
  6538. struct sctp_association *asoc;
  6539. int retval = -EFAULT;
  6540. if (len < sizeof(params)) {
  6541. retval = -EINVAL;
  6542. goto out;
  6543. }
  6544. len = sizeof(params);
  6545. if (copy_from_user(&params, optval, len))
  6546. goto out;
  6547. asoc = sctp_id2assoc(sk, params.assoc_id);
  6548. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6549. sctp_style(sk, UDP)) {
  6550. retval = -EINVAL;
  6551. goto out;
  6552. }
  6553. params.assoc_value = asoc ? asoc->peer.intl_capable
  6554. : sctp_sk(sk)->ep->intl_enable;
  6555. if (put_user(len, optlen))
  6556. goto out;
  6557. if (copy_to_user(optval, &params, len))
  6558. goto out;
  6559. retval = 0;
  6560. out:
  6561. return retval;
  6562. }
  6563. static int sctp_getsockopt_reuse_port(struct sock *sk, int len,
  6564. char __user *optval,
  6565. int __user *optlen)
  6566. {
  6567. int val;
  6568. if (len < sizeof(int))
  6569. return -EINVAL;
  6570. len = sizeof(int);
  6571. val = sctp_sk(sk)->reuse;
  6572. if (put_user(len, optlen))
  6573. return -EFAULT;
  6574. if (copy_to_user(optval, &val, len))
  6575. return -EFAULT;
  6576. return 0;
  6577. }
  6578. static int sctp_getsockopt_event(struct sock *sk, int len, char __user *optval,
  6579. int __user *optlen)
  6580. {
  6581. struct sctp_association *asoc;
  6582. struct sctp_event param;
  6583. __u16 subscribe;
  6584. if (len < sizeof(param))
  6585. return -EINVAL;
  6586. len = sizeof(param);
  6587. if (copy_from_user(&param, optval, len))
  6588. return -EFAULT;
  6589. if (param.se_type < SCTP_SN_TYPE_BASE ||
  6590. param.se_type > SCTP_SN_TYPE_MAX)
  6591. return -EINVAL;
  6592. asoc = sctp_id2assoc(sk, param.se_assoc_id);
  6593. if (!asoc && param.se_assoc_id != SCTP_FUTURE_ASSOC &&
  6594. sctp_style(sk, UDP))
  6595. return -EINVAL;
  6596. subscribe = asoc ? asoc->subscribe : sctp_sk(sk)->subscribe;
  6597. param.se_on = sctp_ulpevent_type_enabled(subscribe, param.se_type);
  6598. if (put_user(len, optlen))
  6599. return -EFAULT;
  6600. if (copy_to_user(optval, &param, len))
  6601. return -EFAULT;
  6602. return 0;
  6603. }
  6604. static int sctp_getsockopt_asconf_supported(struct sock *sk, int len,
  6605. char __user *optval,
  6606. int __user *optlen)
  6607. {
  6608. struct sctp_assoc_value params;
  6609. struct sctp_association *asoc;
  6610. int retval = -EFAULT;
  6611. if (len < sizeof(params)) {
  6612. retval = -EINVAL;
  6613. goto out;
  6614. }
  6615. len = sizeof(params);
  6616. if (copy_from_user(&params, optval, len))
  6617. goto out;
  6618. asoc = sctp_id2assoc(sk, params.assoc_id);
  6619. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6620. sctp_style(sk, UDP)) {
  6621. retval = -EINVAL;
  6622. goto out;
  6623. }
  6624. params.assoc_value = asoc ? asoc->peer.asconf_capable
  6625. : sctp_sk(sk)->ep->asconf_enable;
  6626. if (put_user(len, optlen))
  6627. goto out;
  6628. if (copy_to_user(optval, &params, len))
  6629. goto out;
  6630. retval = 0;
  6631. out:
  6632. return retval;
  6633. }
  6634. static int sctp_getsockopt_auth_supported(struct sock *sk, int len,
  6635. char __user *optval,
  6636. int __user *optlen)
  6637. {
  6638. struct sctp_assoc_value params;
  6639. struct sctp_association *asoc;
  6640. int retval = -EFAULT;
  6641. if (len < sizeof(params)) {
  6642. retval = -EINVAL;
  6643. goto out;
  6644. }
  6645. len = sizeof(params);
  6646. if (copy_from_user(&params, optval, len))
  6647. goto out;
  6648. asoc = sctp_id2assoc(sk, params.assoc_id);
  6649. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6650. sctp_style(sk, UDP)) {
  6651. retval = -EINVAL;
  6652. goto out;
  6653. }
  6654. params.assoc_value = asoc ? asoc->peer.auth_capable
  6655. : sctp_sk(sk)->ep->auth_enable;
  6656. if (put_user(len, optlen))
  6657. goto out;
  6658. if (copy_to_user(optval, &params, len))
  6659. goto out;
  6660. retval = 0;
  6661. out:
  6662. return retval;
  6663. }
  6664. static int sctp_getsockopt_ecn_supported(struct sock *sk, int len,
  6665. char __user *optval,
  6666. int __user *optlen)
  6667. {
  6668. struct sctp_assoc_value params;
  6669. struct sctp_association *asoc;
  6670. int retval = -EFAULT;
  6671. if (len < sizeof(params)) {
  6672. retval = -EINVAL;
  6673. goto out;
  6674. }
  6675. len = sizeof(params);
  6676. if (copy_from_user(&params, optval, len))
  6677. goto out;
  6678. asoc = sctp_id2assoc(sk, params.assoc_id);
  6679. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6680. sctp_style(sk, UDP)) {
  6681. retval = -EINVAL;
  6682. goto out;
  6683. }
  6684. params.assoc_value = asoc ? asoc->peer.ecn_capable
  6685. : sctp_sk(sk)->ep->ecn_enable;
  6686. if (put_user(len, optlen))
  6687. goto out;
  6688. if (copy_to_user(optval, &params, len))
  6689. goto out;
  6690. retval = 0;
  6691. out:
  6692. return retval;
  6693. }
  6694. static int sctp_getsockopt_pf_expose(struct sock *sk, int len,
  6695. char __user *optval,
  6696. int __user *optlen)
  6697. {
  6698. struct sctp_assoc_value params;
  6699. struct sctp_association *asoc;
  6700. int retval = -EFAULT;
  6701. if (len < sizeof(params)) {
  6702. retval = -EINVAL;
  6703. goto out;
  6704. }
  6705. len = sizeof(params);
  6706. if (copy_from_user(&params, optval, len))
  6707. goto out;
  6708. asoc = sctp_id2assoc(sk, params.assoc_id);
  6709. if (!asoc && params.assoc_id != SCTP_FUTURE_ASSOC &&
  6710. sctp_style(sk, UDP)) {
  6711. retval = -EINVAL;
  6712. goto out;
  6713. }
  6714. params.assoc_value = asoc ? asoc->pf_expose
  6715. : sctp_sk(sk)->pf_expose;
  6716. if (put_user(len, optlen))
  6717. goto out;
  6718. if (copy_to_user(optval, &params, len))
  6719. goto out;
  6720. retval = 0;
  6721. out:
  6722. return retval;
  6723. }
  6724. static int sctp_getsockopt_encap_port(struct sock *sk, int len,
  6725. char __user *optval, int __user *optlen)
  6726. {
  6727. struct sctp_association *asoc;
  6728. struct sctp_udpencaps encap;
  6729. struct sctp_transport *t;
  6730. __be16 encap_port;
  6731. if (len < sizeof(encap))
  6732. return -EINVAL;
  6733. len = sizeof(encap);
  6734. if (copy_from_user(&encap, optval, len))
  6735. return -EFAULT;
  6736. /* If an address other than INADDR_ANY is specified, and
  6737. * no transport is found, then the request is invalid.
  6738. */
  6739. if (!sctp_is_any(sk, (union sctp_addr *)&encap.sue_address)) {
  6740. t = sctp_addr_id2transport(sk, &encap.sue_address,
  6741. encap.sue_assoc_id);
  6742. if (!t) {
  6743. pr_debug("%s: failed no transport\n", __func__);
  6744. return -EINVAL;
  6745. }
  6746. encap_port = t->encap_port;
  6747. goto out;
  6748. }
  6749. /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
  6750. * socket is a one to many style socket, and an association
  6751. * was not found, then the id was invalid.
  6752. */
  6753. asoc = sctp_id2assoc(sk, encap.sue_assoc_id);
  6754. if (!asoc && encap.sue_assoc_id != SCTP_FUTURE_ASSOC &&
  6755. sctp_style(sk, UDP)) {
  6756. pr_debug("%s: failed no association\n", __func__);
  6757. return -EINVAL;
  6758. }
  6759. if (asoc) {
  6760. encap_port = asoc->encap_port;
  6761. goto out;
  6762. }
  6763. encap_port = sctp_sk(sk)->encap_port;
  6764. out:
  6765. encap.sue_port = (__force uint16_t)encap_port;
  6766. if (copy_to_user(optval, &encap, len))
  6767. return -EFAULT;
  6768. if (put_user(len, optlen))
  6769. return -EFAULT;
  6770. return 0;
  6771. }
  6772. static int sctp_getsockopt_probe_interval(struct sock *sk, int len,
  6773. char __user *optval,
  6774. int __user *optlen)
  6775. {
  6776. struct sctp_probeinterval params;
  6777. struct sctp_association *asoc;
  6778. struct sctp_transport *t;
  6779. __u32 probe_interval;
  6780. if (len < sizeof(params))
  6781. return -EINVAL;
  6782. len = sizeof(params);
  6783. if (copy_from_user(&params, optval, len))
  6784. return -EFAULT;
  6785. /* If an address other than INADDR_ANY is specified, and
  6786. * no transport is found, then the request is invalid.
  6787. */
  6788. if (!sctp_is_any(sk, (union sctp_addr *)&params.spi_address)) {
  6789. t = sctp_addr_id2transport(sk, &params.spi_address,
  6790. params.spi_assoc_id);
  6791. if (!t) {
  6792. pr_debug("%s: failed no transport\n", __func__);
  6793. return -EINVAL;
  6794. }
  6795. probe_interval = jiffies_to_msecs(t->probe_interval);
  6796. goto out;
  6797. }
  6798. /* Get association, if assoc_id != SCTP_FUTURE_ASSOC and the
  6799. * socket is a one to many style socket, and an association
  6800. * was not found, then the id was invalid.
  6801. */
  6802. asoc = sctp_id2assoc(sk, params.spi_assoc_id);
  6803. if (!asoc && params.spi_assoc_id != SCTP_FUTURE_ASSOC &&
  6804. sctp_style(sk, UDP)) {
  6805. pr_debug("%s: failed no association\n", __func__);
  6806. return -EINVAL;
  6807. }
  6808. if (asoc) {
  6809. probe_interval = jiffies_to_msecs(asoc->probe_interval);
  6810. goto out;
  6811. }
  6812. probe_interval = sctp_sk(sk)->probe_interval;
  6813. out:
  6814. params.spi_interval = probe_interval;
  6815. if (copy_to_user(optval, &params, len))
  6816. return -EFAULT;
  6817. if (put_user(len, optlen))
  6818. return -EFAULT;
  6819. return 0;
  6820. }
  6821. static int sctp_getsockopt(struct sock *sk, int level, int optname,
  6822. char __user *optval, int __user *optlen)
  6823. {
  6824. int retval = 0;
  6825. int len;
  6826. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  6827. /* I can hardly begin to describe how wrong this is. This is
  6828. * so broken as to be worse than useless. The API draft
  6829. * REALLY is NOT helpful here... I am not convinced that the
  6830. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  6831. * are at all well-founded.
  6832. */
  6833. if (level != SOL_SCTP) {
  6834. struct sctp_af *af = sctp_sk(sk)->pf->af;
  6835. retval = af->getsockopt(sk, level, optname, optval, optlen);
  6836. return retval;
  6837. }
  6838. if (get_user(len, optlen))
  6839. return -EFAULT;
  6840. if (len < 0)
  6841. return -EINVAL;
  6842. lock_sock(sk);
  6843. switch (optname) {
  6844. case SCTP_STATUS:
  6845. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  6846. break;
  6847. case SCTP_DISABLE_FRAGMENTS:
  6848. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  6849. optlen);
  6850. break;
  6851. case SCTP_EVENTS:
  6852. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  6853. break;
  6854. case SCTP_AUTOCLOSE:
  6855. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  6856. break;
  6857. case SCTP_SOCKOPT_PEELOFF:
  6858. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  6859. break;
  6860. case SCTP_SOCKOPT_PEELOFF_FLAGS:
  6861. retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
  6862. break;
  6863. case SCTP_PEER_ADDR_PARAMS:
  6864. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  6865. optlen);
  6866. break;
  6867. case SCTP_DELAYED_SACK:
  6868. retval = sctp_getsockopt_delayed_ack(sk, len, optval,
  6869. optlen);
  6870. break;
  6871. case SCTP_INITMSG:
  6872. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  6873. break;
  6874. case SCTP_GET_PEER_ADDRS:
  6875. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  6876. optlen);
  6877. break;
  6878. case SCTP_GET_LOCAL_ADDRS:
  6879. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  6880. optlen);
  6881. break;
  6882. case SCTP_SOCKOPT_CONNECTX3:
  6883. retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
  6884. break;
  6885. case SCTP_DEFAULT_SEND_PARAM:
  6886. retval = sctp_getsockopt_default_send_param(sk, len,
  6887. optval, optlen);
  6888. break;
  6889. case SCTP_DEFAULT_SNDINFO:
  6890. retval = sctp_getsockopt_default_sndinfo(sk, len,
  6891. optval, optlen);
  6892. break;
  6893. case SCTP_PRIMARY_ADDR:
  6894. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  6895. break;
  6896. case SCTP_NODELAY:
  6897. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  6898. break;
  6899. case SCTP_RTOINFO:
  6900. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  6901. break;
  6902. case SCTP_ASSOCINFO:
  6903. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  6904. break;
  6905. case SCTP_I_WANT_MAPPED_V4_ADDR:
  6906. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  6907. break;
  6908. case SCTP_MAXSEG:
  6909. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  6910. break;
  6911. case SCTP_GET_PEER_ADDR_INFO:
  6912. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  6913. optlen);
  6914. break;
  6915. case SCTP_ADAPTATION_LAYER:
  6916. retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
  6917. optlen);
  6918. break;
  6919. case SCTP_CONTEXT:
  6920. retval = sctp_getsockopt_context(sk, len, optval, optlen);
  6921. break;
  6922. case SCTP_FRAGMENT_INTERLEAVE:
  6923. retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
  6924. optlen);
  6925. break;
  6926. case SCTP_PARTIAL_DELIVERY_POINT:
  6927. retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
  6928. optlen);
  6929. break;
  6930. case SCTP_MAX_BURST:
  6931. retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
  6932. break;
  6933. case SCTP_AUTH_KEY:
  6934. case SCTP_AUTH_CHUNK:
  6935. case SCTP_AUTH_DELETE_KEY:
  6936. case SCTP_AUTH_DEACTIVATE_KEY:
  6937. retval = -EOPNOTSUPP;
  6938. break;
  6939. case SCTP_HMAC_IDENT:
  6940. retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
  6941. break;
  6942. case SCTP_AUTH_ACTIVE_KEY:
  6943. retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
  6944. break;
  6945. case SCTP_PEER_AUTH_CHUNKS:
  6946. retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
  6947. optlen);
  6948. break;
  6949. case SCTP_LOCAL_AUTH_CHUNKS:
  6950. retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
  6951. optlen);
  6952. break;
  6953. case SCTP_GET_ASSOC_NUMBER:
  6954. retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
  6955. break;
  6956. case SCTP_GET_ASSOC_ID_LIST:
  6957. retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
  6958. break;
  6959. case SCTP_AUTO_ASCONF:
  6960. retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
  6961. break;
  6962. case SCTP_PEER_ADDR_THLDS:
  6963. retval = sctp_getsockopt_paddr_thresholds(sk, optval, len,
  6964. optlen, false);
  6965. break;
  6966. case SCTP_PEER_ADDR_THLDS_V2:
  6967. retval = sctp_getsockopt_paddr_thresholds(sk, optval, len,
  6968. optlen, true);
  6969. break;
  6970. case SCTP_GET_ASSOC_STATS:
  6971. retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
  6972. break;
  6973. case SCTP_RECVRCVINFO:
  6974. retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
  6975. break;
  6976. case SCTP_RECVNXTINFO:
  6977. retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
  6978. break;
  6979. case SCTP_PR_SUPPORTED:
  6980. retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
  6981. break;
  6982. case SCTP_DEFAULT_PRINFO:
  6983. retval = sctp_getsockopt_default_prinfo(sk, len, optval,
  6984. optlen);
  6985. break;
  6986. case SCTP_PR_ASSOC_STATUS:
  6987. retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
  6988. optlen);
  6989. break;
  6990. case SCTP_PR_STREAM_STATUS:
  6991. retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
  6992. optlen);
  6993. break;
  6994. case SCTP_RECONFIG_SUPPORTED:
  6995. retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
  6996. optlen);
  6997. break;
  6998. case SCTP_ENABLE_STREAM_RESET:
  6999. retval = sctp_getsockopt_enable_strreset(sk, len, optval,
  7000. optlen);
  7001. break;
  7002. case SCTP_STREAM_SCHEDULER:
  7003. retval = sctp_getsockopt_scheduler(sk, len, optval,
  7004. optlen);
  7005. break;
  7006. case SCTP_STREAM_SCHEDULER_VALUE:
  7007. retval = sctp_getsockopt_scheduler_value(sk, len, optval,
  7008. optlen);
  7009. break;
  7010. case SCTP_INTERLEAVING_SUPPORTED:
  7011. retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
  7012. optlen);
  7013. break;
  7014. case SCTP_REUSE_PORT:
  7015. retval = sctp_getsockopt_reuse_port(sk, len, optval, optlen);
  7016. break;
  7017. case SCTP_EVENT:
  7018. retval = sctp_getsockopt_event(sk, len, optval, optlen);
  7019. break;
  7020. case SCTP_ASCONF_SUPPORTED:
  7021. retval = sctp_getsockopt_asconf_supported(sk, len, optval,
  7022. optlen);
  7023. break;
  7024. case SCTP_AUTH_SUPPORTED:
  7025. retval = sctp_getsockopt_auth_supported(sk, len, optval,
  7026. optlen);
  7027. break;
  7028. case SCTP_ECN_SUPPORTED:
  7029. retval = sctp_getsockopt_ecn_supported(sk, len, optval, optlen);
  7030. break;
  7031. case SCTP_EXPOSE_POTENTIALLY_FAILED_STATE:
  7032. retval = sctp_getsockopt_pf_expose(sk, len, optval, optlen);
  7033. break;
  7034. case SCTP_REMOTE_UDP_ENCAPS_PORT:
  7035. retval = sctp_getsockopt_encap_port(sk, len, optval, optlen);
  7036. break;
  7037. case SCTP_PLPMTUD_PROBE_INTERVAL:
  7038. retval = sctp_getsockopt_probe_interval(sk, len, optval, optlen);
  7039. break;
  7040. default:
  7041. retval = -ENOPROTOOPT;
  7042. break;
  7043. }
  7044. release_sock(sk);
  7045. return retval;
  7046. }
  7047. static bool sctp_bpf_bypass_getsockopt(int level, int optname)
  7048. {
  7049. if (level == SOL_SCTP) {
  7050. switch (optname) {
  7051. case SCTP_SOCKOPT_PEELOFF:
  7052. case SCTP_SOCKOPT_PEELOFF_FLAGS:
  7053. case SCTP_SOCKOPT_CONNECTX3:
  7054. return true;
  7055. default:
  7056. return false;
  7057. }
  7058. }
  7059. return false;
  7060. }
  7061. static int sctp_hash(struct sock *sk)
  7062. {
  7063. /* STUB */
  7064. return 0;
  7065. }
  7066. static void sctp_unhash(struct sock *sk)
  7067. {
  7068. /* STUB */
  7069. }
  7070. /* Check if port is acceptable. Possibly find first available port.
  7071. *
  7072. * The port hash table (contained in the 'global' SCTP protocol storage
  7073. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  7074. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  7075. * list (the list number is the port number hashed out, so as you
  7076. * would expect from a hash function, all the ports in a given list have
  7077. * such a number that hashes out to the same list number; you were
  7078. * expecting that, right?); so each list has a set of ports, with a
  7079. * link to the socket (struct sock) that uses it, the port number and
  7080. * a fastreuse flag (FIXME: NPI ipg).
  7081. */
  7082. static struct sctp_bind_bucket *sctp_bucket_create(
  7083. struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
  7084. static int sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  7085. {
  7086. struct sctp_sock *sp = sctp_sk(sk);
  7087. bool reuse = (sk->sk_reuse || sp->reuse);
  7088. struct sctp_bind_hashbucket *head; /* hash list */
  7089. struct net *net = sock_net(sk);
  7090. kuid_t uid = sock_i_uid(sk);
  7091. struct sctp_bind_bucket *pp;
  7092. unsigned short snum;
  7093. int ret;
  7094. snum = ntohs(addr->v4.sin_port);
  7095. pr_debug("%s: begins, snum:%d\n", __func__, snum);
  7096. if (snum == 0) {
  7097. /* Search for an available port. */
  7098. int low, high, remaining, index;
  7099. unsigned int rover;
  7100. inet_sk_get_local_port_range(sk, &low, &high);
  7101. remaining = (high - low) + 1;
  7102. rover = get_random_u32_below(remaining) + low;
  7103. do {
  7104. rover++;
  7105. if ((rover < low) || (rover > high))
  7106. rover = low;
  7107. if (inet_is_local_reserved_port(net, rover))
  7108. continue;
  7109. index = sctp_phashfn(net, rover);
  7110. head = &sctp_port_hashtable[index];
  7111. spin_lock_bh(&head->lock);
  7112. sctp_for_each_hentry(pp, &head->chain)
  7113. if ((pp->port == rover) &&
  7114. net_eq(net, pp->net))
  7115. goto next;
  7116. break;
  7117. next:
  7118. spin_unlock_bh(&head->lock);
  7119. cond_resched();
  7120. } while (--remaining > 0);
  7121. /* Exhausted local port range during search? */
  7122. ret = 1;
  7123. if (remaining <= 0)
  7124. return ret;
  7125. /* OK, here is the one we will use. HEAD (the port
  7126. * hash table list entry) is non-NULL and we hold it's
  7127. * mutex.
  7128. */
  7129. snum = rover;
  7130. } else {
  7131. /* We are given an specific port number; we verify
  7132. * that it is not being used. If it is used, we will
  7133. * exahust the search in the hash list corresponding
  7134. * to the port number (snum) - we detect that with the
  7135. * port iterator, pp being NULL.
  7136. */
  7137. head = &sctp_port_hashtable[sctp_phashfn(net, snum)];
  7138. spin_lock_bh(&head->lock);
  7139. sctp_for_each_hentry(pp, &head->chain) {
  7140. if ((pp->port == snum) && net_eq(pp->net, net))
  7141. goto pp_found;
  7142. }
  7143. }
  7144. pp = NULL;
  7145. goto pp_not_found;
  7146. pp_found:
  7147. if (!hlist_empty(&pp->owner)) {
  7148. /* We had a port hash table hit - there is an
  7149. * available port (pp != NULL) and it is being
  7150. * used by other socket (pp->owner not empty); that other
  7151. * socket is going to be sk2.
  7152. */
  7153. struct sock *sk2;
  7154. pr_debug("%s: found a possible match\n", __func__);
  7155. if ((pp->fastreuse && reuse &&
  7156. sk->sk_state != SCTP_SS_LISTENING) ||
  7157. (pp->fastreuseport && sk->sk_reuseport &&
  7158. uid_eq(pp->fastuid, uid)))
  7159. goto success;
  7160. /* Run through the list of sockets bound to the port
  7161. * (pp->port) [via the pointers bind_next and
  7162. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  7163. * we get the endpoint they describe and run through
  7164. * the endpoint's list of IP (v4 or v6) addresses,
  7165. * comparing each of the addresses with the address of
  7166. * the socket sk. If we find a match, then that means
  7167. * that this port/socket (sk) combination are already
  7168. * in an endpoint.
  7169. */
  7170. sk_for_each_bound(sk2, &pp->owner) {
  7171. int bound_dev_if2 = READ_ONCE(sk2->sk_bound_dev_if);
  7172. struct sctp_sock *sp2 = sctp_sk(sk2);
  7173. struct sctp_endpoint *ep2 = sp2->ep;
  7174. if (sk == sk2 ||
  7175. (reuse && (sk2->sk_reuse || sp2->reuse) &&
  7176. sk2->sk_state != SCTP_SS_LISTENING) ||
  7177. (sk->sk_reuseport && sk2->sk_reuseport &&
  7178. uid_eq(uid, sock_i_uid(sk2))))
  7179. continue;
  7180. if ((!sk->sk_bound_dev_if || !bound_dev_if2 ||
  7181. sk->sk_bound_dev_if == bound_dev_if2) &&
  7182. sctp_bind_addr_conflict(&ep2->base.bind_addr,
  7183. addr, sp2, sp)) {
  7184. ret = 1;
  7185. goto fail_unlock;
  7186. }
  7187. }
  7188. pr_debug("%s: found a match\n", __func__);
  7189. }
  7190. pp_not_found:
  7191. /* If there was a hash table miss, create a new port. */
  7192. ret = 1;
  7193. if (!pp && !(pp = sctp_bucket_create(head, net, snum)))
  7194. goto fail_unlock;
  7195. /* In either case (hit or miss), make sure fastreuse is 1 only
  7196. * if sk->sk_reuse is too (that is, if the caller requested
  7197. * SO_REUSEADDR on this socket -sk-).
  7198. */
  7199. if (hlist_empty(&pp->owner)) {
  7200. if (reuse && sk->sk_state != SCTP_SS_LISTENING)
  7201. pp->fastreuse = 1;
  7202. else
  7203. pp->fastreuse = 0;
  7204. if (sk->sk_reuseport) {
  7205. pp->fastreuseport = 1;
  7206. pp->fastuid = uid;
  7207. } else {
  7208. pp->fastreuseport = 0;
  7209. }
  7210. } else {
  7211. if (pp->fastreuse &&
  7212. (!reuse || sk->sk_state == SCTP_SS_LISTENING))
  7213. pp->fastreuse = 0;
  7214. if (pp->fastreuseport &&
  7215. (!sk->sk_reuseport || !uid_eq(pp->fastuid, uid)))
  7216. pp->fastreuseport = 0;
  7217. }
  7218. /* We are set, so fill up all the data in the hash table
  7219. * entry, tie the socket list information with the rest of the
  7220. * sockets FIXME: Blurry, NPI (ipg).
  7221. */
  7222. success:
  7223. if (!sp->bind_hash) {
  7224. inet_sk(sk)->inet_num = snum;
  7225. sk_add_bind_node(sk, &pp->owner);
  7226. sp->bind_hash = pp;
  7227. }
  7228. ret = 0;
  7229. fail_unlock:
  7230. spin_unlock_bh(&head->lock);
  7231. return ret;
  7232. }
  7233. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  7234. * port is requested.
  7235. */
  7236. static int sctp_get_port(struct sock *sk, unsigned short snum)
  7237. {
  7238. union sctp_addr addr;
  7239. struct sctp_af *af = sctp_sk(sk)->pf->af;
  7240. /* Set up a dummy address struct from the sk. */
  7241. af->from_sk(&addr, sk);
  7242. addr.v4.sin_port = htons(snum);
  7243. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  7244. return sctp_get_port_local(sk, &addr);
  7245. }
  7246. /*
  7247. * Move a socket to LISTENING state.
  7248. */
  7249. static int sctp_listen_start(struct sock *sk, int backlog)
  7250. {
  7251. struct sctp_sock *sp = sctp_sk(sk);
  7252. struct sctp_endpoint *ep = sp->ep;
  7253. struct crypto_shash *tfm = NULL;
  7254. char alg[32];
  7255. int err;
  7256. /* Allocate HMAC for generating cookie. */
  7257. if (!sp->hmac && sp->sctp_hmac_alg) {
  7258. sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
  7259. tfm = crypto_alloc_shash(alg, 0, 0);
  7260. if (IS_ERR(tfm)) {
  7261. net_info_ratelimited("failed to load transform for %s: %ld\n",
  7262. sp->sctp_hmac_alg, PTR_ERR(tfm));
  7263. return -ENOSYS;
  7264. }
  7265. sctp_sk(sk)->hmac = tfm;
  7266. }
  7267. /*
  7268. * If a bind() or sctp_bindx() is not called prior to a listen()
  7269. * call that allows new associations to be accepted, the system
  7270. * picks an ephemeral port and will choose an address set equivalent
  7271. * to binding with a wildcard address.
  7272. *
  7273. * This is not currently spelled out in the SCTP sockets
  7274. * extensions draft, but follows the practice as seen in TCP
  7275. * sockets.
  7276. *
  7277. */
  7278. inet_sk_set_state(sk, SCTP_SS_LISTENING);
  7279. if (!ep->base.bind_addr.port) {
  7280. if (sctp_autobind(sk)) {
  7281. err = -EAGAIN;
  7282. goto err;
  7283. }
  7284. } else {
  7285. if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
  7286. err = -EADDRINUSE;
  7287. goto err;
  7288. }
  7289. }
  7290. WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
  7291. err = sctp_hash_endpoint(ep);
  7292. if (err)
  7293. goto err;
  7294. return 0;
  7295. err:
  7296. inet_sk_set_state(sk, SCTP_SS_CLOSED);
  7297. return err;
  7298. }
  7299. /*
  7300. * 4.1.3 / 5.1.3 listen()
  7301. *
  7302. * By default, new associations are not accepted for UDP style sockets.
  7303. * An application uses listen() to mark a socket as being able to
  7304. * accept new associations.
  7305. *
  7306. * On TCP style sockets, applications use listen() to ready the SCTP
  7307. * endpoint for accepting inbound associations.
  7308. *
  7309. * On both types of endpoints a backlog of '0' disables listening.
  7310. *
  7311. * Move a socket to LISTENING state.
  7312. */
  7313. int sctp_inet_listen(struct socket *sock, int backlog)
  7314. {
  7315. struct sock *sk = sock->sk;
  7316. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  7317. int err = -EINVAL;
  7318. if (unlikely(backlog < 0))
  7319. return err;
  7320. lock_sock(sk);
  7321. /* Peeled-off sockets are not allowed to listen(). */
  7322. if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
  7323. goto out;
  7324. if (sock->state != SS_UNCONNECTED)
  7325. goto out;
  7326. if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
  7327. goto out;
  7328. /* If backlog is zero, disable listening. */
  7329. if (!backlog) {
  7330. if (sctp_sstate(sk, CLOSED))
  7331. goto out;
  7332. err = 0;
  7333. sctp_unhash_endpoint(ep);
  7334. sk->sk_state = SCTP_SS_CLOSED;
  7335. if (sk->sk_reuse || sctp_sk(sk)->reuse)
  7336. sctp_sk(sk)->bind_hash->fastreuse = 1;
  7337. goto out;
  7338. }
  7339. /* If we are already listening, just update the backlog */
  7340. if (sctp_sstate(sk, LISTENING))
  7341. WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
  7342. else {
  7343. err = sctp_listen_start(sk, backlog);
  7344. if (err)
  7345. goto out;
  7346. }
  7347. err = 0;
  7348. out:
  7349. release_sock(sk);
  7350. return err;
  7351. }
  7352. /*
  7353. * This function is done by modeling the current datagram_poll() and the
  7354. * tcp_poll(). Note that, based on these implementations, we don't
  7355. * lock the socket in this function, even though it seems that,
  7356. * ideally, locking or some other mechanisms can be used to ensure
  7357. * the integrity of the counters (sndbuf and wmem_alloc) used
  7358. * in this place. We assume that we don't need locks either until proven
  7359. * otherwise.
  7360. *
  7361. * Another thing to note is that we include the Async I/O support
  7362. * here, again, by modeling the current TCP/UDP code. We don't have
  7363. * a good way to test with it yet.
  7364. */
  7365. __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  7366. {
  7367. struct sock *sk = sock->sk;
  7368. struct sctp_sock *sp = sctp_sk(sk);
  7369. __poll_t mask;
  7370. poll_wait(file, sk_sleep(sk), wait);
  7371. sock_rps_record_flow(sk);
  7372. /* A TCP-style listening socket becomes readable when the accept queue
  7373. * is not empty.
  7374. */
  7375. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  7376. return (!list_empty(&sp->ep->asocs)) ?
  7377. (EPOLLIN | EPOLLRDNORM) : 0;
  7378. mask = 0;
  7379. /* Is there any exceptional events? */
  7380. if (sk->sk_err || !skb_queue_empty_lockless(&sk->sk_error_queue))
  7381. mask |= EPOLLERR |
  7382. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
  7383. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7384. mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  7385. if (sk->sk_shutdown == SHUTDOWN_MASK)
  7386. mask |= EPOLLHUP;
  7387. /* Is it readable? Reconsider this code with TCP-style support. */
  7388. if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
  7389. mask |= EPOLLIN | EPOLLRDNORM;
  7390. /* The association is either gone or not ready. */
  7391. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  7392. return mask;
  7393. /* Is it writable? */
  7394. if (sctp_writeable(sk)) {
  7395. mask |= EPOLLOUT | EPOLLWRNORM;
  7396. } else {
  7397. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  7398. /*
  7399. * Since the socket is not locked, the buffer
  7400. * might be made available after the writeable check and
  7401. * before the bit is set. This could cause a lost I/O
  7402. * signal. tcp_poll() has a race breaker for this race
  7403. * condition. Based on their implementation, we put
  7404. * in the following code to cover it as well.
  7405. */
  7406. if (sctp_writeable(sk))
  7407. mask |= EPOLLOUT | EPOLLWRNORM;
  7408. }
  7409. return mask;
  7410. }
  7411. /********************************************************************
  7412. * 2nd Level Abstractions
  7413. ********************************************************************/
  7414. static struct sctp_bind_bucket *sctp_bucket_create(
  7415. struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
  7416. {
  7417. struct sctp_bind_bucket *pp;
  7418. pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
  7419. if (pp) {
  7420. SCTP_DBG_OBJCNT_INC(bind_bucket);
  7421. pp->port = snum;
  7422. pp->fastreuse = 0;
  7423. INIT_HLIST_HEAD(&pp->owner);
  7424. pp->net = net;
  7425. hlist_add_head(&pp->node, &head->chain);
  7426. }
  7427. return pp;
  7428. }
  7429. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  7430. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  7431. {
  7432. if (pp && hlist_empty(&pp->owner)) {
  7433. __hlist_del(&pp->node);
  7434. kmem_cache_free(sctp_bucket_cachep, pp);
  7435. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  7436. }
  7437. }
  7438. /* Release this socket's reference to a local port. */
  7439. static inline void __sctp_put_port(struct sock *sk)
  7440. {
  7441. struct sctp_bind_hashbucket *head =
  7442. &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
  7443. inet_sk(sk)->inet_num)];
  7444. struct sctp_bind_bucket *pp;
  7445. spin_lock(&head->lock);
  7446. pp = sctp_sk(sk)->bind_hash;
  7447. __sk_del_bind_node(sk);
  7448. sctp_sk(sk)->bind_hash = NULL;
  7449. inet_sk(sk)->inet_num = 0;
  7450. sctp_bucket_destroy(pp);
  7451. spin_unlock(&head->lock);
  7452. }
  7453. void sctp_put_port(struct sock *sk)
  7454. {
  7455. local_bh_disable();
  7456. __sctp_put_port(sk);
  7457. local_bh_enable();
  7458. }
  7459. /*
  7460. * The system picks an ephemeral port and choose an address set equivalent
  7461. * to binding with a wildcard address.
  7462. * One of those addresses will be the primary address for the association.
  7463. * This automatically enables the multihoming capability of SCTP.
  7464. */
  7465. static int sctp_autobind(struct sock *sk)
  7466. {
  7467. union sctp_addr autoaddr;
  7468. struct sctp_af *af;
  7469. __be16 port;
  7470. /* Initialize a local sockaddr structure to INADDR_ANY. */
  7471. af = sctp_sk(sk)->pf->af;
  7472. port = htons(inet_sk(sk)->inet_num);
  7473. af->inaddr_any(&autoaddr, port);
  7474. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  7475. }
  7476. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  7477. *
  7478. * From RFC 2292
  7479. * 4.2 The cmsghdr Structure *
  7480. *
  7481. * When ancillary data is sent or received, any number of ancillary data
  7482. * objects can be specified by the msg_control and msg_controllen members of
  7483. * the msghdr structure, because each object is preceded by
  7484. * a cmsghdr structure defining the object's length (the cmsg_len member).
  7485. * Historically Berkeley-derived implementations have passed only one object
  7486. * at a time, but this API allows multiple objects to be
  7487. * passed in a single call to sendmsg() or recvmsg(). The following example
  7488. * shows two ancillary data objects in a control buffer.
  7489. *
  7490. * |<--------------------------- msg_controllen -------------------------->|
  7491. * | |
  7492. *
  7493. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  7494. *
  7495. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  7496. * | | |
  7497. *
  7498. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  7499. *
  7500. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  7501. * | | | | |
  7502. *
  7503. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  7504. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  7505. *
  7506. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  7507. *
  7508. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  7509. * ^
  7510. * |
  7511. *
  7512. * msg_control
  7513. * points here
  7514. */
  7515. static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
  7516. {
  7517. struct msghdr *my_msg = (struct msghdr *)msg;
  7518. struct cmsghdr *cmsg;
  7519. for_each_cmsghdr(cmsg, my_msg) {
  7520. if (!CMSG_OK(my_msg, cmsg))
  7521. return -EINVAL;
  7522. /* Should we parse this header or ignore? */
  7523. if (cmsg->cmsg_level != IPPROTO_SCTP)
  7524. continue;
  7525. /* Strictly check lengths following example in SCM code. */
  7526. switch (cmsg->cmsg_type) {
  7527. case SCTP_INIT:
  7528. /* SCTP Socket API Extension
  7529. * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
  7530. *
  7531. * This cmsghdr structure provides information for
  7532. * initializing new SCTP associations with sendmsg().
  7533. * The SCTP_INITMSG socket option uses this same data
  7534. * structure. This structure is not used for
  7535. * recvmsg().
  7536. *
  7537. * cmsg_level cmsg_type cmsg_data[]
  7538. * ------------ ------------ ----------------------
  7539. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  7540. */
  7541. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
  7542. return -EINVAL;
  7543. cmsgs->init = CMSG_DATA(cmsg);
  7544. break;
  7545. case SCTP_SNDRCV:
  7546. /* SCTP Socket API Extension
  7547. * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
  7548. *
  7549. * This cmsghdr structure specifies SCTP options for
  7550. * sendmsg() and describes SCTP header information
  7551. * about a received message through recvmsg().
  7552. *
  7553. * cmsg_level cmsg_type cmsg_data[]
  7554. * ------------ ------------ ----------------------
  7555. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  7556. */
  7557. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  7558. return -EINVAL;
  7559. cmsgs->srinfo = CMSG_DATA(cmsg);
  7560. if (cmsgs->srinfo->sinfo_flags &
  7561. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  7562. SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
  7563. SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
  7564. return -EINVAL;
  7565. break;
  7566. case SCTP_SNDINFO:
  7567. /* SCTP Socket API Extension
  7568. * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
  7569. *
  7570. * This cmsghdr structure specifies SCTP options for
  7571. * sendmsg(). This structure and SCTP_RCVINFO replaces
  7572. * SCTP_SNDRCV which has been deprecated.
  7573. *
  7574. * cmsg_level cmsg_type cmsg_data[]
  7575. * ------------ ------------ ---------------------
  7576. * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
  7577. */
  7578. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
  7579. return -EINVAL;
  7580. cmsgs->sinfo = CMSG_DATA(cmsg);
  7581. if (cmsgs->sinfo->snd_flags &
  7582. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  7583. SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
  7584. SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
  7585. return -EINVAL;
  7586. break;
  7587. case SCTP_PRINFO:
  7588. /* SCTP Socket API Extension
  7589. * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
  7590. *
  7591. * This cmsghdr structure specifies SCTP options for sendmsg().
  7592. *
  7593. * cmsg_level cmsg_type cmsg_data[]
  7594. * ------------ ------------ ---------------------
  7595. * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
  7596. */
  7597. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
  7598. return -EINVAL;
  7599. cmsgs->prinfo = CMSG_DATA(cmsg);
  7600. if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
  7601. return -EINVAL;
  7602. if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
  7603. cmsgs->prinfo->pr_value = 0;
  7604. break;
  7605. case SCTP_AUTHINFO:
  7606. /* SCTP Socket API Extension
  7607. * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
  7608. *
  7609. * This cmsghdr structure specifies SCTP options for sendmsg().
  7610. *
  7611. * cmsg_level cmsg_type cmsg_data[]
  7612. * ------------ ------------ ---------------------
  7613. * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
  7614. */
  7615. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
  7616. return -EINVAL;
  7617. cmsgs->authinfo = CMSG_DATA(cmsg);
  7618. break;
  7619. case SCTP_DSTADDRV4:
  7620. case SCTP_DSTADDRV6:
  7621. /* SCTP Socket API Extension
  7622. * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
  7623. *
  7624. * This cmsghdr structure specifies SCTP options for sendmsg().
  7625. *
  7626. * cmsg_level cmsg_type cmsg_data[]
  7627. * ------------ ------------ ---------------------
  7628. * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
  7629. * ------------ ------------ ---------------------
  7630. * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
  7631. */
  7632. cmsgs->addrs_msg = my_msg;
  7633. break;
  7634. default:
  7635. return -EINVAL;
  7636. }
  7637. }
  7638. return 0;
  7639. }
  7640. /*
  7641. * Wait for a packet..
  7642. * Note: This function is the same function as in core/datagram.c
  7643. * with a few modifications to make lksctp work.
  7644. */
  7645. static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
  7646. {
  7647. int error;
  7648. DEFINE_WAIT(wait);
  7649. prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  7650. /* Socket errors? */
  7651. error = sock_error(sk);
  7652. if (error)
  7653. goto out;
  7654. if (!skb_queue_empty(&sk->sk_receive_queue))
  7655. goto ready;
  7656. /* Socket shut down? */
  7657. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7658. goto out;
  7659. /* Sequenced packets can come disconnected. If so we report the
  7660. * problem.
  7661. */
  7662. error = -ENOTCONN;
  7663. /* Is there a good reason to think that we may receive some data? */
  7664. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  7665. goto out;
  7666. /* Handle signals. */
  7667. if (signal_pending(current))
  7668. goto interrupted;
  7669. /* Let another process have a go. Since we are going to sleep
  7670. * anyway. Note: This may cause odd behaviors if the message
  7671. * does not fit in the user's buffer, but this seems to be the
  7672. * only way to honor MSG_DONTWAIT realistically.
  7673. */
  7674. release_sock(sk);
  7675. *timeo_p = schedule_timeout(*timeo_p);
  7676. lock_sock(sk);
  7677. ready:
  7678. finish_wait(sk_sleep(sk), &wait);
  7679. return 0;
  7680. interrupted:
  7681. error = sock_intr_errno(*timeo_p);
  7682. out:
  7683. finish_wait(sk_sleep(sk), &wait);
  7684. *err = error;
  7685. return error;
  7686. }
  7687. /* Receive a datagram.
  7688. * Note: This is pretty much the same routine as in core/datagram.c
  7689. * with a few changes to make lksctp work.
  7690. */
  7691. struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, int *err)
  7692. {
  7693. int error;
  7694. struct sk_buff *skb;
  7695. long timeo;
  7696. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  7697. pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
  7698. MAX_SCHEDULE_TIMEOUT);
  7699. do {
  7700. /* Again only user level code calls this function,
  7701. * so nothing interrupt level
  7702. * will suddenly eat the receive_queue.
  7703. *
  7704. * Look at current nfs client by the way...
  7705. * However, this function was correct in any case. 8)
  7706. */
  7707. if (flags & MSG_PEEK) {
  7708. skb = skb_peek(&sk->sk_receive_queue);
  7709. if (skb)
  7710. refcount_inc(&skb->users);
  7711. } else {
  7712. skb = __skb_dequeue(&sk->sk_receive_queue);
  7713. }
  7714. if (skb)
  7715. return skb;
  7716. /* Caller is allowed not to check sk->sk_err before calling. */
  7717. error = sock_error(sk);
  7718. if (error)
  7719. goto no_packet;
  7720. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7721. break;
  7722. /* User doesn't want to wait. */
  7723. error = -EAGAIN;
  7724. if (!timeo)
  7725. goto no_packet;
  7726. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  7727. return NULL;
  7728. no_packet:
  7729. *err = error;
  7730. return NULL;
  7731. }
  7732. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  7733. static void __sctp_write_space(struct sctp_association *asoc)
  7734. {
  7735. struct sock *sk = asoc->base.sk;
  7736. if (sctp_wspace(asoc) <= 0)
  7737. return;
  7738. if (waitqueue_active(&asoc->wait))
  7739. wake_up_interruptible(&asoc->wait);
  7740. if (sctp_writeable(sk)) {
  7741. struct socket_wq *wq;
  7742. rcu_read_lock();
  7743. wq = rcu_dereference(sk->sk_wq);
  7744. if (wq) {
  7745. if (waitqueue_active(&wq->wait))
  7746. wake_up_interruptible_poll(&wq->wait, EPOLLOUT |
  7747. EPOLLWRNORM | EPOLLWRBAND);
  7748. /* Note that we try to include the Async I/O support
  7749. * here by modeling from the current TCP/UDP code.
  7750. * We have not tested with it yet.
  7751. */
  7752. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  7753. sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
  7754. }
  7755. rcu_read_unlock();
  7756. }
  7757. }
  7758. static void sctp_wake_up_waiters(struct sock *sk,
  7759. struct sctp_association *asoc)
  7760. {
  7761. struct sctp_association *tmp = asoc;
  7762. /* We do accounting for the sndbuf space per association,
  7763. * so we only need to wake our own association.
  7764. */
  7765. if (asoc->ep->sndbuf_policy)
  7766. return __sctp_write_space(asoc);
  7767. /* If association goes down and is just flushing its
  7768. * outq, then just normally notify others.
  7769. */
  7770. if (asoc->base.dead)
  7771. return sctp_write_space(sk);
  7772. /* Accounting for the sndbuf space is per socket, so we
  7773. * need to wake up others, try to be fair and in case of
  7774. * other associations, let them have a go first instead
  7775. * of just doing a sctp_write_space() call.
  7776. *
  7777. * Note that we reach sctp_wake_up_waiters() only when
  7778. * associations free up queued chunks, thus we are under
  7779. * lock and the list of associations on a socket is
  7780. * guaranteed not to change.
  7781. */
  7782. for (tmp = list_next_entry(tmp, asocs); 1;
  7783. tmp = list_next_entry(tmp, asocs)) {
  7784. /* Manually skip the head element. */
  7785. if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
  7786. continue;
  7787. /* Wake up association. */
  7788. __sctp_write_space(tmp);
  7789. /* We've reached the end. */
  7790. if (tmp == asoc)
  7791. break;
  7792. }
  7793. }
  7794. /* Do accounting for the sndbuf space.
  7795. * Decrement the used sndbuf space of the corresponding association by the
  7796. * data size which was just transmitted(freed).
  7797. */
  7798. static void sctp_wfree(struct sk_buff *skb)
  7799. {
  7800. struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
  7801. struct sctp_association *asoc = chunk->asoc;
  7802. struct sock *sk = asoc->base.sk;
  7803. sk_mem_uncharge(sk, skb->truesize);
  7804. sk_wmem_queued_add(sk, -(skb->truesize + sizeof(struct sctp_chunk)));
  7805. asoc->sndbuf_used -= skb->truesize + sizeof(struct sctp_chunk);
  7806. WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk),
  7807. &sk->sk_wmem_alloc));
  7808. if (chunk->shkey) {
  7809. struct sctp_shared_key *shkey = chunk->shkey;
  7810. /* refcnt == 2 and !list_empty mean after this release, it's
  7811. * not being used anywhere, and it's time to notify userland
  7812. * that this shkey can be freed if it's been deactivated.
  7813. */
  7814. if (shkey->deactivated && !list_empty(&shkey->key_list) &&
  7815. refcount_read(&shkey->refcnt) == 2) {
  7816. struct sctp_ulpevent *ev;
  7817. ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
  7818. SCTP_AUTH_FREE_KEY,
  7819. GFP_KERNEL);
  7820. if (ev)
  7821. asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
  7822. }
  7823. sctp_auth_shkey_release(chunk->shkey);
  7824. }
  7825. sock_wfree(skb);
  7826. sctp_wake_up_waiters(sk, asoc);
  7827. sctp_association_put(asoc);
  7828. }
  7829. /* Do accounting for the receive space on the socket.
  7830. * Accounting for the association is done in ulpevent.c
  7831. * We set this as a destructor for the cloned data skbs so that
  7832. * accounting is done at the correct time.
  7833. */
  7834. void sctp_sock_rfree(struct sk_buff *skb)
  7835. {
  7836. struct sock *sk = skb->sk;
  7837. struct sctp_ulpevent *event = sctp_skb2event(skb);
  7838. atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
  7839. /*
  7840. * Mimic the behavior of sock_rfree
  7841. */
  7842. sk_mem_uncharge(sk, event->rmem_len);
  7843. }
  7844. /* Helper function to wait for space in the sndbuf. */
  7845. static int sctp_wait_for_sndbuf(struct sctp_association *asoc,
  7846. struct sctp_transport *transport,
  7847. long *timeo_p, size_t msg_len)
  7848. {
  7849. struct sock *sk = asoc->base.sk;
  7850. long current_timeo = *timeo_p;
  7851. DEFINE_WAIT(wait);
  7852. int err = 0;
  7853. pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
  7854. *timeo_p, msg_len);
  7855. /* Increment the transport and association's refcnt. */
  7856. if (transport)
  7857. sctp_transport_hold(transport);
  7858. sctp_association_hold(asoc);
  7859. /* Wait on the association specific sndbuf space. */
  7860. for (;;) {
  7861. prepare_to_wait_exclusive(&asoc->wait, &wait,
  7862. TASK_INTERRUPTIBLE);
  7863. if (asoc->base.dead)
  7864. goto do_dead;
  7865. if ((!*timeo_p) || (transport && transport->dead))
  7866. goto do_nonblock;
  7867. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
  7868. goto do_error;
  7869. if (signal_pending(current))
  7870. goto do_interrupted;
  7871. if ((int)msg_len <= sctp_wspace(asoc) &&
  7872. sk_wmem_schedule(sk, msg_len))
  7873. break;
  7874. /* Let another process have a go. Since we are going
  7875. * to sleep anyway.
  7876. */
  7877. release_sock(sk);
  7878. current_timeo = schedule_timeout(current_timeo);
  7879. lock_sock(sk);
  7880. if (sk != asoc->base.sk)
  7881. goto do_error;
  7882. *timeo_p = current_timeo;
  7883. }
  7884. out:
  7885. finish_wait(&asoc->wait, &wait);
  7886. /* Release the transport and association's refcnt. */
  7887. if (transport)
  7888. sctp_transport_put(transport);
  7889. sctp_association_put(asoc);
  7890. return err;
  7891. do_dead:
  7892. err = -ESRCH;
  7893. goto out;
  7894. do_error:
  7895. err = -EPIPE;
  7896. goto out;
  7897. do_interrupted:
  7898. err = sock_intr_errno(*timeo_p);
  7899. goto out;
  7900. do_nonblock:
  7901. err = -EAGAIN;
  7902. goto out;
  7903. }
  7904. void sctp_data_ready(struct sock *sk)
  7905. {
  7906. struct socket_wq *wq;
  7907. trace_sk_data_ready(sk);
  7908. rcu_read_lock();
  7909. wq = rcu_dereference(sk->sk_wq);
  7910. if (skwq_has_sleeper(wq))
  7911. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
  7912. EPOLLRDNORM | EPOLLRDBAND);
  7913. sk_wake_async_rcu(sk, SOCK_WAKE_WAITD, POLL_IN);
  7914. rcu_read_unlock();
  7915. }
  7916. /* If socket sndbuf has changed, wake up all per association waiters. */
  7917. void sctp_write_space(struct sock *sk)
  7918. {
  7919. struct sctp_association *asoc;
  7920. /* Wake up the tasks in each wait queue. */
  7921. list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
  7922. __sctp_write_space(asoc);
  7923. }
  7924. }
  7925. /* Is there any sndbuf space available on the socket?
  7926. *
  7927. * Note that sk_wmem_alloc is the sum of the send buffers on all of the
  7928. * associations on the same socket. For a UDP-style socket with
  7929. * multiple associations, it is possible for it to be "unwriteable"
  7930. * prematurely. I assume that this is acceptable because
  7931. * a premature "unwriteable" is better than an accidental "writeable" which
  7932. * would cause an unwanted block under certain circumstances. For the 1-1
  7933. * UDP-style sockets or TCP-style sockets, this code should work.
  7934. * - Daisy
  7935. */
  7936. static bool sctp_writeable(const struct sock *sk)
  7937. {
  7938. return READ_ONCE(sk->sk_sndbuf) > READ_ONCE(sk->sk_wmem_queued);
  7939. }
  7940. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  7941. * returns immediately with EINPROGRESS.
  7942. */
  7943. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  7944. {
  7945. struct sock *sk = asoc->base.sk;
  7946. int err = 0;
  7947. long current_timeo = *timeo_p;
  7948. DEFINE_WAIT(wait);
  7949. pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
  7950. /* Increment the association's refcnt. */
  7951. sctp_association_hold(asoc);
  7952. for (;;) {
  7953. prepare_to_wait_exclusive(&asoc->wait, &wait,
  7954. TASK_INTERRUPTIBLE);
  7955. if (!*timeo_p)
  7956. goto do_nonblock;
  7957. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7958. break;
  7959. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  7960. asoc->base.dead)
  7961. goto do_error;
  7962. if (signal_pending(current))
  7963. goto do_interrupted;
  7964. if (sctp_state(asoc, ESTABLISHED))
  7965. break;
  7966. /* Let another process have a go. Since we are going
  7967. * to sleep anyway.
  7968. */
  7969. release_sock(sk);
  7970. current_timeo = schedule_timeout(current_timeo);
  7971. lock_sock(sk);
  7972. *timeo_p = current_timeo;
  7973. }
  7974. out:
  7975. finish_wait(&asoc->wait, &wait);
  7976. /* Release the association's refcnt. */
  7977. sctp_association_put(asoc);
  7978. return err;
  7979. do_error:
  7980. if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
  7981. err = -ETIMEDOUT;
  7982. else
  7983. err = -ECONNREFUSED;
  7984. goto out;
  7985. do_interrupted:
  7986. err = sock_intr_errno(*timeo_p);
  7987. goto out;
  7988. do_nonblock:
  7989. err = -EINPROGRESS;
  7990. goto out;
  7991. }
  7992. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  7993. {
  7994. struct sctp_endpoint *ep;
  7995. int err = 0;
  7996. DEFINE_WAIT(wait);
  7997. ep = sctp_sk(sk)->ep;
  7998. for (;;) {
  7999. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  8000. TASK_INTERRUPTIBLE);
  8001. if (list_empty(&ep->asocs)) {
  8002. release_sock(sk);
  8003. timeo = schedule_timeout(timeo);
  8004. lock_sock(sk);
  8005. }
  8006. err = -EINVAL;
  8007. if (!sctp_sstate(sk, LISTENING) ||
  8008. (sk->sk_shutdown & RCV_SHUTDOWN))
  8009. break;
  8010. err = 0;
  8011. if (!list_empty(&ep->asocs))
  8012. break;
  8013. err = sock_intr_errno(timeo);
  8014. if (signal_pending(current))
  8015. break;
  8016. err = -EAGAIN;
  8017. if (!timeo)
  8018. break;
  8019. }
  8020. finish_wait(sk_sleep(sk), &wait);
  8021. return err;
  8022. }
  8023. static void sctp_wait_for_close(struct sock *sk, long timeout)
  8024. {
  8025. DEFINE_WAIT(wait);
  8026. do {
  8027. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  8028. if (list_empty(&sctp_sk(sk)->ep->asocs))
  8029. break;
  8030. release_sock(sk);
  8031. timeout = schedule_timeout(timeout);
  8032. lock_sock(sk);
  8033. } while (!signal_pending(current) && timeout);
  8034. finish_wait(sk_sleep(sk), &wait);
  8035. }
  8036. static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
  8037. {
  8038. struct sk_buff *frag;
  8039. if (!skb->data_len)
  8040. goto done;
  8041. /* Don't forget the fragments. */
  8042. skb_walk_frags(skb, frag)
  8043. sctp_skb_set_owner_r_frag(frag, sk);
  8044. done:
  8045. sctp_skb_set_owner_r(skb, sk);
  8046. }
  8047. void sctp_copy_sock(struct sock *newsk, struct sock *sk,
  8048. struct sctp_association *asoc)
  8049. {
  8050. struct inet_sock *inet = inet_sk(sk);
  8051. struct inet_sock *newinet;
  8052. struct sctp_sock *sp = sctp_sk(sk);
  8053. newsk->sk_type = sk->sk_type;
  8054. newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
  8055. newsk->sk_flags = sk->sk_flags;
  8056. newsk->sk_tsflags = sk->sk_tsflags;
  8057. newsk->sk_no_check_tx = sk->sk_no_check_tx;
  8058. newsk->sk_no_check_rx = sk->sk_no_check_rx;
  8059. newsk->sk_reuse = sk->sk_reuse;
  8060. sctp_sk(newsk)->reuse = sp->reuse;
  8061. newsk->sk_shutdown = sk->sk_shutdown;
  8062. newsk->sk_destruct = sk->sk_destruct;
  8063. newsk->sk_family = sk->sk_family;
  8064. newsk->sk_protocol = IPPROTO_SCTP;
  8065. newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  8066. newsk->sk_sndbuf = sk->sk_sndbuf;
  8067. newsk->sk_rcvbuf = sk->sk_rcvbuf;
  8068. newsk->sk_lingertime = sk->sk_lingertime;
  8069. newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
  8070. newsk->sk_sndtimeo = sk->sk_sndtimeo;
  8071. newsk->sk_rxhash = sk->sk_rxhash;
  8072. newinet = inet_sk(newsk);
  8073. /* Initialize sk's sport, dport, rcv_saddr and daddr for
  8074. * getsockname() and getpeername()
  8075. */
  8076. newinet->inet_sport = inet->inet_sport;
  8077. newinet->inet_saddr = inet->inet_saddr;
  8078. newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
  8079. newinet->inet_dport = htons(asoc->peer.port);
  8080. newinet->pmtudisc = inet->pmtudisc;
  8081. atomic_set(&newinet->inet_id, get_random_u16());
  8082. newinet->uc_ttl = inet->uc_ttl;
  8083. inet_set_bit(MC_LOOP, newsk);
  8084. newinet->mc_ttl = 1;
  8085. newinet->mc_index = 0;
  8086. newinet->mc_list = NULL;
  8087. if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
  8088. net_enable_timestamp();
  8089. /* Set newsk security attributes from original sk and connection
  8090. * security attribute from asoc.
  8091. */
  8092. security_sctp_sk_clone(asoc, sk, newsk);
  8093. }
  8094. static inline void sctp_copy_descendant(struct sock *sk_to,
  8095. const struct sock *sk_from)
  8096. {
  8097. size_t ancestor_size = sizeof(struct inet_sock);
  8098. ancestor_size += sk_from->sk_prot->obj_size;
  8099. ancestor_size -= offsetof(struct sctp_sock, pd_lobby);
  8100. __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
  8101. }
  8102. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  8103. * and its messages to the newsk.
  8104. */
  8105. static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  8106. struct sctp_association *assoc,
  8107. enum sctp_socket_type type)
  8108. {
  8109. struct sctp_sock *oldsp = sctp_sk(oldsk);
  8110. struct sctp_sock *newsp = sctp_sk(newsk);
  8111. struct sctp_bind_bucket *pp; /* hash list port iterator */
  8112. struct sctp_endpoint *newep = newsp->ep;
  8113. struct sk_buff *skb, *tmp;
  8114. struct sctp_ulpevent *event;
  8115. struct sctp_bind_hashbucket *head;
  8116. int err;
  8117. /* Migrate socket buffer sizes and all the socket level options to the
  8118. * new socket.
  8119. */
  8120. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  8121. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  8122. /* Brute force copy old sctp opt. */
  8123. sctp_copy_descendant(newsk, oldsk);
  8124. /* Restore the ep value that was overwritten with the above structure
  8125. * copy.
  8126. */
  8127. newsp->ep = newep;
  8128. newsp->hmac = NULL;
  8129. /* Hook this new socket in to the bind_hash list. */
  8130. head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
  8131. inet_sk(oldsk)->inet_num)];
  8132. spin_lock_bh(&head->lock);
  8133. pp = sctp_sk(oldsk)->bind_hash;
  8134. sk_add_bind_node(newsk, &pp->owner);
  8135. sctp_sk(newsk)->bind_hash = pp;
  8136. inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
  8137. spin_unlock_bh(&head->lock);
  8138. /* Copy the bind_addr list from the original endpoint to the new
  8139. * endpoint so that we can handle restarts properly
  8140. */
  8141. err = sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
  8142. &oldsp->ep->base.bind_addr, GFP_KERNEL);
  8143. if (err)
  8144. return err;
  8145. /* New ep's auth_hmacs should be set if old ep's is set, in case
  8146. * that net->sctp.auth_enable has been changed to 0 by users and
  8147. * new ep's auth_hmacs couldn't be set in sctp_endpoint_init().
  8148. */
  8149. if (oldsp->ep->auth_hmacs) {
  8150. err = sctp_auth_init_hmacs(newsp->ep, GFP_KERNEL);
  8151. if (err)
  8152. return err;
  8153. }
  8154. sctp_auto_asconf_init(newsp);
  8155. /* Move any messages in the old socket's receive queue that are for the
  8156. * peeled off association to the new socket's receive queue.
  8157. */
  8158. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  8159. event = sctp_skb2event(skb);
  8160. if (event->asoc == assoc) {
  8161. __skb_unlink(skb, &oldsk->sk_receive_queue);
  8162. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  8163. sctp_skb_set_owner_r_frag(skb, newsk);
  8164. }
  8165. }
  8166. /* Clean up any messages pending delivery due to partial
  8167. * delivery. Three cases:
  8168. * 1) No partial deliver; no work.
  8169. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  8170. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  8171. */
  8172. atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
  8173. if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
  8174. struct sk_buff_head *queue;
  8175. /* Decide which queue to move pd_lobby skbs to. */
  8176. if (assoc->ulpq.pd_mode) {
  8177. queue = &newsp->pd_lobby;
  8178. } else
  8179. queue = &newsk->sk_receive_queue;
  8180. /* Walk through the pd_lobby, looking for skbs that
  8181. * need moved to the new socket.
  8182. */
  8183. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  8184. event = sctp_skb2event(skb);
  8185. if (event->asoc == assoc) {
  8186. __skb_unlink(skb, &oldsp->pd_lobby);
  8187. __skb_queue_tail(queue, skb);
  8188. sctp_skb_set_owner_r_frag(skb, newsk);
  8189. }
  8190. }
  8191. /* Clear up any skbs waiting for the partial
  8192. * delivery to finish.
  8193. */
  8194. if (assoc->ulpq.pd_mode)
  8195. sctp_clear_pd(oldsk, NULL);
  8196. }
  8197. sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
  8198. /* Set the type of socket to indicate that it is peeled off from the
  8199. * original UDP-style socket or created with the accept() call on a
  8200. * TCP-style socket..
  8201. */
  8202. newsp->type = type;
  8203. /* Mark the new socket "in-use" by the user so that any packets
  8204. * that may arrive on the association after we've moved it are
  8205. * queued to the backlog. This prevents a potential race between
  8206. * backlog processing on the old socket and new-packet processing
  8207. * on the new socket.
  8208. *
  8209. * The caller has just allocated newsk so we can guarantee that other
  8210. * paths won't try to lock it and then oldsk.
  8211. */
  8212. lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
  8213. sctp_for_each_tx_datachunk(assoc, true, sctp_clear_owner_w);
  8214. sctp_assoc_migrate(assoc, newsk);
  8215. sctp_for_each_tx_datachunk(assoc, false, sctp_set_owner_w);
  8216. /* If the association on the newsk is already closed before accept()
  8217. * is called, set RCV_SHUTDOWN flag.
  8218. */
  8219. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
  8220. inet_sk_set_state(newsk, SCTP_SS_CLOSED);
  8221. newsk->sk_shutdown |= RCV_SHUTDOWN;
  8222. } else {
  8223. inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
  8224. }
  8225. release_sock(newsk);
  8226. return 0;
  8227. }
  8228. /* This proto struct describes the ULP interface for SCTP. */
  8229. struct proto sctp_prot = {
  8230. .name = "SCTP",
  8231. .owner = THIS_MODULE,
  8232. .close = sctp_close,
  8233. .disconnect = sctp_disconnect,
  8234. .accept = sctp_accept,
  8235. .ioctl = sctp_ioctl,
  8236. .init = sctp_init_sock,
  8237. .destroy = sctp_destroy_sock,
  8238. .shutdown = sctp_shutdown,
  8239. .setsockopt = sctp_setsockopt,
  8240. .getsockopt = sctp_getsockopt,
  8241. .bpf_bypass_getsockopt = sctp_bpf_bypass_getsockopt,
  8242. .sendmsg = sctp_sendmsg,
  8243. .recvmsg = sctp_recvmsg,
  8244. .bind = sctp_bind,
  8245. .bind_add = sctp_bind_add,
  8246. .backlog_rcv = sctp_backlog_rcv,
  8247. .hash = sctp_hash,
  8248. .unhash = sctp_unhash,
  8249. .no_autobind = true,
  8250. .obj_size = sizeof(struct sctp_sock),
  8251. .useroffset = offsetof(struct sctp_sock, subscribe),
  8252. .usersize = offsetof(struct sctp_sock, initmsg) -
  8253. offsetof(struct sctp_sock, subscribe) +
  8254. sizeof_field(struct sctp_sock, initmsg),
  8255. .sysctl_mem = sysctl_sctp_mem,
  8256. .sysctl_rmem = sysctl_sctp_rmem,
  8257. .sysctl_wmem = sysctl_sctp_wmem,
  8258. .memory_pressure = &sctp_memory_pressure,
  8259. .enter_memory_pressure = sctp_enter_memory_pressure,
  8260. .memory_allocated = &sctp_memory_allocated,
  8261. .per_cpu_fw_alloc = &sctp_memory_per_cpu_fw_alloc,
  8262. .sockets_allocated = &sctp_sockets_allocated,
  8263. };
  8264. #if IS_ENABLED(CONFIG_IPV6)
  8265. static void sctp_v6_destruct_sock(struct sock *sk)
  8266. {
  8267. sctp_destruct_common(sk);
  8268. inet6_sock_destruct(sk);
  8269. }
  8270. static int sctp_v6_init_sock(struct sock *sk)
  8271. {
  8272. int ret = sctp_init_sock(sk);
  8273. if (!ret)
  8274. sk->sk_destruct = sctp_v6_destruct_sock;
  8275. return ret;
  8276. }
  8277. struct proto sctpv6_prot = {
  8278. .name = "SCTPv6",
  8279. .owner = THIS_MODULE,
  8280. .close = sctp_close,
  8281. .disconnect = sctp_disconnect,
  8282. .accept = sctp_accept,
  8283. .ioctl = sctp_ioctl,
  8284. .init = sctp_v6_init_sock,
  8285. .destroy = sctp_destroy_sock,
  8286. .shutdown = sctp_shutdown,
  8287. .setsockopt = sctp_setsockopt,
  8288. .getsockopt = sctp_getsockopt,
  8289. .bpf_bypass_getsockopt = sctp_bpf_bypass_getsockopt,
  8290. .sendmsg = sctp_sendmsg,
  8291. .recvmsg = sctp_recvmsg,
  8292. .bind = sctp_bind,
  8293. .bind_add = sctp_bind_add,
  8294. .backlog_rcv = sctp_backlog_rcv,
  8295. .hash = sctp_hash,
  8296. .unhash = sctp_unhash,
  8297. .no_autobind = true,
  8298. .obj_size = sizeof(struct sctp6_sock),
  8299. .ipv6_pinfo_offset = offsetof(struct sctp6_sock, inet6),
  8300. .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
  8301. .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
  8302. offsetof(struct sctp6_sock, sctp.subscribe) +
  8303. sizeof_field(struct sctp6_sock, sctp.initmsg),
  8304. .sysctl_mem = sysctl_sctp_mem,
  8305. .sysctl_rmem = sysctl_sctp_rmem,
  8306. .sysctl_wmem = sysctl_sctp_wmem,
  8307. .memory_pressure = &sctp_memory_pressure,
  8308. .enter_memory_pressure = sctp_enter_memory_pressure,
  8309. .memory_allocated = &sctp_memory_allocated,
  8310. .per_cpu_fw_alloc = &sctp_memory_per_cpu_fw_alloc,
  8311. .sockets_allocated = &sctp_sockets_allocated,
  8312. };
  8313. #endif /* IS_ENABLED(CONFIG_IPV6) */