socket.c 219 KB

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