socket.c 198 KB

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