socket.c 187 KB

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