socket.c 236 KB

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