socket.c 227 KB

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