socket.c 186 KB

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