socket.c 74 KB

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