socket.c 207 KB

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