socket.c 72 KB

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