socket.c 195 KB

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