socket.c 200 KB

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