socket.c 76 KB

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