socket.c 234 KB

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