socket.c 228 KB

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