socket.c 187 KB

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