socket.c 72 KB

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