socket.c 202 KB

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