socket.c 72 KB

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