socket.c 72 KB

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