socket.c 182 KB

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