socket.c 209 KB

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