socket.c 194 KB

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