socket.c 247 KB

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