socket.c 247 KB

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