socket.c 74 KB

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