socket.c 73 KB

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