socket.c 194 KB

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