socket.c 88 KB

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