socket.c 231 KB

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