socket.c 72 KB

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