socket.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012-2015, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <linux/rhashtable.h>
  37. #include "core.h"
  38. #include "name_table.h"
  39. #include "node.h"
  40. #include "link.h"
  41. #include "name_distr.h"
  42. #include "socket.h"
  43. #include "bcast.h"
  44. #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_link_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_link_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_link_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 >= 0)) {
  623. rc = dsz;
  624. break;
  625. }
  626. if (rc == -EMSGSIZE) {
  627. msg->msg_iter = save;
  628. goto new_mtu;
  629. }
  630. if (rc != -ELINKCONG)
  631. break;
  632. tipc_sk(sk)->link_cong = 1;
  633. rc = tipc_wait_for_sndmsg(sock, &timeo);
  634. if (rc)
  635. __skb_queue_purge(pktchain);
  636. } while (!rc);
  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_link_xmit(net, pktchain, dnode, tsk->portid);
  841. if (likely(rc >= 0)) {
  842. if (sock->state != SS_READY)
  843. sock->state = SS_CONNECTING;
  844. rc = dsz;
  845. break;
  846. }
  847. if (rc == -EMSGSIZE) {
  848. m->msg_iter = save;
  849. goto new_mtu;
  850. }
  851. if (rc != -ELINKCONG)
  852. break;
  853. tsk->link_cong = 1;
  854. rc = tipc_wait_for_sndmsg(sock, &timeo);
  855. if (rc)
  856. __skb_queue_purge(pktchain);
  857. } while (!rc);
  858. return rc;
  859. }
  860. static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
  861. {
  862. struct sock *sk = sock->sk;
  863. struct tipc_sock *tsk = tipc_sk(sk);
  864. DEFINE_WAIT(wait);
  865. int done;
  866. do {
  867. int err = sock_error(sk);
  868. if (err)
  869. return err;
  870. if (sock->state == SS_DISCONNECTING)
  871. return -EPIPE;
  872. else if (sock->state != SS_CONNECTED)
  873. return -ENOTCONN;
  874. if (!*timeo_p)
  875. return -EAGAIN;
  876. if (signal_pending(current))
  877. return sock_intr_errno(*timeo_p);
  878. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  879. done = sk_wait_event(sk, timeo_p,
  880. (!tsk->link_cong &&
  881. !tsk_conn_cong(tsk)) ||
  882. !tsk->connected);
  883. finish_wait(sk_sleep(sk), &wait);
  884. } while (!done);
  885. return 0;
  886. }
  887. /**
  888. * tipc_send_stream - send stream-oriented data
  889. * @sock: socket structure
  890. * @m: data to send
  891. * @dsz: total length of data to be transmitted
  892. *
  893. * Used for SOCK_STREAM data.
  894. *
  895. * Returns the number of bytes sent on success (or partial success),
  896. * or errno if no data sent
  897. */
  898. static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
  899. {
  900. struct sock *sk = sock->sk;
  901. int ret;
  902. lock_sock(sk);
  903. ret = __tipc_send_stream(sock, m, dsz);
  904. release_sock(sk);
  905. return ret;
  906. }
  907. static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
  908. {
  909. struct sock *sk = sock->sk;
  910. struct net *net = sock_net(sk);
  911. struct tipc_sock *tsk = tipc_sk(sk);
  912. struct tipc_msg *mhdr = &tsk->phdr;
  913. struct sk_buff_head *pktchain = &sk->sk_write_queue;
  914. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  915. u32 portid = tsk->portid;
  916. int rc = -EINVAL;
  917. long timeo;
  918. u32 dnode;
  919. uint mtu, send, sent = 0;
  920. struct iov_iter save;
  921. /* Handle implied connection establishment */
  922. if (unlikely(dest)) {
  923. rc = __tipc_sendmsg(sock, m, dsz);
  924. if (dsz && (dsz == rc))
  925. tsk->sent_unacked = 1;
  926. return rc;
  927. }
  928. if (dsz > (uint)INT_MAX)
  929. return -EMSGSIZE;
  930. if (unlikely(sock->state != SS_CONNECTED)) {
  931. if (sock->state == SS_DISCONNECTING)
  932. return -EPIPE;
  933. else
  934. return -ENOTCONN;
  935. }
  936. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  937. dnode = tsk_peer_node(tsk);
  938. next:
  939. save = m->msg_iter;
  940. mtu = tsk->max_pkt;
  941. send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
  942. rc = tipc_msg_build(mhdr, m, sent, send, mtu, pktchain);
  943. if (unlikely(rc < 0))
  944. return rc;
  945. do {
  946. if (likely(!tsk_conn_cong(tsk))) {
  947. rc = tipc_link_xmit(net, pktchain, dnode, portid);
  948. if (likely(!rc)) {
  949. tsk->sent_unacked++;
  950. sent += send;
  951. if (sent == dsz)
  952. break;
  953. goto next;
  954. }
  955. if (rc == -EMSGSIZE) {
  956. tsk->max_pkt = tipc_node_get_mtu(net, dnode,
  957. portid);
  958. m->msg_iter = save;
  959. goto next;
  960. }
  961. if (rc != -ELINKCONG)
  962. break;
  963. tsk->link_cong = 1;
  964. }
  965. rc = tipc_wait_for_sndpkt(sock, &timeo);
  966. if (rc)
  967. __skb_queue_purge(pktchain);
  968. } while (!rc);
  969. return sent ? sent : rc;
  970. }
  971. /**
  972. * tipc_send_packet - send a connection-oriented message
  973. * @sock: socket structure
  974. * @m: message to send
  975. * @dsz: length of data to be transmitted
  976. *
  977. * Used for SOCK_SEQPACKET messages.
  978. *
  979. * Returns the number of bytes sent on success, or errno otherwise
  980. */
  981. static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
  982. {
  983. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  984. return -EMSGSIZE;
  985. return tipc_send_stream(sock, m, dsz);
  986. }
  987. /* tipc_sk_finish_conn - complete the setup of a connection
  988. */
  989. static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
  990. u32 peer_node)
  991. {
  992. struct sock *sk = &tsk->sk;
  993. struct net *net = sock_net(sk);
  994. struct tipc_msg *msg = &tsk->phdr;
  995. msg_set_destnode(msg, peer_node);
  996. msg_set_destport(msg, peer_port);
  997. msg_set_type(msg, TIPC_CONN_MSG);
  998. msg_set_lookup_scope(msg, 0);
  999. msg_set_hdr_sz(msg, SHORT_H_SIZE);
  1000. tsk->probing_intv = CONN_PROBING_INTERVAL;
  1001. tsk->probing_state = TIPC_CONN_OK;
  1002. tsk->connected = 1;
  1003. sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
  1004. tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
  1005. tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
  1006. }
  1007. /**
  1008. * set_orig_addr - capture sender's address for received message
  1009. * @m: descriptor for message info
  1010. * @msg: received message header
  1011. *
  1012. * Note: Address is not captured if not requested by receiver.
  1013. */
  1014. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  1015. {
  1016. DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
  1017. if (addr) {
  1018. addr->family = AF_TIPC;
  1019. addr->addrtype = TIPC_ADDR_ID;
  1020. memset(&addr->addr, 0, sizeof(addr->addr));
  1021. addr->addr.id.ref = msg_origport(msg);
  1022. addr->addr.id.node = msg_orignode(msg);
  1023. addr->addr.name.domain = 0; /* could leave uninitialized */
  1024. addr->scope = 0; /* could leave uninitialized */
  1025. m->msg_namelen = sizeof(struct sockaddr_tipc);
  1026. }
  1027. }
  1028. /**
  1029. * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
  1030. * @m: descriptor for message info
  1031. * @msg: received message header
  1032. * @tsk: TIPC port associated with message
  1033. *
  1034. * Note: Ancillary data is not captured if not requested by receiver.
  1035. *
  1036. * Returns 0 if successful, otherwise errno
  1037. */
  1038. static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  1039. struct tipc_sock *tsk)
  1040. {
  1041. u32 anc_data[3];
  1042. u32 err;
  1043. u32 dest_type;
  1044. int has_name;
  1045. int res;
  1046. if (likely(m->msg_controllen == 0))
  1047. return 0;
  1048. /* Optionally capture errored message object(s) */
  1049. err = msg ? msg_errcode(msg) : 0;
  1050. if (unlikely(err)) {
  1051. anc_data[0] = err;
  1052. anc_data[1] = msg_data_sz(msg);
  1053. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  1054. if (res)
  1055. return res;
  1056. if (anc_data[1]) {
  1057. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  1058. msg_data(msg));
  1059. if (res)
  1060. return res;
  1061. }
  1062. }
  1063. /* Optionally capture message destination object */
  1064. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  1065. switch (dest_type) {
  1066. case TIPC_NAMED_MSG:
  1067. has_name = 1;
  1068. anc_data[0] = msg_nametype(msg);
  1069. anc_data[1] = msg_namelower(msg);
  1070. anc_data[2] = msg_namelower(msg);
  1071. break;
  1072. case TIPC_MCAST_MSG:
  1073. has_name = 1;
  1074. anc_data[0] = msg_nametype(msg);
  1075. anc_data[1] = msg_namelower(msg);
  1076. anc_data[2] = msg_nameupper(msg);
  1077. break;
  1078. case TIPC_CONN_MSG:
  1079. has_name = (tsk->conn_type != 0);
  1080. anc_data[0] = tsk->conn_type;
  1081. anc_data[1] = tsk->conn_instance;
  1082. anc_data[2] = tsk->conn_instance;
  1083. break;
  1084. default:
  1085. has_name = 0;
  1086. }
  1087. if (has_name) {
  1088. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  1089. if (res)
  1090. return res;
  1091. }
  1092. return 0;
  1093. }
  1094. static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
  1095. {
  1096. struct net *net = sock_net(&tsk->sk);
  1097. struct sk_buff *skb = NULL;
  1098. struct tipc_msg *msg;
  1099. u32 peer_port = tsk_peer_port(tsk);
  1100. u32 dnode = tsk_peer_node(tsk);
  1101. if (!tsk->connected)
  1102. return;
  1103. skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
  1104. dnode, tsk_own_node(tsk), peer_port,
  1105. tsk->portid, TIPC_OK);
  1106. if (!skb)
  1107. return;
  1108. msg = buf_msg(skb);
  1109. msg_set_msgcnt(msg, ack);
  1110. tipc_link_xmit_skb(net, skb, dnode, msg_link_selector(msg));
  1111. }
  1112. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  1113. {
  1114. struct sock *sk = sock->sk;
  1115. DEFINE_WAIT(wait);
  1116. long timeo = *timeop;
  1117. int err;
  1118. for (;;) {
  1119. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1120. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1121. if (sock->state == SS_DISCONNECTING) {
  1122. err = -ENOTCONN;
  1123. break;
  1124. }
  1125. release_sock(sk);
  1126. timeo = schedule_timeout(timeo);
  1127. lock_sock(sk);
  1128. }
  1129. err = 0;
  1130. if (!skb_queue_empty(&sk->sk_receive_queue))
  1131. break;
  1132. err = -EAGAIN;
  1133. if (!timeo)
  1134. break;
  1135. err = sock_intr_errno(timeo);
  1136. if (signal_pending(current))
  1137. break;
  1138. }
  1139. finish_wait(sk_sleep(sk), &wait);
  1140. *timeop = timeo;
  1141. return err;
  1142. }
  1143. /**
  1144. * tipc_recvmsg - receive packet-oriented message
  1145. * @m: descriptor for message info
  1146. * @buf_len: total size of user buffer area
  1147. * @flags: receive flags
  1148. *
  1149. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  1150. * If the complete message doesn't fit in user area, truncate it.
  1151. *
  1152. * Returns size of returned message data, errno otherwise
  1153. */
  1154. static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
  1155. int flags)
  1156. {
  1157. struct sock *sk = sock->sk;
  1158. struct tipc_sock *tsk = tipc_sk(sk);
  1159. struct sk_buff *buf;
  1160. struct tipc_msg *msg;
  1161. long timeo;
  1162. unsigned int sz;
  1163. u32 err;
  1164. int res;
  1165. /* Catch invalid receive requests */
  1166. if (unlikely(!buf_len))
  1167. return -EINVAL;
  1168. lock_sock(sk);
  1169. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1170. res = -ENOTCONN;
  1171. goto exit;
  1172. }
  1173. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1174. restart:
  1175. /* Look for a message in receive queue; wait if necessary */
  1176. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1177. if (res)
  1178. goto exit;
  1179. /* Look at first message in receive queue */
  1180. buf = skb_peek(&sk->sk_receive_queue);
  1181. msg = buf_msg(buf);
  1182. sz = msg_data_sz(msg);
  1183. err = msg_errcode(msg);
  1184. /* Discard an empty non-errored message & try again */
  1185. if ((!sz) && (!err)) {
  1186. tsk_advance_rx_queue(sk);
  1187. goto restart;
  1188. }
  1189. /* Capture sender's address (optional) */
  1190. set_orig_addr(m, msg);
  1191. /* Capture ancillary data (optional) */
  1192. res = tipc_sk_anc_data_recv(m, msg, tsk);
  1193. if (res)
  1194. goto exit;
  1195. /* Capture message data (if valid) & compute return value (always) */
  1196. if (!err) {
  1197. if (unlikely(buf_len < sz)) {
  1198. sz = buf_len;
  1199. m->msg_flags |= MSG_TRUNC;
  1200. }
  1201. res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
  1202. if (res)
  1203. goto exit;
  1204. res = sz;
  1205. } else {
  1206. if ((sock->state == SS_READY) ||
  1207. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  1208. res = 0;
  1209. else
  1210. res = -ECONNRESET;
  1211. }
  1212. /* Consume received message (optional) */
  1213. if (likely(!(flags & MSG_PEEK))) {
  1214. if ((sock->state != SS_READY) &&
  1215. (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
  1216. tipc_sk_send_ack(tsk, tsk->rcv_unacked);
  1217. tsk->rcv_unacked = 0;
  1218. }
  1219. tsk_advance_rx_queue(sk);
  1220. }
  1221. exit:
  1222. release_sock(sk);
  1223. return res;
  1224. }
  1225. /**
  1226. * tipc_recv_stream - receive stream-oriented data
  1227. * @m: descriptor for message info
  1228. * @buf_len: total size of user buffer area
  1229. * @flags: receive flags
  1230. *
  1231. * Used for SOCK_STREAM messages only. If not enough data is available
  1232. * will optionally wait for more; never truncates data.
  1233. *
  1234. * Returns size of returned message data, errno otherwise
  1235. */
  1236. static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
  1237. size_t buf_len, int flags)
  1238. {
  1239. struct sock *sk = sock->sk;
  1240. struct tipc_sock *tsk = tipc_sk(sk);
  1241. struct sk_buff *buf;
  1242. struct tipc_msg *msg;
  1243. long timeo;
  1244. unsigned int sz;
  1245. int sz_to_copy, target, needed;
  1246. int sz_copied = 0;
  1247. u32 err;
  1248. int res = 0;
  1249. /* Catch invalid receive attempts */
  1250. if (unlikely(!buf_len))
  1251. return -EINVAL;
  1252. lock_sock(sk);
  1253. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1254. res = -ENOTCONN;
  1255. goto exit;
  1256. }
  1257. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  1258. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1259. restart:
  1260. /* Look for a message in receive queue; wait if necessary */
  1261. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1262. if (res)
  1263. goto exit;
  1264. /* Look at first message in receive queue */
  1265. buf = skb_peek(&sk->sk_receive_queue);
  1266. msg = buf_msg(buf);
  1267. sz = msg_data_sz(msg);
  1268. err = msg_errcode(msg);
  1269. /* Discard an empty non-errored message & try again */
  1270. if ((!sz) && (!err)) {
  1271. tsk_advance_rx_queue(sk);
  1272. goto restart;
  1273. }
  1274. /* Optionally capture sender's address & ancillary data of first msg */
  1275. if (sz_copied == 0) {
  1276. set_orig_addr(m, msg);
  1277. res = tipc_sk_anc_data_recv(m, msg, tsk);
  1278. if (res)
  1279. goto exit;
  1280. }
  1281. /* Capture message data (if valid) & compute return value (always) */
  1282. if (!err) {
  1283. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  1284. sz -= offset;
  1285. needed = (buf_len - sz_copied);
  1286. sz_to_copy = (sz <= needed) ? sz : needed;
  1287. res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
  1288. m, sz_to_copy);
  1289. if (res)
  1290. goto exit;
  1291. sz_copied += sz_to_copy;
  1292. if (sz_to_copy < sz) {
  1293. if (!(flags & MSG_PEEK))
  1294. TIPC_SKB_CB(buf)->handle =
  1295. (void *)(unsigned long)(offset + sz_to_copy);
  1296. goto exit;
  1297. }
  1298. } else {
  1299. if (sz_copied != 0)
  1300. goto exit; /* can't add error msg to valid data */
  1301. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  1302. res = 0;
  1303. else
  1304. res = -ECONNRESET;
  1305. }
  1306. /* Consume received message (optional) */
  1307. if (likely(!(flags & MSG_PEEK))) {
  1308. if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
  1309. tipc_sk_send_ack(tsk, tsk->rcv_unacked);
  1310. tsk->rcv_unacked = 0;
  1311. }
  1312. tsk_advance_rx_queue(sk);
  1313. }
  1314. /* Loop around if more data is required */
  1315. if ((sz_copied < buf_len) && /* didn't get all requested data */
  1316. (!skb_queue_empty(&sk->sk_receive_queue) ||
  1317. (sz_copied < target)) && /* and more is ready or required */
  1318. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  1319. (!err)) /* and haven't reached a FIN */
  1320. goto restart;
  1321. exit:
  1322. release_sock(sk);
  1323. return sz_copied ? sz_copied : res;
  1324. }
  1325. /**
  1326. * tipc_write_space - wake up thread if port congestion is released
  1327. * @sk: socket
  1328. */
  1329. static void tipc_write_space(struct sock *sk)
  1330. {
  1331. struct socket_wq *wq;
  1332. rcu_read_lock();
  1333. wq = rcu_dereference(sk->sk_wq);
  1334. if (wq_has_sleeper(wq))
  1335. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1336. POLLWRNORM | POLLWRBAND);
  1337. rcu_read_unlock();
  1338. }
  1339. /**
  1340. * tipc_data_ready - wake up threads to indicate messages have been received
  1341. * @sk: socket
  1342. * @len: the length of messages
  1343. */
  1344. static void tipc_data_ready(struct sock *sk)
  1345. {
  1346. struct socket_wq *wq;
  1347. rcu_read_lock();
  1348. wq = rcu_dereference(sk->sk_wq);
  1349. if (wq_has_sleeper(wq))
  1350. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1351. POLLRDNORM | POLLRDBAND);
  1352. rcu_read_unlock();
  1353. }
  1354. /**
  1355. * filter_connect - Handle all incoming messages for a connection-based socket
  1356. * @tsk: TIPC socket
  1357. * @skb: pointer to message buffer. Set to NULL if buffer is consumed
  1358. *
  1359. * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
  1360. */
  1361. static int filter_connect(struct tipc_sock *tsk, struct sk_buff **skb)
  1362. {
  1363. struct sock *sk = &tsk->sk;
  1364. struct net *net = sock_net(sk);
  1365. struct socket *sock = sk->sk_socket;
  1366. struct tipc_msg *msg = buf_msg(*skb);
  1367. int retval = -TIPC_ERR_NO_PORT;
  1368. if (msg_mcast(msg))
  1369. return retval;
  1370. switch ((int)sock->state) {
  1371. case SS_CONNECTED:
  1372. /* Accept only connection-based messages sent by peer */
  1373. if (tsk_peer_msg(tsk, msg)) {
  1374. if (unlikely(msg_errcode(msg))) {
  1375. sock->state = SS_DISCONNECTING;
  1376. tsk->connected = 0;
  1377. /* let timer expire on it's own */
  1378. tipc_node_remove_conn(net, tsk_peer_node(tsk),
  1379. tsk->portid);
  1380. }
  1381. retval = TIPC_OK;
  1382. }
  1383. break;
  1384. case SS_CONNECTING:
  1385. /* Accept only ACK or NACK message */
  1386. if (unlikely(!msg_connected(msg)))
  1387. break;
  1388. if (unlikely(msg_errcode(msg))) {
  1389. sock->state = SS_DISCONNECTING;
  1390. sk->sk_err = ECONNREFUSED;
  1391. retval = TIPC_OK;
  1392. break;
  1393. }
  1394. if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
  1395. sock->state = SS_DISCONNECTING;
  1396. sk->sk_err = EINVAL;
  1397. retval = TIPC_OK;
  1398. break;
  1399. }
  1400. tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
  1401. msg_set_importance(&tsk->phdr, msg_importance(msg));
  1402. sock->state = SS_CONNECTED;
  1403. /* If an incoming message is an 'ACK-', it should be
  1404. * discarded here because it doesn't contain useful
  1405. * data. In addition, we should try to wake up
  1406. * connect() routine if sleeping.
  1407. */
  1408. if (msg_data_sz(msg) == 0) {
  1409. kfree_skb(*skb);
  1410. *skb = NULL;
  1411. if (waitqueue_active(sk_sleep(sk)))
  1412. wake_up_interruptible(sk_sleep(sk));
  1413. }
  1414. retval = TIPC_OK;
  1415. break;
  1416. case SS_LISTENING:
  1417. case SS_UNCONNECTED:
  1418. /* Accept only SYN message */
  1419. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1420. retval = TIPC_OK;
  1421. break;
  1422. case SS_DISCONNECTING:
  1423. break;
  1424. default:
  1425. pr_err("Unknown socket state %u\n", sock->state);
  1426. }
  1427. return retval;
  1428. }
  1429. /**
  1430. * rcvbuf_limit - get proper overload limit of socket receive queue
  1431. * @sk: socket
  1432. * @buf: message
  1433. *
  1434. * For all connection oriented messages, irrespective of importance,
  1435. * the default overload value (i.e. 67MB) is set as limit.
  1436. *
  1437. * For all connectionless messages, by default new queue limits are
  1438. * as belows:
  1439. *
  1440. * TIPC_LOW_IMPORTANCE (4 MB)
  1441. * TIPC_MEDIUM_IMPORTANCE (8 MB)
  1442. * TIPC_HIGH_IMPORTANCE (16 MB)
  1443. * TIPC_CRITICAL_IMPORTANCE (32 MB)
  1444. *
  1445. * Returns overload limit according to corresponding message importance
  1446. */
  1447. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
  1448. {
  1449. struct tipc_msg *msg = buf_msg(buf);
  1450. if (msg_connected(msg))
  1451. return sysctl_tipc_rmem[2];
  1452. return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
  1453. msg_importance(msg);
  1454. }
  1455. /**
  1456. * filter_rcv - validate incoming message
  1457. * @sk: socket
  1458. * @skb: pointer to message. Set to NULL if buffer is consumed.
  1459. *
  1460. * Enqueues message on receive queue if acceptable; optionally handles
  1461. * disconnect indication for a connected socket.
  1462. *
  1463. * Called with socket lock already taken
  1464. *
  1465. * Returns 0 (TIPC_OK) if message was ok, -TIPC error code if rejected
  1466. */
  1467. static int filter_rcv(struct sock *sk, struct sk_buff **skb)
  1468. {
  1469. struct socket *sock = sk->sk_socket;
  1470. struct tipc_sock *tsk = tipc_sk(sk);
  1471. struct tipc_msg *msg = buf_msg(*skb);
  1472. unsigned int limit = rcvbuf_limit(sk, *skb);
  1473. int rc = TIPC_OK;
  1474. if (unlikely(msg_user(msg) == CONN_MANAGER)) {
  1475. tipc_sk_proto_rcv(tsk, skb);
  1476. return TIPC_OK;
  1477. }
  1478. if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
  1479. kfree_skb(*skb);
  1480. tsk->link_cong = 0;
  1481. sk->sk_write_space(sk);
  1482. *skb = NULL;
  1483. return TIPC_OK;
  1484. }
  1485. /* Reject message if it is wrong sort of message for socket */
  1486. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1487. return -TIPC_ERR_NO_PORT;
  1488. if (sock->state == SS_READY) {
  1489. if (msg_connected(msg))
  1490. return -TIPC_ERR_NO_PORT;
  1491. } else {
  1492. rc = filter_connect(tsk, skb);
  1493. if (rc != TIPC_OK || !*skb)
  1494. return rc;
  1495. }
  1496. /* Reject message if there isn't room to queue it */
  1497. if (sk_rmem_alloc_get(sk) + (*skb)->truesize >= limit)
  1498. return -TIPC_ERR_OVERLOAD;
  1499. /* Enqueue message */
  1500. TIPC_SKB_CB(*skb)->handle = NULL;
  1501. __skb_queue_tail(&sk->sk_receive_queue, *skb);
  1502. skb_set_owner_r(*skb, sk);
  1503. sk->sk_data_ready(sk);
  1504. *skb = NULL;
  1505. return TIPC_OK;
  1506. }
  1507. /**
  1508. * tipc_backlog_rcv - handle incoming message from backlog queue
  1509. * @sk: socket
  1510. * @skb: message
  1511. *
  1512. * Caller must hold socket lock
  1513. *
  1514. * Returns 0
  1515. */
  1516. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  1517. {
  1518. int err;
  1519. atomic_t *dcnt;
  1520. u32 dnode;
  1521. struct tipc_sock *tsk = tipc_sk(sk);
  1522. struct net *net = sock_net(sk);
  1523. uint truesize = skb->truesize;
  1524. err = filter_rcv(sk, &skb);
  1525. if (likely(!skb)) {
  1526. dcnt = &tsk->dupl_rcvcnt;
  1527. if (atomic_read(dcnt) < TIPC_CONN_OVERLOAD_LIMIT)
  1528. atomic_add(truesize, dcnt);
  1529. return 0;
  1530. }
  1531. if (!err || tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode, -err))
  1532. tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
  1533. return 0;
  1534. }
  1535. /**
  1536. * tipc_sk_enqueue - extract all buffers with destination 'dport' from
  1537. * inputq and try adding them to socket or backlog queue
  1538. * @inputq: list of incoming buffers with potentially different destinations
  1539. * @sk: socket where the buffers should be enqueued
  1540. * @dport: port number for the socket
  1541. * @_skb: returned buffer to be forwarded or rejected, if applicable
  1542. *
  1543. * Caller must hold socket lock
  1544. *
  1545. * Returns TIPC_OK if all buffers enqueued, otherwise -TIPC_ERR_OVERLOAD
  1546. * or -TIPC_ERR_NO_PORT
  1547. */
  1548. static int tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
  1549. u32 dport, struct sk_buff **_skb)
  1550. {
  1551. unsigned int lim;
  1552. atomic_t *dcnt;
  1553. int err;
  1554. struct sk_buff *skb;
  1555. unsigned long time_limit = jiffies + 2;
  1556. while (skb_queue_len(inputq)) {
  1557. if (unlikely(time_after_eq(jiffies, time_limit)))
  1558. return TIPC_OK;
  1559. skb = tipc_skb_dequeue(inputq, dport);
  1560. if (unlikely(!skb))
  1561. return TIPC_OK;
  1562. if (!sock_owned_by_user(sk)) {
  1563. err = filter_rcv(sk, &skb);
  1564. if (likely(!skb))
  1565. continue;
  1566. *_skb = skb;
  1567. return err;
  1568. }
  1569. dcnt = &tipc_sk(sk)->dupl_rcvcnt;
  1570. if (sk->sk_backlog.len)
  1571. atomic_set(dcnt, 0);
  1572. lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
  1573. if (likely(!sk_add_backlog(sk, skb, lim)))
  1574. continue;
  1575. *_skb = skb;
  1576. return -TIPC_ERR_OVERLOAD;
  1577. }
  1578. return TIPC_OK;
  1579. }
  1580. /**
  1581. * tipc_sk_rcv - handle a chain of incoming buffers
  1582. * @inputq: buffer list containing the buffers
  1583. * Consumes all buffers in list until inputq is empty
  1584. * Note: may be called in multiple threads referring to the same queue
  1585. * Returns 0 if last buffer was accepted, otherwise -EHOSTUNREACH
  1586. * Only node local calls check the return value, sending single-buffer queues
  1587. */
  1588. int tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
  1589. {
  1590. u32 dnode, dport = 0;
  1591. int err;
  1592. struct sk_buff *skb;
  1593. struct tipc_sock *tsk;
  1594. struct tipc_net *tn;
  1595. struct sock *sk;
  1596. while (skb_queue_len(inputq)) {
  1597. err = -TIPC_ERR_NO_PORT;
  1598. skb = NULL;
  1599. dport = tipc_skb_peek_port(inputq, dport);
  1600. tsk = tipc_sk_lookup(net, dport);
  1601. if (likely(tsk)) {
  1602. sk = &tsk->sk;
  1603. if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
  1604. err = tipc_sk_enqueue(inputq, sk, dport, &skb);
  1605. spin_unlock_bh(&sk->sk_lock.slock);
  1606. dport = 0;
  1607. }
  1608. sock_put(sk);
  1609. } else {
  1610. skb = tipc_skb_dequeue(inputq, dport);
  1611. }
  1612. if (likely(!skb))
  1613. continue;
  1614. if (tipc_msg_lookup_dest(net, skb, &dnode, &err))
  1615. goto xmit;
  1616. if (!err) {
  1617. dnode = msg_destnode(buf_msg(skb));
  1618. goto xmit;
  1619. }
  1620. tn = net_generic(net, tipc_net_id);
  1621. if (!tipc_msg_reverse(tn->own_addr, skb, &dnode, -err))
  1622. continue;
  1623. xmit:
  1624. tipc_link_xmit_skb(net, skb, dnode, dport);
  1625. }
  1626. return err ? -EHOSTUNREACH : 0;
  1627. }
  1628. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  1629. {
  1630. struct sock *sk = sock->sk;
  1631. DEFINE_WAIT(wait);
  1632. int done;
  1633. do {
  1634. int err = sock_error(sk);
  1635. if (err)
  1636. return err;
  1637. if (!*timeo_p)
  1638. return -ETIMEDOUT;
  1639. if (signal_pending(current))
  1640. return sock_intr_errno(*timeo_p);
  1641. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1642. done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
  1643. finish_wait(sk_sleep(sk), &wait);
  1644. } while (!done);
  1645. return 0;
  1646. }
  1647. /**
  1648. * tipc_connect - establish a connection to another TIPC port
  1649. * @sock: socket structure
  1650. * @dest: socket address for destination port
  1651. * @destlen: size of socket address data structure
  1652. * @flags: file-related flags associated with socket
  1653. *
  1654. * Returns 0 on success, errno otherwise
  1655. */
  1656. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  1657. int destlen, int flags)
  1658. {
  1659. struct sock *sk = sock->sk;
  1660. struct tipc_sock *tsk = tipc_sk(sk);
  1661. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1662. struct msghdr m = {NULL,};
  1663. long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
  1664. socket_state previous;
  1665. int res = 0;
  1666. lock_sock(sk);
  1667. /* DGRAM/RDM connect(), just save the destaddr */
  1668. if (sock->state == SS_READY) {
  1669. if (dst->family == AF_UNSPEC) {
  1670. memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
  1671. tsk->connected = 0;
  1672. } else if (destlen != sizeof(struct sockaddr_tipc)) {
  1673. res = -EINVAL;
  1674. } else {
  1675. memcpy(&tsk->remote, dest, destlen);
  1676. tsk->connected = 1;
  1677. }
  1678. goto exit;
  1679. }
  1680. /*
  1681. * Reject connection attempt using multicast address
  1682. *
  1683. * Note: send_msg() validates the rest of the address fields,
  1684. * so there's no need to do it here
  1685. */
  1686. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1687. res = -EINVAL;
  1688. goto exit;
  1689. }
  1690. previous = sock->state;
  1691. switch (sock->state) {
  1692. case SS_UNCONNECTED:
  1693. /* Send a 'SYN-' to destination */
  1694. m.msg_name = dest;
  1695. m.msg_namelen = destlen;
  1696. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1697. * indicate send_msg() is never blocked.
  1698. */
  1699. if (!timeout)
  1700. m.msg_flags = MSG_DONTWAIT;
  1701. res = __tipc_sendmsg(sock, &m, 0);
  1702. if ((res < 0) && (res != -EWOULDBLOCK))
  1703. goto exit;
  1704. /* Just entered SS_CONNECTING state; the only
  1705. * difference is that return value in non-blocking
  1706. * case is EINPROGRESS, rather than EALREADY.
  1707. */
  1708. res = -EINPROGRESS;
  1709. case SS_CONNECTING:
  1710. if (previous == SS_CONNECTING)
  1711. res = -EALREADY;
  1712. if (!timeout)
  1713. goto exit;
  1714. timeout = msecs_to_jiffies(timeout);
  1715. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1716. res = tipc_wait_for_connect(sock, &timeout);
  1717. break;
  1718. case SS_CONNECTED:
  1719. res = -EISCONN;
  1720. break;
  1721. default:
  1722. res = -EINVAL;
  1723. break;
  1724. }
  1725. exit:
  1726. release_sock(sk);
  1727. return res;
  1728. }
  1729. /**
  1730. * tipc_listen - allow socket to listen for incoming connections
  1731. * @sock: socket structure
  1732. * @len: (unused)
  1733. *
  1734. * Returns 0 on success, errno otherwise
  1735. */
  1736. static int tipc_listen(struct socket *sock, int len)
  1737. {
  1738. struct sock *sk = sock->sk;
  1739. int res;
  1740. lock_sock(sk);
  1741. if (sock->state != SS_UNCONNECTED)
  1742. res = -EINVAL;
  1743. else {
  1744. sock->state = SS_LISTENING;
  1745. res = 0;
  1746. }
  1747. release_sock(sk);
  1748. return res;
  1749. }
  1750. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  1751. {
  1752. struct sock *sk = sock->sk;
  1753. DEFINE_WAIT(wait);
  1754. int err;
  1755. /* True wake-one mechanism for incoming connections: only
  1756. * one process gets woken up, not the 'whole herd'.
  1757. * Since we do not 'race & poll' for established sockets
  1758. * anymore, the common case will execute the loop only once.
  1759. */
  1760. for (;;) {
  1761. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  1762. TASK_INTERRUPTIBLE);
  1763. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1764. release_sock(sk);
  1765. timeo = schedule_timeout(timeo);
  1766. lock_sock(sk);
  1767. }
  1768. err = 0;
  1769. if (!skb_queue_empty(&sk->sk_receive_queue))
  1770. break;
  1771. err = -EINVAL;
  1772. if (sock->state != SS_LISTENING)
  1773. break;
  1774. err = -EAGAIN;
  1775. if (!timeo)
  1776. break;
  1777. err = sock_intr_errno(timeo);
  1778. if (signal_pending(current))
  1779. break;
  1780. }
  1781. finish_wait(sk_sleep(sk), &wait);
  1782. return err;
  1783. }
  1784. /**
  1785. * tipc_accept - wait for connection request
  1786. * @sock: listening socket
  1787. * @newsock: new socket that is to be connected
  1788. * @flags: file-related flags associated with socket
  1789. *
  1790. * Returns 0 on success, errno otherwise
  1791. */
  1792. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
  1793. {
  1794. struct sock *new_sk, *sk = sock->sk;
  1795. struct sk_buff *buf;
  1796. struct tipc_sock *new_tsock;
  1797. struct tipc_msg *msg;
  1798. long timeo;
  1799. int res;
  1800. lock_sock(sk);
  1801. if (sock->state != SS_LISTENING) {
  1802. res = -EINVAL;
  1803. goto exit;
  1804. }
  1805. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1806. res = tipc_wait_for_accept(sock, timeo);
  1807. if (res)
  1808. goto exit;
  1809. buf = skb_peek(&sk->sk_receive_queue);
  1810. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
  1811. if (res)
  1812. goto exit;
  1813. new_sk = new_sock->sk;
  1814. new_tsock = tipc_sk(new_sk);
  1815. msg = buf_msg(buf);
  1816. /* we lock on new_sk; but lockdep sees the lock on sk */
  1817. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1818. /*
  1819. * Reject any stray messages received by new socket
  1820. * before the socket lock was taken (very, very unlikely)
  1821. */
  1822. tsk_rej_rx_queue(new_sk);
  1823. /* Connect new socket to it's peer */
  1824. tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
  1825. new_sock->state = SS_CONNECTED;
  1826. tsk_set_importance(new_tsock, msg_importance(msg));
  1827. if (msg_named(msg)) {
  1828. new_tsock->conn_type = msg_nametype(msg);
  1829. new_tsock->conn_instance = msg_nameinst(msg);
  1830. }
  1831. /*
  1832. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1833. * Respond to 'SYN+' by queuing it on new socket.
  1834. */
  1835. if (!msg_data_sz(msg)) {
  1836. struct msghdr m = {NULL,};
  1837. tsk_advance_rx_queue(sk);
  1838. __tipc_send_stream(new_sock, &m, 0);
  1839. } else {
  1840. __skb_dequeue(&sk->sk_receive_queue);
  1841. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1842. skb_set_owner_r(buf, new_sk);
  1843. }
  1844. release_sock(new_sk);
  1845. exit:
  1846. release_sock(sk);
  1847. return res;
  1848. }
  1849. /**
  1850. * tipc_shutdown - shutdown socket connection
  1851. * @sock: socket structure
  1852. * @how: direction to close (must be SHUT_RDWR)
  1853. *
  1854. * Terminates connection (if necessary), then purges socket's receive queue.
  1855. *
  1856. * Returns 0 on success, errno otherwise
  1857. */
  1858. static int tipc_shutdown(struct socket *sock, int how)
  1859. {
  1860. struct sock *sk = sock->sk;
  1861. struct net *net = sock_net(sk);
  1862. struct tipc_sock *tsk = tipc_sk(sk);
  1863. struct sk_buff *skb;
  1864. u32 dnode;
  1865. int res;
  1866. if (how != SHUT_RDWR)
  1867. return -EINVAL;
  1868. lock_sock(sk);
  1869. switch (sock->state) {
  1870. case SS_CONNECTING:
  1871. case SS_CONNECTED:
  1872. restart:
  1873. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1874. skb = __skb_dequeue(&sk->sk_receive_queue);
  1875. if (skb) {
  1876. if (TIPC_SKB_CB(skb)->handle != NULL) {
  1877. kfree_skb(skb);
  1878. goto restart;
  1879. }
  1880. if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
  1881. TIPC_CONN_SHUTDOWN))
  1882. tipc_link_xmit_skb(net, skb, dnode,
  1883. tsk->portid);
  1884. } else {
  1885. dnode = tsk_peer_node(tsk);
  1886. skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
  1887. TIPC_CONN_MSG, SHORT_H_SIZE,
  1888. 0, dnode, tsk_own_node(tsk),
  1889. tsk_peer_port(tsk),
  1890. tsk->portid, TIPC_CONN_SHUTDOWN);
  1891. tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
  1892. }
  1893. tsk->connected = 0;
  1894. sock->state = SS_DISCONNECTING;
  1895. tipc_node_remove_conn(net, dnode, tsk->portid);
  1896. /* fall through */
  1897. case SS_DISCONNECTING:
  1898. /* Discard any unreceived messages */
  1899. __skb_queue_purge(&sk->sk_receive_queue);
  1900. /* Wake up anyone sleeping in poll */
  1901. sk->sk_state_change(sk);
  1902. res = 0;
  1903. break;
  1904. default:
  1905. res = -ENOTCONN;
  1906. }
  1907. release_sock(sk);
  1908. return res;
  1909. }
  1910. static void tipc_sk_timeout(unsigned long data)
  1911. {
  1912. struct tipc_sock *tsk = (struct tipc_sock *)data;
  1913. struct sock *sk = &tsk->sk;
  1914. struct sk_buff *skb = NULL;
  1915. u32 peer_port, peer_node;
  1916. u32 own_node = tsk_own_node(tsk);
  1917. bh_lock_sock(sk);
  1918. if (!tsk->connected) {
  1919. bh_unlock_sock(sk);
  1920. goto exit;
  1921. }
  1922. peer_port = tsk_peer_port(tsk);
  1923. peer_node = tsk_peer_node(tsk);
  1924. if (tsk->probing_state == TIPC_CONN_PROBING) {
  1925. if (!sock_owned_by_user(sk)) {
  1926. sk->sk_socket->state = SS_DISCONNECTING;
  1927. tsk->connected = 0;
  1928. tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
  1929. tsk_peer_port(tsk));
  1930. sk->sk_state_change(sk);
  1931. } else {
  1932. /* Try again later */
  1933. sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
  1934. }
  1935. } else {
  1936. skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
  1937. INT_H_SIZE, 0, peer_node, own_node,
  1938. peer_port, tsk->portid, TIPC_OK);
  1939. tsk->probing_state = TIPC_CONN_PROBING;
  1940. sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
  1941. }
  1942. bh_unlock_sock(sk);
  1943. if (skb)
  1944. tipc_link_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
  1945. exit:
  1946. sock_put(sk);
  1947. }
  1948. static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
  1949. struct tipc_name_seq const *seq)
  1950. {
  1951. struct net *net = sock_net(&tsk->sk);
  1952. struct publication *publ;
  1953. u32 key;
  1954. if (tsk->connected)
  1955. return -EINVAL;
  1956. key = tsk->portid + tsk->pub_count + 1;
  1957. if (key == tsk->portid)
  1958. return -EADDRINUSE;
  1959. publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
  1960. scope, tsk->portid, key);
  1961. if (unlikely(!publ))
  1962. return -EINVAL;
  1963. list_add(&publ->pport_list, &tsk->publications);
  1964. tsk->pub_count++;
  1965. tsk->published = 1;
  1966. return 0;
  1967. }
  1968. static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
  1969. struct tipc_name_seq const *seq)
  1970. {
  1971. struct net *net = sock_net(&tsk->sk);
  1972. struct publication *publ;
  1973. struct publication *safe;
  1974. int rc = -EINVAL;
  1975. list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
  1976. if (seq) {
  1977. if (publ->scope != scope)
  1978. continue;
  1979. if (publ->type != seq->type)
  1980. continue;
  1981. if (publ->lower != seq->lower)
  1982. continue;
  1983. if (publ->upper != seq->upper)
  1984. break;
  1985. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  1986. publ->ref, publ->key);
  1987. rc = 0;
  1988. break;
  1989. }
  1990. tipc_nametbl_withdraw(net, publ->type, publ->lower,
  1991. publ->ref, publ->key);
  1992. rc = 0;
  1993. }
  1994. if (list_empty(&tsk->publications))
  1995. tsk->published = 0;
  1996. return rc;
  1997. }
  1998. /* tipc_sk_reinit: set non-zero address in all existing sockets
  1999. * when we go from standalone to network mode.
  2000. */
  2001. void tipc_sk_reinit(struct net *net)
  2002. {
  2003. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2004. const struct bucket_table *tbl;
  2005. struct rhash_head *pos;
  2006. struct tipc_sock *tsk;
  2007. struct tipc_msg *msg;
  2008. int i;
  2009. rcu_read_lock();
  2010. tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
  2011. for (i = 0; i < tbl->size; i++) {
  2012. rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
  2013. spin_lock_bh(&tsk->sk.sk_lock.slock);
  2014. msg = &tsk->phdr;
  2015. msg_set_prevnode(msg, tn->own_addr);
  2016. msg_set_orignode(msg, tn->own_addr);
  2017. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2018. }
  2019. }
  2020. rcu_read_unlock();
  2021. }
  2022. static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
  2023. {
  2024. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2025. struct tipc_sock *tsk;
  2026. rcu_read_lock();
  2027. tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
  2028. if (tsk)
  2029. sock_hold(&tsk->sk);
  2030. rcu_read_unlock();
  2031. return tsk;
  2032. }
  2033. static int tipc_sk_insert(struct tipc_sock *tsk)
  2034. {
  2035. struct sock *sk = &tsk->sk;
  2036. struct net *net = sock_net(sk);
  2037. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2038. u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
  2039. u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
  2040. while (remaining--) {
  2041. portid++;
  2042. if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
  2043. portid = TIPC_MIN_PORT;
  2044. tsk->portid = portid;
  2045. sock_hold(&tsk->sk);
  2046. if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
  2047. tsk_rht_params))
  2048. return 0;
  2049. sock_put(&tsk->sk);
  2050. }
  2051. return -1;
  2052. }
  2053. static void tipc_sk_remove(struct tipc_sock *tsk)
  2054. {
  2055. struct sock *sk = &tsk->sk;
  2056. struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
  2057. if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
  2058. WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
  2059. __sock_put(sk);
  2060. }
  2061. }
  2062. static const struct rhashtable_params tsk_rht_params = {
  2063. .nelem_hint = 192,
  2064. .head_offset = offsetof(struct tipc_sock, node),
  2065. .key_offset = offsetof(struct tipc_sock, portid),
  2066. .key_len = sizeof(u32), /* portid */
  2067. .max_size = 1048576,
  2068. .min_size = 256,
  2069. .automatic_shrinking = true,
  2070. };
  2071. int tipc_sk_rht_init(struct net *net)
  2072. {
  2073. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2074. return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
  2075. }
  2076. void tipc_sk_rht_destroy(struct net *net)
  2077. {
  2078. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2079. /* Wait for socket readers to complete */
  2080. synchronize_net();
  2081. rhashtable_destroy(&tn->sk_rht);
  2082. }
  2083. /**
  2084. * tipc_setsockopt - set socket option
  2085. * @sock: socket structure
  2086. * @lvl: option level
  2087. * @opt: option identifier
  2088. * @ov: pointer to new option value
  2089. * @ol: length of option value
  2090. *
  2091. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  2092. * (to ease compatibility).
  2093. *
  2094. * Returns 0 on success, errno otherwise
  2095. */
  2096. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  2097. char __user *ov, unsigned int ol)
  2098. {
  2099. struct sock *sk = sock->sk;
  2100. struct tipc_sock *tsk = tipc_sk(sk);
  2101. u32 value;
  2102. int res;
  2103. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2104. return 0;
  2105. if (lvl != SOL_TIPC)
  2106. return -ENOPROTOOPT;
  2107. if (ol < sizeof(value))
  2108. return -EINVAL;
  2109. res = get_user(value, (u32 __user *)ov);
  2110. if (res)
  2111. return res;
  2112. lock_sock(sk);
  2113. switch (opt) {
  2114. case TIPC_IMPORTANCE:
  2115. res = tsk_set_importance(tsk, value);
  2116. break;
  2117. case TIPC_SRC_DROPPABLE:
  2118. if (sock->type != SOCK_STREAM)
  2119. tsk_set_unreliable(tsk, value);
  2120. else
  2121. res = -ENOPROTOOPT;
  2122. break;
  2123. case TIPC_DEST_DROPPABLE:
  2124. tsk_set_unreturnable(tsk, value);
  2125. break;
  2126. case TIPC_CONN_TIMEOUT:
  2127. tipc_sk(sk)->conn_timeout = value;
  2128. /* no need to set "res", since already 0 at this point */
  2129. break;
  2130. default:
  2131. res = -EINVAL;
  2132. }
  2133. release_sock(sk);
  2134. return res;
  2135. }
  2136. /**
  2137. * tipc_getsockopt - get socket option
  2138. * @sock: socket structure
  2139. * @lvl: option level
  2140. * @opt: option identifier
  2141. * @ov: receptacle for option value
  2142. * @ol: receptacle for length of option value
  2143. *
  2144. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  2145. * (to ease compatibility).
  2146. *
  2147. * Returns 0 on success, errno otherwise
  2148. */
  2149. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  2150. char __user *ov, int __user *ol)
  2151. {
  2152. struct sock *sk = sock->sk;
  2153. struct tipc_sock *tsk = tipc_sk(sk);
  2154. int len;
  2155. u32 value;
  2156. int res;
  2157. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  2158. return put_user(0, ol);
  2159. if (lvl != SOL_TIPC)
  2160. return -ENOPROTOOPT;
  2161. res = get_user(len, ol);
  2162. if (res)
  2163. return res;
  2164. lock_sock(sk);
  2165. switch (opt) {
  2166. case TIPC_IMPORTANCE:
  2167. value = tsk_importance(tsk);
  2168. break;
  2169. case TIPC_SRC_DROPPABLE:
  2170. value = tsk_unreliable(tsk);
  2171. break;
  2172. case TIPC_DEST_DROPPABLE:
  2173. value = tsk_unreturnable(tsk);
  2174. break;
  2175. case TIPC_CONN_TIMEOUT:
  2176. value = tsk->conn_timeout;
  2177. /* no need to set "res", since already 0 at this point */
  2178. break;
  2179. case TIPC_NODE_RECVQ_DEPTH:
  2180. value = 0; /* was tipc_queue_size, now obsolete */
  2181. break;
  2182. case TIPC_SOCK_RECVQ_DEPTH:
  2183. value = skb_queue_len(&sk->sk_receive_queue);
  2184. break;
  2185. default:
  2186. res = -EINVAL;
  2187. }
  2188. release_sock(sk);
  2189. if (res)
  2190. return res; /* "get" failed */
  2191. if (len < sizeof(value))
  2192. return -EINVAL;
  2193. if (copy_to_user(ov, &value, sizeof(value)))
  2194. return -EFAULT;
  2195. return put_user(sizeof(value), ol);
  2196. }
  2197. static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  2198. {
  2199. struct sock *sk = sock->sk;
  2200. struct tipc_sioc_ln_req lnr;
  2201. void __user *argp = (void __user *)arg;
  2202. switch (cmd) {
  2203. case SIOCGETLINKNAME:
  2204. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  2205. return -EFAULT;
  2206. if (!tipc_node_get_linkname(sock_net(sk),
  2207. lnr.bearer_id & 0xffff, lnr.peer,
  2208. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  2209. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  2210. return -EFAULT;
  2211. return 0;
  2212. }
  2213. return -EADDRNOTAVAIL;
  2214. default:
  2215. return -ENOIOCTLCMD;
  2216. }
  2217. }
  2218. /* Protocol switches for the various types of TIPC sockets */
  2219. static const struct proto_ops msg_ops = {
  2220. .owner = THIS_MODULE,
  2221. .family = AF_TIPC,
  2222. .release = tipc_release,
  2223. .bind = tipc_bind,
  2224. .connect = tipc_connect,
  2225. .socketpair = sock_no_socketpair,
  2226. .accept = sock_no_accept,
  2227. .getname = tipc_getname,
  2228. .poll = tipc_poll,
  2229. .ioctl = tipc_ioctl,
  2230. .listen = sock_no_listen,
  2231. .shutdown = tipc_shutdown,
  2232. .setsockopt = tipc_setsockopt,
  2233. .getsockopt = tipc_getsockopt,
  2234. .sendmsg = tipc_sendmsg,
  2235. .recvmsg = tipc_recvmsg,
  2236. .mmap = sock_no_mmap,
  2237. .sendpage = sock_no_sendpage
  2238. };
  2239. static const struct proto_ops packet_ops = {
  2240. .owner = THIS_MODULE,
  2241. .family = AF_TIPC,
  2242. .release = tipc_release,
  2243. .bind = tipc_bind,
  2244. .connect = tipc_connect,
  2245. .socketpair = sock_no_socketpair,
  2246. .accept = tipc_accept,
  2247. .getname = tipc_getname,
  2248. .poll = tipc_poll,
  2249. .ioctl = tipc_ioctl,
  2250. .listen = tipc_listen,
  2251. .shutdown = tipc_shutdown,
  2252. .setsockopt = tipc_setsockopt,
  2253. .getsockopt = tipc_getsockopt,
  2254. .sendmsg = tipc_send_packet,
  2255. .recvmsg = tipc_recvmsg,
  2256. .mmap = sock_no_mmap,
  2257. .sendpage = sock_no_sendpage
  2258. };
  2259. static const struct proto_ops stream_ops = {
  2260. .owner = THIS_MODULE,
  2261. .family = AF_TIPC,
  2262. .release = tipc_release,
  2263. .bind = tipc_bind,
  2264. .connect = tipc_connect,
  2265. .socketpair = sock_no_socketpair,
  2266. .accept = tipc_accept,
  2267. .getname = tipc_getname,
  2268. .poll = tipc_poll,
  2269. .ioctl = tipc_ioctl,
  2270. .listen = tipc_listen,
  2271. .shutdown = tipc_shutdown,
  2272. .setsockopt = tipc_setsockopt,
  2273. .getsockopt = tipc_getsockopt,
  2274. .sendmsg = tipc_send_stream,
  2275. .recvmsg = tipc_recv_stream,
  2276. .mmap = sock_no_mmap,
  2277. .sendpage = sock_no_sendpage
  2278. };
  2279. static const struct net_proto_family tipc_family_ops = {
  2280. .owner = THIS_MODULE,
  2281. .family = AF_TIPC,
  2282. .create = tipc_sk_create
  2283. };
  2284. static struct proto tipc_proto = {
  2285. .name = "TIPC",
  2286. .owner = THIS_MODULE,
  2287. .obj_size = sizeof(struct tipc_sock),
  2288. .sysctl_rmem = sysctl_tipc_rmem
  2289. };
  2290. /**
  2291. * tipc_socket_init - initialize TIPC socket interface
  2292. *
  2293. * Returns 0 on success, errno otherwise
  2294. */
  2295. int tipc_socket_init(void)
  2296. {
  2297. int res;
  2298. res = proto_register(&tipc_proto, 1);
  2299. if (res) {
  2300. pr_err("Failed to register TIPC protocol type\n");
  2301. goto out;
  2302. }
  2303. res = sock_register(&tipc_family_ops);
  2304. if (res) {
  2305. pr_err("Failed to register TIPC socket type\n");
  2306. proto_unregister(&tipc_proto);
  2307. goto out;
  2308. }
  2309. out:
  2310. return res;
  2311. }
  2312. /**
  2313. * tipc_socket_stop - stop TIPC socket interface
  2314. */
  2315. void tipc_socket_stop(void)
  2316. {
  2317. sock_unregister(tipc_family_ops.family);
  2318. proto_unregister(&tipc_proto);
  2319. }
  2320. /* Caller should hold socket lock for the passed tipc socket. */
  2321. static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
  2322. {
  2323. u32 peer_node;
  2324. u32 peer_port;
  2325. struct nlattr *nest;
  2326. peer_node = tsk_peer_node(tsk);
  2327. peer_port = tsk_peer_port(tsk);
  2328. nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
  2329. if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
  2330. goto msg_full;
  2331. if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
  2332. goto msg_full;
  2333. if (tsk->conn_type != 0) {
  2334. if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
  2335. goto msg_full;
  2336. if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
  2337. goto msg_full;
  2338. if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
  2339. goto msg_full;
  2340. }
  2341. nla_nest_end(skb, nest);
  2342. return 0;
  2343. msg_full:
  2344. nla_nest_cancel(skb, nest);
  2345. return -EMSGSIZE;
  2346. }
  2347. /* Caller should hold socket lock for the passed tipc socket. */
  2348. static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
  2349. struct tipc_sock *tsk)
  2350. {
  2351. int err;
  2352. void *hdr;
  2353. struct nlattr *attrs;
  2354. struct net *net = sock_net(skb->sk);
  2355. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2356. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2357. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
  2358. if (!hdr)
  2359. goto msg_cancel;
  2360. attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
  2361. if (!attrs)
  2362. goto genlmsg_cancel;
  2363. if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
  2364. goto attr_msg_cancel;
  2365. if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
  2366. goto attr_msg_cancel;
  2367. if (tsk->connected) {
  2368. err = __tipc_nl_add_sk_con(skb, tsk);
  2369. if (err)
  2370. goto attr_msg_cancel;
  2371. } else if (!list_empty(&tsk->publications)) {
  2372. if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
  2373. goto attr_msg_cancel;
  2374. }
  2375. nla_nest_end(skb, attrs);
  2376. genlmsg_end(skb, hdr);
  2377. return 0;
  2378. attr_msg_cancel:
  2379. nla_nest_cancel(skb, attrs);
  2380. genlmsg_cancel:
  2381. genlmsg_cancel(skb, hdr);
  2382. msg_cancel:
  2383. return -EMSGSIZE;
  2384. }
  2385. int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2386. {
  2387. int err;
  2388. struct tipc_sock *tsk;
  2389. const struct bucket_table *tbl;
  2390. struct rhash_head *pos;
  2391. struct net *net = sock_net(skb->sk);
  2392. struct tipc_net *tn = net_generic(net, tipc_net_id);
  2393. u32 tbl_id = cb->args[0];
  2394. u32 prev_portid = cb->args[1];
  2395. rcu_read_lock();
  2396. tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
  2397. for (; tbl_id < tbl->size; tbl_id++) {
  2398. rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
  2399. spin_lock_bh(&tsk->sk.sk_lock.slock);
  2400. if (prev_portid && prev_portid != tsk->portid) {
  2401. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2402. continue;
  2403. }
  2404. err = __tipc_nl_add_sk(skb, cb, tsk);
  2405. if (err) {
  2406. prev_portid = tsk->portid;
  2407. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2408. goto out;
  2409. }
  2410. prev_portid = 0;
  2411. spin_unlock_bh(&tsk->sk.sk_lock.slock);
  2412. }
  2413. }
  2414. out:
  2415. rcu_read_unlock();
  2416. cb->args[0] = tbl_id;
  2417. cb->args[1] = prev_portid;
  2418. return skb->len;
  2419. }
  2420. /* Caller should hold socket lock for the passed tipc socket. */
  2421. static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
  2422. struct netlink_callback *cb,
  2423. struct publication *publ)
  2424. {
  2425. void *hdr;
  2426. struct nlattr *attrs;
  2427. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2428. &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
  2429. if (!hdr)
  2430. goto msg_cancel;
  2431. attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
  2432. if (!attrs)
  2433. goto genlmsg_cancel;
  2434. if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
  2435. goto attr_msg_cancel;
  2436. if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
  2437. goto attr_msg_cancel;
  2438. if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
  2439. goto attr_msg_cancel;
  2440. if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
  2441. goto attr_msg_cancel;
  2442. nla_nest_end(skb, attrs);
  2443. genlmsg_end(skb, hdr);
  2444. return 0;
  2445. attr_msg_cancel:
  2446. nla_nest_cancel(skb, attrs);
  2447. genlmsg_cancel:
  2448. genlmsg_cancel(skb, hdr);
  2449. msg_cancel:
  2450. return -EMSGSIZE;
  2451. }
  2452. /* Caller should hold socket lock for the passed tipc socket. */
  2453. static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
  2454. struct netlink_callback *cb,
  2455. struct tipc_sock *tsk, u32 *last_publ)
  2456. {
  2457. int err;
  2458. struct publication *p;
  2459. if (*last_publ) {
  2460. list_for_each_entry(p, &tsk->publications, pport_list) {
  2461. if (p->key == *last_publ)
  2462. break;
  2463. }
  2464. if (p->key != *last_publ) {
  2465. /* We never set seq or call nl_dump_check_consistent()
  2466. * this means that setting prev_seq here will cause the
  2467. * consistence check to fail in the netlink callback
  2468. * handler. Resulting in the last NLMSG_DONE message
  2469. * having the NLM_F_DUMP_INTR flag set.
  2470. */
  2471. cb->prev_seq = 1;
  2472. *last_publ = 0;
  2473. return -EPIPE;
  2474. }
  2475. } else {
  2476. p = list_first_entry(&tsk->publications, struct publication,
  2477. pport_list);
  2478. }
  2479. list_for_each_entry_from(p, &tsk->publications, pport_list) {
  2480. err = __tipc_nl_add_sk_publ(skb, cb, p);
  2481. if (err) {
  2482. *last_publ = p->key;
  2483. return err;
  2484. }
  2485. }
  2486. *last_publ = 0;
  2487. return 0;
  2488. }
  2489. int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
  2490. {
  2491. int err;
  2492. u32 tsk_portid = cb->args[0];
  2493. u32 last_publ = cb->args[1];
  2494. u32 done = cb->args[2];
  2495. struct net *net = sock_net(skb->sk);
  2496. struct tipc_sock *tsk;
  2497. if (!tsk_portid) {
  2498. struct nlattr **attrs;
  2499. struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
  2500. err = tipc_nlmsg_parse(cb->nlh, &attrs);
  2501. if (err)
  2502. return err;
  2503. err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
  2504. attrs[TIPC_NLA_SOCK],
  2505. tipc_nl_sock_policy);
  2506. if (err)
  2507. return err;
  2508. if (!sock[TIPC_NLA_SOCK_REF])
  2509. return -EINVAL;
  2510. tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
  2511. }
  2512. if (done)
  2513. return 0;
  2514. tsk = tipc_sk_lookup(net, tsk_portid);
  2515. if (!tsk)
  2516. return -EINVAL;
  2517. lock_sock(&tsk->sk);
  2518. err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
  2519. if (!err)
  2520. done = 1;
  2521. release_sock(&tsk->sk);
  2522. sock_put(&tsk->sk);
  2523. cb->args[0] = tsk_portid;
  2524. cb->args[1] = last_publ;
  2525. cb->args[2] = done;
  2526. return skb->len;
  2527. }