socket.c 86 KB

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