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->sent_unacked = 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->link_cong)
  466. mask |= POLLOUT;
  467. break;
  468. case SS_READY:
  469. case SS_CONNECTED:
  470. if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
  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 conn_cong;
  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. conn_cong = tipc_sk_conn_cong(tsk);
  503. tsk->sent_unacked -= msg_msgcnt(msg);
  504. if (conn_cong)
  505. tipc_sock_wakeup(tsk);
  506. } else if (msg_type(msg) == CONN_PROBE) {
  507. if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
  508. return TIPC_OK;
  509. msg_set_type(msg, CONN_PROBE_REPLY);
  510. return TIPC_FWD_MSG;
  511. }
  512. /* Do nothing if msg_type() == CONN_PROBE_REPLY */
  513. exit:
  514. kfree_skb(buf);
  515. return TIPC_OK;
  516. }
  517. /**
  518. * dest_name_check - verify user is permitted to send to specified port name
  519. * @dest: destination address
  520. * @m: descriptor for message to be sent
  521. *
  522. * Prevents restricted configuration commands from being issued by
  523. * unauthorized users.
  524. *
  525. * Returns 0 if permission is granted, otherwise errno
  526. */
  527. static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
  528. {
  529. struct tipc_cfg_msg_hdr hdr;
  530. if (unlikely(dest->addrtype == TIPC_ADDR_ID))
  531. return 0;
  532. if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
  533. return 0;
  534. if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
  535. return 0;
  536. if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
  537. return -EACCES;
  538. if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
  539. return -EMSGSIZE;
  540. if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
  541. return -EFAULT;
  542. if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
  543. return -EACCES;
  544. return 0;
  545. }
  546. static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
  547. {
  548. struct sock *sk = sock->sk;
  549. struct tipc_sock *tsk = tipc_sk(sk);
  550. DEFINE_WAIT(wait);
  551. int done;
  552. do {
  553. int err = sock_error(sk);
  554. if (err)
  555. return err;
  556. if (sock->state == SS_DISCONNECTING)
  557. return -EPIPE;
  558. if (!*timeo_p)
  559. return -EAGAIN;
  560. if (signal_pending(current))
  561. return sock_intr_errno(*timeo_p);
  562. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  563. done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
  564. finish_wait(sk_sleep(sk), &wait);
  565. } while (!done);
  566. return 0;
  567. }
  568. /**
  569. * tipc_sendmcast - send multicast message
  570. * @sock: socket structure
  571. * @seq: destination address
  572. * @iov: message data to send
  573. * @dsz: total length of message data
  574. * @timeo: timeout to wait for wakeup
  575. *
  576. * Called from function tipc_sendmsg(), which has done all sanity checks
  577. * Returns the number of bytes sent on success, or errno
  578. */
  579. static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
  580. struct iovec *iov, size_t dsz, long timeo)
  581. {
  582. struct sock *sk = sock->sk;
  583. struct tipc_sock *tsk = tipc_sk(sk);
  584. int rc;
  585. do {
  586. if (sock->state != SS_READY) {
  587. rc = -EOPNOTSUPP;
  588. break;
  589. }
  590. rc = tipc_port_mcast_xmit(&tsk->port, seq, iov, dsz);
  591. if (likely(rc >= 0)) {
  592. if (sock->state != SS_READY)
  593. sock->state = SS_CONNECTING;
  594. break;
  595. }
  596. if (rc != -ELINKCONG)
  597. break;
  598. rc = tipc_wait_for_sndmsg(sock, &timeo);
  599. } while (!rc);
  600. return rc;
  601. }
  602. /**
  603. * tipc_sendmsg - send message in connectionless manner
  604. * @iocb: if NULL, indicates that socket lock is already held
  605. * @sock: socket structure
  606. * @m: message to send
  607. * @dsz: amount of user data to be sent
  608. *
  609. * Message must have an destination specified explicitly.
  610. * Used for SOCK_RDM and SOCK_DGRAM messages,
  611. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  612. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  613. *
  614. * Returns the number of bytes sent on success, or errno otherwise
  615. */
  616. static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
  617. struct msghdr *m, size_t dsz)
  618. {
  619. DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
  620. struct sock *sk = sock->sk;
  621. struct tipc_sock *tsk = tipc_sk(sk);
  622. struct tipc_port *port = &tsk->port;
  623. struct tipc_msg *mhdr = &port->phdr;
  624. struct iovec *iov = m->msg_iov;
  625. u32 dnode, dport;
  626. struct sk_buff *buf;
  627. struct tipc_name_seq *seq = &dest->addr.nameseq;
  628. u32 mtu;
  629. long timeo;
  630. int rc = -EINVAL;
  631. if (unlikely(!dest))
  632. return -EDESTADDRREQ;
  633. if (unlikely((m->msg_namelen < sizeof(*dest)) ||
  634. (dest->family != AF_TIPC)))
  635. return -EINVAL;
  636. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  637. return -EMSGSIZE;
  638. if (iocb)
  639. lock_sock(sk);
  640. if (unlikely(sock->state != SS_READY)) {
  641. if (sock->state == SS_LISTENING) {
  642. rc = -EPIPE;
  643. goto exit;
  644. }
  645. if (sock->state != SS_UNCONNECTED) {
  646. rc = -EISCONN;
  647. goto exit;
  648. }
  649. if (tsk->port.published) {
  650. rc = -EOPNOTSUPP;
  651. goto exit;
  652. }
  653. if (dest->addrtype == TIPC_ADDR_NAME) {
  654. tsk->port.conn_type = dest->addr.name.name.type;
  655. tsk->port.conn_instance = dest->addr.name.name.instance;
  656. }
  657. }
  658. rc = dest_name_check(dest, m);
  659. if (rc)
  660. goto exit;
  661. timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  662. if (dest->addrtype == TIPC_ADDR_MCAST) {
  663. rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
  664. goto exit;
  665. } else if (dest->addrtype == TIPC_ADDR_NAME) {
  666. u32 type = dest->addr.name.name.type;
  667. u32 inst = dest->addr.name.name.instance;
  668. u32 domain = dest->addr.name.domain;
  669. dnode = domain;
  670. msg_set_type(mhdr, TIPC_NAMED_MSG);
  671. msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
  672. msg_set_nametype(mhdr, type);
  673. msg_set_nameinst(mhdr, inst);
  674. msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
  675. dport = tipc_nametbl_translate(type, inst, &dnode);
  676. msg_set_destnode(mhdr, dnode);
  677. msg_set_destport(mhdr, dport);
  678. if (unlikely(!dport && !dnode)) {
  679. rc = -EHOSTUNREACH;
  680. goto exit;
  681. }
  682. } else if (dest->addrtype == TIPC_ADDR_ID) {
  683. dnode = dest->addr.id.node;
  684. msg_set_type(mhdr, TIPC_DIRECT_MSG);
  685. msg_set_lookup_scope(mhdr, 0);
  686. msg_set_destnode(mhdr, dnode);
  687. msg_set_destport(mhdr, dest->addr.id.ref);
  688. msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
  689. }
  690. new_mtu:
  691. mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
  692. rc = tipc_msg_build2(mhdr, iov, 0, dsz, mtu, &buf);
  693. if (rc < 0)
  694. goto exit;
  695. do {
  696. rc = tipc_link_xmit2(buf, dnode, tsk->port.ref);
  697. if (likely(rc >= 0)) {
  698. if (sock->state != SS_READY)
  699. sock->state = SS_CONNECTING;
  700. rc = dsz;
  701. break;
  702. }
  703. if (rc == -EMSGSIZE)
  704. goto new_mtu;
  705. if (rc != -ELINKCONG)
  706. break;
  707. rc = tipc_wait_for_sndmsg(sock, &timeo);
  708. if (rc)
  709. kfree_skb_list(buf);
  710. } while (!rc);
  711. exit:
  712. if (iocb)
  713. release_sock(sk);
  714. return rc;
  715. }
  716. static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
  717. {
  718. struct sock *sk = sock->sk;
  719. struct tipc_sock *tsk = tipc_sk(sk);
  720. DEFINE_WAIT(wait);
  721. int done;
  722. do {
  723. int err = sock_error(sk);
  724. if (err)
  725. return err;
  726. if (sock->state == SS_DISCONNECTING)
  727. return -EPIPE;
  728. else if (sock->state != SS_CONNECTED)
  729. return -ENOTCONN;
  730. if (!*timeo_p)
  731. return -EAGAIN;
  732. if (signal_pending(current))
  733. return sock_intr_errno(*timeo_p);
  734. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  735. done = sk_wait_event(sk, timeo_p,
  736. (!tsk->link_cong &&
  737. !tipc_sk_conn_cong(tsk)) ||
  738. !tsk->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->sent_unacked = 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. next:
  790. mtu = port->max_pkt;
  791. send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
  792. rc = tipc_msg_build2(mhdr, m->msg_iov, sent, send, mtu, &buf);
  793. if (unlikely(rc < 0))
  794. goto exit;
  795. do {
  796. if (likely(!tipc_sk_conn_cong(tsk))) {
  797. rc = tipc_link_xmit2(buf, dnode, ref);
  798. if (likely(!rc)) {
  799. tsk->sent_unacked++;
  800. sent += send;
  801. if (sent == dsz)
  802. break;
  803. goto next;
  804. }
  805. if (rc == -EMSGSIZE) {
  806. port->max_pkt = tipc_node_get_mtu(dnode, ref);
  807. goto next;
  808. }
  809. if (rc != -ELINKCONG)
  810. break;
  811. }
  812. rc = tipc_wait_for_sndpkt(sock, &timeo);
  813. if (rc)
  814. kfree_skb_list(buf);
  815. } while (!rc);
  816. exit:
  817. if (iocb)
  818. release_sock(sk);
  819. return sent ? sent : rc;
  820. }
  821. /**
  822. * tipc_send_packet - send a connection-oriented message
  823. * @iocb: if NULL, indicates that socket lock is already held
  824. * @sock: socket structure
  825. * @m: message to send
  826. * @dsz: length of data to be transmitted
  827. *
  828. * Used for SOCK_SEQPACKET messages.
  829. *
  830. * Returns the number of bytes sent on success, or errno otherwise
  831. */
  832. static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
  833. struct msghdr *m, size_t dsz)
  834. {
  835. if (dsz > TIPC_MAX_USER_MSG_SIZE)
  836. return -EMSGSIZE;
  837. return tipc_send_stream(iocb, sock, m, dsz);
  838. }
  839. /**
  840. * auto_connect - complete connection setup to a remote port
  841. * @tsk: tipc socket structure
  842. * @msg: peer's response message
  843. *
  844. * Returns 0 on success, errno otherwise
  845. */
  846. static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
  847. {
  848. struct tipc_port *port = &tsk->port;
  849. struct socket *sock = tsk->sk.sk_socket;
  850. struct tipc_portid peer;
  851. peer.ref = msg_origport(msg);
  852. peer.node = msg_orignode(msg);
  853. __tipc_port_connect(port->ref, port, &peer);
  854. if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
  855. return -EINVAL;
  856. msg_set_importance(&port->phdr, (u32)msg_importance(msg));
  857. sock->state = SS_CONNECTED;
  858. return 0;
  859. }
  860. /**
  861. * set_orig_addr - capture sender's address for received message
  862. * @m: descriptor for message info
  863. * @msg: received message header
  864. *
  865. * Note: Address is not captured if not requested by receiver.
  866. */
  867. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  868. {
  869. DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
  870. if (addr) {
  871. addr->family = AF_TIPC;
  872. addr->addrtype = TIPC_ADDR_ID;
  873. memset(&addr->addr, 0, sizeof(addr->addr));
  874. addr->addr.id.ref = msg_origport(msg);
  875. addr->addr.id.node = msg_orignode(msg);
  876. addr->addr.name.domain = 0; /* could leave uninitialized */
  877. addr->scope = 0; /* could leave uninitialized */
  878. m->msg_namelen = sizeof(struct sockaddr_tipc);
  879. }
  880. }
  881. /**
  882. * anc_data_recv - optionally capture ancillary data for received message
  883. * @m: descriptor for message info
  884. * @msg: received message header
  885. * @tport: TIPC port associated with message
  886. *
  887. * Note: Ancillary data is not captured if not requested by receiver.
  888. *
  889. * Returns 0 if successful, otherwise errno
  890. */
  891. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  892. struct tipc_port *tport)
  893. {
  894. u32 anc_data[3];
  895. u32 err;
  896. u32 dest_type;
  897. int has_name;
  898. int res;
  899. if (likely(m->msg_controllen == 0))
  900. return 0;
  901. /* Optionally capture errored message object(s) */
  902. err = msg ? msg_errcode(msg) : 0;
  903. if (unlikely(err)) {
  904. anc_data[0] = err;
  905. anc_data[1] = msg_data_sz(msg);
  906. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  907. if (res)
  908. return res;
  909. if (anc_data[1]) {
  910. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  911. msg_data(msg));
  912. if (res)
  913. return res;
  914. }
  915. }
  916. /* Optionally capture message destination object */
  917. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  918. switch (dest_type) {
  919. case TIPC_NAMED_MSG:
  920. has_name = 1;
  921. anc_data[0] = msg_nametype(msg);
  922. anc_data[1] = msg_namelower(msg);
  923. anc_data[2] = msg_namelower(msg);
  924. break;
  925. case TIPC_MCAST_MSG:
  926. has_name = 1;
  927. anc_data[0] = msg_nametype(msg);
  928. anc_data[1] = msg_namelower(msg);
  929. anc_data[2] = msg_nameupper(msg);
  930. break;
  931. case TIPC_CONN_MSG:
  932. has_name = (tport->conn_type != 0);
  933. anc_data[0] = tport->conn_type;
  934. anc_data[1] = tport->conn_instance;
  935. anc_data[2] = tport->conn_instance;
  936. break;
  937. default:
  938. has_name = 0;
  939. }
  940. if (has_name) {
  941. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  942. if (res)
  943. return res;
  944. }
  945. return 0;
  946. }
  947. static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
  948. {
  949. struct sock *sk = sock->sk;
  950. DEFINE_WAIT(wait);
  951. long timeo = *timeop;
  952. int err;
  953. for (;;) {
  954. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  955. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  956. if (sock->state == SS_DISCONNECTING) {
  957. err = -ENOTCONN;
  958. break;
  959. }
  960. release_sock(sk);
  961. timeo = schedule_timeout(timeo);
  962. lock_sock(sk);
  963. }
  964. err = 0;
  965. if (!skb_queue_empty(&sk->sk_receive_queue))
  966. break;
  967. err = sock_intr_errno(timeo);
  968. if (signal_pending(current))
  969. break;
  970. err = -EAGAIN;
  971. if (!timeo)
  972. break;
  973. }
  974. finish_wait(sk_sleep(sk), &wait);
  975. *timeop = timeo;
  976. return err;
  977. }
  978. /**
  979. * tipc_recvmsg - receive packet-oriented message
  980. * @iocb: (unused)
  981. * @m: descriptor for message info
  982. * @buf_len: total size of user buffer area
  983. * @flags: receive flags
  984. *
  985. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  986. * If the complete message doesn't fit in user area, truncate it.
  987. *
  988. * Returns size of returned message data, errno otherwise
  989. */
  990. static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
  991. struct msghdr *m, size_t buf_len, int flags)
  992. {
  993. struct sock *sk = sock->sk;
  994. struct tipc_sock *tsk = tipc_sk(sk);
  995. struct tipc_port *port = &tsk->port;
  996. struct sk_buff *buf;
  997. struct tipc_msg *msg;
  998. long timeo;
  999. unsigned int sz;
  1000. u32 err;
  1001. int res;
  1002. /* Catch invalid receive requests */
  1003. if (unlikely(!buf_len))
  1004. return -EINVAL;
  1005. lock_sock(sk);
  1006. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1007. res = -ENOTCONN;
  1008. goto exit;
  1009. }
  1010. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1011. restart:
  1012. /* Look for a message in receive queue; wait if necessary */
  1013. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1014. if (res)
  1015. goto exit;
  1016. /* Look at first message in receive queue */
  1017. buf = skb_peek(&sk->sk_receive_queue);
  1018. msg = buf_msg(buf);
  1019. sz = msg_data_sz(msg);
  1020. err = msg_errcode(msg);
  1021. /* Discard an empty non-errored message & try again */
  1022. if ((!sz) && (!err)) {
  1023. advance_rx_queue(sk);
  1024. goto restart;
  1025. }
  1026. /* Capture sender's address (optional) */
  1027. set_orig_addr(m, msg);
  1028. /* Capture ancillary data (optional) */
  1029. res = anc_data_recv(m, msg, port);
  1030. if (res)
  1031. goto exit;
  1032. /* Capture message data (if valid) & compute return value (always) */
  1033. if (!err) {
  1034. if (unlikely(buf_len < sz)) {
  1035. sz = buf_len;
  1036. m->msg_flags |= MSG_TRUNC;
  1037. }
  1038. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
  1039. m->msg_iov, sz);
  1040. if (res)
  1041. goto exit;
  1042. res = sz;
  1043. } else {
  1044. if ((sock->state == SS_READY) ||
  1045. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  1046. res = 0;
  1047. else
  1048. res = -ECONNRESET;
  1049. }
  1050. /* Consume received message (optional) */
  1051. if (likely(!(flags & MSG_PEEK))) {
  1052. if ((sock->state != SS_READY) &&
  1053. (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
  1054. tipc_acknowledge(port->ref, tsk->rcv_unacked);
  1055. tsk->rcv_unacked = 0;
  1056. }
  1057. advance_rx_queue(sk);
  1058. }
  1059. exit:
  1060. release_sock(sk);
  1061. return res;
  1062. }
  1063. /**
  1064. * tipc_recv_stream - receive stream-oriented data
  1065. * @iocb: (unused)
  1066. * @m: descriptor for message info
  1067. * @buf_len: total size of user buffer area
  1068. * @flags: receive flags
  1069. *
  1070. * Used for SOCK_STREAM messages only. If not enough data is available
  1071. * will optionally wait for more; never truncates data.
  1072. *
  1073. * Returns size of returned message data, errno otherwise
  1074. */
  1075. static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
  1076. struct msghdr *m, size_t buf_len, int flags)
  1077. {
  1078. struct sock *sk = sock->sk;
  1079. struct tipc_sock *tsk = tipc_sk(sk);
  1080. struct tipc_port *port = &tsk->port;
  1081. struct sk_buff *buf;
  1082. struct tipc_msg *msg;
  1083. long timeo;
  1084. unsigned int sz;
  1085. int sz_to_copy, target, needed;
  1086. int sz_copied = 0;
  1087. u32 err;
  1088. int res = 0;
  1089. /* Catch invalid receive attempts */
  1090. if (unlikely(!buf_len))
  1091. return -EINVAL;
  1092. lock_sock(sk);
  1093. if (unlikely(sock->state == SS_UNCONNECTED)) {
  1094. res = -ENOTCONN;
  1095. goto exit;
  1096. }
  1097. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  1098. timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1099. restart:
  1100. /* Look for a message in receive queue; wait if necessary */
  1101. res = tipc_wait_for_rcvmsg(sock, &timeo);
  1102. if (res)
  1103. goto exit;
  1104. /* Look at first message in receive queue */
  1105. buf = skb_peek(&sk->sk_receive_queue);
  1106. msg = buf_msg(buf);
  1107. sz = msg_data_sz(msg);
  1108. err = msg_errcode(msg);
  1109. /* Discard an empty non-errored message & try again */
  1110. if ((!sz) && (!err)) {
  1111. advance_rx_queue(sk);
  1112. goto restart;
  1113. }
  1114. /* Optionally capture sender's address & ancillary data of first msg */
  1115. if (sz_copied == 0) {
  1116. set_orig_addr(m, msg);
  1117. res = anc_data_recv(m, msg, port);
  1118. if (res)
  1119. goto exit;
  1120. }
  1121. /* Capture message data (if valid) & compute return value (always) */
  1122. if (!err) {
  1123. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  1124. sz -= offset;
  1125. needed = (buf_len - sz_copied);
  1126. sz_to_copy = (sz <= needed) ? sz : needed;
  1127. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
  1128. m->msg_iov, sz_to_copy);
  1129. if (res)
  1130. goto exit;
  1131. sz_copied += sz_to_copy;
  1132. if (sz_to_copy < sz) {
  1133. if (!(flags & MSG_PEEK))
  1134. TIPC_SKB_CB(buf)->handle =
  1135. (void *)(unsigned long)(offset + sz_to_copy);
  1136. goto exit;
  1137. }
  1138. } else {
  1139. if (sz_copied != 0)
  1140. goto exit; /* can't add error msg to valid data */
  1141. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  1142. res = 0;
  1143. else
  1144. res = -ECONNRESET;
  1145. }
  1146. /* Consume received message (optional) */
  1147. if (likely(!(flags & MSG_PEEK))) {
  1148. if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
  1149. tipc_acknowledge(port->ref, tsk->rcv_unacked);
  1150. tsk->rcv_unacked = 0;
  1151. }
  1152. advance_rx_queue(sk);
  1153. }
  1154. /* Loop around if more data is required */
  1155. if ((sz_copied < buf_len) && /* didn't get all requested data */
  1156. (!skb_queue_empty(&sk->sk_receive_queue) ||
  1157. (sz_copied < target)) && /* and more is ready or required */
  1158. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  1159. (!err)) /* and haven't reached a FIN */
  1160. goto restart;
  1161. exit:
  1162. release_sock(sk);
  1163. return sz_copied ? sz_copied : res;
  1164. }
  1165. /**
  1166. * tipc_write_space - wake up thread if port congestion is released
  1167. * @sk: socket
  1168. */
  1169. static void tipc_write_space(struct sock *sk)
  1170. {
  1171. struct socket_wq *wq;
  1172. rcu_read_lock();
  1173. wq = rcu_dereference(sk->sk_wq);
  1174. if (wq_has_sleeper(wq))
  1175. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1176. POLLWRNORM | POLLWRBAND);
  1177. rcu_read_unlock();
  1178. }
  1179. /**
  1180. * tipc_data_ready - wake up threads to indicate messages have been received
  1181. * @sk: socket
  1182. * @len: the length of messages
  1183. */
  1184. static void tipc_data_ready(struct sock *sk)
  1185. {
  1186. struct socket_wq *wq;
  1187. rcu_read_lock();
  1188. wq = rcu_dereference(sk->sk_wq);
  1189. if (wq_has_sleeper(wq))
  1190. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1191. POLLRDNORM | POLLRDBAND);
  1192. rcu_read_unlock();
  1193. }
  1194. /**
  1195. * filter_connect - Handle all incoming messages for a connection-based socket
  1196. * @tsk: TIPC socket
  1197. * @msg: message
  1198. *
  1199. * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
  1200. */
  1201. static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
  1202. {
  1203. struct sock *sk = &tsk->sk;
  1204. struct tipc_port *port = &tsk->port;
  1205. struct socket *sock = sk->sk_socket;
  1206. struct tipc_msg *msg = buf_msg(*buf);
  1207. int retval = -TIPC_ERR_NO_PORT;
  1208. int res;
  1209. if (msg_mcast(msg))
  1210. return retval;
  1211. switch ((int)sock->state) {
  1212. case SS_CONNECTED:
  1213. /* Accept only connection-based messages sent by peer */
  1214. if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
  1215. if (unlikely(msg_errcode(msg))) {
  1216. sock->state = SS_DISCONNECTING;
  1217. __tipc_port_disconnect(port);
  1218. }
  1219. retval = TIPC_OK;
  1220. }
  1221. break;
  1222. case SS_CONNECTING:
  1223. /* Accept only ACK or NACK message */
  1224. if (unlikely(msg_errcode(msg))) {
  1225. sock->state = SS_DISCONNECTING;
  1226. sk->sk_err = ECONNREFUSED;
  1227. retval = TIPC_OK;
  1228. break;
  1229. }
  1230. if (unlikely(!msg_connected(msg)))
  1231. break;
  1232. res = auto_connect(tsk, msg);
  1233. if (res) {
  1234. sock->state = SS_DISCONNECTING;
  1235. sk->sk_err = -res;
  1236. retval = TIPC_OK;
  1237. break;
  1238. }
  1239. /* If an incoming message is an 'ACK-', it should be
  1240. * discarded here because it doesn't contain useful
  1241. * data. In addition, we should try to wake up
  1242. * connect() routine if sleeping.
  1243. */
  1244. if (msg_data_sz(msg) == 0) {
  1245. kfree_skb(*buf);
  1246. *buf = NULL;
  1247. if (waitqueue_active(sk_sleep(sk)))
  1248. wake_up_interruptible(sk_sleep(sk));
  1249. }
  1250. retval = TIPC_OK;
  1251. break;
  1252. case SS_LISTENING:
  1253. case SS_UNCONNECTED:
  1254. /* Accept only SYN message */
  1255. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1256. retval = TIPC_OK;
  1257. break;
  1258. case SS_DISCONNECTING:
  1259. break;
  1260. default:
  1261. pr_err("Unknown socket state %u\n", sock->state);
  1262. }
  1263. return retval;
  1264. }
  1265. /**
  1266. * rcvbuf_limit - get proper overload limit of socket receive queue
  1267. * @sk: socket
  1268. * @buf: message
  1269. *
  1270. * For all connection oriented messages, irrespective of importance,
  1271. * the default overload value (i.e. 67MB) is set as limit.
  1272. *
  1273. * For all connectionless messages, by default new queue limits are
  1274. * as belows:
  1275. *
  1276. * TIPC_LOW_IMPORTANCE (4 MB)
  1277. * TIPC_MEDIUM_IMPORTANCE (8 MB)
  1278. * TIPC_HIGH_IMPORTANCE (16 MB)
  1279. * TIPC_CRITICAL_IMPORTANCE (32 MB)
  1280. *
  1281. * Returns overload limit according to corresponding message importance
  1282. */
  1283. static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
  1284. {
  1285. struct tipc_msg *msg = buf_msg(buf);
  1286. if (msg_connected(msg))
  1287. return sysctl_tipc_rmem[2];
  1288. return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
  1289. msg_importance(msg);
  1290. }
  1291. /**
  1292. * filter_rcv - validate incoming message
  1293. * @sk: socket
  1294. * @buf: message
  1295. *
  1296. * Enqueues message on receive queue if acceptable; optionally handles
  1297. * disconnect indication for a connected socket.
  1298. *
  1299. * Called with socket lock already taken; port lock may also be taken.
  1300. *
  1301. * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
  1302. * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
  1303. */
  1304. static int filter_rcv(struct sock *sk, struct sk_buff *buf)
  1305. {
  1306. struct socket *sock = sk->sk_socket;
  1307. struct tipc_sock *tsk = tipc_sk(sk);
  1308. struct tipc_msg *msg = buf_msg(buf);
  1309. unsigned int limit = rcvbuf_limit(sk, buf);
  1310. u32 onode;
  1311. int rc = TIPC_OK;
  1312. if (unlikely(msg_user(msg) == CONN_MANAGER))
  1313. return tipc_sk_proto_rcv(tsk, &onode, buf);
  1314. /* Reject message if it is wrong sort of message for socket */
  1315. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1316. return -TIPC_ERR_NO_PORT;
  1317. if (sock->state == SS_READY) {
  1318. if (msg_connected(msg))
  1319. return -TIPC_ERR_NO_PORT;
  1320. } else {
  1321. rc = filter_connect(tsk, &buf);
  1322. if (rc != TIPC_OK || buf == NULL)
  1323. return rc;
  1324. }
  1325. /* Reject message if there isn't room to queue it */
  1326. if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
  1327. return -TIPC_ERR_OVERLOAD;
  1328. /* Enqueue message */
  1329. TIPC_SKB_CB(buf)->handle = NULL;
  1330. __skb_queue_tail(&sk->sk_receive_queue, buf);
  1331. skb_set_owner_r(buf, sk);
  1332. sk->sk_data_ready(sk);
  1333. return TIPC_OK;
  1334. }
  1335. /**
  1336. * tipc_backlog_rcv - handle incoming message from backlog queue
  1337. * @sk: socket
  1338. * @buf: message
  1339. *
  1340. * Caller must hold socket lock, but not port lock.
  1341. *
  1342. * Returns 0
  1343. */
  1344. static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
  1345. {
  1346. int rc;
  1347. u32 onode;
  1348. struct tipc_sock *tsk = tipc_sk(sk);
  1349. uint truesize = buf->truesize;
  1350. rc = filter_rcv(sk, buf);
  1351. if (likely(!rc)) {
  1352. if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
  1353. atomic_add(truesize, &tsk->dupl_rcvcnt);
  1354. return 0;
  1355. }
  1356. if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
  1357. return 0;
  1358. tipc_link_xmit2(buf, onode, 0);
  1359. return 0;
  1360. }
  1361. /**
  1362. * tipc_sk_rcv - handle incoming message
  1363. * @buf: buffer containing arriving message
  1364. * Consumes buffer
  1365. * Returns 0 if success, or errno: -EHOSTUNREACH
  1366. */
  1367. int tipc_sk_rcv(struct sk_buff *buf)
  1368. {
  1369. struct tipc_sock *tsk;
  1370. struct tipc_port *port;
  1371. struct sock *sk;
  1372. u32 dport = msg_destport(buf_msg(buf));
  1373. int rc = TIPC_OK;
  1374. uint limit;
  1375. u32 dnode;
  1376. /* Validate destination and message */
  1377. port = tipc_port_lock(dport);
  1378. if (unlikely(!port)) {
  1379. rc = tipc_msg_eval(buf, &dnode);
  1380. goto exit;
  1381. }
  1382. tsk = tipc_port_to_sock(port);
  1383. sk = &tsk->sk;
  1384. /* Queue message */
  1385. bh_lock_sock(sk);
  1386. if (!sock_owned_by_user(sk)) {
  1387. rc = filter_rcv(sk, buf);
  1388. } else {
  1389. if (sk->sk_backlog.len == 0)
  1390. atomic_set(&tsk->dupl_rcvcnt, 0);
  1391. limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
  1392. if (sk_add_backlog(sk, buf, limit))
  1393. rc = -TIPC_ERR_OVERLOAD;
  1394. }
  1395. bh_unlock_sock(sk);
  1396. tipc_port_unlock(port);
  1397. if (likely(!rc))
  1398. return 0;
  1399. exit:
  1400. if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
  1401. return -EHOSTUNREACH;
  1402. tipc_link_xmit2(buf, dnode, 0);
  1403. return (rc < 0) ? -EHOSTUNREACH : 0;
  1404. }
  1405. static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
  1406. {
  1407. struct sock *sk = sock->sk;
  1408. DEFINE_WAIT(wait);
  1409. int done;
  1410. do {
  1411. int err = sock_error(sk);
  1412. if (err)
  1413. return err;
  1414. if (!*timeo_p)
  1415. return -ETIMEDOUT;
  1416. if (signal_pending(current))
  1417. return sock_intr_errno(*timeo_p);
  1418. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1419. done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
  1420. finish_wait(sk_sleep(sk), &wait);
  1421. } while (!done);
  1422. return 0;
  1423. }
  1424. /**
  1425. * tipc_connect - establish a connection to another TIPC port
  1426. * @sock: socket structure
  1427. * @dest: socket address for destination port
  1428. * @destlen: size of socket address data structure
  1429. * @flags: file-related flags associated with socket
  1430. *
  1431. * Returns 0 on success, errno otherwise
  1432. */
  1433. static int tipc_connect(struct socket *sock, struct sockaddr *dest,
  1434. int destlen, int flags)
  1435. {
  1436. struct sock *sk = sock->sk;
  1437. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1438. struct msghdr m = {NULL,};
  1439. long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
  1440. socket_state previous;
  1441. int res;
  1442. lock_sock(sk);
  1443. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1444. if (sock->state == SS_READY) {
  1445. res = -EOPNOTSUPP;
  1446. goto exit;
  1447. }
  1448. /*
  1449. * Reject connection attempt using multicast address
  1450. *
  1451. * Note: send_msg() validates the rest of the address fields,
  1452. * so there's no need to do it here
  1453. */
  1454. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1455. res = -EINVAL;
  1456. goto exit;
  1457. }
  1458. previous = sock->state;
  1459. switch (sock->state) {
  1460. case SS_UNCONNECTED:
  1461. /* Send a 'SYN-' to destination */
  1462. m.msg_name = dest;
  1463. m.msg_namelen = destlen;
  1464. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1465. * indicate send_msg() is never blocked.
  1466. */
  1467. if (!timeout)
  1468. m.msg_flags = MSG_DONTWAIT;
  1469. res = tipc_sendmsg(NULL, sock, &m, 0);
  1470. if ((res < 0) && (res != -EWOULDBLOCK))
  1471. goto exit;
  1472. /* Just entered SS_CONNECTING state; the only
  1473. * difference is that return value in non-blocking
  1474. * case is EINPROGRESS, rather than EALREADY.
  1475. */
  1476. res = -EINPROGRESS;
  1477. case SS_CONNECTING:
  1478. if (previous == SS_CONNECTING)
  1479. res = -EALREADY;
  1480. if (!timeout)
  1481. goto exit;
  1482. timeout = msecs_to_jiffies(timeout);
  1483. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1484. res = tipc_wait_for_connect(sock, &timeout);
  1485. break;
  1486. case SS_CONNECTED:
  1487. res = -EISCONN;
  1488. break;
  1489. default:
  1490. res = -EINVAL;
  1491. break;
  1492. }
  1493. exit:
  1494. release_sock(sk);
  1495. return res;
  1496. }
  1497. /**
  1498. * tipc_listen - allow socket to listen for incoming connections
  1499. * @sock: socket structure
  1500. * @len: (unused)
  1501. *
  1502. * Returns 0 on success, errno otherwise
  1503. */
  1504. static int tipc_listen(struct socket *sock, int len)
  1505. {
  1506. struct sock *sk = sock->sk;
  1507. int res;
  1508. lock_sock(sk);
  1509. if (sock->state != SS_UNCONNECTED)
  1510. res = -EINVAL;
  1511. else {
  1512. sock->state = SS_LISTENING;
  1513. res = 0;
  1514. }
  1515. release_sock(sk);
  1516. return res;
  1517. }
  1518. static int tipc_wait_for_accept(struct socket *sock, long timeo)
  1519. {
  1520. struct sock *sk = sock->sk;
  1521. DEFINE_WAIT(wait);
  1522. int err;
  1523. /* True wake-one mechanism for incoming connections: only
  1524. * one process gets woken up, not the 'whole herd'.
  1525. * Since we do not 'race & poll' for established sockets
  1526. * anymore, the common case will execute the loop only once.
  1527. */
  1528. for (;;) {
  1529. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  1530. TASK_INTERRUPTIBLE);
  1531. if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
  1532. release_sock(sk);
  1533. timeo = schedule_timeout(timeo);
  1534. lock_sock(sk);
  1535. }
  1536. err = 0;
  1537. if (!skb_queue_empty(&sk->sk_receive_queue))
  1538. break;
  1539. err = -EINVAL;
  1540. if (sock->state != SS_LISTENING)
  1541. break;
  1542. err = sock_intr_errno(timeo);
  1543. if (signal_pending(current))
  1544. break;
  1545. err = -EAGAIN;
  1546. if (!timeo)
  1547. break;
  1548. }
  1549. finish_wait(sk_sleep(sk), &wait);
  1550. return err;
  1551. }
  1552. /**
  1553. * tipc_accept - wait for connection request
  1554. * @sock: listening socket
  1555. * @newsock: new socket that is to be connected
  1556. * @flags: file-related flags associated with socket
  1557. *
  1558. * Returns 0 on success, errno otherwise
  1559. */
  1560. static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
  1561. {
  1562. struct sock *new_sk, *sk = sock->sk;
  1563. struct sk_buff *buf;
  1564. struct tipc_port *new_port;
  1565. struct tipc_msg *msg;
  1566. struct tipc_portid peer;
  1567. u32 new_ref;
  1568. long timeo;
  1569. int res;
  1570. lock_sock(sk);
  1571. if (sock->state != SS_LISTENING) {
  1572. res = -EINVAL;
  1573. goto exit;
  1574. }
  1575. timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  1576. res = tipc_wait_for_accept(sock, timeo);
  1577. if (res)
  1578. goto exit;
  1579. buf = skb_peek(&sk->sk_receive_queue);
  1580. res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
  1581. if (res)
  1582. goto exit;
  1583. new_sk = new_sock->sk;
  1584. new_port = &tipc_sk(new_sk)->port;
  1585. new_ref = new_port->ref;
  1586. msg = buf_msg(buf);
  1587. /* we lock on new_sk; but lockdep sees the lock on sk */
  1588. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1589. /*
  1590. * Reject any stray messages received by new socket
  1591. * before the socket lock was taken (very, very unlikely)
  1592. */
  1593. reject_rx_queue(new_sk);
  1594. /* Connect new socket to it's peer */
  1595. peer.ref = msg_origport(msg);
  1596. peer.node = msg_orignode(msg);
  1597. tipc_port_connect(new_ref, &peer);
  1598. new_sock->state = SS_CONNECTED;
  1599. tipc_port_set_importance(new_port, msg_importance(msg));
  1600. if (msg_named(msg)) {
  1601. new_port->conn_type = msg_nametype(msg);
  1602. new_port->conn_instance = msg_nameinst(msg);
  1603. }
  1604. /*
  1605. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1606. * Respond to 'SYN+' by queuing it on new socket.
  1607. */
  1608. if (!msg_data_sz(msg)) {
  1609. struct msghdr m = {NULL,};
  1610. advance_rx_queue(sk);
  1611. tipc_send_packet(NULL, new_sock, &m, 0);
  1612. } else {
  1613. __skb_dequeue(&sk->sk_receive_queue);
  1614. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1615. skb_set_owner_r(buf, new_sk);
  1616. }
  1617. release_sock(new_sk);
  1618. exit:
  1619. release_sock(sk);
  1620. return res;
  1621. }
  1622. /**
  1623. * tipc_shutdown - shutdown socket connection
  1624. * @sock: socket structure
  1625. * @how: direction to close (must be SHUT_RDWR)
  1626. *
  1627. * Terminates connection (if necessary), then purges socket's receive queue.
  1628. *
  1629. * Returns 0 on success, errno otherwise
  1630. */
  1631. static int tipc_shutdown(struct socket *sock, int how)
  1632. {
  1633. struct sock *sk = sock->sk;
  1634. struct tipc_sock *tsk = tipc_sk(sk);
  1635. struct tipc_port *port = &tsk->port;
  1636. struct sk_buff *buf;
  1637. u32 peer;
  1638. int res;
  1639. if (how != SHUT_RDWR)
  1640. return -EINVAL;
  1641. lock_sock(sk);
  1642. switch (sock->state) {
  1643. case SS_CONNECTING:
  1644. case SS_CONNECTED:
  1645. restart:
  1646. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1647. buf = __skb_dequeue(&sk->sk_receive_queue);
  1648. if (buf) {
  1649. if (TIPC_SKB_CB(buf)->handle != NULL) {
  1650. kfree_skb(buf);
  1651. goto restart;
  1652. }
  1653. tipc_port_disconnect(port->ref);
  1654. if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN))
  1655. tipc_link_xmit2(buf, peer, 0);
  1656. } else {
  1657. tipc_port_shutdown(port->ref);
  1658. }
  1659. sock->state = SS_DISCONNECTING;
  1660. /* fall through */
  1661. case SS_DISCONNECTING:
  1662. /* Discard any unreceived messages */
  1663. __skb_queue_purge(&sk->sk_receive_queue);
  1664. /* Wake up anyone sleeping in poll */
  1665. sk->sk_state_change(sk);
  1666. res = 0;
  1667. break;
  1668. default:
  1669. res = -ENOTCONN;
  1670. }
  1671. release_sock(sk);
  1672. return res;
  1673. }
  1674. /**
  1675. * tipc_setsockopt - set socket option
  1676. * @sock: socket structure
  1677. * @lvl: option level
  1678. * @opt: option identifier
  1679. * @ov: pointer to new option value
  1680. * @ol: length of option value
  1681. *
  1682. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1683. * (to ease compatibility).
  1684. *
  1685. * Returns 0 on success, errno otherwise
  1686. */
  1687. static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
  1688. char __user *ov, unsigned int ol)
  1689. {
  1690. struct sock *sk = sock->sk;
  1691. struct tipc_sock *tsk = tipc_sk(sk);
  1692. struct tipc_port *port = &tsk->port;
  1693. u32 value;
  1694. int res;
  1695. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1696. return 0;
  1697. if (lvl != SOL_TIPC)
  1698. return -ENOPROTOOPT;
  1699. if (ol < sizeof(value))
  1700. return -EINVAL;
  1701. res = get_user(value, (u32 __user *)ov);
  1702. if (res)
  1703. return res;
  1704. lock_sock(sk);
  1705. switch (opt) {
  1706. case TIPC_IMPORTANCE:
  1707. tipc_port_set_importance(port, value);
  1708. break;
  1709. case TIPC_SRC_DROPPABLE:
  1710. if (sock->type != SOCK_STREAM)
  1711. tipc_port_set_unreliable(port, value);
  1712. else
  1713. res = -ENOPROTOOPT;
  1714. break;
  1715. case TIPC_DEST_DROPPABLE:
  1716. tipc_port_set_unreturnable(port, value);
  1717. break;
  1718. case TIPC_CONN_TIMEOUT:
  1719. tipc_sk(sk)->conn_timeout = value;
  1720. /* no need to set "res", since already 0 at this point */
  1721. break;
  1722. default:
  1723. res = -EINVAL;
  1724. }
  1725. release_sock(sk);
  1726. return res;
  1727. }
  1728. /**
  1729. * tipc_getsockopt - get socket option
  1730. * @sock: socket structure
  1731. * @lvl: option level
  1732. * @opt: option identifier
  1733. * @ov: receptacle for option value
  1734. * @ol: receptacle for length of option value
  1735. *
  1736. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1737. * (to ease compatibility).
  1738. *
  1739. * Returns 0 on success, errno otherwise
  1740. */
  1741. static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
  1742. char __user *ov, int __user *ol)
  1743. {
  1744. struct sock *sk = sock->sk;
  1745. struct tipc_sock *tsk = tipc_sk(sk);
  1746. struct tipc_port *port = &tsk->port;
  1747. int len;
  1748. u32 value;
  1749. int res;
  1750. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1751. return put_user(0, ol);
  1752. if (lvl != SOL_TIPC)
  1753. return -ENOPROTOOPT;
  1754. res = get_user(len, ol);
  1755. if (res)
  1756. return res;
  1757. lock_sock(sk);
  1758. switch (opt) {
  1759. case TIPC_IMPORTANCE:
  1760. value = tipc_port_importance(port);
  1761. break;
  1762. case TIPC_SRC_DROPPABLE:
  1763. value = tipc_port_unreliable(port);
  1764. break;
  1765. case TIPC_DEST_DROPPABLE:
  1766. value = tipc_port_unreturnable(port);
  1767. break;
  1768. case TIPC_CONN_TIMEOUT:
  1769. value = tipc_sk(sk)->conn_timeout;
  1770. /* no need to set "res", since already 0 at this point */
  1771. break;
  1772. case TIPC_NODE_RECVQ_DEPTH:
  1773. value = 0; /* was tipc_queue_size, now obsolete */
  1774. break;
  1775. case TIPC_SOCK_RECVQ_DEPTH:
  1776. value = skb_queue_len(&sk->sk_receive_queue);
  1777. break;
  1778. default:
  1779. res = -EINVAL;
  1780. }
  1781. release_sock(sk);
  1782. if (res)
  1783. return res; /* "get" failed */
  1784. if (len < sizeof(value))
  1785. return -EINVAL;
  1786. if (copy_to_user(ov, &value, sizeof(value)))
  1787. return -EFAULT;
  1788. return put_user(sizeof(value), ol);
  1789. }
  1790. int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
  1791. {
  1792. struct tipc_sioc_ln_req lnr;
  1793. void __user *argp = (void __user *)arg;
  1794. switch (cmd) {
  1795. case SIOCGETLINKNAME:
  1796. if (copy_from_user(&lnr, argp, sizeof(lnr)))
  1797. return -EFAULT;
  1798. if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
  1799. lnr.linkname, TIPC_MAX_LINK_NAME)) {
  1800. if (copy_to_user(argp, &lnr, sizeof(lnr)))
  1801. return -EFAULT;
  1802. return 0;
  1803. }
  1804. return -EADDRNOTAVAIL;
  1805. default:
  1806. return -ENOIOCTLCMD;
  1807. }
  1808. }
  1809. /* Protocol switches for the various types of TIPC sockets */
  1810. static const struct proto_ops msg_ops = {
  1811. .owner = THIS_MODULE,
  1812. .family = AF_TIPC,
  1813. .release = tipc_release,
  1814. .bind = tipc_bind,
  1815. .connect = tipc_connect,
  1816. .socketpair = sock_no_socketpair,
  1817. .accept = sock_no_accept,
  1818. .getname = tipc_getname,
  1819. .poll = tipc_poll,
  1820. .ioctl = tipc_ioctl,
  1821. .listen = sock_no_listen,
  1822. .shutdown = tipc_shutdown,
  1823. .setsockopt = tipc_setsockopt,
  1824. .getsockopt = tipc_getsockopt,
  1825. .sendmsg = tipc_sendmsg,
  1826. .recvmsg = tipc_recvmsg,
  1827. .mmap = sock_no_mmap,
  1828. .sendpage = sock_no_sendpage
  1829. };
  1830. static const struct proto_ops packet_ops = {
  1831. .owner = THIS_MODULE,
  1832. .family = AF_TIPC,
  1833. .release = tipc_release,
  1834. .bind = tipc_bind,
  1835. .connect = tipc_connect,
  1836. .socketpair = sock_no_socketpair,
  1837. .accept = tipc_accept,
  1838. .getname = tipc_getname,
  1839. .poll = tipc_poll,
  1840. .ioctl = tipc_ioctl,
  1841. .listen = tipc_listen,
  1842. .shutdown = tipc_shutdown,
  1843. .setsockopt = tipc_setsockopt,
  1844. .getsockopt = tipc_getsockopt,
  1845. .sendmsg = tipc_send_packet,
  1846. .recvmsg = tipc_recvmsg,
  1847. .mmap = sock_no_mmap,
  1848. .sendpage = sock_no_sendpage
  1849. };
  1850. static const struct proto_ops stream_ops = {
  1851. .owner = THIS_MODULE,
  1852. .family = AF_TIPC,
  1853. .release = tipc_release,
  1854. .bind = tipc_bind,
  1855. .connect = tipc_connect,
  1856. .socketpair = sock_no_socketpair,
  1857. .accept = tipc_accept,
  1858. .getname = tipc_getname,
  1859. .poll = tipc_poll,
  1860. .ioctl = tipc_ioctl,
  1861. .listen = tipc_listen,
  1862. .shutdown = tipc_shutdown,
  1863. .setsockopt = tipc_setsockopt,
  1864. .getsockopt = tipc_getsockopt,
  1865. .sendmsg = tipc_send_stream,
  1866. .recvmsg = tipc_recv_stream,
  1867. .mmap = sock_no_mmap,
  1868. .sendpage = sock_no_sendpage
  1869. };
  1870. static const struct net_proto_family tipc_family_ops = {
  1871. .owner = THIS_MODULE,
  1872. .family = AF_TIPC,
  1873. .create = tipc_sk_create
  1874. };
  1875. static struct proto tipc_proto = {
  1876. .name = "TIPC",
  1877. .owner = THIS_MODULE,
  1878. .obj_size = sizeof(struct tipc_sock),
  1879. .sysctl_rmem = sysctl_tipc_rmem
  1880. };
  1881. static struct proto tipc_proto_kern = {
  1882. .name = "TIPC",
  1883. .obj_size = sizeof(struct tipc_sock),
  1884. .sysctl_rmem = sysctl_tipc_rmem
  1885. };
  1886. /**
  1887. * tipc_socket_init - initialize TIPC socket interface
  1888. *
  1889. * Returns 0 on success, errno otherwise
  1890. */
  1891. int tipc_socket_init(void)
  1892. {
  1893. int res;
  1894. res = proto_register(&tipc_proto, 1);
  1895. if (res) {
  1896. pr_err("Failed to register TIPC protocol type\n");
  1897. goto out;
  1898. }
  1899. res = sock_register(&tipc_family_ops);
  1900. if (res) {
  1901. pr_err("Failed to register TIPC socket type\n");
  1902. proto_unregister(&tipc_proto);
  1903. goto out;
  1904. }
  1905. out:
  1906. return res;
  1907. }
  1908. /**
  1909. * tipc_socket_stop - stop TIPC socket interface
  1910. */
  1911. void tipc_socket_stop(void)
  1912. {
  1913. sock_unregister(tipc_family_ops.family);
  1914. proto_unregister(&tipc_proto);
  1915. }