socket.c 72 KB

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