socket.c 174 KB

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