socket.c 194 KB

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