socket.c 84 KB

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