socket.c 74 KB

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