socket.c 74 KB

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