socket.c 76 KB

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