socket.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "port.h"
  38. #include "name_table.h"
  39. #include "node.h"
  40. #include "link.h"
  41. #include <linux/export.h>
  42. #include "link.h"
  43. #define SS_LISTENING -1 /* socket is listening */
  44. #define SS_READY -2 /* socket is connectionless */
  45. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  46. #define TIPC_FWD_MSG 1
  47. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
  48. static void tipc_data_ready(struct sock *sk);
  49. static void tipc_write_space(struct sock *sk);
  50. static int tipc_release(struct socket *sock);
  51. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
  52. static const struct proto_ops packet_ops;
  53. static const struct proto_ops stream_ops;
  54. static const struct proto_ops msg_ops;
  55. static struct proto tipc_proto;
  56. static struct proto tipc_proto_kern;
  57. /*
  58. * Revised TIPC socket locking policy:
  59. *
  60. * Most socket operations take the standard socket lock when they start
  61. * and hold it until they finish (or until they need to sleep). Acquiring
  62. * this lock grants the owner exclusive access to the fields of the socket
  63. * data structures, with the exception of the backlog queue. A few socket
  64. * operations can be done without taking the socket lock because they only
  65. * read socket information that never changes during the life of the socket.
  66. *
  67. * Socket operations may acquire the lock for the associated TIPC port if they
  68. * need to perform an operation on the port. If any routine needs to acquire
  69. * both the socket lock and the port lock it must take the socket lock first
  70. * to avoid the risk of deadlock.
  71. *
  72. * The dispatcher handling incoming messages cannot grab the socket lock in
  73. * the standard fashion, since invoked it runs at the BH level and cannot block.
  74. * Instead, it checks to see if the socket lock is currently owned by someone,
  75. * and either handles the message itself or adds it to the socket's backlog
  76. * queue; in the latter case the queued message is processed once the process
  77. * owning the socket lock releases it.
  78. *
  79. * NOTE: Releasing the socket lock while an operation is sleeping overcomes
  80. * the problem of a blocked socket operation preventing any other operations
  81. * from occurring. However, applications must be careful if they have
  82. * multiple threads trying to send (or receive) on the same socket, as these
  83. * operations might interfere with each other. For example, doing a connect
  84. * and a receive at the same time might allow the receive to consume the
  85. * ACK message meant for the connect. While additional work could be done
  86. * to try and overcome this, it doesn't seem to be worthwhile at the present.
  87. *
  88. * NOTE: Releasing the socket lock while an operation is sleeping also ensures
  89. * that another operation that must be performed in a non-blocking manner is
  90. * not delayed for very long because the lock has already been taken.
  91. *
  92. * NOTE: This code assumes that certain fields of a port/socket pair are
  93. * constant over its lifetime; such fields can be examined without taking
  94. * the socket lock and/or port lock, and do not need to be re-read even
  95. * after resuming processing after waiting. These fields include:
  96. * - socket type
  97. * - pointer to socket sk structure (aka tipc_sock structure)
  98. * - pointer to port structure
  99. * - port reference
  100. */
  101. #include "socket.h"
  102. /**
  103. * advance_rx_queue - discard first buffer in socket receive queue
  104. *
  105. * Caller must hold socket lock
  106. */
  107. static void advance_rx_queue(struct sock *sk)
  108. {
  109. kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
  110. }
  111. /**
  112. * reject_rx_queue - reject all buffers in socket receive queue
  113. *
  114. * Caller must hold socket lock
  115. */
  116. static void reject_rx_queue(struct sock *sk)
  117. {
  118. struct sk_buff *buf;
  119. u32 dnode;
  120. while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
  121. if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
  122. tipc_link_xmit2(buf, dnode, 0);
  123. }
  124. }
  125. /**
  126. * tipc_sk_create - create a TIPC socket
  127. * @net: network namespace (must be default network)
  128. * @sock: pre-allocated socket structure
  129. * @protocol: protocol indicator (must be 0)
  130. * @kern: caused by kernel or by userspace?
  131. *
  132. * This routine creates additional data structures used by the TIPC socket,
  133. * initializes them, and links them together.
  134. *
  135. * Returns 0 on success, errno otherwise
  136. */
  137. static int tipc_sk_create(struct net *net, struct socket *sock,
  138. int protocol, int kern)
  139. {
  140. const struct proto_ops *ops;
  141. socket_state state;
  142. struct sock *sk;
  143. struct tipc_sock *tsk;
  144. struct tipc_port *port;
  145. u32 ref;
  146. /* Validate arguments */
  147. if (unlikely(protocol != 0))
  148. return -EPROTONOSUPPORT;
  149. switch (sock->type) {
  150. case SOCK_STREAM:
  151. ops = &stream_ops;
  152. state = SS_UNCONNECTED;
  153. break;
  154. case SOCK_SEQPACKET:
  155. ops = &packet_ops;
  156. state = SS_UNCONNECTED;
  157. break;
  158. case SOCK_DGRAM:
  159. case SOCK_RDM:
  160. ops = &msg_ops;
  161. state = SS_READY;
  162. break;
  163. default:
  164. return -EPROTOTYPE;
  165. }
  166. /* Allocate socket's protocol area */
  167. if (!kern)
  168. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
  169. else
  170. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
  171. if (sk == NULL)
  172. return -ENOMEM;
  173. tsk = tipc_sk(sk);
  174. port = &tsk->port;
  175. ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
  176. if (!ref) {
  177. pr_warn("Socket registration failed, ref. table exhausted\n");
  178. sk_free(sk);
  179. return -ENOMEM;
  180. }
  181. /* Finish initializing socket data structures */
  182. sock->ops = ops;
  183. sock->state = state;
  184. sock_init_data(sock, sk);
  185. sk->sk_backlog_rcv = tipc_backlog_rcv;
  186. sk->sk_rcvbuf = sysctl_tipc_rmem[1];
  187. sk->sk_data_ready = tipc_data_ready;
  188. sk->sk_write_space = tipc_write_space;
  189. tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
  190. tsk->port.sent = 0;
  191. atomic_set(&tsk->dupl_rcvcnt, 0);
  192. tipc_port_unlock(port);
  193. if (sock->state == SS_READY) {
  194. tipc_port_set_unreturnable(port, true);
  195. if (sock->type == SOCK_DGRAM)
  196. tipc_port_set_unreliable(port, true);
  197. }
  198. return 0;
  199. }
  200. /**
  201. * tipc_sock_create_local - create TIPC socket from inside TIPC module
  202. * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
  203. *
  204. * We cannot use sock_creat_kern here because it bumps module user count.
  205. * Since socket owner and creator is the same module we must make sure
  206. * that module count remains zero for module local sockets, otherwise
  207. * we cannot do rmmod.
  208. *
  209. * Returns 0 on success, errno otherwise
  210. */
  211. int tipc_sock_create_local(int type, struct socket **res)
  212. {
  213. int rc;
  214. rc = sock_create_lite(AF_TIPC, type, 0, res);
  215. if (rc < 0) {
  216. pr_err("Failed to create kernel socket\n");
  217. return rc;
  218. }
  219. tipc_sk_create(&init_net, *res, 0, 1);
  220. return 0;
  221. }
  222. /**
  223. * tipc_sock_release_local - release socket created by tipc_sock_create_local
  224. * @sock: the socket to be released.
  225. *
  226. * Module reference count is not incremented when such sockets are created,
  227. * so we must keep it from being decremented when they are released.
  228. */
  229. void tipc_sock_release_local(struct socket *sock)
  230. {
  231. tipc_release(sock);
  232. sock->ops = NULL;
  233. sock_release(sock);
  234. }
  235. /**
  236. * tipc_sock_accept_local - accept a connection on a socket created
  237. * with tipc_sock_create_local. Use this function to avoid that
  238. * module reference count is inadvertently incremented.
  239. *
  240. * @sock: the accepting socket
  241. * @newsock: reference to the new socket to be created
  242. * @flags: socket flags
  243. */
  244. int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
  245. int flags)
  246. {
  247. struct sock *sk = sock->sk;
  248. int ret;
  249. ret = sock_create_lite(sk->sk_family, sk->sk_type,
  250. sk->sk_protocol, newsock);
  251. if (ret < 0)
  252. return ret;
  253. ret = tipc_accept(sock, *newsock, flags);
  254. if (ret < 0) {
  255. sock_release(*newsock);
  256. return ret;
  257. }
  258. (*newsock)->ops = sock->ops;
  259. return ret;
  260. }
  261. /**
  262. * tipc_release - destroy a TIPC socket
  263. * @sock: socket to destroy
  264. *
  265. * This routine cleans up any messages that are still queued on the socket.
  266. * For DGRAM and RDM socket types, all queued messages are rejected.
  267. * For SEQPACKET and STREAM socket types, the first message is rejected
  268. * and any others are discarded. (If the first message on a STREAM socket
  269. * is partially-read, it is discarded and the next one is rejected instead.)
  270. *
  271. * NOTE: Rejected messages are not necessarily returned to the sender! They
  272. * are returned or discarded according to the "destination droppable" setting
  273. * specified for the message by the sender.
  274. *
  275. * Returns 0 on success, errno otherwise
  276. */
  277. static int tipc_release(struct socket *sock)
  278. {
  279. struct sock *sk = sock->sk;
  280. struct tipc_sock *tsk;
  281. struct tipc_port *port;
  282. struct sk_buff *buf;
  283. u32 dnode;
  284. /*
  285. * Exit if socket isn't fully initialized (occurs when a failed accept()
  286. * releases a pre-allocated child socket that was never used)
  287. */
  288. if (sk == NULL)
  289. return 0;
  290. tsk = tipc_sk(sk);
  291. port = &tsk->port;
  292. lock_sock(sk);
  293. /*
  294. * Reject all unreceived messages, except on an active connection
  295. * (which disconnects locally & sends a 'FIN+' to peer)
  296. */
  297. while (sock->state != SS_DISCONNECTING) {
  298. buf = __skb_dequeue(&sk->sk_receive_queue);
  299. if (buf == NULL)
  300. break;
  301. if (TIPC_SKB_CB(buf)->handle != NULL)
  302. kfree_skb(buf);
  303. else {
  304. if ((sock->state == SS_CONNECTING) ||
  305. (sock->state == SS_CONNECTED)) {
  306. sock->state = SS_DISCONNECTING;
  307. tipc_port_disconnect(port->ref);
  308. }
  309. if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
  310. tipc_link_xmit2(buf, dnode, 0);
  311. }
  312. }
  313. /* Destroy TIPC port; also disconnects an active connection and
  314. * sends a 'FIN-' to peer.
  315. */
  316. tipc_port_destroy(port);
  317. /* Discard any remaining (connection-based) messages in receive queue */
  318. __skb_queue_purge(&sk->sk_receive_queue);
  319. /* Reject any messages that accumulated in backlog queue */
  320. sock->state = SS_DISCONNECTING;
  321. release_sock(sk);
  322. sock_put(sk);
  323. sock->sk = NULL;
  324. return 0;
  325. }
  326. /**
  327. * tipc_bind - associate or disassocate TIPC name(s) with a socket
  328. * @sock: socket structure
  329. * @uaddr: socket address describing name(s) and desired operation
  330. * @uaddr_len: size of socket address data structure
  331. *
  332. * Name and name sequence binding is indicated using a positive scope value;
  333. * a negative scope value unbinds the specified name. Specifying no name
  334. * (i.e. a socket address length of 0) unbinds all names from the socket.
  335. *
  336. * Returns 0 on success, errno otherwise
  337. *
  338. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  339. * access any non-constant socket information.
  340. */
  341. static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
  342. int uaddr_len)
  343. {
  344. struct sock *sk = sock->sk;
  345. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  346. struct tipc_sock *tsk = tipc_sk(sk);
  347. int res = -EINVAL;
  348. lock_sock(sk);
  349. if (unlikely(!uaddr_len)) {
  350. res = tipc_withdraw(&tsk->port, 0, NULL);
  351. goto exit;
  352. }
  353. if (uaddr_len < sizeof(struct sockaddr_tipc)) {
  354. res = -EINVAL;
  355. goto exit;
  356. }
  357. if (addr->family != AF_TIPC) {
  358. res = -EAFNOSUPPORT;
  359. goto exit;
  360. }
  361. if (addr->addrtype == TIPC_ADDR_NAME)
  362. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  363. else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
  364. res = -EAFNOSUPPORT;
  365. goto exit;
  366. }
  367. if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
  368. (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
  369. (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
  370. res = -EACCES;
  371. goto exit;
  372. }
  373. res = (addr->scope > 0) ?
  374. tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
  375. tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
  376. exit:
  377. release_sock(sk);
  378. return res;
  379. }
  380. /**
  381. * tipc_getname - get port ID of socket or peer socket
  382. * @sock: socket structure
  383. * @uaddr: area for returned socket address
  384. * @uaddr_len: area for returned length of socket address
  385. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  386. *
  387. * Returns 0 on success, errno otherwise
  388. *
  389. * NOTE: This routine doesn't need to take the socket lock since it only
  390. * accesses socket information that is unchanging (or which changes in
  391. * a completely predictable manner).
  392. */
  393. static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
  394. int *uaddr_len, int peer)
  395. {
  396. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  397. struct tipc_sock *tsk = tipc_sk(sock->sk);
  398. memset(addr, 0, sizeof(*addr));
  399. if (peer) {
  400. if ((sock->state != SS_CONNECTED) &&
  401. ((peer != 2) || (sock->state != SS_DISCONNECTING)))
  402. return -ENOTCONN;
  403. addr->addr.id.ref = tipc_port_peerport(&tsk->port);
  404. addr->addr.id.node = tipc_port_peernode(&tsk->port);
  405. } else {
  406. addr->addr.id.ref = tsk->port.ref;
  407. addr->addr.id.node = tipc_own_addr;
  408. }
  409. *uaddr_len = sizeof(*addr);
  410. addr->addrtype = TIPC_ADDR_ID;
  411. addr->family = AF_TIPC;
  412. addr->scope = 0;
  413. addr->addr.name.domain = 0;
  414. return 0;
  415. }
  416. /**
  417. * tipc_poll - read and possibly block on pollmask
  418. * @file: file structure associated with the socket
  419. * @sock: socket for which to calculate the poll bits
  420. * @wait: ???
  421. *
  422. * Returns pollmask value
  423. *
  424. * COMMENTARY:
  425. * It appears that the usual socket locking mechanisms are not useful here
  426. * since the pollmask info is potentially out-of-date the moment this routine
  427. * exits. TCP and other protocols seem to rely on higher level poll routines
  428. * to handle any preventable race conditions, so TIPC will do the same ...
  429. *
  430. * TIPC sets the returned events as follows:
  431. *
  432. * socket state flags set
  433. * ------------ ---------
  434. * unconnected no read flags
  435. * POLLOUT if port is not congested
  436. *
  437. * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
  438. * no write flags
  439. *
  440. * connected POLLIN/POLLRDNORM if data in rx queue
  441. * POLLOUT if port is not congested
  442. *
  443. * disconnecting POLLIN/POLLRDNORM/POLLHUP
  444. * no write flags
  445. *
  446. * listening POLLIN if SYN in rx queue
  447. * no write flags
  448. *
  449. * ready POLLIN/POLLRDNORM if data in rx queue
  450. * [connectionless] POLLOUT (since port cannot be congested)
  451. *
  452. * IMPORTANT: The fact that a read or write operation is indicated does NOT
  453. * imply that the operation will succeed, merely that it should be performed
  454. * and will not block.
  455. */
  456. static unsigned int tipc_poll(struct file *file, struct socket *sock,
  457. poll_table *wait)
  458. {
  459. struct sock *sk = sock->sk;
  460. struct tipc_sock *tsk = tipc_sk(sk);
  461. u32 mask = 0;
  462. sock_poll_wait(file, sk_sleep(sk), wait);
  463. switch ((int)sock->state) {
  464. case SS_UNCONNECTED:
  465. if (!tsk->port.congested)
  466. mask |= POLLOUT;
  467. break;
  468. case SS_READY:
  469. case SS_CONNECTED:
  470. if (!tsk->port.congested)
  471. mask |= POLLOUT;
  472. /* fall thru' */
  473. case SS_CONNECTING:
  474. case SS_LISTENING:
  475. if (!skb_queue_empty(&sk->sk_receive_queue))
  476. mask |= (POLLIN | POLLRDNORM);
  477. break;
  478. case SS_DISCONNECTING:
  479. mask = (POLLIN | POLLRDNORM | POLLHUP);
  480. break;
  481. }
  482. return mask;
  483. }
  484. /**
  485. * tipc_sk_proto_rcv - receive a connection mng protocol message
  486. * @tsk: receiving socket
  487. * @dnode: node to send response message to, if any
  488. * @buf: buffer containing protocol message
  489. * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
  490. * (CONN_PROBE_REPLY) message should be forwarded.
  491. */
  492. int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode, struct sk_buff *buf)
  493. {
  494. struct tipc_msg *msg = buf_msg(buf);
  495. struct tipc_port *port = &tsk->port;
  496. int wakeable;
  497. /* Ignore if connection cannot be validated: */
  498. if (!port->connected || !tipc_port_peer_msg(port, msg))
  499. goto exit;
  500. port->probing_state = TIPC_CONN_OK;
  501. if (msg_type(msg) == CONN_ACK) {
  502. wakeable = tipc_port_congested(port) && port->congested;
  503. port->acked += msg_msgcnt(msg);
  504. if (!tipc_port_congested(port)) {
  505. port->congested = 0;
  506. if (wakeable)
  507. tipc_port_wakeup(port);
  508. }
  509. } else if (msg_type(msg) == CONN_PROBE) {
  510. if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
  511. return TIPC_OK;
  512. msg_set_type(msg, CONN_PROBE_REPLY);
  513. return TIPC_FWD_MSG;
  514. }
  515. /* Do nothing if msg_type() == CONN_PROBE_REPLY */
  516. exit:
  517. kfree_skb(buf);
  518. return TIPC_OK;
  519. }
  520. /**
  521. * dest_name_check - verify user is permitted to send to specified port name
  522. * @dest: destination address
  523. * @m: descriptor for message to be sent
  524. *
  525. * Prevents restricted configuration commands from being issued by
  526. * unauthorized users.
  527. *
  528. * Returns 0 if permission is granted, otherwise errno
  529. */
  530. static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
  531. {
  532. struct tipc_cfg_msg_hdr hdr;
  533. if (unlikely(dest->addrtype == TIPC_ADDR_ID))
  534. return 0;
  535. if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
  536. return 0;
  537. if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
  538. return 0;
  539. if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
  540. return -EACCES;
  541. if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
  542. return -EMSGSIZE;
  543. if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
  544. return -EFAULT;
  545. if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
  546. return -EACCES;
  547. return 0;
  548. }
  549. static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
  550. {
  551. struct sock *sk = sock->sk;
  552. struct tipc_sock *tsk = tipc_sk(sk);
  553. DEFINE_WAIT(wait);
  554. int done;
  555. do {
  556. int err = sock_error(sk);
  557. if (err)
  558. return err;
  559. if (sock->state == SS_DISCONNECTING)
  560. return -EPIPE;
  561. if (!*timeo_p)
  562. return -EAGAIN;
  563. if (signal_pending(current))
  564. return sock_intr_errno(*timeo_p);
  565. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  566. done = sk_wait_event(sk, timeo_p, !tsk->port.congested);
  567. finish_wait(sk_sleep(sk), &wait);
  568. } while (!done);
  569. return 0;
  570. }
  571. /**
  572. * tipc_sendmcast - send multicast message
  573. * @sock: socket structure
  574. * @seq: destination address
  575. * @iov: message data to send
  576. * @dsz: total length of message data
  577. * @timeo: timeout to wait for wakeup
  578. *
  579. * Called from function tipc_sendmsg(), which has done all sanity checks
  580. * Returns the number of bytes sent on success, or errno
  581. */
  582. static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
  583. struct iovec *iov, size_t dsz, long timeo)
  584. {
  585. struct sock *sk = sock->sk;
  586. struct tipc_sock *tsk = tipc_sk(sk);
  587. int rc;
  588. do {
  589. if (sock->state != SS_READY) {
  590. rc = -EOPNOTSUPP;
  591. break;
  592. }
  593. rc = tipc_port_mcast_xmit(&tsk->port, seq, iov, dsz);
  594. if (likely(rc >= 0)) {
  595. if (sock->state != SS_READY)
  596. sock->state = SS_CONNECTING;
  597. break;
  598. }
  599. if (rc != -ELINKCONG)
  600. break;
  601. rc = tipc_wait_for_sndmsg(sock, &timeo);
  602. } while (!rc);
  603. return rc;
  604. }
  605. /**
  606. * tipc_sendmsg - send message in connectionless manner
  607. * @iocb: if NULL, indicates that socket lock is already held
  608. * @sock: socket structure
  609. * @m: message to send
  610. * @dsz: amount of user data to be sent
  611. *
  612. * Message must have an destination specified explicitly.
  613. * Used for SOCK_RDM and SOCK_DGRAM messages,
  614. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  615. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  616. *
  617. * Returns the number of bytes sent on success, or errno otherwise
  618. */
  619. static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
  620. struct msghdr *m, size_t dsz)
  621. {
  622. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  623. struct sock *sk = sock->sk;
  624. struct tipc_sock *tsk = tipc_sk(sk);
  625. struct tipc_port *port = &tsk->port;
  626. struct tipc_msg *mhdr = &port->phdr;
  627. struct iovec *iov = m->msg_iov;
  628. u32 dnode, dport;
  629. struct sk_buff *buf;
  630. struct tipc_name_seq *seq = &dest->addr.nameseq;
  631. u32 mtu;
  632. long timeo;
  633. int rc = -EINVAL;
  634. if (unlikely(!dest))
  635. return -EDESTADDRREQ;
  636. if (unlikely((m->msg_namelen < sizeof(*dest)) ||
  637. (dest->family != AF_TIPC)))
  638. return -EINVAL;
  639. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  640. return -EMSGSIZE;
  641. if (iocb)
  642. lock_sock(sk);
  643. if (unlikely(sock->state != SS_READY)) {
  644. if (sock->state == SS_LISTENING) {
  645. rc = -EPIPE;
  646. goto exit;
  647. }
  648. if (sock->state != SS_UNCONNECTED) {
  649. rc = -EISCONN;
  650. goto exit;
  651. }
  652. if (tsk->port.published) {
  653. rc = -EOPNOTSUPP;
  654. goto exit;
  655. }
  656. if (dest->addrtype == TIPC_ADDR_NAME) {
  657. tsk->port.conn_type = dest->addr.name.name.type;
  658. tsk->port.conn_instance = dest->addr.name.name.instance;
  659. }
  660. }
  661. rc = dest_name_check(dest, m);
  662. if (rc)
  663. goto exit;
  664. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  665. if (dest->addrtype == TIPC_ADDR_MCAST) {
  666. rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
  667. goto exit;
  668. } else if (dest->addrtype == TIPC_ADDR_NAME) {
  669. u32 type = dest->addr.name.name.type;
  670. u32 inst = dest->addr.name.name.instance;
  671. u32 domain = dest->addr.name.domain;
  672. dnode = domain;
  673. msg_set_type(mhdr, TIPC_NAMED_MSG);
  674. msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
  675. msg_set_nametype(mhdr, type);
  676. msg_set_nameinst(mhdr, inst);
  677. msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
  678. dport = tipc_nametbl_translate(type, inst, &dnode);
  679. msg_set_destnode(mhdr, dnode);
  680. msg_set_destport(mhdr, dport);
  681. if (unlikely(!dport && !dnode)) {
  682. rc = -EHOSTUNREACH;
  683. goto exit;
  684. }
  685. } else if (dest->addrtype == TIPC_ADDR_ID) {
  686. dnode = dest->addr.id.node;
  687. msg_set_type(mhdr, TIPC_DIRECT_MSG);
  688. msg_set_lookup_scope(mhdr, 0);
  689. msg_set_destnode(mhdr, dnode);
  690. msg_set_destport(mhdr, dest->addr.id.ref);
  691. msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
  692. }
  693. new_mtu:
  694. mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
  695. rc = tipc_msg_build2(mhdr, iov, 0, dsz, mtu, &buf);
  696. if (rc < 0)
  697. goto exit;
  698. do {
  699. rc = tipc_link_xmit2(buf, dnode, tsk->port.ref);
  700. if (likely(rc >= 0)) {
  701. if (sock->state != SS_READY)
  702. sock->state = SS_CONNECTING;
  703. rc = dsz;
  704. break;
  705. }
  706. if (rc == -EMSGSIZE)
  707. goto new_mtu;
  708. if (rc != -ELINKCONG)
  709. break;
  710. rc = tipc_wait_for_sndmsg(sock, &timeo);
  711. } while (!rc);
  712. exit:
  713. if (iocb)
  714. release_sock(sk);
  715. return rc;
  716. }
  717. static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
  718. {
  719. struct sock *sk = sock->sk;
  720. struct tipc_sock *tsk = tipc_sk(sk);
  721. struct tipc_port *port = &tsk->port;
  722. DEFINE_WAIT(wait);
  723. int done;
  724. do {
  725. int err = sock_error(sk);
  726. if (err)
  727. return err;
  728. if (sock->state == SS_DISCONNECTING)
  729. return -EPIPE;
  730. else if (sock->state != SS_CONNECTED)
  731. return -ENOTCONN;
  732. if (!*timeo_p)
  733. return -EAGAIN;
  734. if (signal_pending(current))
  735. return sock_intr_errno(*timeo_p);
  736. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  737. done = sk_wait_event(sk, timeo_p,
  738. (!port->congested || !port->connected));
  739. finish_wait(sk_sleep(sk), &wait);
  740. } while (!done);
  741. return 0;
  742. }
  743. /**
  744. * tipc_send_stream - send stream-oriented data
  745. * @iocb: (unused)
  746. * @sock: socket structure
  747. * @m: data to send
  748. * @dsz: total length of data to be transmitted
  749. *
  750. * Used for SOCK_STREAM data.
  751. *
  752. * Returns the number of bytes sent on success (or partial success),
  753. * or errno if no data sent
  754. */
  755. static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
  756. struct msghdr *m, size_t dsz)
  757. {
  758. struct sock *sk = sock->sk;
  759. struct tipc_sock *tsk = tipc_sk(sk);
  760. struct tipc_port *port = &tsk->port;
  761. struct tipc_msg *mhdr = &port->phdr;
  762. struct sk_buff *buf;
  763. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  764. u32 ref = port->ref;
  765. int rc = -EINVAL;
  766. long timeo;
  767. u32 dnode;
  768. uint mtu, send, sent = 0;
  769. /* Handle implied connection establishment */
  770. if (unlikely(dest)) {
  771. rc = tipc_sendmsg(iocb, sock, m, dsz);
  772. if (dsz && (dsz == rc))
  773. tsk->port.sent = 1;
  774. return rc;
  775. }
  776. if (dsz > (uint)INT_MAX)
  777. return -EMSGSIZE;
  778. if (iocb)
  779. lock_sock(sk);
  780. if (unlikely(sock->state != SS_CONNECTED)) {
  781. if (sock->state == SS_DISCONNECTING)
  782. rc = -EPIPE;
  783. else
  784. rc = -ENOTCONN;
  785. goto exit;
  786. }
  787. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  788. dnode = tipc_port_peernode(port);
  789. port->congested = 1;
  790. next:
  791. mtu = port->max_pkt;
  792. send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
  793. rc = tipc_msg_build2(mhdr, m->msg_iov, sent, send, mtu, &buf);
  794. if (unlikely(rc < 0))
  795. goto exit;
  796. do {
  797. port->congested = 1;
  798. if (likely(!tipc_port_congested(port))) {
  799. rc = tipc_link_xmit2(buf, dnode, ref);
  800. if (likely(!rc)) {
  801. port->sent++;
  802. sent += send;
  803. if (sent == dsz)
  804. break;
  805. goto next;
  806. }
  807. if (rc == -EMSGSIZE) {
  808. port->max_pkt = tipc_node_get_mtu(dnode, ref);
  809. goto next;
  810. }
  811. if (rc != -ELINKCONG)
  812. break;
  813. }
  814. rc = tipc_wait_for_sndpkt(sock, &timeo);
  815. } while (!rc);
  816. port->congested = 0;
  817. exit:
  818. if (iocb)
  819. release_sock(sk);
  820. return sent ? sent : rc;
  821. }
  822. /**
  823. * tipc_send_packet - send a connection-oriented message
  824. * @iocb: if NULL, indicates that socket lock is already held
  825. * @sock: socket structure
  826. * @m: message to send
  827. * @dsz: length of data to be transmitted
  828. *
  829. * Used for SOCK_SEQPACKET messages.
  830. *
  831. * Returns the number of bytes sent on success, or errno otherwise
  832. */
  833. static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
  834. struct msghdr *m, size_t dsz)
  835. {
  836. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  837. return -EMSGSIZE;
  838. return tipc_send_stream(iocb, sock, m, dsz);
  839. }
  840. /**
  841. * auto_connect - complete connection setup to a remote port
  842. * @tsk: tipc socket structure
  843. * @msg: peer's response message
  844. *
  845. * Returns 0 on success, errno otherwise
  846. */
  847. static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
  848. {
  849. struct tipc_port *port = &tsk->port;
  850. struct socket *sock = tsk->sk.sk_socket;
  851. struct tipc_portid peer;
  852. peer.ref = msg_origport(msg);
  853. peer.node = msg_orignode(msg);
  854. __tipc_port_connect(port->ref, port, &peer);
  855. if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
  856. return -EINVAL;
  857. msg_set_importance(&port->phdr, (u32)msg_importance(msg));
  858. sock->state = SS_CONNECTED;
  859. return 0;
  860. }
  861. /**
  862. * set_orig_addr - capture sender's address for received message
  863. * @m: descriptor for message info
  864. * @msg: received message header
  865. *
  866. * Note: Address is not captured if not requested by receiver.
  867. */
  868. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  869. {
  870. DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
  871. if (addr) {
  872. addr->family = AF_TIPC;
  873. addr->addrtype = TIPC_ADDR_ID;
  874. memset(&addr->addr, 0, sizeof(addr->addr));
  875. addr->addr.id.ref = msg_origport(msg);
  876. addr->addr.id.node = msg_orignode(msg);
  877. addr->addr.name.domain = 0; /* could leave uninitialized */
  878. addr->scope = 0; /* could leave uninitialized */
  879. m->msg_namelen = sizeof(struct sockaddr_tipc);
  880. }
  881. }
  882. /**
  883. * anc_data_recv - optionally capture ancillary data for received message
  884. * @m: descriptor for message info
  885. * @msg: received message header
  886. * @tport: TIPC port associated with message
  887. *
  888. * Note: Ancillary data is not captured if not requested by receiver.
  889. *
  890. * Returns 0 if successful, otherwise errno
  891. */
  892. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  893. struct tipc_port *tport)
  894. {
  895. u32 anc_data[3];
  896. u32 err;
  897. u32 dest_type;
  898. int has_name;
  899. int res;
  900. if (likely(m->msg_controllen == 0))
  901. return 0;
  902. /* Optionally capture errored message object(s) */
  903. err = msg ? msg_errcode(msg) : 0;
  904. if (unlikely(err)) {
  905. anc_data[0] = err;
  906. anc_data[1] = msg_data_sz(msg);
  907. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  908. if (res)
  909. return res;
  910. if (anc_data[1]) {
  911. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  912. msg_data(msg));
  913. if (res)
  914. return res;
  915. }
  916. }
  917. /* Optionally capture message destination object */
  918. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  919. switch (dest_type) {
  920. case TIPC_NAMED_MSG:
  921. has_name = 1;
  922. anc_data[0] = msg_nametype(msg);
  923. anc_data[1] = msg_namelower(msg);
  924. anc_data[2] = msg_namelower(msg);
  925. break;
  926. case TIPC_MCAST_MSG:
  927. has_name = 1;
  928. anc_data[0] = msg_nametype(msg);
  929. anc_data[1] = msg_namelower(msg);
  930. anc_data[2] = msg_nameupper(msg);
  931. break;
  932. case TIPC_CONN_MSG:
  933. has_name = (tport->conn_type != 0);
  934. anc_data[0] = tport->conn_type;
  935. anc_data[1] = tport->conn_instance;
  936. anc_data[2] = tport->conn_instance;
  937. break;
  938. default:
  939. has_name = 0;
  940. }
  941. if (has_name) {
  942. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  943. if (res)
  944. return res;
  945. }
  946. return 0;
  947. }
  948. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  949. {
  950. struct sock *sk = sock->sk;
  951. DEFINE_WAIT(wait);
  952. long timeo = *timeop;
  953. int err;
  954. for (;;) {
  955. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  956. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  957. if (sock->state == SS_DISCONNECTING) {
  958. err = -ENOTCONN;
  959. break;
  960. }
  961. release_sock(sk);
  962. timeo = schedule_timeout(timeo);
  963. lock_sock(sk);
  964. }
  965. err = 0;
  966. if (!skb_queue_empty(&sk->sk_receive_queue))
  967. break;
  968. err = sock_intr_errno(timeo);
  969. if (signal_pending(current))
  970. break;
  971. err = -EAGAIN;
  972. if (!timeo)
  973. break;
  974. }
  975. finish_wait(sk_sleep(sk), &wait);
  976. *timeop = timeo;
  977. return err;
  978. }
  979. /**
  980. * tipc_recvmsg - receive packet-oriented message
  981. * @iocb: (unused)
  982. * @m: descriptor for message info
  983. * @buf_len: total size of user buffer area
  984. * @flags: receive flags
  985. *
  986. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  987. * If the complete message doesn't fit in user area, truncate it.
  988. *
  989. * Returns size of returned message data, errno otherwise
  990. */
  991. static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
  992. struct msghdr *m, size_t buf_len, int flags)
  993. {
  994. struct sock *sk = sock->sk;
  995. struct tipc_sock *tsk = tipc_sk(sk);
  996. struct tipc_port *port = &tsk->port;
  997. struct sk_buff *buf;
  998. struct tipc_msg *msg;
  999. long timeo;
  1000. unsigned int sz;
  1001. u32 err;
  1002. int res;
  1003. /* Catch invalid receive requests */
  1004. if (unlikely(!buf_len))
  1005. return -EINVAL;
  1006. lock_sock(sk);
  1007. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1008. res = -ENOTCONN;
  1009. goto exit;
  1010. }
  1011. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1012. restart:
  1013. /* Look for a message in receive queue; wait if necessary */
  1014. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1015. if (res)
  1016. goto exit;
  1017. /* Look at first message in receive queue */
  1018. buf = skb_peek(&sk->sk_receive_queue);
  1019. msg = buf_msg(buf);
  1020. sz = msg_data_sz(msg);
  1021. err = msg_errcode(msg);
  1022. /* Discard an empty non-errored message & try again */
  1023. if ((!sz) && (!err)) {
  1024. advance_rx_queue(sk);
  1025. goto restart;
  1026. }
  1027. /* Capture sender's address (optional) */
  1028. set_orig_addr(m, msg);
  1029. /* Capture ancillary data (optional) */
  1030. res = anc_data_recv(m, msg, port);
  1031. if (res)
  1032. goto exit;
  1033. /* Capture message data (if valid) & compute return value (always) */
  1034. if (!err) {
  1035. if (unlikely(buf_len < sz)) {
  1036. sz = buf_len;
  1037. m->msg_flags |= MSG_TRUNC;
  1038. }
  1039. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
  1040. m->msg_iov, sz);
  1041. if (res)
  1042. goto exit;
  1043. res = sz;
  1044. } else {
  1045. if ((sock->state == SS_READY) ||
  1046. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  1047. res = 0;
  1048. else
  1049. res = -ECONNRESET;
  1050. }
  1051. /* Consume received message (optional) */
  1052. if (likely(!(flags & MSG_PEEK))) {
  1053. if ((sock->state != SS_READY) &&
  1054. (++port->conn_unacked >= TIPC_CONNACK_INTV))
  1055. tipc_acknowledge(port->ref, port->conn_unacked);
  1056. advance_rx_queue(sk);
  1057. }
  1058. exit:
  1059. release_sock(sk);
  1060. return res;
  1061. }
  1062. /**
  1063. * tipc_recv_stream - receive stream-oriented data
  1064. * @iocb: (unused)
  1065. * @m: descriptor for message info
  1066. * @buf_len: total size of user buffer area
  1067. * @flags: receive flags
  1068. *
  1069. * Used for SOCK_STREAM messages only. If not enough data is available
  1070. * will optionally wait for more; never truncates data.
  1071. *
  1072. * Returns size of returned message data, errno otherwise
  1073. */
  1074. static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
  1075. struct msghdr *m, size_t buf_len, int flags)
  1076. {
  1077. struct sock *sk = sock->sk;
  1078. struct tipc_sock *tsk = tipc_sk(sk);
  1079. struct tipc_port *port = &tsk->port;
  1080. struct sk_buff *buf;
  1081. struct tipc_msg *msg;
  1082. long timeo;
  1083. unsigned int sz;
  1084. int sz_to_copy, target, needed;
  1085. int sz_copied = 0;
  1086. u32 err;
  1087. int res = 0;
  1088. /* Catch invalid receive attempts */
  1089. if (unlikely(!buf_len))
  1090. return -EINVAL;
  1091. lock_sock(sk);
  1092. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1093. res = -ENOTCONN;
  1094. goto exit;
  1095. }
  1096. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  1097. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1098. restart:
  1099. /* Look for a message in receive queue; wait if necessary */
  1100. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1101. if (res)
  1102. goto exit;
  1103. /* Look at first message in receive queue */
  1104. buf = skb_peek(&sk->sk_receive_queue);
  1105. msg = buf_msg(buf);
  1106. sz = msg_data_sz(msg);
  1107. err = msg_errcode(msg);
  1108. /* Discard an empty non-errored message & try again */
  1109. if ((!sz) && (!err)) {
  1110. advance_rx_queue(sk);
  1111. goto restart;
  1112. }
  1113. /* Optionally capture sender's address & ancillary data of first msg */
  1114. if (sz_copied == 0) {
  1115. set_orig_addr(m, msg);
  1116. res = anc_data_recv(m, msg, port);
  1117. if (res)
  1118. goto exit;
  1119. }
  1120. /* Capture message data (if valid) & compute return value (always) */
  1121. if (!err) {
  1122. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  1123. sz -= offset;
  1124. needed = (buf_len - sz_copied);
  1125. sz_to_copy = (sz <= needed) ? sz : needed;
  1126. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
  1127. m->msg_iov, sz_to_copy);
  1128. if (res)
  1129. goto exit;
  1130. sz_copied += sz_to_copy;
  1131. if (sz_to_copy < sz) {
  1132. if (!(flags & MSG_PEEK))
  1133. TIPC_SKB_CB(buf)->handle =
  1134. (void *)(unsigned long)(offset + sz_to_copy);
  1135. goto exit;
  1136. }
  1137. } else {
  1138. if (sz_copied != 0)
  1139. goto exit; /* can't add error msg to valid data */
  1140. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  1141. res = 0;
  1142. else
  1143. res = -ECONNRESET;
  1144. }
  1145. /* Consume received message (optional) */
  1146. if (likely(!(flags & MSG_PEEK))) {
  1147. if (unlikely(++port->conn_unacked >= TIPC_CONNACK_INTV))
  1148. tipc_acknowledge(port->ref, port->conn_unacked);
  1149. advance_rx_queue(sk);
  1150. }
  1151. /* Loop around if more data is required */
  1152. if ((sz_copied < buf_len) && /* didn't get all requested data */
  1153. (!skb_queue_empty(&sk->sk_receive_queue) ||
  1154. (sz_copied < target)) && /* and more is ready or required */
  1155. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  1156. (!err)) /* and haven't reached a FIN */
  1157. goto restart;
  1158. exit:
  1159. release_sock(sk);
  1160. return sz_copied ? sz_copied : res;
  1161. }
  1162. /**
  1163. * tipc_write_space - wake up thread if port congestion is released
  1164. * @sk: socket
  1165. */
  1166. static void tipc_write_space(struct sock *sk)
  1167. {
  1168. struct socket_wq *wq;
  1169. rcu_read_lock();
  1170. wq = rcu_dereference(sk->sk_wq);
  1171. if (wq_has_sleeper(wq))
  1172. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1173. POLLWRNORM | POLLWRBAND);
  1174. rcu_read_unlock();
  1175. }
  1176. /**
  1177. * tipc_data_ready - wake up threads to indicate messages have been received
  1178. * @sk: socket
  1179. * @len: the length of messages
  1180. */
  1181. static void tipc_data_ready(struct sock *sk)
  1182. {
  1183. struct socket_wq *wq;
  1184. rcu_read_lock();
  1185. wq = rcu_dereference(sk->sk_wq);
  1186. if (wq_has_sleeper(wq))
  1187. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1188. POLLRDNORM | POLLRDBAND);
  1189. rcu_read_unlock();
  1190. }
  1191. /**
  1192. * filter_connect - Handle all incoming messages for a connection-based socket
  1193. * @tsk: TIPC socket
  1194. * @msg: message
  1195. *
  1196. * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
  1197. */
  1198. static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
  1199. {
  1200. struct sock *sk = &tsk->sk;
  1201. struct tipc_port *port = &tsk->port;
  1202. struct socket *sock = sk->sk_socket;
  1203. struct tipc_msg *msg = buf_msg(*buf);
  1204. int retval = -TIPC_ERR_NO_PORT;
  1205. int res;
  1206. if (msg_mcast(msg))
  1207. return retval;
  1208. switch ((int)sock->state) {
  1209. case SS_CONNECTED:
  1210. /* Accept only connection-based messages sent by peer */
  1211. if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
  1212. if (unlikely(msg_errcode(msg))) {
  1213. sock->state = SS_DISCONNECTING;
  1214. __tipc_port_disconnect(port);
  1215. }
  1216. retval = TIPC_OK;
  1217. }
  1218. break;
  1219. case SS_CONNECTING:
  1220. /* Accept only ACK or NACK message */
  1221. if (unlikely(msg_errcode(msg))) {
  1222. sock->state = SS_DISCONNECTING;
  1223. sk->sk_err = ECONNREFUSED;
  1224. retval = TIPC_OK;
  1225. break;
  1226. }
  1227. if (unlikely(!msg_connected(msg)))
  1228. break;
  1229. res = auto_connect(tsk, msg);
  1230. if (res) {
  1231. sock->state = SS_DISCONNECTING;
  1232. sk->sk_err = -res;
  1233. retval = TIPC_OK;
  1234. break;
  1235. }
  1236. /* If an incoming message is an 'ACK-', it should be
  1237. * discarded here because it doesn't contain useful
  1238. * data. In addition, we should try to wake up
  1239. * connect() routine if sleeping.
  1240. */
  1241. if (msg_data_sz(msg) == 0) {
  1242. kfree_skb(*buf);
  1243. *buf = NULL;
  1244. if (waitqueue_active(sk_sleep(sk)))
  1245. wake_up_interruptible(sk_sleep(sk));
  1246. }
  1247. retval = TIPC_OK;
  1248. break;
  1249. case SS_LISTENING:
  1250. case SS_UNCONNECTED:
  1251. /* Accept only SYN message */
  1252. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1253. retval = TIPC_OK;
  1254. break;
  1255. case SS_DISCONNECTING:
  1256. break;
  1257. default:
  1258. pr_err("Unknown socket state %u\n", sock->state);
  1259. }
  1260. return retval;
  1261. }
  1262. /**
  1263. * rcvbuf_limit - get proper overload limit of socket receive queue
  1264. * @sk: socket
  1265. * @buf: message
  1266. *
  1267. * For all connection oriented messages, irrespective of importance,
  1268. * the default overload value (i.e. 67MB) is set as limit.
  1269. *
  1270. * For all connectionless messages, by default new queue limits are
  1271. * as belows:
  1272. *
  1273. * TIPC_LOW_IMPORTANCE (4 MB)
  1274. * TIPC_MEDIUM_IMPORTANCE (8 MB)
  1275. * TIPC_HIGH_IMPORTANCE (16 MB)
  1276. * TIPC_CRITICAL_IMPORTANCE (32 MB)
  1277. *
  1278. * Returns overload limit according to corresponding message importance
  1279. */
  1280. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
  1281. {
  1282. struct tipc_msg *msg = buf_msg(buf);
  1283. if (msg_connected(msg))
  1284. return sysctl_tipc_rmem[2];
  1285. return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
  1286. msg_importance(msg);
  1287. }
  1288. /**
  1289. * filter_rcv - validate incoming message
  1290. * @sk: socket
  1291. * @buf: message
  1292. *
  1293. * Enqueues message on receive queue if acceptable; optionally handles
  1294. * disconnect indication for a connected socket.
  1295. *
  1296. * Called with socket lock already taken; port lock may also be taken.
  1297. *
  1298. * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
  1299. * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
  1300. */
  1301. static int filter_rcv(struct sock *sk, struct sk_buff *buf)
  1302. {
  1303. struct socket *sock = sk->sk_socket;
  1304. struct tipc_sock *tsk = tipc_sk(sk);
  1305. struct tipc_msg *msg = buf_msg(buf);
  1306. unsigned int limit = rcvbuf_limit(sk, buf);
  1307. u32 onode;
  1308. int rc = TIPC_OK;
  1309. if (unlikely(msg_user(msg) == CONN_MANAGER))
  1310. return tipc_sk_proto_rcv(tsk, &onode, buf);
  1311. /* Reject message if it is wrong sort of message for socket */
  1312. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1313. return -TIPC_ERR_NO_PORT;
  1314. if (sock->state == SS_READY) {
  1315. if (msg_connected(msg))
  1316. return -TIPC_ERR_NO_PORT;
  1317. } else {
  1318. rc = filter_connect(tsk, &buf);
  1319. if (rc != TIPC_OK || buf == NULL)
  1320. return rc;
  1321. }
  1322. /* Reject message if there isn't room to queue it */
  1323. if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
  1324. return -TIPC_ERR_OVERLOAD;
  1325. /* Enqueue message */
  1326. TIPC_SKB_CB(buf)->handle = NULL;
  1327. __skb_queue_tail(&sk->sk_receive_queue, buf);
  1328. skb_set_owner_r(buf, sk);
  1329. sk->sk_data_ready(sk);
  1330. return TIPC_OK;
  1331. }
  1332. /**
  1333. * tipc_backlog_rcv - handle incoming message from backlog queue
  1334. * @sk: socket
  1335. * @buf: message
  1336. *
  1337. * Caller must hold socket lock, but not port lock.
  1338. *
  1339. * Returns 0
  1340. */
  1341. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
  1342. {
  1343. int rc;
  1344. u32 onode;
  1345. struct tipc_sock *tsk = tipc_sk(sk);
  1346. uint truesize = buf->truesize;
  1347. rc = filter_rcv(sk, buf);
  1348. if (likely(!rc)) {
  1349. if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
  1350. atomic_add(truesize, &tsk->dupl_rcvcnt);
  1351. return 0;
  1352. }
  1353. if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
  1354. return 0;
  1355. tipc_link_xmit2(buf, onode, 0);
  1356. return 0;
  1357. }
  1358. /**
  1359. * tipc_sk_rcv - handle incoming message
  1360. * @buf: buffer containing arriving message
  1361. * Consumes buffer
  1362. * Returns 0 if success, or errno: -EHOSTUNREACH
  1363. */
  1364. int tipc_sk_rcv(struct sk_buff *buf)
  1365. {
  1366. struct tipc_sock *tsk;
  1367. struct tipc_port *port;
  1368. struct sock *sk;
  1369. u32 dport = msg_destport(buf_msg(buf));
  1370. int rc = TIPC_OK;
  1371. uint limit;
  1372. u32 dnode;
  1373. /* Validate destination and message */
  1374. port = tipc_port_lock(dport);
  1375. if (unlikely(!port)) {
  1376. rc = tipc_msg_eval(buf, &dnode);
  1377. goto exit;
  1378. }
  1379. tsk = tipc_port_to_sock(port);
  1380. sk = &tsk->sk;
  1381. /* Queue message */
  1382. bh_lock_sock(sk);
  1383. if (!sock_owned_by_user(sk)) {
  1384. rc = filter_rcv(sk, buf);
  1385. } else {
  1386. if (sk->sk_backlog.len == 0)
  1387. atomic_set(&tsk->dupl_rcvcnt, 0);
  1388. limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
  1389. if (sk_add_backlog(sk, buf, limit))
  1390. rc = -TIPC_ERR_OVERLOAD;
  1391. }
  1392. bh_unlock_sock(sk);
  1393. tipc_port_unlock(port);
  1394. if (likely(!rc))
  1395. return 0;
  1396. exit:
  1397. if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
  1398. return -EHOSTUNREACH;
  1399. tipc_link_xmit2(buf, dnode, 0);
  1400. return (rc < 0) ? -EHOSTUNREACH : 0;
  1401. }
  1402. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  1403. {
  1404. struct sock *sk = sock->sk;
  1405. DEFINE_WAIT(wait);
  1406. int done;
  1407. do {
  1408. int err = sock_error(sk);
  1409. if (err)
  1410. return err;
  1411. if (!*timeo_p)
  1412. return -ETIMEDOUT;
  1413. if (signal_pending(current))
  1414. return sock_intr_errno(*timeo_p);
  1415. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1416. done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
  1417. finish_wait(sk_sleep(sk), &wait);
  1418. } while (!done);
  1419. return 0;
  1420. }
  1421. /**
  1422. * tipc_connect - establish a connection to another TIPC port
  1423. * @sock: socket structure
  1424. * @dest: socket address for destination port
  1425. * @destlen: size of socket address data structure
  1426. * @flags: file-related flags associated with socket
  1427. *
  1428. * Returns 0 on success, errno otherwise
  1429. */
  1430. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  1431. int destlen, int flags)
  1432. {
  1433. struct sock *sk = sock->sk;
  1434. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1435. struct msghdr m = {NULL,};
  1436. long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
  1437. socket_state previous;
  1438. int res;
  1439. lock_sock(sk);
  1440. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1441. if (sock->state == SS_READY) {
  1442. res = -EOPNOTSUPP;
  1443. goto exit;
  1444. }
  1445. /*
  1446. * Reject connection attempt using multicast address
  1447. *
  1448. * Note: send_msg() validates the rest of the address fields,
  1449. * so there's no need to do it here
  1450. */
  1451. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1452. res = -EINVAL;
  1453. goto exit;
  1454. }
  1455. previous = sock->state;
  1456. switch (sock->state) {
  1457. case SS_UNCONNECTED:
  1458. /* Send a 'SYN-' to destination */
  1459. m.msg_name = dest;
  1460. m.msg_namelen = destlen;
  1461. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1462. * indicate send_msg() is never blocked.
  1463. */
  1464. if (!timeout)
  1465. m.msg_flags = MSG_DONTWAIT;
  1466. res = tipc_sendmsg(NULL, sock, &m, 0);
  1467. if ((res < 0) && (res != -EWOULDBLOCK))
  1468. goto exit;
  1469. /* Just entered SS_CONNECTING state; the only
  1470. * difference is that return value in non-blocking
  1471. * case is EINPROGRESS, rather than EALREADY.
  1472. */
  1473. res = -EINPROGRESS;
  1474. case SS_CONNECTING:
  1475. if (previous == SS_CONNECTING)
  1476. res = -EALREADY;
  1477. if (!timeout)
  1478. goto exit;
  1479. timeout = msecs_to_jiffies(timeout);
  1480. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1481. res = tipc_wait_for_connect(sock, &timeout);
  1482. break;
  1483. case SS_CONNECTED:
  1484. res = -EISCONN;
  1485. break;
  1486. default:
  1487. res = -EINVAL;
  1488. break;
  1489. }
  1490. exit:
  1491. release_sock(sk);
  1492. return res;
  1493. }
  1494. /**
  1495. * tipc_listen - allow socket to listen for incoming connections
  1496. * @sock: socket structure
  1497. * @len: (unused)
  1498. *
  1499. * Returns 0 on success, errno otherwise
  1500. */
  1501. static int tipc_listen(struct socket *sock, int len)
  1502. {
  1503. struct sock *sk = sock->sk;
  1504. int res;
  1505. lock_sock(sk);
  1506. if (sock->state != SS_UNCONNECTED)
  1507. res = -EINVAL;
  1508. else {
  1509. sock->state = SS_LISTENING;
  1510. res = 0;
  1511. }
  1512. release_sock(sk);
  1513. return res;
  1514. }
  1515. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  1516. {
  1517. struct sock *sk = sock->sk;
  1518. DEFINE_WAIT(wait);
  1519. int err;
  1520. /* True wake-one mechanism for incoming connections: only
  1521. * one process gets woken up, not the 'whole herd'.
  1522. * Since we do not 'race & poll' for established sockets
  1523. * anymore, the common case will execute the loop only once.
  1524. */
  1525. for (;;) {
  1526. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  1527. TASK_INTERRUPTIBLE);
  1528. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1529. release_sock(sk);
  1530. timeo = schedule_timeout(timeo);
  1531. lock_sock(sk);
  1532. }
  1533. err = 0;
  1534. if (!skb_queue_empty(&sk->sk_receive_queue))
  1535. break;
  1536. err = -EINVAL;
  1537. if (sock->state != SS_LISTENING)
  1538. break;
  1539. err = sock_intr_errno(timeo);
  1540. if (signal_pending(current))
  1541. break;
  1542. err = -EAGAIN;
  1543. if (!timeo)
  1544. break;
  1545. }
  1546. finish_wait(sk_sleep(sk), &wait);
  1547. return err;
  1548. }
  1549. /**
  1550. * tipc_accept - wait for connection request
  1551. * @sock: listening socket
  1552. * @newsock: new socket that is to be connected
  1553. * @flags: file-related flags associated with socket
  1554. *
  1555. * Returns 0 on success, errno otherwise
  1556. */
  1557. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
  1558. {
  1559. struct sock *new_sk, *sk = sock->sk;
  1560. struct sk_buff *buf;
  1561. struct tipc_port *new_port;
  1562. struct tipc_msg *msg;
  1563. struct tipc_portid peer;
  1564. u32 new_ref;
  1565. long timeo;
  1566. int res;
  1567. lock_sock(sk);
  1568. if (sock->state != SS_LISTENING) {
  1569. res = -EINVAL;
  1570. goto exit;
  1571. }
  1572. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1573. res = tipc_wait_for_accept(sock, timeo);
  1574. if (res)
  1575. goto exit;
  1576. buf = skb_peek(&sk->sk_receive_queue);
  1577. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
  1578. if (res)
  1579. goto exit;
  1580. new_sk = new_sock->sk;
  1581. new_port = &tipc_sk(new_sk)->port;
  1582. new_ref = new_port->ref;
  1583. msg = buf_msg(buf);
  1584. /* we lock on new_sk; but lockdep sees the lock on sk */
  1585. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1586. /*
  1587. * Reject any stray messages received by new socket
  1588. * before the socket lock was taken (very, very unlikely)
  1589. */
  1590. reject_rx_queue(new_sk);
  1591. /* Connect new socket to it's peer */
  1592. peer.ref = msg_origport(msg);
  1593. peer.node = msg_orignode(msg);
  1594. tipc_port_connect(new_ref, &peer);
  1595. new_sock->state = SS_CONNECTED;
  1596. tipc_port_set_importance(new_port, msg_importance(msg));
  1597. if (msg_named(msg)) {
  1598. new_port->conn_type = msg_nametype(msg);
  1599. new_port->conn_instance = msg_nameinst(msg);
  1600. }
  1601. /*
  1602. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1603. * Respond to 'SYN+' by queuing it on new socket.
  1604. */
  1605. if (!msg_data_sz(msg)) {
  1606. struct msghdr m = {NULL,};
  1607. advance_rx_queue(sk);
  1608. tipc_send_packet(NULL, new_sock, &m, 0);
  1609. } else {
  1610. __skb_dequeue(&sk->sk_receive_queue);
  1611. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1612. skb_set_owner_r(buf, new_sk);
  1613. }
  1614. release_sock(new_sk);
  1615. exit:
  1616. release_sock(sk);
  1617. return res;
  1618. }
  1619. /**
  1620. * tipc_shutdown - shutdown socket connection
  1621. * @sock: socket structure
  1622. * @how: direction to close (must be SHUT_RDWR)
  1623. *
  1624. * Terminates connection (if necessary), then purges socket's receive queue.
  1625. *
  1626. * Returns 0 on success, errno otherwise
  1627. */
  1628. static int tipc_shutdown(struct socket *sock, int how)
  1629. {
  1630. struct sock *sk = sock->sk;
  1631. struct tipc_sock *tsk = tipc_sk(sk);
  1632. struct tipc_port *port = &tsk->port;
  1633. struct sk_buff *buf;
  1634. u32 peer;
  1635. int res;
  1636. if (how != SHUT_RDWR)
  1637. return -EINVAL;
  1638. lock_sock(sk);
  1639. switch (sock->state) {
  1640. case SS_CONNECTING:
  1641. case SS_CONNECTED:
  1642. restart:
  1643. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1644. buf = __skb_dequeue(&sk->sk_receive_queue);
  1645. if (buf) {
  1646. if (TIPC_SKB_CB(buf)->handle != NULL) {
  1647. kfree_skb(buf);
  1648. goto restart;
  1649. }
  1650. tipc_port_disconnect(port->ref);
  1651. if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN))
  1652. tipc_link_xmit2(buf, peer, 0);
  1653. } else {
  1654. tipc_port_shutdown(port->ref);
  1655. }
  1656. sock->state = SS_DISCONNECTING;
  1657. /* fall through */
  1658. case SS_DISCONNECTING:
  1659. /* Discard any unreceived messages */
  1660. __skb_queue_purge(&sk->sk_receive_queue);
  1661. /* Wake up anyone sleeping in poll */
  1662. sk->sk_state_change(sk);
  1663. res = 0;
  1664. break;
  1665. default:
  1666. res = -ENOTCONN;
  1667. }
  1668. release_sock(sk);
  1669. return res;
  1670. }
  1671. /**
  1672. * tipc_setsockopt - set socket option
  1673. * @sock: socket structure
  1674. * @lvl: option level
  1675. * @opt: option identifier
  1676. * @ov: pointer to new option value
  1677. * @ol: length of option value
  1678. *
  1679. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1680. * (to ease compatibility).
  1681. *
  1682. * Returns 0 on success, errno otherwise
  1683. */
  1684. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  1685. char __user *ov, unsigned int ol)
  1686. {
  1687. struct sock *sk = sock->sk;
  1688. struct tipc_sock *tsk = tipc_sk(sk);
  1689. struct tipc_port *port = &tsk->port;
  1690. u32 value;
  1691. int res;
  1692. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1693. return 0;
  1694. if (lvl != SOL_TIPC)
  1695. return -ENOPROTOOPT;
  1696. if (ol < sizeof(value))
  1697. return -EINVAL;
  1698. res = get_user(value, (u32 __user *)ov);
  1699. if (res)
  1700. return res;
  1701. lock_sock(sk);
  1702. switch (opt) {
  1703. case TIPC_IMPORTANCE:
  1704. tipc_port_set_importance(port, value);
  1705. break;
  1706. case TIPC_SRC_DROPPABLE:
  1707. if (sock->type != SOCK_STREAM)
  1708. tipc_port_set_unreliable(port, value);
  1709. else
  1710. res = -ENOPROTOOPT;
  1711. break;
  1712. case TIPC_DEST_DROPPABLE:
  1713. tipc_port_set_unreturnable(port, value);
  1714. break;
  1715. case TIPC_CONN_TIMEOUT:
  1716. tipc_sk(sk)->conn_timeout = value;
  1717. /* no need to set "res", since already 0 at this point */
  1718. break;
  1719. default:
  1720. res = -EINVAL;
  1721. }
  1722. release_sock(sk);
  1723. return res;
  1724. }
  1725. /**
  1726. * tipc_getsockopt - get socket option
  1727. * @sock: socket structure
  1728. * @lvl: option level
  1729. * @opt: option identifier
  1730. * @ov: receptacle for option value
  1731. * @ol: receptacle for length of option value
  1732. *
  1733. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1734. * (to ease compatibility).
  1735. *
  1736. * Returns 0 on success, errno otherwise
  1737. */
  1738. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  1739. char __user *ov, int __user *ol)
  1740. {
  1741. struct sock *sk = sock->sk;
  1742. struct tipc_sock *tsk = tipc_sk(sk);
  1743. struct tipc_port *port = &tsk->port;
  1744. int len;
  1745. u32 value;
  1746. int res;
  1747. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1748. return put_user(0, ol);
  1749. if (lvl != SOL_TIPC)
  1750. return -ENOPROTOOPT;
  1751. res = get_user(len, ol);
  1752. if (res)
  1753. return res;
  1754. lock_sock(sk);
  1755. switch (opt) {
  1756. case TIPC_IMPORTANCE:
  1757. value = tipc_port_importance(port);
  1758. break;
  1759. case TIPC_SRC_DROPPABLE:
  1760. value = tipc_port_unreliable(port);
  1761. break;
  1762. case TIPC_DEST_DROPPABLE:
  1763. value = tipc_port_unreturnable(port);
  1764. break;
  1765. case TIPC_CONN_TIMEOUT:
  1766. value = tipc_sk(sk)->conn_timeout;
  1767. /* no need to set "res", since already 0 at this point */
  1768. break;
  1769. case TIPC_NODE_RECVQ_DEPTH:
  1770. value = 0; /* was tipc_queue_size, now obsolete */
  1771. break;
  1772. case TIPC_SOCK_RECVQ_DEPTH:
  1773. value = skb_queue_len(&sk->sk_receive_queue);
  1774. break;
  1775. default:
  1776. res = -EINVAL;
  1777. }
  1778. release_sock(sk);
  1779. if (res)
  1780. return res; /* "get" failed */
  1781. if (len < sizeof(value))
  1782. return -EINVAL;
  1783. if (copy_to_user(ov, &value, sizeof(value)))
  1784. return -EFAULT;
  1785. return put_user(sizeof(value), ol);
  1786. }
  1787. int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
  1788. {
  1789. struct tipc_sioc_ln_req lnr;
  1790. void __user *argp = (void __user *)arg;
  1791. switch (cmd) {
  1792. case SIOCGETLINKNAME:
  1793. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  1794. return -EFAULT;
  1795. if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
  1796. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  1797. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  1798. return -EFAULT;
  1799. return 0;
  1800. }
  1801. return -EADDRNOTAVAIL;
  1802. break;
  1803. default:
  1804. return -ENOIOCTLCMD;
  1805. }
  1806. }
  1807. /* Protocol switches for the various types of TIPC sockets */
  1808. static const struct proto_ops msg_ops = {
  1809. .owner = THIS_MODULE,
  1810. .family = AF_TIPC,
  1811. .release = tipc_release,
  1812. .bind = tipc_bind,
  1813. .connect = tipc_connect,
  1814. .socketpair = sock_no_socketpair,
  1815. .accept = sock_no_accept,
  1816. .getname = tipc_getname,
  1817. .poll = tipc_poll,
  1818. .ioctl = tipc_ioctl,
  1819. .listen = sock_no_listen,
  1820. .shutdown = tipc_shutdown,
  1821. .setsockopt = tipc_setsockopt,
  1822. .getsockopt = tipc_getsockopt,
  1823. .sendmsg = tipc_sendmsg,
  1824. .recvmsg = tipc_recvmsg,
  1825. .mmap = sock_no_mmap,
  1826. .sendpage = sock_no_sendpage
  1827. };
  1828. static const struct proto_ops packet_ops = {
  1829. .owner = THIS_MODULE,
  1830. .family = AF_TIPC,
  1831. .release = tipc_release,
  1832. .bind = tipc_bind,
  1833. .connect = tipc_connect,
  1834. .socketpair = sock_no_socketpair,
  1835. .accept = tipc_accept,
  1836. .getname = tipc_getname,
  1837. .poll = tipc_poll,
  1838. .ioctl = tipc_ioctl,
  1839. .listen = tipc_listen,
  1840. .shutdown = tipc_shutdown,
  1841. .setsockopt = tipc_setsockopt,
  1842. .getsockopt = tipc_getsockopt,
  1843. .sendmsg = tipc_send_packet,
  1844. .recvmsg = tipc_recvmsg,
  1845. .mmap = sock_no_mmap,
  1846. .sendpage = sock_no_sendpage
  1847. };
  1848. static const struct proto_ops stream_ops = {
  1849. .owner = THIS_MODULE,
  1850. .family = AF_TIPC,
  1851. .release = tipc_release,
  1852. .bind = tipc_bind,
  1853. .connect = tipc_connect,
  1854. .socketpair = sock_no_socketpair,
  1855. .accept = tipc_accept,
  1856. .getname = tipc_getname,
  1857. .poll = tipc_poll,
  1858. .ioctl = tipc_ioctl,
  1859. .listen = tipc_listen,
  1860. .shutdown = tipc_shutdown,
  1861. .setsockopt = tipc_setsockopt,
  1862. .getsockopt = tipc_getsockopt,
  1863. .sendmsg = tipc_send_stream,
  1864. .recvmsg = tipc_recv_stream,
  1865. .mmap = sock_no_mmap,
  1866. .sendpage = sock_no_sendpage
  1867. };
  1868. static const struct net_proto_family tipc_family_ops = {
  1869. .owner = THIS_MODULE,
  1870. .family = AF_TIPC,
  1871. .create = tipc_sk_create
  1872. };
  1873. static struct proto tipc_proto = {
  1874. .name = "TIPC",
  1875. .owner = THIS_MODULE,
  1876. .obj_size = sizeof(struct tipc_sock),
  1877. .sysctl_rmem = sysctl_tipc_rmem
  1878. };
  1879. static struct proto tipc_proto_kern = {
  1880. .name = "TIPC",
  1881. .obj_size = sizeof(struct tipc_sock),
  1882. .sysctl_rmem = sysctl_tipc_rmem
  1883. };
  1884. /**
  1885. * tipc_socket_init - initialize TIPC socket interface
  1886. *
  1887. * Returns 0 on success, errno otherwise
  1888. */
  1889. int tipc_socket_init(void)
  1890. {
  1891. int res;
  1892. res = proto_register(&tipc_proto, 1);
  1893. if (res) {
  1894. pr_err("Failed to register TIPC protocol type\n");
  1895. goto out;
  1896. }
  1897. res = sock_register(&tipc_family_ops);
  1898. if (res) {
  1899. pr_err("Failed to register TIPC socket type\n");
  1900. proto_unregister(&tipc_proto);
  1901. goto out;
  1902. }
  1903. out:
  1904. return res;
  1905. }
  1906. /**
  1907. * tipc_socket_stop - stop TIPC socket interface
  1908. */
  1909. void tipc_socket_stop(void)
  1910. {
  1911. sock_unregister(tipc_family_ops.family);
  1912. proto_unregister(&tipc_proto);
  1913. }