socket.c 200 KB

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