socket.c 139 KB

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