socket.c 72 KB

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