socket.c 187 KB

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