socket.c 74 KB

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