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