socket.c 184 KB

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