socket.c 235 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686
  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 (sctp_state(asoc, CLOSED)) {
  1621. err = sctp_primitive_ASSOCIATE(net, asoc, NULL);
  1622. if (err)
  1623. goto err;
  1624. if (sctp_sk(sk)->strm_interleave) {
  1625. timeo = sock_sndtimeo(sk, 0);
  1626. err = sctp_wait_for_connect(asoc, &timeo);
  1627. if (err)
  1628. goto err;
  1629. } else {
  1630. wait_connect = true;
  1631. }
  1632. pr_debug("%s: we associated primitively\n", __func__);
  1633. }
  1634. if (asoc->pmtu_pending)
  1635. sctp_assoc_pending_pmtu(asoc);
  1636. if (sctp_wspace(asoc) < msg_len)
  1637. sctp_prsctp_prune(asoc, sinfo, msg_len - sctp_wspace(asoc));
  1638. if (!sctp_wspace(asoc)) {
  1639. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1640. err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
  1641. if (err)
  1642. goto err;
  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.1.23 SCTP_AUTO_ASCONF
  3144. *
  3145. * This option will enable or disable the use of the automatic generation of
  3146. * ASCONF chunks to add and delete addresses to an existing association. Note
  3147. * that this option has two caveats namely: a) it only affects sockets that
  3148. * are bound to all addresses available to the SCTP stack, and b) the system
  3149. * administrator may have an overriding control that turns the ASCONF feature
  3150. * off no matter what setting the socket option may have.
  3151. * This option expects an integer boolean flag, where a non-zero value turns on
  3152. * the option, and a zero value turns off the option.
  3153. * Note. In this implementation, socket operation overrides default parameter
  3154. * being set by sysctl as well as FreeBSD implementation
  3155. */
  3156. static int sctp_setsockopt_auto_asconf(struct sock *sk, char __user *optval,
  3157. unsigned int optlen)
  3158. {
  3159. int val;
  3160. struct sctp_sock *sp = sctp_sk(sk);
  3161. if (optlen < sizeof(int))
  3162. return -EINVAL;
  3163. if (get_user(val, (int __user *)optval))
  3164. return -EFAULT;
  3165. if (!sctp_is_ep_boundall(sk) && val)
  3166. return -EINVAL;
  3167. if ((val && sp->do_auto_asconf) || (!val && !sp->do_auto_asconf))
  3168. return 0;
  3169. spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock);
  3170. if (val == 0 && sp->do_auto_asconf) {
  3171. list_del(&sp->auto_asconf_list);
  3172. sp->do_auto_asconf = 0;
  3173. } else if (val && !sp->do_auto_asconf) {
  3174. list_add_tail(&sp->auto_asconf_list,
  3175. &sock_net(sk)->sctp.auto_asconf_splist);
  3176. sp->do_auto_asconf = 1;
  3177. }
  3178. spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock);
  3179. return 0;
  3180. }
  3181. /*
  3182. * SCTP_PEER_ADDR_THLDS
  3183. *
  3184. * This option allows us to alter the partially failed threshold for one or all
  3185. * transports in an association. See Section 6.1 of:
  3186. * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
  3187. */
  3188. static int sctp_setsockopt_paddr_thresholds(struct sock *sk,
  3189. char __user *optval,
  3190. unsigned int optlen)
  3191. {
  3192. struct sctp_paddrthlds val;
  3193. struct sctp_transport *trans;
  3194. struct sctp_association *asoc;
  3195. if (optlen < sizeof(struct sctp_paddrthlds))
  3196. return -EINVAL;
  3197. if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval,
  3198. sizeof(struct sctp_paddrthlds)))
  3199. return -EFAULT;
  3200. if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
  3201. asoc = sctp_id2assoc(sk, val.spt_assoc_id);
  3202. if (!asoc)
  3203. return -ENOENT;
  3204. list_for_each_entry(trans, &asoc->peer.transport_addr_list,
  3205. transports) {
  3206. if (val.spt_pathmaxrxt)
  3207. trans->pathmaxrxt = val.spt_pathmaxrxt;
  3208. trans->pf_retrans = val.spt_pathpfthld;
  3209. }
  3210. if (val.spt_pathmaxrxt)
  3211. asoc->pathmaxrxt = val.spt_pathmaxrxt;
  3212. asoc->pf_retrans = val.spt_pathpfthld;
  3213. } else {
  3214. trans = sctp_addr_id2transport(sk, &val.spt_address,
  3215. val.spt_assoc_id);
  3216. if (!trans)
  3217. return -ENOENT;
  3218. if (val.spt_pathmaxrxt)
  3219. trans->pathmaxrxt = val.spt_pathmaxrxt;
  3220. trans->pf_retrans = val.spt_pathpfthld;
  3221. }
  3222. return 0;
  3223. }
  3224. static int sctp_setsockopt_recvrcvinfo(struct sock *sk,
  3225. char __user *optval,
  3226. unsigned int optlen)
  3227. {
  3228. int val;
  3229. if (optlen < sizeof(int))
  3230. return -EINVAL;
  3231. if (get_user(val, (int __user *) optval))
  3232. return -EFAULT;
  3233. sctp_sk(sk)->recvrcvinfo = (val == 0) ? 0 : 1;
  3234. return 0;
  3235. }
  3236. static int sctp_setsockopt_recvnxtinfo(struct sock *sk,
  3237. char __user *optval,
  3238. unsigned int optlen)
  3239. {
  3240. int val;
  3241. if (optlen < sizeof(int))
  3242. return -EINVAL;
  3243. if (get_user(val, (int __user *) optval))
  3244. return -EFAULT;
  3245. sctp_sk(sk)->recvnxtinfo = (val == 0) ? 0 : 1;
  3246. return 0;
  3247. }
  3248. static int sctp_setsockopt_pr_supported(struct sock *sk,
  3249. char __user *optval,
  3250. unsigned int optlen)
  3251. {
  3252. struct sctp_assoc_value params;
  3253. struct sctp_association *asoc;
  3254. int retval = -EINVAL;
  3255. if (optlen != sizeof(params))
  3256. goto out;
  3257. if (copy_from_user(&params, optval, optlen)) {
  3258. retval = -EFAULT;
  3259. goto out;
  3260. }
  3261. asoc = sctp_id2assoc(sk, params.assoc_id);
  3262. if (asoc) {
  3263. asoc->prsctp_enable = !!params.assoc_value;
  3264. } else if (!params.assoc_id) {
  3265. struct sctp_sock *sp = sctp_sk(sk);
  3266. sp->ep->prsctp_enable = !!params.assoc_value;
  3267. } else {
  3268. goto out;
  3269. }
  3270. retval = 0;
  3271. out:
  3272. return retval;
  3273. }
  3274. static int sctp_setsockopt_default_prinfo(struct sock *sk,
  3275. char __user *optval,
  3276. unsigned int optlen)
  3277. {
  3278. struct sctp_default_prinfo info;
  3279. struct sctp_association *asoc;
  3280. int retval = -EINVAL;
  3281. if (optlen != sizeof(info))
  3282. goto out;
  3283. if (copy_from_user(&info, optval, sizeof(info))) {
  3284. retval = -EFAULT;
  3285. goto out;
  3286. }
  3287. if (info.pr_policy & ~SCTP_PR_SCTP_MASK)
  3288. goto out;
  3289. if (info.pr_policy == SCTP_PR_SCTP_NONE)
  3290. info.pr_value = 0;
  3291. asoc = sctp_id2assoc(sk, info.pr_assoc_id);
  3292. if (asoc) {
  3293. SCTP_PR_SET_POLICY(asoc->default_flags, info.pr_policy);
  3294. asoc->default_timetolive = info.pr_value;
  3295. } else if (!info.pr_assoc_id) {
  3296. struct sctp_sock *sp = sctp_sk(sk);
  3297. SCTP_PR_SET_POLICY(sp->default_flags, info.pr_policy);
  3298. sp->default_timetolive = info.pr_value;
  3299. } else {
  3300. goto out;
  3301. }
  3302. retval = 0;
  3303. out:
  3304. return retval;
  3305. }
  3306. static int sctp_setsockopt_reconfig_supported(struct sock *sk,
  3307. char __user *optval,
  3308. unsigned int optlen)
  3309. {
  3310. struct sctp_assoc_value params;
  3311. struct sctp_association *asoc;
  3312. int retval = -EINVAL;
  3313. if (optlen != sizeof(params))
  3314. goto out;
  3315. if (copy_from_user(&params, optval, optlen)) {
  3316. retval = -EFAULT;
  3317. goto out;
  3318. }
  3319. asoc = sctp_id2assoc(sk, params.assoc_id);
  3320. if (asoc) {
  3321. asoc->reconf_enable = !!params.assoc_value;
  3322. } else if (!params.assoc_id) {
  3323. struct sctp_sock *sp = sctp_sk(sk);
  3324. sp->ep->reconf_enable = !!params.assoc_value;
  3325. } else {
  3326. goto out;
  3327. }
  3328. retval = 0;
  3329. out:
  3330. return retval;
  3331. }
  3332. static int sctp_setsockopt_enable_strreset(struct sock *sk,
  3333. char __user *optval,
  3334. unsigned int optlen)
  3335. {
  3336. struct sctp_assoc_value params;
  3337. struct sctp_association *asoc;
  3338. int retval = -EINVAL;
  3339. if (optlen != sizeof(params))
  3340. goto out;
  3341. if (copy_from_user(&params, optval, optlen)) {
  3342. retval = -EFAULT;
  3343. goto out;
  3344. }
  3345. if (params.assoc_value & (~SCTP_ENABLE_STRRESET_MASK))
  3346. goto out;
  3347. asoc = sctp_id2assoc(sk, params.assoc_id);
  3348. if (asoc) {
  3349. asoc->strreset_enable = params.assoc_value;
  3350. } else if (!params.assoc_id) {
  3351. struct sctp_sock *sp = sctp_sk(sk);
  3352. sp->ep->strreset_enable = params.assoc_value;
  3353. } else {
  3354. goto out;
  3355. }
  3356. retval = 0;
  3357. out:
  3358. return retval;
  3359. }
  3360. static int sctp_setsockopt_reset_streams(struct sock *sk,
  3361. char __user *optval,
  3362. unsigned int optlen)
  3363. {
  3364. struct sctp_reset_streams *params;
  3365. struct sctp_association *asoc;
  3366. int retval = -EINVAL;
  3367. if (optlen < sizeof(*params))
  3368. return -EINVAL;
  3369. /* srs_number_streams is u16, so optlen can't be bigger than this. */
  3370. optlen = min_t(unsigned int, optlen, USHRT_MAX +
  3371. sizeof(__u16) * sizeof(*params));
  3372. params = memdup_user(optval, optlen);
  3373. if (IS_ERR(params))
  3374. return PTR_ERR(params);
  3375. if (params->srs_number_streams * sizeof(__u16) >
  3376. optlen - sizeof(*params))
  3377. goto out;
  3378. asoc = sctp_id2assoc(sk, params->srs_assoc_id);
  3379. if (!asoc)
  3380. goto out;
  3381. retval = sctp_send_reset_streams(asoc, params);
  3382. out:
  3383. kfree(params);
  3384. return retval;
  3385. }
  3386. static int sctp_setsockopt_reset_assoc(struct sock *sk,
  3387. char __user *optval,
  3388. unsigned int optlen)
  3389. {
  3390. struct sctp_association *asoc;
  3391. sctp_assoc_t associd;
  3392. int retval = -EINVAL;
  3393. if (optlen != sizeof(associd))
  3394. goto out;
  3395. if (copy_from_user(&associd, optval, optlen)) {
  3396. retval = -EFAULT;
  3397. goto out;
  3398. }
  3399. asoc = sctp_id2assoc(sk, associd);
  3400. if (!asoc)
  3401. goto out;
  3402. retval = sctp_send_reset_assoc(asoc);
  3403. out:
  3404. return retval;
  3405. }
  3406. static int sctp_setsockopt_add_streams(struct sock *sk,
  3407. char __user *optval,
  3408. unsigned int optlen)
  3409. {
  3410. struct sctp_association *asoc;
  3411. struct sctp_add_streams params;
  3412. int retval = -EINVAL;
  3413. if (optlen != sizeof(params))
  3414. goto out;
  3415. if (copy_from_user(&params, optval, optlen)) {
  3416. retval = -EFAULT;
  3417. goto out;
  3418. }
  3419. asoc = sctp_id2assoc(sk, params.sas_assoc_id);
  3420. if (!asoc)
  3421. goto out;
  3422. retval = sctp_send_add_streams(asoc, &params);
  3423. out:
  3424. return retval;
  3425. }
  3426. static int sctp_setsockopt_scheduler(struct sock *sk,
  3427. char __user *optval,
  3428. unsigned int optlen)
  3429. {
  3430. struct sctp_association *asoc;
  3431. struct sctp_assoc_value params;
  3432. int retval = -EINVAL;
  3433. if (optlen < sizeof(params))
  3434. goto out;
  3435. optlen = sizeof(params);
  3436. if (copy_from_user(&params, optval, optlen)) {
  3437. retval = -EFAULT;
  3438. goto out;
  3439. }
  3440. if (params.assoc_value > SCTP_SS_MAX)
  3441. goto out;
  3442. asoc = sctp_id2assoc(sk, params.assoc_id);
  3443. if (!asoc)
  3444. goto out;
  3445. retval = sctp_sched_set_sched(asoc, params.assoc_value);
  3446. out:
  3447. return retval;
  3448. }
  3449. static int sctp_setsockopt_scheduler_value(struct sock *sk,
  3450. char __user *optval,
  3451. unsigned int optlen)
  3452. {
  3453. struct sctp_association *asoc;
  3454. struct sctp_stream_value params;
  3455. int retval = -EINVAL;
  3456. if (optlen < sizeof(params))
  3457. goto out;
  3458. optlen = sizeof(params);
  3459. if (copy_from_user(&params, optval, optlen)) {
  3460. retval = -EFAULT;
  3461. goto out;
  3462. }
  3463. asoc = sctp_id2assoc(sk, params.assoc_id);
  3464. if (!asoc)
  3465. goto out;
  3466. retval = sctp_sched_set_value(asoc, params.stream_id,
  3467. params.stream_value, GFP_KERNEL);
  3468. out:
  3469. return retval;
  3470. }
  3471. static int sctp_setsockopt_interleaving_supported(struct sock *sk,
  3472. char __user *optval,
  3473. unsigned int optlen)
  3474. {
  3475. struct sctp_sock *sp = sctp_sk(sk);
  3476. struct net *net = sock_net(sk);
  3477. struct sctp_assoc_value params;
  3478. int retval = -EINVAL;
  3479. if (optlen < sizeof(params))
  3480. goto out;
  3481. optlen = sizeof(params);
  3482. if (copy_from_user(&params, optval, optlen)) {
  3483. retval = -EFAULT;
  3484. goto out;
  3485. }
  3486. if (params.assoc_id)
  3487. goto out;
  3488. if (!net->sctp.intl_enable || !sp->frag_interleave) {
  3489. retval = -EPERM;
  3490. goto out;
  3491. }
  3492. sp->strm_interleave = !!params.assoc_value;
  3493. retval = 0;
  3494. out:
  3495. return retval;
  3496. }
  3497. /* API 6.2 setsockopt(), getsockopt()
  3498. *
  3499. * Applications use setsockopt() and getsockopt() to set or retrieve
  3500. * socket options. Socket options are used to change the default
  3501. * behavior of sockets calls. They are described in Section 7.
  3502. *
  3503. * The syntax is:
  3504. *
  3505. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  3506. * int __user *optlen);
  3507. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  3508. * int optlen);
  3509. *
  3510. * sd - the socket descript.
  3511. * level - set to IPPROTO_SCTP for all SCTP options.
  3512. * optname - the option name.
  3513. * optval - the buffer to store the value of the option.
  3514. * optlen - the size of the buffer.
  3515. */
  3516. static int sctp_setsockopt(struct sock *sk, int level, int optname,
  3517. char __user *optval, unsigned int optlen)
  3518. {
  3519. int retval = 0;
  3520. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  3521. /* I can hardly begin to describe how wrong this is. This is
  3522. * so broken as to be worse than useless. The API draft
  3523. * REALLY is NOT helpful here... I am not convinced that the
  3524. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  3525. * are at all well-founded.
  3526. */
  3527. if (level != SOL_SCTP) {
  3528. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3529. retval = af->setsockopt(sk, level, optname, optval, optlen);
  3530. goto out_nounlock;
  3531. }
  3532. lock_sock(sk);
  3533. switch (optname) {
  3534. case SCTP_SOCKOPT_BINDX_ADD:
  3535. /* 'optlen' is the size of the addresses buffer. */
  3536. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  3537. optlen, SCTP_BINDX_ADD_ADDR);
  3538. break;
  3539. case SCTP_SOCKOPT_BINDX_REM:
  3540. /* 'optlen' is the size of the addresses buffer. */
  3541. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  3542. optlen, SCTP_BINDX_REM_ADDR);
  3543. break;
  3544. case SCTP_SOCKOPT_CONNECTX_OLD:
  3545. /* 'optlen' is the size of the addresses buffer. */
  3546. retval = sctp_setsockopt_connectx_old(sk,
  3547. (struct sockaddr __user *)optval,
  3548. optlen);
  3549. break;
  3550. case SCTP_SOCKOPT_CONNECTX:
  3551. /* 'optlen' is the size of the addresses buffer. */
  3552. retval = sctp_setsockopt_connectx(sk,
  3553. (struct sockaddr __user *)optval,
  3554. optlen);
  3555. break;
  3556. case SCTP_DISABLE_FRAGMENTS:
  3557. retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
  3558. break;
  3559. case SCTP_EVENTS:
  3560. retval = sctp_setsockopt_events(sk, optval, optlen);
  3561. break;
  3562. case SCTP_AUTOCLOSE:
  3563. retval = sctp_setsockopt_autoclose(sk, optval, optlen);
  3564. break;
  3565. case SCTP_PEER_ADDR_PARAMS:
  3566. retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
  3567. break;
  3568. case SCTP_DELAYED_SACK:
  3569. retval = sctp_setsockopt_delayed_ack(sk, optval, optlen);
  3570. break;
  3571. case SCTP_PARTIAL_DELIVERY_POINT:
  3572. retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen);
  3573. break;
  3574. case SCTP_INITMSG:
  3575. retval = sctp_setsockopt_initmsg(sk, optval, optlen);
  3576. break;
  3577. case SCTP_DEFAULT_SEND_PARAM:
  3578. retval = sctp_setsockopt_default_send_param(sk, optval,
  3579. optlen);
  3580. break;
  3581. case SCTP_DEFAULT_SNDINFO:
  3582. retval = sctp_setsockopt_default_sndinfo(sk, optval, optlen);
  3583. break;
  3584. case SCTP_PRIMARY_ADDR:
  3585. retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
  3586. break;
  3587. case SCTP_SET_PEER_PRIMARY_ADDR:
  3588. retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
  3589. break;
  3590. case SCTP_NODELAY:
  3591. retval = sctp_setsockopt_nodelay(sk, optval, optlen);
  3592. break;
  3593. case SCTP_RTOINFO:
  3594. retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
  3595. break;
  3596. case SCTP_ASSOCINFO:
  3597. retval = sctp_setsockopt_associnfo(sk, optval, optlen);
  3598. break;
  3599. case SCTP_I_WANT_MAPPED_V4_ADDR:
  3600. retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
  3601. break;
  3602. case SCTP_MAXSEG:
  3603. retval = sctp_setsockopt_maxseg(sk, optval, optlen);
  3604. break;
  3605. case SCTP_ADAPTATION_LAYER:
  3606. retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen);
  3607. break;
  3608. case SCTP_CONTEXT:
  3609. retval = sctp_setsockopt_context(sk, optval, optlen);
  3610. break;
  3611. case SCTP_FRAGMENT_INTERLEAVE:
  3612. retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen);
  3613. break;
  3614. case SCTP_MAX_BURST:
  3615. retval = sctp_setsockopt_maxburst(sk, optval, optlen);
  3616. break;
  3617. case SCTP_AUTH_CHUNK:
  3618. retval = sctp_setsockopt_auth_chunk(sk, optval, optlen);
  3619. break;
  3620. case SCTP_HMAC_IDENT:
  3621. retval = sctp_setsockopt_hmac_ident(sk, optval, optlen);
  3622. break;
  3623. case SCTP_AUTH_KEY:
  3624. retval = sctp_setsockopt_auth_key(sk, optval, optlen);
  3625. break;
  3626. case SCTP_AUTH_ACTIVE_KEY:
  3627. retval = sctp_setsockopt_active_key(sk, optval, optlen);
  3628. break;
  3629. case SCTP_AUTH_DELETE_KEY:
  3630. retval = sctp_setsockopt_del_key(sk, optval, optlen);
  3631. break;
  3632. case SCTP_AUTO_ASCONF:
  3633. retval = sctp_setsockopt_auto_asconf(sk, optval, optlen);
  3634. break;
  3635. case SCTP_PEER_ADDR_THLDS:
  3636. retval = sctp_setsockopt_paddr_thresholds(sk, optval, optlen);
  3637. break;
  3638. case SCTP_RECVRCVINFO:
  3639. retval = sctp_setsockopt_recvrcvinfo(sk, optval, optlen);
  3640. break;
  3641. case SCTP_RECVNXTINFO:
  3642. retval = sctp_setsockopt_recvnxtinfo(sk, optval, optlen);
  3643. break;
  3644. case SCTP_PR_SUPPORTED:
  3645. retval = sctp_setsockopt_pr_supported(sk, optval, optlen);
  3646. break;
  3647. case SCTP_DEFAULT_PRINFO:
  3648. retval = sctp_setsockopt_default_prinfo(sk, optval, optlen);
  3649. break;
  3650. case SCTP_RECONFIG_SUPPORTED:
  3651. retval = sctp_setsockopt_reconfig_supported(sk, optval, optlen);
  3652. break;
  3653. case SCTP_ENABLE_STREAM_RESET:
  3654. retval = sctp_setsockopt_enable_strreset(sk, optval, optlen);
  3655. break;
  3656. case SCTP_RESET_STREAMS:
  3657. retval = sctp_setsockopt_reset_streams(sk, optval, optlen);
  3658. break;
  3659. case SCTP_RESET_ASSOC:
  3660. retval = sctp_setsockopt_reset_assoc(sk, optval, optlen);
  3661. break;
  3662. case SCTP_ADD_STREAMS:
  3663. retval = sctp_setsockopt_add_streams(sk, optval, optlen);
  3664. break;
  3665. case SCTP_STREAM_SCHEDULER:
  3666. retval = sctp_setsockopt_scheduler(sk, optval, optlen);
  3667. break;
  3668. case SCTP_STREAM_SCHEDULER_VALUE:
  3669. retval = sctp_setsockopt_scheduler_value(sk, optval, optlen);
  3670. break;
  3671. case SCTP_INTERLEAVING_SUPPORTED:
  3672. retval = sctp_setsockopt_interleaving_supported(sk, optval,
  3673. optlen);
  3674. break;
  3675. default:
  3676. retval = -ENOPROTOOPT;
  3677. break;
  3678. }
  3679. release_sock(sk);
  3680. out_nounlock:
  3681. return retval;
  3682. }
  3683. /* API 3.1.6 connect() - UDP Style Syntax
  3684. *
  3685. * An application may use the connect() call in the UDP model to initiate an
  3686. * association without sending data.
  3687. *
  3688. * The syntax is:
  3689. *
  3690. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  3691. *
  3692. * sd: the socket descriptor to have a new association added to.
  3693. *
  3694. * nam: the address structure (either struct sockaddr_in or struct
  3695. * sockaddr_in6 defined in RFC2553 [7]).
  3696. *
  3697. * len: the size of the address.
  3698. */
  3699. static int sctp_connect(struct sock *sk, struct sockaddr *addr,
  3700. int addr_len)
  3701. {
  3702. int err = 0;
  3703. struct sctp_af *af;
  3704. lock_sock(sk);
  3705. pr_debug("%s: sk:%p, sockaddr:%p, addr_len:%d\n", __func__, sk,
  3706. addr, addr_len);
  3707. /* Validate addr_len before calling common connect/connectx routine. */
  3708. af = sctp_get_af_specific(addr->sa_family);
  3709. if (!af || addr_len < af->sockaddr_len) {
  3710. err = -EINVAL;
  3711. } else {
  3712. /* Pass correct addr len to common routine (so it knows there
  3713. * is only one address being passed.
  3714. */
  3715. err = __sctp_connect(sk, addr, af->sockaddr_len, NULL);
  3716. }
  3717. release_sock(sk);
  3718. return err;
  3719. }
  3720. /* FIXME: Write comments. */
  3721. static int sctp_disconnect(struct sock *sk, int flags)
  3722. {
  3723. return -EOPNOTSUPP; /* STUB */
  3724. }
  3725. /* 4.1.4 accept() - TCP Style Syntax
  3726. *
  3727. * Applications use accept() call to remove an established SCTP
  3728. * association from the accept queue of the endpoint. A new socket
  3729. * descriptor will be returned from accept() to represent the newly
  3730. * formed association.
  3731. */
  3732. static struct sock *sctp_accept(struct sock *sk, int flags, int *err, bool kern)
  3733. {
  3734. struct sctp_sock *sp;
  3735. struct sctp_endpoint *ep;
  3736. struct sock *newsk = NULL;
  3737. struct sctp_association *asoc;
  3738. long timeo;
  3739. int error = 0;
  3740. lock_sock(sk);
  3741. sp = sctp_sk(sk);
  3742. ep = sp->ep;
  3743. if (!sctp_style(sk, TCP)) {
  3744. error = -EOPNOTSUPP;
  3745. goto out;
  3746. }
  3747. if (!sctp_sstate(sk, LISTENING)) {
  3748. error = -EINVAL;
  3749. goto out;
  3750. }
  3751. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  3752. error = sctp_wait_for_accept(sk, timeo);
  3753. if (error)
  3754. goto out;
  3755. /* We treat the list of associations on the endpoint as the accept
  3756. * queue and pick the first association on the list.
  3757. */
  3758. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  3759. newsk = sp->pf->create_accept_sk(sk, asoc, kern);
  3760. if (!newsk) {
  3761. error = -ENOMEM;
  3762. goto out;
  3763. }
  3764. /* Populate the fields of the newsk from the oldsk and migrate the
  3765. * asoc to the newsk.
  3766. */
  3767. sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  3768. out:
  3769. release_sock(sk);
  3770. *err = error;
  3771. return newsk;
  3772. }
  3773. /* The SCTP ioctl handler. */
  3774. static int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  3775. {
  3776. int rc = -ENOTCONN;
  3777. lock_sock(sk);
  3778. /*
  3779. * SEQPACKET-style sockets in LISTENING state are valid, for
  3780. * SCTP, so only discard TCP-style sockets in LISTENING state.
  3781. */
  3782. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  3783. goto out;
  3784. switch (cmd) {
  3785. case SIOCINQ: {
  3786. struct sk_buff *skb;
  3787. unsigned int amount = 0;
  3788. skb = skb_peek(&sk->sk_receive_queue);
  3789. if (skb != NULL) {
  3790. /*
  3791. * We will only return the amount of this packet since
  3792. * that is all that will be read.
  3793. */
  3794. amount = skb->len;
  3795. }
  3796. rc = put_user(amount, (int __user *)arg);
  3797. break;
  3798. }
  3799. default:
  3800. rc = -ENOIOCTLCMD;
  3801. break;
  3802. }
  3803. out:
  3804. release_sock(sk);
  3805. return rc;
  3806. }
  3807. /* This is the function which gets called during socket creation to
  3808. * initialized the SCTP-specific portion of the sock.
  3809. * The sock structure should already be zero-filled memory.
  3810. */
  3811. static int sctp_init_sock(struct sock *sk)
  3812. {
  3813. struct net *net = sock_net(sk);
  3814. struct sctp_sock *sp;
  3815. pr_debug("%s: sk:%p\n", __func__, sk);
  3816. sp = sctp_sk(sk);
  3817. /* Initialize the SCTP per socket area. */
  3818. switch (sk->sk_type) {
  3819. case SOCK_SEQPACKET:
  3820. sp->type = SCTP_SOCKET_UDP;
  3821. break;
  3822. case SOCK_STREAM:
  3823. sp->type = SCTP_SOCKET_TCP;
  3824. break;
  3825. default:
  3826. return -ESOCKTNOSUPPORT;
  3827. }
  3828. sk->sk_gso_type = SKB_GSO_SCTP;
  3829. /* Initialize default send parameters. These parameters can be
  3830. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  3831. */
  3832. sp->default_stream = 0;
  3833. sp->default_ppid = 0;
  3834. sp->default_flags = 0;
  3835. sp->default_context = 0;
  3836. sp->default_timetolive = 0;
  3837. sp->default_rcv_context = 0;
  3838. sp->max_burst = net->sctp.max_burst;
  3839. sp->sctp_hmac_alg = net->sctp.sctp_hmac_alg;
  3840. /* Initialize default setup parameters. These parameters
  3841. * can be modified with the SCTP_INITMSG socket option or
  3842. * overridden by the SCTP_INIT CMSG.
  3843. */
  3844. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  3845. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  3846. sp->initmsg.sinit_max_attempts = net->sctp.max_retrans_init;
  3847. sp->initmsg.sinit_max_init_timeo = net->sctp.rto_max;
  3848. /* Initialize default RTO related parameters. These parameters can
  3849. * be modified for with the SCTP_RTOINFO socket option.
  3850. */
  3851. sp->rtoinfo.srto_initial = net->sctp.rto_initial;
  3852. sp->rtoinfo.srto_max = net->sctp.rto_max;
  3853. sp->rtoinfo.srto_min = net->sctp.rto_min;
  3854. /* Initialize default association related parameters. These parameters
  3855. * can be modified with the SCTP_ASSOCINFO socket option.
  3856. */
  3857. sp->assocparams.sasoc_asocmaxrxt = net->sctp.max_retrans_association;
  3858. sp->assocparams.sasoc_number_peer_destinations = 0;
  3859. sp->assocparams.sasoc_peer_rwnd = 0;
  3860. sp->assocparams.sasoc_local_rwnd = 0;
  3861. sp->assocparams.sasoc_cookie_life = net->sctp.valid_cookie_life;
  3862. /* Initialize default event subscriptions. By default, all the
  3863. * options are off.
  3864. */
  3865. memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
  3866. /* Default Peer Address Parameters. These defaults can
  3867. * be modified via SCTP_PEER_ADDR_PARAMS
  3868. */
  3869. sp->hbinterval = net->sctp.hb_interval;
  3870. sp->pathmaxrxt = net->sctp.max_retrans_path;
  3871. sp->pathmtu = 0; /* allow default discovery */
  3872. sp->sackdelay = net->sctp.sack_timeout;
  3873. sp->sackfreq = 2;
  3874. sp->param_flags = SPP_HB_ENABLE |
  3875. SPP_PMTUD_ENABLE |
  3876. SPP_SACKDELAY_ENABLE;
  3877. /* If enabled no SCTP message fragmentation will be performed.
  3878. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  3879. */
  3880. sp->disable_fragments = 0;
  3881. /* Enable Nagle algorithm by default. */
  3882. sp->nodelay = 0;
  3883. sp->recvrcvinfo = 0;
  3884. sp->recvnxtinfo = 0;
  3885. /* Enable by default. */
  3886. sp->v4mapped = 1;
  3887. /* Auto-close idle associations after the configured
  3888. * number of seconds. A value of 0 disables this
  3889. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  3890. * for UDP-style sockets only.
  3891. */
  3892. sp->autoclose = 0;
  3893. /* User specified fragmentation limit. */
  3894. sp->user_frag = 0;
  3895. sp->adaptation_ind = 0;
  3896. sp->pf = sctp_get_pf_specific(sk->sk_family);
  3897. /* Control variables for partial data delivery. */
  3898. atomic_set(&sp->pd_mode, 0);
  3899. skb_queue_head_init(&sp->pd_lobby);
  3900. sp->frag_interleave = 0;
  3901. /* Create a per socket endpoint structure. Even if we
  3902. * change the data structure relationships, this may still
  3903. * be useful for storing pre-connect address information.
  3904. */
  3905. sp->ep = sctp_endpoint_new(sk, GFP_KERNEL);
  3906. if (!sp->ep)
  3907. return -ENOMEM;
  3908. sp->hmac = NULL;
  3909. sk->sk_destruct = sctp_destruct_sock;
  3910. SCTP_DBG_OBJCNT_INC(sock);
  3911. local_bh_disable();
  3912. sk_sockets_allocated_inc(sk);
  3913. sock_prot_inuse_add(net, sk->sk_prot, 1);
  3914. /* Nothing can fail after this block, otherwise
  3915. * sctp_destroy_sock() will be called without addr_wq_lock held
  3916. */
  3917. if (net->sctp.default_auto_asconf) {
  3918. spin_lock(&sock_net(sk)->sctp.addr_wq_lock);
  3919. list_add_tail(&sp->auto_asconf_list,
  3920. &net->sctp.auto_asconf_splist);
  3921. sp->do_auto_asconf = 1;
  3922. spin_unlock(&sock_net(sk)->sctp.addr_wq_lock);
  3923. } else {
  3924. sp->do_auto_asconf = 0;
  3925. }
  3926. local_bh_enable();
  3927. return 0;
  3928. }
  3929. /* Cleanup any SCTP per socket resources. Must be called with
  3930. * sock_net(sk)->sctp.addr_wq_lock held if sp->do_auto_asconf is true
  3931. */
  3932. static void sctp_destroy_sock(struct sock *sk)
  3933. {
  3934. struct sctp_sock *sp;
  3935. pr_debug("%s: sk:%p\n", __func__, sk);
  3936. /* Release our hold on the endpoint. */
  3937. sp = sctp_sk(sk);
  3938. /* This could happen during socket init, thus we bail out
  3939. * early, since the rest of the below is not setup either.
  3940. */
  3941. if (sp->ep == NULL)
  3942. return;
  3943. if (sp->do_auto_asconf) {
  3944. sp->do_auto_asconf = 0;
  3945. list_del(&sp->auto_asconf_list);
  3946. }
  3947. sctp_endpoint_free(sp->ep);
  3948. local_bh_disable();
  3949. sk_sockets_allocated_dec(sk);
  3950. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  3951. local_bh_enable();
  3952. }
  3953. /* Triggered when there are no references on the socket anymore */
  3954. static void sctp_destruct_sock(struct sock *sk)
  3955. {
  3956. struct sctp_sock *sp = sctp_sk(sk);
  3957. /* Free up the HMAC transform. */
  3958. crypto_free_shash(sp->hmac);
  3959. inet_sock_destruct(sk);
  3960. }
  3961. /* API 4.1.7 shutdown() - TCP Style Syntax
  3962. * int shutdown(int socket, int how);
  3963. *
  3964. * sd - the socket descriptor of the association to be closed.
  3965. * how - Specifies the type of shutdown. The values are
  3966. * as follows:
  3967. * SHUT_RD
  3968. * Disables further receive operations. No SCTP
  3969. * protocol action is taken.
  3970. * SHUT_WR
  3971. * Disables further send operations, and initiates
  3972. * the SCTP shutdown sequence.
  3973. * SHUT_RDWR
  3974. * Disables further send and receive operations
  3975. * and initiates the SCTP shutdown sequence.
  3976. */
  3977. static void sctp_shutdown(struct sock *sk, int how)
  3978. {
  3979. struct net *net = sock_net(sk);
  3980. struct sctp_endpoint *ep;
  3981. if (!sctp_style(sk, TCP))
  3982. return;
  3983. ep = sctp_sk(sk)->ep;
  3984. if (how & SEND_SHUTDOWN && !list_empty(&ep->asocs)) {
  3985. struct sctp_association *asoc;
  3986. inet_sk_set_state(sk, SCTP_SS_CLOSING);
  3987. asoc = list_entry(ep->asocs.next,
  3988. struct sctp_association, asocs);
  3989. sctp_primitive_SHUTDOWN(net, asoc, NULL);
  3990. }
  3991. }
  3992. int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
  3993. struct sctp_info *info)
  3994. {
  3995. struct sctp_transport *prim;
  3996. struct list_head *pos;
  3997. int mask;
  3998. memset(info, 0, sizeof(*info));
  3999. if (!asoc) {
  4000. struct sctp_sock *sp = sctp_sk(sk);
  4001. info->sctpi_s_autoclose = sp->autoclose;
  4002. info->sctpi_s_adaptation_ind = sp->adaptation_ind;
  4003. info->sctpi_s_pd_point = sp->pd_point;
  4004. info->sctpi_s_nodelay = sp->nodelay;
  4005. info->sctpi_s_disable_fragments = sp->disable_fragments;
  4006. info->sctpi_s_v4mapped = sp->v4mapped;
  4007. info->sctpi_s_frag_interleave = sp->frag_interleave;
  4008. info->sctpi_s_type = sp->type;
  4009. return 0;
  4010. }
  4011. info->sctpi_tag = asoc->c.my_vtag;
  4012. info->sctpi_state = asoc->state;
  4013. info->sctpi_rwnd = asoc->a_rwnd;
  4014. info->sctpi_unackdata = asoc->unack_data;
  4015. info->sctpi_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  4016. info->sctpi_instrms = asoc->stream.incnt;
  4017. info->sctpi_outstrms = asoc->stream.outcnt;
  4018. list_for_each(pos, &asoc->base.inqueue.in_chunk_list)
  4019. info->sctpi_inqueue++;
  4020. list_for_each(pos, &asoc->outqueue.out_chunk_list)
  4021. info->sctpi_outqueue++;
  4022. info->sctpi_overall_error = asoc->overall_error_count;
  4023. info->sctpi_max_burst = asoc->max_burst;
  4024. info->sctpi_maxseg = asoc->frag_point;
  4025. info->sctpi_peer_rwnd = asoc->peer.rwnd;
  4026. info->sctpi_peer_tag = asoc->c.peer_vtag;
  4027. mask = asoc->peer.ecn_capable << 1;
  4028. mask = (mask | asoc->peer.ipv4_address) << 1;
  4029. mask = (mask | asoc->peer.ipv6_address) << 1;
  4030. mask = (mask | asoc->peer.hostname_address) << 1;
  4031. mask = (mask | asoc->peer.asconf_capable) << 1;
  4032. mask = (mask | asoc->peer.prsctp_capable) << 1;
  4033. mask = (mask | asoc->peer.auth_capable);
  4034. info->sctpi_peer_capable = mask;
  4035. mask = asoc->peer.sack_needed << 1;
  4036. mask = (mask | asoc->peer.sack_generation) << 1;
  4037. mask = (mask | asoc->peer.zero_window_announced);
  4038. info->sctpi_peer_sack = mask;
  4039. info->sctpi_isacks = asoc->stats.isacks;
  4040. info->sctpi_osacks = asoc->stats.osacks;
  4041. info->sctpi_opackets = asoc->stats.opackets;
  4042. info->sctpi_ipackets = asoc->stats.ipackets;
  4043. info->sctpi_rtxchunks = asoc->stats.rtxchunks;
  4044. info->sctpi_outofseqtsns = asoc->stats.outofseqtsns;
  4045. info->sctpi_idupchunks = asoc->stats.idupchunks;
  4046. info->sctpi_gapcnt = asoc->stats.gapcnt;
  4047. info->sctpi_ouodchunks = asoc->stats.ouodchunks;
  4048. info->sctpi_iuodchunks = asoc->stats.iuodchunks;
  4049. info->sctpi_oodchunks = asoc->stats.oodchunks;
  4050. info->sctpi_iodchunks = asoc->stats.iodchunks;
  4051. info->sctpi_octrlchunks = asoc->stats.octrlchunks;
  4052. info->sctpi_ictrlchunks = asoc->stats.ictrlchunks;
  4053. prim = asoc->peer.primary_path;
  4054. memcpy(&info->sctpi_p_address, &prim->ipaddr, sizeof(prim->ipaddr));
  4055. info->sctpi_p_state = prim->state;
  4056. info->sctpi_p_cwnd = prim->cwnd;
  4057. info->sctpi_p_srtt = prim->srtt;
  4058. info->sctpi_p_rto = jiffies_to_msecs(prim->rto);
  4059. info->sctpi_p_hbinterval = prim->hbinterval;
  4060. info->sctpi_p_pathmaxrxt = prim->pathmaxrxt;
  4061. info->sctpi_p_sackdelay = jiffies_to_msecs(prim->sackdelay);
  4062. info->sctpi_p_ssthresh = prim->ssthresh;
  4063. info->sctpi_p_partial_bytes_acked = prim->partial_bytes_acked;
  4064. info->sctpi_p_flight_size = prim->flight_size;
  4065. info->sctpi_p_error = prim->error_count;
  4066. return 0;
  4067. }
  4068. EXPORT_SYMBOL_GPL(sctp_get_sctp_info);
  4069. /* use callback to avoid exporting the core structure */
  4070. void sctp_transport_walk_start(struct rhashtable_iter *iter)
  4071. {
  4072. rhltable_walk_enter(&sctp_transport_hashtable, iter);
  4073. rhashtable_walk_start(iter);
  4074. }
  4075. void sctp_transport_walk_stop(struct rhashtable_iter *iter)
  4076. {
  4077. rhashtable_walk_stop(iter);
  4078. rhashtable_walk_exit(iter);
  4079. }
  4080. struct sctp_transport *sctp_transport_get_next(struct net *net,
  4081. struct rhashtable_iter *iter)
  4082. {
  4083. struct sctp_transport *t;
  4084. t = rhashtable_walk_next(iter);
  4085. for (; t; t = rhashtable_walk_next(iter)) {
  4086. if (IS_ERR(t)) {
  4087. if (PTR_ERR(t) == -EAGAIN)
  4088. continue;
  4089. break;
  4090. }
  4091. if (net_eq(sock_net(t->asoc->base.sk), net) &&
  4092. t->asoc->peer.primary_path == t)
  4093. break;
  4094. }
  4095. return t;
  4096. }
  4097. struct sctp_transport *sctp_transport_get_idx(struct net *net,
  4098. struct rhashtable_iter *iter,
  4099. int pos)
  4100. {
  4101. void *obj = SEQ_START_TOKEN;
  4102. while (pos && (obj = sctp_transport_get_next(net, iter)) &&
  4103. !IS_ERR(obj))
  4104. pos--;
  4105. return obj;
  4106. }
  4107. int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *),
  4108. void *p) {
  4109. int err = 0;
  4110. int hash = 0;
  4111. struct sctp_ep_common *epb;
  4112. struct sctp_hashbucket *head;
  4113. for (head = sctp_ep_hashtable; hash < sctp_ep_hashsize;
  4114. hash++, head++) {
  4115. read_lock_bh(&head->lock);
  4116. sctp_for_each_hentry(epb, &head->chain) {
  4117. err = cb(sctp_ep(epb), p);
  4118. if (err)
  4119. break;
  4120. }
  4121. read_unlock_bh(&head->lock);
  4122. }
  4123. return err;
  4124. }
  4125. EXPORT_SYMBOL_GPL(sctp_for_each_endpoint);
  4126. int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
  4127. struct net *net,
  4128. const union sctp_addr *laddr,
  4129. const union sctp_addr *paddr, void *p)
  4130. {
  4131. struct sctp_transport *transport;
  4132. int err;
  4133. rcu_read_lock();
  4134. transport = sctp_addrs_lookup_transport(net, laddr, paddr);
  4135. rcu_read_unlock();
  4136. if (!transport)
  4137. return -ENOENT;
  4138. err = cb(transport, p);
  4139. sctp_transport_put(transport);
  4140. return err;
  4141. }
  4142. EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
  4143. int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
  4144. int (*cb_done)(struct sctp_transport *, void *),
  4145. struct net *net, int *pos, void *p) {
  4146. struct rhashtable_iter hti;
  4147. struct sctp_transport *tsp;
  4148. int ret;
  4149. again:
  4150. ret = 0;
  4151. sctp_transport_walk_start(&hti);
  4152. tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
  4153. for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
  4154. if (!sctp_transport_hold(tsp))
  4155. continue;
  4156. ret = cb(tsp, p);
  4157. if (ret)
  4158. break;
  4159. (*pos)++;
  4160. sctp_transport_put(tsp);
  4161. }
  4162. sctp_transport_walk_stop(&hti);
  4163. if (ret) {
  4164. if (cb_done && !cb_done(tsp, p)) {
  4165. (*pos)++;
  4166. sctp_transport_put(tsp);
  4167. goto again;
  4168. }
  4169. sctp_transport_put(tsp);
  4170. }
  4171. return ret;
  4172. }
  4173. EXPORT_SYMBOL_GPL(sctp_for_each_transport);
  4174. /* 7.2.1 Association Status (SCTP_STATUS)
  4175. * Applications can retrieve current status information about an
  4176. * association, including association state, peer receiver window size,
  4177. * number of unacked data chunks, and number of data chunks pending
  4178. * receipt. This information is read-only.
  4179. */
  4180. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  4181. char __user *optval,
  4182. int __user *optlen)
  4183. {
  4184. struct sctp_status status;
  4185. struct sctp_association *asoc = NULL;
  4186. struct sctp_transport *transport;
  4187. sctp_assoc_t associd;
  4188. int retval = 0;
  4189. if (len < sizeof(status)) {
  4190. retval = -EINVAL;
  4191. goto out;
  4192. }
  4193. len = sizeof(status);
  4194. if (copy_from_user(&status, optval, len)) {
  4195. retval = -EFAULT;
  4196. goto out;
  4197. }
  4198. associd = status.sstat_assoc_id;
  4199. asoc = sctp_id2assoc(sk, associd);
  4200. if (!asoc) {
  4201. retval = -EINVAL;
  4202. goto out;
  4203. }
  4204. transport = asoc->peer.primary_path;
  4205. status.sstat_assoc_id = sctp_assoc2id(asoc);
  4206. status.sstat_state = sctp_assoc_to_state(asoc);
  4207. status.sstat_rwnd = asoc->peer.rwnd;
  4208. status.sstat_unackdata = asoc->unack_data;
  4209. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  4210. status.sstat_instrms = asoc->stream.incnt;
  4211. status.sstat_outstrms = asoc->stream.outcnt;
  4212. status.sstat_fragmentation_point = asoc->frag_point;
  4213. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  4214. memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr,
  4215. transport->af_specific->sockaddr_len);
  4216. /* Map ipv4 address into v4-mapped-on-v6 address. */
  4217. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sctp_sk(sk),
  4218. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  4219. status.sstat_primary.spinfo_state = transport->state;
  4220. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  4221. status.sstat_primary.spinfo_srtt = transport->srtt;
  4222. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  4223. status.sstat_primary.spinfo_mtu = transport->pathmtu;
  4224. if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
  4225. status.sstat_primary.spinfo_state = SCTP_ACTIVE;
  4226. if (put_user(len, optlen)) {
  4227. retval = -EFAULT;
  4228. goto out;
  4229. }
  4230. pr_debug("%s: len:%d, state:%d, rwnd:%d, assoc_id:%d\n",
  4231. __func__, len, status.sstat_state, status.sstat_rwnd,
  4232. status.sstat_assoc_id);
  4233. if (copy_to_user(optval, &status, len)) {
  4234. retval = -EFAULT;
  4235. goto out;
  4236. }
  4237. out:
  4238. return retval;
  4239. }
  4240. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  4241. *
  4242. * Applications can retrieve information about a specific peer address
  4243. * of an association, including its reachability state, congestion
  4244. * window, and retransmission timer values. This information is
  4245. * read-only.
  4246. */
  4247. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  4248. char __user *optval,
  4249. int __user *optlen)
  4250. {
  4251. struct sctp_paddrinfo pinfo;
  4252. struct sctp_transport *transport;
  4253. int retval = 0;
  4254. if (len < sizeof(pinfo)) {
  4255. retval = -EINVAL;
  4256. goto out;
  4257. }
  4258. len = sizeof(pinfo);
  4259. if (copy_from_user(&pinfo, optval, len)) {
  4260. retval = -EFAULT;
  4261. goto out;
  4262. }
  4263. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  4264. pinfo.spinfo_assoc_id);
  4265. if (!transport)
  4266. return -EINVAL;
  4267. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  4268. pinfo.spinfo_state = transport->state;
  4269. pinfo.spinfo_cwnd = transport->cwnd;
  4270. pinfo.spinfo_srtt = transport->srtt;
  4271. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  4272. pinfo.spinfo_mtu = transport->pathmtu;
  4273. if (pinfo.spinfo_state == SCTP_UNKNOWN)
  4274. pinfo.spinfo_state = SCTP_ACTIVE;
  4275. if (put_user(len, optlen)) {
  4276. retval = -EFAULT;
  4277. goto out;
  4278. }
  4279. if (copy_to_user(optval, &pinfo, len)) {
  4280. retval = -EFAULT;
  4281. goto out;
  4282. }
  4283. out:
  4284. return retval;
  4285. }
  4286. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  4287. *
  4288. * This option is a on/off flag. If enabled no SCTP message
  4289. * fragmentation will be performed. Instead if a message being sent
  4290. * exceeds the current PMTU size, the message will NOT be sent and
  4291. * instead a error will be indicated to the user.
  4292. */
  4293. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  4294. char __user *optval, int __user *optlen)
  4295. {
  4296. int val;
  4297. if (len < sizeof(int))
  4298. return -EINVAL;
  4299. len = sizeof(int);
  4300. val = (sctp_sk(sk)->disable_fragments == 1);
  4301. if (put_user(len, optlen))
  4302. return -EFAULT;
  4303. if (copy_to_user(optval, &val, len))
  4304. return -EFAULT;
  4305. return 0;
  4306. }
  4307. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  4308. *
  4309. * This socket option is used to specify various notifications and
  4310. * ancillary data the user wishes to receive.
  4311. */
  4312. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  4313. int __user *optlen)
  4314. {
  4315. if (len == 0)
  4316. return -EINVAL;
  4317. if (len > sizeof(struct sctp_event_subscribe))
  4318. len = sizeof(struct sctp_event_subscribe);
  4319. if (put_user(len, optlen))
  4320. return -EFAULT;
  4321. if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
  4322. return -EFAULT;
  4323. return 0;
  4324. }
  4325. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  4326. *
  4327. * This socket option is applicable to the UDP-style socket only. When
  4328. * set it will cause associations that are idle for more than the
  4329. * specified number of seconds to automatically close. An association
  4330. * being idle is defined an association that has NOT sent or received
  4331. * user data. The special value of '0' indicates that no automatic
  4332. * close of any associations should be performed. The option expects an
  4333. * integer defining the number of seconds of idle time before an
  4334. * association is closed.
  4335. */
  4336. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4337. {
  4338. /* Applicable to UDP-style socket only */
  4339. if (sctp_style(sk, TCP))
  4340. return -EOPNOTSUPP;
  4341. if (len < sizeof(int))
  4342. return -EINVAL;
  4343. len = sizeof(int);
  4344. if (put_user(len, optlen))
  4345. return -EFAULT;
  4346. if (put_user(sctp_sk(sk)->autoclose, (int __user *)optval))
  4347. return -EFAULT;
  4348. return 0;
  4349. }
  4350. /* Helper routine to branch off an association to a new socket. */
  4351. int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp)
  4352. {
  4353. struct sctp_association *asoc = sctp_id2assoc(sk, id);
  4354. struct sctp_sock *sp = sctp_sk(sk);
  4355. struct socket *sock;
  4356. int err = 0;
  4357. /* Do not peel off from one netns to another one. */
  4358. if (!net_eq(current->nsproxy->net_ns, sock_net(sk)))
  4359. return -EINVAL;
  4360. if (!asoc)
  4361. return -EINVAL;
  4362. /* An association cannot be branched off from an already peeled-off
  4363. * socket, nor is this supported for tcp style sockets.
  4364. */
  4365. if (!sctp_style(sk, UDP))
  4366. return -EINVAL;
  4367. /* Create a new socket. */
  4368. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  4369. if (err < 0)
  4370. return err;
  4371. sctp_copy_sock(sock->sk, sk, asoc);
  4372. /* Make peeled-off sockets more like 1-1 accepted sockets.
  4373. * Set the daddr and initialize id to something more random
  4374. */
  4375. sp->pf->to_sk_daddr(&asoc->peer.primary_addr, sk);
  4376. /* Populate the fields of the newsk from the oldsk and migrate the
  4377. * asoc to the newsk.
  4378. */
  4379. sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  4380. *sockp = sock;
  4381. return err;
  4382. }
  4383. EXPORT_SYMBOL(sctp_do_peeloff);
  4384. static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *peeloff,
  4385. struct file **newfile, unsigned flags)
  4386. {
  4387. struct socket *newsock;
  4388. int retval;
  4389. retval = sctp_do_peeloff(sk, peeloff->associd, &newsock);
  4390. if (retval < 0)
  4391. goto out;
  4392. /* Map the socket to an unused fd that can be returned to the user. */
  4393. retval = get_unused_fd_flags(flags & SOCK_CLOEXEC);
  4394. if (retval < 0) {
  4395. sock_release(newsock);
  4396. goto out;
  4397. }
  4398. *newfile = sock_alloc_file(newsock, 0, NULL);
  4399. if (IS_ERR(*newfile)) {
  4400. put_unused_fd(retval);
  4401. retval = PTR_ERR(*newfile);
  4402. *newfile = NULL;
  4403. return retval;
  4404. }
  4405. pr_debug("%s: sk:%p, newsk:%p, sd:%d\n", __func__, sk, newsock->sk,
  4406. retval);
  4407. peeloff->sd = retval;
  4408. if (flags & SOCK_NONBLOCK)
  4409. (*newfile)->f_flags |= O_NONBLOCK;
  4410. out:
  4411. return retval;
  4412. }
  4413. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4414. {
  4415. sctp_peeloff_arg_t peeloff;
  4416. struct file *newfile = NULL;
  4417. int retval = 0;
  4418. if (len < sizeof(sctp_peeloff_arg_t))
  4419. return -EINVAL;
  4420. len = sizeof(sctp_peeloff_arg_t);
  4421. if (copy_from_user(&peeloff, optval, len))
  4422. return -EFAULT;
  4423. retval = sctp_getsockopt_peeloff_common(sk, &peeloff, &newfile, 0);
  4424. if (retval < 0)
  4425. goto out;
  4426. /* Return the fd mapped to the new socket. */
  4427. if (put_user(len, optlen)) {
  4428. fput(newfile);
  4429. put_unused_fd(retval);
  4430. return -EFAULT;
  4431. }
  4432. if (copy_to_user(optval, &peeloff, len)) {
  4433. fput(newfile);
  4434. put_unused_fd(retval);
  4435. return -EFAULT;
  4436. }
  4437. fd_install(retval, newfile);
  4438. out:
  4439. return retval;
  4440. }
  4441. static int sctp_getsockopt_peeloff_flags(struct sock *sk, int len,
  4442. char __user *optval, int __user *optlen)
  4443. {
  4444. sctp_peeloff_flags_arg_t peeloff;
  4445. struct file *newfile = NULL;
  4446. int retval = 0;
  4447. if (len < sizeof(sctp_peeloff_flags_arg_t))
  4448. return -EINVAL;
  4449. len = sizeof(sctp_peeloff_flags_arg_t);
  4450. if (copy_from_user(&peeloff, optval, len))
  4451. return -EFAULT;
  4452. retval = sctp_getsockopt_peeloff_common(sk, &peeloff.p_arg,
  4453. &newfile, peeloff.flags);
  4454. if (retval < 0)
  4455. goto out;
  4456. /* Return the fd mapped to the new socket. */
  4457. if (put_user(len, optlen)) {
  4458. fput(newfile);
  4459. put_unused_fd(retval);
  4460. return -EFAULT;
  4461. }
  4462. if (copy_to_user(optval, &peeloff, len)) {
  4463. fput(newfile);
  4464. put_unused_fd(retval);
  4465. return -EFAULT;
  4466. }
  4467. fd_install(retval, newfile);
  4468. out:
  4469. return retval;
  4470. }
  4471. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  4472. *
  4473. * Applications can enable or disable heartbeats for any peer address of
  4474. * an association, modify an address's heartbeat interval, force a
  4475. * heartbeat to be sent immediately, and adjust the address's maximum
  4476. * number of retransmissions sent before an address is considered
  4477. * unreachable. The following structure is used to access and modify an
  4478. * address's parameters:
  4479. *
  4480. * struct sctp_paddrparams {
  4481. * sctp_assoc_t spp_assoc_id;
  4482. * struct sockaddr_storage spp_address;
  4483. * uint32_t spp_hbinterval;
  4484. * uint16_t spp_pathmaxrxt;
  4485. * uint32_t spp_pathmtu;
  4486. * uint32_t spp_sackdelay;
  4487. * uint32_t spp_flags;
  4488. * };
  4489. *
  4490. * spp_assoc_id - (one-to-many style socket) This is filled in the
  4491. * application, and identifies the association for
  4492. * this query.
  4493. * spp_address - This specifies which address is of interest.
  4494. * spp_hbinterval - This contains the value of the heartbeat interval,
  4495. * in milliseconds. If a value of zero
  4496. * is present in this field then no changes are to
  4497. * be made to this parameter.
  4498. * spp_pathmaxrxt - This contains the maximum number of
  4499. * retransmissions before this address shall be
  4500. * considered unreachable. If a value of zero
  4501. * is present in this field then no changes are to
  4502. * be made to this parameter.
  4503. * spp_pathmtu - When Path MTU discovery is disabled the value
  4504. * specified here will be the "fixed" path mtu.
  4505. * Note that if the spp_address field is empty
  4506. * then all associations on this address will
  4507. * have this fixed path mtu set upon them.
  4508. *
  4509. * spp_sackdelay - When delayed sack is enabled, this value specifies
  4510. * the number of milliseconds that sacks will be delayed
  4511. * for. This value will apply to all addresses of an
  4512. * association if the spp_address field is empty. Note
  4513. * also, that if delayed sack is enabled and this
  4514. * value is set to 0, no change is made to the last
  4515. * recorded delayed sack timer value.
  4516. *
  4517. * spp_flags - These flags are used to control various features
  4518. * on an association. The flag field may contain
  4519. * zero or more of the following options.
  4520. *
  4521. * SPP_HB_ENABLE - Enable heartbeats on the
  4522. * specified address. Note that if the address
  4523. * field is empty all addresses for the association
  4524. * have heartbeats enabled upon them.
  4525. *
  4526. * SPP_HB_DISABLE - Disable heartbeats on the
  4527. * speicifed address. Note that if the address
  4528. * field is empty all addresses for the association
  4529. * will have their heartbeats disabled. Note also
  4530. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  4531. * mutually exclusive, only one of these two should
  4532. * be specified. Enabling both fields will have
  4533. * undetermined results.
  4534. *
  4535. * SPP_HB_DEMAND - Request a user initiated heartbeat
  4536. * to be made immediately.
  4537. *
  4538. * SPP_PMTUD_ENABLE - This field will enable PMTU
  4539. * discovery upon the specified address. Note that
  4540. * if the address feild is empty then all addresses
  4541. * on the association are effected.
  4542. *
  4543. * SPP_PMTUD_DISABLE - This field will disable PMTU
  4544. * discovery upon the specified address. Note that
  4545. * if the address feild is empty then all addresses
  4546. * on the association are effected. Not also that
  4547. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  4548. * exclusive. Enabling both will have undetermined
  4549. * results.
  4550. *
  4551. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  4552. * on delayed sack. The time specified in spp_sackdelay
  4553. * is used to specify the sack delay for this address. Note
  4554. * that if spp_address is empty then all addresses will
  4555. * enable delayed sack and take on the sack delay
  4556. * value specified in spp_sackdelay.
  4557. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  4558. * off delayed sack. If the spp_address field is blank then
  4559. * delayed sack is disabled for the entire association. Note
  4560. * also that this field is mutually exclusive to
  4561. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  4562. * results.
  4563. */
  4564. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  4565. char __user *optval, int __user *optlen)
  4566. {
  4567. struct sctp_paddrparams params;
  4568. struct sctp_transport *trans = NULL;
  4569. struct sctp_association *asoc = NULL;
  4570. struct sctp_sock *sp = sctp_sk(sk);
  4571. if (len < sizeof(struct sctp_paddrparams))
  4572. return -EINVAL;
  4573. len = sizeof(struct sctp_paddrparams);
  4574. if (copy_from_user(&params, optval, len))
  4575. return -EFAULT;
  4576. /* If an address other than INADDR_ANY is specified, and
  4577. * no transport is found, then the request is invalid.
  4578. */
  4579. if (!sctp_is_any(sk, (union sctp_addr *)&params.spp_address)) {
  4580. trans = sctp_addr_id2transport(sk, &params.spp_address,
  4581. params.spp_assoc_id);
  4582. if (!trans) {
  4583. pr_debug("%s: failed no transport\n", __func__);
  4584. return -EINVAL;
  4585. }
  4586. }
  4587. /* Get association, if assoc_id != 0 and the socket is a one
  4588. * to many style socket, and an association was not found, then
  4589. * the id was invalid.
  4590. */
  4591. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  4592. if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
  4593. pr_debug("%s: failed no association\n", __func__);
  4594. return -EINVAL;
  4595. }
  4596. if (trans) {
  4597. /* Fetch transport values. */
  4598. params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
  4599. params.spp_pathmtu = trans->pathmtu;
  4600. params.spp_pathmaxrxt = trans->pathmaxrxt;
  4601. params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
  4602. /*draft-11 doesn't say what to return in spp_flags*/
  4603. params.spp_flags = trans->param_flags;
  4604. } else if (asoc) {
  4605. /* Fetch association values. */
  4606. params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
  4607. params.spp_pathmtu = asoc->pathmtu;
  4608. params.spp_pathmaxrxt = asoc->pathmaxrxt;
  4609. params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
  4610. /*draft-11 doesn't say what to return in spp_flags*/
  4611. params.spp_flags = asoc->param_flags;
  4612. } else {
  4613. /* Fetch socket values. */
  4614. params.spp_hbinterval = sp->hbinterval;
  4615. params.spp_pathmtu = sp->pathmtu;
  4616. params.spp_sackdelay = sp->sackdelay;
  4617. params.spp_pathmaxrxt = sp->pathmaxrxt;
  4618. /*draft-11 doesn't say what to return in spp_flags*/
  4619. params.spp_flags = sp->param_flags;
  4620. }
  4621. if (copy_to_user(optval, &params, len))
  4622. return -EFAULT;
  4623. if (put_user(len, optlen))
  4624. return -EFAULT;
  4625. return 0;
  4626. }
  4627. /*
  4628. * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK)
  4629. *
  4630. * This option will effect the way delayed acks are performed. This
  4631. * option allows you to get or set the delayed ack time, in
  4632. * milliseconds. It also allows changing the delayed ack frequency.
  4633. * Changing the frequency to 1 disables the delayed sack algorithm. If
  4634. * the assoc_id is 0, then this sets or gets the endpoints default
  4635. * values. If the assoc_id field is non-zero, then the set or get
  4636. * effects the specified association for the one to many model (the
  4637. * assoc_id field is ignored by the one to one model). Note that if
  4638. * sack_delay or sack_freq are 0 when setting this option, then the
  4639. * current values will remain unchanged.
  4640. *
  4641. * struct sctp_sack_info {
  4642. * sctp_assoc_t sack_assoc_id;
  4643. * uint32_t sack_delay;
  4644. * uint32_t sack_freq;
  4645. * };
  4646. *
  4647. * sack_assoc_id - This parameter, indicates which association the user
  4648. * is performing an action upon. Note that if this field's value is
  4649. * zero then the endpoints default value is changed (effecting future
  4650. * associations only).
  4651. *
  4652. * sack_delay - This parameter contains the number of milliseconds that
  4653. * the user is requesting the delayed ACK timer be set to. Note that
  4654. * this value is defined in the standard to be between 200 and 500
  4655. * milliseconds.
  4656. *
  4657. * sack_freq - This parameter contains the number of packets that must
  4658. * be received before a sack is sent without waiting for the delay
  4659. * timer to expire. The default value for this is 2, setting this
  4660. * value to 1 will disable the delayed sack algorithm.
  4661. */
  4662. static int sctp_getsockopt_delayed_ack(struct sock *sk, int len,
  4663. char __user *optval,
  4664. int __user *optlen)
  4665. {
  4666. struct sctp_sack_info params;
  4667. struct sctp_association *asoc = NULL;
  4668. struct sctp_sock *sp = sctp_sk(sk);
  4669. if (len >= sizeof(struct sctp_sack_info)) {
  4670. len = sizeof(struct sctp_sack_info);
  4671. if (copy_from_user(&params, optval, len))
  4672. return -EFAULT;
  4673. } else if (len == sizeof(struct sctp_assoc_value)) {
  4674. pr_warn_ratelimited(DEPRECATED
  4675. "%s (pid %d) "
  4676. "Use of struct sctp_assoc_value in delayed_ack socket option.\n"
  4677. "Use struct sctp_sack_info instead\n",
  4678. current->comm, task_pid_nr(current));
  4679. if (copy_from_user(&params, optval, len))
  4680. return -EFAULT;
  4681. } else
  4682. return -EINVAL;
  4683. /* Get association, if sack_assoc_id != 0 and the socket is a one
  4684. * to many style socket, and an association was not found, then
  4685. * the id was invalid.
  4686. */
  4687. asoc = sctp_id2assoc(sk, params.sack_assoc_id);
  4688. if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP))
  4689. return -EINVAL;
  4690. if (asoc) {
  4691. /* Fetch association values. */
  4692. if (asoc->param_flags & SPP_SACKDELAY_ENABLE) {
  4693. params.sack_delay = jiffies_to_msecs(
  4694. asoc->sackdelay);
  4695. params.sack_freq = asoc->sackfreq;
  4696. } else {
  4697. params.sack_delay = 0;
  4698. params.sack_freq = 1;
  4699. }
  4700. } else {
  4701. /* Fetch socket values. */
  4702. if (sp->param_flags & SPP_SACKDELAY_ENABLE) {
  4703. params.sack_delay = sp->sackdelay;
  4704. params.sack_freq = sp->sackfreq;
  4705. } else {
  4706. params.sack_delay = 0;
  4707. params.sack_freq = 1;
  4708. }
  4709. }
  4710. if (copy_to_user(optval, &params, len))
  4711. return -EFAULT;
  4712. if (put_user(len, optlen))
  4713. return -EFAULT;
  4714. return 0;
  4715. }
  4716. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  4717. *
  4718. * Applications can specify protocol parameters for the default association
  4719. * initialization. The option name argument to setsockopt() and getsockopt()
  4720. * is SCTP_INITMSG.
  4721. *
  4722. * Setting initialization parameters is effective only on an unconnected
  4723. * socket (for UDP-style sockets only future associations are effected
  4724. * by the change). With TCP-style sockets, this option is inherited by
  4725. * sockets derived from a listener socket.
  4726. */
  4727. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  4728. {
  4729. if (len < sizeof(struct sctp_initmsg))
  4730. return -EINVAL;
  4731. len = sizeof(struct sctp_initmsg);
  4732. if (put_user(len, optlen))
  4733. return -EFAULT;
  4734. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  4735. return -EFAULT;
  4736. return 0;
  4737. }
  4738. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  4739. char __user *optval, int __user *optlen)
  4740. {
  4741. struct sctp_association *asoc;
  4742. int cnt = 0;
  4743. struct sctp_getaddrs getaddrs;
  4744. struct sctp_transport *from;
  4745. void __user *to;
  4746. union sctp_addr temp;
  4747. struct sctp_sock *sp = sctp_sk(sk);
  4748. int addrlen;
  4749. size_t space_left;
  4750. int bytes_copied;
  4751. if (len < sizeof(struct sctp_getaddrs))
  4752. return -EINVAL;
  4753. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  4754. return -EFAULT;
  4755. /* For UDP-style sockets, id specifies the association to query. */
  4756. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  4757. if (!asoc)
  4758. return -EINVAL;
  4759. to = optval + offsetof(struct sctp_getaddrs, addrs);
  4760. space_left = len - offsetof(struct sctp_getaddrs, addrs);
  4761. list_for_each_entry(from, &asoc->peer.transport_addr_list,
  4762. transports) {
  4763. memcpy(&temp, &from->ipaddr, sizeof(temp));
  4764. addrlen = sctp_get_pf_specific(sk->sk_family)
  4765. ->addr_to_user(sp, &temp);
  4766. if (space_left < addrlen)
  4767. return -ENOMEM;
  4768. if (copy_to_user(to, &temp, addrlen))
  4769. return -EFAULT;
  4770. to += addrlen;
  4771. cnt++;
  4772. space_left -= addrlen;
  4773. }
  4774. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  4775. return -EFAULT;
  4776. bytes_copied = ((char __user *)to) - optval;
  4777. if (put_user(bytes_copied, optlen))
  4778. return -EFAULT;
  4779. return 0;
  4780. }
  4781. static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to,
  4782. size_t space_left, int *bytes_copied)
  4783. {
  4784. struct sctp_sockaddr_entry *addr;
  4785. union sctp_addr temp;
  4786. int cnt = 0;
  4787. int addrlen;
  4788. struct net *net = sock_net(sk);
  4789. rcu_read_lock();
  4790. list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
  4791. if (!addr->valid)
  4792. continue;
  4793. if ((PF_INET == sk->sk_family) &&
  4794. (AF_INET6 == addr->a.sa.sa_family))
  4795. continue;
  4796. if ((PF_INET6 == sk->sk_family) &&
  4797. inet_v6_ipv6only(sk) &&
  4798. (AF_INET == addr->a.sa.sa_family))
  4799. continue;
  4800. memcpy(&temp, &addr->a, sizeof(temp));
  4801. if (!temp.v4.sin_port)
  4802. temp.v4.sin_port = htons(port);
  4803. addrlen = sctp_get_pf_specific(sk->sk_family)
  4804. ->addr_to_user(sctp_sk(sk), &temp);
  4805. if (space_left < addrlen) {
  4806. cnt = -ENOMEM;
  4807. break;
  4808. }
  4809. memcpy(to, &temp, addrlen);
  4810. to += addrlen;
  4811. cnt++;
  4812. space_left -= addrlen;
  4813. *bytes_copied += addrlen;
  4814. }
  4815. rcu_read_unlock();
  4816. return cnt;
  4817. }
  4818. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  4819. char __user *optval, int __user *optlen)
  4820. {
  4821. struct sctp_bind_addr *bp;
  4822. struct sctp_association *asoc;
  4823. int cnt = 0;
  4824. struct sctp_getaddrs getaddrs;
  4825. struct sctp_sockaddr_entry *addr;
  4826. void __user *to;
  4827. union sctp_addr temp;
  4828. struct sctp_sock *sp = sctp_sk(sk);
  4829. int addrlen;
  4830. int err = 0;
  4831. size_t space_left;
  4832. int bytes_copied = 0;
  4833. void *addrs;
  4834. void *buf;
  4835. if (len < sizeof(struct sctp_getaddrs))
  4836. return -EINVAL;
  4837. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  4838. return -EFAULT;
  4839. /*
  4840. * For UDP-style sockets, id specifies the association to query.
  4841. * If the id field is set to the value '0' then the locally bound
  4842. * addresses are returned without regard to any particular
  4843. * association.
  4844. */
  4845. if (0 == getaddrs.assoc_id) {
  4846. bp = &sctp_sk(sk)->ep->base.bind_addr;
  4847. } else {
  4848. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  4849. if (!asoc)
  4850. return -EINVAL;
  4851. bp = &asoc->base.bind_addr;
  4852. }
  4853. to = optval + offsetof(struct sctp_getaddrs, addrs);
  4854. space_left = len - offsetof(struct sctp_getaddrs, addrs);
  4855. addrs = kmalloc(space_left, GFP_USER | __GFP_NOWARN);
  4856. if (!addrs)
  4857. return -ENOMEM;
  4858. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  4859. * addresses from the global local address list.
  4860. */
  4861. if (sctp_list_single_entry(&bp->address_list)) {
  4862. addr = list_entry(bp->address_list.next,
  4863. struct sctp_sockaddr_entry, list);
  4864. if (sctp_is_any(sk, &addr->a)) {
  4865. cnt = sctp_copy_laddrs(sk, bp->port, addrs,
  4866. space_left, &bytes_copied);
  4867. if (cnt < 0) {
  4868. err = cnt;
  4869. goto out;
  4870. }
  4871. goto copy_getaddrs;
  4872. }
  4873. }
  4874. buf = addrs;
  4875. /* Protection on the bound address list is not needed since
  4876. * in the socket option context we hold a socket lock and
  4877. * thus the bound address list can't change.
  4878. */
  4879. list_for_each_entry(addr, &bp->address_list, list) {
  4880. memcpy(&temp, &addr->a, sizeof(temp));
  4881. addrlen = sctp_get_pf_specific(sk->sk_family)
  4882. ->addr_to_user(sp, &temp);
  4883. if (space_left < addrlen) {
  4884. err = -ENOMEM; /*fixme: right error?*/
  4885. goto out;
  4886. }
  4887. memcpy(buf, &temp, addrlen);
  4888. buf += addrlen;
  4889. bytes_copied += addrlen;
  4890. cnt++;
  4891. space_left -= addrlen;
  4892. }
  4893. copy_getaddrs:
  4894. if (copy_to_user(to, addrs, bytes_copied)) {
  4895. err = -EFAULT;
  4896. goto out;
  4897. }
  4898. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) {
  4899. err = -EFAULT;
  4900. goto out;
  4901. }
  4902. /* XXX: We should have accounted for sizeof(struct sctp_getaddrs) too,
  4903. * but we can't change it anymore.
  4904. */
  4905. if (put_user(bytes_copied, optlen))
  4906. err = -EFAULT;
  4907. out:
  4908. kfree(addrs);
  4909. return err;
  4910. }
  4911. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  4912. *
  4913. * Requests that the local SCTP stack use the enclosed peer address as
  4914. * the association primary. The enclosed address must be one of the
  4915. * association peer's addresses.
  4916. */
  4917. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  4918. char __user *optval, int __user *optlen)
  4919. {
  4920. struct sctp_prim prim;
  4921. struct sctp_association *asoc;
  4922. struct sctp_sock *sp = sctp_sk(sk);
  4923. if (len < sizeof(struct sctp_prim))
  4924. return -EINVAL;
  4925. len = sizeof(struct sctp_prim);
  4926. if (copy_from_user(&prim, optval, len))
  4927. return -EFAULT;
  4928. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  4929. if (!asoc)
  4930. return -EINVAL;
  4931. if (!asoc->peer.primary_path)
  4932. return -ENOTCONN;
  4933. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  4934. asoc->peer.primary_path->af_specific->sockaddr_len);
  4935. sctp_get_pf_specific(sk->sk_family)->addr_to_user(sp,
  4936. (union sctp_addr *)&prim.ssp_addr);
  4937. if (put_user(len, optlen))
  4938. return -EFAULT;
  4939. if (copy_to_user(optval, &prim, len))
  4940. return -EFAULT;
  4941. return 0;
  4942. }
  4943. /*
  4944. * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER)
  4945. *
  4946. * Requests that the local endpoint set the specified Adaptation Layer
  4947. * Indication parameter for all future INIT and INIT-ACK exchanges.
  4948. */
  4949. static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len,
  4950. char __user *optval, int __user *optlen)
  4951. {
  4952. struct sctp_setadaptation adaptation;
  4953. if (len < sizeof(struct sctp_setadaptation))
  4954. return -EINVAL;
  4955. len = sizeof(struct sctp_setadaptation);
  4956. adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind;
  4957. if (put_user(len, optlen))
  4958. return -EFAULT;
  4959. if (copy_to_user(optval, &adaptation, len))
  4960. return -EFAULT;
  4961. return 0;
  4962. }
  4963. /*
  4964. *
  4965. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  4966. *
  4967. * Applications that wish to use the sendto() system call may wish to
  4968. * specify a default set of parameters that would normally be supplied
  4969. * through the inclusion of ancillary data. This socket option allows
  4970. * such an application to set the default sctp_sndrcvinfo structure.
  4971. * The application that wishes to use this socket option simply passes
  4972. * in to this call the sctp_sndrcvinfo structure defined in Section
  4973. * 5.2.2) The input parameters accepted by this call include
  4974. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  4975. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  4976. * to this call if the caller is using the UDP model.
  4977. *
  4978. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  4979. */
  4980. static int sctp_getsockopt_default_send_param(struct sock *sk,
  4981. int len, char __user *optval,
  4982. int __user *optlen)
  4983. {
  4984. struct sctp_sock *sp = sctp_sk(sk);
  4985. struct sctp_association *asoc;
  4986. struct sctp_sndrcvinfo info;
  4987. if (len < sizeof(info))
  4988. return -EINVAL;
  4989. len = sizeof(info);
  4990. if (copy_from_user(&info, optval, len))
  4991. return -EFAULT;
  4992. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  4993. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  4994. return -EINVAL;
  4995. if (asoc) {
  4996. info.sinfo_stream = asoc->default_stream;
  4997. info.sinfo_flags = asoc->default_flags;
  4998. info.sinfo_ppid = asoc->default_ppid;
  4999. info.sinfo_context = asoc->default_context;
  5000. info.sinfo_timetolive = asoc->default_timetolive;
  5001. } else {
  5002. info.sinfo_stream = sp->default_stream;
  5003. info.sinfo_flags = sp->default_flags;
  5004. info.sinfo_ppid = sp->default_ppid;
  5005. info.sinfo_context = sp->default_context;
  5006. info.sinfo_timetolive = sp->default_timetolive;
  5007. }
  5008. if (put_user(len, optlen))
  5009. return -EFAULT;
  5010. if (copy_to_user(optval, &info, len))
  5011. return -EFAULT;
  5012. return 0;
  5013. }
  5014. /* RFC6458, Section 8.1.31. Set/get Default Send Parameters
  5015. * (SCTP_DEFAULT_SNDINFO)
  5016. */
  5017. static int sctp_getsockopt_default_sndinfo(struct sock *sk, int len,
  5018. char __user *optval,
  5019. int __user *optlen)
  5020. {
  5021. struct sctp_sock *sp = sctp_sk(sk);
  5022. struct sctp_association *asoc;
  5023. struct sctp_sndinfo info;
  5024. if (len < sizeof(info))
  5025. return -EINVAL;
  5026. len = sizeof(info);
  5027. if (copy_from_user(&info, optval, len))
  5028. return -EFAULT;
  5029. asoc = sctp_id2assoc(sk, info.snd_assoc_id);
  5030. if (!asoc && info.snd_assoc_id && sctp_style(sk, UDP))
  5031. return -EINVAL;
  5032. if (asoc) {
  5033. info.snd_sid = asoc->default_stream;
  5034. info.snd_flags = asoc->default_flags;
  5035. info.snd_ppid = asoc->default_ppid;
  5036. info.snd_context = asoc->default_context;
  5037. } else {
  5038. info.snd_sid = sp->default_stream;
  5039. info.snd_flags = sp->default_flags;
  5040. info.snd_ppid = sp->default_ppid;
  5041. info.snd_context = sp->default_context;
  5042. }
  5043. if (put_user(len, optlen))
  5044. return -EFAULT;
  5045. if (copy_to_user(optval, &info, len))
  5046. return -EFAULT;
  5047. return 0;
  5048. }
  5049. /*
  5050. *
  5051. * 7.1.5 SCTP_NODELAY
  5052. *
  5053. * Turn on/off any Nagle-like algorithm. This means that packets are
  5054. * generally sent as soon as possible and no unnecessary delays are
  5055. * introduced, at the cost of more packets in the network. Expects an
  5056. * integer boolean flag.
  5057. */
  5058. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  5059. char __user *optval, int __user *optlen)
  5060. {
  5061. int val;
  5062. if (len < sizeof(int))
  5063. return -EINVAL;
  5064. len = sizeof(int);
  5065. val = (sctp_sk(sk)->nodelay == 1);
  5066. if (put_user(len, optlen))
  5067. return -EFAULT;
  5068. if (copy_to_user(optval, &val, len))
  5069. return -EFAULT;
  5070. return 0;
  5071. }
  5072. /*
  5073. *
  5074. * 7.1.1 SCTP_RTOINFO
  5075. *
  5076. * The protocol parameters used to initialize and bound retransmission
  5077. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  5078. * and modify these parameters.
  5079. * All parameters are time values, in milliseconds. A value of 0, when
  5080. * modifying the parameters, indicates that the current value should not
  5081. * be changed.
  5082. *
  5083. */
  5084. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  5085. char __user *optval,
  5086. int __user *optlen) {
  5087. struct sctp_rtoinfo rtoinfo;
  5088. struct sctp_association *asoc;
  5089. if (len < sizeof (struct sctp_rtoinfo))
  5090. return -EINVAL;
  5091. len = sizeof(struct sctp_rtoinfo);
  5092. if (copy_from_user(&rtoinfo, optval, len))
  5093. return -EFAULT;
  5094. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  5095. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  5096. return -EINVAL;
  5097. /* Values corresponding to the specific association. */
  5098. if (asoc) {
  5099. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  5100. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  5101. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  5102. } else {
  5103. /* Values corresponding to the endpoint. */
  5104. struct sctp_sock *sp = sctp_sk(sk);
  5105. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  5106. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  5107. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  5108. }
  5109. if (put_user(len, optlen))
  5110. return -EFAULT;
  5111. if (copy_to_user(optval, &rtoinfo, len))
  5112. return -EFAULT;
  5113. return 0;
  5114. }
  5115. /*
  5116. *
  5117. * 7.1.2 SCTP_ASSOCINFO
  5118. *
  5119. * This option is used to tune the maximum retransmission attempts
  5120. * of the association.
  5121. * Returns an error if the new association retransmission value is
  5122. * greater than the sum of the retransmission value of the peer.
  5123. * See [SCTP] for more information.
  5124. *
  5125. */
  5126. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  5127. char __user *optval,
  5128. int __user *optlen)
  5129. {
  5130. struct sctp_assocparams assocparams;
  5131. struct sctp_association *asoc;
  5132. struct list_head *pos;
  5133. int cnt = 0;
  5134. if (len < sizeof (struct sctp_assocparams))
  5135. return -EINVAL;
  5136. len = sizeof(struct sctp_assocparams);
  5137. if (copy_from_user(&assocparams, optval, len))
  5138. return -EFAULT;
  5139. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  5140. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  5141. return -EINVAL;
  5142. /* Values correspoinding to the specific association */
  5143. if (asoc) {
  5144. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  5145. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  5146. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  5147. assocparams.sasoc_cookie_life = ktime_to_ms(asoc->cookie_life);
  5148. list_for_each(pos, &asoc->peer.transport_addr_list) {
  5149. cnt++;
  5150. }
  5151. assocparams.sasoc_number_peer_destinations = cnt;
  5152. } else {
  5153. /* Values corresponding to the endpoint */
  5154. struct sctp_sock *sp = sctp_sk(sk);
  5155. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  5156. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  5157. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  5158. assocparams.sasoc_cookie_life =
  5159. sp->assocparams.sasoc_cookie_life;
  5160. assocparams.sasoc_number_peer_destinations =
  5161. sp->assocparams.
  5162. sasoc_number_peer_destinations;
  5163. }
  5164. if (put_user(len, optlen))
  5165. return -EFAULT;
  5166. if (copy_to_user(optval, &assocparams, len))
  5167. return -EFAULT;
  5168. return 0;
  5169. }
  5170. /*
  5171. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  5172. *
  5173. * This socket option is a boolean flag which turns on or off mapped V4
  5174. * addresses. If this option is turned on and the socket is type
  5175. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  5176. * If this option is turned off, then no mapping will be done of V4
  5177. * addresses and a user will receive both PF_INET6 and PF_INET type
  5178. * addresses on the socket.
  5179. */
  5180. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  5181. char __user *optval, int __user *optlen)
  5182. {
  5183. int val;
  5184. struct sctp_sock *sp = sctp_sk(sk);
  5185. if (len < sizeof(int))
  5186. return -EINVAL;
  5187. len = sizeof(int);
  5188. val = sp->v4mapped;
  5189. if (put_user(len, optlen))
  5190. return -EFAULT;
  5191. if (copy_to_user(optval, &val, len))
  5192. return -EFAULT;
  5193. return 0;
  5194. }
  5195. /*
  5196. * 7.1.29. Set or Get the default context (SCTP_CONTEXT)
  5197. * (chapter and verse is quoted at sctp_setsockopt_context())
  5198. */
  5199. static int sctp_getsockopt_context(struct sock *sk, int len,
  5200. char __user *optval, int __user *optlen)
  5201. {
  5202. struct sctp_assoc_value params;
  5203. struct sctp_sock *sp;
  5204. struct sctp_association *asoc;
  5205. if (len < sizeof(struct sctp_assoc_value))
  5206. return -EINVAL;
  5207. len = sizeof(struct sctp_assoc_value);
  5208. if (copy_from_user(&params, optval, len))
  5209. return -EFAULT;
  5210. sp = sctp_sk(sk);
  5211. if (params.assoc_id != 0) {
  5212. asoc = sctp_id2assoc(sk, params.assoc_id);
  5213. if (!asoc)
  5214. return -EINVAL;
  5215. params.assoc_value = asoc->default_rcv_context;
  5216. } else {
  5217. params.assoc_value = sp->default_rcv_context;
  5218. }
  5219. if (put_user(len, optlen))
  5220. return -EFAULT;
  5221. if (copy_to_user(optval, &params, len))
  5222. return -EFAULT;
  5223. return 0;
  5224. }
  5225. /*
  5226. * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG)
  5227. * This option will get or set the maximum size to put in any outgoing
  5228. * SCTP DATA chunk. If a message is larger than this size it will be
  5229. * fragmented by SCTP into the specified size. Note that the underlying
  5230. * SCTP implementation may fragment into smaller sized chunks when the
  5231. * PMTU of the underlying association is smaller than the value set by
  5232. * the user. The default value for this option is '0' which indicates
  5233. * the user is NOT limiting fragmentation and only the PMTU will effect
  5234. * SCTP's choice of DATA chunk size. Note also that values set larger
  5235. * than the maximum size of an IP datagram will effectively let SCTP
  5236. * control fragmentation (i.e. the same as setting this option to 0).
  5237. *
  5238. * The following structure is used to access and modify this parameter:
  5239. *
  5240. * struct sctp_assoc_value {
  5241. * sctp_assoc_t assoc_id;
  5242. * uint32_t assoc_value;
  5243. * };
  5244. *
  5245. * assoc_id: This parameter is ignored for one-to-one style sockets.
  5246. * For one-to-many style sockets this parameter indicates which
  5247. * association the user is performing an action upon. Note that if
  5248. * this field's value is zero then the endpoints default value is
  5249. * changed (effecting future associations only).
  5250. * assoc_value: This parameter specifies the maximum size in bytes.
  5251. */
  5252. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  5253. char __user *optval, int __user *optlen)
  5254. {
  5255. struct sctp_assoc_value params;
  5256. struct sctp_association *asoc;
  5257. if (len == sizeof(int)) {
  5258. pr_warn_ratelimited(DEPRECATED
  5259. "%s (pid %d) "
  5260. "Use of int in maxseg socket option.\n"
  5261. "Use struct sctp_assoc_value instead\n",
  5262. current->comm, task_pid_nr(current));
  5263. params.assoc_id = 0;
  5264. } else if (len >= sizeof(struct sctp_assoc_value)) {
  5265. len = sizeof(struct sctp_assoc_value);
  5266. if (copy_from_user(&params, optval, len))
  5267. return -EFAULT;
  5268. } else
  5269. return -EINVAL;
  5270. asoc = sctp_id2assoc(sk, params.assoc_id);
  5271. if (!asoc && params.assoc_id && sctp_style(sk, UDP))
  5272. return -EINVAL;
  5273. if (asoc)
  5274. params.assoc_value = asoc->frag_point;
  5275. else
  5276. params.assoc_value = sctp_sk(sk)->user_frag;
  5277. if (put_user(len, optlen))
  5278. return -EFAULT;
  5279. if (len == sizeof(int)) {
  5280. if (copy_to_user(optval, &params.assoc_value, len))
  5281. return -EFAULT;
  5282. } else {
  5283. if (copy_to_user(optval, &params, len))
  5284. return -EFAULT;
  5285. }
  5286. return 0;
  5287. }
  5288. /*
  5289. * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE)
  5290. * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave())
  5291. */
  5292. static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len,
  5293. char __user *optval, int __user *optlen)
  5294. {
  5295. int val;
  5296. if (len < sizeof(int))
  5297. return -EINVAL;
  5298. len = sizeof(int);
  5299. val = sctp_sk(sk)->frag_interleave;
  5300. if (put_user(len, optlen))
  5301. return -EFAULT;
  5302. if (copy_to_user(optval, &val, len))
  5303. return -EFAULT;
  5304. return 0;
  5305. }
  5306. /*
  5307. * 7.1.25. Set or Get the sctp partial delivery point
  5308. * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point())
  5309. */
  5310. static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len,
  5311. char __user *optval,
  5312. int __user *optlen)
  5313. {
  5314. u32 val;
  5315. if (len < sizeof(u32))
  5316. return -EINVAL;
  5317. len = sizeof(u32);
  5318. val = sctp_sk(sk)->pd_point;
  5319. if (put_user(len, optlen))
  5320. return -EFAULT;
  5321. if (copy_to_user(optval, &val, len))
  5322. return -EFAULT;
  5323. return 0;
  5324. }
  5325. /*
  5326. * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST)
  5327. * (chapter and verse is quoted at sctp_setsockopt_maxburst())
  5328. */
  5329. static int sctp_getsockopt_maxburst(struct sock *sk, int len,
  5330. char __user *optval,
  5331. int __user *optlen)
  5332. {
  5333. struct sctp_assoc_value params;
  5334. struct sctp_sock *sp;
  5335. struct sctp_association *asoc;
  5336. if (len == sizeof(int)) {
  5337. pr_warn_ratelimited(DEPRECATED
  5338. "%s (pid %d) "
  5339. "Use of int in max_burst socket option.\n"
  5340. "Use struct sctp_assoc_value instead\n",
  5341. current->comm, task_pid_nr(current));
  5342. params.assoc_id = 0;
  5343. } else if (len >= sizeof(struct sctp_assoc_value)) {
  5344. len = sizeof(struct sctp_assoc_value);
  5345. if (copy_from_user(&params, optval, len))
  5346. return -EFAULT;
  5347. } else
  5348. return -EINVAL;
  5349. sp = sctp_sk(sk);
  5350. if (params.assoc_id != 0) {
  5351. asoc = sctp_id2assoc(sk, params.assoc_id);
  5352. if (!asoc)
  5353. return -EINVAL;
  5354. params.assoc_value = asoc->max_burst;
  5355. } else
  5356. params.assoc_value = sp->max_burst;
  5357. if (len == sizeof(int)) {
  5358. if (copy_to_user(optval, &params.assoc_value, len))
  5359. return -EFAULT;
  5360. } else {
  5361. if (copy_to_user(optval, &params, len))
  5362. return -EFAULT;
  5363. }
  5364. return 0;
  5365. }
  5366. static int sctp_getsockopt_hmac_ident(struct sock *sk, int len,
  5367. char __user *optval, int __user *optlen)
  5368. {
  5369. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5370. struct sctp_hmacalgo __user *p = (void __user *)optval;
  5371. struct sctp_hmac_algo_param *hmacs;
  5372. __u16 data_len = 0;
  5373. u32 num_idents;
  5374. int i;
  5375. if (!ep->auth_enable)
  5376. return -EACCES;
  5377. hmacs = ep->auth_hmacs_list;
  5378. data_len = ntohs(hmacs->param_hdr.length) -
  5379. sizeof(struct sctp_paramhdr);
  5380. if (len < sizeof(struct sctp_hmacalgo) + data_len)
  5381. return -EINVAL;
  5382. len = sizeof(struct sctp_hmacalgo) + data_len;
  5383. num_idents = data_len / sizeof(u16);
  5384. if (put_user(len, optlen))
  5385. return -EFAULT;
  5386. if (put_user(num_idents, &p->shmac_num_idents))
  5387. return -EFAULT;
  5388. for (i = 0; i < num_idents; i++) {
  5389. __u16 hmacid = ntohs(hmacs->hmac_ids[i]);
  5390. if (copy_to_user(&p->shmac_idents[i], &hmacid, sizeof(__u16)))
  5391. return -EFAULT;
  5392. }
  5393. return 0;
  5394. }
  5395. static int sctp_getsockopt_active_key(struct sock *sk, int len,
  5396. char __user *optval, int __user *optlen)
  5397. {
  5398. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5399. struct sctp_authkeyid val;
  5400. struct sctp_association *asoc;
  5401. if (!ep->auth_enable)
  5402. return -EACCES;
  5403. if (len < sizeof(struct sctp_authkeyid))
  5404. return -EINVAL;
  5405. len = sizeof(struct sctp_authkeyid);
  5406. if (copy_from_user(&val, optval, len))
  5407. return -EFAULT;
  5408. asoc = sctp_id2assoc(sk, val.scact_assoc_id);
  5409. if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP))
  5410. return -EINVAL;
  5411. if (asoc)
  5412. val.scact_keynumber = asoc->active_key_id;
  5413. else
  5414. val.scact_keynumber = ep->active_key_id;
  5415. if (put_user(len, optlen))
  5416. return -EFAULT;
  5417. if (copy_to_user(optval, &val, len))
  5418. return -EFAULT;
  5419. return 0;
  5420. }
  5421. static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len,
  5422. char __user *optval, int __user *optlen)
  5423. {
  5424. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5425. struct sctp_authchunks __user *p = (void __user *)optval;
  5426. struct sctp_authchunks val;
  5427. struct sctp_association *asoc;
  5428. struct sctp_chunks_param *ch;
  5429. u32 num_chunks = 0;
  5430. char __user *to;
  5431. if (!ep->auth_enable)
  5432. return -EACCES;
  5433. if (len < sizeof(struct sctp_authchunks))
  5434. return -EINVAL;
  5435. if (copy_from_user(&val, optval, sizeof(val)))
  5436. return -EFAULT;
  5437. to = p->gauth_chunks;
  5438. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  5439. if (!asoc)
  5440. return -EINVAL;
  5441. ch = asoc->peer.peer_chunks;
  5442. if (!ch)
  5443. goto num;
  5444. /* See if the user provided enough room for all the data */
  5445. num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
  5446. if (len < num_chunks)
  5447. return -EINVAL;
  5448. if (copy_to_user(to, ch->chunks, num_chunks))
  5449. return -EFAULT;
  5450. num:
  5451. len = sizeof(struct sctp_authchunks) + num_chunks;
  5452. if (put_user(len, optlen))
  5453. return -EFAULT;
  5454. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  5455. return -EFAULT;
  5456. return 0;
  5457. }
  5458. static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len,
  5459. char __user *optval, int __user *optlen)
  5460. {
  5461. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  5462. struct sctp_authchunks __user *p = (void __user *)optval;
  5463. struct sctp_authchunks val;
  5464. struct sctp_association *asoc;
  5465. struct sctp_chunks_param *ch;
  5466. u32 num_chunks = 0;
  5467. char __user *to;
  5468. if (!ep->auth_enable)
  5469. return -EACCES;
  5470. if (len < sizeof(struct sctp_authchunks))
  5471. return -EINVAL;
  5472. if (copy_from_user(&val, optval, sizeof(val)))
  5473. return -EFAULT;
  5474. to = p->gauth_chunks;
  5475. asoc = sctp_id2assoc(sk, val.gauth_assoc_id);
  5476. if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP))
  5477. return -EINVAL;
  5478. if (asoc)
  5479. ch = (struct sctp_chunks_param *)asoc->c.auth_chunks;
  5480. else
  5481. ch = ep->auth_chunk_list;
  5482. if (!ch)
  5483. goto num;
  5484. num_chunks = ntohs(ch->param_hdr.length) - sizeof(struct sctp_paramhdr);
  5485. if (len < sizeof(struct sctp_authchunks) + num_chunks)
  5486. return -EINVAL;
  5487. if (copy_to_user(to, ch->chunks, num_chunks))
  5488. return -EFAULT;
  5489. num:
  5490. len = sizeof(struct sctp_authchunks) + num_chunks;
  5491. if (put_user(len, optlen))
  5492. return -EFAULT;
  5493. if (put_user(num_chunks, &p->gauth_number_of_chunks))
  5494. return -EFAULT;
  5495. return 0;
  5496. }
  5497. /*
  5498. * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER)
  5499. * This option gets the current number of associations that are attached
  5500. * to a one-to-many style socket. The option value is an uint32_t.
  5501. */
  5502. static int sctp_getsockopt_assoc_number(struct sock *sk, int len,
  5503. char __user *optval, int __user *optlen)
  5504. {
  5505. struct sctp_sock *sp = sctp_sk(sk);
  5506. struct sctp_association *asoc;
  5507. u32 val = 0;
  5508. if (sctp_style(sk, TCP))
  5509. return -EOPNOTSUPP;
  5510. if (len < sizeof(u32))
  5511. return -EINVAL;
  5512. len = sizeof(u32);
  5513. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  5514. val++;
  5515. }
  5516. if (put_user(len, optlen))
  5517. return -EFAULT;
  5518. if (copy_to_user(optval, &val, len))
  5519. return -EFAULT;
  5520. return 0;
  5521. }
  5522. /*
  5523. * 8.1.23 SCTP_AUTO_ASCONF
  5524. * See the corresponding setsockopt entry as description
  5525. */
  5526. static int sctp_getsockopt_auto_asconf(struct sock *sk, int len,
  5527. char __user *optval, int __user *optlen)
  5528. {
  5529. int val = 0;
  5530. if (len < sizeof(int))
  5531. return -EINVAL;
  5532. len = sizeof(int);
  5533. if (sctp_sk(sk)->do_auto_asconf && sctp_is_ep_boundall(sk))
  5534. val = 1;
  5535. if (put_user(len, optlen))
  5536. return -EFAULT;
  5537. if (copy_to_user(optval, &val, len))
  5538. return -EFAULT;
  5539. return 0;
  5540. }
  5541. /*
  5542. * 8.2.6. Get the Current Identifiers of Associations
  5543. * (SCTP_GET_ASSOC_ID_LIST)
  5544. *
  5545. * This option gets the current list of SCTP association identifiers of
  5546. * the SCTP associations handled by a one-to-many style socket.
  5547. */
  5548. static int sctp_getsockopt_assoc_ids(struct sock *sk, int len,
  5549. char __user *optval, int __user *optlen)
  5550. {
  5551. struct sctp_sock *sp = sctp_sk(sk);
  5552. struct sctp_association *asoc;
  5553. struct sctp_assoc_ids *ids;
  5554. u32 num = 0;
  5555. if (sctp_style(sk, TCP))
  5556. return -EOPNOTSUPP;
  5557. if (len < sizeof(struct sctp_assoc_ids))
  5558. return -EINVAL;
  5559. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  5560. num++;
  5561. }
  5562. if (len < sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num)
  5563. return -EINVAL;
  5564. len = sizeof(struct sctp_assoc_ids) + sizeof(sctp_assoc_t) * num;
  5565. ids = kmalloc(len, GFP_USER | __GFP_NOWARN);
  5566. if (unlikely(!ids))
  5567. return -ENOMEM;
  5568. ids->gaids_number_of_ids = num;
  5569. num = 0;
  5570. list_for_each_entry(asoc, &(sp->ep->asocs), asocs) {
  5571. ids->gaids_assoc_id[num++] = asoc->assoc_id;
  5572. }
  5573. if (put_user(len, optlen) || copy_to_user(optval, ids, len)) {
  5574. kfree(ids);
  5575. return -EFAULT;
  5576. }
  5577. kfree(ids);
  5578. return 0;
  5579. }
  5580. /*
  5581. * SCTP_PEER_ADDR_THLDS
  5582. *
  5583. * This option allows us to fetch the partially failed threshold for one or all
  5584. * transports in an association. See Section 6.1 of:
  5585. * http://www.ietf.org/id/draft-nishida-tsvwg-sctp-failover-05.txt
  5586. */
  5587. static int sctp_getsockopt_paddr_thresholds(struct sock *sk,
  5588. char __user *optval,
  5589. int len,
  5590. int __user *optlen)
  5591. {
  5592. struct sctp_paddrthlds val;
  5593. struct sctp_transport *trans;
  5594. struct sctp_association *asoc;
  5595. if (len < sizeof(struct sctp_paddrthlds))
  5596. return -EINVAL;
  5597. len = sizeof(struct sctp_paddrthlds);
  5598. if (copy_from_user(&val, (struct sctp_paddrthlds __user *)optval, len))
  5599. return -EFAULT;
  5600. if (sctp_is_any(sk, (const union sctp_addr *)&val.spt_address)) {
  5601. asoc = sctp_id2assoc(sk, val.spt_assoc_id);
  5602. if (!asoc)
  5603. return -ENOENT;
  5604. val.spt_pathpfthld = asoc->pf_retrans;
  5605. val.spt_pathmaxrxt = asoc->pathmaxrxt;
  5606. } else {
  5607. trans = sctp_addr_id2transport(sk, &val.spt_address,
  5608. val.spt_assoc_id);
  5609. if (!trans)
  5610. return -ENOENT;
  5611. val.spt_pathmaxrxt = trans->pathmaxrxt;
  5612. val.spt_pathpfthld = trans->pf_retrans;
  5613. }
  5614. if (put_user(len, optlen) || copy_to_user(optval, &val, len))
  5615. return -EFAULT;
  5616. return 0;
  5617. }
  5618. /*
  5619. * SCTP_GET_ASSOC_STATS
  5620. *
  5621. * This option retrieves local per endpoint statistics. It is modeled
  5622. * after OpenSolaris' implementation
  5623. */
  5624. static int sctp_getsockopt_assoc_stats(struct sock *sk, int len,
  5625. char __user *optval,
  5626. int __user *optlen)
  5627. {
  5628. struct sctp_assoc_stats sas;
  5629. struct sctp_association *asoc = NULL;
  5630. /* User must provide at least the assoc id */
  5631. if (len < sizeof(sctp_assoc_t))
  5632. return -EINVAL;
  5633. /* Allow the struct to grow and fill in as much as possible */
  5634. len = min_t(size_t, len, sizeof(sas));
  5635. if (copy_from_user(&sas, optval, len))
  5636. return -EFAULT;
  5637. asoc = sctp_id2assoc(sk, sas.sas_assoc_id);
  5638. if (!asoc)
  5639. return -EINVAL;
  5640. sas.sas_rtxchunks = asoc->stats.rtxchunks;
  5641. sas.sas_gapcnt = asoc->stats.gapcnt;
  5642. sas.sas_outofseqtsns = asoc->stats.outofseqtsns;
  5643. sas.sas_osacks = asoc->stats.osacks;
  5644. sas.sas_isacks = asoc->stats.isacks;
  5645. sas.sas_octrlchunks = asoc->stats.octrlchunks;
  5646. sas.sas_ictrlchunks = asoc->stats.ictrlchunks;
  5647. sas.sas_oodchunks = asoc->stats.oodchunks;
  5648. sas.sas_iodchunks = asoc->stats.iodchunks;
  5649. sas.sas_ouodchunks = asoc->stats.ouodchunks;
  5650. sas.sas_iuodchunks = asoc->stats.iuodchunks;
  5651. sas.sas_idupchunks = asoc->stats.idupchunks;
  5652. sas.sas_opackets = asoc->stats.opackets;
  5653. sas.sas_ipackets = asoc->stats.ipackets;
  5654. /* New high max rto observed, will return 0 if not a single
  5655. * RTO update took place. obs_rto_ipaddr will be bogus
  5656. * in such a case
  5657. */
  5658. sas.sas_maxrto = asoc->stats.max_obs_rto;
  5659. memcpy(&sas.sas_obs_rto_ipaddr, &asoc->stats.obs_rto_ipaddr,
  5660. sizeof(struct sockaddr_storage));
  5661. /* Mark beginning of a new observation period */
  5662. asoc->stats.max_obs_rto = asoc->rto_min;
  5663. if (put_user(len, optlen))
  5664. return -EFAULT;
  5665. pr_debug("%s: len:%d, assoc_id:%d\n", __func__, len, sas.sas_assoc_id);
  5666. if (copy_to_user(optval, &sas, len))
  5667. return -EFAULT;
  5668. return 0;
  5669. }
  5670. static int sctp_getsockopt_recvrcvinfo(struct sock *sk, int len,
  5671. char __user *optval,
  5672. int __user *optlen)
  5673. {
  5674. int val = 0;
  5675. if (len < sizeof(int))
  5676. return -EINVAL;
  5677. len = sizeof(int);
  5678. if (sctp_sk(sk)->recvrcvinfo)
  5679. val = 1;
  5680. if (put_user(len, optlen))
  5681. return -EFAULT;
  5682. if (copy_to_user(optval, &val, len))
  5683. return -EFAULT;
  5684. return 0;
  5685. }
  5686. static int sctp_getsockopt_recvnxtinfo(struct sock *sk, int len,
  5687. char __user *optval,
  5688. int __user *optlen)
  5689. {
  5690. int val = 0;
  5691. if (len < sizeof(int))
  5692. return -EINVAL;
  5693. len = sizeof(int);
  5694. if (sctp_sk(sk)->recvnxtinfo)
  5695. val = 1;
  5696. if (put_user(len, optlen))
  5697. return -EFAULT;
  5698. if (copy_to_user(optval, &val, len))
  5699. return -EFAULT;
  5700. return 0;
  5701. }
  5702. static int sctp_getsockopt_pr_supported(struct sock *sk, int len,
  5703. char __user *optval,
  5704. int __user *optlen)
  5705. {
  5706. struct sctp_assoc_value params;
  5707. struct sctp_association *asoc;
  5708. int retval = -EFAULT;
  5709. if (len < sizeof(params)) {
  5710. retval = -EINVAL;
  5711. goto out;
  5712. }
  5713. len = sizeof(params);
  5714. if (copy_from_user(&params, optval, len))
  5715. goto out;
  5716. asoc = sctp_id2assoc(sk, params.assoc_id);
  5717. if (asoc) {
  5718. params.assoc_value = asoc->prsctp_enable;
  5719. } else if (!params.assoc_id) {
  5720. struct sctp_sock *sp = sctp_sk(sk);
  5721. params.assoc_value = sp->ep->prsctp_enable;
  5722. } else {
  5723. retval = -EINVAL;
  5724. goto out;
  5725. }
  5726. if (put_user(len, optlen))
  5727. goto out;
  5728. if (copy_to_user(optval, &params, len))
  5729. goto out;
  5730. retval = 0;
  5731. out:
  5732. return retval;
  5733. }
  5734. static int sctp_getsockopt_default_prinfo(struct sock *sk, int len,
  5735. char __user *optval,
  5736. int __user *optlen)
  5737. {
  5738. struct sctp_default_prinfo info;
  5739. struct sctp_association *asoc;
  5740. int retval = -EFAULT;
  5741. if (len < sizeof(info)) {
  5742. retval = -EINVAL;
  5743. goto out;
  5744. }
  5745. len = sizeof(info);
  5746. if (copy_from_user(&info, optval, len))
  5747. goto out;
  5748. asoc = sctp_id2assoc(sk, info.pr_assoc_id);
  5749. if (asoc) {
  5750. info.pr_policy = SCTP_PR_POLICY(asoc->default_flags);
  5751. info.pr_value = asoc->default_timetolive;
  5752. } else if (!info.pr_assoc_id) {
  5753. struct sctp_sock *sp = sctp_sk(sk);
  5754. info.pr_policy = SCTP_PR_POLICY(sp->default_flags);
  5755. info.pr_value = sp->default_timetolive;
  5756. } else {
  5757. retval = -EINVAL;
  5758. goto out;
  5759. }
  5760. if (put_user(len, optlen))
  5761. goto out;
  5762. if (copy_to_user(optval, &info, len))
  5763. goto out;
  5764. retval = 0;
  5765. out:
  5766. return retval;
  5767. }
  5768. static int sctp_getsockopt_pr_assocstatus(struct sock *sk, int len,
  5769. char __user *optval,
  5770. int __user *optlen)
  5771. {
  5772. struct sctp_prstatus params;
  5773. struct sctp_association *asoc;
  5774. int policy;
  5775. int retval = -EINVAL;
  5776. if (len < sizeof(params))
  5777. goto out;
  5778. len = sizeof(params);
  5779. if (copy_from_user(&params, optval, len)) {
  5780. retval = -EFAULT;
  5781. goto out;
  5782. }
  5783. policy = params.sprstat_policy;
  5784. if (policy & ~SCTP_PR_SCTP_MASK)
  5785. goto out;
  5786. asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
  5787. if (!asoc)
  5788. goto out;
  5789. if (policy == SCTP_PR_SCTP_NONE) {
  5790. params.sprstat_abandoned_unsent = 0;
  5791. params.sprstat_abandoned_sent = 0;
  5792. for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
  5793. params.sprstat_abandoned_unsent +=
  5794. asoc->abandoned_unsent[policy];
  5795. params.sprstat_abandoned_sent +=
  5796. asoc->abandoned_sent[policy];
  5797. }
  5798. } else {
  5799. params.sprstat_abandoned_unsent =
  5800. asoc->abandoned_unsent[__SCTP_PR_INDEX(policy)];
  5801. params.sprstat_abandoned_sent =
  5802. asoc->abandoned_sent[__SCTP_PR_INDEX(policy)];
  5803. }
  5804. if (put_user(len, optlen)) {
  5805. retval = -EFAULT;
  5806. goto out;
  5807. }
  5808. if (copy_to_user(optval, &params, len)) {
  5809. retval = -EFAULT;
  5810. goto out;
  5811. }
  5812. retval = 0;
  5813. out:
  5814. return retval;
  5815. }
  5816. static int sctp_getsockopt_pr_streamstatus(struct sock *sk, int len,
  5817. char __user *optval,
  5818. int __user *optlen)
  5819. {
  5820. struct sctp_stream_out_ext *streamoute;
  5821. struct sctp_association *asoc;
  5822. struct sctp_prstatus params;
  5823. int retval = -EINVAL;
  5824. int policy;
  5825. if (len < sizeof(params))
  5826. goto out;
  5827. len = sizeof(params);
  5828. if (copy_from_user(&params, optval, len)) {
  5829. retval = -EFAULT;
  5830. goto out;
  5831. }
  5832. policy = params.sprstat_policy;
  5833. if (policy & ~SCTP_PR_SCTP_MASK)
  5834. goto out;
  5835. asoc = sctp_id2assoc(sk, params.sprstat_assoc_id);
  5836. if (!asoc || params.sprstat_sid >= asoc->stream.outcnt)
  5837. goto out;
  5838. streamoute = asoc->stream.out[params.sprstat_sid].ext;
  5839. if (!streamoute) {
  5840. /* Not allocated yet, means all stats are 0 */
  5841. params.sprstat_abandoned_unsent = 0;
  5842. params.sprstat_abandoned_sent = 0;
  5843. retval = 0;
  5844. goto out;
  5845. }
  5846. if (policy == SCTP_PR_SCTP_NONE) {
  5847. params.sprstat_abandoned_unsent = 0;
  5848. params.sprstat_abandoned_sent = 0;
  5849. for (policy = 0; policy <= SCTP_PR_INDEX(MAX); policy++) {
  5850. params.sprstat_abandoned_unsent +=
  5851. streamoute->abandoned_unsent[policy];
  5852. params.sprstat_abandoned_sent +=
  5853. streamoute->abandoned_sent[policy];
  5854. }
  5855. } else {
  5856. params.sprstat_abandoned_unsent =
  5857. streamoute->abandoned_unsent[__SCTP_PR_INDEX(policy)];
  5858. params.sprstat_abandoned_sent =
  5859. streamoute->abandoned_sent[__SCTP_PR_INDEX(policy)];
  5860. }
  5861. if (put_user(len, optlen) || copy_to_user(optval, &params, len)) {
  5862. retval = -EFAULT;
  5863. goto out;
  5864. }
  5865. retval = 0;
  5866. out:
  5867. return retval;
  5868. }
  5869. static int sctp_getsockopt_reconfig_supported(struct sock *sk, int len,
  5870. char __user *optval,
  5871. int __user *optlen)
  5872. {
  5873. struct sctp_assoc_value params;
  5874. struct sctp_association *asoc;
  5875. int retval = -EFAULT;
  5876. if (len < sizeof(params)) {
  5877. retval = -EINVAL;
  5878. goto out;
  5879. }
  5880. len = sizeof(params);
  5881. if (copy_from_user(&params, optval, len))
  5882. goto out;
  5883. asoc = sctp_id2assoc(sk, params.assoc_id);
  5884. if (asoc) {
  5885. params.assoc_value = asoc->reconf_enable;
  5886. } else if (!params.assoc_id) {
  5887. struct sctp_sock *sp = sctp_sk(sk);
  5888. params.assoc_value = sp->ep->reconf_enable;
  5889. } else {
  5890. retval = -EINVAL;
  5891. goto out;
  5892. }
  5893. if (put_user(len, optlen))
  5894. goto out;
  5895. if (copy_to_user(optval, &params, len))
  5896. goto out;
  5897. retval = 0;
  5898. out:
  5899. return retval;
  5900. }
  5901. static int sctp_getsockopt_enable_strreset(struct sock *sk, int len,
  5902. char __user *optval,
  5903. int __user *optlen)
  5904. {
  5905. struct sctp_assoc_value params;
  5906. struct sctp_association *asoc;
  5907. int retval = -EFAULT;
  5908. if (len < sizeof(params)) {
  5909. retval = -EINVAL;
  5910. goto out;
  5911. }
  5912. len = sizeof(params);
  5913. if (copy_from_user(&params, optval, len))
  5914. goto out;
  5915. asoc = sctp_id2assoc(sk, params.assoc_id);
  5916. if (asoc) {
  5917. params.assoc_value = asoc->strreset_enable;
  5918. } else if (!params.assoc_id) {
  5919. struct sctp_sock *sp = sctp_sk(sk);
  5920. params.assoc_value = sp->ep->strreset_enable;
  5921. } else {
  5922. retval = -EINVAL;
  5923. goto out;
  5924. }
  5925. if (put_user(len, optlen))
  5926. goto out;
  5927. if (copy_to_user(optval, &params, len))
  5928. goto out;
  5929. retval = 0;
  5930. out:
  5931. return retval;
  5932. }
  5933. static int sctp_getsockopt_scheduler(struct sock *sk, int len,
  5934. char __user *optval,
  5935. int __user *optlen)
  5936. {
  5937. struct sctp_assoc_value params;
  5938. struct sctp_association *asoc;
  5939. int retval = -EFAULT;
  5940. if (len < sizeof(params)) {
  5941. retval = -EINVAL;
  5942. goto out;
  5943. }
  5944. len = sizeof(params);
  5945. if (copy_from_user(&params, optval, len))
  5946. goto out;
  5947. asoc = sctp_id2assoc(sk, params.assoc_id);
  5948. if (!asoc) {
  5949. retval = -EINVAL;
  5950. goto out;
  5951. }
  5952. params.assoc_value = sctp_sched_get_sched(asoc);
  5953. if (put_user(len, optlen))
  5954. goto out;
  5955. if (copy_to_user(optval, &params, len))
  5956. goto out;
  5957. retval = 0;
  5958. out:
  5959. return retval;
  5960. }
  5961. static int sctp_getsockopt_scheduler_value(struct sock *sk, int len,
  5962. char __user *optval,
  5963. int __user *optlen)
  5964. {
  5965. struct sctp_stream_value params;
  5966. struct sctp_association *asoc;
  5967. int retval = -EFAULT;
  5968. if (len < sizeof(params)) {
  5969. retval = -EINVAL;
  5970. goto out;
  5971. }
  5972. len = sizeof(params);
  5973. if (copy_from_user(&params, optval, len))
  5974. goto out;
  5975. asoc = sctp_id2assoc(sk, params.assoc_id);
  5976. if (!asoc) {
  5977. retval = -EINVAL;
  5978. goto out;
  5979. }
  5980. retval = sctp_sched_get_value(asoc, params.stream_id,
  5981. &params.stream_value);
  5982. if (retval)
  5983. goto out;
  5984. if (put_user(len, optlen)) {
  5985. retval = -EFAULT;
  5986. goto out;
  5987. }
  5988. if (copy_to_user(optval, &params, len)) {
  5989. retval = -EFAULT;
  5990. goto out;
  5991. }
  5992. out:
  5993. return retval;
  5994. }
  5995. static int sctp_getsockopt_interleaving_supported(struct sock *sk, int len,
  5996. char __user *optval,
  5997. int __user *optlen)
  5998. {
  5999. struct sctp_assoc_value params;
  6000. struct sctp_association *asoc;
  6001. int retval = -EFAULT;
  6002. if (len < sizeof(params)) {
  6003. retval = -EINVAL;
  6004. goto out;
  6005. }
  6006. len = sizeof(params);
  6007. if (copy_from_user(&params, optval, len))
  6008. goto out;
  6009. asoc = sctp_id2assoc(sk, params.assoc_id);
  6010. if (asoc) {
  6011. params.assoc_value = asoc->intl_enable;
  6012. } else if (!params.assoc_id) {
  6013. struct sctp_sock *sp = sctp_sk(sk);
  6014. params.assoc_value = sp->strm_interleave;
  6015. } else {
  6016. retval = -EINVAL;
  6017. goto out;
  6018. }
  6019. if (put_user(len, optlen))
  6020. goto out;
  6021. if (copy_to_user(optval, &params, len))
  6022. goto out;
  6023. retval = 0;
  6024. out:
  6025. return retval;
  6026. }
  6027. static int sctp_getsockopt(struct sock *sk, int level, int optname,
  6028. char __user *optval, int __user *optlen)
  6029. {
  6030. int retval = 0;
  6031. int len;
  6032. pr_debug("%s: sk:%p, optname:%d\n", __func__, sk, optname);
  6033. /* I can hardly begin to describe how wrong this is. This is
  6034. * so broken as to be worse than useless. The API draft
  6035. * REALLY is NOT helpful here... I am not convinced that the
  6036. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  6037. * are at all well-founded.
  6038. */
  6039. if (level != SOL_SCTP) {
  6040. struct sctp_af *af = sctp_sk(sk)->pf->af;
  6041. retval = af->getsockopt(sk, level, optname, optval, optlen);
  6042. return retval;
  6043. }
  6044. if (get_user(len, optlen))
  6045. return -EFAULT;
  6046. if (len < 0)
  6047. return -EINVAL;
  6048. lock_sock(sk);
  6049. switch (optname) {
  6050. case SCTP_STATUS:
  6051. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  6052. break;
  6053. case SCTP_DISABLE_FRAGMENTS:
  6054. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  6055. optlen);
  6056. break;
  6057. case SCTP_EVENTS:
  6058. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  6059. break;
  6060. case SCTP_AUTOCLOSE:
  6061. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  6062. break;
  6063. case SCTP_SOCKOPT_PEELOFF:
  6064. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  6065. break;
  6066. case SCTP_SOCKOPT_PEELOFF_FLAGS:
  6067. retval = sctp_getsockopt_peeloff_flags(sk, len, optval, optlen);
  6068. break;
  6069. case SCTP_PEER_ADDR_PARAMS:
  6070. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  6071. optlen);
  6072. break;
  6073. case SCTP_DELAYED_SACK:
  6074. retval = sctp_getsockopt_delayed_ack(sk, len, optval,
  6075. optlen);
  6076. break;
  6077. case SCTP_INITMSG:
  6078. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  6079. break;
  6080. case SCTP_GET_PEER_ADDRS:
  6081. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  6082. optlen);
  6083. break;
  6084. case SCTP_GET_LOCAL_ADDRS:
  6085. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  6086. optlen);
  6087. break;
  6088. case SCTP_SOCKOPT_CONNECTX3:
  6089. retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
  6090. break;
  6091. case SCTP_DEFAULT_SEND_PARAM:
  6092. retval = sctp_getsockopt_default_send_param(sk, len,
  6093. optval, optlen);
  6094. break;
  6095. case SCTP_DEFAULT_SNDINFO:
  6096. retval = sctp_getsockopt_default_sndinfo(sk, len,
  6097. optval, optlen);
  6098. break;
  6099. case SCTP_PRIMARY_ADDR:
  6100. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  6101. break;
  6102. case SCTP_NODELAY:
  6103. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  6104. break;
  6105. case SCTP_RTOINFO:
  6106. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  6107. break;
  6108. case SCTP_ASSOCINFO:
  6109. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  6110. break;
  6111. case SCTP_I_WANT_MAPPED_V4_ADDR:
  6112. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  6113. break;
  6114. case SCTP_MAXSEG:
  6115. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  6116. break;
  6117. case SCTP_GET_PEER_ADDR_INFO:
  6118. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  6119. optlen);
  6120. break;
  6121. case SCTP_ADAPTATION_LAYER:
  6122. retval = sctp_getsockopt_adaptation_layer(sk, len, optval,
  6123. optlen);
  6124. break;
  6125. case SCTP_CONTEXT:
  6126. retval = sctp_getsockopt_context(sk, len, optval, optlen);
  6127. break;
  6128. case SCTP_FRAGMENT_INTERLEAVE:
  6129. retval = sctp_getsockopt_fragment_interleave(sk, len, optval,
  6130. optlen);
  6131. break;
  6132. case SCTP_PARTIAL_DELIVERY_POINT:
  6133. retval = sctp_getsockopt_partial_delivery_point(sk, len, optval,
  6134. optlen);
  6135. break;
  6136. case SCTP_MAX_BURST:
  6137. retval = sctp_getsockopt_maxburst(sk, len, optval, optlen);
  6138. break;
  6139. case SCTP_AUTH_KEY:
  6140. case SCTP_AUTH_CHUNK:
  6141. case SCTP_AUTH_DELETE_KEY:
  6142. retval = -EOPNOTSUPP;
  6143. break;
  6144. case SCTP_HMAC_IDENT:
  6145. retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen);
  6146. break;
  6147. case SCTP_AUTH_ACTIVE_KEY:
  6148. retval = sctp_getsockopt_active_key(sk, len, optval, optlen);
  6149. break;
  6150. case SCTP_PEER_AUTH_CHUNKS:
  6151. retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval,
  6152. optlen);
  6153. break;
  6154. case SCTP_LOCAL_AUTH_CHUNKS:
  6155. retval = sctp_getsockopt_local_auth_chunks(sk, len, optval,
  6156. optlen);
  6157. break;
  6158. case SCTP_GET_ASSOC_NUMBER:
  6159. retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen);
  6160. break;
  6161. case SCTP_GET_ASSOC_ID_LIST:
  6162. retval = sctp_getsockopt_assoc_ids(sk, len, optval, optlen);
  6163. break;
  6164. case SCTP_AUTO_ASCONF:
  6165. retval = sctp_getsockopt_auto_asconf(sk, len, optval, optlen);
  6166. break;
  6167. case SCTP_PEER_ADDR_THLDS:
  6168. retval = sctp_getsockopt_paddr_thresholds(sk, optval, len, optlen);
  6169. break;
  6170. case SCTP_GET_ASSOC_STATS:
  6171. retval = sctp_getsockopt_assoc_stats(sk, len, optval, optlen);
  6172. break;
  6173. case SCTP_RECVRCVINFO:
  6174. retval = sctp_getsockopt_recvrcvinfo(sk, len, optval, optlen);
  6175. break;
  6176. case SCTP_RECVNXTINFO:
  6177. retval = sctp_getsockopt_recvnxtinfo(sk, len, optval, optlen);
  6178. break;
  6179. case SCTP_PR_SUPPORTED:
  6180. retval = sctp_getsockopt_pr_supported(sk, len, optval, optlen);
  6181. break;
  6182. case SCTP_DEFAULT_PRINFO:
  6183. retval = sctp_getsockopt_default_prinfo(sk, len, optval,
  6184. optlen);
  6185. break;
  6186. case SCTP_PR_ASSOC_STATUS:
  6187. retval = sctp_getsockopt_pr_assocstatus(sk, len, optval,
  6188. optlen);
  6189. break;
  6190. case SCTP_PR_STREAM_STATUS:
  6191. retval = sctp_getsockopt_pr_streamstatus(sk, len, optval,
  6192. optlen);
  6193. break;
  6194. case SCTP_RECONFIG_SUPPORTED:
  6195. retval = sctp_getsockopt_reconfig_supported(sk, len, optval,
  6196. optlen);
  6197. break;
  6198. case SCTP_ENABLE_STREAM_RESET:
  6199. retval = sctp_getsockopt_enable_strreset(sk, len, optval,
  6200. optlen);
  6201. break;
  6202. case SCTP_STREAM_SCHEDULER:
  6203. retval = sctp_getsockopt_scheduler(sk, len, optval,
  6204. optlen);
  6205. break;
  6206. case SCTP_STREAM_SCHEDULER_VALUE:
  6207. retval = sctp_getsockopt_scheduler_value(sk, len, optval,
  6208. optlen);
  6209. break;
  6210. case SCTP_INTERLEAVING_SUPPORTED:
  6211. retval = sctp_getsockopt_interleaving_supported(sk, len, optval,
  6212. optlen);
  6213. break;
  6214. default:
  6215. retval = -ENOPROTOOPT;
  6216. break;
  6217. }
  6218. release_sock(sk);
  6219. return retval;
  6220. }
  6221. static int sctp_hash(struct sock *sk)
  6222. {
  6223. /* STUB */
  6224. return 0;
  6225. }
  6226. static void sctp_unhash(struct sock *sk)
  6227. {
  6228. /* STUB */
  6229. }
  6230. /* Check if port is acceptable. Possibly find first available port.
  6231. *
  6232. * The port hash table (contained in the 'global' SCTP protocol storage
  6233. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  6234. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  6235. * list (the list number is the port number hashed out, so as you
  6236. * would expect from a hash function, all the ports in a given list have
  6237. * such a number that hashes out to the same list number; you were
  6238. * expecting that, right?); so each list has a set of ports, with a
  6239. * link to the socket (struct sock) that uses it, the port number and
  6240. * a fastreuse flag (FIXME: NPI ipg).
  6241. */
  6242. static struct sctp_bind_bucket *sctp_bucket_create(
  6243. struct sctp_bind_hashbucket *head, struct net *, unsigned short snum);
  6244. static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  6245. {
  6246. struct sctp_bind_hashbucket *head; /* hash list */
  6247. struct sctp_bind_bucket *pp;
  6248. unsigned short snum;
  6249. int ret;
  6250. snum = ntohs(addr->v4.sin_port);
  6251. pr_debug("%s: begins, snum:%d\n", __func__, snum);
  6252. local_bh_disable();
  6253. if (snum == 0) {
  6254. /* Search for an available port. */
  6255. int low, high, remaining, index;
  6256. unsigned int rover;
  6257. struct net *net = sock_net(sk);
  6258. inet_get_local_port_range(net, &low, &high);
  6259. remaining = (high - low) + 1;
  6260. rover = prandom_u32() % remaining + low;
  6261. do {
  6262. rover++;
  6263. if ((rover < low) || (rover > high))
  6264. rover = low;
  6265. if (inet_is_local_reserved_port(net, rover))
  6266. continue;
  6267. index = sctp_phashfn(sock_net(sk), rover);
  6268. head = &sctp_port_hashtable[index];
  6269. spin_lock(&head->lock);
  6270. sctp_for_each_hentry(pp, &head->chain)
  6271. if ((pp->port == rover) &&
  6272. net_eq(sock_net(sk), pp->net))
  6273. goto next;
  6274. break;
  6275. next:
  6276. spin_unlock(&head->lock);
  6277. } while (--remaining > 0);
  6278. /* Exhausted local port range during search? */
  6279. ret = 1;
  6280. if (remaining <= 0)
  6281. goto fail;
  6282. /* OK, here is the one we will use. HEAD (the port
  6283. * hash table list entry) is non-NULL and we hold it's
  6284. * mutex.
  6285. */
  6286. snum = rover;
  6287. } else {
  6288. /* We are given an specific port number; we verify
  6289. * that it is not being used. If it is used, we will
  6290. * exahust the search in the hash list corresponding
  6291. * to the port number (snum) - we detect that with the
  6292. * port iterator, pp being NULL.
  6293. */
  6294. head = &sctp_port_hashtable[sctp_phashfn(sock_net(sk), snum)];
  6295. spin_lock(&head->lock);
  6296. sctp_for_each_hentry(pp, &head->chain) {
  6297. if ((pp->port == snum) && net_eq(pp->net, sock_net(sk)))
  6298. goto pp_found;
  6299. }
  6300. }
  6301. pp = NULL;
  6302. goto pp_not_found;
  6303. pp_found:
  6304. if (!hlist_empty(&pp->owner)) {
  6305. /* We had a port hash table hit - there is an
  6306. * available port (pp != NULL) and it is being
  6307. * used by other socket (pp->owner not empty); that other
  6308. * socket is going to be sk2.
  6309. */
  6310. int reuse = sk->sk_reuse;
  6311. struct sock *sk2;
  6312. pr_debug("%s: found a possible match\n", __func__);
  6313. if (pp->fastreuse && sk->sk_reuse &&
  6314. sk->sk_state != SCTP_SS_LISTENING)
  6315. goto success;
  6316. /* Run through the list of sockets bound to the port
  6317. * (pp->port) [via the pointers bind_next and
  6318. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  6319. * we get the endpoint they describe and run through
  6320. * the endpoint's list of IP (v4 or v6) addresses,
  6321. * comparing each of the addresses with the address of
  6322. * the socket sk. If we find a match, then that means
  6323. * that this port/socket (sk) combination are already
  6324. * in an endpoint.
  6325. */
  6326. sk_for_each_bound(sk2, &pp->owner) {
  6327. struct sctp_endpoint *ep2;
  6328. ep2 = sctp_sk(sk2)->ep;
  6329. if (sk == sk2 ||
  6330. (reuse && sk2->sk_reuse &&
  6331. sk2->sk_state != SCTP_SS_LISTENING))
  6332. continue;
  6333. if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr,
  6334. sctp_sk(sk2), sctp_sk(sk))) {
  6335. ret = (long)sk2;
  6336. goto fail_unlock;
  6337. }
  6338. }
  6339. pr_debug("%s: found a match\n", __func__);
  6340. }
  6341. pp_not_found:
  6342. /* If there was a hash table miss, create a new port. */
  6343. ret = 1;
  6344. if (!pp && !(pp = sctp_bucket_create(head, sock_net(sk), snum)))
  6345. goto fail_unlock;
  6346. /* In either case (hit or miss), make sure fastreuse is 1 only
  6347. * if sk->sk_reuse is too (that is, if the caller requested
  6348. * SO_REUSEADDR on this socket -sk-).
  6349. */
  6350. if (hlist_empty(&pp->owner)) {
  6351. if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING)
  6352. pp->fastreuse = 1;
  6353. else
  6354. pp->fastreuse = 0;
  6355. } else if (pp->fastreuse &&
  6356. (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING))
  6357. pp->fastreuse = 0;
  6358. /* We are set, so fill up all the data in the hash table
  6359. * entry, tie the socket list information with the rest of the
  6360. * sockets FIXME: Blurry, NPI (ipg).
  6361. */
  6362. success:
  6363. if (!sctp_sk(sk)->bind_hash) {
  6364. inet_sk(sk)->inet_num = snum;
  6365. sk_add_bind_node(sk, &pp->owner);
  6366. sctp_sk(sk)->bind_hash = pp;
  6367. }
  6368. ret = 0;
  6369. fail_unlock:
  6370. spin_unlock(&head->lock);
  6371. fail:
  6372. local_bh_enable();
  6373. return ret;
  6374. }
  6375. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  6376. * port is requested.
  6377. */
  6378. static int sctp_get_port(struct sock *sk, unsigned short snum)
  6379. {
  6380. union sctp_addr addr;
  6381. struct sctp_af *af = sctp_sk(sk)->pf->af;
  6382. /* Set up a dummy address struct from the sk. */
  6383. af->from_sk(&addr, sk);
  6384. addr.v4.sin_port = htons(snum);
  6385. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  6386. return !!sctp_get_port_local(sk, &addr);
  6387. }
  6388. /*
  6389. * Move a socket to LISTENING state.
  6390. */
  6391. static int sctp_listen_start(struct sock *sk, int backlog)
  6392. {
  6393. struct sctp_sock *sp = sctp_sk(sk);
  6394. struct sctp_endpoint *ep = sp->ep;
  6395. struct crypto_shash *tfm = NULL;
  6396. char alg[32];
  6397. /* Allocate HMAC for generating cookie. */
  6398. if (!sp->hmac && sp->sctp_hmac_alg) {
  6399. sprintf(alg, "hmac(%s)", sp->sctp_hmac_alg);
  6400. tfm = crypto_alloc_shash(alg, 0, 0);
  6401. if (IS_ERR(tfm)) {
  6402. net_info_ratelimited("failed to load transform for %s: %ld\n",
  6403. sp->sctp_hmac_alg, PTR_ERR(tfm));
  6404. return -ENOSYS;
  6405. }
  6406. sctp_sk(sk)->hmac = tfm;
  6407. }
  6408. /*
  6409. * If a bind() or sctp_bindx() is not called prior to a listen()
  6410. * call that allows new associations to be accepted, the system
  6411. * picks an ephemeral port and will choose an address set equivalent
  6412. * to binding with a wildcard address.
  6413. *
  6414. * This is not currently spelled out in the SCTP sockets
  6415. * extensions draft, but follows the practice as seen in TCP
  6416. * sockets.
  6417. *
  6418. */
  6419. inet_sk_set_state(sk, SCTP_SS_LISTENING);
  6420. if (!ep->base.bind_addr.port) {
  6421. if (sctp_autobind(sk))
  6422. return -EAGAIN;
  6423. } else {
  6424. if (sctp_get_port(sk, inet_sk(sk)->inet_num)) {
  6425. inet_sk_set_state(sk, SCTP_SS_CLOSED);
  6426. return -EADDRINUSE;
  6427. }
  6428. }
  6429. sk->sk_max_ack_backlog = backlog;
  6430. sctp_hash_endpoint(ep);
  6431. return 0;
  6432. }
  6433. /*
  6434. * 4.1.3 / 5.1.3 listen()
  6435. *
  6436. * By default, new associations are not accepted for UDP style sockets.
  6437. * An application uses listen() to mark a socket as being able to
  6438. * accept new associations.
  6439. *
  6440. * On TCP style sockets, applications use listen() to ready the SCTP
  6441. * endpoint for accepting inbound associations.
  6442. *
  6443. * On both types of endpoints a backlog of '0' disables listening.
  6444. *
  6445. * Move a socket to LISTENING state.
  6446. */
  6447. int sctp_inet_listen(struct socket *sock, int backlog)
  6448. {
  6449. struct sock *sk = sock->sk;
  6450. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  6451. int err = -EINVAL;
  6452. if (unlikely(backlog < 0))
  6453. return err;
  6454. lock_sock(sk);
  6455. /* Peeled-off sockets are not allowed to listen(). */
  6456. if (sctp_style(sk, UDP_HIGH_BANDWIDTH))
  6457. goto out;
  6458. if (sock->state != SS_UNCONNECTED)
  6459. goto out;
  6460. if (!sctp_sstate(sk, LISTENING) && !sctp_sstate(sk, CLOSED))
  6461. goto out;
  6462. /* If backlog is zero, disable listening. */
  6463. if (!backlog) {
  6464. if (sctp_sstate(sk, CLOSED))
  6465. goto out;
  6466. err = 0;
  6467. sctp_unhash_endpoint(ep);
  6468. sk->sk_state = SCTP_SS_CLOSED;
  6469. if (sk->sk_reuse)
  6470. sctp_sk(sk)->bind_hash->fastreuse = 1;
  6471. goto out;
  6472. }
  6473. /* If we are already listening, just update the backlog */
  6474. if (sctp_sstate(sk, LISTENING))
  6475. sk->sk_max_ack_backlog = backlog;
  6476. else {
  6477. err = sctp_listen_start(sk, backlog);
  6478. if (err)
  6479. goto out;
  6480. }
  6481. err = 0;
  6482. out:
  6483. release_sock(sk);
  6484. return err;
  6485. }
  6486. /*
  6487. * This function is done by modeling the current datagram_poll() and the
  6488. * tcp_poll(). Note that, based on these implementations, we don't
  6489. * lock the socket in this function, even though it seems that,
  6490. * ideally, locking or some other mechanisms can be used to ensure
  6491. * the integrity of the counters (sndbuf and wmem_alloc) used
  6492. * in this place. We assume that we don't need locks either until proven
  6493. * otherwise.
  6494. *
  6495. * Another thing to note is that we include the Async I/O support
  6496. * here, again, by modeling the current TCP/UDP code. We don't have
  6497. * a good way to test with it yet.
  6498. */
  6499. __poll_t sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  6500. {
  6501. struct sock *sk = sock->sk;
  6502. struct sctp_sock *sp = sctp_sk(sk);
  6503. __poll_t mask;
  6504. poll_wait(file, sk_sleep(sk), wait);
  6505. sock_rps_record_flow(sk);
  6506. /* A TCP-style listening socket becomes readable when the accept queue
  6507. * is not empty.
  6508. */
  6509. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  6510. return (!list_empty(&sp->ep->asocs)) ?
  6511. (EPOLLIN | EPOLLRDNORM) : 0;
  6512. mask = 0;
  6513. /* Is there any exceptional events? */
  6514. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  6515. mask |= EPOLLERR |
  6516. (sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
  6517. if (sk->sk_shutdown & RCV_SHUTDOWN)
  6518. mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  6519. if (sk->sk_shutdown == SHUTDOWN_MASK)
  6520. mask |= EPOLLHUP;
  6521. /* Is it readable? Reconsider this code with TCP-style support. */
  6522. if (!skb_queue_empty(&sk->sk_receive_queue))
  6523. mask |= EPOLLIN | EPOLLRDNORM;
  6524. /* The association is either gone or not ready. */
  6525. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  6526. return mask;
  6527. /* Is it writable? */
  6528. if (sctp_writeable(sk)) {
  6529. mask |= EPOLLOUT | EPOLLWRNORM;
  6530. } else {
  6531. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  6532. /*
  6533. * Since the socket is not locked, the buffer
  6534. * might be made available after the writeable check and
  6535. * before the bit is set. This could cause a lost I/O
  6536. * signal. tcp_poll() has a race breaker for this race
  6537. * condition. Based on their implementation, we put
  6538. * in the following code to cover it as well.
  6539. */
  6540. if (sctp_writeable(sk))
  6541. mask |= EPOLLOUT | EPOLLWRNORM;
  6542. }
  6543. return mask;
  6544. }
  6545. /********************************************************************
  6546. * 2nd Level Abstractions
  6547. ********************************************************************/
  6548. static struct sctp_bind_bucket *sctp_bucket_create(
  6549. struct sctp_bind_hashbucket *head, struct net *net, unsigned short snum)
  6550. {
  6551. struct sctp_bind_bucket *pp;
  6552. pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC);
  6553. if (pp) {
  6554. SCTP_DBG_OBJCNT_INC(bind_bucket);
  6555. pp->port = snum;
  6556. pp->fastreuse = 0;
  6557. INIT_HLIST_HEAD(&pp->owner);
  6558. pp->net = net;
  6559. hlist_add_head(&pp->node, &head->chain);
  6560. }
  6561. return pp;
  6562. }
  6563. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  6564. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  6565. {
  6566. if (pp && hlist_empty(&pp->owner)) {
  6567. __hlist_del(&pp->node);
  6568. kmem_cache_free(sctp_bucket_cachep, pp);
  6569. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  6570. }
  6571. }
  6572. /* Release this socket's reference to a local port. */
  6573. static inline void __sctp_put_port(struct sock *sk)
  6574. {
  6575. struct sctp_bind_hashbucket *head =
  6576. &sctp_port_hashtable[sctp_phashfn(sock_net(sk),
  6577. inet_sk(sk)->inet_num)];
  6578. struct sctp_bind_bucket *pp;
  6579. spin_lock(&head->lock);
  6580. pp = sctp_sk(sk)->bind_hash;
  6581. __sk_del_bind_node(sk);
  6582. sctp_sk(sk)->bind_hash = NULL;
  6583. inet_sk(sk)->inet_num = 0;
  6584. sctp_bucket_destroy(pp);
  6585. spin_unlock(&head->lock);
  6586. }
  6587. void sctp_put_port(struct sock *sk)
  6588. {
  6589. local_bh_disable();
  6590. __sctp_put_port(sk);
  6591. local_bh_enable();
  6592. }
  6593. /*
  6594. * The system picks an ephemeral port and choose an address set equivalent
  6595. * to binding with a wildcard address.
  6596. * One of those addresses will be the primary address for the association.
  6597. * This automatically enables the multihoming capability of SCTP.
  6598. */
  6599. static int sctp_autobind(struct sock *sk)
  6600. {
  6601. union sctp_addr autoaddr;
  6602. struct sctp_af *af;
  6603. __be16 port;
  6604. /* Initialize a local sockaddr structure to INADDR_ANY. */
  6605. af = sctp_sk(sk)->pf->af;
  6606. port = htons(inet_sk(sk)->inet_num);
  6607. af->inaddr_any(&autoaddr, port);
  6608. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  6609. }
  6610. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  6611. *
  6612. * From RFC 2292
  6613. * 4.2 The cmsghdr Structure *
  6614. *
  6615. * When ancillary data is sent or received, any number of ancillary data
  6616. * objects can be specified by the msg_control and msg_controllen members of
  6617. * the msghdr structure, because each object is preceded by
  6618. * a cmsghdr structure defining the object's length (the cmsg_len member).
  6619. * Historically Berkeley-derived implementations have passed only one object
  6620. * at a time, but this API allows multiple objects to be
  6621. * passed in a single call to sendmsg() or recvmsg(). The following example
  6622. * shows two ancillary data objects in a control buffer.
  6623. *
  6624. * |<--------------------------- msg_controllen -------------------------->|
  6625. * | |
  6626. *
  6627. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  6628. *
  6629. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  6630. * | | |
  6631. *
  6632. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  6633. *
  6634. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  6635. * | | | | |
  6636. *
  6637. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  6638. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  6639. *
  6640. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  6641. *
  6642. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  6643. * ^
  6644. * |
  6645. *
  6646. * msg_control
  6647. * points here
  6648. */
  6649. static int sctp_msghdr_parse(const struct msghdr *msg, struct sctp_cmsgs *cmsgs)
  6650. {
  6651. struct msghdr *my_msg = (struct msghdr *)msg;
  6652. struct cmsghdr *cmsg;
  6653. for_each_cmsghdr(cmsg, my_msg) {
  6654. if (!CMSG_OK(my_msg, cmsg))
  6655. return -EINVAL;
  6656. /* Should we parse this header or ignore? */
  6657. if (cmsg->cmsg_level != IPPROTO_SCTP)
  6658. continue;
  6659. /* Strictly check lengths following example in SCM code. */
  6660. switch (cmsg->cmsg_type) {
  6661. case SCTP_INIT:
  6662. /* SCTP Socket API Extension
  6663. * 5.3.1 SCTP Initiation Structure (SCTP_INIT)
  6664. *
  6665. * This cmsghdr structure provides information for
  6666. * initializing new SCTP associations with sendmsg().
  6667. * The SCTP_INITMSG socket option uses this same data
  6668. * structure. This structure is not used for
  6669. * recvmsg().
  6670. *
  6671. * cmsg_level cmsg_type cmsg_data[]
  6672. * ------------ ------------ ----------------------
  6673. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  6674. */
  6675. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_initmsg)))
  6676. return -EINVAL;
  6677. cmsgs->init = CMSG_DATA(cmsg);
  6678. break;
  6679. case SCTP_SNDRCV:
  6680. /* SCTP Socket API Extension
  6681. * 5.3.2 SCTP Header Information Structure(SCTP_SNDRCV)
  6682. *
  6683. * This cmsghdr structure specifies SCTP options for
  6684. * sendmsg() and describes SCTP header information
  6685. * about a received message through recvmsg().
  6686. *
  6687. * cmsg_level cmsg_type cmsg_data[]
  6688. * ------------ ------------ ----------------------
  6689. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  6690. */
  6691. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  6692. return -EINVAL;
  6693. cmsgs->srinfo = CMSG_DATA(cmsg);
  6694. if (cmsgs->srinfo->sinfo_flags &
  6695. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  6696. SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
  6697. SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
  6698. return -EINVAL;
  6699. break;
  6700. case SCTP_SNDINFO:
  6701. /* SCTP Socket API Extension
  6702. * 5.3.4 SCTP Send Information Structure (SCTP_SNDINFO)
  6703. *
  6704. * This cmsghdr structure specifies SCTP options for
  6705. * sendmsg(). This structure and SCTP_RCVINFO replaces
  6706. * SCTP_SNDRCV which has been deprecated.
  6707. *
  6708. * cmsg_level cmsg_type cmsg_data[]
  6709. * ------------ ------------ ---------------------
  6710. * IPPROTO_SCTP SCTP_SNDINFO struct sctp_sndinfo
  6711. */
  6712. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_sndinfo)))
  6713. return -EINVAL;
  6714. cmsgs->sinfo = CMSG_DATA(cmsg);
  6715. if (cmsgs->sinfo->snd_flags &
  6716. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  6717. SCTP_SACK_IMMEDIATELY | SCTP_SENDALL |
  6718. SCTP_PR_SCTP_MASK | SCTP_ABORT | SCTP_EOF))
  6719. return -EINVAL;
  6720. break;
  6721. case SCTP_PRINFO:
  6722. /* SCTP Socket API Extension
  6723. * 5.3.7 SCTP PR-SCTP Information Structure (SCTP_PRINFO)
  6724. *
  6725. * This cmsghdr structure specifies SCTP options for sendmsg().
  6726. *
  6727. * cmsg_level cmsg_type cmsg_data[]
  6728. * ------------ ------------ ---------------------
  6729. * IPPROTO_SCTP SCTP_PRINFO struct sctp_prinfo
  6730. */
  6731. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_prinfo)))
  6732. return -EINVAL;
  6733. cmsgs->prinfo = CMSG_DATA(cmsg);
  6734. if (cmsgs->prinfo->pr_policy & ~SCTP_PR_SCTP_MASK)
  6735. return -EINVAL;
  6736. if (cmsgs->prinfo->pr_policy == SCTP_PR_SCTP_NONE)
  6737. cmsgs->prinfo->pr_value = 0;
  6738. break;
  6739. case SCTP_AUTHINFO:
  6740. /* SCTP Socket API Extension
  6741. * 5.3.8 SCTP AUTH Information Structure (SCTP_AUTHINFO)
  6742. *
  6743. * This cmsghdr structure specifies SCTP options for sendmsg().
  6744. *
  6745. * cmsg_level cmsg_type cmsg_data[]
  6746. * ------------ ------------ ---------------------
  6747. * IPPROTO_SCTP SCTP_AUTHINFO struct sctp_authinfo
  6748. */
  6749. if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct sctp_authinfo)))
  6750. return -EINVAL;
  6751. cmsgs->authinfo = CMSG_DATA(cmsg);
  6752. break;
  6753. case SCTP_DSTADDRV4:
  6754. case SCTP_DSTADDRV6:
  6755. /* SCTP Socket API Extension
  6756. * 5.3.9/10 SCTP Destination IPv4/6 Address Structure (SCTP_DSTADDRV4/6)
  6757. *
  6758. * This cmsghdr structure specifies SCTP options for sendmsg().
  6759. *
  6760. * cmsg_level cmsg_type cmsg_data[]
  6761. * ------------ ------------ ---------------------
  6762. * IPPROTO_SCTP SCTP_DSTADDRV4 struct in_addr
  6763. * ------------ ------------ ---------------------
  6764. * IPPROTO_SCTP SCTP_DSTADDRV6 struct in6_addr
  6765. */
  6766. cmsgs->addrs_msg = my_msg;
  6767. break;
  6768. default:
  6769. return -EINVAL;
  6770. }
  6771. }
  6772. return 0;
  6773. }
  6774. /*
  6775. * Wait for a packet..
  6776. * Note: This function is the same function as in core/datagram.c
  6777. * with a few modifications to make lksctp work.
  6778. */
  6779. static int sctp_wait_for_packet(struct sock *sk, int *err, long *timeo_p)
  6780. {
  6781. int error;
  6782. DEFINE_WAIT(wait);
  6783. prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  6784. /* Socket errors? */
  6785. error = sock_error(sk);
  6786. if (error)
  6787. goto out;
  6788. if (!skb_queue_empty(&sk->sk_receive_queue))
  6789. goto ready;
  6790. /* Socket shut down? */
  6791. if (sk->sk_shutdown & RCV_SHUTDOWN)
  6792. goto out;
  6793. /* Sequenced packets can come disconnected. If so we report the
  6794. * problem.
  6795. */
  6796. error = -ENOTCONN;
  6797. /* Is there a good reason to think that we may receive some data? */
  6798. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  6799. goto out;
  6800. /* Handle signals. */
  6801. if (signal_pending(current))
  6802. goto interrupted;
  6803. /* Let another process have a go. Since we are going to sleep
  6804. * anyway. Note: This may cause odd behaviors if the message
  6805. * does not fit in the user's buffer, but this seems to be the
  6806. * only way to honor MSG_DONTWAIT realistically.
  6807. */
  6808. release_sock(sk);
  6809. *timeo_p = schedule_timeout(*timeo_p);
  6810. lock_sock(sk);
  6811. ready:
  6812. finish_wait(sk_sleep(sk), &wait);
  6813. return 0;
  6814. interrupted:
  6815. error = sock_intr_errno(*timeo_p);
  6816. out:
  6817. finish_wait(sk_sleep(sk), &wait);
  6818. *err = error;
  6819. return error;
  6820. }
  6821. /* Receive a datagram.
  6822. * Note: This is pretty much the same routine as in core/datagram.c
  6823. * with a few changes to make lksctp work.
  6824. */
  6825. struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  6826. int noblock, int *err)
  6827. {
  6828. int error;
  6829. struct sk_buff *skb;
  6830. long timeo;
  6831. timeo = sock_rcvtimeo(sk, noblock);
  6832. pr_debug("%s: timeo:%ld, max:%ld\n", __func__, timeo,
  6833. MAX_SCHEDULE_TIMEOUT);
  6834. do {
  6835. /* Again only user level code calls this function,
  6836. * so nothing interrupt level
  6837. * will suddenly eat the receive_queue.
  6838. *
  6839. * Look at current nfs client by the way...
  6840. * However, this function was correct in any case. 8)
  6841. */
  6842. if (flags & MSG_PEEK) {
  6843. skb = skb_peek(&sk->sk_receive_queue);
  6844. if (skb)
  6845. refcount_inc(&skb->users);
  6846. } else {
  6847. skb = __skb_dequeue(&sk->sk_receive_queue);
  6848. }
  6849. if (skb)
  6850. return skb;
  6851. /* Caller is allowed not to check sk->sk_err before calling. */
  6852. error = sock_error(sk);
  6853. if (error)
  6854. goto no_packet;
  6855. if (sk->sk_shutdown & RCV_SHUTDOWN)
  6856. break;
  6857. if (sk_can_busy_loop(sk)) {
  6858. sk_busy_loop(sk, noblock);
  6859. if (!skb_queue_empty(&sk->sk_receive_queue))
  6860. continue;
  6861. }
  6862. /* User doesn't want to wait. */
  6863. error = -EAGAIN;
  6864. if (!timeo)
  6865. goto no_packet;
  6866. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  6867. return NULL;
  6868. no_packet:
  6869. *err = error;
  6870. return NULL;
  6871. }
  6872. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  6873. static void __sctp_write_space(struct sctp_association *asoc)
  6874. {
  6875. struct sock *sk = asoc->base.sk;
  6876. if (sctp_wspace(asoc) <= 0)
  6877. return;
  6878. if (waitqueue_active(&asoc->wait))
  6879. wake_up_interruptible(&asoc->wait);
  6880. if (sctp_writeable(sk)) {
  6881. struct socket_wq *wq;
  6882. rcu_read_lock();
  6883. wq = rcu_dereference(sk->sk_wq);
  6884. if (wq) {
  6885. if (waitqueue_active(&wq->wait))
  6886. wake_up_interruptible(&wq->wait);
  6887. /* Note that we try to include the Async I/O support
  6888. * here by modeling from the current TCP/UDP code.
  6889. * We have not tested with it yet.
  6890. */
  6891. if (!(sk->sk_shutdown & SEND_SHUTDOWN))
  6892. sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
  6893. }
  6894. rcu_read_unlock();
  6895. }
  6896. }
  6897. static void sctp_wake_up_waiters(struct sock *sk,
  6898. struct sctp_association *asoc)
  6899. {
  6900. struct sctp_association *tmp = asoc;
  6901. /* We do accounting for the sndbuf space per association,
  6902. * so we only need to wake our own association.
  6903. */
  6904. if (asoc->ep->sndbuf_policy)
  6905. return __sctp_write_space(asoc);
  6906. /* If association goes down and is just flushing its
  6907. * outq, then just normally notify others.
  6908. */
  6909. if (asoc->base.dead)
  6910. return sctp_write_space(sk);
  6911. /* Accounting for the sndbuf space is per socket, so we
  6912. * need to wake up others, try to be fair and in case of
  6913. * other associations, let them have a go first instead
  6914. * of just doing a sctp_write_space() call.
  6915. *
  6916. * Note that we reach sctp_wake_up_waiters() only when
  6917. * associations free up queued chunks, thus we are under
  6918. * lock and the list of associations on a socket is
  6919. * guaranteed not to change.
  6920. */
  6921. for (tmp = list_next_entry(tmp, asocs); 1;
  6922. tmp = list_next_entry(tmp, asocs)) {
  6923. /* Manually skip the head element. */
  6924. if (&tmp->asocs == &((sctp_sk(sk))->ep->asocs))
  6925. continue;
  6926. /* Wake up association. */
  6927. __sctp_write_space(tmp);
  6928. /* We've reached the end. */
  6929. if (tmp == asoc)
  6930. break;
  6931. }
  6932. }
  6933. /* Do accounting for the sndbuf space.
  6934. * Decrement the used sndbuf space of the corresponding association by the
  6935. * data size which was just transmitted(freed).
  6936. */
  6937. static void sctp_wfree(struct sk_buff *skb)
  6938. {
  6939. struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
  6940. struct sctp_association *asoc = chunk->asoc;
  6941. struct sock *sk = asoc->base.sk;
  6942. asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
  6943. sizeof(struct sk_buff) +
  6944. sizeof(struct sctp_chunk);
  6945. WARN_ON(refcount_sub_and_test(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc));
  6946. /*
  6947. * This undoes what is done via sctp_set_owner_w and sk_mem_charge
  6948. */
  6949. sk->sk_wmem_queued -= skb->truesize;
  6950. sk_mem_uncharge(sk, skb->truesize);
  6951. if (chunk->shkey)
  6952. sctp_auth_shkey_release(chunk->shkey);
  6953. sock_wfree(skb);
  6954. sctp_wake_up_waiters(sk, asoc);
  6955. sctp_association_put(asoc);
  6956. }
  6957. /* Do accounting for the receive space on the socket.
  6958. * Accounting for the association is done in ulpevent.c
  6959. * We set this as a destructor for the cloned data skbs so that
  6960. * accounting is done at the correct time.
  6961. */
  6962. void sctp_sock_rfree(struct sk_buff *skb)
  6963. {
  6964. struct sock *sk = skb->sk;
  6965. struct sctp_ulpevent *event = sctp_skb2event(skb);
  6966. atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
  6967. /*
  6968. * Mimic the behavior of sock_rfree
  6969. */
  6970. sk_mem_uncharge(sk, event->rmem_len);
  6971. }
  6972. /* Helper function to wait for space in the sndbuf. */
  6973. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  6974. size_t msg_len)
  6975. {
  6976. struct sock *sk = asoc->base.sk;
  6977. long current_timeo = *timeo_p;
  6978. DEFINE_WAIT(wait);
  6979. int err = 0;
  6980. pr_debug("%s: asoc:%p, timeo:%ld, msg_len:%zu\n", __func__, asoc,
  6981. *timeo_p, msg_len);
  6982. /* Increment the association's refcnt. */
  6983. sctp_association_hold(asoc);
  6984. /* Wait on the association specific sndbuf space. */
  6985. for (;;) {
  6986. prepare_to_wait_exclusive(&asoc->wait, &wait,
  6987. TASK_INTERRUPTIBLE);
  6988. if (asoc->base.dead)
  6989. goto do_dead;
  6990. if (!*timeo_p)
  6991. goto do_nonblock;
  6992. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING)
  6993. goto do_error;
  6994. if (signal_pending(current))
  6995. goto do_interrupted;
  6996. if (msg_len <= sctp_wspace(asoc))
  6997. break;
  6998. /* Let another process have a go. Since we are going
  6999. * to sleep anyway.
  7000. */
  7001. release_sock(sk);
  7002. current_timeo = schedule_timeout(current_timeo);
  7003. lock_sock(sk);
  7004. if (sk != asoc->base.sk)
  7005. goto do_error;
  7006. *timeo_p = current_timeo;
  7007. }
  7008. out:
  7009. finish_wait(&asoc->wait, &wait);
  7010. /* Release the association's refcnt. */
  7011. sctp_association_put(asoc);
  7012. return err;
  7013. do_dead:
  7014. err = -ESRCH;
  7015. goto out;
  7016. do_error:
  7017. err = -EPIPE;
  7018. goto out;
  7019. do_interrupted:
  7020. err = sock_intr_errno(*timeo_p);
  7021. goto out;
  7022. do_nonblock:
  7023. err = -EAGAIN;
  7024. goto out;
  7025. }
  7026. void sctp_data_ready(struct sock *sk)
  7027. {
  7028. struct socket_wq *wq;
  7029. rcu_read_lock();
  7030. wq = rcu_dereference(sk->sk_wq);
  7031. if (skwq_has_sleeper(wq))
  7032. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
  7033. EPOLLRDNORM | EPOLLRDBAND);
  7034. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  7035. rcu_read_unlock();
  7036. }
  7037. /* If socket sndbuf has changed, wake up all per association waiters. */
  7038. void sctp_write_space(struct sock *sk)
  7039. {
  7040. struct sctp_association *asoc;
  7041. /* Wake up the tasks in each wait queue. */
  7042. list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) {
  7043. __sctp_write_space(asoc);
  7044. }
  7045. }
  7046. /* Is there any sndbuf space available on the socket?
  7047. *
  7048. * Note that sk_wmem_alloc is the sum of the send buffers on all of the
  7049. * associations on the same socket. For a UDP-style socket with
  7050. * multiple associations, it is possible for it to be "unwriteable"
  7051. * prematurely. I assume that this is acceptable because
  7052. * a premature "unwriteable" is better than an accidental "writeable" which
  7053. * would cause an unwanted block under certain circumstances. For the 1-1
  7054. * UDP-style sockets or TCP-style sockets, this code should work.
  7055. * - Daisy
  7056. */
  7057. static int sctp_writeable(struct sock *sk)
  7058. {
  7059. int amt = 0;
  7060. amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  7061. if (amt < 0)
  7062. amt = 0;
  7063. return amt;
  7064. }
  7065. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  7066. * returns immediately with EINPROGRESS.
  7067. */
  7068. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  7069. {
  7070. struct sock *sk = asoc->base.sk;
  7071. int err = 0;
  7072. long current_timeo = *timeo_p;
  7073. DEFINE_WAIT(wait);
  7074. pr_debug("%s: asoc:%p, timeo:%ld\n", __func__, asoc, *timeo_p);
  7075. /* Increment the association's refcnt. */
  7076. sctp_association_hold(asoc);
  7077. for (;;) {
  7078. prepare_to_wait_exclusive(&asoc->wait, &wait,
  7079. TASK_INTERRUPTIBLE);
  7080. if (!*timeo_p)
  7081. goto do_nonblock;
  7082. if (sk->sk_shutdown & RCV_SHUTDOWN)
  7083. break;
  7084. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  7085. asoc->base.dead)
  7086. goto do_error;
  7087. if (signal_pending(current))
  7088. goto do_interrupted;
  7089. if (sctp_state(asoc, ESTABLISHED))
  7090. break;
  7091. /* Let another process have a go. Since we are going
  7092. * to sleep anyway.
  7093. */
  7094. release_sock(sk);
  7095. current_timeo = schedule_timeout(current_timeo);
  7096. lock_sock(sk);
  7097. *timeo_p = current_timeo;
  7098. }
  7099. out:
  7100. finish_wait(&asoc->wait, &wait);
  7101. /* Release the association's refcnt. */
  7102. sctp_association_put(asoc);
  7103. return err;
  7104. do_error:
  7105. if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
  7106. err = -ETIMEDOUT;
  7107. else
  7108. err = -ECONNREFUSED;
  7109. goto out;
  7110. do_interrupted:
  7111. err = sock_intr_errno(*timeo_p);
  7112. goto out;
  7113. do_nonblock:
  7114. err = -EINPROGRESS;
  7115. goto out;
  7116. }
  7117. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  7118. {
  7119. struct sctp_endpoint *ep;
  7120. int err = 0;
  7121. DEFINE_WAIT(wait);
  7122. ep = sctp_sk(sk)->ep;
  7123. for (;;) {
  7124. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  7125. TASK_INTERRUPTIBLE);
  7126. if (list_empty(&ep->asocs)) {
  7127. release_sock(sk);
  7128. timeo = schedule_timeout(timeo);
  7129. lock_sock(sk);
  7130. }
  7131. err = -EINVAL;
  7132. if (!sctp_sstate(sk, LISTENING))
  7133. break;
  7134. err = 0;
  7135. if (!list_empty(&ep->asocs))
  7136. break;
  7137. err = sock_intr_errno(timeo);
  7138. if (signal_pending(current))
  7139. break;
  7140. err = -EAGAIN;
  7141. if (!timeo)
  7142. break;
  7143. }
  7144. finish_wait(sk_sleep(sk), &wait);
  7145. return err;
  7146. }
  7147. static void sctp_wait_for_close(struct sock *sk, long timeout)
  7148. {
  7149. DEFINE_WAIT(wait);
  7150. do {
  7151. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  7152. if (list_empty(&sctp_sk(sk)->ep->asocs))
  7153. break;
  7154. release_sock(sk);
  7155. timeout = schedule_timeout(timeout);
  7156. lock_sock(sk);
  7157. } while (!signal_pending(current) && timeout);
  7158. finish_wait(sk_sleep(sk), &wait);
  7159. }
  7160. static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
  7161. {
  7162. struct sk_buff *frag;
  7163. if (!skb->data_len)
  7164. goto done;
  7165. /* Don't forget the fragments. */
  7166. skb_walk_frags(skb, frag)
  7167. sctp_skb_set_owner_r_frag(frag, sk);
  7168. done:
  7169. sctp_skb_set_owner_r(skb, sk);
  7170. }
  7171. void sctp_copy_sock(struct sock *newsk, struct sock *sk,
  7172. struct sctp_association *asoc)
  7173. {
  7174. struct inet_sock *inet = inet_sk(sk);
  7175. struct inet_sock *newinet;
  7176. newsk->sk_type = sk->sk_type;
  7177. newsk->sk_bound_dev_if = sk->sk_bound_dev_if;
  7178. newsk->sk_flags = sk->sk_flags;
  7179. newsk->sk_tsflags = sk->sk_tsflags;
  7180. newsk->sk_no_check_tx = sk->sk_no_check_tx;
  7181. newsk->sk_no_check_rx = sk->sk_no_check_rx;
  7182. newsk->sk_reuse = sk->sk_reuse;
  7183. newsk->sk_shutdown = sk->sk_shutdown;
  7184. newsk->sk_destruct = sctp_destruct_sock;
  7185. newsk->sk_family = sk->sk_family;
  7186. newsk->sk_protocol = IPPROTO_SCTP;
  7187. newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  7188. newsk->sk_sndbuf = sk->sk_sndbuf;
  7189. newsk->sk_rcvbuf = sk->sk_rcvbuf;
  7190. newsk->sk_lingertime = sk->sk_lingertime;
  7191. newsk->sk_rcvtimeo = sk->sk_rcvtimeo;
  7192. newsk->sk_sndtimeo = sk->sk_sndtimeo;
  7193. newsk->sk_rxhash = sk->sk_rxhash;
  7194. newinet = inet_sk(newsk);
  7195. /* Initialize sk's sport, dport, rcv_saddr and daddr for
  7196. * getsockname() and getpeername()
  7197. */
  7198. newinet->inet_sport = inet->inet_sport;
  7199. newinet->inet_saddr = inet->inet_saddr;
  7200. newinet->inet_rcv_saddr = inet->inet_rcv_saddr;
  7201. newinet->inet_dport = htons(asoc->peer.port);
  7202. newinet->pmtudisc = inet->pmtudisc;
  7203. newinet->inet_id = asoc->next_tsn ^ jiffies;
  7204. newinet->uc_ttl = inet->uc_ttl;
  7205. newinet->mc_loop = 1;
  7206. newinet->mc_ttl = 1;
  7207. newinet->mc_index = 0;
  7208. newinet->mc_list = NULL;
  7209. if (newsk->sk_flags & SK_FLAGS_TIMESTAMP)
  7210. net_enable_timestamp();
  7211. security_sk_clone(sk, newsk);
  7212. }
  7213. static inline void sctp_copy_descendant(struct sock *sk_to,
  7214. const struct sock *sk_from)
  7215. {
  7216. int ancestor_size = sizeof(struct inet_sock) +
  7217. sizeof(struct sctp_sock) -
  7218. offsetof(struct sctp_sock, auto_asconf_list);
  7219. if (sk_from->sk_family == PF_INET6)
  7220. ancestor_size += sizeof(struct ipv6_pinfo);
  7221. __inet_sk_copy_descendant(sk_to, sk_from, ancestor_size);
  7222. }
  7223. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  7224. * and its messages to the newsk.
  7225. */
  7226. static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  7227. struct sctp_association *assoc,
  7228. enum sctp_socket_type type)
  7229. {
  7230. struct sctp_sock *oldsp = sctp_sk(oldsk);
  7231. struct sctp_sock *newsp = sctp_sk(newsk);
  7232. struct sctp_bind_bucket *pp; /* hash list port iterator */
  7233. struct sctp_endpoint *newep = newsp->ep;
  7234. struct sk_buff *skb, *tmp;
  7235. struct sctp_ulpevent *event;
  7236. struct sctp_bind_hashbucket *head;
  7237. /* Migrate socket buffer sizes and all the socket level options to the
  7238. * new socket.
  7239. */
  7240. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  7241. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  7242. /* Brute force copy old sctp opt. */
  7243. sctp_copy_descendant(newsk, oldsk);
  7244. /* Restore the ep value that was overwritten with the above structure
  7245. * copy.
  7246. */
  7247. newsp->ep = newep;
  7248. newsp->hmac = NULL;
  7249. /* Hook this new socket in to the bind_hash list. */
  7250. head = &sctp_port_hashtable[sctp_phashfn(sock_net(oldsk),
  7251. inet_sk(oldsk)->inet_num)];
  7252. spin_lock_bh(&head->lock);
  7253. pp = sctp_sk(oldsk)->bind_hash;
  7254. sk_add_bind_node(newsk, &pp->owner);
  7255. sctp_sk(newsk)->bind_hash = pp;
  7256. inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num;
  7257. spin_unlock_bh(&head->lock);
  7258. /* Copy the bind_addr list from the original endpoint to the new
  7259. * endpoint so that we can handle restarts properly
  7260. */
  7261. sctp_bind_addr_dup(&newsp->ep->base.bind_addr,
  7262. &oldsp->ep->base.bind_addr, GFP_KERNEL);
  7263. /* Move any messages in the old socket's receive queue that are for the
  7264. * peeled off association to the new socket's receive queue.
  7265. */
  7266. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  7267. event = sctp_skb2event(skb);
  7268. if (event->asoc == assoc) {
  7269. __skb_unlink(skb, &oldsk->sk_receive_queue);
  7270. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  7271. sctp_skb_set_owner_r_frag(skb, newsk);
  7272. }
  7273. }
  7274. /* Clean up any messages pending delivery due to partial
  7275. * delivery. Three cases:
  7276. * 1) No partial deliver; no work.
  7277. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  7278. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  7279. */
  7280. skb_queue_head_init(&newsp->pd_lobby);
  7281. atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode);
  7282. if (atomic_read(&sctp_sk(oldsk)->pd_mode)) {
  7283. struct sk_buff_head *queue;
  7284. /* Decide which queue to move pd_lobby skbs to. */
  7285. if (assoc->ulpq.pd_mode) {
  7286. queue = &newsp->pd_lobby;
  7287. } else
  7288. queue = &newsk->sk_receive_queue;
  7289. /* Walk through the pd_lobby, looking for skbs that
  7290. * need moved to the new socket.
  7291. */
  7292. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  7293. event = sctp_skb2event(skb);
  7294. if (event->asoc == assoc) {
  7295. __skb_unlink(skb, &oldsp->pd_lobby);
  7296. __skb_queue_tail(queue, skb);
  7297. sctp_skb_set_owner_r_frag(skb, newsk);
  7298. }
  7299. }
  7300. /* Clear up any skbs waiting for the partial
  7301. * delivery to finish.
  7302. */
  7303. if (assoc->ulpq.pd_mode)
  7304. sctp_clear_pd(oldsk, NULL);
  7305. }
  7306. sctp_for_each_rx_skb(assoc, newsk, sctp_skb_set_owner_r_frag);
  7307. /* Set the type of socket to indicate that it is peeled off from the
  7308. * original UDP-style socket or created with the accept() call on a
  7309. * TCP-style socket..
  7310. */
  7311. newsp->type = type;
  7312. /* Mark the new socket "in-use" by the user so that any packets
  7313. * that may arrive on the association after we've moved it are
  7314. * queued to the backlog. This prevents a potential race between
  7315. * backlog processing on the old socket and new-packet processing
  7316. * on the new socket.
  7317. *
  7318. * The caller has just allocated newsk so we can guarantee that other
  7319. * paths won't try to lock it and then oldsk.
  7320. */
  7321. lock_sock_nested(newsk, SINGLE_DEPTH_NESTING);
  7322. sctp_for_each_tx_datachunk(assoc, sctp_clear_owner_w);
  7323. sctp_assoc_migrate(assoc, newsk);
  7324. sctp_for_each_tx_datachunk(assoc, sctp_set_owner_w);
  7325. /* If the association on the newsk is already closed before accept()
  7326. * is called, set RCV_SHUTDOWN flag.
  7327. */
  7328. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) {
  7329. inet_sk_set_state(newsk, SCTP_SS_CLOSED);
  7330. newsk->sk_shutdown |= RCV_SHUTDOWN;
  7331. } else {
  7332. inet_sk_set_state(newsk, SCTP_SS_ESTABLISHED);
  7333. }
  7334. release_sock(newsk);
  7335. }
  7336. /* This proto struct describes the ULP interface for SCTP. */
  7337. struct proto sctp_prot = {
  7338. .name = "SCTP",
  7339. .owner = THIS_MODULE,
  7340. .close = sctp_close,
  7341. .connect = sctp_connect,
  7342. .disconnect = sctp_disconnect,
  7343. .accept = sctp_accept,
  7344. .ioctl = sctp_ioctl,
  7345. .init = sctp_init_sock,
  7346. .destroy = sctp_destroy_sock,
  7347. .shutdown = sctp_shutdown,
  7348. .setsockopt = sctp_setsockopt,
  7349. .getsockopt = sctp_getsockopt,
  7350. .sendmsg = sctp_sendmsg,
  7351. .recvmsg = sctp_recvmsg,
  7352. .bind = sctp_bind,
  7353. .backlog_rcv = sctp_backlog_rcv,
  7354. .hash = sctp_hash,
  7355. .unhash = sctp_unhash,
  7356. .get_port = sctp_get_port,
  7357. .obj_size = sizeof(struct sctp_sock),
  7358. .useroffset = offsetof(struct sctp_sock, subscribe),
  7359. .usersize = offsetof(struct sctp_sock, initmsg) -
  7360. offsetof(struct sctp_sock, subscribe) +
  7361. sizeof_field(struct sctp_sock, initmsg),
  7362. .sysctl_mem = sysctl_sctp_mem,
  7363. .sysctl_rmem = sysctl_sctp_rmem,
  7364. .sysctl_wmem = sysctl_sctp_wmem,
  7365. .memory_pressure = &sctp_memory_pressure,
  7366. .enter_memory_pressure = sctp_enter_memory_pressure,
  7367. .memory_allocated = &sctp_memory_allocated,
  7368. .sockets_allocated = &sctp_sockets_allocated,
  7369. };
  7370. #if IS_ENABLED(CONFIG_IPV6)
  7371. #include <net/transp_v6.h>
  7372. static void sctp_v6_destroy_sock(struct sock *sk)
  7373. {
  7374. sctp_destroy_sock(sk);
  7375. inet6_destroy_sock(sk);
  7376. }
  7377. struct proto sctpv6_prot = {
  7378. .name = "SCTPv6",
  7379. .owner = THIS_MODULE,
  7380. .close = sctp_close,
  7381. .connect = sctp_connect,
  7382. .disconnect = sctp_disconnect,
  7383. .accept = sctp_accept,
  7384. .ioctl = sctp_ioctl,
  7385. .init = sctp_init_sock,
  7386. .destroy = sctp_v6_destroy_sock,
  7387. .shutdown = sctp_shutdown,
  7388. .setsockopt = sctp_setsockopt,
  7389. .getsockopt = sctp_getsockopt,
  7390. .sendmsg = sctp_sendmsg,
  7391. .recvmsg = sctp_recvmsg,
  7392. .bind = sctp_bind,
  7393. .backlog_rcv = sctp_backlog_rcv,
  7394. .hash = sctp_hash,
  7395. .unhash = sctp_unhash,
  7396. .get_port = sctp_get_port,
  7397. .obj_size = sizeof(struct sctp6_sock),
  7398. .useroffset = offsetof(struct sctp6_sock, sctp.subscribe),
  7399. .usersize = offsetof(struct sctp6_sock, sctp.initmsg) -
  7400. offsetof(struct sctp6_sock, sctp.subscribe) +
  7401. sizeof_field(struct sctp6_sock, sctp.initmsg),
  7402. .sysctl_mem = sysctl_sctp_mem,
  7403. .sysctl_rmem = sysctl_sctp_rmem,
  7404. .sysctl_wmem = sysctl_sctp_wmem,
  7405. .memory_pressure = &sctp_memory_pressure,
  7406. .enter_memory_pressure = sctp_enter_memory_pressure,
  7407. .memory_allocated = &sctp_memory_allocated,
  7408. .sockets_allocated = &sctp_sockets_allocated,
  7409. };
  7410. #endif /* IS_ENABLED(CONFIG_IPV6) */