socket.c 74 KB

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