socket.c 74 KB

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