socket.c 73 KB

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