socket.c 72 KB

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