socket.c 198 KB

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