socket.c 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001-2003 Intel Corp.
  6. * Copyright (c) 2001-2002 Nokia, Inc.
  7. * Copyright (c) 2001 La Monte H.P. Yarroll
  8. *
  9. * This file is part of the SCTP kernel reference Implementation
  10. *
  11. * These functions interface with the sockets layer to implement the
  12. * SCTP Extensions for the Sockets API.
  13. *
  14. * Note that the descriptions from the specification are USER level
  15. * functions--this file is the functions which populate the struct proto
  16. * for SCTP which is the BOTTOM of the sockets interface.
  17. *
  18. * The SCTP reference implementation is free software;
  19. * you can redistribute it and/or modify it under the terms of
  20. * the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2, or (at your option)
  22. * any later version.
  23. *
  24. * The SCTP reference implementation is distributed in the hope that it
  25. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  26. * ************************
  27. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  28. * See the GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with GNU CC; see the file COPYING. If not, write to
  32. * the Free Software Foundation, 59 Temple Place - Suite 330,
  33. * Boston, MA 02111-1307, USA.
  34. *
  35. * Please send any bug reports or fixes you make to the
  36. * email address(es):
  37. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  38. *
  39. * Or submit a bug report through the following website:
  40. * http://www.sf.net/projects/lksctp
  41. *
  42. * Written or modified by:
  43. * La Monte H.P. Yarroll <piggy@acm.org>
  44. * Narasimha Budihal <narsi@refcode.org>
  45. * Karl Knutson <karl@athena.chicago.il.us>
  46. * Jon Grimm <jgrimm@us.ibm.com>
  47. * Xingang Guo <xingang.guo@intel.com>
  48. * Daisy Chang <daisyc@us.ibm.com>
  49. * Sridhar Samudrala <samudrala@us.ibm.com>
  50. * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
  51. * Ardelle Fan <ardelle.fan@intel.com>
  52. * Ryan Layer <rmlayer@us.ibm.com>
  53. * Anup Pemmaiah <pemmaiah@cc.usu.edu>
  54. * Kevin Gao <kevin.gao@intel.com>
  55. *
  56. * Any bugs reported given to us we will try to fix... any fixes shared will
  57. * be incorporated into the next SCTP release.
  58. */
  59. #include <linux/config.h>
  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/fcntl.h>
  66. #include <linux/poll.h>
  67. #include <linux/init.h>
  68. #include <linux/crypto.h>
  69. #include <net/ip.h>
  70. #include <net/icmp.h>
  71. #include <net/route.h>
  72. #include <net/ipv6.h>
  73. #include <net/inet_common.h>
  74. #include <linux/socket.h> /* for sa_family_t */
  75. #include <net/sock.h>
  76. #include <net/sctp/sctp.h>
  77. #include <net/sctp/sm.h>
  78. /* WARNING: Please do not remove the SCTP_STATIC attribute to
  79. * any of the functions below as they are used to export functions
  80. * used by a project regression testsuite.
  81. */
  82. /* Forward declarations for internal helper functions. */
  83. static int sctp_writeable(struct sock *sk);
  84. static void sctp_wfree(struct sk_buff *skb);
  85. static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
  86. size_t msg_len);
  87. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
  88. static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
  89. static int sctp_wait_for_accept(struct sock *sk, long timeo);
  90. static void sctp_wait_for_close(struct sock *sk, long timeo);
  91. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  92. union sctp_addr *addr, int len);
  93. static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
  94. static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
  95. static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
  96. static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
  97. static int sctp_send_asconf(struct sctp_association *asoc,
  98. struct sctp_chunk *chunk);
  99. static int sctp_do_bind(struct sock *, union sctp_addr *, int);
  100. static int sctp_autobind(struct sock *sk);
  101. static void sctp_sock_migrate(struct sock *, struct sock *,
  102. struct sctp_association *, sctp_socket_type_t);
  103. static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
  104. extern kmem_cache_t *sctp_bucket_cachep;
  105. /* Get the sndbuf space available at the time on the association. */
  106. static inline int sctp_wspace(struct sctp_association *asoc)
  107. {
  108. struct sock *sk = asoc->base.sk;
  109. int amt = 0;
  110. if (asoc->ep->sndbuf_policy) {
  111. /* make sure that no association uses more than sk_sndbuf */
  112. amt = sk->sk_sndbuf - asoc->sndbuf_used;
  113. } else {
  114. /* do socket level accounting */
  115. amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
  116. }
  117. if (amt < 0)
  118. amt = 0;
  119. return amt;
  120. }
  121. /* Increment the used sndbuf space count of the corresponding association by
  122. * the size of the outgoing data chunk.
  123. * Also, set the skb destructor for sndbuf accounting later.
  124. *
  125. * Since it is always 1-1 between chunk and skb, and also a new skb is always
  126. * allocated for chunk bundling in sctp_packet_transmit(), we can use the
  127. * destructor in the data chunk skb for the purpose of the sndbuf space
  128. * tracking.
  129. */
  130. static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
  131. {
  132. struct sctp_association *asoc = chunk->asoc;
  133. struct sock *sk = asoc->base.sk;
  134. /* The sndbuf space is tracked per association. */
  135. sctp_association_hold(asoc);
  136. skb_set_owner_w(chunk->skb, sk);
  137. chunk->skb->destructor = sctp_wfree;
  138. /* Save the chunk pointer in skb for sctp_wfree to use later. */
  139. *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
  140. asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
  141. sizeof(struct sk_buff) +
  142. sizeof(struct sctp_chunk);
  143. sk->sk_wmem_queued += SCTP_DATA_SNDSIZE(chunk) +
  144. sizeof(struct sk_buff) +
  145. sizeof(struct sctp_chunk);
  146. atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  147. }
  148. /* Verify that this is a valid address. */
  149. static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
  150. int len)
  151. {
  152. struct sctp_af *af;
  153. /* Verify basic sockaddr. */
  154. af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
  155. if (!af)
  156. return -EINVAL;
  157. /* Is this a valid SCTP address? */
  158. if (!af->addr_valid(addr, sctp_sk(sk)))
  159. return -EINVAL;
  160. if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
  161. return -EINVAL;
  162. return 0;
  163. }
  164. /* Look up the association by its id. If this is not a UDP-style
  165. * socket, the ID field is always ignored.
  166. */
  167. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
  168. {
  169. struct sctp_association *asoc = NULL;
  170. /* If this is not a UDP-style socket, assoc id should be ignored. */
  171. if (!sctp_style(sk, UDP)) {
  172. /* Return NULL if the socket state is not ESTABLISHED. It
  173. * could be a TCP-style listening socket or a socket which
  174. * hasn't yet called connect() to establish an association.
  175. */
  176. if (!sctp_sstate(sk, ESTABLISHED))
  177. return NULL;
  178. /* Get the first and the only association from the list. */
  179. if (!list_empty(&sctp_sk(sk)->ep->asocs))
  180. asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
  181. struct sctp_association, asocs);
  182. return asoc;
  183. }
  184. /* Otherwise this is a UDP-style socket. */
  185. if (!id || (id == (sctp_assoc_t)-1))
  186. return NULL;
  187. spin_lock_bh(&sctp_assocs_id_lock);
  188. asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
  189. spin_unlock_bh(&sctp_assocs_id_lock);
  190. if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
  191. return NULL;
  192. return asoc;
  193. }
  194. /* Look up the transport from an address and an assoc id. If both address and
  195. * id are specified, the associations matching the address and the id should be
  196. * the same.
  197. */
  198. static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
  199. struct sockaddr_storage *addr,
  200. sctp_assoc_t id)
  201. {
  202. struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
  203. struct sctp_transport *transport;
  204. union sctp_addr *laddr = (union sctp_addr *)addr;
  205. laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
  206. addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
  207. (union sctp_addr *)addr,
  208. &transport);
  209. laddr->v4.sin_port = htons(laddr->v4.sin_port);
  210. if (!addr_asoc)
  211. return NULL;
  212. id_asoc = sctp_id2assoc(sk, id);
  213. if (id_asoc && (id_asoc != addr_asoc))
  214. return NULL;
  215. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  216. (union sctp_addr *)addr);
  217. return transport;
  218. }
  219. /* API 3.1.2 bind() - UDP Style Syntax
  220. * The syntax of bind() is,
  221. *
  222. * ret = bind(int sd, struct sockaddr *addr, int addrlen);
  223. *
  224. * sd - the socket descriptor returned by socket().
  225. * addr - the address structure (struct sockaddr_in or struct
  226. * sockaddr_in6 [RFC 2553]),
  227. * addr_len - the size of the address structure.
  228. */
  229. SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  230. {
  231. int retval = 0;
  232. sctp_lock_sock(sk);
  233. SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, uaddr: %p, addr_len: %d)\n",
  234. sk, uaddr, addr_len);
  235. /* Disallow binding twice. */
  236. if (!sctp_sk(sk)->ep->base.bind_addr.port)
  237. retval = sctp_do_bind(sk, (union sctp_addr *)uaddr,
  238. addr_len);
  239. else
  240. retval = -EINVAL;
  241. sctp_release_sock(sk);
  242. return retval;
  243. }
  244. static long sctp_get_port_local(struct sock *, union sctp_addr *);
  245. /* Verify this is a valid sockaddr. */
  246. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  247. union sctp_addr *addr, int len)
  248. {
  249. struct sctp_af *af;
  250. /* Check minimum size. */
  251. if (len < sizeof (struct sockaddr))
  252. return NULL;
  253. /* Does this PF support this AF? */
  254. if (!opt->pf->af_supported(addr->sa.sa_family, opt))
  255. return NULL;
  256. /* If we get this far, af is valid. */
  257. af = sctp_get_af_specific(addr->sa.sa_family);
  258. if (len < af->sockaddr_len)
  259. return NULL;
  260. return af;
  261. }
  262. /* Bind a local address either to an endpoint or to an association. */
  263. SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
  264. {
  265. struct sctp_sock *sp = sctp_sk(sk);
  266. struct sctp_endpoint *ep = sp->ep;
  267. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  268. struct sctp_af *af;
  269. unsigned short snum;
  270. int ret = 0;
  271. SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d)\n",
  272. sk, addr, len);
  273. /* Common sockaddr verification. */
  274. af = sctp_sockaddr_af(sp, addr, len);
  275. if (!af)
  276. return -EINVAL;
  277. /* PF specific bind() address verification. */
  278. if (!sp->pf->bind_verify(sp, addr))
  279. return -EADDRNOTAVAIL;
  280. snum= ntohs(addr->v4.sin_port);
  281. SCTP_DEBUG_PRINTK("sctp_do_bind: port: %d, new port: %d\n",
  282. bp->port, snum);
  283. /* We must either be unbound, or bind to the same port. */
  284. if (bp->port && (snum != bp->port)) {
  285. SCTP_DEBUG_PRINTK("sctp_do_bind:"
  286. " New port %d does not match existing port "
  287. "%d.\n", snum, bp->port);
  288. return -EINVAL;
  289. }
  290. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  291. return -EACCES;
  292. /* Make sure we are allowed to bind here.
  293. * The function sctp_get_port_local() does duplicate address
  294. * detection.
  295. */
  296. if ((ret = sctp_get_port_local(sk, addr))) {
  297. if (ret == (long) sk) {
  298. /* This endpoint has a conflicting address. */
  299. return -EINVAL;
  300. } else {
  301. return -EADDRINUSE;
  302. }
  303. }
  304. /* Refresh ephemeral port. */
  305. if (!bp->port)
  306. bp->port = inet_sk(sk)->num;
  307. /* Add the address to the bind address list. */
  308. sctp_local_bh_disable();
  309. sctp_write_lock(&ep->base.addr_lock);
  310. /* Use GFP_ATOMIC since BHs are disabled. */
  311. addr->v4.sin_port = ntohs(addr->v4.sin_port);
  312. ret = sctp_add_bind_addr(bp, addr, GFP_ATOMIC);
  313. addr->v4.sin_port = htons(addr->v4.sin_port);
  314. sctp_write_unlock(&ep->base.addr_lock);
  315. sctp_local_bh_enable();
  316. /* Copy back into socket for getsockname() use. */
  317. if (!ret) {
  318. inet_sk(sk)->sport = htons(inet_sk(sk)->num);
  319. af->to_sk_saddr(addr, sk);
  320. }
  321. return ret;
  322. }
  323. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  324. *
  325. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  326. * at any one time. If a sender, after sending an ASCONF chunk, decides
  327. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  328. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  329. * subsequent ASCONF. Note this restriction binds each side, so at any
  330. * time two ASCONF may be in-transit on any given association (one sent
  331. * from each endpoint).
  332. */
  333. static int sctp_send_asconf(struct sctp_association *asoc,
  334. struct sctp_chunk *chunk)
  335. {
  336. int retval = 0;
  337. /* If there is an outstanding ASCONF chunk, queue it for later
  338. * transmission.
  339. */
  340. if (asoc->addip_last_asconf) {
  341. __skb_queue_tail(&asoc->addip_chunks, (struct sk_buff *)chunk);
  342. goto out;
  343. }
  344. /* Hold the chunk until an ASCONF_ACK is received. */
  345. sctp_chunk_hold(chunk);
  346. retval = sctp_primitive_ASCONF(asoc, chunk);
  347. if (retval)
  348. sctp_chunk_free(chunk);
  349. else
  350. asoc->addip_last_asconf = chunk;
  351. out:
  352. return retval;
  353. }
  354. /* Add a list of addresses as bind addresses to local endpoint or
  355. * association.
  356. *
  357. * Basically run through each address specified in the addrs/addrcnt
  358. * array/length pair, determine if it is IPv6 or IPv4 and call
  359. * sctp_do_bind() on it.
  360. *
  361. * If any of them fails, then the operation will be reversed and the
  362. * ones that were added will be removed.
  363. *
  364. * Only sctp_setsockopt_bindx() is supposed to call this function.
  365. */
  366. int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  367. {
  368. int cnt;
  369. int retval = 0;
  370. void *addr_buf;
  371. struct sockaddr *sa_addr;
  372. struct sctp_af *af;
  373. SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
  374. sk, addrs, addrcnt);
  375. addr_buf = addrs;
  376. for (cnt = 0; cnt < addrcnt; cnt++) {
  377. /* The list may contain either IPv4 or IPv6 address;
  378. * determine the address length for walking thru the list.
  379. */
  380. sa_addr = (struct sockaddr *)addr_buf;
  381. af = sctp_get_af_specific(sa_addr->sa_family);
  382. if (!af) {
  383. retval = -EINVAL;
  384. goto err_bindx_add;
  385. }
  386. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  387. af->sockaddr_len);
  388. addr_buf += af->sockaddr_len;
  389. err_bindx_add:
  390. if (retval < 0) {
  391. /* Failed. Cleanup the ones that have been added */
  392. if (cnt > 0)
  393. sctp_bindx_rem(sk, addrs, cnt);
  394. return retval;
  395. }
  396. }
  397. return retval;
  398. }
  399. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  400. * associations that are part of the endpoint indicating that a list of local
  401. * addresses are added to the endpoint.
  402. *
  403. * If any of the addresses is already in the bind address list of the
  404. * association, we do not send the chunk for that association. But it will not
  405. * affect other associations.
  406. *
  407. * Only sctp_setsockopt_bindx() is supposed to call this function.
  408. */
  409. static int sctp_send_asconf_add_ip(struct sock *sk,
  410. struct sockaddr *addrs,
  411. int addrcnt)
  412. {
  413. struct sctp_sock *sp;
  414. struct sctp_endpoint *ep;
  415. struct sctp_association *asoc;
  416. struct sctp_bind_addr *bp;
  417. struct sctp_chunk *chunk;
  418. struct sctp_sockaddr_entry *laddr;
  419. union sctp_addr *addr;
  420. void *addr_buf;
  421. struct sctp_af *af;
  422. struct list_head *pos;
  423. struct list_head *p;
  424. int i;
  425. int retval = 0;
  426. if (!sctp_addip_enable)
  427. return retval;
  428. sp = sctp_sk(sk);
  429. ep = sp->ep;
  430. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  431. __FUNCTION__, sk, addrs, addrcnt);
  432. list_for_each(pos, &ep->asocs) {
  433. asoc = list_entry(pos, struct sctp_association, asocs);
  434. if (!asoc->peer.asconf_capable)
  435. continue;
  436. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  437. continue;
  438. if (!sctp_state(asoc, ESTABLISHED))
  439. continue;
  440. /* Check if any address in the packed array of addresses is
  441. * in the bind address list of the association. If so,
  442. * do not send the asconf chunk to its peer, but continue with
  443. * other associations.
  444. */
  445. addr_buf = addrs;
  446. for (i = 0; i < addrcnt; i++) {
  447. addr = (union sctp_addr *)addr_buf;
  448. af = sctp_get_af_specific(addr->v4.sin_family);
  449. if (!af) {
  450. retval = -EINVAL;
  451. goto out;
  452. }
  453. if (sctp_assoc_lookup_laddr(asoc, addr))
  454. break;
  455. addr_buf += af->sockaddr_len;
  456. }
  457. if (i < addrcnt)
  458. continue;
  459. /* Use the first address in bind addr list of association as
  460. * Address Parameter of ASCONF CHUNK.
  461. */
  462. sctp_read_lock(&asoc->base.addr_lock);
  463. bp = &asoc->base.bind_addr;
  464. p = bp->address_list.next;
  465. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  466. sctp_read_unlock(&asoc->base.addr_lock);
  467. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  468. addrcnt, SCTP_PARAM_ADD_IP);
  469. if (!chunk) {
  470. retval = -ENOMEM;
  471. goto out;
  472. }
  473. retval = sctp_send_asconf(asoc, chunk);
  474. /* FIXME: After sending the add address ASCONF chunk, we
  475. * cannot append the address to the association's binding
  476. * address list, because the new address may be used as the
  477. * source of a message sent to the peer before the ASCONF
  478. * chunk is received by the peer. So we should wait until
  479. * ASCONF_ACK is received.
  480. */
  481. }
  482. out:
  483. return retval;
  484. }
  485. /* Remove a list of addresses from bind addresses list. Do not remove the
  486. * last address.
  487. *
  488. * Basically run through each address specified in the addrs/addrcnt
  489. * array/length pair, determine if it is IPv6 or IPv4 and call
  490. * sctp_del_bind() on it.
  491. *
  492. * If any of them fails, then the operation will be reversed and the
  493. * ones that were removed will be added back.
  494. *
  495. * At least one address has to be left; if only one address is
  496. * available, the operation will return -EBUSY.
  497. *
  498. * Only sctp_setsockopt_bindx() is supposed to call this function.
  499. */
  500. int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  501. {
  502. struct sctp_sock *sp = sctp_sk(sk);
  503. struct sctp_endpoint *ep = sp->ep;
  504. int cnt;
  505. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  506. int retval = 0;
  507. union sctp_addr saveaddr;
  508. void *addr_buf;
  509. struct sockaddr *sa_addr;
  510. struct sctp_af *af;
  511. SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
  512. sk, addrs, addrcnt);
  513. addr_buf = addrs;
  514. for (cnt = 0; cnt < addrcnt; cnt++) {
  515. /* If the bind address list is empty or if there is only one
  516. * bind address, there is nothing more to be removed (we need
  517. * at least one address here).
  518. */
  519. if (list_empty(&bp->address_list) ||
  520. (sctp_list_single_entry(&bp->address_list))) {
  521. retval = -EBUSY;
  522. goto err_bindx_rem;
  523. }
  524. /* The list may contain either IPv4 or IPv6 address;
  525. * determine the address length to copy the address to
  526. * saveaddr.
  527. */
  528. sa_addr = (struct sockaddr *)addr_buf;
  529. af = sctp_get_af_specific(sa_addr->sa_family);
  530. if (!af) {
  531. retval = -EINVAL;
  532. goto err_bindx_rem;
  533. }
  534. memcpy(&saveaddr, sa_addr, af->sockaddr_len);
  535. saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
  536. if (saveaddr.v4.sin_port != bp->port) {
  537. retval = -EINVAL;
  538. goto err_bindx_rem;
  539. }
  540. /* FIXME - There is probably a need to check if sk->sk_saddr and
  541. * sk->sk_rcv_addr are currently set to one of the addresses to
  542. * be removed. This is something which needs to be looked into
  543. * when we are fixing the outstanding issues with multi-homing
  544. * socket routing and failover schemes. Refer to comments in
  545. * sctp_do_bind(). -daisy
  546. */
  547. sctp_local_bh_disable();
  548. sctp_write_lock(&ep->base.addr_lock);
  549. retval = sctp_del_bind_addr(bp, &saveaddr);
  550. sctp_write_unlock(&ep->base.addr_lock);
  551. sctp_local_bh_enable();
  552. addr_buf += af->sockaddr_len;
  553. err_bindx_rem:
  554. if (retval < 0) {
  555. /* Failed. Add the ones that has been removed back */
  556. if (cnt > 0)
  557. sctp_bindx_add(sk, addrs, cnt);
  558. return retval;
  559. }
  560. }
  561. return retval;
  562. }
  563. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  564. * the associations that are part of the endpoint indicating that a list of
  565. * local addresses are removed from the endpoint.
  566. *
  567. * If any of the addresses is already in the bind address list of the
  568. * association, we do not send the chunk for that association. But it will not
  569. * affect other associations.
  570. *
  571. * Only sctp_setsockopt_bindx() is supposed to call this function.
  572. */
  573. static int sctp_send_asconf_del_ip(struct sock *sk,
  574. struct sockaddr *addrs,
  575. int addrcnt)
  576. {
  577. struct sctp_sock *sp;
  578. struct sctp_endpoint *ep;
  579. struct sctp_association *asoc;
  580. struct sctp_bind_addr *bp;
  581. struct sctp_chunk *chunk;
  582. union sctp_addr *laddr;
  583. void *addr_buf;
  584. struct sctp_af *af;
  585. struct list_head *pos;
  586. int i;
  587. int retval = 0;
  588. if (!sctp_addip_enable)
  589. return retval;
  590. sp = sctp_sk(sk);
  591. ep = sp->ep;
  592. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  593. __FUNCTION__, sk, addrs, addrcnt);
  594. list_for_each(pos, &ep->asocs) {
  595. asoc = list_entry(pos, struct sctp_association, asocs);
  596. if (!asoc->peer.asconf_capable)
  597. continue;
  598. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  599. continue;
  600. if (!sctp_state(asoc, ESTABLISHED))
  601. continue;
  602. /* Check if any address in the packed array of addresses is
  603. * not present in the bind address list of the association.
  604. * If so, do not send the asconf chunk to its peer, but
  605. * continue with other associations.
  606. */
  607. addr_buf = addrs;
  608. for (i = 0; i < addrcnt; i++) {
  609. laddr = (union sctp_addr *)addr_buf;
  610. af = sctp_get_af_specific(laddr->v4.sin_family);
  611. if (!af) {
  612. retval = -EINVAL;
  613. goto out;
  614. }
  615. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  616. break;
  617. addr_buf += af->sockaddr_len;
  618. }
  619. if (i < addrcnt)
  620. continue;
  621. /* Find one address in the association's bind address list
  622. * that is not in the packed array of addresses. This is to
  623. * make sure that we do not delete all the addresses in the
  624. * association.
  625. */
  626. sctp_read_lock(&asoc->base.addr_lock);
  627. bp = &asoc->base.bind_addr;
  628. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  629. addrcnt, sp);
  630. sctp_read_unlock(&asoc->base.addr_lock);
  631. if (!laddr)
  632. continue;
  633. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  634. SCTP_PARAM_DEL_IP);
  635. if (!chunk) {
  636. retval = -ENOMEM;
  637. goto out;
  638. }
  639. retval = sctp_send_asconf(asoc, chunk);
  640. /* FIXME: After sending the delete address ASCONF chunk, we
  641. * cannot remove the addresses from the association's bind
  642. * address list, because there maybe some packet send to
  643. * the delete addresses, so we should wait until ASCONF_ACK
  644. * packet is received.
  645. */
  646. }
  647. out:
  648. return retval;
  649. }
  650. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  651. *
  652. * API 8.1
  653. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  654. * int flags);
  655. *
  656. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  657. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  658. * or IPv6 addresses.
  659. *
  660. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  661. * Section 3.1.2 for this usage.
  662. *
  663. * addrs is a pointer to an array of one or more socket addresses. Each
  664. * address is contained in its appropriate structure (i.e. struct
  665. * sockaddr_in or struct sockaddr_in6) the family of the address type
  666. * must be used to distengish the address length (note that this
  667. * representation is termed a "packed array" of addresses). The caller
  668. * specifies the number of addresses in the array with addrcnt.
  669. *
  670. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  671. * -1, and sets errno to the appropriate error code.
  672. *
  673. * For SCTP, the port given in each socket address must be the same, or
  674. * sctp_bindx() will fail, setting errno to EINVAL.
  675. *
  676. * The flags parameter is formed from the bitwise OR of zero or more of
  677. * the following currently defined flags:
  678. *
  679. * SCTP_BINDX_ADD_ADDR
  680. *
  681. * SCTP_BINDX_REM_ADDR
  682. *
  683. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  684. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  685. * addresses from the association. The two flags are mutually exclusive;
  686. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  687. * not remove all addresses from an association; sctp_bindx() will
  688. * reject such an attempt with EINVAL.
  689. *
  690. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  691. * additional addresses with an endpoint after calling bind(). Or use
  692. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  693. * socket is associated with so that no new association accepted will be
  694. * associated with those addresses. If the endpoint supports dynamic
  695. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  696. * endpoint to send the appropriate message to the peer to change the
  697. * peers address lists.
  698. *
  699. * Adding and removing addresses from a connected association is
  700. * optional functionality. Implementations that do not support this
  701. * functionality should return EOPNOTSUPP.
  702. *
  703. * Basically do nothing but copying the addresses from user to kernel
  704. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  705. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() * from userspace.
  706. *
  707. * We don't use copy_from_user() for optimization: we first do the
  708. * sanity checks (buffer size -fast- and access check-healthy
  709. * pointer); if all of those succeed, then we can alloc the memory
  710. * (expensive operation) needed to copy the data to kernel. Then we do
  711. * the copying without checking the user space area
  712. * (__copy_from_user()).
  713. *
  714. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  715. * it.
  716. *
  717. * sk The sk of the socket
  718. * addrs The pointer to the addresses in user land
  719. * addrssize Size of the addrs buffer
  720. * op Operation to perform (add or remove, see the flags of
  721. * sctp_bindx)
  722. *
  723. * Returns 0 if ok, <0 errno code on error.
  724. */
  725. SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
  726. struct sockaddr __user *addrs,
  727. int addrs_size, int op)
  728. {
  729. struct sockaddr *kaddrs;
  730. int err;
  731. int addrcnt = 0;
  732. int walk_size = 0;
  733. struct sockaddr *sa_addr;
  734. void *addr_buf;
  735. struct sctp_af *af;
  736. SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
  737. " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
  738. if (unlikely(addrs_size <= 0))
  739. return -EINVAL;
  740. /* Check the user passed a healthy pointer. */
  741. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  742. return -EFAULT;
  743. /* Alloc space for the address array in kernel memory. */
  744. kaddrs = (struct sockaddr *)kmalloc(addrs_size, GFP_KERNEL);
  745. if (unlikely(!kaddrs))
  746. return -ENOMEM;
  747. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  748. kfree(kaddrs);
  749. return -EFAULT;
  750. }
  751. /* Walk through the addrs buffer and count the number of addresses. */
  752. addr_buf = kaddrs;
  753. while (walk_size < addrs_size) {
  754. sa_addr = (struct sockaddr *)addr_buf;
  755. af = sctp_get_af_specific(sa_addr->sa_family);
  756. /* If the address family is not supported or if this address
  757. * causes the address buffer to overflow return EINVAL.
  758. */
  759. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  760. kfree(kaddrs);
  761. return -EINVAL;
  762. }
  763. addrcnt++;
  764. addr_buf += af->sockaddr_len;
  765. walk_size += af->sockaddr_len;
  766. }
  767. /* Do the work. */
  768. switch (op) {
  769. case SCTP_BINDX_ADD_ADDR:
  770. err = sctp_bindx_add(sk, kaddrs, addrcnt);
  771. if (err)
  772. goto out;
  773. err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
  774. break;
  775. case SCTP_BINDX_REM_ADDR:
  776. err = sctp_bindx_rem(sk, kaddrs, addrcnt);
  777. if (err)
  778. goto out;
  779. err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
  780. break;
  781. default:
  782. err = -EINVAL;
  783. break;
  784. };
  785. out:
  786. kfree(kaddrs);
  787. return err;
  788. }
  789. /* API 3.1.4 close() - UDP Style Syntax
  790. * Applications use close() to perform graceful shutdown (as described in
  791. * Section 10.1 of [SCTP]) on ALL the associations currently represented
  792. * by a UDP-style socket.
  793. *
  794. * The syntax is
  795. *
  796. * ret = close(int sd);
  797. *
  798. * sd - the socket descriptor of the associations to be closed.
  799. *
  800. * To gracefully shutdown a specific association represented by the
  801. * UDP-style socket, an application should use the sendmsg() call,
  802. * passing no user data, but including the appropriate flag in the
  803. * ancillary data (see Section xxxx).
  804. *
  805. * If sd in the close() call is a branched-off socket representing only
  806. * one association, the shutdown is performed on that association only.
  807. *
  808. * 4.1.6 close() - TCP Style Syntax
  809. *
  810. * Applications use close() to gracefully close down an association.
  811. *
  812. * The syntax is:
  813. *
  814. * int close(int sd);
  815. *
  816. * sd - the socket descriptor of the association to be closed.
  817. *
  818. * After an application calls close() on a socket descriptor, no further
  819. * socket operations will succeed on that descriptor.
  820. *
  821. * API 7.1.4 SO_LINGER
  822. *
  823. * An application using the TCP-style socket can use this option to
  824. * perform the SCTP ABORT primitive. The linger option structure is:
  825. *
  826. * struct linger {
  827. * int l_onoff; // option on/off
  828. * int l_linger; // linger time
  829. * };
  830. *
  831. * To enable the option, set l_onoff to 1. If the l_linger value is set
  832. * to 0, calling close() is the same as the ABORT primitive. If the
  833. * value is set to a negative value, the setsockopt() call will return
  834. * an error. If the value is set to a positive value linger_time, the
  835. * close() can be blocked for at most linger_time ms. If the graceful
  836. * shutdown phase does not finish during this period, close() will
  837. * return but the graceful shutdown phase continues in the system.
  838. */
  839. SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
  840. {
  841. struct sctp_endpoint *ep;
  842. struct sctp_association *asoc;
  843. struct list_head *pos, *temp;
  844. SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
  845. sctp_lock_sock(sk);
  846. sk->sk_shutdown = SHUTDOWN_MASK;
  847. ep = sctp_sk(sk)->ep;
  848. /* Walk all associations on a socket, not on an endpoint. */
  849. list_for_each_safe(pos, temp, &ep->asocs) {
  850. asoc = list_entry(pos, struct sctp_association, asocs);
  851. if (sctp_style(sk, TCP)) {
  852. /* A closed association can still be in the list if
  853. * it belongs to a TCP-style listening socket that is
  854. * not yet accepted. If so, free it. If not, send an
  855. * ABORT or SHUTDOWN based on the linger options.
  856. */
  857. if (sctp_state(asoc, CLOSED)) {
  858. sctp_unhash_established(asoc);
  859. sctp_association_free(asoc);
  860. } else if (sock_flag(sk, SOCK_LINGER) &&
  861. !sk->sk_lingertime)
  862. sctp_primitive_ABORT(asoc, NULL);
  863. else
  864. sctp_primitive_SHUTDOWN(asoc, NULL);
  865. } else
  866. sctp_primitive_SHUTDOWN(asoc, NULL);
  867. }
  868. /* Clean up any skbs sitting on the receive queue. */
  869. sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
  870. sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
  871. /* On a TCP-style socket, block for at most linger_time if set. */
  872. if (sctp_style(sk, TCP) && timeout)
  873. sctp_wait_for_close(sk, timeout);
  874. /* This will run the backlog queue. */
  875. sctp_release_sock(sk);
  876. /* Supposedly, no process has access to the socket, but
  877. * the net layers still may.
  878. */
  879. sctp_local_bh_disable();
  880. sctp_bh_lock_sock(sk);
  881. /* Hold the sock, since sk_common_release() will put sock_put()
  882. * and we have just a little more cleanup.
  883. */
  884. sock_hold(sk);
  885. sk_common_release(sk);
  886. sctp_bh_unlock_sock(sk);
  887. sctp_local_bh_enable();
  888. sock_put(sk);
  889. SCTP_DBG_OBJCNT_DEC(sock);
  890. }
  891. /* Handle EPIPE error. */
  892. static int sctp_error(struct sock *sk, int flags, int err)
  893. {
  894. if (err == -EPIPE)
  895. err = sock_error(sk) ? : -EPIPE;
  896. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  897. send_sig(SIGPIPE, current, 0);
  898. return err;
  899. }
  900. /* API 3.1.3 sendmsg() - UDP Style Syntax
  901. *
  902. * An application uses sendmsg() and recvmsg() calls to transmit data to
  903. * and receive data from its peer.
  904. *
  905. * ssize_t sendmsg(int socket, const struct msghdr *message,
  906. * int flags);
  907. *
  908. * socket - the socket descriptor of the endpoint.
  909. * message - pointer to the msghdr structure which contains a single
  910. * user message and possibly some ancillary data.
  911. *
  912. * See Section 5 for complete description of the data
  913. * structures.
  914. *
  915. * flags - flags sent or received with the user message, see Section
  916. * 5 for complete description of the flags.
  917. *
  918. * Note: This function could use a rewrite especially when explicit
  919. * connect support comes in.
  920. */
  921. /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
  922. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
  923. SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
  924. struct msghdr *msg, size_t msg_len)
  925. {
  926. struct sctp_sock *sp;
  927. struct sctp_endpoint *ep;
  928. struct sctp_association *new_asoc=NULL, *asoc=NULL;
  929. struct sctp_transport *transport, *chunk_tp;
  930. struct sctp_chunk *chunk;
  931. union sctp_addr to;
  932. struct sockaddr *msg_name = NULL;
  933. struct sctp_sndrcvinfo default_sinfo = { 0 };
  934. struct sctp_sndrcvinfo *sinfo;
  935. struct sctp_initmsg *sinit;
  936. sctp_assoc_t associd = 0;
  937. sctp_cmsgs_t cmsgs = { NULL };
  938. int err;
  939. sctp_scope_t scope;
  940. long timeo;
  941. __u16 sinfo_flags = 0;
  942. struct sctp_datamsg *datamsg;
  943. struct list_head *pos;
  944. int msg_flags = msg->msg_flags;
  945. SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
  946. sk, msg, msg_len);
  947. err = 0;
  948. sp = sctp_sk(sk);
  949. ep = sp->ep;
  950. SCTP_DEBUG_PRINTK("Using endpoint: %s.\n", ep->debug_name);
  951. /* We cannot send a message over a TCP-style listening socket. */
  952. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
  953. err = -EPIPE;
  954. goto out_nounlock;
  955. }
  956. /* Parse out the SCTP CMSGs. */
  957. err = sctp_msghdr_parse(msg, &cmsgs);
  958. if (err) {
  959. SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
  960. goto out_nounlock;
  961. }
  962. /* Fetch the destination address for this packet. This
  963. * address only selects the association--it is not necessarily
  964. * the address we will send to.
  965. * For a peeled-off socket, msg_name is ignored.
  966. */
  967. if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
  968. int msg_namelen = msg->msg_namelen;
  969. err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
  970. msg_namelen);
  971. if (err)
  972. return err;
  973. if (msg_namelen > sizeof(to))
  974. msg_namelen = sizeof(to);
  975. memcpy(&to, msg->msg_name, msg_namelen);
  976. SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
  977. "0x%x:%u.\n",
  978. to.v4.sin_addr.s_addr, to.v4.sin_port);
  979. to.v4.sin_port = ntohs(to.v4.sin_port);
  980. msg_name = msg->msg_name;
  981. }
  982. sinfo = cmsgs.info;
  983. sinit = cmsgs.init;
  984. /* Did the user specify SNDRCVINFO? */
  985. if (sinfo) {
  986. sinfo_flags = sinfo->sinfo_flags;
  987. associd = sinfo->sinfo_assoc_id;
  988. }
  989. SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
  990. msg_len, sinfo_flags);
  991. /* MSG_EOF or MSG_ABORT cannot be set on a TCP-style socket. */
  992. if (sctp_style(sk, TCP) && (sinfo_flags & (MSG_EOF | MSG_ABORT))) {
  993. err = -EINVAL;
  994. goto out_nounlock;
  995. }
  996. /* If MSG_EOF is set, no data can be sent. Disallow sending zero
  997. * length messages when MSG_EOF|MSG_ABORT is not set.
  998. * If MSG_ABORT is set, the message length could be non zero with
  999. * the msg_iov set to the user abort reason.
  1000. */
  1001. if (((sinfo_flags & MSG_EOF) && (msg_len > 0)) ||
  1002. (!(sinfo_flags & (MSG_EOF|MSG_ABORT)) && (msg_len == 0))) {
  1003. err = -EINVAL;
  1004. goto out_nounlock;
  1005. }
  1006. /* If MSG_ADDR_OVER is set, there must be an address
  1007. * specified in msg_name.
  1008. */
  1009. if ((sinfo_flags & MSG_ADDR_OVER) && (!msg->msg_name)) {
  1010. err = -EINVAL;
  1011. goto out_nounlock;
  1012. }
  1013. transport = NULL;
  1014. SCTP_DEBUG_PRINTK("About to look up association.\n");
  1015. sctp_lock_sock(sk);
  1016. /* If a msg_name has been specified, assume this is to be used. */
  1017. if (msg_name) {
  1018. /* Look for a matching association on the endpoint. */
  1019. asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  1020. if (!asoc) {
  1021. /* If we could not find a matching association on the
  1022. * endpoint, make sure that it is not a TCP-style
  1023. * socket that already has an association or there is
  1024. * no peeled-off association on another socket.
  1025. */
  1026. if ((sctp_style(sk, TCP) &&
  1027. sctp_sstate(sk, ESTABLISHED)) ||
  1028. sctp_endpoint_is_peeled_off(ep, &to)) {
  1029. err = -EADDRNOTAVAIL;
  1030. goto out_unlock;
  1031. }
  1032. }
  1033. } else {
  1034. asoc = sctp_id2assoc(sk, associd);
  1035. if (!asoc) {
  1036. err = -EPIPE;
  1037. goto out_unlock;
  1038. }
  1039. }
  1040. if (asoc) {
  1041. SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
  1042. /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
  1043. * socket that has an association in CLOSED state. This can
  1044. * happen when an accepted socket has an association that is
  1045. * already CLOSED.
  1046. */
  1047. if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
  1048. err = -EPIPE;
  1049. goto out_unlock;
  1050. }
  1051. if (sinfo_flags & MSG_EOF) {
  1052. SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
  1053. asoc);
  1054. sctp_primitive_SHUTDOWN(asoc, NULL);
  1055. err = 0;
  1056. goto out_unlock;
  1057. }
  1058. if (sinfo_flags & MSG_ABORT) {
  1059. SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
  1060. sctp_primitive_ABORT(asoc, msg);
  1061. err = 0;
  1062. goto out_unlock;
  1063. }
  1064. }
  1065. /* Do we need to create the association? */
  1066. if (!asoc) {
  1067. SCTP_DEBUG_PRINTK("There is no association yet.\n");
  1068. if (sinfo_flags & (MSG_EOF | MSG_ABORT)) {
  1069. err = -EINVAL;
  1070. goto out_unlock;
  1071. }
  1072. /* Check for invalid stream against the stream counts,
  1073. * either the default or the user specified stream counts.
  1074. */
  1075. if (sinfo) {
  1076. if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
  1077. /* Check against the defaults. */
  1078. if (sinfo->sinfo_stream >=
  1079. sp->initmsg.sinit_num_ostreams) {
  1080. err = -EINVAL;
  1081. goto out_unlock;
  1082. }
  1083. } else {
  1084. /* Check against the requested. */
  1085. if (sinfo->sinfo_stream >=
  1086. sinit->sinit_num_ostreams) {
  1087. err = -EINVAL;
  1088. goto out_unlock;
  1089. }
  1090. }
  1091. }
  1092. /*
  1093. * API 3.1.2 bind() - UDP Style Syntax
  1094. * If a bind() or sctp_bindx() is not called prior to a
  1095. * sendmsg() call that initiates a new association, the
  1096. * system picks an ephemeral port and will choose an address
  1097. * set equivalent to binding with a wildcard address.
  1098. */
  1099. if (!ep->base.bind_addr.port) {
  1100. if (sctp_autobind(sk)) {
  1101. err = -EAGAIN;
  1102. goto out_unlock;
  1103. }
  1104. }
  1105. scope = sctp_scope(&to);
  1106. new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  1107. if (!new_asoc) {
  1108. err = -ENOMEM;
  1109. goto out_unlock;
  1110. }
  1111. asoc = new_asoc;
  1112. /* If the SCTP_INIT ancillary data is specified, set all
  1113. * the association init values accordingly.
  1114. */
  1115. if (sinit) {
  1116. if (sinit->sinit_num_ostreams) {
  1117. asoc->c.sinit_num_ostreams =
  1118. sinit->sinit_num_ostreams;
  1119. }
  1120. if (sinit->sinit_max_instreams) {
  1121. asoc->c.sinit_max_instreams =
  1122. sinit->sinit_max_instreams;
  1123. }
  1124. if (sinit->sinit_max_attempts) {
  1125. asoc->max_init_attempts
  1126. = sinit->sinit_max_attempts;
  1127. }
  1128. if (sinit->sinit_max_init_timeo) {
  1129. asoc->max_init_timeo =
  1130. msecs_to_jiffies(sinit->sinit_max_init_timeo);
  1131. }
  1132. }
  1133. /* Prime the peer's transport structures. */
  1134. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
  1135. if (!transport) {
  1136. err = -ENOMEM;
  1137. goto out_free;
  1138. }
  1139. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  1140. if (err < 0) {
  1141. err = -ENOMEM;
  1142. goto out_free;
  1143. }
  1144. }
  1145. /* ASSERT: we have a valid association at this point. */
  1146. SCTP_DEBUG_PRINTK("We have a valid association.\n");
  1147. if (!sinfo) {
  1148. /* If the user didn't specify SNDRCVINFO, make up one with
  1149. * some defaults.
  1150. */
  1151. default_sinfo.sinfo_stream = asoc->default_stream;
  1152. default_sinfo.sinfo_flags = asoc->default_flags;
  1153. default_sinfo.sinfo_ppid = asoc->default_ppid;
  1154. default_sinfo.sinfo_context = asoc->default_context;
  1155. default_sinfo.sinfo_timetolive = asoc->default_timetolive;
  1156. default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
  1157. sinfo = &default_sinfo;
  1158. }
  1159. /* API 7.1.7, the sndbuf size per association bounds the
  1160. * maximum size of data that can be sent in a single send call.
  1161. */
  1162. if (msg_len > sk->sk_sndbuf) {
  1163. err = -EMSGSIZE;
  1164. goto out_free;
  1165. }
  1166. /* If fragmentation is disabled and the message length exceeds the
  1167. * association fragmentation point, return EMSGSIZE. The I-D
  1168. * does not specify what this error is, but this looks like
  1169. * a great fit.
  1170. */
  1171. if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
  1172. err = -EMSGSIZE;
  1173. goto out_free;
  1174. }
  1175. if (sinfo) {
  1176. /* Check for invalid stream. */
  1177. if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
  1178. err = -EINVAL;
  1179. goto out_free;
  1180. }
  1181. }
  1182. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1183. if (!sctp_wspace(asoc)) {
  1184. err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
  1185. if (err)
  1186. goto out_free;
  1187. }
  1188. /* If an address is passed with the sendto/sendmsg call, it is used
  1189. * to override the primary destination address in the TCP model, or
  1190. * when MSG_ADDR_OVER flag is set in the UDP model.
  1191. */
  1192. if ((sctp_style(sk, TCP) && msg_name) ||
  1193. (sinfo_flags & MSG_ADDR_OVER)) {
  1194. chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
  1195. if (!chunk_tp) {
  1196. err = -EINVAL;
  1197. goto out_free;
  1198. }
  1199. } else
  1200. chunk_tp = NULL;
  1201. /* Auto-connect, if we aren't connected already. */
  1202. if (sctp_state(asoc, CLOSED)) {
  1203. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  1204. if (err < 0)
  1205. goto out_free;
  1206. SCTP_DEBUG_PRINTK("We associated primitively.\n");
  1207. }
  1208. /* Break the message into multiple chunks of maximum size. */
  1209. datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
  1210. if (!datamsg) {
  1211. err = -ENOMEM;
  1212. goto out_free;
  1213. }
  1214. /* Now send the (possibly) fragmented message. */
  1215. list_for_each(pos, &datamsg->chunks) {
  1216. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  1217. sctp_datamsg_track(chunk);
  1218. /* Do accounting for the write space. */
  1219. sctp_set_owner_w(chunk);
  1220. chunk->transport = chunk_tp;
  1221. /* Send it to the lower layers. Note: all chunks
  1222. * must either fail or succeed. The lower layer
  1223. * works that way today. Keep it that way or this
  1224. * breaks.
  1225. */
  1226. err = sctp_primitive_SEND(asoc, chunk);
  1227. /* Did the lower layer accept the chunk? */
  1228. if (err)
  1229. sctp_chunk_free(chunk);
  1230. SCTP_DEBUG_PRINTK("We sent primitively.\n");
  1231. }
  1232. sctp_datamsg_free(datamsg);
  1233. if (err)
  1234. goto out_free;
  1235. else
  1236. err = msg_len;
  1237. /* If we are already past ASSOCIATE, the lower
  1238. * layers are responsible for association cleanup.
  1239. */
  1240. goto out_unlock;
  1241. out_free:
  1242. if (new_asoc)
  1243. sctp_association_free(asoc);
  1244. out_unlock:
  1245. sctp_release_sock(sk);
  1246. out_nounlock:
  1247. return sctp_error(sk, msg_flags, err);
  1248. #if 0
  1249. do_sock_err:
  1250. if (msg_len)
  1251. err = msg_len;
  1252. else
  1253. err = sock_error(sk);
  1254. goto out;
  1255. do_interrupted:
  1256. if (msg_len)
  1257. err = msg_len;
  1258. goto out;
  1259. #endif /* 0 */
  1260. }
  1261. /* This is an extended version of skb_pull() that removes the data from the
  1262. * start of a skb even when data is spread across the list of skb's in the
  1263. * frag_list. len specifies the total amount of data that needs to be removed.
  1264. * when 'len' bytes could be removed from the skb, it returns 0.
  1265. * If 'len' exceeds the total skb length, it returns the no. of bytes that
  1266. * could not be removed.
  1267. */
  1268. static int sctp_skb_pull(struct sk_buff *skb, int len)
  1269. {
  1270. struct sk_buff *list;
  1271. int skb_len = skb_headlen(skb);
  1272. int rlen;
  1273. if (len <= skb_len) {
  1274. __skb_pull(skb, len);
  1275. return 0;
  1276. }
  1277. len -= skb_len;
  1278. __skb_pull(skb, skb_len);
  1279. for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
  1280. rlen = sctp_skb_pull(list, len);
  1281. skb->len -= (len-rlen);
  1282. skb->data_len -= (len-rlen);
  1283. if (!rlen)
  1284. return 0;
  1285. len = rlen;
  1286. }
  1287. return len;
  1288. }
  1289. /* API 3.1.3 recvmsg() - UDP Style Syntax
  1290. *
  1291. * ssize_t recvmsg(int socket, struct msghdr *message,
  1292. * int flags);
  1293. *
  1294. * socket - the socket descriptor of the endpoint.
  1295. * message - pointer to the msghdr structure which contains a single
  1296. * user message and possibly some ancillary data.
  1297. *
  1298. * See Section 5 for complete description of the data
  1299. * structures.
  1300. *
  1301. * flags - flags sent or received with the user message, see Section
  1302. * 5 for complete description of the flags.
  1303. */
  1304. static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
  1305. SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
  1306. struct msghdr *msg, size_t len, int noblock,
  1307. int flags, int *addr_len)
  1308. {
  1309. struct sctp_ulpevent *event = NULL;
  1310. struct sctp_sock *sp = sctp_sk(sk);
  1311. struct sk_buff *skb;
  1312. int copied;
  1313. int err = 0;
  1314. int skb_len;
  1315. SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
  1316. "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
  1317. "len", len, "knoblauch", noblock,
  1318. "flags", flags, "addr_len", addr_len);
  1319. sctp_lock_sock(sk);
  1320. if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
  1321. err = -ENOTCONN;
  1322. goto out;
  1323. }
  1324. skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
  1325. if (!skb)
  1326. goto out;
  1327. /* Get the total length of the skb including any skb's in the
  1328. * frag_list.
  1329. */
  1330. skb_len = skb->len;
  1331. copied = skb_len;
  1332. if (copied > len)
  1333. copied = len;
  1334. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1335. event = sctp_skb2event(skb);
  1336. if (err)
  1337. goto out_free;
  1338. sock_recv_timestamp(msg, sk, skb);
  1339. if (sctp_ulpevent_is_notification(event)) {
  1340. msg->msg_flags |= MSG_NOTIFICATION;
  1341. sp->pf->event_msgname(event, msg->msg_name, addr_len);
  1342. } else {
  1343. sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
  1344. }
  1345. /* Check if we allow SCTP_SNDRCVINFO. */
  1346. if (sp->subscribe.sctp_data_io_event)
  1347. sctp_ulpevent_read_sndrcvinfo(event, msg);
  1348. #if 0
  1349. /* FIXME: we should be calling IP/IPv6 layers. */
  1350. if (sk->sk_protinfo.af_inet.cmsg_flags)
  1351. ip_cmsg_recv(msg, skb);
  1352. #endif
  1353. err = copied;
  1354. /* If skb's length exceeds the user's buffer, update the skb and
  1355. * push it back to the receive_queue so that the next call to
  1356. * recvmsg() will return the remaining data. Don't set MSG_EOR.
  1357. */
  1358. if (skb_len > copied) {
  1359. msg->msg_flags &= ~MSG_EOR;
  1360. if (flags & MSG_PEEK)
  1361. goto out_free;
  1362. sctp_skb_pull(skb, copied);
  1363. skb_queue_head(&sk->sk_receive_queue, skb);
  1364. /* When only partial message is copied to the user, increase
  1365. * rwnd by that amount. If all the data in the skb is read,
  1366. * rwnd is updated when the event is freed.
  1367. */
  1368. sctp_assoc_rwnd_increase(event->asoc, copied);
  1369. goto out;
  1370. } else if ((event->msg_flags & MSG_NOTIFICATION) ||
  1371. (event->msg_flags & MSG_EOR))
  1372. msg->msg_flags |= MSG_EOR;
  1373. else
  1374. msg->msg_flags &= ~MSG_EOR;
  1375. out_free:
  1376. if (flags & MSG_PEEK) {
  1377. /* Release the skb reference acquired after peeking the skb in
  1378. * sctp_skb_recv_datagram().
  1379. */
  1380. kfree_skb(skb);
  1381. } else {
  1382. /* Free the event which includes releasing the reference to
  1383. * the owner of the skb, freeing the skb and updating the
  1384. * rwnd.
  1385. */
  1386. sctp_ulpevent_free(event);
  1387. }
  1388. out:
  1389. sctp_release_sock(sk);
  1390. return err;
  1391. }
  1392. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  1393. *
  1394. * This option is a on/off flag. If enabled no SCTP message
  1395. * fragmentation will be performed. Instead if a message being sent
  1396. * exceeds the current PMTU size, the message will NOT be sent and
  1397. * instead a error will be indicated to the user.
  1398. */
  1399. static int sctp_setsockopt_disable_fragments(struct sock *sk,
  1400. char __user *optval, int optlen)
  1401. {
  1402. int val;
  1403. if (optlen < sizeof(int))
  1404. return -EINVAL;
  1405. if (get_user(val, (int __user *)optval))
  1406. return -EFAULT;
  1407. sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
  1408. return 0;
  1409. }
  1410. static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
  1411. int optlen)
  1412. {
  1413. if (optlen != sizeof(struct sctp_event_subscribe))
  1414. return -EINVAL;
  1415. if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
  1416. return -EFAULT;
  1417. return 0;
  1418. }
  1419. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  1420. *
  1421. * This socket option is applicable to the UDP-style socket only. When
  1422. * set it will cause associations that are idle for more than the
  1423. * specified number of seconds to automatically close. An association
  1424. * being idle is defined an association that has NOT sent or received
  1425. * user data. The special value of '0' indicates that no automatic
  1426. * close of any associations should be performed. The option expects an
  1427. * integer defining the number of seconds of idle time before an
  1428. * association is closed.
  1429. */
  1430. static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
  1431. int optlen)
  1432. {
  1433. struct sctp_sock *sp = sctp_sk(sk);
  1434. /* Applicable to UDP-style socket only */
  1435. if (sctp_style(sk, TCP))
  1436. return -EOPNOTSUPP;
  1437. if (optlen != sizeof(int))
  1438. return -EINVAL;
  1439. if (copy_from_user(&sp->autoclose, optval, optlen))
  1440. return -EFAULT;
  1441. sp->ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
  1442. return 0;
  1443. }
  1444. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  1445. *
  1446. * Applications can enable or disable heartbeats for any peer address of
  1447. * an association, modify an address's heartbeat interval, force a
  1448. * heartbeat to be sent immediately, and adjust the address's maximum
  1449. * number of retransmissions sent before an address is considered
  1450. * unreachable. The following structure is used to access and modify an
  1451. * address's parameters:
  1452. *
  1453. * struct sctp_paddrparams {
  1454. * sctp_assoc_t spp_assoc_id;
  1455. * struct sockaddr_storage spp_address;
  1456. * uint32_t spp_hbinterval;
  1457. * uint16_t spp_pathmaxrxt;
  1458. * };
  1459. *
  1460. * spp_assoc_id - (UDP style socket) This is filled in the application,
  1461. * and identifies the association for this query.
  1462. * spp_address - This specifies which address is of interest.
  1463. * spp_hbinterval - This contains the value of the heartbeat interval,
  1464. * in milliseconds. A value of 0, when modifying the
  1465. * parameter, specifies that the heartbeat on this
  1466. * address should be disabled. A value of UINT32_MAX
  1467. * (4294967295), when modifying the parameter,
  1468. * specifies that a heartbeat should be sent
  1469. * immediately to the peer address, and the current
  1470. * interval should remain unchanged.
  1471. * spp_pathmaxrxt - This contains the maximum number of
  1472. * retransmissions before this address shall be
  1473. * considered unreachable.
  1474. */
  1475. static int sctp_setsockopt_peer_addr_params(struct sock *sk,
  1476. char __user *optval, int optlen)
  1477. {
  1478. struct sctp_paddrparams params;
  1479. struct sctp_transport *trans;
  1480. int error;
  1481. if (optlen != sizeof(struct sctp_paddrparams))
  1482. return -EINVAL;
  1483. if (copy_from_user(&params, optval, optlen))
  1484. return -EFAULT;
  1485. /*
  1486. * API 7. Socket Options (setting the default value for the endpoint)
  1487. * All options that support specific settings on an association by
  1488. * filling in either an association id variable or a sockaddr_storage
  1489. * SHOULD also support setting of the same value for the entire endpoint
  1490. * (i.e. future associations). To accomplish this the following logic is
  1491. * used when setting one of these options:
  1492. * c) If neither the sockaddr_storage or association identification is
  1493. * set i.e. the sockaddr_storage is set to all 0's (INADDR_ANY) and
  1494. * the association identification is 0, the settings are a default
  1495. * and to be applied to the endpoint (all future associations).
  1496. */
  1497. /* update default value for endpoint (all future associations) */
  1498. if (!params.spp_assoc_id &&
  1499. sctp_is_any(( union sctp_addr *)&params.spp_address)) {
  1500. /* Manual heartbeat on an endpoint is invalid. */
  1501. if (0xffffffff == params.spp_hbinterval)
  1502. return -EINVAL;
  1503. else if (params.spp_hbinterval)
  1504. sctp_sk(sk)->paddrparam.spp_hbinterval =
  1505. params.spp_hbinterval;
  1506. if (params.spp_pathmaxrxt)
  1507. sctp_sk(sk)->paddrparam.spp_pathmaxrxt =
  1508. params.spp_pathmaxrxt;
  1509. return 0;
  1510. }
  1511. trans = sctp_addr_id2transport(sk, &params.spp_address,
  1512. params.spp_assoc_id);
  1513. if (!trans)
  1514. return -EINVAL;
  1515. /* Applications can enable or disable heartbeats for any peer address
  1516. * of an association, modify an address's heartbeat interval, force a
  1517. * heartbeat to be sent immediately, and adjust the address's maximum
  1518. * number of retransmissions sent before an address is considered
  1519. * unreachable.
  1520. *
  1521. * The value of the heartbeat interval, in milliseconds. A value of
  1522. * UINT32_MAX (4294967295), when modifying the parameter, specifies
  1523. * that a heartbeat should be sent immediately to the peer address,
  1524. * and the current interval should remain unchanged.
  1525. */
  1526. if (0xffffffff == params.spp_hbinterval) {
  1527. error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
  1528. if (error)
  1529. return error;
  1530. } else {
  1531. /* The value of the heartbeat interval, in milliseconds. A value of 0,
  1532. * when modifying the parameter, specifies that the heartbeat on this
  1533. * address should be disabled.
  1534. */
  1535. if (params.spp_hbinterval) {
  1536. trans->hb_allowed = 1;
  1537. trans->hb_interval =
  1538. msecs_to_jiffies(params.spp_hbinterval);
  1539. } else
  1540. trans->hb_allowed = 0;
  1541. }
  1542. /* spp_pathmaxrxt contains the maximum number of retransmissions
  1543. * before this address shall be considered unreachable.
  1544. */
  1545. if (params.spp_pathmaxrxt)
  1546. trans->max_retrans = params.spp_pathmaxrxt;
  1547. return 0;
  1548. }
  1549. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  1550. *
  1551. * Applications can specify protocol parameters for the default association
  1552. * initialization. The option name argument to setsockopt() and getsockopt()
  1553. * is SCTP_INITMSG.
  1554. *
  1555. * Setting initialization parameters is effective only on an unconnected
  1556. * socket (for UDP-style sockets only future associations are effected
  1557. * by the change). With TCP-style sockets, this option is inherited by
  1558. * sockets derived from a listener socket.
  1559. */
  1560. static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
  1561. {
  1562. struct sctp_initmsg sinit;
  1563. struct sctp_sock *sp = sctp_sk(sk);
  1564. if (optlen != sizeof(struct sctp_initmsg))
  1565. return -EINVAL;
  1566. if (copy_from_user(&sinit, optval, optlen))
  1567. return -EFAULT;
  1568. if (sinit.sinit_num_ostreams)
  1569. sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
  1570. if (sinit.sinit_max_instreams)
  1571. sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
  1572. if (sinit.sinit_max_attempts)
  1573. sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
  1574. if (sinit.sinit_max_init_timeo)
  1575. sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
  1576. return 0;
  1577. }
  1578. /*
  1579. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  1580. *
  1581. * Applications that wish to use the sendto() system call may wish to
  1582. * specify a default set of parameters that would normally be supplied
  1583. * through the inclusion of ancillary data. This socket option allows
  1584. * such an application to set the default sctp_sndrcvinfo structure.
  1585. * The application that wishes to use this socket option simply passes
  1586. * in to this call the sctp_sndrcvinfo structure defined in Section
  1587. * 5.2.2) The input parameters accepted by this call include
  1588. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  1589. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  1590. * to this call if the caller is using the UDP model.
  1591. */
  1592. static int sctp_setsockopt_default_send_param(struct sock *sk,
  1593. char __user *optval, int optlen)
  1594. {
  1595. struct sctp_sndrcvinfo info;
  1596. struct sctp_association *asoc;
  1597. struct sctp_sock *sp = sctp_sk(sk);
  1598. if (optlen != sizeof(struct sctp_sndrcvinfo))
  1599. return -EINVAL;
  1600. if (copy_from_user(&info, optval, optlen))
  1601. return -EFAULT;
  1602. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  1603. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  1604. return -EINVAL;
  1605. if (asoc) {
  1606. asoc->default_stream = info.sinfo_stream;
  1607. asoc->default_flags = info.sinfo_flags;
  1608. asoc->default_ppid = info.sinfo_ppid;
  1609. asoc->default_context = info.sinfo_context;
  1610. asoc->default_timetolive = info.sinfo_timetolive;
  1611. } else {
  1612. sp->default_stream = info.sinfo_stream;
  1613. sp->default_flags = info.sinfo_flags;
  1614. sp->default_ppid = info.sinfo_ppid;
  1615. sp->default_context = info.sinfo_context;
  1616. sp->default_timetolive = info.sinfo_timetolive;
  1617. }
  1618. return 0;
  1619. }
  1620. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  1621. *
  1622. * Requests that the local SCTP stack use the enclosed peer address as
  1623. * the association primary. The enclosed address must be one of the
  1624. * association peer's addresses.
  1625. */
  1626. static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
  1627. int optlen)
  1628. {
  1629. struct sctp_prim prim;
  1630. struct sctp_transport *trans;
  1631. if (optlen != sizeof(struct sctp_prim))
  1632. return -EINVAL;
  1633. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  1634. return -EFAULT;
  1635. trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
  1636. if (!trans)
  1637. return -EINVAL;
  1638. sctp_assoc_set_primary(trans->asoc, trans);
  1639. return 0;
  1640. }
  1641. /*
  1642. * 7.1.5 SCTP_NODELAY
  1643. *
  1644. * Turn on/off any Nagle-like algorithm. This means that packets are
  1645. * generally sent as soon as possible and no unnecessary delays are
  1646. * introduced, at the cost of more packets in the network. Expects an
  1647. * integer boolean flag.
  1648. */
  1649. static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
  1650. int optlen)
  1651. {
  1652. int val;
  1653. if (optlen < sizeof(int))
  1654. return -EINVAL;
  1655. if (get_user(val, (int __user *)optval))
  1656. return -EFAULT;
  1657. sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
  1658. return 0;
  1659. }
  1660. /*
  1661. *
  1662. * 7.1.1 SCTP_RTOINFO
  1663. *
  1664. * The protocol parameters used to initialize and bound retransmission
  1665. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  1666. * and modify these parameters.
  1667. * All parameters are time values, in milliseconds. A value of 0, when
  1668. * modifying the parameters, indicates that the current value should not
  1669. * be changed.
  1670. *
  1671. */
  1672. static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
  1673. struct sctp_rtoinfo rtoinfo;
  1674. struct sctp_association *asoc;
  1675. if (optlen != sizeof (struct sctp_rtoinfo))
  1676. return -EINVAL;
  1677. if (copy_from_user(&rtoinfo, optval, optlen))
  1678. return -EFAULT;
  1679. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  1680. /* Set the values to the specific association */
  1681. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  1682. return -EINVAL;
  1683. if (asoc) {
  1684. if (rtoinfo.srto_initial != 0)
  1685. asoc->rto_initial =
  1686. msecs_to_jiffies(rtoinfo.srto_initial);
  1687. if (rtoinfo.srto_max != 0)
  1688. asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
  1689. if (rtoinfo.srto_min != 0)
  1690. asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
  1691. } else {
  1692. /* If there is no association or the association-id = 0
  1693. * set the values to the endpoint.
  1694. */
  1695. struct sctp_sock *sp = sctp_sk(sk);
  1696. if (rtoinfo.srto_initial != 0)
  1697. sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
  1698. if (rtoinfo.srto_max != 0)
  1699. sp->rtoinfo.srto_max = rtoinfo.srto_max;
  1700. if (rtoinfo.srto_min != 0)
  1701. sp->rtoinfo.srto_min = rtoinfo.srto_min;
  1702. }
  1703. return 0;
  1704. }
  1705. /*
  1706. *
  1707. * 7.1.2 SCTP_ASSOCINFO
  1708. *
  1709. * This option is used to tune the the maximum retransmission attempts
  1710. * of the association.
  1711. * Returns an error if the new association retransmission value is
  1712. * greater than the sum of the retransmission value of the peer.
  1713. * See [SCTP] for more information.
  1714. *
  1715. */
  1716. static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
  1717. {
  1718. struct sctp_assocparams assocparams;
  1719. struct sctp_association *asoc;
  1720. if (optlen != sizeof(struct sctp_assocparams))
  1721. return -EINVAL;
  1722. if (copy_from_user(&assocparams, optval, optlen))
  1723. return -EFAULT;
  1724. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  1725. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  1726. return -EINVAL;
  1727. /* Set the values to the specific association */
  1728. if (asoc) {
  1729. if (assocparams.sasoc_asocmaxrxt != 0)
  1730. asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
  1731. if (assocparams.sasoc_cookie_life != 0) {
  1732. asoc->cookie_life.tv_sec =
  1733. assocparams.sasoc_cookie_life / 1000;
  1734. asoc->cookie_life.tv_usec =
  1735. (assocparams.sasoc_cookie_life % 1000)
  1736. * 1000;
  1737. }
  1738. } else {
  1739. /* Set the values to the endpoint */
  1740. struct sctp_sock *sp = sctp_sk(sk);
  1741. if (assocparams.sasoc_asocmaxrxt != 0)
  1742. sp->assocparams.sasoc_asocmaxrxt =
  1743. assocparams.sasoc_asocmaxrxt;
  1744. if (assocparams.sasoc_cookie_life != 0)
  1745. sp->assocparams.sasoc_cookie_life =
  1746. assocparams.sasoc_cookie_life;
  1747. }
  1748. return 0;
  1749. }
  1750. /*
  1751. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  1752. *
  1753. * This socket option is a boolean flag which turns on or off mapped V4
  1754. * addresses. If this option is turned on and the socket is type
  1755. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  1756. * If this option is turned off, then no mapping will be done of V4
  1757. * addresses and a user will receive both PF_INET6 and PF_INET type
  1758. * addresses on the socket.
  1759. */
  1760. static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
  1761. {
  1762. int val;
  1763. struct sctp_sock *sp = sctp_sk(sk);
  1764. if (optlen < sizeof(int))
  1765. return -EINVAL;
  1766. if (get_user(val, (int __user *)optval))
  1767. return -EFAULT;
  1768. if (val)
  1769. sp->v4mapped = 1;
  1770. else
  1771. sp->v4mapped = 0;
  1772. return 0;
  1773. }
  1774. /*
  1775. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  1776. *
  1777. * This socket option specifies the maximum size to put in any outgoing
  1778. * SCTP chunk. If a message is larger than this size it will be
  1779. * fragmented by SCTP into the specified size. Note that the underlying
  1780. * SCTP implementation may fragment into smaller sized chunks when the
  1781. * PMTU of the underlying association is smaller than the value set by
  1782. * the user.
  1783. */
  1784. static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
  1785. {
  1786. struct sctp_association *asoc;
  1787. struct list_head *pos;
  1788. struct sctp_sock *sp = sctp_sk(sk);
  1789. int val;
  1790. if (optlen < sizeof(int))
  1791. return -EINVAL;
  1792. if (get_user(val, (int __user *)optval))
  1793. return -EFAULT;
  1794. if ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))
  1795. return -EINVAL;
  1796. sp->user_frag = val;
  1797. if (val) {
  1798. /* Update the frag_point of the existing associations. */
  1799. list_for_each(pos, &(sp->ep->asocs)) {
  1800. asoc = list_entry(pos, struct sctp_association, asocs);
  1801. asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
  1802. }
  1803. }
  1804. return 0;
  1805. }
  1806. /*
  1807. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  1808. *
  1809. * Requests that the peer mark the enclosed address as the association
  1810. * primary. The enclosed address must be one of the association's
  1811. * locally bound addresses. The following structure is used to make a
  1812. * set primary request:
  1813. */
  1814. static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
  1815. int optlen)
  1816. {
  1817. struct sctp_sock *sp;
  1818. struct sctp_endpoint *ep;
  1819. struct sctp_association *asoc = NULL;
  1820. struct sctp_setpeerprim prim;
  1821. struct sctp_chunk *chunk;
  1822. int err;
  1823. sp = sctp_sk(sk);
  1824. ep = sp->ep;
  1825. if (!sctp_addip_enable)
  1826. return -EPERM;
  1827. if (optlen != sizeof(struct sctp_setpeerprim))
  1828. return -EINVAL;
  1829. if (copy_from_user(&prim, optval, optlen))
  1830. return -EFAULT;
  1831. asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
  1832. if (!asoc)
  1833. return -EINVAL;
  1834. if (!asoc->peer.asconf_capable)
  1835. return -EPERM;
  1836. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  1837. return -EPERM;
  1838. if (!sctp_state(asoc, ESTABLISHED))
  1839. return -ENOTCONN;
  1840. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
  1841. return -EADDRNOTAVAIL;
  1842. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  1843. chunk = sctp_make_asconf_set_prim(asoc,
  1844. (union sctp_addr *)&prim.sspp_addr);
  1845. if (!chunk)
  1846. return -ENOMEM;
  1847. err = sctp_send_asconf(asoc, chunk);
  1848. SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
  1849. return err;
  1850. }
  1851. static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
  1852. int optlen)
  1853. {
  1854. __u32 val;
  1855. if (optlen < sizeof(__u32))
  1856. return -EINVAL;
  1857. if (copy_from_user(&val, optval, sizeof(__u32)))
  1858. return -EFAULT;
  1859. sctp_sk(sk)->adaption_ind = val;
  1860. return 0;
  1861. }
  1862. /* API 6.2 setsockopt(), getsockopt()
  1863. *
  1864. * Applications use setsockopt() and getsockopt() to set or retrieve
  1865. * socket options. Socket options are used to change the default
  1866. * behavior of sockets calls. They are described in Section 7.
  1867. *
  1868. * The syntax is:
  1869. *
  1870. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  1871. * int __user *optlen);
  1872. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  1873. * int optlen);
  1874. *
  1875. * sd - the socket descript.
  1876. * level - set to IPPROTO_SCTP for all SCTP options.
  1877. * optname - the option name.
  1878. * optval - the buffer to store the value of the option.
  1879. * optlen - the size of the buffer.
  1880. */
  1881. SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
  1882. char __user *optval, int optlen)
  1883. {
  1884. int retval = 0;
  1885. SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
  1886. sk, optname);
  1887. /* I can hardly begin to describe how wrong this is. This is
  1888. * so broken as to be worse than useless. The API draft
  1889. * REALLY is NOT helpful here... I am not convinced that the
  1890. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  1891. * are at all well-founded.
  1892. */
  1893. if (level != SOL_SCTP) {
  1894. struct sctp_af *af = sctp_sk(sk)->pf->af;
  1895. retval = af->setsockopt(sk, level, optname, optval, optlen);
  1896. goto out_nounlock;
  1897. }
  1898. sctp_lock_sock(sk);
  1899. switch (optname) {
  1900. case SCTP_SOCKOPT_BINDX_ADD:
  1901. /* 'optlen' is the size of the addresses buffer. */
  1902. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  1903. optlen, SCTP_BINDX_ADD_ADDR);
  1904. break;
  1905. case SCTP_SOCKOPT_BINDX_REM:
  1906. /* 'optlen' is the size of the addresses buffer. */
  1907. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  1908. optlen, SCTP_BINDX_REM_ADDR);
  1909. break;
  1910. case SCTP_DISABLE_FRAGMENTS:
  1911. retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
  1912. break;
  1913. case SCTP_EVENTS:
  1914. retval = sctp_setsockopt_events(sk, optval, optlen);
  1915. break;
  1916. case SCTP_AUTOCLOSE:
  1917. retval = sctp_setsockopt_autoclose(sk, optval, optlen);
  1918. break;
  1919. case SCTP_PEER_ADDR_PARAMS:
  1920. retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
  1921. break;
  1922. case SCTP_INITMSG:
  1923. retval = sctp_setsockopt_initmsg(sk, optval, optlen);
  1924. break;
  1925. case SCTP_DEFAULT_SEND_PARAM:
  1926. retval = sctp_setsockopt_default_send_param(sk, optval,
  1927. optlen);
  1928. break;
  1929. case SCTP_PRIMARY_ADDR:
  1930. retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
  1931. break;
  1932. case SCTP_SET_PEER_PRIMARY_ADDR:
  1933. retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
  1934. break;
  1935. case SCTP_NODELAY:
  1936. retval = sctp_setsockopt_nodelay(sk, optval, optlen);
  1937. break;
  1938. case SCTP_RTOINFO:
  1939. retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
  1940. break;
  1941. case SCTP_ASSOCINFO:
  1942. retval = sctp_setsockopt_associnfo(sk, optval, optlen);
  1943. break;
  1944. case SCTP_I_WANT_MAPPED_V4_ADDR:
  1945. retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
  1946. break;
  1947. case SCTP_MAXSEG:
  1948. retval = sctp_setsockopt_maxseg(sk, optval, optlen);
  1949. break;
  1950. case SCTP_ADAPTION_LAYER:
  1951. retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
  1952. break;
  1953. default:
  1954. retval = -ENOPROTOOPT;
  1955. break;
  1956. };
  1957. sctp_release_sock(sk);
  1958. out_nounlock:
  1959. return retval;
  1960. }
  1961. /* API 3.1.6 connect() - UDP Style Syntax
  1962. *
  1963. * An application may use the connect() call in the UDP model to initiate an
  1964. * association without sending data.
  1965. *
  1966. * The syntax is:
  1967. *
  1968. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  1969. *
  1970. * sd: the socket descriptor to have a new association added to.
  1971. *
  1972. * nam: the address structure (either struct sockaddr_in or struct
  1973. * sockaddr_in6 defined in RFC2553 [7]).
  1974. *
  1975. * len: the size of the address.
  1976. */
  1977. SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *uaddr,
  1978. int addr_len)
  1979. {
  1980. struct sctp_sock *sp;
  1981. struct sctp_endpoint *ep;
  1982. struct sctp_association *asoc;
  1983. struct sctp_transport *transport;
  1984. union sctp_addr to;
  1985. struct sctp_af *af;
  1986. sctp_scope_t scope;
  1987. long timeo;
  1988. int err = 0;
  1989. sctp_lock_sock(sk);
  1990. SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d)\n",
  1991. __FUNCTION__, sk, uaddr, addr_len);
  1992. sp = sctp_sk(sk);
  1993. ep = sp->ep;
  1994. /* connect() cannot be done on a socket that is already in ESTABLISHED
  1995. * state - UDP-style peeled off socket or a TCP-style socket that
  1996. * is already connected.
  1997. * It cannot be done even on a TCP-style listening socket.
  1998. */
  1999. if (sctp_sstate(sk, ESTABLISHED) ||
  2000. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
  2001. err = -EISCONN;
  2002. goto out_unlock;
  2003. }
  2004. err = sctp_verify_addr(sk, (union sctp_addr *)uaddr, addr_len);
  2005. if (err)
  2006. goto out_unlock;
  2007. if (addr_len > sizeof(to))
  2008. addr_len = sizeof(to);
  2009. memcpy(&to, uaddr, addr_len);
  2010. to.v4.sin_port = ntohs(to.v4.sin_port);
  2011. asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  2012. if (asoc) {
  2013. if (asoc->state >= SCTP_STATE_ESTABLISHED)
  2014. err = -EISCONN;
  2015. else
  2016. err = -EALREADY;
  2017. goto out_unlock;
  2018. }
  2019. /* If we could not find a matching association on the endpoint,
  2020. * make sure that there is no peeled-off association matching the
  2021. * peer address even on another socket.
  2022. */
  2023. if (sctp_endpoint_is_peeled_off(ep, &to)) {
  2024. err = -EADDRNOTAVAIL;
  2025. goto out_unlock;
  2026. }
  2027. /* If a bind() or sctp_bindx() is not called prior to a connect()
  2028. * call, the system picks an ephemeral port and will choose an address
  2029. * set equivalent to binding with a wildcard address.
  2030. */
  2031. if (!ep->base.bind_addr.port) {
  2032. if (sctp_autobind(sk)) {
  2033. err = -EAGAIN;
  2034. goto out_unlock;
  2035. }
  2036. }
  2037. scope = sctp_scope(&to);
  2038. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  2039. if (!asoc) {
  2040. err = -ENOMEM;
  2041. goto out_unlock;
  2042. }
  2043. /* Prime the peer's transport structures. */
  2044. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL);
  2045. if (!transport) {
  2046. sctp_association_free(asoc);
  2047. goto out_unlock;
  2048. }
  2049. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  2050. if (err < 0) {
  2051. sctp_association_free(asoc);
  2052. goto out_unlock;
  2053. }
  2054. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  2055. if (err < 0) {
  2056. sctp_association_free(asoc);
  2057. goto out_unlock;
  2058. }
  2059. /* Initialize sk's dport and daddr for getpeername() */
  2060. inet_sk(sk)->dport = htons(asoc->peer.port);
  2061. af = sctp_get_af_specific(to.sa.sa_family);
  2062. af->to_sk_daddr(&to, sk);
  2063. timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
  2064. err = sctp_wait_for_connect(asoc, &timeo);
  2065. out_unlock:
  2066. sctp_release_sock(sk);
  2067. return err;
  2068. }
  2069. /* FIXME: Write comments. */
  2070. SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
  2071. {
  2072. return -EOPNOTSUPP; /* STUB */
  2073. }
  2074. /* 4.1.4 accept() - TCP Style Syntax
  2075. *
  2076. * Applications use accept() call to remove an established SCTP
  2077. * association from the accept queue of the endpoint. A new socket
  2078. * descriptor will be returned from accept() to represent the newly
  2079. * formed association.
  2080. */
  2081. SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
  2082. {
  2083. struct sctp_sock *sp;
  2084. struct sctp_endpoint *ep;
  2085. struct sock *newsk = NULL;
  2086. struct sctp_association *asoc;
  2087. long timeo;
  2088. int error = 0;
  2089. sctp_lock_sock(sk);
  2090. sp = sctp_sk(sk);
  2091. ep = sp->ep;
  2092. if (!sctp_style(sk, TCP)) {
  2093. error = -EOPNOTSUPP;
  2094. goto out;
  2095. }
  2096. if (!sctp_sstate(sk, LISTENING)) {
  2097. error = -EINVAL;
  2098. goto out;
  2099. }
  2100. timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
  2101. error = sctp_wait_for_accept(sk, timeo);
  2102. if (error)
  2103. goto out;
  2104. /* We treat the list of associations on the endpoint as the accept
  2105. * queue and pick the first association on the list.
  2106. */
  2107. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  2108. newsk = sp->pf->create_accept_sk(sk, asoc);
  2109. if (!newsk) {
  2110. error = -ENOMEM;
  2111. goto out;
  2112. }
  2113. /* Populate the fields of the newsk from the oldsk and migrate the
  2114. * asoc to the newsk.
  2115. */
  2116. sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  2117. out:
  2118. sctp_release_sock(sk);
  2119. *err = error;
  2120. return newsk;
  2121. }
  2122. /* The SCTP ioctl handler. */
  2123. SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  2124. {
  2125. return -ENOIOCTLCMD;
  2126. }
  2127. /* This is the function which gets called during socket creation to
  2128. * initialized the SCTP-specific portion of the sock.
  2129. * The sock structure should already be zero-filled memory.
  2130. */
  2131. SCTP_STATIC int sctp_init_sock(struct sock *sk)
  2132. {
  2133. struct sctp_endpoint *ep;
  2134. struct sctp_sock *sp;
  2135. SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
  2136. sp = sctp_sk(sk);
  2137. /* Initialize the SCTP per socket area. */
  2138. switch (sk->sk_type) {
  2139. case SOCK_SEQPACKET:
  2140. sp->type = SCTP_SOCKET_UDP;
  2141. break;
  2142. case SOCK_STREAM:
  2143. sp->type = SCTP_SOCKET_TCP;
  2144. break;
  2145. default:
  2146. return -ESOCKTNOSUPPORT;
  2147. }
  2148. /* Initialize default send parameters. These parameters can be
  2149. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  2150. */
  2151. sp->default_stream = 0;
  2152. sp->default_ppid = 0;
  2153. sp->default_flags = 0;
  2154. sp->default_context = 0;
  2155. sp->default_timetolive = 0;
  2156. /* Initialize default setup parameters. These parameters
  2157. * can be modified with the SCTP_INITMSG socket option or
  2158. * overridden by the SCTP_INIT CMSG.
  2159. */
  2160. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  2161. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  2162. sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
  2163. sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
  2164. /* Initialize default RTO related parameters. These parameters can
  2165. * be modified for with the SCTP_RTOINFO socket option.
  2166. */
  2167. sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
  2168. sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
  2169. sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
  2170. /* Initialize default association related parameters. These parameters
  2171. * can be modified with the SCTP_ASSOCINFO socket option.
  2172. */
  2173. sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
  2174. sp->assocparams.sasoc_number_peer_destinations = 0;
  2175. sp->assocparams.sasoc_peer_rwnd = 0;
  2176. sp->assocparams.sasoc_local_rwnd = 0;
  2177. sp->assocparams.sasoc_cookie_life =
  2178. jiffies_to_msecs(sctp_valid_cookie_life);
  2179. /* Initialize default event subscriptions. By default, all the
  2180. * options are off.
  2181. */
  2182. memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
  2183. /* Default Peer Address Parameters. These defaults can
  2184. * be modified via SCTP_PEER_ADDR_PARAMS
  2185. */
  2186. sp->paddrparam.spp_hbinterval = jiffies_to_msecs(sctp_hb_interval);
  2187. sp->paddrparam.spp_pathmaxrxt = sctp_max_retrans_path;
  2188. /* If enabled no SCTP message fragmentation will be performed.
  2189. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  2190. */
  2191. sp->disable_fragments = 0;
  2192. /* Turn on/off any Nagle-like algorithm. */
  2193. sp->nodelay = 1;
  2194. /* Enable by default. */
  2195. sp->v4mapped = 1;
  2196. /* Auto-close idle associations after the configured
  2197. * number of seconds. A value of 0 disables this
  2198. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  2199. * for UDP-style sockets only.
  2200. */
  2201. sp->autoclose = 0;
  2202. /* User specified fragmentation limit. */
  2203. sp->user_frag = 0;
  2204. sp->adaption_ind = 0;
  2205. sp->pf = sctp_get_pf_specific(sk->sk_family);
  2206. /* Control variables for partial data delivery. */
  2207. sp->pd_mode = 0;
  2208. skb_queue_head_init(&sp->pd_lobby);
  2209. /* Create a per socket endpoint structure. Even if we
  2210. * change the data structure relationships, this may still
  2211. * be useful for storing pre-connect address information.
  2212. */
  2213. ep = sctp_endpoint_new(sk, GFP_KERNEL);
  2214. if (!ep)
  2215. return -ENOMEM;
  2216. sp->ep = ep;
  2217. sp->hmac = NULL;
  2218. SCTP_DBG_OBJCNT_INC(sock);
  2219. return 0;
  2220. }
  2221. /* Cleanup any SCTP per socket resources. */
  2222. SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
  2223. {
  2224. struct sctp_endpoint *ep;
  2225. SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
  2226. /* Release our hold on the endpoint. */
  2227. ep = sctp_sk(sk)->ep;
  2228. sctp_endpoint_free(ep);
  2229. return 0;
  2230. }
  2231. /* API 4.1.7 shutdown() - TCP Style Syntax
  2232. * int shutdown(int socket, int how);
  2233. *
  2234. * sd - the socket descriptor of the association to be closed.
  2235. * how - Specifies the type of shutdown. The values are
  2236. * as follows:
  2237. * SHUT_RD
  2238. * Disables further receive operations. No SCTP
  2239. * protocol action is taken.
  2240. * SHUT_WR
  2241. * Disables further send operations, and initiates
  2242. * the SCTP shutdown sequence.
  2243. * SHUT_RDWR
  2244. * Disables further send and receive operations
  2245. * and initiates the SCTP shutdown sequence.
  2246. */
  2247. SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
  2248. {
  2249. struct sctp_endpoint *ep;
  2250. struct sctp_association *asoc;
  2251. if (!sctp_style(sk, TCP))
  2252. return;
  2253. if (how & SEND_SHUTDOWN) {
  2254. ep = sctp_sk(sk)->ep;
  2255. if (!list_empty(&ep->asocs)) {
  2256. asoc = list_entry(ep->asocs.next,
  2257. struct sctp_association, asocs);
  2258. sctp_primitive_SHUTDOWN(asoc, NULL);
  2259. }
  2260. }
  2261. }
  2262. /* 7.2.1 Association Status (SCTP_STATUS)
  2263. * Applications can retrieve current status information about an
  2264. * association, including association state, peer receiver window size,
  2265. * number of unacked data chunks, and number of data chunks pending
  2266. * receipt. This information is read-only.
  2267. */
  2268. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  2269. char __user *optval,
  2270. int __user *optlen)
  2271. {
  2272. struct sctp_status status;
  2273. struct sctp_association *asoc = NULL;
  2274. struct sctp_transport *transport;
  2275. sctp_assoc_t associd;
  2276. int retval = 0;
  2277. if (len != sizeof(status)) {
  2278. retval = -EINVAL;
  2279. goto out;
  2280. }
  2281. if (copy_from_user(&status, optval, sizeof(status))) {
  2282. retval = -EFAULT;
  2283. goto out;
  2284. }
  2285. associd = status.sstat_assoc_id;
  2286. asoc = sctp_id2assoc(sk, associd);
  2287. if (!asoc) {
  2288. retval = -EINVAL;
  2289. goto out;
  2290. }
  2291. transport = asoc->peer.primary_path;
  2292. status.sstat_assoc_id = sctp_assoc2id(asoc);
  2293. status.sstat_state = asoc->state;
  2294. status.sstat_rwnd = asoc->peer.rwnd;
  2295. status.sstat_unackdata = asoc->unack_data;
  2296. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  2297. status.sstat_instrms = asoc->c.sinit_max_instreams;
  2298. status.sstat_outstrms = asoc->c.sinit_num_ostreams;
  2299. status.sstat_fragmentation_point = asoc->frag_point;
  2300. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  2301. memcpy(&status.sstat_primary.spinfo_address,
  2302. &(transport->ipaddr), sizeof(union sctp_addr));
  2303. /* Map ipv4 address into v4-mapped-on-v6 address. */
  2304. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  2305. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  2306. status.sstat_primary.spinfo_state = transport->active;
  2307. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  2308. status.sstat_primary.spinfo_srtt = transport->srtt;
  2309. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  2310. status.sstat_primary.spinfo_mtu = transport->pmtu;
  2311. if (put_user(len, optlen)) {
  2312. retval = -EFAULT;
  2313. goto out;
  2314. }
  2315. SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
  2316. len, status.sstat_state, status.sstat_rwnd,
  2317. status.sstat_assoc_id);
  2318. if (copy_to_user(optval, &status, len)) {
  2319. retval = -EFAULT;
  2320. goto out;
  2321. }
  2322. out:
  2323. return (retval);
  2324. }
  2325. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  2326. *
  2327. * Applications can retrieve information about a specific peer address
  2328. * of an association, including its reachability state, congestion
  2329. * window, and retransmission timer values. This information is
  2330. * read-only.
  2331. */
  2332. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  2333. char __user *optval,
  2334. int __user *optlen)
  2335. {
  2336. struct sctp_paddrinfo pinfo;
  2337. struct sctp_transport *transport;
  2338. int retval = 0;
  2339. if (len != sizeof(pinfo)) {
  2340. retval = -EINVAL;
  2341. goto out;
  2342. }
  2343. if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
  2344. retval = -EFAULT;
  2345. goto out;
  2346. }
  2347. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  2348. pinfo.spinfo_assoc_id);
  2349. if (!transport)
  2350. return -EINVAL;
  2351. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  2352. pinfo.spinfo_state = transport->active;
  2353. pinfo.spinfo_cwnd = transport->cwnd;
  2354. pinfo.spinfo_srtt = transport->srtt;
  2355. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  2356. pinfo.spinfo_mtu = transport->pmtu;
  2357. if (put_user(len, optlen)) {
  2358. retval = -EFAULT;
  2359. goto out;
  2360. }
  2361. if (copy_to_user(optval, &pinfo, len)) {
  2362. retval = -EFAULT;
  2363. goto out;
  2364. }
  2365. out:
  2366. return (retval);
  2367. }
  2368. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  2369. *
  2370. * This option is a on/off flag. If enabled no SCTP message
  2371. * fragmentation will be performed. Instead if a message being sent
  2372. * exceeds the current PMTU size, the message will NOT be sent and
  2373. * instead a error will be indicated to the user.
  2374. */
  2375. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  2376. char __user *optval, int __user *optlen)
  2377. {
  2378. int val;
  2379. if (len < sizeof(int))
  2380. return -EINVAL;
  2381. len = sizeof(int);
  2382. val = (sctp_sk(sk)->disable_fragments == 1);
  2383. if (put_user(len, optlen))
  2384. return -EFAULT;
  2385. if (copy_to_user(optval, &val, len))
  2386. return -EFAULT;
  2387. return 0;
  2388. }
  2389. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  2390. *
  2391. * This socket option is used to specify various notifications and
  2392. * ancillary data the user wishes to receive.
  2393. */
  2394. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  2395. int __user *optlen)
  2396. {
  2397. if (len != sizeof(struct sctp_event_subscribe))
  2398. return -EINVAL;
  2399. if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
  2400. return -EFAULT;
  2401. return 0;
  2402. }
  2403. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  2404. *
  2405. * This socket option is applicable to the UDP-style socket only. When
  2406. * set it will cause associations that are idle for more than the
  2407. * specified number of seconds to automatically close. An association
  2408. * being idle is defined an association that has NOT sent or received
  2409. * user data. The special value of '0' indicates that no automatic
  2410. * close of any associations should be performed. The option expects an
  2411. * integer defining the number of seconds of idle time before an
  2412. * association is closed.
  2413. */
  2414. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2415. {
  2416. /* Applicable to UDP-style socket only */
  2417. if (sctp_style(sk, TCP))
  2418. return -EOPNOTSUPP;
  2419. if (len != sizeof(int))
  2420. return -EINVAL;
  2421. if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
  2422. return -EFAULT;
  2423. return 0;
  2424. }
  2425. /* Helper routine to branch off an association to a new socket. */
  2426. SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
  2427. struct socket **sockp)
  2428. {
  2429. struct sock *sk = asoc->base.sk;
  2430. struct socket *sock;
  2431. int err = 0;
  2432. /* An association cannot be branched off from an already peeled-off
  2433. * socket, nor is this supported for tcp style sockets.
  2434. */
  2435. if (!sctp_style(sk, UDP))
  2436. return -EINVAL;
  2437. /* Create a new socket. */
  2438. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  2439. if (err < 0)
  2440. return err;
  2441. /* Populate the fields of the newsk from the oldsk and migrate the
  2442. * asoc to the newsk.
  2443. */
  2444. sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  2445. *sockp = sock;
  2446. return err;
  2447. }
  2448. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2449. {
  2450. sctp_peeloff_arg_t peeloff;
  2451. struct socket *newsock;
  2452. int retval = 0;
  2453. struct sctp_association *asoc;
  2454. if (len != sizeof(sctp_peeloff_arg_t))
  2455. return -EINVAL;
  2456. if (copy_from_user(&peeloff, optval, len))
  2457. return -EFAULT;
  2458. asoc = sctp_id2assoc(sk, peeloff.associd);
  2459. if (!asoc) {
  2460. retval = -EINVAL;
  2461. goto out;
  2462. }
  2463. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
  2464. retval = sctp_do_peeloff(asoc, &newsock);
  2465. if (retval < 0)
  2466. goto out;
  2467. /* Map the socket to an unused fd that can be returned to the user. */
  2468. retval = sock_map_fd(newsock);
  2469. if (retval < 0) {
  2470. sock_release(newsock);
  2471. goto out;
  2472. }
  2473. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
  2474. __FUNCTION__, sk, asoc, newsock->sk, retval);
  2475. /* Return the fd mapped to the new socket. */
  2476. peeloff.sd = retval;
  2477. if (copy_to_user(optval, &peeloff, len))
  2478. retval = -EFAULT;
  2479. out:
  2480. return retval;
  2481. }
  2482. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  2483. *
  2484. * Applications can enable or disable heartbeats for any peer address of
  2485. * an association, modify an address's heartbeat interval, force a
  2486. * heartbeat to be sent immediately, and adjust the address's maximum
  2487. * number of retransmissions sent before an address is considered
  2488. * unreachable. The following structure is used to access and modify an
  2489. * address's parameters:
  2490. *
  2491. * struct sctp_paddrparams {
  2492. * sctp_assoc_t spp_assoc_id;
  2493. * struct sockaddr_storage spp_address;
  2494. * uint32_t spp_hbinterval;
  2495. * uint16_t spp_pathmaxrxt;
  2496. * };
  2497. *
  2498. * spp_assoc_id - (UDP style socket) This is filled in the application,
  2499. * and identifies the association for this query.
  2500. * spp_address - This specifies which address is of interest.
  2501. * spp_hbinterval - This contains the value of the heartbeat interval,
  2502. * in milliseconds. A value of 0, when modifying the
  2503. * parameter, specifies that the heartbeat on this
  2504. * address should be disabled. A value of UINT32_MAX
  2505. * (4294967295), when modifying the parameter,
  2506. * specifies that a heartbeat should be sent
  2507. * immediately to the peer address, and the current
  2508. * interval should remain unchanged.
  2509. * spp_pathmaxrxt - This contains the maximum number of
  2510. * retransmissions before this address shall be
  2511. * considered unreachable.
  2512. */
  2513. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  2514. char __user *optval, int __user *optlen)
  2515. {
  2516. struct sctp_paddrparams params;
  2517. struct sctp_transport *trans;
  2518. if (len != sizeof(struct sctp_paddrparams))
  2519. return -EINVAL;
  2520. if (copy_from_user(&params, optval, len))
  2521. return -EFAULT;
  2522. /* If no association id is specified retrieve the default value
  2523. * for the endpoint that will be used for all future associations
  2524. */
  2525. if (!params.spp_assoc_id &&
  2526. sctp_is_any(( union sctp_addr *)&params.spp_address)) {
  2527. params.spp_hbinterval = sctp_sk(sk)->paddrparam.spp_hbinterval;
  2528. params.spp_pathmaxrxt = sctp_sk(sk)->paddrparam.spp_pathmaxrxt;
  2529. goto done;
  2530. }
  2531. trans = sctp_addr_id2transport(sk, &params.spp_address,
  2532. params.spp_assoc_id);
  2533. if (!trans)
  2534. return -EINVAL;
  2535. /* The value of the heartbeat interval, in milliseconds. A value of 0,
  2536. * when modifying the parameter, specifies that the heartbeat on this
  2537. * address should be disabled.
  2538. */
  2539. if (!trans->hb_allowed)
  2540. params.spp_hbinterval = 0;
  2541. else
  2542. params.spp_hbinterval = jiffies_to_msecs(trans->hb_interval);
  2543. /* spp_pathmaxrxt contains the maximum number of retransmissions
  2544. * before this address shall be considered unreachable.
  2545. */
  2546. params.spp_pathmaxrxt = trans->max_retrans;
  2547. done:
  2548. if (copy_to_user(optval, &params, len))
  2549. return -EFAULT;
  2550. if (put_user(len, optlen))
  2551. return -EFAULT;
  2552. return 0;
  2553. }
  2554. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  2555. *
  2556. * Applications can specify protocol parameters for the default association
  2557. * initialization. The option name argument to setsockopt() and getsockopt()
  2558. * is SCTP_INITMSG.
  2559. *
  2560. * Setting initialization parameters is effective only on an unconnected
  2561. * socket (for UDP-style sockets only future associations are effected
  2562. * by the change). With TCP-style sockets, this option is inherited by
  2563. * sockets derived from a listener socket.
  2564. */
  2565. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2566. {
  2567. if (len != sizeof(struct sctp_initmsg))
  2568. return -EINVAL;
  2569. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  2570. return -EFAULT;
  2571. return 0;
  2572. }
  2573. static int sctp_getsockopt_peer_addrs_num(struct sock *sk, int len,
  2574. char __user *optval, int __user *optlen)
  2575. {
  2576. sctp_assoc_t id;
  2577. struct sctp_association *asoc;
  2578. struct list_head *pos;
  2579. int cnt = 0;
  2580. if (len != sizeof(sctp_assoc_t))
  2581. return -EINVAL;
  2582. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  2583. return -EFAULT;
  2584. /* For UDP-style sockets, id specifies the association to query. */
  2585. asoc = sctp_id2assoc(sk, id);
  2586. if (!asoc)
  2587. return -EINVAL;
  2588. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2589. cnt ++;
  2590. }
  2591. return cnt;
  2592. }
  2593. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  2594. char __user *optval, int __user *optlen)
  2595. {
  2596. struct sctp_association *asoc;
  2597. struct list_head *pos;
  2598. int cnt = 0;
  2599. struct sctp_getaddrs getaddrs;
  2600. struct sctp_transport *from;
  2601. void __user *to;
  2602. union sctp_addr temp;
  2603. struct sctp_sock *sp = sctp_sk(sk);
  2604. int addrlen;
  2605. if (len != sizeof(struct sctp_getaddrs))
  2606. return -EINVAL;
  2607. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  2608. return -EFAULT;
  2609. if (getaddrs.addr_num <= 0) return -EINVAL;
  2610. /* For UDP-style sockets, id specifies the association to query. */
  2611. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  2612. if (!asoc)
  2613. return -EINVAL;
  2614. to = (void __user *)getaddrs.addrs;
  2615. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2616. from = list_entry(pos, struct sctp_transport, transports);
  2617. memcpy(&temp, &from->ipaddr, sizeof(temp));
  2618. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  2619. addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
  2620. temp.v4.sin_port = htons(temp.v4.sin_port);
  2621. if (copy_to_user(to, &temp, addrlen))
  2622. return -EFAULT;
  2623. to += addrlen ;
  2624. cnt ++;
  2625. if (cnt >= getaddrs.addr_num) break;
  2626. }
  2627. getaddrs.addr_num = cnt;
  2628. if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
  2629. return -EFAULT;
  2630. return 0;
  2631. }
  2632. static int sctp_getsockopt_local_addrs_num(struct sock *sk, int len,
  2633. char __user *optval,
  2634. int __user *optlen)
  2635. {
  2636. sctp_assoc_t id;
  2637. struct sctp_bind_addr *bp;
  2638. struct sctp_association *asoc;
  2639. struct list_head *pos;
  2640. struct sctp_sockaddr_entry *addr;
  2641. rwlock_t *addr_lock;
  2642. unsigned long flags;
  2643. int cnt = 0;
  2644. if (len != sizeof(sctp_assoc_t))
  2645. return -EINVAL;
  2646. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  2647. return -EFAULT;
  2648. /*
  2649. * For UDP-style sockets, id specifies the association to query.
  2650. * If the id field is set to the value '0' then the locally bound
  2651. * addresses are returned without regard to any particular
  2652. * association.
  2653. */
  2654. if (0 == id) {
  2655. bp = &sctp_sk(sk)->ep->base.bind_addr;
  2656. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  2657. } else {
  2658. asoc = sctp_id2assoc(sk, id);
  2659. if (!asoc)
  2660. return -EINVAL;
  2661. bp = &asoc->base.bind_addr;
  2662. addr_lock = &asoc->base.addr_lock;
  2663. }
  2664. sctp_read_lock(addr_lock);
  2665. /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
  2666. * addresses from the global local address list.
  2667. */
  2668. if (sctp_list_single_entry(&bp->address_list)) {
  2669. addr = list_entry(bp->address_list.next,
  2670. struct sctp_sockaddr_entry, list);
  2671. if (sctp_is_any(&addr->a)) {
  2672. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  2673. list_for_each(pos, &sctp_local_addr_list) {
  2674. addr = list_entry(pos,
  2675. struct sctp_sockaddr_entry,
  2676. list);
  2677. if ((PF_INET == sk->sk_family) &&
  2678. (AF_INET6 == addr->a.sa.sa_family))
  2679. continue;
  2680. cnt++;
  2681. }
  2682. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  2683. flags);
  2684. } else {
  2685. cnt = 1;
  2686. }
  2687. goto done;
  2688. }
  2689. list_for_each(pos, &bp->address_list) {
  2690. cnt ++;
  2691. }
  2692. done:
  2693. sctp_read_unlock(addr_lock);
  2694. return cnt;
  2695. }
  2696. /* Helper function that copies local addresses to user and returns the number
  2697. * of addresses copied.
  2698. */
  2699. static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port, int max_addrs,
  2700. void __user *to)
  2701. {
  2702. struct list_head *pos;
  2703. struct sctp_sockaddr_entry *addr;
  2704. unsigned long flags;
  2705. union sctp_addr temp;
  2706. int cnt = 0;
  2707. int addrlen;
  2708. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  2709. list_for_each(pos, &sctp_local_addr_list) {
  2710. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  2711. if ((PF_INET == sk->sk_family) &&
  2712. (AF_INET6 == addr->a.sa.sa_family))
  2713. continue;
  2714. memcpy(&temp, &addr->a, sizeof(temp));
  2715. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  2716. &temp);
  2717. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  2718. temp.v4.sin_port = htons(port);
  2719. if (copy_to_user(to, &temp, addrlen)) {
  2720. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  2721. flags);
  2722. return -EFAULT;
  2723. }
  2724. to += addrlen;
  2725. cnt ++;
  2726. if (cnt >= max_addrs) break;
  2727. }
  2728. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
  2729. return cnt;
  2730. }
  2731. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  2732. char __user *optval, int __user *optlen)
  2733. {
  2734. struct sctp_bind_addr *bp;
  2735. struct sctp_association *asoc;
  2736. struct list_head *pos;
  2737. int cnt = 0;
  2738. struct sctp_getaddrs getaddrs;
  2739. struct sctp_sockaddr_entry *addr;
  2740. void __user *to;
  2741. union sctp_addr temp;
  2742. struct sctp_sock *sp = sctp_sk(sk);
  2743. int addrlen;
  2744. rwlock_t *addr_lock;
  2745. int err = 0;
  2746. if (len != sizeof(struct sctp_getaddrs))
  2747. return -EINVAL;
  2748. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  2749. return -EFAULT;
  2750. if (getaddrs.addr_num <= 0) return -EINVAL;
  2751. /*
  2752. * For UDP-style sockets, id specifies the association to query.
  2753. * If the id field is set to the value '0' then the locally bound
  2754. * addresses are returned without regard to any particular
  2755. * association.
  2756. */
  2757. if (0 == getaddrs.assoc_id) {
  2758. bp = &sctp_sk(sk)->ep->base.bind_addr;
  2759. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  2760. } else {
  2761. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  2762. if (!asoc)
  2763. return -EINVAL;
  2764. bp = &asoc->base.bind_addr;
  2765. addr_lock = &asoc->base.addr_lock;
  2766. }
  2767. to = getaddrs.addrs;
  2768. sctp_read_lock(addr_lock);
  2769. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  2770. * addresses from the global local address list.
  2771. */
  2772. if (sctp_list_single_entry(&bp->address_list)) {
  2773. addr = list_entry(bp->address_list.next,
  2774. struct sctp_sockaddr_entry, list);
  2775. if (sctp_is_any(&addr->a)) {
  2776. cnt = sctp_copy_laddrs_to_user(sk, bp->port,
  2777. getaddrs.addr_num, to);
  2778. if (cnt < 0) {
  2779. err = cnt;
  2780. goto unlock;
  2781. }
  2782. goto copy_getaddrs;
  2783. }
  2784. }
  2785. list_for_each(pos, &bp->address_list) {
  2786. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  2787. memcpy(&temp, &addr->a, sizeof(temp));
  2788. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  2789. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  2790. temp.v4.sin_port = htons(temp.v4.sin_port);
  2791. if (copy_to_user(to, &temp, addrlen)) {
  2792. err = -EFAULT;
  2793. goto unlock;
  2794. }
  2795. to += addrlen;
  2796. cnt ++;
  2797. if (cnt >= getaddrs.addr_num) break;
  2798. }
  2799. copy_getaddrs:
  2800. getaddrs.addr_num = cnt;
  2801. if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs)))
  2802. err = -EFAULT;
  2803. unlock:
  2804. sctp_read_unlock(addr_lock);
  2805. return err;
  2806. }
  2807. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  2808. *
  2809. * Requests that the local SCTP stack use the enclosed peer address as
  2810. * the association primary. The enclosed address must be one of the
  2811. * association peer's addresses.
  2812. */
  2813. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  2814. char __user *optval, int __user *optlen)
  2815. {
  2816. struct sctp_prim prim;
  2817. struct sctp_association *asoc;
  2818. struct sctp_sock *sp = sctp_sk(sk);
  2819. if (len != sizeof(struct sctp_prim))
  2820. return -EINVAL;
  2821. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  2822. return -EFAULT;
  2823. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  2824. if (!asoc)
  2825. return -EINVAL;
  2826. if (!asoc->peer.primary_path)
  2827. return -ENOTCONN;
  2828. asoc->peer.primary_path->ipaddr.v4.sin_port =
  2829. htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
  2830. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  2831. sizeof(union sctp_addr));
  2832. asoc->peer.primary_path->ipaddr.v4.sin_port =
  2833. ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
  2834. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
  2835. (union sctp_addr *)&prim.ssp_addr);
  2836. if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
  2837. return -EFAULT;
  2838. return 0;
  2839. }
  2840. /*
  2841. * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
  2842. *
  2843. * Requests that the local endpoint set the specified Adaption Layer
  2844. * Indication parameter for all future INIT and INIT-ACK exchanges.
  2845. */
  2846. static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
  2847. char __user *optval, int __user *optlen)
  2848. {
  2849. __u32 val;
  2850. if (len < sizeof(__u32))
  2851. return -EINVAL;
  2852. len = sizeof(__u32);
  2853. val = sctp_sk(sk)->adaption_ind;
  2854. if (put_user(len, optlen))
  2855. return -EFAULT;
  2856. if (copy_to_user(optval, &val, len))
  2857. return -EFAULT;
  2858. return 0;
  2859. }
  2860. /*
  2861. *
  2862. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  2863. *
  2864. * Applications that wish to use the sendto() system call may wish to
  2865. * specify a default set of parameters that would normally be supplied
  2866. * through the inclusion of ancillary data. This socket option allows
  2867. * such an application to set the default sctp_sndrcvinfo structure.
  2868. * The application that wishes to use this socket option simply passes
  2869. * in to this call the sctp_sndrcvinfo structure defined in Section
  2870. * 5.2.2) The input parameters accepted by this call include
  2871. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  2872. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  2873. * to this call if the caller is using the UDP model.
  2874. *
  2875. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  2876. */
  2877. static int sctp_getsockopt_default_send_param(struct sock *sk,
  2878. int len, char __user *optval,
  2879. int __user *optlen)
  2880. {
  2881. struct sctp_sndrcvinfo info;
  2882. struct sctp_association *asoc;
  2883. struct sctp_sock *sp = sctp_sk(sk);
  2884. if (len != sizeof(struct sctp_sndrcvinfo))
  2885. return -EINVAL;
  2886. if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
  2887. return -EFAULT;
  2888. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  2889. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  2890. return -EINVAL;
  2891. if (asoc) {
  2892. info.sinfo_stream = asoc->default_stream;
  2893. info.sinfo_flags = asoc->default_flags;
  2894. info.sinfo_ppid = asoc->default_ppid;
  2895. info.sinfo_context = asoc->default_context;
  2896. info.sinfo_timetolive = asoc->default_timetolive;
  2897. } else {
  2898. info.sinfo_stream = sp->default_stream;
  2899. info.sinfo_flags = sp->default_flags;
  2900. info.sinfo_ppid = sp->default_ppid;
  2901. info.sinfo_context = sp->default_context;
  2902. info.sinfo_timetolive = sp->default_timetolive;
  2903. }
  2904. if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
  2905. return -EFAULT;
  2906. return 0;
  2907. }
  2908. /*
  2909. *
  2910. * 7.1.5 SCTP_NODELAY
  2911. *
  2912. * Turn on/off any Nagle-like algorithm. This means that packets are
  2913. * generally sent as soon as possible and no unnecessary delays are
  2914. * introduced, at the cost of more packets in the network. Expects an
  2915. * integer boolean flag.
  2916. */
  2917. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  2918. char __user *optval, int __user *optlen)
  2919. {
  2920. int val;
  2921. if (len < sizeof(int))
  2922. return -EINVAL;
  2923. len = sizeof(int);
  2924. val = (sctp_sk(sk)->nodelay == 1);
  2925. if (put_user(len, optlen))
  2926. return -EFAULT;
  2927. if (copy_to_user(optval, &val, len))
  2928. return -EFAULT;
  2929. return 0;
  2930. }
  2931. /*
  2932. *
  2933. * 7.1.1 SCTP_RTOINFO
  2934. *
  2935. * The protocol parameters used to initialize and bound retransmission
  2936. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  2937. * and modify these parameters.
  2938. * All parameters are time values, in milliseconds. A value of 0, when
  2939. * modifying the parameters, indicates that the current value should not
  2940. * be changed.
  2941. *
  2942. */
  2943. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  2944. char __user *optval,
  2945. int __user *optlen) {
  2946. struct sctp_rtoinfo rtoinfo;
  2947. struct sctp_association *asoc;
  2948. if (len != sizeof (struct sctp_rtoinfo))
  2949. return -EINVAL;
  2950. if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
  2951. return -EFAULT;
  2952. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  2953. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  2954. return -EINVAL;
  2955. /* Values corresponding to the specific association. */
  2956. if (asoc) {
  2957. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  2958. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  2959. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  2960. } else {
  2961. /* Values corresponding to the endpoint. */
  2962. struct sctp_sock *sp = sctp_sk(sk);
  2963. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  2964. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  2965. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  2966. }
  2967. if (put_user(len, optlen))
  2968. return -EFAULT;
  2969. if (copy_to_user(optval, &rtoinfo, len))
  2970. return -EFAULT;
  2971. return 0;
  2972. }
  2973. /*
  2974. *
  2975. * 7.1.2 SCTP_ASSOCINFO
  2976. *
  2977. * This option is used to tune the the maximum retransmission attempts
  2978. * of the association.
  2979. * Returns an error if the new association retransmission value is
  2980. * greater than the sum of the retransmission value of the peer.
  2981. * See [SCTP] for more information.
  2982. *
  2983. */
  2984. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  2985. char __user *optval,
  2986. int __user *optlen)
  2987. {
  2988. struct sctp_assocparams assocparams;
  2989. struct sctp_association *asoc;
  2990. struct list_head *pos;
  2991. int cnt = 0;
  2992. if (len != sizeof (struct sctp_assocparams))
  2993. return -EINVAL;
  2994. if (copy_from_user(&assocparams, optval,
  2995. sizeof (struct sctp_assocparams)))
  2996. return -EFAULT;
  2997. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  2998. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  2999. return -EINVAL;
  3000. /* Values correspoinding to the specific association */
  3001. if (asoc) {
  3002. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  3003. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  3004. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  3005. assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
  3006. * 1000) +
  3007. (asoc->cookie_life.tv_usec
  3008. / 1000);
  3009. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3010. cnt ++;
  3011. }
  3012. assocparams.sasoc_number_peer_destinations = cnt;
  3013. } else {
  3014. /* Values corresponding to the endpoint */
  3015. struct sctp_sock *sp = sctp_sk(sk);
  3016. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  3017. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  3018. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  3019. assocparams.sasoc_cookie_life =
  3020. sp->assocparams.sasoc_cookie_life;
  3021. assocparams.sasoc_number_peer_destinations =
  3022. sp->assocparams.
  3023. sasoc_number_peer_destinations;
  3024. }
  3025. if (put_user(len, optlen))
  3026. return -EFAULT;
  3027. if (copy_to_user(optval, &assocparams, len))
  3028. return -EFAULT;
  3029. return 0;
  3030. }
  3031. /*
  3032. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  3033. *
  3034. * This socket option is a boolean flag which turns on or off mapped V4
  3035. * addresses. If this option is turned on and the socket is type
  3036. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  3037. * If this option is turned off, then no mapping will be done of V4
  3038. * addresses and a user will receive both PF_INET6 and PF_INET type
  3039. * addresses on the socket.
  3040. */
  3041. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  3042. char __user *optval, int __user *optlen)
  3043. {
  3044. int val;
  3045. struct sctp_sock *sp = sctp_sk(sk);
  3046. if (len < sizeof(int))
  3047. return -EINVAL;
  3048. len = sizeof(int);
  3049. val = sp->v4mapped;
  3050. if (put_user(len, optlen))
  3051. return -EFAULT;
  3052. if (copy_to_user(optval, &val, len))
  3053. return -EFAULT;
  3054. return 0;
  3055. }
  3056. /*
  3057. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  3058. *
  3059. * This socket option specifies the maximum size to put in any outgoing
  3060. * SCTP chunk. If a message is larger than this size it will be
  3061. * fragmented by SCTP into the specified size. Note that the underlying
  3062. * SCTP implementation may fragment into smaller sized chunks when the
  3063. * PMTU of the underlying association is smaller than the value set by
  3064. * the user.
  3065. */
  3066. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  3067. char __user *optval, int __user *optlen)
  3068. {
  3069. int val;
  3070. if (len < sizeof(int))
  3071. return -EINVAL;
  3072. len = sizeof(int);
  3073. val = sctp_sk(sk)->user_frag;
  3074. if (put_user(len, optlen))
  3075. return -EFAULT;
  3076. if (copy_to_user(optval, &val, len))
  3077. return -EFAULT;
  3078. return 0;
  3079. }
  3080. SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
  3081. char __user *optval, int __user *optlen)
  3082. {
  3083. int retval = 0;
  3084. int len;
  3085. SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p, ...)\n", sk);
  3086. /* I can hardly begin to describe how wrong this is. This is
  3087. * so broken as to be worse than useless. The API draft
  3088. * REALLY is NOT helpful here... I am not convinced that the
  3089. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  3090. * are at all well-founded.
  3091. */
  3092. if (level != SOL_SCTP) {
  3093. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3094. retval = af->getsockopt(sk, level, optname, optval, optlen);
  3095. return retval;
  3096. }
  3097. if (get_user(len, optlen))
  3098. return -EFAULT;
  3099. sctp_lock_sock(sk);
  3100. switch (optname) {
  3101. case SCTP_STATUS:
  3102. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  3103. break;
  3104. case SCTP_DISABLE_FRAGMENTS:
  3105. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  3106. optlen);
  3107. break;
  3108. case SCTP_EVENTS:
  3109. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  3110. break;
  3111. case SCTP_AUTOCLOSE:
  3112. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  3113. break;
  3114. case SCTP_SOCKOPT_PEELOFF:
  3115. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  3116. break;
  3117. case SCTP_PEER_ADDR_PARAMS:
  3118. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  3119. optlen);
  3120. break;
  3121. case SCTP_INITMSG:
  3122. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  3123. break;
  3124. case SCTP_GET_PEER_ADDRS_NUM:
  3125. retval = sctp_getsockopt_peer_addrs_num(sk, len, optval,
  3126. optlen);
  3127. break;
  3128. case SCTP_GET_LOCAL_ADDRS_NUM:
  3129. retval = sctp_getsockopt_local_addrs_num(sk, len, optval,
  3130. optlen);
  3131. break;
  3132. case SCTP_GET_PEER_ADDRS:
  3133. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  3134. optlen);
  3135. break;
  3136. case SCTP_GET_LOCAL_ADDRS:
  3137. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  3138. optlen);
  3139. break;
  3140. case SCTP_DEFAULT_SEND_PARAM:
  3141. retval = sctp_getsockopt_default_send_param(sk, len,
  3142. optval, optlen);
  3143. break;
  3144. case SCTP_PRIMARY_ADDR:
  3145. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  3146. break;
  3147. case SCTP_NODELAY:
  3148. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  3149. break;
  3150. case SCTP_RTOINFO:
  3151. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  3152. break;
  3153. case SCTP_ASSOCINFO:
  3154. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  3155. break;
  3156. case SCTP_I_WANT_MAPPED_V4_ADDR:
  3157. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  3158. break;
  3159. case SCTP_MAXSEG:
  3160. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  3161. break;
  3162. case SCTP_GET_PEER_ADDR_INFO:
  3163. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  3164. optlen);
  3165. break;
  3166. case SCTP_ADAPTION_LAYER:
  3167. retval = sctp_getsockopt_adaption_layer(sk, len, optval,
  3168. optlen);
  3169. break;
  3170. default:
  3171. retval = -ENOPROTOOPT;
  3172. break;
  3173. };
  3174. sctp_release_sock(sk);
  3175. return retval;
  3176. }
  3177. static void sctp_hash(struct sock *sk)
  3178. {
  3179. /* STUB */
  3180. }
  3181. static void sctp_unhash(struct sock *sk)
  3182. {
  3183. /* STUB */
  3184. }
  3185. /* Check if port is acceptable. Possibly find first available port.
  3186. *
  3187. * The port hash table (contained in the 'global' SCTP protocol storage
  3188. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  3189. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  3190. * list (the list number is the port number hashed out, so as you
  3191. * would expect from a hash function, all the ports in a given list have
  3192. * such a number that hashes out to the same list number; you were
  3193. * expecting that, right?); so each list has a set of ports, with a
  3194. * link to the socket (struct sock) that uses it, the port number and
  3195. * a fastreuse flag (FIXME: NPI ipg).
  3196. */
  3197. static struct sctp_bind_bucket *sctp_bucket_create(
  3198. struct sctp_bind_hashbucket *head, unsigned short snum);
  3199. static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  3200. {
  3201. struct sctp_bind_hashbucket *head; /* hash list */
  3202. struct sctp_bind_bucket *pp; /* hash list port iterator */
  3203. unsigned short snum;
  3204. int ret;
  3205. /* NOTE: Remember to put this back to net order. */
  3206. addr->v4.sin_port = ntohs(addr->v4.sin_port);
  3207. snum = addr->v4.sin_port;
  3208. SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
  3209. sctp_local_bh_disable();
  3210. if (snum == 0) {
  3211. /* Search for an available port.
  3212. *
  3213. * 'sctp_port_rover' was the last port assigned, so
  3214. * we start to search from 'sctp_port_rover +
  3215. * 1'. What we do is first check if port 'rover' is
  3216. * already in the hash table; if not, we use that; if
  3217. * it is, we try next.
  3218. */
  3219. int low = sysctl_local_port_range[0];
  3220. int high = sysctl_local_port_range[1];
  3221. int remaining = (high - low) + 1;
  3222. int rover;
  3223. int index;
  3224. sctp_spin_lock(&sctp_port_alloc_lock);
  3225. rover = sctp_port_rover;
  3226. do {
  3227. rover++;
  3228. if ((rover < low) || (rover > high))
  3229. rover = low;
  3230. index = sctp_phashfn(rover);
  3231. head = &sctp_port_hashtable[index];
  3232. sctp_spin_lock(&head->lock);
  3233. for (pp = head->chain; pp; pp = pp->next)
  3234. if (pp->port == rover)
  3235. goto next;
  3236. break;
  3237. next:
  3238. sctp_spin_unlock(&head->lock);
  3239. } while (--remaining > 0);
  3240. sctp_port_rover = rover;
  3241. sctp_spin_unlock(&sctp_port_alloc_lock);
  3242. /* Exhausted local port range during search? */
  3243. ret = 1;
  3244. if (remaining <= 0)
  3245. goto fail;
  3246. /* OK, here is the one we will use. HEAD (the port
  3247. * hash table list entry) is non-NULL and we hold it's
  3248. * mutex.
  3249. */
  3250. snum = rover;
  3251. } else {
  3252. /* We are given an specific port number; we verify
  3253. * that it is not being used. If it is used, we will
  3254. * exahust the search in the hash list corresponding
  3255. * to the port number (snum) - we detect that with the
  3256. * port iterator, pp being NULL.
  3257. */
  3258. head = &sctp_port_hashtable[sctp_phashfn(snum)];
  3259. sctp_spin_lock(&head->lock);
  3260. for (pp = head->chain; pp; pp = pp->next) {
  3261. if (pp->port == snum)
  3262. goto pp_found;
  3263. }
  3264. }
  3265. pp = NULL;
  3266. goto pp_not_found;
  3267. pp_found:
  3268. if (!hlist_empty(&pp->owner)) {
  3269. /* We had a port hash table hit - there is an
  3270. * available port (pp != NULL) and it is being
  3271. * used by other socket (pp->owner not empty); that other
  3272. * socket is going to be sk2.
  3273. */
  3274. int reuse = sk->sk_reuse;
  3275. struct sock *sk2;
  3276. struct hlist_node *node;
  3277. SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
  3278. if (pp->fastreuse && sk->sk_reuse)
  3279. goto success;
  3280. /* Run through the list of sockets bound to the port
  3281. * (pp->port) [via the pointers bind_next and
  3282. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  3283. * we get the endpoint they describe and run through
  3284. * the endpoint's list of IP (v4 or v6) addresses,
  3285. * comparing each of the addresses with the address of
  3286. * the socket sk. If we find a match, then that means
  3287. * that this port/socket (sk) combination are already
  3288. * in an endpoint.
  3289. */
  3290. sk_for_each_bound(sk2, node, &pp->owner) {
  3291. struct sctp_endpoint *ep2;
  3292. ep2 = sctp_sk(sk2)->ep;
  3293. if (reuse && sk2->sk_reuse)
  3294. continue;
  3295. if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
  3296. sctp_sk(sk))) {
  3297. ret = (long)sk2;
  3298. goto fail_unlock;
  3299. }
  3300. }
  3301. SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
  3302. }
  3303. pp_not_found:
  3304. /* If there was a hash table miss, create a new port. */
  3305. ret = 1;
  3306. if (!pp && !(pp = sctp_bucket_create(head, snum)))
  3307. goto fail_unlock;
  3308. /* In either case (hit or miss), make sure fastreuse is 1 only
  3309. * if sk->sk_reuse is too (that is, if the caller requested
  3310. * SO_REUSEADDR on this socket -sk-).
  3311. */
  3312. if (hlist_empty(&pp->owner))
  3313. pp->fastreuse = sk->sk_reuse ? 1 : 0;
  3314. else if (pp->fastreuse && !sk->sk_reuse)
  3315. pp->fastreuse = 0;
  3316. /* We are set, so fill up all the data in the hash table
  3317. * entry, tie the socket list information with the rest of the
  3318. * sockets FIXME: Blurry, NPI (ipg).
  3319. */
  3320. success:
  3321. inet_sk(sk)->num = snum;
  3322. if (!sctp_sk(sk)->bind_hash) {
  3323. sk_add_bind_node(sk, &pp->owner);
  3324. sctp_sk(sk)->bind_hash = pp;
  3325. }
  3326. ret = 0;
  3327. fail_unlock:
  3328. sctp_spin_unlock(&head->lock);
  3329. fail:
  3330. sctp_local_bh_enable();
  3331. addr->v4.sin_port = htons(addr->v4.sin_port);
  3332. return ret;
  3333. }
  3334. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  3335. * port is requested.
  3336. */
  3337. static int sctp_get_port(struct sock *sk, unsigned short snum)
  3338. {
  3339. long ret;
  3340. union sctp_addr addr;
  3341. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3342. /* Set up a dummy address struct from the sk. */
  3343. af->from_sk(&addr, sk);
  3344. addr.v4.sin_port = htons(snum);
  3345. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  3346. ret = sctp_get_port_local(sk, &addr);
  3347. return (ret ? 1 : 0);
  3348. }
  3349. /*
  3350. * 3.1.3 listen() - UDP Style Syntax
  3351. *
  3352. * By default, new associations are not accepted for UDP style sockets.
  3353. * An application uses listen() to mark a socket as being able to
  3354. * accept new associations.
  3355. */
  3356. SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
  3357. {
  3358. struct sctp_sock *sp = sctp_sk(sk);
  3359. struct sctp_endpoint *ep = sp->ep;
  3360. /* Only UDP style sockets that are not peeled off are allowed to
  3361. * listen().
  3362. */
  3363. if (!sctp_style(sk, UDP))
  3364. return -EINVAL;
  3365. /* If backlog is zero, disable listening. */
  3366. if (!backlog) {
  3367. if (sctp_sstate(sk, CLOSED))
  3368. return 0;
  3369. sctp_unhash_endpoint(ep);
  3370. sk->sk_state = SCTP_SS_CLOSED;
  3371. }
  3372. /* Return if we are already listening. */
  3373. if (sctp_sstate(sk, LISTENING))
  3374. return 0;
  3375. /*
  3376. * If a bind() or sctp_bindx() is not called prior to a listen()
  3377. * call that allows new associations to be accepted, the system
  3378. * picks an ephemeral port and will choose an address set equivalent
  3379. * to binding with a wildcard address.
  3380. *
  3381. * This is not currently spelled out in the SCTP sockets
  3382. * extensions draft, but follows the practice as seen in TCP
  3383. * sockets.
  3384. */
  3385. if (!ep->base.bind_addr.port) {
  3386. if (sctp_autobind(sk))
  3387. return -EAGAIN;
  3388. }
  3389. sk->sk_state = SCTP_SS_LISTENING;
  3390. sctp_hash_endpoint(ep);
  3391. return 0;
  3392. }
  3393. /*
  3394. * 4.1.3 listen() - TCP Style Syntax
  3395. *
  3396. * Applications uses listen() to ready the SCTP endpoint for accepting
  3397. * inbound associations.
  3398. */
  3399. SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
  3400. {
  3401. struct sctp_sock *sp = sctp_sk(sk);
  3402. struct sctp_endpoint *ep = sp->ep;
  3403. /* If backlog is zero, disable listening. */
  3404. if (!backlog) {
  3405. if (sctp_sstate(sk, CLOSED))
  3406. return 0;
  3407. sctp_unhash_endpoint(ep);
  3408. sk->sk_state = SCTP_SS_CLOSED;
  3409. }
  3410. if (sctp_sstate(sk, LISTENING))
  3411. return 0;
  3412. /*
  3413. * If a bind() or sctp_bindx() is not called prior to a listen()
  3414. * call that allows new associations to be accepted, the system
  3415. * picks an ephemeral port and will choose an address set equivalent
  3416. * to binding with a wildcard address.
  3417. *
  3418. * This is not currently spelled out in the SCTP sockets
  3419. * extensions draft, but follows the practice as seen in TCP
  3420. * sockets.
  3421. */
  3422. if (!ep->base.bind_addr.port) {
  3423. if (sctp_autobind(sk))
  3424. return -EAGAIN;
  3425. }
  3426. sk->sk_state = SCTP_SS_LISTENING;
  3427. sk->sk_max_ack_backlog = backlog;
  3428. sctp_hash_endpoint(ep);
  3429. return 0;
  3430. }
  3431. /*
  3432. * Move a socket to LISTENING state.
  3433. */
  3434. int sctp_inet_listen(struct socket *sock, int backlog)
  3435. {
  3436. struct sock *sk = sock->sk;
  3437. struct crypto_tfm *tfm=NULL;
  3438. int err = -EINVAL;
  3439. if (unlikely(backlog < 0))
  3440. goto out;
  3441. sctp_lock_sock(sk);
  3442. if (sock->state != SS_UNCONNECTED)
  3443. goto out;
  3444. /* Allocate HMAC for generating cookie. */
  3445. if (sctp_hmac_alg) {
  3446. tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
  3447. if (!tfm) {
  3448. err = -ENOSYS;
  3449. goto out;
  3450. }
  3451. }
  3452. switch (sock->type) {
  3453. case SOCK_SEQPACKET:
  3454. err = sctp_seqpacket_listen(sk, backlog);
  3455. break;
  3456. case SOCK_STREAM:
  3457. err = sctp_stream_listen(sk, backlog);
  3458. break;
  3459. default:
  3460. break;
  3461. };
  3462. if (err)
  3463. goto cleanup;
  3464. /* Store away the transform reference. */
  3465. sctp_sk(sk)->hmac = tfm;
  3466. out:
  3467. sctp_release_sock(sk);
  3468. return err;
  3469. cleanup:
  3470. if (tfm)
  3471. sctp_crypto_free_tfm(tfm);
  3472. goto out;
  3473. }
  3474. /*
  3475. * This function is done by modeling the current datagram_poll() and the
  3476. * tcp_poll(). Note that, based on these implementations, we don't
  3477. * lock the socket in this function, even though it seems that,
  3478. * ideally, locking or some other mechanisms can be used to ensure
  3479. * the integrity of the counters (sndbuf and wmem_queued) used
  3480. * in this place. We assume that we don't need locks either until proven
  3481. * otherwise.
  3482. *
  3483. * Another thing to note is that we include the Async I/O support
  3484. * here, again, by modeling the current TCP/UDP code. We don't have
  3485. * a good way to test with it yet.
  3486. */
  3487. unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  3488. {
  3489. struct sock *sk = sock->sk;
  3490. struct sctp_sock *sp = sctp_sk(sk);
  3491. unsigned int mask;
  3492. poll_wait(file, sk->sk_sleep, wait);
  3493. /* A TCP-style listening socket becomes readable when the accept queue
  3494. * is not empty.
  3495. */
  3496. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  3497. return (!list_empty(&sp->ep->asocs)) ?
  3498. (POLLIN | POLLRDNORM) : 0;
  3499. mask = 0;
  3500. /* Is there any exceptional events? */
  3501. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  3502. mask |= POLLERR;
  3503. if (sk->sk_shutdown == SHUTDOWN_MASK)
  3504. mask |= POLLHUP;
  3505. /* Is it readable? Reconsider this code with TCP-style support. */
  3506. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  3507. (sk->sk_shutdown & RCV_SHUTDOWN))
  3508. mask |= POLLIN | POLLRDNORM;
  3509. /* The association is either gone or not ready. */
  3510. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  3511. return mask;
  3512. /* Is it writable? */
  3513. if (sctp_writeable(sk)) {
  3514. mask |= POLLOUT | POLLWRNORM;
  3515. } else {
  3516. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  3517. /*
  3518. * Since the socket is not locked, the buffer
  3519. * might be made available after the writeable check and
  3520. * before the bit is set. This could cause a lost I/O
  3521. * signal. tcp_poll() has a race breaker for this race
  3522. * condition. Based on their implementation, we put
  3523. * in the following code to cover it as well.
  3524. */
  3525. if (sctp_writeable(sk))
  3526. mask |= POLLOUT | POLLWRNORM;
  3527. }
  3528. return mask;
  3529. }
  3530. /********************************************************************
  3531. * 2nd Level Abstractions
  3532. ********************************************************************/
  3533. static struct sctp_bind_bucket *sctp_bucket_create(
  3534. struct sctp_bind_hashbucket *head, unsigned short snum)
  3535. {
  3536. struct sctp_bind_bucket *pp;
  3537. pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
  3538. SCTP_DBG_OBJCNT_INC(bind_bucket);
  3539. if (pp) {
  3540. pp->port = snum;
  3541. pp->fastreuse = 0;
  3542. INIT_HLIST_HEAD(&pp->owner);
  3543. if ((pp->next = head->chain) != NULL)
  3544. pp->next->pprev = &pp->next;
  3545. head->chain = pp;
  3546. pp->pprev = &head->chain;
  3547. }
  3548. return pp;
  3549. }
  3550. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  3551. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  3552. {
  3553. if (hlist_empty(&pp->owner)) {
  3554. if (pp->next)
  3555. pp->next->pprev = pp->pprev;
  3556. *(pp->pprev) = pp->next;
  3557. kmem_cache_free(sctp_bucket_cachep, pp);
  3558. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  3559. }
  3560. }
  3561. /* Release this socket's reference to a local port. */
  3562. static inline void __sctp_put_port(struct sock *sk)
  3563. {
  3564. struct sctp_bind_hashbucket *head =
  3565. &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
  3566. struct sctp_bind_bucket *pp;
  3567. sctp_spin_lock(&head->lock);
  3568. pp = sctp_sk(sk)->bind_hash;
  3569. __sk_del_bind_node(sk);
  3570. sctp_sk(sk)->bind_hash = NULL;
  3571. inet_sk(sk)->num = 0;
  3572. sctp_bucket_destroy(pp);
  3573. sctp_spin_unlock(&head->lock);
  3574. }
  3575. void sctp_put_port(struct sock *sk)
  3576. {
  3577. sctp_local_bh_disable();
  3578. __sctp_put_port(sk);
  3579. sctp_local_bh_enable();
  3580. }
  3581. /*
  3582. * The system picks an ephemeral port and choose an address set equivalent
  3583. * to binding with a wildcard address.
  3584. * One of those addresses will be the primary address for the association.
  3585. * This automatically enables the multihoming capability of SCTP.
  3586. */
  3587. static int sctp_autobind(struct sock *sk)
  3588. {
  3589. union sctp_addr autoaddr;
  3590. struct sctp_af *af;
  3591. unsigned short port;
  3592. /* Initialize a local sockaddr structure to INADDR_ANY. */
  3593. af = sctp_sk(sk)->pf->af;
  3594. port = htons(inet_sk(sk)->num);
  3595. af->inaddr_any(&autoaddr, port);
  3596. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  3597. }
  3598. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  3599. *
  3600. * From RFC 2292
  3601. * 4.2 The cmsghdr Structure *
  3602. *
  3603. * When ancillary data is sent or received, any number of ancillary data
  3604. * objects can be specified by the msg_control and msg_controllen members of
  3605. * the msghdr structure, because each object is preceded by
  3606. * a cmsghdr structure defining the object's length (the cmsg_len member).
  3607. * Historically Berkeley-derived implementations have passed only one object
  3608. * at a time, but this API allows multiple objects to be
  3609. * passed in a single call to sendmsg() or recvmsg(). The following example
  3610. * shows two ancillary data objects in a control buffer.
  3611. *
  3612. * |<--------------------------- msg_controllen -------------------------->|
  3613. * | |
  3614. *
  3615. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  3616. *
  3617. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  3618. * | | |
  3619. *
  3620. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  3621. *
  3622. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  3623. * | | | | |
  3624. *
  3625. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  3626. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  3627. *
  3628. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  3629. *
  3630. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  3631. * ^
  3632. * |
  3633. *
  3634. * msg_control
  3635. * points here
  3636. */
  3637. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
  3638. sctp_cmsgs_t *cmsgs)
  3639. {
  3640. struct cmsghdr *cmsg;
  3641. for (cmsg = CMSG_FIRSTHDR(msg);
  3642. cmsg != NULL;
  3643. cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
  3644. if (!CMSG_OK(msg, cmsg))
  3645. return -EINVAL;
  3646. /* Should we parse this header or ignore? */
  3647. if (cmsg->cmsg_level != IPPROTO_SCTP)
  3648. continue;
  3649. /* Strictly check lengths following example in SCM code. */
  3650. switch (cmsg->cmsg_type) {
  3651. case SCTP_INIT:
  3652. /* SCTP Socket API Extension
  3653. * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
  3654. *
  3655. * This cmsghdr structure provides information for
  3656. * initializing new SCTP associations with sendmsg().
  3657. * The SCTP_INITMSG socket option uses this same data
  3658. * structure. This structure is not used for
  3659. * recvmsg().
  3660. *
  3661. * cmsg_level cmsg_type cmsg_data[]
  3662. * ------------ ------------ ----------------------
  3663. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  3664. */
  3665. if (cmsg->cmsg_len !=
  3666. CMSG_LEN(sizeof(struct sctp_initmsg)))
  3667. return -EINVAL;
  3668. cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
  3669. break;
  3670. case SCTP_SNDRCV:
  3671. /* SCTP Socket API Extension
  3672. * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
  3673. *
  3674. * This cmsghdr structure specifies SCTP options for
  3675. * sendmsg() and describes SCTP header information
  3676. * about a received message through recvmsg().
  3677. *
  3678. * cmsg_level cmsg_type cmsg_data[]
  3679. * ------------ ------------ ----------------------
  3680. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  3681. */
  3682. if (cmsg->cmsg_len !=
  3683. CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  3684. return -EINVAL;
  3685. cmsgs->info =
  3686. (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
  3687. /* Minimally, validate the sinfo_flags. */
  3688. if (cmsgs->info->sinfo_flags &
  3689. ~(MSG_UNORDERED | MSG_ADDR_OVER |
  3690. MSG_ABORT | MSG_EOF))
  3691. return -EINVAL;
  3692. break;
  3693. default:
  3694. return -EINVAL;
  3695. };
  3696. }
  3697. return 0;
  3698. }
  3699. /*
  3700. * Wait for a packet..
  3701. * Note: This function is the same function as in core/datagram.c
  3702. * with a few modifications to make lksctp work.
  3703. */
  3704. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
  3705. {
  3706. int error;
  3707. DEFINE_WAIT(wait);
  3708. prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  3709. /* Socket errors? */
  3710. error = sock_error(sk);
  3711. if (error)
  3712. goto out;
  3713. if (!skb_queue_empty(&sk->sk_receive_queue))
  3714. goto ready;
  3715. /* Socket shut down? */
  3716. if (sk->sk_shutdown & RCV_SHUTDOWN)
  3717. goto out;
  3718. /* Sequenced packets can come disconnected. If so we report the
  3719. * problem.
  3720. */
  3721. error = -ENOTCONN;
  3722. /* Is there a good reason to think that we may receive some data? */
  3723. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  3724. goto out;
  3725. /* Handle signals. */
  3726. if (signal_pending(current))
  3727. goto interrupted;
  3728. /* Let another process have a go. Since we are going to sleep
  3729. * anyway. Note: This may cause odd behaviors if the message
  3730. * does not fit in the user's buffer, but this seems to be the
  3731. * only way to honor MSG_DONTWAIT realistically.
  3732. */
  3733. sctp_release_sock(sk);
  3734. *timeo_p = schedule_timeout(*timeo_p);
  3735. sctp_lock_sock(sk);
  3736. ready:
  3737. finish_wait(sk->sk_sleep, &wait);
  3738. return 0;
  3739. interrupted:
  3740. error = sock_intr_errno(*timeo_p);
  3741. out:
  3742. finish_wait(sk->sk_sleep, &wait);
  3743. *err = error;
  3744. return error;
  3745. }
  3746. /* Receive a datagram.
  3747. * Note: This is pretty much the same routine as in core/datagram.c
  3748. * with a few changes to make lksctp work.
  3749. */
  3750. static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  3751. int noblock, int *err)
  3752. {
  3753. int error;
  3754. struct sk_buff *skb;
  3755. long timeo;
  3756. /* Caller is allowed not to check sk->sk_err before calling. */
  3757. error = sock_error(sk);
  3758. if (error)
  3759. goto no_packet;
  3760. timeo = sock_rcvtimeo(sk, noblock);
  3761. SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
  3762. timeo, MAX_SCHEDULE_TIMEOUT);
  3763. do {
  3764. /* Again only user level code calls this function,
  3765. * so nothing interrupt level
  3766. * will suddenly eat the receive_queue.
  3767. *
  3768. * Look at current nfs client by the way...
  3769. * However, this function was corrent in any case. 8)
  3770. */
  3771. if (flags & MSG_PEEK) {
  3772. spin_lock_bh(&sk->sk_receive_queue.lock);
  3773. skb = skb_peek(&sk->sk_receive_queue);
  3774. if (skb)
  3775. atomic_inc(&skb->users);
  3776. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3777. } else {
  3778. skb = skb_dequeue(&sk->sk_receive_queue);
  3779. }
  3780. if (skb)
  3781. return skb;
  3782. if (sk->sk_shutdown & RCV_SHUTDOWN)
  3783. break;
  3784. /* User doesn't want to wait. */
  3785. error = -EAGAIN;
  3786. if (!timeo)
  3787. goto no_packet;
  3788. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  3789. return NULL;
  3790. no_packet:
  3791. *err = error;
  3792. return NULL;
  3793. }
  3794. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  3795. static void __sctp_write_space(struct sctp_association *asoc)
  3796. {
  3797. struct sock *sk = asoc->base.sk;
  3798. struct socket *sock = sk->sk_socket;
  3799. if ((sctp_wspace(asoc) > 0) && sock) {
  3800. if (waitqueue_active(&asoc->wait))
  3801. wake_up_interruptible(&asoc->wait);
  3802. if (sctp_writeable(sk)) {
  3803. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  3804. wake_up_interruptible(sk->sk_sleep);
  3805. /* Note that we try to include the Async I/O support
  3806. * here by modeling from the current TCP/UDP code.
  3807. * We have not tested with it yet.
  3808. */
  3809. if (sock->fasync_list &&
  3810. !(sk->sk_shutdown & SEND_SHUTDOWN))
  3811. sock_wake_async(sock, 2, POLL_OUT);
  3812. }
  3813. }
  3814. }
  3815. /* Do accounting for the sndbuf space.
  3816. * Decrement the used sndbuf space of the corresponding association by the
  3817. * data size which was just transmitted(freed).
  3818. */
  3819. static void sctp_wfree(struct sk_buff *skb)
  3820. {
  3821. struct sctp_association *asoc;
  3822. struct sctp_chunk *chunk;
  3823. struct sock *sk;
  3824. /* Get the saved chunk pointer. */
  3825. chunk = *((struct sctp_chunk **)(skb->cb));
  3826. asoc = chunk->asoc;
  3827. sk = asoc->base.sk;
  3828. asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
  3829. sizeof(struct sk_buff) +
  3830. sizeof(struct sctp_chunk);
  3831. sk->sk_wmem_queued -= SCTP_DATA_SNDSIZE(chunk) +
  3832. sizeof(struct sk_buff) +
  3833. sizeof(struct sctp_chunk);
  3834. atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  3835. sock_wfree(skb);
  3836. __sctp_write_space(asoc);
  3837. sctp_association_put(asoc);
  3838. }
  3839. /* Helper function to wait for space in the sndbuf. */
  3840. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  3841. size_t msg_len)
  3842. {
  3843. struct sock *sk = asoc->base.sk;
  3844. int err = 0;
  3845. long current_timeo = *timeo_p;
  3846. DEFINE_WAIT(wait);
  3847. SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
  3848. asoc, (long)(*timeo_p), msg_len);
  3849. /* Increment the association's refcnt. */
  3850. sctp_association_hold(asoc);
  3851. /* Wait on the association specific sndbuf space. */
  3852. for (;;) {
  3853. prepare_to_wait_exclusive(&asoc->wait, &wait,
  3854. TASK_INTERRUPTIBLE);
  3855. if (!*timeo_p)
  3856. goto do_nonblock;
  3857. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  3858. asoc->base.dead)
  3859. goto do_error;
  3860. if (signal_pending(current))
  3861. goto do_interrupted;
  3862. if (msg_len <= sctp_wspace(asoc))
  3863. break;
  3864. /* Let another process have a go. Since we are going
  3865. * to sleep anyway.
  3866. */
  3867. sctp_release_sock(sk);
  3868. current_timeo = schedule_timeout(current_timeo);
  3869. sctp_lock_sock(sk);
  3870. *timeo_p = current_timeo;
  3871. }
  3872. out:
  3873. finish_wait(&asoc->wait, &wait);
  3874. /* Release the association's refcnt. */
  3875. sctp_association_put(asoc);
  3876. return err;
  3877. do_error:
  3878. err = -EPIPE;
  3879. goto out;
  3880. do_interrupted:
  3881. err = sock_intr_errno(*timeo_p);
  3882. goto out;
  3883. do_nonblock:
  3884. err = -EAGAIN;
  3885. goto out;
  3886. }
  3887. /* If socket sndbuf has changed, wake up all per association waiters. */
  3888. void sctp_write_space(struct sock *sk)
  3889. {
  3890. struct sctp_association *asoc;
  3891. struct list_head *pos;
  3892. /* Wake up the tasks in each wait queue. */
  3893. list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
  3894. asoc = list_entry(pos, struct sctp_association, asocs);
  3895. __sctp_write_space(asoc);
  3896. }
  3897. }
  3898. /* Is there any sndbuf space available on the socket?
  3899. *
  3900. * Note that wmem_queued is the sum of the send buffers on all of the
  3901. * associations on the same socket. For a UDP-style socket with
  3902. * multiple associations, it is possible for it to be "unwriteable"
  3903. * prematurely. I assume that this is acceptable because
  3904. * a premature "unwriteable" is better than an accidental "writeable" which
  3905. * would cause an unwanted block under certain circumstances. For the 1-1
  3906. * UDP-style sockets or TCP-style sockets, this code should work.
  3907. * - Daisy
  3908. */
  3909. static int sctp_writeable(struct sock *sk)
  3910. {
  3911. int amt = 0;
  3912. amt = sk->sk_sndbuf - sk->sk_wmem_queued;
  3913. if (amt < 0)
  3914. amt = 0;
  3915. return amt;
  3916. }
  3917. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  3918. * returns immediately with EINPROGRESS.
  3919. */
  3920. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  3921. {
  3922. struct sock *sk = asoc->base.sk;
  3923. int err = 0;
  3924. long current_timeo = *timeo_p;
  3925. DEFINE_WAIT(wait);
  3926. SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
  3927. (long)(*timeo_p));
  3928. /* Increment the association's refcnt. */
  3929. sctp_association_hold(asoc);
  3930. for (;;) {
  3931. prepare_to_wait_exclusive(&asoc->wait, &wait,
  3932. TASK_INTERRUPTIBLE);
  3933. if (!*timeo_p)
  3934. goto do_nonblock;
  3935. if (sk->sk_shutdown & RCV_SHUTDOWN)
  3936. break;
  3937. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  3938. asoc->base.dead)
  3939. goto do_error;
  3940. if (signal_pending(current))
  3941. goto do_interrupted;
  3942. if (sctp_state(asoc, ESTABLISHED))
  3943. break;
  3944. /* Let another process have a go. Since we are going
  3945. * to sleep anyway.
  3946. */
  3947. sctp_release_sock(sk);
  3948. current_timeo = schedule_timeout(current_timeo);
  3949. sctp_lock_sock(sk);
  3950. *timeo_p = current_timeo;
  3951. }
  3952. out:
  3953. finish_wait(&asoc->wait, &wait);
  3954. /* Release the association's refcnt. */
  3955. sctp_association_put(asoc);
  3956. return err;
  3957. do_error:
  3958. if (asoc->counters[SCTP_COUNTER_INIT_ERROR] + 1 >=
  3959. asoc->max_init_attempts)
  3960. err = -ETIMEDOUT;
  3961. else
  3962. err = -ECONNREFUSED;
  3963. goto out;
  3964. do_interrupted:
  3965. err = sock_intr_errno(*timeo_p);
  3966. goto out;
  3967. do_nonblock:
  3968. err = -EINPROGRESS;
  3969. goto out;
  3970. }
  3971. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  3972. {
  3973. struct sctp_endpoint *ep;
  3974. int err = 0;
  3975. DEFINE_WAIT(wait);
  3976. ep = sctp_sk(sk)->ep;
  3977. for (;;) {
  3978. prepare_to_wait_exclusive(sk->sk_sleep, &wait,
  3979. TASK_INTERRUPTIBLE);
  3980. if (list_empty(&ep->asocs)) {
  3981. sctp_release_sock(sk);
  3982. timeo = schedule_timeout(timeo);
  3983. sctp_lock_sock(sk);
  3984. }
  3985. err = -EINVAL;
  3986. if (!sctp_sstate(sk, LISTENING))
  3987. break;
  3988. err = 0;
  3989. if (!list_empty(&ep->asocs))
  3990. break;
  3991. err = sock_intr_errno(timeo);
  3992. if (signal_pending(current))
  3993. break;
  3994. err = -EAGAIN;
  3995. if (!timeo)
  3996. break;
  3997. }
  3998. finish_wait(sk->sk_sleep, &wait);
  3999. return err;
  4000. }
  4001. void sctp_wait_for_close(struct sock *sk, long timeout)
  4002. {
  4003. DEFINE_WAIT(wait);
  4004. do {
  4005. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  4006. if (list_empty(&sctp_sk(sk)->ep->asocs))
  4007. break;
  4008. sctp_release_sock(sk);
  4009. timeout = schedule_timeout(timeout);
  4010. sctp_lock_sock(sk);
  4011. } while (!signal_pending(current) && timeout);
  4012. finish_wait(sk->sk_sleep, &wait);
  4013. }
  4014. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  4015. * and its messages to the newsk.
  4016. */
  4017. static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  4018. struct sctp_association *assoc,
  4019. sctp_socket_type_t type)
  4020. {
  4021. struct sctp_sock *oldsp = sctp_sk(oldsk);
  4022. struct sctp_sock *newsp = sctp_sk(newsk);
  4023. struct sctp_bind_bucket *pp; /* hash list port iterator */
  4024. struct sctp_endpoint *newep = newsp->ep;
  4025. struct sk_buff *skb, *tmp;
  4026. struct sctp_ulpevent *event;
  4027. int flags = 0;
  4028. /* Migrate socket buffer sizes and all the socket level options to the
  4029. * new socket.
  4030. */
  4031. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  4032. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  4033. /* Brute force copy old sctp opt. */
  4034. inet_sk_copy_descendant(newsk, oldsk);
  4035. /* Restore the ep value that was overwritten with the above structure
  4036. * copy.
  4037. */
  4038. newsp->ep = newep;
  4039. newsp->hmac = NULL;
  4040. /* Hook this new socket in to the bind_hash list. */
  4041. pp = sctp_sk(oldsk)->bind_hash;
  4042. sk_add_bind_node(newsk, &pp->owner);
  4043. sctp_sk(newsk)->bind_hash = pp;
  4044. inet_sk(newsk)->num = inet_sk(oldsk)->num;
  4045. /* Copy the bind_addr list from the original endpoint to the new
  4046. * endpoint so that we can handle restarts properly
  4047. */
  4048. if (assoc->peer.ipv4_address)
  4049. flags |= SCTP_ADDR4_PEERSUPP;
  4050. if (assoc->peer.ipv6_address)
  4051. flags |= SCTP_ADDR6_PEERSUPP;
  4052. sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
  4053. &oldsp->ep->base.bind_addr,
  4054. SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
  4055. /* Move any messages in the old socket's receive queue that are for the
  4056. * peeled off association to the new socket's receive queue.
  4057. */
  4058. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  4059. event = sctp_skb2event(skb);
  4060. if (event->asoc == assoc) {
  4061. __skb_unlink(skb, skb->list);
  4062. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  4063. }
  4064. }
  4065. /* Clean up any messages pending delivery due to partial
  4066. * delivery. Three cases:
  4067. * 1) No partial deliver; no work.
  4068. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  4069. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  4070. */
  4071. skb_queue_head_init(&newsp->pd_lobby);
  4072. sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
  4073. if (sctp_sk(oldsk)->pd_mode) {
  4074. struct sk_buff_head *queue;
  4075. /* Decide which queue to move pd_lobby skbs to. */
  4076. if (assoc->ulpq.pd_mode) {
  4077. queue = &newsp->pd_lobby;
  4078. } else
  4079. queue = &newsk->sk_receive_queue;
  4080. /* Walk through the pd_lobby, looking for skbs that
  4081. * need moved to the new socket.
  4082. */
  4083. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  4084. event = sctp_skb2event(skb);
  4085. if (event->asoc == assoc) {
  4086. __skb_unlink(skb, skb->list);
  4087. __skb_queue_tail(queue, skb);
  4088. }
  4089. }
  4090. /* Clear up any skbs waiting for the partial
  4091. * delivery to finish.
  4092. */
  4093. if (assoc->ulpq.pd_mode)
  4094. sctp_clear_pd(oldsk);
  4095. }
  4096. /* Set the type of socket to indicate that it is peeled off from the
  4097. * original UDP-style socket or created with the accept() call on a
  4098. * TCP-style socket..
  4099. */
  4100. newsp->type = type;
  4101. /* Migrate the association to the new socket. */
  4102. sctp_assoc_migrate(assoc, newsk);
  4103. /* If the association on the newsk is already closed before accept()
  4104. * is called, set RCV_SHUTDOWN flag.
  4105. */
  4106. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
  4107. newsk->sk_shutdown |= RCV_SHUTDOWN;
  4108. newsk->sk_state = SCTP_SS_ESTABLISHED;
  4109. }
  4110. /* This proto struct describes the ULP interface for SCTP. */
  4111. struct proto sctp_prot = {
  4112. .name = "SCTP",
  4113. .owner = THIS_MODULE,
  4114. .close = sctp_close,
  4115. .connect = sctp_connect,
  4116. .disconnect = sctp_disconnect,
  4117. .accept = sctp_accept,
  4118. .ioctl = sctp_ioctl,
  4119. .init = sctp_init_sock,
  4120. .destroy = sctp_destroy_sock,
  4121. .shutdown = sctp_shutdown,
  4122. .setsockopt = sctp_setsockopt,
  4123. .getsockopt = sctp_getsockopt,
  4124. .sendmsg = sctp_sendmsg,
  4125. .recvmsg = sctp_recvmsg,
  4126. .bind = sctp_bind,
  4127. .backlog_rcv = sctp_backlog_rcv,
  4128. .hash = sctp_hash,
  4129. .unhash = sctp_unhash,
  4130. .get_port = sctp_get_port,
  4131. .obj_size = sizeof(struct sctp_sock),
  4132. };
  4133. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  4134. struct proto sctpv6_prot = {
  4135. .name = "SCTPv6",
  4136. .owner = THIS_MODULE,
  4137. .close = sctp_close,
  4138. .connect = sctp_connect,
  4139. .disconnect = sctp_disconnect,
  4140. .accept = sctp_accept,
  4141. .ioctl = sctp_ioctl,
  4142. .init = sctp_init_sock,
  4143. .destroy = sctp_destroy_sock,
  4144. .shutdown = sctp_shutdown,
  4145. .setsockopt = sctp_setsockopt,
  4146. .getsockopt = sctp_getsockopt,
  4147. .sendmsg = sctp_sendmsg,
  4148. .recvmsg = sctp_recvmsg,
  4149. .bind = sctp_bind,
  4150. .backlog_rcv = sctp_backlog_rcv,
  4151. .hash = sctp_hash,
  4152. .unhash = sctp_unhash,
  4153. .get_port = sctp_get_port,
  4154. .obj_size = sizeof(struct sctp6_sock),
  4155. };
  4156. #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */