socket.c 200 KB

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