socket.c 73 KB

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