socket.c 189 KB

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