socket.c 200 KB

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