socket.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012-2017, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <linux/rhashtable.h>
  37. #include <linux/sched/signal.h>
  38. #include "core.h"
  39. #include "name_table.h"
  40. #include "node.h"
  41. #include "link.h"
  42. #include "name_distr.h"
  43. #include "socket.h"
  44. #include "bcast.h"
  45. #include "netlink.h"
  46. #include "group.h"
  47. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  48. #define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */
  49. #define TIPC_FWD_MSG 1
  50. #define TIPC_MAX_PORT 0xffffffff
  51. #define TIPC_MIN_PORT 1
  52. #define TIPC_ACK_RATE 4 /* ACK at 1/4 of of rcv window size */
  53. enum {
  54. TIPC_LISTEN = TCP_LISTEN,
  55. TIPC_ESTABLISHED = TCP_ESTABLISHED,
  56. TIPC_OPEN = TCP_CLOSE,
  57. TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
  58. TIPC_CONNECTING = TCP_SYN_SENT,
  59. };
  60. struct sockaddr_pair {
  61. struct sockaddr_tipc sock;
  62. struct sockaddr_tipc member;
  63. };
  64. /**
  65. * struct tipc_sock - TIPC socket structure
  66. * @sk: socket - interacts with 'port' and with user via the socket API
  67. * @conn_type: TIPC type used when connection was established
  68. * @conn_instance: TIPC instance used when connection was established
  69. * @published: non-zero if port has one or more associated names
  70. * @max_pkt: maximum packet size "hint" used when building messages sent by port
  71. * @portid: unique port identity in TIPC socket hash table
  72. * @phdr: preformatted message header used when sending messages
  73. * #cong_links: list of congested links
  74. * @publications: list of publications for port
  75. * @blocking_link: address of the congested link we are currently sleeping on
  76. * @pub_count: total # of publications port has made during its lifetime
  77. * @conn_timeout: the time we can wait for an unresponded setup request
  78. * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
  79. * @cong_link_cnt: number of congested links
  80. * @snt_unacked: # messages sent by socket, and not yet acked by peer
  81. * @rcv_unacked: # messages read by user, but not yet acked back to peer
  82. * @peer: 'connected' peer for dgram/rdm
  83. * @node: hash table node
  84. * @mc_method: cookie for use between socket and broadcast layer
  85. * @rcu: rcu struct for tipc_sock
  86. */
  87. struct tipc_sock {
  88. struct sock sk;
  89. u32 conn_type;
  90. u32 conn_instance;
  91. int published;
  92. u32 max_pkt;
  93. u32 portid;
  94. struct tipc_msg phdr;
  95. struct list_head cong_links;
  96. struct list_head publications;
  97. u32 pub_count;
  98. atomic_t dupl_rcvcnt;
  99. u16 conn_timeout;
  100. bool probe_unacked;
  101. u16 cong_link_cnt;
  102. u16 snt_unacked;
  103. u16 snd_win;
  104. u16 peer_caps;
  105. u16 rcv_unacked;
  106. u16 rcv_win;
  107. struct sockaddr_tipc peer;
  108. struct rhash_head node;
  109. struct tipc_mc_method mc_method;
  110. struct rcu_head rcu;
  111. struct tipc_group *group;
  112. bool group_is_open;
  113. };
  114. static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
  115. static void tipc_data_ready(struct sock *sk);
  116. static void tipc_write_space(struct sock *sk);
  117. static void tipc_sock_destruct(struct sock *sk);
  118. static int tipc_release(struct socket *sock);
  119. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
  120. bool kern);
  121. static void tipc_sk_timeout(struct timer_list *t);
  122. static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
  123. struct tipc_name_seq const *seq);
  124. static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
  125. struct tipc_name_seq const *seq);
  126. static int tipc_sk_leave(struct tipc_sock *tsk);
  127. static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
  128. static int tipc_sk_insert(struct tipc_sock *tsk);
  129. static void tipc_sk_remove(struct tipc_sock *tsk);
  130. static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
  131. static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
  132. static const struct proto_ops packet_ops;
  133. static const struct proto_ops stream_ops;
  134. static const struct proto_ops msg_ops;
  135. static struct proto tipc_proto;
  136. static const struct rhashtable_params tsk_rht_params;
  137. static u32 tsk_own_node(struct tipc_sock *tsk)
  138. {
  139. return msg_prevnode(&tsk->phdr);
  140. }
  141. static u32 tsk_peer_node(struct tipc_sock *tsk)
  142. {
  143. return msg_destnode(&tsk->phdr);
  144. }
  145. static u32 tsk_peer_port(struct tipc_sock *tsk)
  146. {
  147. return msg_destport(&tsk->phdr);
  148. }
  149. static bool tsk_unreliable(struct tipc_sock *tsk)
  150. {
  151. return msg_src_droppable(&tsk->phdr) != 0;
  152. }
  153. static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
  154. {
  155. msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
  156. }
  157. static bool tsk_unreturnable(struct tipc_sock *tsk)
  158. {
  159. return msg_dest_droppable(&tsk->phdr) != 0;
  160. }
  161. static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
  162. {
  163. msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
  164. }
  165. static int tsk_importance(struct tipc_sock *tsk)
  166. {
  167. return msg_importance(&tsk->phdr);
  168. }
  169. static int tsk_set_importance(struct tipc_sock *tsk, int imp)
  170. {
  171. if (imp > TIPC_CRITICAL_IMPORTANCE)
  172. return -EINVAL;
  173. msg_set_importance(&tsk->phdr, (u32)imp);
  174. return 0;
  175. }
  176. static struct tipc_sock *tipc_sk(const struct sock *sk)
  177. {
  178. return container_of(sk, struct tipc_sock, sk);
  179. }
  180. static bool tsk_conn_cong(struct tipc_sock *tsk)
  181. {
  182. return tsk->snt_unacked > tsk->snd_win;
  183. }
  184. static u16 tsk_blocks(int len)
  185. {
  186. return ((len / FLOWCTL_BLK_SZ) + 1);
  187. }
  188. /* tsk_blocks(): translate a buffer size in bytes to number of
  189. * advertisable blocks, taking into account the ratio truesize(len)/len
  190. * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
  191. */
  192. static u16 tsk_adv_blocks(int len)
  193. {
  194. return len / FLOWCTL_BLK_SZ / 4;
  195. }
  196. /* tsk_inc(): increment counter for sent or received data
  197. * - If block based flow control is not supported by peer we
  198. * fall back to message based ditto, incrementing the counter
  199. */
  200. static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
  201. {
  202. if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
  203. return ((msglen / FLOWCTL_BLK_SZ) + 1);
  204. return 1;
  205. }
  206. /**
  207. * tsk_advance_rx_queue - discard first buffer in socket receive queue
  208. *
  209. * Caller must hold socket lock
  210. */
  211. static void tsk_advance_rx_queue(struct sock *sk)
  212. {
  213. kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
  214. }
  215. /* tipc_sk_respond() : send response message back to sender
  216. */
  217. static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
  218. {
  219. u32 selector;
  220. u32 dnode;
  221. u32 onode = tipc_own_addr(sock_net(sk));
  222. if (!tipc_msg_reverse(onode, &skb, err))
  223. return;
  224. dnode = msg_destnode(buf_msg(skb));
  225. selector = msg_origport(buf_msg(skb));
  226. tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
  227. }
  228. /**
  229. * tsk_rej_rx_queue - reject all buffers in socket receive queue
  230. *
  231. * Caller must hold socket lock
  232. */
  233. static void tsk_rej_rx_queue(struct sock *sk)
  234. {
  235. struct sk_buff *skb;
  236. while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
  237. tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
  238. }
  239. static bool tipc_sk_connected(struct sock *sk)
  240. {
  241. return sk->sk_state == TIPC_ESTABLISHED;
  242. }
  243. /* tipc_sk_type_connectionless - check if the socket is datagram socket
  244. * @sk: socket
  245. *
  246. * Returns true if connection less, false otherwise
  247. */
  248. static bool tipc_sk_type_connectionless(struct sock *sk)
  249. {
  250. return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
  251. }
  252. /* tsk_peer_msg - verify if message was sent by connected port's peer
  253. *
  254. * Handles cases where the node's network address has changed from
  255. * the default of <0.0.0> to its configured setting.
  256. */
  257. static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
  258. {
  259. struct sock *sk = &tsk->sk;
  260. u32 self = tipc_own_addr(sock_net(sk));
  261. u32 peer_port = tsk_peer_port(tsk);
  262. u32 orig_node, peer_node;
  263. if (unlikely(!tipc_sk_connected(sk)))
  264. return false;
  265. if (unlikely(msg_origport(msg) != peer_port))
  266. return false;
  267. orig_node = msg_orignode(msg);
  268. peer_node = tsk_peer_node(tsk);
  269. if (likely(orig_node == peer_node))
  270. return true;
  271. if (!orig_node && peer_node == self)
  272. return true;
  273. if (!peer_node && orig_node == self)
  274. return true;
  275. return false;
  276. }
  277. /* tipc_set_sk_state - set the sk_state of the socket
  278. * @sk: socket
  279. *
  280. * Caller must hold socket lock
  281. *
  282. * Returns 0 on success, errno otherwise
  283. */
  284. static int tipc_set_sk_state(struct sock *sk, int state)
  285. {
  286. int oldsk_state = sk->sk_state;
  287. int res = -EINVAL;
  288. switch (state) {
  289. case TIPC_OPEN:
  290. res = 0;
  291. break;
  292. case TIPC_LISTEN:
  293. case TIPC_CONNECTING:
  294. if (oldsk_state == TIPC_OPEN)
  295. res = 0;
  296. break;
  297. case TIPC_ESTABLISHED:
  298. if (oldsk_state == TIPC_CONNECTING ||
  299. oldsk_state == TIPC_OPEN)
  300. res = 0;
  301. break;
  302. case TIPC_DISCONNECTING:
  303. if (oldsk_state == TIPC_CONNECTING ||
  304. oldsk_state == TIPC_ESTABLISHED)
  305. res = 0;
  306. break;
  307. }
  308. if (!res)
  309. sk->sk_state = state;
  310. return res;
  311. }
  312. static int tipc_sk_sock_err(struct socket *sock, long *timeout)
  313. {
  314. struct sock *sk = sock->sk;
  315. int err = sock_error(sk);
  316. int typ = sock->type;
  317. if (err)
  318. return err;
  319. if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
  320. if (sk->sk_state == TIPC_DISCONNECTING)
  321. return -EPIPE;
  322. else if (!tipc_sk_connected(sk))
  323. return -ENOTCONN;
  324. }
  325. if (!*timeout)
  326. return -EAGAIN;
  327. if (signal_pending(current))
  328. return sock_intr_errno(*timeout);
  329. return 0;
  330. }
  331. #define tipc_wait_for_cond(sock_, timeo_, condition_) \
  332. ({ \
  333. struct sock *sk_; \
  334. int rc_; \
  335. \
  336. while ((rc_ = !(condition_))) { \
  337. DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
  338. sk_ = (sock_)->sk; \
  339. rc_ = tipc_sk_sock_err((sock_), timeo_); \
  340. if (rc_) \
  341. break; \
  342. prepare_to_wait(sk_sleep(sk_), &wait_, TASK_INTERRUPTIBLE); \
  343. release_sock(sk_); \
  344. *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
  345. sched_annotate_sleep(); \
  346. lock_sock(sk_); \
  347. remove_wait_queue(sk_sleep(sk_), &wait_); \
  348. } \
  349. rc_; \
  350. })
  351. /**
  352. * tipc_sk_create - create a TIPC socket
  353. * @net: network namespace (must be default network)
  354. * @sock: pre-allocated socket structure
  355. * @protocol: protocol indicator (must be 0)
  356. * @kern: caused by kernel or by userspace?
  357. *
  358. * This routine creates additional data structures used by the TIPC socket,
  359. * initializes them, and links them together.
  360. *
  361. * Returns 0 on success, errno otherwise
  362. */
  363. static int tipc_sk_create(struct net *net, struct socket *sock,
  364. int protocol, int kern)
  365. {
  366. const struct proto_ops *ops;
  367. struct sock *sk;
  368. struct tipc_sock *tsk;
  369. struct tipc_msg *msg;
  370. /* Validate arguments */
  371. if (unlikely(protocol != 0))
  372. return -EPROTONOSUPPORT;
  373. switch (sock->type) {
  374. case SOCK_STREAM:
  375. ops = &stream_ops;
  376. break;
  377. case SOCK_SEQPACKET:
  378. ops = &packet_ops;
  379. break;
  380. case SOCK_DGRAM:
  381. case SOCK_RDM:
  382. ops = &msg_ops;
  383. break;
  384. default:
  385. return -EPROTOTYPE;
  386. }
  387. /* Allocate socket's protocol area */
  388. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
  389. if (sk == NULL)
  390. return -ENOMEM;
  391. tsk = tipc_sk(sk);
  392. tsk->max_pkt = MAX_PKT_DEFAULT;
  393. INIT_LIST_HEAD(&tsk->publications);
  394. INIT_LIST_HEAD(&tsk->cong_links);
  395. msg = &tsk->phdr;
  396. /* Finish initializing socket data structures */
  397. sock->ops = ops;
  398. sock_init_data(sock, sk);
  399. tipc_set_sk_state(sk, TIPC_OPEN);
  400. if (tipc_sk_insert(tsk)) {
  401. pr_warn("Socket create failed; port number exhausted\n");
  402. return -EINVAL;
  403. }
  404. /* Ensure tsk is visible before we read own_addr. */
  405. smp_mb();
  406. tipc_msg_init(tipc_own_addr(net), msg, TIPC_LOW_IMPORTANCE,
  407. TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
  408. msg_set_origport(msg, tsk->portid);
  409. timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
  410. sk->sk_shutdown = 0;
  411. sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
  412. sk->sk_rcvbuf = sysctl_tipc_rmem[1];
  413. sk->sk_data_ready = tipc_data_ready;
  414. sk->sk_write_space = tipc_write_space;
  415. sk->sk_destruct = tipc_sock_destruct;
  416. tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
  417. tsk->group_is_open = true;
  418. atomic_set(&tsk->dupl_rcvcnt, 0);
  419. /* Start out with safe limits until we receive an advertised window */
  420. tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
  421. tsk->rcv_win = tsk->snd_win;
  422. if (tipc_sk_type_connectionless(sk)) {
  423. tsk_set_unreturnable(tsk, true);
  424. if (sock->type == SOCK_DGRAM)
  425. tsk_set_unreliable(tsk, true);
  426. }
  427. return 0;
  428. }
  429. static void tipc_sk_callback(struct rcu_head *head)
  430. {
  431. struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
  432. sock_put(&tsk->sk);
  433. }
  434. /* Caller should hold socket lock for the socket. */
  435. static void __tipc_shutdown(struct socket *sock, int error)
  436. {
  437. struct sock *sk = sock->sk;
  438. struct tipc_sock *tsk = tipc_sk(sk);
  439. struct net *net = sock_net(sk);
  440. long timeout = CONN_TIMEOUT_DEFAULT;
  441. u32 dnode = tsk_peer_node(tsk);
  442. struct sk_buff *skb;
  443. /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
  444. tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
  445. !tsk_conn_cong(tsk)));
  446. /* Remove any pending SYN message */
  447. __skb_queue_purge(&sk->sk_write_queue);
  448. /* Reject all unreceived messages, except on an active connection
  449. * (which disconnects locally & sends a 'FIN+' to peer).
  450. */
  451. while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  452. if (TIPC_SKB_CB(skb)->bytes_read) {
  453. kfree_skb(skb);
  454. continue;
  455. }
  456. if (!tipc_sk_type_connectionless(sk) &&
  457. sk->sk_state != TIPC_DISCONNECTING) {
  458. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  459. tipc_node_remove_conn(net, dnode, tsk->portid);
  460. }
  461. tipc_sk_respond(sk, skb, error);
  462. }
  463. if (tipc_sk_type_connectionless(sk))
  464. return;
  465. if (sk->sk_state != TIPC_DISCONNECTING) {
  466. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
  467. TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
  468. tsk_own_node(tsk), tsk_peer_port(tsk),
  469. tsk->portid, error);
  470. if (skb)
  471. tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
  472. tipc_node_remove_conn(net, dnode, tsk->portid);
  473. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  474. }
  475. }
  476. /**
  477. * tipc_release - destroy a TIPC socket
  478. * @sock: socket to destroy
  479. *
  480. * This routine cleans up any messages that are still queued on the socket.
  481. * For DGRAM and RDM socket types, all queued messages are rejected.
  482. * For SEQPACKET and STREAM socket types, the first message is rejected
  483. * and any others are discarded. (If the first message on a STREAM socket
  484. * is partially-read, it is discarded and the next one is rejected instead.)
  485. *
  486. * NOTE: Rejected messages are not necessarily returned to the sender! They
  487. * are returned or discarded according to the "destination droppable" setting
  488. * specified for the message by the sender.
  489. *
  490. * Returns 0 on success, errno otherwise
  491. */
  492. static int tipc_release(struct socket *sock)
  493. {
  494. struct sock *sk = sock->sk;
  495. struct tipc_sock *tsk;
  496. /*
  497. * Exit if socket isn't fully initialized (occurs when a failed accept()
  498. * releases a pre-allocated child socket that was never used)
  499. */
  500. if (sk == NULL)
  501. return 0;
  502. tsk = tipc_sk(sk);
  503. lock_sock(sk);
  504. __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
  505. sk->sk_shutdown = SHUTDOWN_MASK;
  506. tipc_sk_leave(tsk);
  507. tipc_sk_withdraw(tsk, 0, NULL);
  508. sk_stop_timer(sk, &sk->sk_timer);
  509. tipc_sk_remove(tsk);
  510. sock_orphan(sk);
  511. /* Reject any messages that accumulated in backlog queue */
  512. release_sock(sk);
  513. tipc_dest_list_purge(&tsk->cong_links);
  514. tsk->cong_link_cnt = 0;
  515. call_rcu(&tsk->rcu, tipc_sk_callback);
  516. sock->sk = NULL;
  517. return 0;
  518. }
  519. /**
  520. * tipc_bind - associate or disassocate TIPC name(s) with a socket
  521. * @sock: socket structure
  522. * @uaddr: socket address describing name(s) and desired operation
  523. * @uaddr_len: size of socket address data structure
  524. *
  525. * Name and name sequence binding is indicated using a positive scope value;
  526. * a negative scope value unbinds the specified name. Specifying no name
  527. * (i.e. a socket address length of 0) unbinds all names from the socket.
  528. *
  529. * Returns 0 on success, errno otherwise
  530. *
  531. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  532. * access any non-constant socket information.
  533. */
  534. static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
  535. int uaddr_len)
  536. {
  537. struct sock *sk = sock->sk;
  538. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  539. struct tipc_sock *tsk = tipc_sk(sk);
  540. int res = -EINVAL;
  541. lock_sock(sk);
  542. if (unlikely(!uaddr_len)) {
  543. res = tipc_sk_withdraw(tsk, 0, NULL);
  544. goto exit;
  545. }
  546. if (tsk->group) {
  547. res = -EACCES;
  548. goto exit;
  549. }
  550. if (uaddr_len < sizeof(struct sockaddr_tipc)) {
  551. res = -EINVAL;
  552. goto exit;
  553. }
  554. if (addr->family != AF_TIPC) {
  555. res = -EAFNOSUPPORT;
  556. goto exit;
  557. }
  558. if (addr->addrtype == TIPC_ADDR_NAME)
  559. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  560. else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
  561. res = -EAFNOSUPPORT;
  562. goto exit;
  563. }
  564. if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
  565. (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
  566. (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
  567. res = -EACCES;
  568. goto exit;
  569. }
  570. res = (addr->scope >= 0) ?
  571. tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
  572. tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
  573. exit:
  574. release_sock(sk);
  575. return res;
  576. }
  577. /**
  578. * tipc_getname - get port ID of socket or peer socket
  579. * @sock: socket structure
  580. * @uaddr: area for returned socket address
  581. * @uaddr_len: area for returned length of socket address
  582. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  583. *
  584. * Returns 0 on success, errno otherwise
  585. *
  586. * NOTE: This routine doesn't need to take the socket lock since it only
  587. * accesses socket information that is unchanging (or which changes in
  588. * a completely predictable manner).
  589. */
  590. static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
  591. int peer)
  592. {
  593. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  594. struct sock *sk = sock->sk;
  595. struct tipc_sock *tsk = tipc_sk(sk);
  596. memset(addr, 0, sizeof(*addr));
  597. if (peer) {
  598. if ((!tipc_sk_connected(sk)) &&
  599. ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
  600. return -ENOTCONN;
  601. addr->addr.id.ref = tsk_peer_port(tsk);
  602. addr->addr.id.node = tsk_peer_node(tsk);
  603. } else {
  604. addr->addr.id.ref = tsk->portid;
  605. addr->addr.id.node = tipc_own_addr(sock_net(sk));
  606. }
  607. addr->addrtype = TIPC_ADDR_ID;
  608. addr->family = AF_TIPC;
  609. addr->scope = 0;
  610. addr->addr.name.domain = 0;
  611. return sizeof(*addr);
  612. }
  613. /**
  614. * tipc_poll - read and possibly block on pollmask
  615. * @file: file structure associated with the socket
  616. * @sock: socket for which to calculate the poll bits
  617. * @wait: ???
  618. *
  619. * Returns pollmask value
  620. *
  621. * COMMENTARY:
  622. * It appears that the usual socket locking mechanisms are not useful here
  623. * since the pollmask info is potentially out-of-date the moment this routine
  624. * exits. TCP and other protocols seem to rely on higher level poll routines
  625. * to handle any preventable race conditions, so TIPC will do the same ...
  626. *
  627. * IMPORTANT: The fact that a read or write operation is indicated does NOT
  628. * imply that the operation will succeed, merely that it should be performed
  629. * and will not block.
  630. */
  631. static __poll_t tipc_poll(struct file *file, struct socket *sock,
  632. poll_table *wait)
  633. {
  634. struct sock *sk = sock->sk;
  635. struct tipc_sock *tsk = tipc_sk(sk);
  636. __poll_t revents = 0;
  637. sock_poll_wait(file, sock, wait);
  638. if (sk->sk_shutdown & RCV_SHUTDOWN)
  639. revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
  640. if (sk->sk_shutdown == SHUTDOWN_MASK)
  641. revents |= EPOLLHUP;
  642. switch (sk->sk_state) {
  643. case TIPC_ESTABLISHED:
  644. case TIPC_CONNECTING:
  645. if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
  646. revents |= EPOLLOUT;
  647. /* fall thru' */
  648. case TIPC_LISTEN:
  649. if (!skb_queue_empty(&sk->sk_receive_queue))
  650. revents |= EPOLLIN | EPOLLRDNORM;
  651. break;
  652. case TIPC_OPEN:
  653. if (tsk->group_is_open && !tsk->cong_link_cnt)
  654. revents |= EPOLLOUT;
  655. if (!tipc_sk_type_connectionless(sk))
  656. break;
  657. if (skb_queue_empty(&sk->sk_receive_queue))
  658. break;
  659. revents |= EPOLLIN | EPOLLRDNORM;
  660. break;
  661. case TIPC_DISCONNECTING:
  662. revents = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
  663. break;
  664. }
  665. return revents;
  666. }
  667. /**
  668. * tipc_sendmcast - send multicast message
  669. * @sock: socket structure
  670. * @seq: destination address
  671. * @msg: message to send
  672. * @dlen: length of data to send
  673. * @timeout: timeout to wait for wakeup
  674. *
  675. * Called from function tipc_sendmsg(), which has done all sanity checks
  676. * Returns the number of bytes sent on success, or errno
  677. */
  678. static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
  679. struct msghdr *msg, size_t dlen, long timeout)
  680. {
  681. struct sock *sk = sock->sk;
  682. struct tipc_sock *tsk = tipc_sk(sk);
  683. struct tipc_msg *hdr = &tsk->phdr;
  684. struct net *net = sock_net(sk);
  685. int mtu = tipc_bcast_get_mtu(net);
  686. struct tipc_mc_method *method = &tsk->mc_method;
  687. struct sk_buff_head pkts;
  688. struct tipc_nlist dsts;
  689. int rc;
  690. if (tsk->group)
  691. return -EACCES;
  692. /* Block or return if any destination link is congested */
  693. rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
  694. if (unlikely(rc))
  695. return rc;
  696. /* Lookup destination nodes */
  697. tipc_nlist_init(&dsts, tipc_own_addr(net));
  698. tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
  699. seq->upper, &dsts);
  700. if (!dsts.local && !dsts.remote)
  701. return -EHOSTUNREACH;
  702. /* Build message header */
  703. msg_set_type(hdr, TIPC_MCAST_MSG);
  704. msg_set_hdr_sz(hdr, MCAST_H_SIZE);
  705. msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
  706. msg_set_destport(hdr, 0);
  707. msg_set_destnode(hdr, 0);
  708. msg_set_nametype(hdr, seq->type);
  709. msg_set_namelower(hdr, seq->lower);
  710. msg_set_nameupper(hdr, seq->upper);
  711. /* Build message as chain of buffers */
  712. skb_queue_head_init(&pkts);
  713. rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
  714. /* Send message if build was successful */
  715. if (unlikely(rc == dlen))
  716. rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
  717. &tsk->cong_link_cnt);
  718. tipc_nlist_purge(&dsts);
  719. return rc ? rc : dlen;
  720. }
  721. /**
  722. * tipc_send_group_msg - send a message to a member in the group
  723. * @net: network namespace
  724. * @m: message to send
  725. * @mb: group member
  726. * @dnode: destination node
  727. * @dport: destination port
  728. * @dlen: total length of message data
  729. */
  730. static int tipc_send_group_msg(struct net *net, struct tipc_sock *tsk,
  731. struct msghdr *m, struct tipc_member *mb,
  732. u32 dnode, u32 dport, int dlen)
  733. {
  734. u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group);
  735. struct tipc_mc_method *method = &tsk->mc_method;
  736. int blks = tsk_blocks(GROUP_H_SIZE + dlen);
  737. struct tipc_msg *hdr = &tsk->phdr;
  738. struct sk_buff_head pkts;
  739. int mtu, rc;
  740. /* Complete message header */
  741. msg_set_type(hdr, TIPC_GRP_UCAST_MSG);
  742. msg_set_hdr_sz(hdr, GROUP_H_SIZE);
  743. msg_set_destport(hdr, dport);
  744. msg_set_destnode(hdr, dnode);
  745. msg_set_grp_bc_seqno(hdr, bc_snd_nxt);
  746. /* Build message as chain of buffers */
  747. skb_queue_head_init(&pkts);
  748. mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
  749. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  750. if (unlikely(rc != dlen))
  751. return rc;
  752. /* Send message */
  753. rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
  754. if (unlikely(rc == -ELINKCONG)) {
  755. tipc_dest_push(&tsk->cong_links, dnode, 0);
  756. tsk->cong_link_cnt++;
  757. }
  758. /* Update send window */
  759. tipc_group_update_member(mb, blks);
  760. /* A broadcast sent within next EXPIRE period must follow same path */
  761. method->rcast = true;
  762. method->mandatory = true;
  763. return dlen;
  764. }
  765. /**
  766. * tipc_send_group_unicast - send message to a member in the group
  767. * @sock: socket structure
  768. * @m: message to send
  769. * @dlen: total length of message data
  770. * @timeout: timeout to wait for wakeup
  771. *
  772. * Called from function tipc_sendmsg(), which has done all sanity checks
  773. * Returns the number of bytes sent on success, or errno
  774. */
  775. static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
  776. int dlen, long timeout)
  777. {
  778. struct sock *sk = sock->sk;
  779. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  780. int blks = tsk_blocks(GROUP_H_SIZE + dlen);
  781. struct tipc_sock *tsk = tipc_sk(sk);
  782. struct tipc_group *grp = tsk->group;
  783. struct net *net = sock_net(sk);
  784. struct tipc_member *mb = NULL;
  785. u32 node, port;
  786. int rc;
  787. node = dest->addr.id.node;
  788. port = dest->addr.id.ref;
  789. if (!port && !node)
  790. return -EHOSTUNREACH;
  791. /* Block or return if destination link or member is congested */
  792. rc = tipc_wait_for_cond(sock, &timeout,
  793. !tipc_dest_find(&tsk->cong_links, node, 0) &&
  794. !tipc_group_cong(grp, node, port, blks, &mb));
  795. if (unlikely(rc))
  796. return rc;
  797. if (unlikely(!mb))
  798. return -EHOSTUNREACH;
  799. rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen);
  800. return rc ? rc : dlen;
  801. }
  802. /**
  803. * tipc_send_group_anycast - send message to any member with given identity
  804. * @sock: socket structure
  805. * @m: message to send
  806. * @dlen: total length of message data
  807. * @timeout: timeout to wait for wakeup
  808. *
  809. * Called from function tipc_sendmsg(), which has done all sanity checks
  810. * Returns the number of bytes sent on success, or errno
  811. */
  812. static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
  813. int dlen, long timeout)
  814. {
  815. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  816. struct sock *sk = sock->sk;
  817. struct tipc_sock *tsk = tipc_sk(sk);
  818. struct list_head *cong_links = &tsk->cong_links;
  819. int blks = tsk_blocks(GROUP_H_SIZE + dlen);
  820. struct tipc_group *grp = tsk->group;
  821. struct tipc_msg *hdr = &tsk->phdr;
  822. struct tipc_member *first = NULL;
  823. struct tipc_member *mbr = NULL;
  824. struct net *net = sock_net(sk);
  825. u32 node, port, exclude;
  826. struct list_head dsts;
  827. u32 type, inst, scope;
  828. int lookups = 0;
  829. int dstcnt, rc;
  830. bool cong;
  831. INIT_LIST_HEAD(&dsts);
  832. type = msg_nametype(hdr);
  833. inst = dest->addr.name.name.instance;
  834. scope = msg_lookup_scope(hdr);
  835. exclude = tipc_group_exclude(grp);
  836. while (++lookups < 4) {
  837. first = NULL;
  838. /* Look for a non-congested destination member, if any */
  839. while (1) {
  840. if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts,
  841. &dstcnt, exclude, false))
  842. return -EHOSTUNREACH;
  843. tipc_dest_pop(&dsts, &node, &port);
  844. cong = tipc_group_cong(grp, node, port, blks, &mbr);
  845. if (!cong)
  846. break;
  847. if (mbr == first)
  848. break;
  849. if (!first)
  850. first = mbr;
  851. }
  852. /* Start over if destination was not in member list */
  853. if (unlikely(!mbr))
  854. continue;
  855. if (likely(!cong && !tipc_dest_find(cong_links, node, 0)))
  856. break;
  857. /* Block or return if destination link or member is congested */
  858. rc = tipc_wait_for_cond(sock, &timeout,
  859. !tipc_dest_find(cong_links, node, 0) &&
  860. !tipc_group_cong(grp, node, port,
  861. blks, &mbr));
  862. if (unlikely(rc))
  863. return rc;
  864. /* Send, unless destination disappeared while waiting */
  865. if (likely(mbr))
  866. break;
  867. }
  868. if (unlikely(lookups >= 4))
  869. return -EHOSTUNREACH;
  870. rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen);
  871. return rc ? rc : dlen;
  872. }
  873. /**
  874. * tipc_send_group_bcast - send message to all members in communication group
  875. * @sk: socket structure
  876. * @m: message to send
  877. * @dlen: total length of message data
  878. * @timeout: timeout to wait for wakeup
  879. *
  880. * Called from function tipc_sendmsg(), which has done all sanity checks
  881. * Returns the number of bytes sent on success, or errno
  882. */
  883. static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
  884. int dlen, long timeout)
  885. {
  886. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  887. struct sock *sk = sock->sk;
  888. struct net *net = sock_net(sk);
  889. struct tipc_sock *tsk = tipc_sk(sk);
  890. struct tipc_group *grp = tsk->group;
  891. struct tipc_nlist *dsts = tipc_group_dests(grp);
  892. struct tipc_mc_method *method = &tsk->mc_method;
  893. bool ack = method->mandatory && method->rcast;
  894. int blks = tsk_blocks(MCAST_H_SIZE + dlen);
  895. struct tipc_msg *hdr = &tsk->phdr;
  896. int mtu = tipc_bcast_get_mtu(net);
  897. struct sk_buff_head pkts;
  898. int rc = -EHOSTUNREACH;
  899. if (!dsts->local && !dsts->remote)
  900. return -EHOSTUNREACH;
  901. /* Block or return if any destination link or member is congested */
  902. rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt &&
  903. !tipc_group_bc_cong(grp, blks));
  904. if (unlikely(rc))
  905. return rc;
  906. /* Complete message header */
  907. if (dest) {
  908. msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
  909. msg_set_nameinst(hdr, dest->addr.name.name.instance);
  910. } else {
  911. msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
  912. msg_set_nameinst(hdr, 0);
  913. }
  914. msg_set_hdr_sz(hdr, GROUP_H_SIZE);
  915. msg_set_destport(hdr, 0);
  916. msg_set_destnode(hdr, 0);
  917. msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(grp));
  918. /* Avoid getting stuck with repeated forced replicasts */
  919. msg_set_grp_bc_ack_req(hdr, ack);
  920. /* Build message as chain of buffers */
  921. skb_queue_head_init(&pkts);
  922. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  923. if (unlikely(rc != dlen))
  924. return rc;
  925. /* Send message */
  926. rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
  927. if (unlikely(rc))
  928. return rc;
  929. /* Update broadcast sequence number and send windows */
  930. tipc_group_update_bc_members(tsk->group, blks, ack);
  931. /* Broadcast link is now free to choose method for next broadcast */
  932. method->mandatory = false;
  933. method->expires = jiffies;
  934. return dlen;
  935. }
  936. /**
  937. * tipc_send_group_mcast - send message to all members with given identity
  938. * @sock: socket structure
  939. * @m: message to send
  940. * @dlen: total length of message data
  941. * @timeout: timeout to wait for wakeup
  942. *
  943. * Called from function tipc_sendmsg(), which has done all sanity checks
  944. * Returns the number of bytes sent on success, or errno
  945. */
  946. static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
  947. int dlen, long timeout)
  948. {
  949. struct sock *sk = sock->sk;
  950. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  951. struct tipc_sock *tsk = tipc_sk(sk);
  952. struct tipc_group *grp = tsk->group;
  953. struct tipc_msg *hdr = &tsk->phdr;
  954. struct net *net = sock_net(sk);
  955. u32 type, inst, scope, exclude;
  956. struct list_head dsts;
  957. u32 dstcnt;
  958. INIT_LIST_HEAD(&dsts);
  959. type = msg_nametype(hdr);
  960. inst = dest->addr.name.name.instance;
  961. scope = msg_lookup_scope(hdr);
  962. exclude = tipc_group_exclude(grp);
  963. if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts,
  964. &dstcnt, exclude, true))
  965. return -EHOSTUNREACH;
  966. if (dstcnt == 1) {
  967. tipc_dest_pop(&dsts, &dest->addr.id.node, &dest->addr.id.ref);
  968. return tipc_send_group_unicast(sock, m, dlen, timeout);
  969. }
  970. tipc_dest_list_purge(&dsts);
  971. return tipc_send_group_bcast(sock, m, dlen, timeout);
  972. }
  973. /**
  974. * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
  975. * @arrvq: queue with arriving messages, to be cloned after destination lookup
  976. * @inputq: queue with cloned messages, delivered to socket after dest lookup
  977. *
  978. * Multi-threaded: parallel calls with reference to same queues may occur
  979. */
  980. void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
  981. struct sk_buff_head *inputq)
  982. {
  983. u32 self = tipc_own_addr(net);
  984. u32 type, lower, upper, scope;
  985. struct sk_buff *skb, *_skb;
  986. u32 portid, onode;
  987. struct sk_buff_head tmpq;
  988. struct list_head dports;
  989. struct tipc_msg *hdr;
  990. int user, mtyp, hlen;
  991. bool exact;
  992. __skb_queue_head_init(&tmpq);
  993. INIT_LIST_HEAD(&dports);
  994. skb = tipc_skb_peek(arrvq, &inputq->lock);
  995. for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
  996. hdr = buf_msg(skb);
  997. user = msg_user(hdr);
  998. mtyp = msg_type(hdr);
  999. hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
  1000. onode = msg_orignode(hdr);
  1001. type = msg_nametype(hdr);
  1002. if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
  1003. spin_lock_bh(&inputq->lock);
  1004. if (skb_peek(arrvq) == skb) {
  1005. __skb_dequeue(arrvq);
  1006. __skb_queue_tail(inputq, skb);
  1007. }
  1008. kfree_skb(skb);
  1009. spin_unlock_bh(&inputq->lock);
  1010. continue;
  1011. }
  1012. /* Group messages require exact scope match */
  1013. if (msg_in_group(hdr)) {
  1014. lower = 0;
  1015. upper = ~0;
  1016. scope = msg_lookup_scope(hdr);
  1017. exact = true;
  1018. } else {
  1019. /* TIPC_NODE_SCOPE means "any scope" in this context */
  1020. if (onode == self)
  1021. scope = TIPC_NODE_SCOPE;
  1022. else
  1023. scope = TIPC_CLUSTER_SCOPE;
  1024. exact = false;
  1025. lower = msg_namelower(hdr);
  1026. upper = msg_nameupper(hdr);
  1027. }
  1028. /* Create destination port list: */
  1029. tipc_nametbl_mc_lookup(net, type, lower, upper,
  1030. scope, exact, &dports);
  1031. /* Clone message per destination */
  1032. while (tipc_dest_pop(&dports, NULL, &portid)) {
  1033. _skb = __pskb_copy(skb, hlen, GFP_ATOMIC);
  1034. if (_skb) {
  1035. msg_set_destport(buf_msg(_skb), portid);
  1036. __skb_queue_tail(&tmpq, _skb);
  1037. continue;
  1038. }
  1039. pr_warn("Failed to clone mcast rcv buffer\n");
  1040. }
  1041. /* Append to inputq if not already done by other thread */
  1042. spin_lock_bh(&inputq->lock);
  1043. if (skb_peek(arrvq) == skb) {
  1044. skb_queue_splice_tail_init(&tmpq, inputq);
  1045. kfree_skb(__skb_dequeue(arrvq));
  1046. }
  1047. spin_unlock_bh(&inputq->lock);
  1048. __skb_queue_purge(&tmpq);
  1049. kfree_skb(skb);
  1050. }
  1051. tipc_sk_rcv(net, inputq);
  1052. }
  1053. /**
  1054. * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
  1055. * @tsk: receiving socket
  1056. * @skb: pointer to message buffer.
  1057. */
  1058. static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
  1059. struct sk_buff_head *inputq,
  1060. struct sk_buff_head *xmitq)
  1061. {
  1062. struct tipc_msg *hdr = buf_msg(skb);
  1063. u32 onode = tsk_own_node(tsk);
  1064. struct sock *sk = &tsk->sk;
  1065. int mtyp = msg_type(hdr);
  1066. bool conn_cong;
  1067. /* Ignore if connection cannot be validated: */
  1068. if (!tsk_peer_msg(tsk, hdr))
  1069. goto exit;
  1070. if (unlikely(msg_errcode(hdr))) {
  1071. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1072. tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
  1073. tsk_peer_port(tsk));
  1074. sk->sk_state_change(sk);
  1075. /* State change is ignored if socket already awake,
  1076. * - convert msg to abort msg and add to inqueue
  1077. */
  1078. msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
  1079. msg_set_type(hdr, TIPC_CONN_MSG);
  1080. msg_set_size(hdr, BASIC_H_SIZE);
  1081. msg_set_hdr_sz(hdr, BASIC_H_SIZE);
  1082. __skb_queue_tail(inputq, skb);
  1083. return;
  1084. }
  1085. tsk->probe_unacked = false;
  1086. if (mtyp == CONN_PROBE) {
  1087. msg_set_type(hdr, CONN_PROBE_REPLY);
  1088. if (tipc_msg_reverse(onode, &skb, TIPC_OK))
  1089. __skb_queue_tail(xmitq, skb);
  1090. return;
  1091. } else if (mtyp == CONN_ACK) {
  1092. conn_cong = tsk_conn_cong(tsk);
  1093. tsk->snt_unacked -= msg_conn_ack(hdr);
  1094. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
  1095. tsk->snd_win = msg_adv_win(hdr);
  1096. if (conn_cong)
  1097. sk->sk_write_space(sk);
  1098. } else if (mtyp != CONN_PROBE_REPLY) {
  1099. pr_warn("Received unknown CONN_PROTO msg\n");
  1100. }
  1101. exit:
  1102. kfree_skb(skb);
  1103. }
  1104. /**
  1105. * tipc_sendmsg - send message in connectionless manner
  1106. * @sock: socket structure
  1107. * @m: message to send
  1108. * @dsz: amount of user data to be sent
  1109. *
  1110. * Message must have an destination specified explicitly.
  1111. * Used for SOCK_RDM and SOCK_DGRAM messages,
  1112. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  1113. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  1114. *
  1115. * Returns the number of bytes sent on success, or errno otherwise
  1116. */
  1117. static int tipc_sendmsg(struct socket *sock,
  1118. struct msghdr *m, size_t dsz)
  1119. {
  1120. struct sock *sk = sock->sk;
  1121. int ret;
  1122. lock_sock(sk);
  1123. ret = __tipc_sendmsg(sock, m, dsz);
  1124. release_sock(sk);
  1125. return ret;
  1126. }
  1127. static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
  1128. {
  1129. struct sock *sk = sock->sk;
  1130. struct net *net = sock_net(sk);
  1131. struct tipc_sock *tsk = tipc_sk(sk);
  1132. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  1133. long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  1134. struct list_head *clinks = &tsk->cong_links;
  1135. bool syn = !tipc_sk_type_connectionless(sk);
  1136. struct tipc_group *grp = tsk->group;
  1137. struct tipc_msg *hdr = &tsk->phdr;
  1138. struct tipc_name_seq *seq;
  1139. struct sk_buff_head pkts;
  1140. u32 dport, dnode = 0;
  1141. u32 type, inst;
  1142. int mtu, rc;
  1143. if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
  1144. return -EMSGSIZE;
  1145. if (likely(dest)) {
  1146. if (unlikely(m->msg_namelen < sizeof(*dest)))
  1147. return -EINVAL;
  1148. if (unlikely(dest->family != AF_TIPC))
  1149. return -EINVAL;
  1150. }
  1151. if (grp) {
  1152. if (!dest)
  1153. return tipc_send_group_bcast(sock, m, dlen, timeout);
  1154. if (dest->addrtype == TIPC_ADDR_NAME)
  1155. return tipc_send_group_anycast(sock, m, dlen, timeout);
  1156. if (dest->addrtype == TIPC_ADDR_ID)
  1157. return tipc_send_group_unicast(sock, m, dlen, timeout);
  1158. if (dest->addrtype == TIPC_ADDR_MCAST)
  1159. return tipc_send_group_mcast(sock, m, dlen, timeout);
  1160. return -EINVAL;
  1161. }
  1162. if (unlikely(!dest)) {
  1163. dest = &tsk->peer;
  1164. if (!syn || dest->family != AF_TIPC)
  1165. return -EDESTADDRREQ;
  1166. }
  1167. if (unlikely(syn)) {
  1168. if (sk->sk_state == TIPC_LISTEN)
  1169. return -EPIPE;
  1170. if (sk->sk_state != TIPC_OPEN)
  1171. return -EISCONN;
  1172. if (tsk->published)
  1173. return -EOPNOTSUPP;
  1174. if (dest->addrtype == TIPC_ADDR_NAME) {
  1175. tsk->conn_type = dest->addr.name.name.type;
  1176. tsk->conn_instance = dest->addr.name.name.instance;
  1177. }
  1178. msg_set_syn(hdr, 1);
  1179. }
  1180. seq = &dest->addr.nameseq;
  1181. if (dest->addrtype == TIPC_ADDR_MCAST)
  1182. return tipc_sendmcast(sock, seq, m, dlen, timeout);
  1183. if (dest->addrtype == TIPC_ADDR_NAME) {
  1184. type = dest->addr.name.name.type;
  1185. inst = dest->addr.name.name.instance;
  1186. dnode = dest->addr.name.domain;
  1187. msg_set_type(hdr, TIPC_NAMED_MSG);
  1188. msg_set_hdr_sz(hdr, NAMED_H_SIZE);
  1189. msg_set_nametype(hdr, type);
  1190. msg_set_nameinst(hdr, inst);
  1191. msg_set_lookup_scope(hdr, tipc_node2scope(dnode));
  1192. dport = tipc_nametbl_translate(net, type, inst, &dnode);
  1193. msg_set_destnode(hdr, dnode);
  1194. msg_set_destport(hdr, dport);
  1195. if (unlikely(!dport && !dnode))
  1196. return -EHOSTUNREACH;
  1197. } else if (dest->addrtype == TIPC_ADDR_ID) {
  1198. dnode = dest->addr.id.node;
  1199. msg_set_type(hdr, TIPC_DIRECT_MSG);
  1200. msg_set_lookup_scope(hdr, 0);
  1201. msg_set_destnode(hdr, dnode);
  1202. msg_set_destport(hdr, dest->addr.id.ref);
  1203. msg_set_hdr_sz(hdr, BASIC_H_SIZE);
  1204. } else {
  1205. return -EINVAL;
  1206. }
  1207. /* Block or return if destination link is congested */
  1208. rc = tipc_wait_for_cond(sock, &timeout,
  1209. !tipc_dest_find(clinks, dnode, 0));
  1210. if (unlikely(rc))
  1211. return rc;
  1212. skb_queue_head_init(&pkts);
  1213. mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
  1214. rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
  1215. if (unlikely(rc != dlen))
  1216. return rc;
  1217. if (unlikely(syn && !tipc_msg_skb_clone(&pkts, &sk->sk_write_queue)))
  1218. return -ENOMEM;
  1219. rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
  1220. if (unlikely(rc == -ELINKCONG)) {
  1221. tipc_dest_push(clinks, dnode, 0);
  1222. tsk->cong_link_cnt++;
  1223. rc = 0;
  1224. }
  1225. if (unlikely(syn && !rc))
  1226. tipc_set_sk_state(sk, TIPC_CONNECTING);
  1227. return rc ? rc : dlen;
  1228. }
  1229. /**
  1230. * tipc_sendstream - send stream-oriented data
  1231. * @sock: socket structure
  1232. * @m: data to send
  1233. * @dsz: total length of data to be transmitted
  1234. *
  1235. * Used for SOCK_STREAM data.
  1236. *
  1237. * Returns the number of bytes sent on success (or partial success),
  1238. * or errno if no data sent
  1239. */
  1240. static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
  1241. {
  1242. struct sock *sk = sock->sk;
  1243. int ret;
  1244. lock_sock(sk);
  1245. ret = __tipc_sendstream(sock, m, dsz);
  1246. release_sock(sk);
  1247. return ret;
  1248. }
  1249. static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
  1250. {
  1251. struct sock *sk = sock->sk;
  1252. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  1253. long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  1254. struct tipc_sock *tsk = tipc_sk(sk);
  1255. struct tipc_msg *hdr = &tsk->phdr;
  1256. struct net *net = sock_net(sk);
  1257. struct sk_buff_head pkts;
  1258. u32 dnode = tsk_peer_node(tsk);
  1259. int send, sent = 0;
  1260. int rc = 0;
  1261. skb_queue_head_init(&pkts);
  1262. if (unlikely(dlen > INT_MAX))
  1263. return -EMSGSIZE;
  1264. /* Handle implicit connection setup */
  1265. if (unlikely(dest)) {
  1266. rc = __tipc_sendmsg(sock, m, dlen);
  1267. if (dlen && dlen == rc) {
  1268. tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
  1269. tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
  1270. }
  1271. return rc;
  1272. }
  1273. do {
  1274. rc = tipc_wait_for_cond(sock, &timeout,
  1275. (!tsk->cong_link_cnt &&
  1276. !tsk_conn_cong(tsk) &&
  1277. tipc_sk_connected(sk)));
  1278. if (unlikely(rc))
  1279. break;
  1280. send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
  1281. rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
  1282. if (unlikely(rc != send))
  1283. break;
  1284. rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
  1285. if (unlikely(rc == -ELINKCONG)) {
  1286. tsk->cong_link_cnt = 1;
  1287. rc = 0;
  1288. }
  1289. if (likely(!rc)) {
  1290. tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
  1291. sent += send;
  1292. }
  1293. } while (sent < dlen && !rc);
  1294. return sent ? sent : rc;
  1295. }
  1296. /**
  1297. * tipc_send_packet - send a connection-oriented message
  1298. * @sock: socket structure
  1299. * @m: message to send
  1300. * @dsz: length of data to be transmitted
  1301. *
  1302. * Used for SOCK_SEQPACKET messages.
  1303. *
  1304. * Returns the number of bytes sent on success, or errno otherwise
  1305. */
  1306. static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
  1307. {
  1308. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  1309. return -EMSGSIZE;
  1310. return tipc_sendstream(sock, m, dsz);
  1311. }
  1312. /* tipc_sk_finish_conn - complete the setup of a connection
  1313. */
  1314. static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
  1315. u32 peer_node)
  1316. {
  1317. struct sock *sk = &tsk->sk;
  1318. struct net *net = sock_net(sk);
  1319. struct tipc_msg *msg = &tsk->phdr;
  1320. msg_set_syn(msg, 0);
  1321. msg_set_destnode(msg, peer_node);
  1322. msg_set_destport(msg, peer_port);
  1323. msg_set_type(msg, TIPC_CONN_MSG);
  1324. msg_set_lookup_scope(msg, 0);
  1325. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  1326. sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
  1327. tipc_set_sk_state(sk, TIPC_ESTABLISHED);
  1328. tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
  1329. tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
  1330. tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
  1331. __skb_queue_purge(&sk->sk_write_queue);
  1332. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
  1333. return;
  1334. /* Fall back to message based flow control */
  1335. tsk->rcv_win = FLOWCTL_MSG_WIN;
  1336. tsk->snd_win = FLOWCTL_MSG_WIN;
  1337. }
  1338. /**
  1339. * tipc_sk_set_orig_addr - capture sender's address for received message
  1340. * @m: descriptor for message info
  1341. * @hdr: received message header
  1342. *
  1343. * Note: Address is not captured if not requested by receiver.
  1344. */
  1345. static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
  1346. {
  1347. DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
  1348. struct tipc_msg *hdr = buf_msg(skb);
  1349. if (!srcaddr)
  1350. return;
  1351. srcaddr->sock.family = AF_TIPC;
  1352. srcaddr->sock.addrtype = TIPC_ADDR_ID;
  1353. srcaddr->sock.scope = 0;
  1354. srcaddr->sock.addr.id.ref = msg_origport(hdr);
  1355. srcaddr->sock.addr.id.node = msg_orignode(hdr);
  1356. srcaddr->sock.addr.name.domain = 0;
  1357. m->msg_namelen = sizeof(struct sockaddr_tipc);
  1358. if (!msg_in_group(hdr))
  1359. return;
  1360. /* Group message users may also want to know sending member's id */
  1361. srcaddr->member.family = AF_TIPC;
  1362. srcaddr->member.addrtype = TIPC_ADDR_NAME;
  1363. srcaddr->member.scope = 0;
  1364. srcaddr->member.addr.name.name.type = msg_nametype(hdr);
  1365. srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member;
  1366. srcaddr->member.addr.name.domain = 0;
  1367. m->msg_namelen = sizeof(*srcaddr);
  1368. }
  1369. /**
  1370. * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
  1371. * @m: descriptor for message info
  1372. * @msg: received message header
  1373. * @tsk: TIPC port associated with message
  1374. *
  1375. * Note: Ancillary data is not captured if not requested by receiver.
  1376. *
  1377. * Returns 0 if successful, otherwise errno
  1378. */
  1379. static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  1380. struct tipc_sock *tsk)
  1381. {
  1382. u32 anc_data[3];
  1383. u32 err;
  1384. u32 dest_type;
  1385. int has_name;
  1386. int res;
  1387. if (likely(m->msg_controllen == 0))
  1388. return 0;
  1389. /* Optionally capture errored message object(s) */
  1390. err = msg ? msg_errcode(msg) : 0;
  1391. if (unlikely(err)) {
  1392. anc_data[0] = err;
  1393. anc_data[1] = msg_data_sz(msg);
  1394. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  1395. if (res)
  1396. return res;
  1397. if (anc_data[1]) {
  1398. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  1399. msg_data(msg));
  1400. if (res)
  1401. return res;
  1402. }
  1403. }
  1404. /* Optionally capture message destination object */
  1405. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  1406. switch (dest_type) {
  1407. case TIPC_NAMED_MSG:
  1408. has_name = 1;
  1409. anc_data[0] = msg_nametype(msg);
  1410. anc_data[1] = msg_namelower(msg);
  1411. anc_data[2] = msg_namelower(msg);
  1412. break;
  1413. case TIPC_MCAST_MSG:
  1414. has_name = 1;
  1415. anc_data[0] = msg_nametype(msg);
  1416. anc_data[1] = msg_namelower(msg);
  1417. anc_data[2] = msg_nameupper(msg);
  1418. break;
  1419. case TIPC_CONN_MSG:
  1420. has_name = (tsk->conn_type != 0);
  1421. anc_data[0] = tsk->conn_type;
  1422. anc_data[1] = tsk->conn_instance;
  1423. anc_data[2] = tsk->conn_instance;
  1424. break;
  1425. default:
  1426. has_name = 0;
  1427. }
  1428. if (has_name) {
  1429. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  1430. if (res)
  1431. return res;
  1432. }
  1433. return 0;
  1434. }
  1435. static void tipc_sk_send_ack(struct tipc_sock *tsk)
  1436. {
  1437. struct sock *sk = &tsk->sk;
  1438. struct net *net = sock_net(sk);
  1439. struct sk_buff *skb = NULL;
  1440. struct tipc_msg *msg;
  1441. u32 peer_port = tsk_peer_port(tsk);
  1442. u32 dnode = tsk_peer_node(tsk);
  1443. if (!tipc_sk_connected(sk))
  1444. return;
  1445. skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
  1446. dnode, tsk_own_node(tsk), peer_port,
  1447. tsk->portid, TIPC_OK);
  1448. if (!skb)
  1449. return;
  1450. msg = buf_msg(skb);
  1451. msg_set_conn_ack(msg, tsk->rcv_unacked);
  1452. tsk->rcv_unacked = 0;
  1453. /* Adjust to and advertize the correct window limit */
  1454. if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
  1455. tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
  1456. msg_set_adv_win(msg, tsk->rcv_win);
  1457. }
  1458. tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
  1459. }
  1460. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  1461. {
  1462. struct sock *sk = sock->sk;
  1463. DEFINE_WAIT(wait);
  1464. long timeo = *timeop;
  1465. int err = sock_error(sk);
  1466. if (err)
  1467. return err;
  1468. for (;;) {
  1469. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1470. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1471. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1472. err = -ENOTCONN;
  1473. break;
  1474. }
  1475. release_sock(sk);
  1476. timeo = schedule_timeout(timeo);
  1477. lock_sock(sk);
  1478. }
  1479. err = 0;
  1480. if (!skb_queue_empty(&sk->sk_receive_queue))
  1481. break;
  1482. err = -EAGAIN;
  1483. if (!timeo)
  1484. break;
  1485. err = sock_intr_errno(timeo);
  1486. if (signal_pending(current))
  1487. break;
  1488. err = sock_error(sk);
  1489. if (err)
  1490. break;
  1491. }
  1492. finish_wait(sk_sleep(sk), &wait);
  1493. *timeop = timeo;
  1494. return err;
  1495. }
  1496. /**
  1497. * tipc_recvmsg - receive packet-oriented message
  1498. * @m: descriptor for message info
  1499. * @buflen: length of user buffer area
  1500. * @flags: receive flags
  1501. *
  1502. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  1503. * If the complete message doesn't fit in user area, truncate it.
  1504. *
  1505. * Returns size of returned message data, errno otherwise
  1506. */
  1507. static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
  1508. size_t buflen, int flags)
  1509. {
  1510. struct sock *sk = sock->sk;
  1511. bool connected = !tipc_sk_type_connectionless(sk);
  1512. struct tipc_sock *tsk = tipc_sk(sk);
  1513. int rc, err, hlen, dlen, copy;
  1514. struct sk_buff_head xmitq;
  1515. struct tipc_msg *hdr;
  1516. struct sk_buff *skb;
  1517. bool grp_evt;
  1518. long timeout;
  1519. /* Catch invalid receive requests */
  1520. if (unlikely(!buflen))
  1521. return -EINVAL;
  1522. lock_sock(sk);
  1523. if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
  1524. rc = -ENOTCONN;
  1525. goto exit;
  1526. }
  1527. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1528. /* Step rcv queue to first msg with data or error; wait if necessary */
  1529. do {
  1530. rc = tipc_wait_for_rcvmsg(sock, &timeout);
  1531. if (unlikely(rc))
  1532. goto exit;
  1533. skb = skb_peek(&sk->sk_receive_queue);
  1534. hdr = buf_msg(skb);
  1535. dlen = msg_data_sz(hdr);
  1536. hlen = msg_hdr_sz(hdr);
  1537. err = msg_errcode(hdr);
  1538. grp_evt = msg_is_grp_evt(hdr);
  1539. if (likely(dlen || err))
  1540. break;
  1541. tsk_advance_rx_queue(sk);
  1542. } while (1);
  1543. /* Collect msg meta data, including error code and rejected data */
  1544. tipc_sk_set_orig_addr(m, skb);
  1545. rc = tipc_sk_anc_data_recv(m, hdr, tsk);
  1546. if (unlikely(rc))
  1547. goto exit;
  1548. /* Capture data if non-error msg, otherwise just set return value */
  1549. if (likely(!err)) {
  1550. copy = min_t(int, dlen, buflen);
  1551. if (unlikely(copy != dlen))
  1552. m->msg_flags |= MSG_TRUNC;
  1553. rc = skb_copy_datagram_msg(skb, hlen, m, copy);
  1554. } else {
  1555. copy = 0;
  1556. rc = 0;
  1557. if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
  1558. rc = -ECONNRESET;
  1559. }
  1560. if (unlikely(rc))
  1561. goto exit;
  1562. /* Mark message as group event if applicable */
  1563. if (unlikely(grp_evt)) {
  1564. if (msg_grp_evt(hdr) == TIPC_WITHDRAWN)
  1565. m->msg_flags |= MSG_EOR;
  1566. m->msg_flags |= MSG_OOB;
  1567. copy = 0;
  1568. }
  1569. /* Caption of data or error code/rejected data was successful */
  1570. if (unlikely(flags & MSG_PEEK))
  1571. goto exit;
  1572. /* Send group flow control advertisement when applicable */
  1573. if (tsk->group && msg_in_group(hdr) && !grp_evt) {
  1574. skb_queue_head_init(&xmitq);
  1575. tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen),
  1576. msg_orignode(hdr), msg_origport(hdr),
  1577. &xmitq);
  1578. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  1579. }
  1580. tsk_advance_rx_queue(sk);
  1581. if (likely(!connected))
  1582. goto exit;
  1583. /* Send connection flow control advertisement when applicable */
  1584. tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
  1585. if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
  1586. tipc_sk_send_ack(tsk);
  1587. exit:
  1588. release_sock(sk);
  1589. return rc ? rc : copy;
  1590. }
  1591. /**
  1592. * tipc_recvstream - receive stream-oriented data
  1593. * @m: descriptor for message info
  1594. * @buflen: total size of user buffer area
  1595. * @flags: receive flags
  1596. *
  1597. * Used for SOCK_STREAM messages only. If not enough data is available
  1598. * will optionally wait for more; never truncates data.
  1599. *
  1600. * Returns size of returned message data, errno otherwise
  1601. */
  1602. static int tipc_recvstream(struct socket *sock, struct msghdr *m,
  1603. size_t buflen, int flags)
  1604. {
  1605. struct sock *sk = sock->sk;
  1606. struct tipc_sock *tsk = tipc_sk(sk);
  1607. struct sk_buff *skb;
  1608. struct tipc_msg *hdr;
  1609. struct tipc_skb_cb *skb_cb;
  1610. bool peek = flags & MSG_PEEK;
  1611. int offset, required, copy, copied = 0;
  1612. int hlen, dlen, err, rc;
  1613. long timeout;
  1614. /* Catch invalid receive attempts */
  1615. if (unlikely(!buflen))
  1616. return -EINVAL;
  1617. lock_sock(sk);
  1618. if (unlikely(sk->sk_state == TIPC_OPEN)) {
  1619. rc = -ENOTCONN;
  1620. goto exit;
  1621. }
  1622. required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
  1623. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1624. do {
  1625. /* Look at first msg in receive queue; wait if necessary */
  1626. rc = tipc_wait_for_rcvmsg(sock, &timeout);
  1627. if (unlikely(rc))
  1628. break;
  1629. skb = skb_peek(&sk->sk_receive_queue);
  1630. skb_cb = TIPC_SKB_CB(skb);
  1631. hdr = buf_msg(skb);
  1632. dlen = msg_data_sz(hdr);
  1633. hlen = msg_hdr_sz(hdr);
  1634. err = msg_errcode(hdr);
  1635. /* Discard any empty non-errored (SYN-) message */
  1636. if (unlikely(!dlen && !err)) {
  1637. tsk_advance_rx_queue(sk);
  1638. continue;
  1639. }
  1640. /* Collect msg meta data, incl. error code and rejected data */
  1641. if (!copied) {
  1642. tipc_sk_set_orig_addr(m, skb);
  1643. rc = tipc_sk_anc_data_recv(m, hdr, tsk);
  1644. if (rc)
  1645. break;
  1646. }
  1647. /* Copy data if msg ok, otherwise return error/partial data */
  1648. if (likely(!err)) {
  1649. offset = skb_cb->bytes_read;
  1650. copy = min_t(int, dlen - offset, buflen - copied);
  1651. rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
  1652. if (unlikely(rc))
  1653. break;
  1654. copied += copy;
  1655. offset += copy;
  1656. if (unlikely(offset < dlen)) {
  1657. if (!peek)
  1658. skb_cb->bytes_read = offset;
  1659. break;
  1660. }
  1661. } else {
  1662. rc = 0;
  1663. if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
  1664. rc = -ECONNRESET;
  1665. if (copied || rc)
  1666. break;
  1667. }
  1668. if (unlikely(peek))
  1669. break;
  1670. tsk_advance_rx_queue(sk);
  1671. /* Send connection flow control advertisement when applicable */
  1672. tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
  1673. if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
  1674. tipc_sk_send_ack(tsk);
  1675. /* Exit if all requested data or FIN/error received */
  1676. if (copied == buflen || err)
  1677. break;
  1678. } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
  1679. exit:
  1680. release_sock(sk);
  1681. return copied ? copied : rc;
  1682. }
  1683. /**
  1684. * tipc_write_space - wake up thread if port congestion is released
  1685. * @sk: socket
  1686. */
  1687. static void tipc_write_space(struct sock *sk)
  1688. {
  1689. struct socket_wq *wq;
  1690. rcu_read_lock();
  1691. wq = rcu_dereference(sk->sk_wq);
  1692. if (skwq_has_sleeper(wq))
  1693. wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
  1694. EPOLLWRNORM | EPOLLWRBAND);
  1695. rcu_read_unlock();
  1696. }
  1697. /**
  1698. * tipc_data_ready - wake up threads to indicate messages have been received
  1699. * @sk: socket
  1700. * @len: the length of messages
  1701. */
  1702. static void tipc_data_ready(struct sock *sk)
  1703. {
  1704. struct socket_wq *wq;
  1705. rcu_read_lock();
  1706. wq = rcu_dereference(sk->sk_wq);
  1707. if (skwq_has_sleeper(wq))
  1708. wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
  1709. EPOLLRDNORM | EPOLLRDBAND);
  1710. rcu_read_unlock();
  1711. }
  1712. static void tipc_sock_destruct(struct sock *sk)
  1713. {
  1714. __skb_queue_purge(&sk->sk_receive_queue);
  1715. }
  1716. static void tipc_sk_proto_rcv(struct sock *sk,
  1717. struct sk_buff_head *inputq,
  1718. struct sk_buff_head *xmitq)
  1719. {
  1720. struct sk_buff *skb = __skb_dequeue(inputq);
  1721. struct tipc_sock *tsk = tipc_sk(sk);
  1722. struct tipc_msg *hdr = buf_msg(skb);
  1723. struct tipc_group *grp = tsk->group;
  1724. bool wakeup = false;
  1725. switch (msg_user(hdr)) {
  1726. case CONN_MANAGER:
  1727. tipc_sk_conn_proto_rcv(tsk, skb, inputq, xmitq);
  1728. return;
  1729. case SOCK_WAKEUP:
  1730. tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
  1731. tsk->cong_link_cnt--;
  1732. wakeup = true;
  1733. break;
  1734. case GROUP_PROTOCOL:
  1735. tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
  1736. break;
  1737. case TOP_SRV:
  1738. tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
  1739. hdr, inputq, xmitq);
  1740. break;
  1741. default:
  1742. break;
  1743. }
  1744. if (wakeup)
  1745. sk->sk_write_space(sk);
  1746. kfree_skb(skb);
  1747. }
  1748. /**
  1749. * tipc_sk_filter_connect - check incoming message for a connection-based socket
  1750. * @tsk: TIPC socket
  1751. * @skb: pointer to message buffer.
  1752. * Returns true if message should be added to receive queue, false otherwise
  1753. */
  1754. static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
  1755. {
  1756. struct sock *sk = &tsk->sk;
  1757. struct net *net = sock_net(sk);
  1758. struct tipc_msg *hdr = buf_msg(skb);
  1759. bool con_msg = msg_connected(hdr);
  1760. u32 pport = tsk_peer_port(tsk);
  1761. u32 pnode = tsk_peer_node(tsk);
  1762. u32 oport = msg_origport(hdr);
  1763. u32 onode = msg_orignode(hdr);
  1764. int err = msg_errcode(hdr);
  1765. unsigned long delay;
  1766. if (unlikely(msg_mcast(hdr)))
  1767. return false;
  1768. switch (sk->sk_state) {
  1769. case TIPC_CONNECTING:
  1770. /* Setup ACK */
  1771. if (likely(con_msg)) {
  1772. if (err)
  1773. break;
  1774. tipc_sk_finish_conn(tsk, oport, onode);
  1775. msg_set_importance(&tsk->phdr, msg_importance(hdr));
  1776. /* ACK+ message with data is added to receive queue */
  1777. if (msg_data_sz(hdr))
  1778. return true;
  1779. /* Empty ACK-, - wake up sleeping connect() and drop */
  1780. sk->sk_data_ready(sk);
  1781. msg_set_dest_droppable(hdr, 1);
  1782. return false;
  1783. }
  1784. /* Ignore connectionless message if not from listening socket */
  1785. if (oport != pport || onode != pnode)
  1786. return false;
  1787. /* Rejected SYN */
  1788. if (err != TIPC_ERR_OVERLOAD)
  1789. break;
  1790. /* Prepare for new setup attempt if we have a SYN clone */
  1791. if (skb_queue_empty(&sk->sk_write_queue))
  1792. break;
  1793. get_random_bytes(&delay, 2);
  1794. delay %= (tsk->conn_timeout / 4);
  1795. delay = msecs_to_jiffies(delay + 100);
  1796. sk_reset_timer(sk, &sk->sk_timer, jiffies + delay);
  1797. return false;
  1798. case TIPC_OPEN:
  1799. case TIPC_DISCONNECTING:
  1800. return false;
  1801. case TIPC_LISTEN:
  1802. /* Accept only SYN message */
  1803. if (!msg_is_syn(hdr) &&
  1804. tipc_node_get_capabilities(net, onode) & TIPC_SYN_BIT)
  1805. return false;
  1806. if (!con_msg && !err)
  1807. return true;
  1808. return false;
  1809. case TIPC_ESTABLISHED:
  1810. /* Accept only connection-based messages sent by peer */
  1811. if (likely(con_msg && !err && pport == oport && pnode == onode))
  1812. return true;
  1813. if (!tsk_peer_msg(tsk, hdr))
  1814. return false;
  1815. if (!err)
  1816. return true;
  1817. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1818. tipc_node_remove_conn(net, pnode, tsk->portid);
  1819. sk->sk_state_change(sk);
  1820. return true;
  1821. default:
  1822. pr_err("Unknown sk_state %u\n", sk->sk_state);
  1823. }
  1824. /* Abort connection setup attempt */
  1825. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  1826. sk->sk_err = ECONNREFUSED;
  1827. sk->sk_state_change(sk);
  1828. return true;
  1829. }
  1830. /**
  1831. * rcvbuf_limit - get proper overload limit of socket receive queue
  1832. * @sk: socket
  1833. * @skb: message
  1834. *
  1835. * For connection oriented messages, irrespective of importance,
  1836. * default queue limit is 2 MB.
  1837. *
  1838. * For connectionless messages, queue limits are based on message
  1839. * importance as follows:
  1840. *
  1841. * TIPC_LOW_IMPORTANCE (2 MB)
  1842. * TIPC_MEDIUM_IMPORTANCE (4 MB)
  1843. * TIPC_HIGH_IMPORTANCE (8 MB)
  1844. * TIPC_CRITICAL_IMPORTANCE (16 MB)
  1845. *
  1846. * Returns overload limit according to corresponding message importance
  1847. */
  1848. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
  1849. {
  1850. struct tipc_sock *tsk = tipc_sk(sk);
  1851. struct tipc_msg *hdr = buf_msg(skb);
  1852. if (unlikely(msg_in_group(hdr)))
  1853. return sk->sk_rcvbuf;
  1854. if (unlikely(!msg_connected(hdr)))
  1855. return sk->sk_rcvbuf << msg_importance(hdr);
  1856. if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
  1857. return sk->sk_rcvbuf;
  1858. return FLOWCTL_MSG_LIM;
  1859. }
  1860. /**
  1861. * tipc_sk_filter_rcv - validate incoming message
  1862. * @sk: socket
  1863. * @skb: pointer to message.
  1864. *
  1865. * Enqueues message on receive queue if acceptable; optionally handles
  1866. * disconnect indication for a connected socket.
  1867. *
  1868. * Called with socket lock already taken
  1869. *
  1870. */
  1871. static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
  1872. struct sk_buff_head *xmitq)
  1873. {
  1874. bool sk_conn = !tipc_sk_type_connectionless(sk);
  1875. struct tipc_sock *tsk = tipc_sk(sk);
  1876. struct tipc_group *grp = tsk->group;
  1877. struct tipc_msg *hdr = buf_msg(skb);
  1878. struct net *net = sock_net(sk);
  1879. struct sk_buff_head inputq;
  1880. int limit, err = TIPC_OK;
  1881. TIPC_SKB_CB(skb)->bytes_read = 0;
  1882. __skb_queue_head_init(&inputq);
  1883. __skb_queue_tail(&inputq, skb);
  1884. if (unlikely(!msg_isdata(hdr)))
  1885. tipc_sk_proto_rcv(sk, &inputq, xmitq);
  1886. if (unlikely(grp))
  1887. tipc_group_filter_msg(grp, &inputq, xmitq);
  1888. /* Validate and add to receive buffer if there is space */
  1889. while ((skb = __skb_dequeue(&inputq))) {
  1890. hdr = buf_msg(skb);
  1891. limit = rcvbuf_limit(sk, skb);
  1892. if ((sk_conn && !tipc_sk_filter_connect(tsk, skb)) ||
  1893. (!sk_conn && msg_connected(hdr)) ||
  1894. (!grp && msg_in_group(hdr)))
  1895. err = TIPC_ERR_NO_PORT;
  1896. else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) {
  1897. atomic_inc(&sk->sk_drops);
  1898. err = TIPC_ERR_OVERLOAD;
  1899. }
  1900. if (unlikely(err)) {
  1901. tipc_skb_reject(net, err, skb, xmitq);
  1902. err = TIPC_OK;
  1903. continue;
  1904. }
  1905. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1906. skb_set_owner_r(skb, sk);
  1907. sk->sk_data_ready(sk);
  1908. }
  1909. }
  1910. /**
  1911. * tipc_sk_backlog_rcv - handle incoming message from backlog queue
  1912. * @sk: socket
  1913. * @skb: message
  1914. *
  1915. * Caller must hold socket lock
  1916. */
  1917. static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  1918. {
  1919. unsigned int before = sk_rmem_alloc_get(sk);
  1920. struct sk_buff_head xmitq;
  1921. unsigned int added;
  1922. __skb_queue_head_init(&xmitq);
  1923. tipc_sk_filter_rcv(sk, skb, &xmitq);
  1924. added = sk_rmem_alloc_get(sk) - before;
  1925. atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
  1926. /* Send pending response/rejected messages, if any */
  1927. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  1928. return 0;
  1929. }
  1930. /**
  1931. * tipc_sk_enqueue - extract all buffers with destination 'dport' from
  1932. * inputq and try adding them to socket or backlog queue
  1933. * @inputq: list of incoming buffers with potentially different destinations
  1934. * @sk: socket where the buffers should be enqueued
  1935. * @dport: port number for the socket
  1936. *
  1937. * Caller must hold socket lock
  1938. */
  1939. static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
  1940. u32 dport, struct sk_buff_head *xmitq)
  1941. {
  1942. unsigned long time_limit = jiffies + 2;
  1943. struct sk_buff *skb;
  1944. unsigned int lim;
  1945. atomic_t *dcnt;
  1946. u32 onode;
  1947. while (skb_queue_len(inputq)) {
  1948. if (unlikely(time_after_eq(jiffies, time_limit)))
  1949. return;
  1950. skb = tipc_skb_dequeue(inputq, dport);
  1951. if (unlikely(!skb))
  1952. return;
  1953. /* Add message directly to receive queue if possible */
  1954. if (!sock_owned_by_user(sk)) {
  1955. tipc_sk_filter_rcv(sk, skb, xmitq);
  1956. continue;
  1957. }
  1958. /* Try backlog, compensating for double-counted bytes */
  1959. dcnt = &tipc_sk(sk)->dupl_rcvcnt;
  1960. if (!sk->sk_backlog.len)
  1961. atomic_set(dcnt, 0);
  1962. lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
  1963. if (likely(!sk_add_backlog(sk, skb, lim)))
  1964. continue;
  1965. /* Overload => reject message back to sender */
  1966. onode = tipc_own_addr(sock_net(sk));
  1967. atomic_inc(&sk->sk_drops);
  1968. if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
  1969. __skb_queue_tail(xmitq, skb);
  1970. break;
  1971. }
  1972. }
  1973. /**
  1974. * tipc_sk_rcv - handle a chain of incoming buffers
  1975. * @inputq: buffer list containing the buffers
  1976. * Consumes all buffers in list until inputq is empty
  1977. * Note: may be called in multiple threads referring to the same queue
  1978. */
  1979. void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
  1980. {
  1981. struct sk_buff_head xmitq;
  1982. u32 dnode, dport = 0;
  1983. int err;
  1984. struct tipc_sock *tsk;
  1985. struct sock *sk;
  1986. struct sk_buff *skb;
  1987. __skb_queue_head_init(&xmitq);
  1988. while (skb_queue_len(inputq)) {
  1989. dport = tipc_skb_peek_port(inputq, dport);
  1990. tsk = tipc_sk_lookup(net, dport);
  1991. if (likely(tsk)) {
  1992. sk = &tsk->sk;
  1993. if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
  1994. tipc_sk_enqueue(inputq, sk, dport, &xmitq);
  1995. spin_unlock_bh(&sk->sk_lock.slock);
  1996. }
  1997. /* Send pending response/rejected messages, if any */
  1998. tipc_node_distr_xmit(sock_net(sk), &xmitq);
  1999. sock_put(sk);
  2000. continue;
  2001. }
  2002. /* No destination socket => dequeue skb if still there */
  2003. skb = tipc_skb_dequeue(inputq, dport);
  2004. if (!skb)
  2005. return;
  2006. /* Try secondary lookup if unresolved named message */
  2007. err = TIPC_ERR_NO_PORT;
  2008. if (tipc_msg_lookup_dest(net, skb, &err))
  2009. goto xmit;
  2010. /* Prepare for message rejection */
  2011. if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
  2012. continue;
  2013. xmit:
  2014. dnode = msg_destnode(buf_msg(skb));
  2015. tipc_node_xmit_skb(net, skb, dnode, dport);
  2016. }
  2017. }
  2018. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  2019. {
  2020. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2021. struct sock *sk = sock->sk;
  2022. int done;
  2023. do {
  2024. int err = sock_error(sk);
  2025. if (err)
  2026. return err;
  2027. if (!*timeo_p)
  2028. return -ETIMEDOUT;
  2029. if (signal_pending(current))
  2030. return sock_intr_errno(*timeo_p);
  2031. add_wait_queue(sk_sleep(sk), &wait);
  2032. done = sk_wait_event(sk, timeo_p,
  2033. sk->sk_state != TIPC_CONNECTING, &wait);
  2034. remove_wait_queue(sk_sleep(sk), &wait);
  2035. } while (!done);
  2036. return 0;
  2037. }
  2038. /**
  2039. * tipc_connect - establish a connection to another TIPC port
  2040. * @sock: socket structure
  2041. * @dest: socket address for destination port
  2042. * @destlen: size of socket address data structure
  2043. * @flags: file-related flags associated with socket
  2044. *
  2045. * Returns 0 on success, errno otherwise
  2046. */
  2047. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  2048. int destlen, int flags)
  2049. {
  2050. struct sock *sk = sock->sk;
  2051. struct tipc_sock *tsk = tipc_sk(sk);
  2052. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  2053. struct msghdr m = {NULL,};
  2054. long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
  2055. int previous;
  2056. int res = 0;
  2057. if (destlen != sizeof(struct sockaddr_tipc))
  2058. return -EINVAL;
  2059. lock_sock(sk);
  2060. if (tsk->group) {
  2061. res = -EINVAL;
  2062. goto exit;
  2063. }
  2064. if (dst->family == AF_UNSPEC) {
  2065. memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
  2066. if (!tipc_sk_type_connectionless(sk))
  2067. res = -EINVAL;
  2068. goto exit;
  2069. } else if (dst->family != AF_TIPC) {
  2070. res = -EINVAL;
  2071. }
  2072. if (dst->addrtype != TIPC_ADDR_ID && dst->addrtype != TIPC_ADDR_NAME)
  2073. res = -EINVAL;
  2074. if (res)
  2075. goto exit;
  2076. /* DGRAM/RDM connect(), just save the destaddr */
  2077. if (tipc_sk_type_connectionless(sk)) {
  2078. memcpy(&tsk->peer, dest, destlen);
  2079. goto exit;
  2080. }
  2081. previous = sk->sk_state;
  2082. switch (sk->sk_state) {
  2083. case TIPC_OPEN:
  2084. /* Send a 'SYN-' to destination */
  2085. m.msg_name = dest;
  2086. m.msg_namelen = destlen;
  2087. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  2088. * indicate send_msg() is never blocked.
  2089. */
  2090. if (!timeout)
  2091. m.msg_flags = MSG_DONTWAIT;
  2092. res = __tipc_sendmsg(sock, &m, 0);
  2093. if ((res < 0) && (res != -EWOULDBLOCK))
  2094. goto exit;
  2095. /* Just entered TIPC_CONNECTING state; the only
  2096. * difference is that return value in non-blocking
  2097. * case is EINPROGRESS, rather than EALREADY.
  2098. */
  2099. res = -EINPROGRESS;
  2100. /* fall thru' */
  2101. case TIPC_CONNECTING:
  2102. if (!timeout) {
  2103. if (previous == TIPC_CONNECTING)
  2104. res = -EALREADY;
  2105. goto exit;
  2106. }
  2107. timeout = msecs_to_jiffies(timeout);
  2108. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  2109. res = tipc_wait_for_connect(sock, &timeout);
  2110. break;
  2111. case TIPC_ESTABLISHED:
  2112. res = -EISCONN;
  2113. break;
  2114. default:
  2115. res = -EINVAL;
  2116. }
  2117. exit:
  2118. release_sock(sk);
  2119. return res;
  2120. }
  2121. /**
  2122. * tipc_listen - allow socket to listen for incoming connections
  2123. * @sock: socket structure
  2124. * @len: (unused)
  2125. *
  2126. * Returns 0 on success, errno otherwise
  2127. */
  2128. static int tipc_listen(struct socket *sock, int len)
  2129. {
  2130. struct sock *sk = sock->sk;
  2131. int res;
  2132. lock_sock(sk);
  2133. res = tipc_set_sk_state(sk, TIPC_LISTEN);
  2134. release_sock(sk);
  2135. return res;
  2136. }
  2137. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  2138. {
  2139. struct sock *sk = sock->sk;
  2140. DEFINE_WAIT(wait);
  2141. int err;
  2142. /* True wake-one mechanism for incoming connections: only
  2143. * one process gets woken up, not the 'whole herd'.
  2144. * Since we do not 'race & poll' for established sockets
  2145. * anymore, the common case will execute the loop only once.
  2146. */
  2147. for (;;) {
  2148. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  2149. TASK_INTERRUPTIBLE);
  2150. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  2151. release_sock(sk);
  2152. timeo = schedule_timeout(timeo);
  2153. lock_sock(sk);
  2154. }
  2155. err = 0;
  2156. if (!skb_queue_empty(&sk->sk_receive_queue))
  2157. break;
  2158. err = -EAGAIN;
  2159. if (!timeo)
  2160. break;
  2161. err = sock_intr_errno(timeo);
  2162. if (signal_pending(current))
  2163. break;
  2164. }
  2165. finish_wait(sk_sleep(sk), &wait);
  2166. return err;
  2167. }
  2168. /**
  2169. * tipc_accept - wait for connection request
  2170. * @sock: listening socket
  2171. * @newsock: new socket that is to be connected
  2172. * @flags: file-related flags associated with socket
  2173. *
  2174. * Returns 0 on success, errno otherwise
  2175. */
  2176. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
  2177. bool kern)
  2178. {
  2179. struct sock *new_sk, *sk = sock->sk;
  2180. struct sk_buff *buf;
  2181. struct tipc_sock *new_tsock;
  2182. struct tipc_msg *msg;
  2183. long timeo;
  2184. int res;
  2185. lock_sock(sk);
  2186. if (sk->sk_state != TIPC_LISTEN) {
  2187. res = -EINVAL;
  2188. goto exit;
  2189. }
  2190. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  2191. res = tipc_wait_for_accept(sock, timeo);
  2192. if (res)
  2193. goto exit;
  2194. buf = skb_peek(&sk->sk_receive_queue);
  2195. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
  2196. if (res)
  2197. goto exit;
  2198. security_sk_clone(sock->sk, new_sock->sk);
  2199. new_sk = new_sock->sk;
  2200. new_tsock = tipc_sk(new_sk);
  2201. msg = buf_msg(buf);
  2202. /* we lock on new_sk; but lockdep sees the lock on sk */
  2203. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  2204. /*
  2205. * Reject any stray messages received by new socket
  2206. * before the socket lock was taken (very, very unlikely)
  2207. */
  2208. tsk_rej_rx_queue(new_sk);
  2209. /* Connect new socket to it's peer */
  2210. tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
  2211. tsk_set_importance(new_tsock, msg_importance(msg));
  2212. if (msg_named(msg)) {
  2213. new_tsock->conn_type = msg_nametype(msg);
  2214. new_tsock->conn_instance = msg_nameinst(msg);
  2215. }
  2216. /*
  2217. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  2218. * Respond to 'SYN+' by queuing it on new socket.
  2219. */
  2220. if (!msg_data_sz(msg)) {
  2221. struct msghdr m = {NULL,};
  2222. tsk_advance_rx_queue(sk);
  2223. __tipc_sendstream(new_sock, &m, 0);
  2224. } else {
  2225. __skb_dequeue(&sk->sk_receive_queue);
  2226. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  2227. skb_set_owner_r(buf, new_sk);
  2228. }
  2229. release_sock(new_sk);
  2230. exit:
  2231. release_sock(sk);
  2232. return res;
  2233. }
  2234. /**
  2235. * tipc_shutdown - shutdown socket connection
  2236. * @sock: socket structure
  2237. * @how: direction to close (must be SHUT_RDWR)
  2238. *
  2239. * Terminates connection (if necessary), then purges socket's receive queue.
  2240. *
  2241. * Returns 0 on success, errno otherwise
  2242. */
  2243. static int tipc_shutdown(struct socket *sock, int how)
  2244. {
  2245. struct sock *sk = sock->sk;
  2246. int res;
  2247. if (how != SHUT_RDWR)
  2248. return -EINVAL;
  2249. lock_sock(sk);
  2250. __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
  2251. sk->sk_shutdown = SEND_SHUTDOWN;
  2252. if (sk->sk_state == TIPC_DISCONNECTING) {
  2253. /* Discard any unreceived messages */
  2254. __skb_queue_purge(&sk->sk_receive_queue);
  2255. /* Wake up anyone sleeping in poll */
  2256. sk->sk_state_change(sk);
  2257. res = 0;
  2258. } else {
  2259. res = -ENOTCONN;
  2260. }
  2261. release_sock(sk);
  2262. return res;
  2263. }
  2264. static void tipc_sk_check_probing_state(struct sock *sk,
  2265. struct sk_buff_head *list)
  2266. {
  2267. struct tipc_sock *tsk = tipc_sk(sk);
  2268. u32 pnode = tsk_peer_node(tsk);
  2269. u32 pport = tsk_peer_port(tsk);
  2270. u32 self = tsk_own_node(tsk);
  2271. u32 oport = tsk->portid;
  2272. struct sk_buff *skb;
  2273. if (tsk->probe_unacked) {
  2274. tipc_set_sk_state(sk, TIPC_DISCONNECTING);
  2275. sk->sk_err = ECONNABORTED;
  2276. tipc_node_remove_conn(sock_net(sk), pnode, pport);
  2277. sk->sk_state_change(sk);
  2278. return;
  2279. }
  2280. /* Prepare new probe */
  2281. skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
  2282. pnode, self, pport, oport, TIPC_OK);
  2283. if (skb)
  2284. __skb_queue_tail(list, skb);
  2285. tsk->probe_unacked = true;
  2286. sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
  2287. }
  2288. static void tipc_sk_retry_connect(struct sock *sk, struct sk_buff_head *list)
  2289. {
  2290. struct tipc_sock *tsk = tipc_sk(sk);
  2291. /* Try again later if dest link is congested */
  2292. if (tsk->cong_link_cnt) {
  2293. sk_reset_timer(sk, &sk->sk_timer, msecs_to_jiffies(100));
  2294. return;
  2295. }
  2296. /* Prepare SYN for retransmit */
  2297. tipc_msg_skb_clone(&sk->sk_write_queue, list);
  2298. }
  2299. static void tipc_sk_timeout(struct timer_list *t)
  2300. {
  2301. struct sock *sk = from_timer(sk, t, sk_timer);
  2302. struct tipc_sock *tsk = tipc_sk(sk);
  2303. u32 pnode = tsk_peer_node(tsk);
  2304. struct sk_buff_head list;
  2305. int rc = 0;
  2306. skb_queue_head_init(&list);
  2307. bh_lock_sock(sk);
  2308. /* Try again later if socket is busy */
  2309. if (sock_owned_by_user(sk)) {
  2310. sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
  2311. bh_unlock_sock(sk);
  2312. return;
  2313. }
  2314. if (sk->sk_state == TIPC_ESTABLISHED)
  2315. tipc_sk_check_probing_state(sk, &list);
  2316. else if (sk->sk_state == TIPC_CONNECTING)
  2317. tipc_sk_retry_connect(sk, &list);
  2318. bh_unlock_sock(sk);
  2319. if (!skb_queue_empty(&list))
  2320. rc = tipc_node_xmit(sock_net(sk), &list, pnode, tsk->portid);
  2321. /* SYN messages may cause link congestion */
  2322. if (rc == -ELINKCONG) {
  2323. tipc_dest_push(&tsk->cong_links, pnode, 0);
  2324. tsk->cong_link_cnt = 1;
  2325. }
  2326. sock_put(sk);
  2327. }
  2328. static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
  2329. struct tipc_name_seq const *seq)
  2330. {
  2331. struct sock *sk = &tsk->sk;
  2332. struct net *net = sock_net(sk);
  2333. struct publication *publ;
  2334. u32 key;
  2335. if (scope != TIPC_NODE_SCOPE)
  2336. scope = TIPC_CLUSTER_SCOPE;
  2337. if (tipc_sk_connected(sk))
  2338. return -EINVAL;
  2339. key = tsk->portid + tsk->pub_count + 1;
  2340. if (key == tsk->portid)
  2341. return -EADDRINUSE;
  2342. publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
  2343. scope, tsk->portid, key);
  2344. if (unlikely(!publ))
  2345. return -EINVAL;
  2346. list_add(&publ->binding_sock, &tsk->publications);
  2347. tsk->pub_count++;
  2348. tsk->published = 1;
  2349. return 0;
  2350. }
  2351. static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
  2352. struct tipc_name_seq const *seq)
  2353. {
  2354. struct net *net = sock_net(&tsk->sk);
  2355. struct publication *publ;
  2356. struct publication *safe;
  2357. int rc = -EINVAL;
  2358. if (scope != TIPC_NODE_SCOPE)
  2359. scope = TIPC_CLUSTER_SCOPE;
  2360. list_for_each_entry_safe(publ, safe, &tsk->publications, binding_sock) {
  2361. if (seq) {
  2362. if (publ->scope != scope)
  2363. continue;
  2364. if (publ->type != seq->type)
  2365. continue;
  2366. if (publ->lower != seq->lower)
  2367. continue;
  2368. if (publ->upper != seq->upper)
  2369. break;
  2370. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  2371. publ->upper, publ->key);
  2372. rc = 0;
  2373. break;
  2374. }
  2375. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  2376. publ->upper, publ->key);
  2377. rc = 0;
  2378. }
  2379. if (list_empty(&tsk->publications))
  2380. tsk->published = 0;
  2381. return rc;
  2382. }
  2383. /* tipc_sk_reinit: set non-zero address in all existing sockets
  2384. * when we go from standalone to network mode.
  2385. */
  2386. void tipc_sk_reinit(struct net *net)
  2387. {
  2388. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2389. struct rhashtable_iter iter;
  2390. struct tipc_sock *tsk;
  2391. struct tipc_msg *msg;
  2392. rhashtable_walk_enter(&tn->sk_rht, &iter);
  2393. do {
  2394. rhashtable_walk_start(&iter);
  2395. while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
  2396. spin_lock_bh(&tsk->sk.sk_lock.slock);
  2397. msg = &tsk->phdr;
  2398. msg_set_prevnode(msg, tipc_own_addr(net));
  2399. msg_set_orignode(msg, tipc_own_addr(net));
  2400. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2401. }
  2402. rhashtable_walk_stop(&iter);
  2403. } while (tsk == ERR_PTR(-EAGAIN));
  2404. rhashtable_walk_exit(&iter);
  2405. }
  2406. static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
  2407. {
  2408. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2409. struct tipc_sock *tsk;
  2410. rcu_read_lock();
  2411. tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
  2412. if (tsk)
  2413. sock_hold(&tsk->sk);
  2414. rcu_read_unlock();
  2415. return tsk;
  2416. }
  2417. static int tipc_sk_insert(struct tipc_sock *tsk)
  2418. {
  2419. struct sock *sk = &tsk->sk;
  2420. struct net *net = sock_net(sk);
  2421. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2422. u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
  2423. u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
  2424. while (remaining--) {
  2425. portid++;
  2426. if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
  2427. portid = TIPC_MIN_PORT;
  2428. tsk->portid = portid;
  2429. sock_hold(&tsk->sk);
  2430. if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
  2431. tsk_rht_params))
  2432. return 0;
  2433. sock_put(&tsk->sk);
  2434. }
  2435. return -1;
  2436. }
  2437. static void tipc_sk_remove(struct tipc_sock *tsk)
  2438. {
  2439. struct sock *sk = &tsk->sk;
  2440. struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
  2441. if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
  2442. WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
  2443. __sock_put(sk);
  2444. }
  2445. }
  2446. static const struct rhashtable_params tsk_rht_params = {
  2447. .nelem_hint = 192,
  2448. .head_offset = offsetof(struct tipc_sock, node),
  2449. .key_offset = offsetof(struct tipc_sock, portid),
  2450. .key_len = sizeof(u32), /* portid */
  2451. .max_size = 1048576,
  2452. .min_size = 256,
  2453. .automatic_shrinking = true,
  2454. };
  2455. int tipc_sk_rht_init(struct net *net)
  2456. {
  2457. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2458. return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
  2459. }
  2460. void tipc_sk_rht_destroy(struct net *net)
  2461. {
  2462. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2463. /* Wait for socket readers to complete */
  2464. synchronize_net();
  2465. rhashtable_destroy(&tn->sk_rht);
  2466. }
  2467. static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
  2468. {
  2469. struct net *net = sock_net(&tsk->sk);
  2470. struct tipc_group *grp = tsk->group;
  2471. struct tipc_msg *hdr = &tsk->phdr;
  2472. struct tipc_name_seq seq;
  2473. int rc;
  2474. if (mreq->type < TIPC_RESERVED_TYPES)
  2475. return -EACCES;
  2476. if (mreq->scope > TIPC_NODE_SCOPE)
  2477. return -EINVAL;
  2478. if (grp)
  2479. return -EACCES;
  2480. grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
  2481. if (!grp)
  2482. return -ENOMEM;
  2483. tsk->group = grp;
  2484. msg_set_lookup_scope(hdr, mreq->scope);
  2485. msg_set_nametype(hdr, mreq->type);
  2486. msg_set_dest_droppable(hdr, true);
  2487. seq.type = mreq->type;
  2488. seq.lower = mreq->instance;
  2489. seq.upper = seq.lower;
  2490. tipc_nametbl_build_group(net, grp, mreq->type, mreq->scope);
  2491. rc = tipc_sk_publish(tsk, mreq->scope, &seq);
  2492. if (rc) {
  2493. tipc_group_delete(net, grp);
  2494. tsk->group = NULL;
  2495. return rc;
  2496. }
  2497. /* Eliminate any risk that a broadcast overtakes sent JOINs */
  2498. tsk->mc_method.rcast = true;
  2499. tsk->mc_method.mandatory = true;
  2500. tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf);
  2501. return rc;
  2502. }
  2503. static int tipc_sk_leave(struct tipc_sock *tsk)
  2504. {
  2505. struct net *net = sock_net(&tsk->sk);
  2506. struct tipc_group *grp = tsk->group;
  2507. struct tipc_name_seq seq;
  2508. int scope;
  2509. if (!grp)
  2510. return -EINVAL;
  2511. tipc_group_self(grp, &seq, &scope);
  2512. tipc_group_delete(net, grp);
  2513. tsk->group = NULL;
  2514. tipc_sk_withdraw(tsk, scope, &seq);
  2515. return 0;
  2516. }
  2517. /**
  2518. * tipc_setsockopt - set socket option
  2519. * @sock: socket structure
  2520. * @lvl: option level
  2521. * @opt: option identifier
  2522. * @ov: pointer to new option value
  2523. * @ol: length of option value
  2524. *
  2525. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  2526. * (to ease compatibility).
  2527. *
  2528. * Returns 0 on success, errno otherwise
  2529. */
  2530. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  2531. char __user *ov, unsigned int ol)
  2532. {
  2533. struct sock *sk = sock->sk;
  2534. struct tipc_sock *tsk = tipc_sk(sk);
  2535. struct tipc_group_req mreq;
  2536. u32 value = 0;
  2537. int res = 0;
  2538. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2539. return 0;
  2540. if (lvl != SOL_TIPC)
  2541. return -ENOPROTOOPT;
  2542. switch (opt) {
  2543. case TIPC_IMPORTANCE:
  2544. case TIPC_SRC_DROPPABLE:
  2545. case TIPC_DEST_DROPPABLE:
  2546. case TIPC_CONN_TIMEOUT:
  2547. if (ol < sizeof(value))
  2548. return -EINVAL;
  2549. if (get_user(value, (u32 __user *)ov))
  2550. return -EFAULT;
  2551. break;
  2552. case TIPC_GROUP_JOIN:
  2553. if (ol < sizeof(mreq))
  2554. return -EINVAL;
  2555. if (copy_from_user(&mreq, ov, sizeof(mreq)))
  2556. return -EFAULT;
  2557. break;
  2558. default:
  2559. if (ov || ol)
  2560. return -EINVAL;
  2561. }
  2562. lock_sock(sk);
  2563. switch (opt) {
  2564. case TIPC_IMPORTANCE:
  2565. res = tsk_set_importance(tsk, value);
  2566. break;
  2567. case TIPC_SRC_DROPPABLE:
  2568. if (sock->type != SOCK_STREAM)
  2569. tsk_set_unreliable(tsk, value);
  2570. else
  2571. res = -ENOPROTOOPT;
  2572. break;
  2573. case TIPC_DEST_DROPPABLE:
  2574. tsk_set_unreturnable(tsk, value);
  2575. break;
  2576. case TIPC_CONN_TIMEOUT:
  2577. tipc_sk(sk)->conn_timeout = value;
  2578. break;
  2579. case TIPC_MCAST_BROADCAST:
  2580. tsk->mc_method.rcast = false;
  2581. tsk->mc_method.mandatory = true;
  2582. break;
  2583. case TIPC_MCAST_REPLICAST:
  2584. tsk->mc_method.rcast = true;
  2585. tsk->mc_method.mandatory = true;
  2586. break;
  2587. case TIPC_GROUP_JOIN:
  2588. res = tipc_sk_join(tsk, &mreq);
  2589. break;
  2590. case TIPC_GROUP_LEAVE:
  2591. res = tipc_sk_leave(tsk);
  2592. break;
  2593. default:
  2594. res = -EINVAL;
  2595. }
  2596. release_sock(sk);
  2597. return res;
  2598. }
  2599. /**
  2600. * tipc_getsockopt - get socket option
  2601. * @sock: socket structure
  2602. * @lvl: option level
  2603. * @opt: option identifier
  2604. * @ov: receptacle for option value
  2605. * @ol: receptacle for length of option value
  2606. *
  2607. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  2608. * (to ease compatibility).
  2609. *
  2610. * Returns 0 on success, errno otherwise
  2611. */
  2612. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  2613. char __user *ov, int __user *ol)
  2614. {
  2615. struct sock *sk = sock->sk;
  2616. struct tipc_sock *tsk = tipc_sk(sk);
  2617. struct tipc_name_seq seq;
  2618. int len, scope;
  2619. u32 value;
  2620. int res;
  2621. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2622. return put_user(0, ol);
  2623. if (lvl != SOL_TIPC)
  2624. return -ENOPROTOOPT;
  2625. res = get_user(len, ol);
  2626. if (res)
  2627. return res;
  2628. lock_sock(sk);
  2629. switch (opt) {
  2630. case TIPC_IMPORTANCE:
  2631. value = tsk_importance(tsk);
  2632. break;
  2633. case TIPC_SRC_DROPPABLE:
  2634. value = tsk_unreliable(tsk);
  2635. break;
  2636. case TIPC_DEST_DROPPABLE:
  2637. value = tsk_unreturnable(tsk);
  2638. break;
  2639. case TIPC_CONN_TIMEOUT:
  2640. value = tsk->conn_timeout;
  2641. /* no need to set "res", since already 0 at this point */
  2642. break;
  2643. case TIPC_NODE_RECVQ_DEPTH:
  2644. value = 0; /* was tipc_queue_size, now obsolete */
  2645. break;
  2646. case TIPC_SOCK_RECVQ_DEPTH:
  2647. value = skb_queue_len(&sk->sk_receive_queue);
  2648. break;
  2649. case TIPC_GROUP_JOIN:
  2650. seq.type = 0;
  2651. if (tsk->group)
  2652. tipc_group_self(tsk->group, &seq, &scope);
  2653. value = seq.type;
  2654. break;
  2655. default:
  2656. res = -EINVAL;
  2657. }
  2658. release_sock(sk);
  2659. if (res)
  2660. return res; /* "get" failed */
  2661. if (len < sizeof(value))
  2662. return -EINVAL;
  2663. if (copy_to_user(ov, &value, sizeof(value)))
  2664. return -EFAULT;
  2665. return put_user(sizeof(value), ol);
  2666. }
  2667. static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2668. {
  2669. struct net *net = sock_net(sock->sk);
  2670. struct tipc_sioc_nodeid_req nr = {0};
  2671. struct tipc_sioc_ln_req lnr;
  2672. void __user *argp = (void __user *)arg;
  2673. switch (cmd) {
  2674. case SIOCGETLINKNAME:
  2675. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  2676. return -EFAULT;
  2677. if (!tipc_node_get_linkname(net,
  2678. lnr.bearer_id & 0xffff, lnr.peer,
  2679. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  2680. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  2681. return -EFAULT;
  2682. return 0;
  2683. }
  2684. return -EADDRNOTAVAIL;
  2685. case SIOCGETNODEID:
  2686. if (copy_from_user(&nr, argp, sizeof(nr)))
  2687. return -EFAULT;
  2688. if (!tipc_node_get_id(net, nr.peer, nr.node_id))
  2689. return -EADDRNOTAVAIL;
  2690. if (copy_to_user(argp, &nr, sizeof(nr)))
  2691. return -EFAULT;
  2692. return 0;
  2693. default:
  2694. return -ENOIOCTLCMD;
  2695. }
  2696. }
  2697. static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
  2698. {
  2699. struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
  2700. struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
  2701. u32 onode = tipc_own_addr(sock_net(sock1->sk));
  2702. tsk1->peer.family = AF_TIPC;
  2703. tsk1->peer.addrtype = TIPC_ADDR_ID;
  2704. tsk1->peer.scope = TIPC_NODE_SCOPE;
  2705. tsk1->peer.addr.id.ref = tsk2->portid;
  2706. tsk1->peer.addr.id.node = onode;
  2707. tsk2->peer.family = AF_TIPC;
  2708. tsk2->peer.addrtype = TIPC_ADDR_ID;
  2709. tsk2->peer.scope = TIPC_NODE_SCOPE;
  2710. tsk2->peer.addr.id.ref = tsk1->portid;
  2711. tsk2->peer.addr.id.node = onode;
  2712. tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
  2713. tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
  2714. return 0;
  2715. }
  2716. /* Protocol switches for the various types of TIPC sockets */
  2717. static const struct proto_ops msg_ops = {
  2718. .owner = THIS_MODULE,
  2719. .family = AF_TIPC,
  2720. .release = tipc_release,
  2721. .bind = tipc_bind,
  2722. .connect = tipc_connect,
  2723. .socketpair = tipc_socketpair,
  2724. .accept = sock_no_accept,
  2725. .getname = tipc_getname,
  2726. .poll = tipc_poll,
  2727. .ioctl = tipc_ioctl,
  2728. .listen = sock_no_listen,
  2729. .shutdown = tipc_shutdown,
  2730. .setsockopt = tipc_setsockopt,
  2731. .getsockopt = tipc_getsockopt,
  2732. .sendmsg = tipc_sendmsg,
  2733. .recvmsg = tipc_recvmsg,
  2734. .mmap = sock_no_mmap,
  2735. .sendpage = sock_no_sendpage
  2736. };
  2737. static const struct proto_ops packet_ops = {
  2738. .owner = THIS_MODULE,
  2739. .family = AF_TIPC,
  2740. .release = tipc_release,
  2741. .bind = tipc_bind,
  2742. .connect = tipc_connect,
  2743. .socketpair = tipc_socketpair,
  2744. .accept = tipc_accept,
  2745. .getname = tipc_getname,
  2746. .poll = tipc_poll,
  2747. .ioctl = tipc_ioctl,
  2748. .listen = tipc_listen,
  2749. .shutdown = tipc_shutdown,
  2750. .setsockopt = tipc_setsockopt,
  2751. .getsockopt = tipc_getsockopt,
  2752. .sendmsg = tipc_send_packet,
  2753. .recvmsg = tipc_recvmsg,
  2754. .mmap = sock_no_mmap,
  2755. .sendpage = sock_no_sendpage
  2756. };
  2757. static const struct proto_ops stream_ops = {
  2758. .owner = THIS_MODULE,
  2759. .family = AF_TIPC,
  2760. .release = tipc_release,
  2761. .bind = tipc_bind,
  2762. .connect = tipc_connect,
  2763. .socketpair = tipc_socketpair,
  2764. .accept = tipc_accept,
  2765. .getname = tipc_getname,
  2766. .poll = tipc_poll,
  2767. .ioctl = tipc_ioctl,
  2768. .listen = tipc_listen,
  2769. .shutdown = tipc_shutdown,
  2770. .setsockopt = tipc_setsockopt,
  2771. .getsockopt = tipc_getsockopt,
  2772. .sendmsg = tipc_sendstream,
  2773. .recvmsg = tipc_recvstream,
  2774. .mmap = sock_no_mmap,
  2775. .sendpage = sock_no_sendpage
  2776. };
  2777. static const struct net_proto_family tipc_family_ops = {
  2778. .owner = THIS_MODULE,
  2779. .family = AF_TIPC,
  2780. .create = tipc_sk_create
  2781. };
  2782. static struct proto tipc_proto = {
  2783. .name = "TIPC",
  2784. .owner = THIS_MODULE,
  2785. .obj_size = sizeof(struct tipc_sock),
  2786. .sysctl_rmem = sysctl_tipc_rmem
  2787. };
  2788. /**
  2789. * tipc_socket_init - initialize TIPC socket interface
  2790. *
  2791. * Returns 0 on success, errno otherwise
  2792. */
  2793. int tipc_socket_init(void)
  2794. {
  2795. int res;
  2796. res = proto_register(&tipc_proto, 1);
  2797. if (res) {
  2798. pr_err("Failed to register TIPC protocol type\n");
  2799. goto out;
  2800. }
  2801. res = sock_register(&tipc_family_ops);
  2802. if (res) {
  2803. pr_err("Failed to register TIPC socket type\n");
  2804. proto_unregister(&tipc_proto);
  2805. goto out;
  2806. }
  2807. out:
  2808. return res;
  2809. }
  2810. /**
  2811. * tipc_socket_stop - stop TIPC socket interface
  2812. */
  2813. void tipc_socket_stop(void)
  2814. {
  2815. sock_unregister(tipc_family_ops.family);
  2816. proto_unregister(&tipc_proto);
  2817. }
  2818. /* Caller should hold socket lock for the passed tipc socket. */
  2819. static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
  2820. {
  2821. u32 peer_node;
  2822. u32 peer_port;
  2823. struct nlattr *nest;
  2824. peer_node = tsk_peer_node(tsk);
  2825. peer_port = tsk_peer_port(tsk);
  2826. nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
  2827. if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
  2828. goto msg_full;
  2829. if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
  2830. goto msg_full;
  2831. if (tsk->conn_type != 0) {
  2832. if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
  2833. goto msg_full;
  2834. if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
  2835. goto msg_full;
  2836. if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
  2837. goto msg_full;
  2838. }
  2839. nla_nest_end(skb, nest);
  2840. return 0;
  2841. msg_full:
  2842. nla_nest_cancel(skb, nest);
  2843. return -EMSGSIZE;
  2844. }
  2845. static int __tipc_nl_add_sk_info(struct sk_buff *skb, struct tipc_sock
  2846. *tsk)
  2847. {
  2848. struct net *net = sock_net(skb->sk);
  2849. struct sock *sk = &tsk->sk;
  2850. if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid) ||
  2851. nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr(net)))
  2852. return -EMSGSIZE;
  2853. if (tipc_sk_connected(sk)) {
  2854. if (__tipc_nl_add_sk_con(skb, tsk))
  2855. return -EMSGSIZE;
  2856. } else if (!list_empty(&tsk->publications)) {
  2857. if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
  2858. return -EMSGSIZE;
  2859. }
  2860. return 0;
  2861. }
  2862. /* Caller should hold socket lock for the passed tipc socket. */
  2863. static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
  2864. struct tipc_sock *tsk)
  2865. {
  2866. struct nlattr *attrs;
  2867. void *hdr;
  2868. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2869. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
  2870. if (!hdr)
  2871. goto msg_cancel;
  2872. attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
  2873. if (!attrs)
  2874. goto genlmsg_cancel;
  2875. if (__tipc_nl_add_sk_info(skb, tsk))
  2876. goto attr_msg_cancel;
  2877. nla_nest_end(skb, attrs);
  2878. genlmsg_end(skb, hdr);
  2879. return 0;
  2880. attr_msg_cancel:
  2881. nla_nest_cancel(skb, attrs);
  2882. genlmsg_cancel:
  2883. genlmsg_cancel(skb, hdr);
  2884. msg_cancel:
  2885. return -EMSGSIZE;
  2886. }
  2887. int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,
  2888. int (*skb_handler)(struct sk_buff *skb,
  2889. struct netlink_callback *cb,
  2890. struct tipc_sock *tsk))
  2891. {
  2892. struct rhashtable_iter *iter = (void *)cb->args[4];
  2893. struct tipc_sock *tsk;
  2894. int err;
  2895. rhashtable_walk_start(iter);
  2896. while ((tsk = rhashtable_walk_next(iter)) != NULL) {
  2897. if (IS_ERR(tsk)) {
  2898. err = PTR_ERR(tsk);
  2899. if (err == -EAGAIN) {
  2900. err = 0;
  2901. continue;
  2902. }
  2903. break;
  2904. }
  2905. sock_hold(&tsk->sk);
  2906. rhashtable_walk_stop(iter);
  2907. lock_sock(&tsk->sk);
  2908. err = skb_handler(skb, cb, tsk);
  2909. if (err) {
  2910. release_sock(&tsk->sk);
  2911. sock_put(&tsk->sk);
  2912. goto out;
  2913. }
  2914. release_sock(&tsk->sk);
  2915. rhashtable_walk_start(iter);
  2916. sock_put(&tsk->sk);
  2917. }
  2918. rhashtable_walk_stop(iter);
  2919. out:
  2920. return skb->len;
  2921. }
  2922. EXPORT_SYMBOL(tipc_nl_sk_walk);
  2923. int tipc_dump_start(struct netlink_callback *cb)
  2924. {
  2925. return __tipc_dump_start(cb, sock_net(cb->skb->sk));
  2926. }
  2927. EXPORT_SYMBOL(tipc_dump_start);
  2928. int __tipc_dump_start(struct netlink_callback *cb, struct net *net)
  2929. {
  2930. /* tipc_nl_name_table_dump() uses cb->args[0...3]. */
  2931. struct rhashtable_iter *iter = (void *)cb->args[4];
  2932. struct tipc_net *tn = tipc_net(net);
  2933. if (!iter) {
  2934. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  2935. if (!iter)
  2936. return -ENOMEM;
  2937. cb->args[4] = (long)iter;
  2938. }
  2939. rhashtable_walk_enter(&tn->sk_rht, iter);
  2940. return 0;
  2941. }
  2942. int tipc_dump_done(struct netlink_callback *cb)
  2943. {
  2944. struct rhashtable_iter *hti = (void *)cb->args[4];
  2945. rhashtable_walk_exit(hti);
  2946. kfree(hti);
  2947. return 0;
  2948. }
  2949. EXPORT_SYMBOL(tipc_dump_done);
  2950. int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb,
  2951. struct tipc_sock *tsk, u32 sk_filter_state,
  2952. u64 (*tipc_diag_gen_cookie)(struct sock *sk))
  2953. {
  2954. struct sock *sk = &tsk->sk;
  2955. struct nlattr *attrs;
  2956. struct nlattr *stat;
  2957. /*filter response w.r.t sk_state*/
  2958. if (!(sk_filter_state & (1 << sk->sk_state)))
  2959. return 0;
  2960. attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
  2961. if (!attrs)
  2962. goto msg_cancel;
  2963. if (__tipc_nl_add_sk_info(skb, tsk))
  2964. goto attr_msg_cancel;
  2965. if (nla_put_u32(skb, TIPC_NLA_SOCK_TYPE, (u32)sk->sk_type) ||
  2966. nla_put_u32(skb, TIPC_NLA_SOCK_TIPC_STATE, (u32)sk->sk_state) ||
  2967. nla_put_u32(skb, TIPC_NLA_SOCK_INO, sock_i_ino(sk)) ||
  2968. nla_put_u32(skb, TIPC_NLA_SOCK_UID,
  2969. from_kuid_munged(sk_user_ns(NETLINK_CB(cb->skb).sk),
  2970. sock_i_uid(sk))) ||
  2971. nla_put_u64_64bit(skb, TIPC_NLA_SOCK_COOKIE,
  2972. tipc_diag_gen_cookie(sk),
  2973. TIPC_NLA_SOCK_PAD))
  2974. goto attr_msg_cancel;
  2975. stat = nla_nest_start(skb, TIPC_NLA_SOCK_STAT);
  2976. if (!stat)
  2977. goto attr_msg_cancel;
  2978. if (nla_put_u32(skb, TIPC_NLA_SOCK_STAT_RCVQ,
  2979. skb_queue_len(&sk->sk_receive_queue)) ||
  2980. nla_put_u32(skb, TIPC_NLA_SOCK_STAT_SENDQ,
  2981. skb_queue_len(&sk->sk_write_queue)) ||
  2982. nla_put_u32(skb, TIPC_NLA_SOCK_STAT_DROP,
  2983. atomic_read(&sk->sk_drops)))
  2984. goto stat_msg_cancel;
  2985. if (tsk->cong_link_cnt &&
  2986. nla_put_flag(skb, TIPC_NLA_SOCK_STAT_LINK_CONG))
  2987. goto stat_msg_cancel;
  2988. if (tsk_conn_cong(tsk) &&
  2989. nla_put_flag(skb, TIPC_NLA_SOCK_STAT_CONN_CONG))
  2990. goto stat_msg_cancel;
  2991. nla_nest_end(skb, stat);
  2992. if (tsk->group)
  2993. if (tipc_group_fill_sock_diag(tsk->group, skb))
  2994. goto stat_msg_cancel;
  2995. nla_nest_end(skb, attrs);
  2996. return 0;
  2997. stat_msg_cancel:
  2998. nla_nest_cancel(skb, stat);
  2999. attr_msg_cancel:
  3000. nla_nest_cancel(skb, attrs);
  3001. msg_cancel:
  3002. return -EMSGSIZE;
  3003. }
  3004. EXPORT_SYMBOL(tipc_sk_fill_sock_diag);
  3005. int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
  3006. {
  3007. return tipc_nl_sk_walk(skb, cb, __tipc_nl_add_sk);
  3008. }
  3009. /* Caller should hold socket lock for the passed tipc socket. */
  3010. static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
  3011. struct netlink_callback *cb,
  3012. struct publication *publ)
  3013. {
  3014. void *hdr;
  3015. struct nlattr *attrs;
  3016. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  3017. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
  3018. if (!hdr)
  3019. goto msg_cancel;
  3020. attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
  3021. if (!attrs)
  3022. goto genlmsg_cancel;
  3023. if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
  3024. goto attr_msg_cancel;
  3025. if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
  3026. goto attr_msg_cancel;
  3027. if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
  3028. goto attr_msg_cancel;
  3029. if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
  3030. goto attr_msg_cancel;
  3031. nla_nest_end(skb, attrs);
  3032. genlmsg_end(skb, hdr);
  3033. return 0;
  3034. attr_msg_cancel:
  3035. nla_nest_cancel(skb, attrs);
  3036. genlmsg_cancel:
  3037. genlmsg_cancel(skb, hdr);
  3038. msg_cancel:
  3039. return -EMSGSIZE;
  3040. }
  3041. /* Caller should hold socket lock for the passed tipc socket. */
  3042. static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
  3043. struct netlink_callback *cb,
  3044. struct tipc_sock *tsk, u32 *last_publ)
  3045. {
  3046. int err;
  3047. struct publication *p;
  3048. if (*last_publ) {
  3049. list_for_each_entry(p, &tsk->publications, binding_sock) {
  3050. if (p->key == *last_publ)
  3051. break;
  3052. }
  3053. if (p->key != *last_publ) {
  3054. /* We never set seq or call nl_dump_check_consistent()
  3055. * this means that setting prev_seq here will cause the
  3056. * consistence check to fail in the netlink callback
  3057. * handler. Resulting in the last NLMSG_DONE message
  3058. * having the NLM_F_DUMP_INTR flag set.
  3059. */
  3060. cb->prev_seq = 1;
  3061. *last_publ = 0;
  3062. return -EPIPE;
  3063. }
  3064. } else {
  3065. p = list_first_entry(&tsk->publications, struct publication,
  3066. binding_sock);
  3067. }
  3068. list_for_each_entry_from(p, &tsk->publications, binding_sock) {
  3069. err = __tipc_nl_add_sk_publ(skb, cb, p);
  3070. if (err) {
  3071. *last_publ = p->key;
  3072. return err;
  3073. }
  3074. }
  3075. *last_publ = 0;
  3076. return 0;
  3077. }
  3078. int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
  3079. {
  3080. int err;
  3081. u32 tsk_portid = cb->args[0];
  3082. u32 last_publ = cb->args[1];
  3083. u32 done = cb->args[2];
  3084. struct net *net = sock_net(skb->sk);
  3085. struct tipc_sock *tsk;
  3086. if (!tsk_portid) {
  3087. struct nlattr **attrs;
  3088. struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
  3089. err = tipc_nlmsg_parse(cb->nlh, &attrs);
  3090. if (err)
  3091. return err;
  3092. if (!attrs[TIPC_NLA_SOCK])
  3093. return -EINVAL;
  3094. err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
  3095. attrs[TIPC_NLA_SOCK],
  3096. tipc_nl_sock_policy, NULL);
  3097. if (err)
  3098. return err;
  3099. if (!sock[TIPC_NLA_SOCK_REF])
  3100. return -EINVAL;
  3101. tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
  3102. }
  3103. if (done)
  3104. return 0;
  3105. tsk = tipc_sk_lookup(net, tsk_portid);
  3106. if (!tsk)
  3107. return -EINVAL;
  3108. lock_sock(&tsk->sk);
  3109. err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
  3110. if (!err)
  3111. done = 1;
  3112. release_sock(&tsk->sk);
  3113. sock_put(&tsk->sk);
  3114. cb->args[0] = tsk_portid;
  3115. cb->args[1] = last_publ;
  3116. cb->args[2] = done;
  3117. return skb->len;
  3118. }