socket.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. /*
  2. * net/tipc/socket.c: TIPC socket API
  3. *
  4. * Copyright (c) 2001-2007, 2012 Ericsson AB
  5. * Copyright (c) 2004-2008, 2010-2012, 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 <linux/export.h>
  39. #include <net/sock.h>
  40. #define SS_LISTENING -1 /* socket is listening */
  41. #define SS_READY -2 /* socket is connectionless */
  42. #define OVERLOAD_LIMIT_BASE 10000
  43. #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
  44. struct tipc_sock {
  45. struct sock sk;
  46. struct tipc_port *p;
  47. struct tipc_portid peer_name;
  48. unsigned int conn_timeout;
  49. };
  50. #define tipc_sk(sk) ((struct tipc_sock *)(sk))
  51. #define tipc_sk_port(sk) (tipc_sk(sk)->p)
  52. #define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
  53. (sock->state == SS_DISCONNECTING))
  54. static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
  55. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
  56. static void wakeupdispatch(struct tipc_port *tport);
  57. static void tipc_data_ready(struct sock *sk, int len);
  58. static void tipc_write_space(struct sock *sk);
  59. static const struct proto_ops packet_ops;
  60. static const struct proto_ops stream_ops;
  61. static const struct proto_ops msg_ops;
  62. static struct proto tipc_proto;
  63. static int sockets_enabled;
  64. /*
  65. * Revised TIPC socket locking policy:
  66. *
  67. * Most socket operations take the standard socket lock when they start
  68. * and hold it until they finish (or until they need to sleep). Acquiring
  69. * this lock grants the owner exclusive access to the fields of the socket
  70. * data structures, with the exception of the backlog queue. A few socket
  71. * operations can be done without taking the socket lock because they only
  72. * read socket information that never changes during the life of the socket.
  73. *
  74. * Socket operations may acquire the lock for the associated TIPC port if they
  75. * need to perform an operation on the port. If any routine needs to acquire
  76. * both the socket lock and the port lock it must take the socket lock first
  77. * to avoid the risk of deadlock.
  78. *
  79. * The dispatcher handling incoming messages cannot grab the socket lock in
  80. * the standard fashion, since invoked it runs at the BH level and cannot block.
  81. * Instead, it checks to see if the socket lock is currently owned by someone,
  82. * and either handles the message itself or adds it to the socket's backlog
  83. * queue; in the latter case the queued message is processed once the process
  84. * owning the socket lock releases it.
  85. *
  86. * NOTE: Releasing the socket lock while an operation is sleeping overcomes
  87. * the problem of a blocked socket operation preventing any other operations
  88. * from occurring. However, applications must be careful if they have
  89. * multiple threads trying to send (or receive) on the same socket, as these
  90. * operations might interfere with each other. For example, doing a connect
  91. * and a receive at the same time might allow the receive to consume the
  92. * ACK message meant for the connect. While additional work could be done
  93. * to try and overcome this, it doesn't seem to be worthwhile at the present.
  94. *
  95. * NOTE: Releasing the socket lock while an operation is sleeping also ensures
  96. * that another operation that must be performed in a non-blocking manner is
  97. * not delayed for very long because the lock has already been taken.
  98. *
  99. * NOTE: This code assumes that certain fields of a port/socket pair are
  100. * constant over its lifetime; such fields can be examined without taking
  101. * the socket lock and/or port lock, and do not need to be re-read even
  102. * after resuming processing after waiting. These fields include:
  103. * - socket type
  104. * - pointer to socket sk structure (aka tipc_sock structure)
  105. * - pointer to port structure
  106. * - port reference
  107. */
  108. /**
  109. * advance_rx_queue - discard first buffer in socket receive queue
  110. *
  111. * Caller must hold socket lock
  112. */
  113. static void advance_rx_queue(struct sock *sk)
  114. {
  115. kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
  116. }
  117. /**
  118. * discard_rx_queue - discard all buffers in socket receive queue
  119. *
  120. * Caller must hold socket lock
  121. */
  122. static void discard_rx_queue(struct sock *sk)
  123. {
  124. struct sk_buff *buf;
  125. while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
  126. kfree_skb(buf);
  127. }
  128. /**
  129. * reject_rx_queue - reject all buffers in socket receive queue
  130. *
  131. * Caller must hold socket lock
  132. */
  133. static void reject_rx_queue(struct sock *sk)
  134. {
  135. struct sk_buff *buf;
  136. while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
  137. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  138. }
  139. /**
  140. * tipc_create - create a TIPC socket
  141. * @net: network namespace (must be default network)
  142. * @sock: pre-allocated socket structure
  143. * @protocol: protocol indicator (must be 0)
  144. * @kern: caused by kernel or by userspace?
  145. *
  146. * This routine creates additional data structures used by the TIPC socket,
  147. * initializes them, and links them together.
  148. *
  149. * Returns 0 on success, errno otherwise
  150. */
  151. static int tipc_create(struct net *net, struct socket *sock, int protocol,
  152. int kern)
  153. {
  154. const struct proto_ops *ops;
  155. socket_state state;
  156. struct sock *sk;
  157. struct tipc_port *tp_ptr;
  158. /* Validate arguments */
  159. if (unlikely(protocol != 0))
  160. return -EPROTONOSUPPORT;
  161. switch (sock->type) {
  162. case SOCK_STREAM:
  163. ops = &stream_ops;
  164. state = SS_UNCONNECTED;
  165. break;
  166. case SOCK_SEQPACKET:
  167. ops = &packet_ops;
  168. state = SS_UNCONNECTED;
  169. break;
  170. case SOCK_DGRAM:
  171. case SOCK_RDM:
  172. ops = &msg_ops;
  173. state = SS_READY;
  174. break;
  175. default:
  176. return -EPROTOTYPE;
  177. }
  178. /* Allocate socket's protocol area */
  179. sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
  180. if (sk == NULL)
  181. return -ENOMEM;
  182. /* Allocate TIPC port for socket to use */
  183. tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
  184. TIPC_LOW_IMPORTANCE);
  185. if (unlikely(!tp_ptr)) {
  186. sk_free(sk);
  187. return -ENOMEM;
  188. }
  189. /* Finish initializing socket data structures */
  190. sock->ops = ops;
  191. sock->state = state;
  192. sock_init_data(sock, sk);
  193. sk->sk_backlog_rcv = backlog_rcv;
  194. sk->sk_rcvbuf = TIPC_FLOW_CONTROL_WIN * 2 * TIPC_MAX_USER_MSG_SIZE * 2;
  195. sk->sk_data_ready = tipc_data_ready;
  196. sk->sk_write_space = tipc_write_space;
  197. tipc_sk(sk)->p = tp_ptr;
  198. tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
  199. spin_unlock_bh(tp_ptr->lock);
  200. if (sock->state == SS_READY) {
  201. tipc_set_portunreturnable(tp_ptr->ref, 1);
  202. if (sock->type == SOCK_DGRAM)
  203. tipc_set_portunreliable(tp_ptr->ref, 1);
  204. }
  205. return 0;
  206. }
  207. /**
  208. * release - destroy a TIPC socket
  209. * @sock: socket to destroy
  210. *
  211. * This routine cleans up any messages that are still queued on the socket.
  212. * For DGRAM and RDM socket types, all queued messages are rejected.
  213. * For SEQPACKET and STREAM socket types, the first message is rejected
  214. * and any others are discarded. (If the first message on a STREAM socket
  215. * is partially-read, it is discarded and the next one is rejected instead.)
  216. *
  217. * NOTE: Rejected messages are not necessarily returned to the sender! They
  218. * are returned or discarded according to the "destination droppable" setting
  219. * specified for the message by the sender.
  220. *
  221. * Returns 0 on success, errno otherwise
  222. */
  223. static int release(struct socket *sock)
  224. {
  225. struct sock *sk = sock->sk;
  226. struct tipc_port *tport;
  227. struct sk_buff *buf;
  228. int res;
  229. /*
  230. * Exit if socket isn't fully initialized (occurs when a failed accept()
  231. * releases a pre-allocated child socket that was never used)
  232. */
  233. if (sk == NULL)
  234. return 0;
  235. tport = tipc_sk_port(sk);
  236. lock_sock(sk);
  237. /*
  238. * Reject all unreceived messages, except on an active connection
  239. * (which disconnects locally & sends a 'FIN+' to peer)
  240. */
  241. while (sock->state != SS_DISCONNECTING) {
  242. buf = __skb_dequeue(&sk->sk_receive_queue);
  243. if (buf == NULL)
  244. break;
  245. if (TIPC_SKB_CB(buf)->handle != 0)
  246. kfree_skb(buf);
  247. else {
  248. if ((sock->state == SS_CONNECTING) ||
  249. (sock->state == SS_CONNECTED)) {
  250. sock->state = SS_DISCONNECTING;
  251. tipc_disconnect(tport->ref);
  252. }
  253. tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
  254. }
  255. }
  256. /*
  257. * Delete TIPC port; this ensures no more messages are queued
  258. * (also disconnects an active connection & sends a 'FIN-' to peer)
  259. */
  260. res = tipc_deleteport(tport->ref);
  261. /* Discard any remaining (connection-based) messages in receive queue */
  262. discard_rx_queue(sk);
  263. /* Reject any messages that accumulated in backlog queue */
  264. sock->state = SS_DISCONNECTING;
  265. release_sock(sk);
  266. sock_put(sk);
  267. sock->sk = NULL;
  268. return res;
  269. }
  270. /**
  271. * bind - associate or disassocate TIPC name(s) with a socket
  272. * @sock: socket structure
  273. * @uaddr: socket address describing name(s) and desired operation
  274. * @uaddr_len: size of socket address data structure
  275. *
  276. * Name and name sequence binding is indicated using a positive scope value;
  277. * a negative scope value unbinds the specified name. Specifying no name
  278. * (i.e. a socket address length of 0) unbinds all names from the socket.
  279. *
  280. * Returns 0 on success, errno otherwise
  281. *
  282. * NOTE: This routine doesn't need to take the socket lock since it doesn't
  283. * access any non-constant socket information.
  284. */
  285. static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
  286. {
  287. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  288. u32 portref = tipc_sk_port(sock->sk)->ref;
  289. if (unlikely(!uaddr_len))
  290. return tipc_withdraw(portref, 0, NULL);
  291. if (uaddr_len < sizeof(struct sockaddr_tipc))
  292. return -EINVAL;
  293. if (addr->family != AF_TIPC)
  294. return -EAFNOSUPPORT;
  295. if (addr->addrtype == TIPC_ADDR_NAME)
  296. addr->addr.nameseq.upper = addr->addr.nameseq.lower;
  297. else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
  298. return -EAFNOSUPPORT;
  299. if (addr->addr.nameseq.type < TIPC_RESERVED_TYPES)
  300. return -EACCES;
  301. return (addr->scope > 0) ?
  302. tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
  303. tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
  304. }
  305. /**
  306. * get_name - get port ID of socket or peer socket
  307. * @sock: socket structure
  308. * @uaddr: area for returned socket address
  309. * @uaddr_len: area for returned length of socket address
  310. * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
  311. *
  312. * Returns 0 on success, errno otherwise
  313. *
  314. * NOTE: This routine doesn't need to take the socket lock since it only
  315. * accesses socket information that is unchanging (or which changes in
  316. * a completely predictable manner).
  317. */
  318. static int get_name(struct socket *sock, struct sockaddr *uaddr,
  319. int *uaddr_len, int peer)
  320. {
  321. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
  322. struct tipc_sock *tsock = tipc_sk(sock->sk);
  323. memset(addr, 0, sizeof(*addr));
  324. if (peer) {
  325. if ((sock->state != SS_CONNECTED) &&
  326. ((peer != 2) || (sock->state != SS_DISCONNECTING)))
  327. return -ENOTCONN;
  328. addr->addr.id.ref = tsock->peer_name.ref;
  329. addr->addr.id.node = tsock->peer_name.node;
  330. } else {
  331. addr->addr.id.ref = tsock->p->ref;
  332. addr->addr.id.node = tipc_own_addr;
  333. }
  334. *uaddr_len = sizeof(*addr);
  335. addr->addrtype = TIPC_ADDR_ID;
  336. addr->family = AF_TIPC;
  337. addr->scope = 0;
  338. addr->addr.name.domain = 0;
  339. return 0;
  340. }
  341. /**
  342. * poll - read and possibly block on pollmask
  343. * @file: file structure associated with the socket
  344. * @sock: socket for which to calculate the poll bits
  345. * @wait: ???
  346. *
  347. * Returns pollmask value
  348. *
  349. * COMMENTARY:
  350. * It appears that the usual socket locking mechanisms are not useful here
  351. * since the pollmask info is potentially out-of-date the moment this routine
  352. * exits. TCP and other protocols seem to rely on higher level poll routines
  353. * to handle any preventable race conditions, so TIPC will do the same ...
  354. *
  355. * TIPC sets the returned events as follows:
  356. *
  357. * socket state flags set
  358. * ------------ ---------
  359. * unconnected no read flags
  360. * POLLOUT if port is not congested
  361. *
  362. * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
  363. * no write flags
  364. *
  365. * connected POLLIN/POLLRDNORM if data in rx queue
  366. * POLLOUT if port is not congested
  367. *
  368. * disconnecting POLLIN/POLLRDNORM/POLLHUP
  369. * no write flags
  370. *
  371. * listening POLLIN if SYN in rx queue
  372. * no write flags
  373. *
  374. * ready POLLIN/POLLRDNORM if data in rx queue
  375. * [connectionless] POLLOUT (since port cannot be congested)
  376. *
  377. * IMPORTANT: The fact that a read or write operation is indicated does NOT
  378. * imply that the operation will succeed, merely that it should be performed
  379. * and will not block.
  380. */
  381. static unsigned int poll(struct file *file, struct socket *sock,
  382. poll_table *wait)
  383. {
  384. struct sock *sk = sock->sk;
  385. u32 mask = 0;
  386. sock_poll_wait(file, sk_sleep(sk), wait);
  387. switch ((int)sock->state) {
  388. case SS_UNCONNECTED:
  389. if (!tipc_sk_port(sk)->congested)
  390. mask |= POLLOUT;
  391. break;
  392. case SS_READY:
  393. case SS_CONNECTED:
  394. if (!tipc_sk_port(sk)->congested)
  395. mask |= POLLOUT;
  396. /* fall thru' */
  397. case SS_CONNECTING:
  398. case SS_LISTENING:
  399. if (!skb_queue_empty(&sk->sk_receive_queue))
  400. mask |= (POLLIN | POLLRDNORM);
  401. break;
  402. case SS_DISCONNECTING:
  403. mask = (POLLIN | POLLRDNORM | POLLHUP);
  404. break;
  405. }
  406. return mask;
  407. }
  408. /**
  409. * dest_name_check - verify user is permitted to send to specified port name
  410. * @dest: destination address
  411. * @m: descriptor for message to be sent
  412. *
  413. * Prevents restricted configuration commands from being issued by
  414. * unauthorized users.
  415. *
  416. * Returns 0 if permission is granted, otherwise errno
  417. */
  418. static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
  419. {
  420. struct tipc_cfg_msg_hdr hdr;
  421. if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
  422. return 0;
  423. if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
  424. return 0;
  425. if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
  426. return -EACCES;
  427. if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
  428. return -EMSGSIZE;
  429. if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
  430. return -EFAULT;
  431. if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
  432. return -EACCES;
  433. return 0;
  434. }
  435. /**
  436. * send_msg - send message in connectionless manner
  437. * @iocb: if NULL, indicates that socket lock is already held
  438. * @sock: socket structure
  439. * @m: message to send
  440. * @total_len: length of message
  441. *
  442. * Message must have an destination specified explicitly.
  443. * Used for SOCK_RDM and SOCK_DGRAM messages,
  444. * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
  445. * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
  446. *
  447. * Returns the number of bytes sent on success, or errno otherwise
  448. */
  449. static int send_msg(struct kiocb *iocb, struct socket *sock,
  450. struct msghdr *m, size_t total_len)
  451. {
  452. struct sock *sk = sock->sk;
  453. struct tipc_port *tport = tipc_sk_port(sk);
  454. struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
  455. int needs_conn;
  456. long timeout_val;
  457. int res = -EINVAL;
  458. if (unlikely(!dest))
  459. return -EDESTADDRREQ;
  460. if (unlikely((m->msg_namelen < sizeof(*dest)) ||
  461. (dest->family != AF_TIPC)))
  462. return -EINVAL;
  463. if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
  464. (m->msg_iovlen > (unsigned int)INT_MAX))
  465. return -EMSGSIZE;
  466. if (iocb)
  467. lock_sock(sk);
  468. needs_conn = (sock->state != SS_READY);
  469. if (unlikely(needs_conn)) {
  470. if (sock->state == SS_LISTENING) {
  471. res = -EPIPE;
  472. goto exit;
  473. }
  474. if (sock->state != SS_UNCONNECTED) {
  475. res = -EISCONN;
  476. goto exit;
  477. }
  478. if ((tport->published) ||
  479. ((sock->type == SOCK_STREAM) && (total_len != 0))) {
  480. res = -EOPNOTSUPP;
  481. goto exit;
  482. }
  483. if (dest->addrtype == TIPC_ADDR_NAME) {
  484. tport->conn_type = dest->addr.name.name.type;
  485. tport->conn_instance = dest->addr.name.name.instance;
  486. }
  487. /* Abort any pending connection attempts (very unlikely) */
  488. reject_rx_queue(sk);
  489. }
  490. timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  491. do {
  492. if (dest->addrtype == TIPC_ADDR_NAME) {
  493. res = dest_name_check(dest, m);
  494. if (res)
  495. break;
  496. res = tipc_send2name(tport->ref,
  497. &dest->addr.name.name,
  498. dest->addr.name.domain,
  499. m->msg_iovlen,
  500. m->msg_iov,
  501. total_len);
  502. } else if (dest->addrtype == TIPC_ADDR_ID) {
  503. res = tipc_send2port(tport->ref,
  504. &dest->addr.id,
  505. m->msg_iovlen,
  506. m->msg_iov,
  507. total_len);
  508. } else if (dest->addrtype == TIPC_ADDR_MCAST) {
  509. if (needs_conn) {
  510. res = -EOPNOTSUPP;
  511. break;
  512. }
  513. res = dest_name_check(dest, m);
  514. if (res)
  515. break;
  516. res = tipc_multicast(tport->ref,
  517. &dest->addr.nameseq,
  518. m->msg_iovlen,
  519. m->msg_iov,
  520. total_len);
  521. }
  522. if (likely(res != -ELINKCONG)) {
  523. if (needs_conn && (res >= 0))
  524. sock->state = SS_CONNECTING;
  525. break;
  526. }
  527. if (timeout_val <= 0L) {
  528. res = timeout_val ? timeout_val : -EWOULDBLOCK;
  529. break;
  530. }
  531. release_sock(sk);
  532. timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
  533. !tport->congested, timeout_val);
  534. lock_sock(sk);
  535. } while (1);
  536. exit:
  537. if (iocb)
  538. release_sock(sk);
  539. return res;
  540. }
  541. /**
  542. * send_packet - send a connection-oriented message
  543. * @iocb: if NULL, indicates that socket lock is already held
  544. * @sock: socket structure
  545. * @m: message to send
  546. * @total_len: length of message
  547. *
  548. * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
  549. *
  550. * Returns the number of bytes sent on success, or errno otherwise
  551. */
  552. static int send_packet(struct kiocb *iocb, struct socket *sock,
  553. struct msghdr *m, size_t total_len)
  554. {
  555. struct sock *sk = sock->sk;
  556. struct tipc_port *tport = tipc_sk_port(sk);
  557. struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
  558. long timeout_val;
  559. int res;
  560. /* Handle implied connection establishment */
  561. if (unlikely(dest))
  562. return send_msg(iocb, sock, m, total_len);
  563. if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
  564. (m->msg_iovlen > (unsigned int)INT_MAX))
  565. return -EMSGSIZE;
  566. if (iocb)
  567. lock_sock(sk);
  568. timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
  569. do {
  570. if (unlikely(sock->state != SS_CONNECTED)) {
  571. if (sock->state == SS_DISCONNECTING)
  572. res = -EPIPE;
  573. else
  574. res = -ENOTCONN;
  575. break;
  576. }
  577. res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
  578. total_len);
  579. if (likely(res != -ELINKCONG))
  580. break;
  581. if (timeout_val <= 0L) {
  582. res = timeout_val ? timeout_val : -EWOULDBLOCK;
  583. break;
  584. }
  585. release_sock(sk);
  586. timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
  587. (!tport->congested || !tport->connected), timeout_val);
  588. lock_sock(sk);
  589. } while (1);
  590. if (iocb)
  591. release_sock(sk);
  592. return res;
  593. }
  594. /**
  595. * send_stream - send stream-oriented data
  596. * @iocb: (unused)
  597. * @sock: socket structure
  598. * @m: data to send
  599. * @total_len: total length of data to be sent
  600. *
  601. * Used for SOCK_STREAM data.
  602. *
  603. * Returns the number of bytes sent on success (or partial success),
  604. * or errno if no data sent
  605. */
  606. static int send_stream(struct kiocb *iocb, struct socket *sock,
  607. struct msghdr *m, size_t total_len)
  608. {
  609. struct sock *sk = sock->sk;
  610. struct tipc_port *tport = tipc_sk_port(sk);
  611. struct msghdr my_msg;
  612. struct iovec my_iov;
  613. struct iovec *curr_iov;
  614. int curr_iovlen;
  615. char __user *curr_start;
  616. u32 hdr_size;
  617. int curr_left;
  618. int bytes_to_send;
  619. int bytes_sent;
  620. int res;
  621. lock_sock(sk);
  622. /* Handle special cases where there is no connection */
  623. if (unlikely(sock->state != SS_CONNECTED)) {
  624. if (sock->state == SS_UNCONNECTED) {
  625. res = send_packet(NULL, sock, m, total_len);
  626. goto exit;
  627. } else if (sock->state == SS_DISCONNECTING) {
  628. res = -EPIPE;
  629. goto exit;
  630. } else {
  631. res = -ENOTCONN;
  632. goto exit;
  633. }
  634. }
  635. if (unlikely(m->msg_name)) {
  636. res = -EISCONN;
  637. goto exit;
  638. }
  639. if ((total_len > (unsigned int)INT_MAX) ||
  640. (m->msg_iovlen > (unsigned int)INT_MAX)) {
  641. res = -EMSGSIZE;
  642. goto exit;
  643. }
  644. /*
  645. * Send each iovec entry using one or more messages
  646. *
  647. * Note: This algorithm is good for the most likely case
  648. * (i.e. one large iovec entry), but could be improved to pass sets
  649. * of small iovec entries into send_packet().
  650. */
  651. curr_iov = m->msg_iov;
  652. curr_iovlen = m->msg_iovlen;
  653. my_msg.msg_iov = &my_iov;
  654. my_msg.msg_iovlen = 1;
  655. my_msg.msg_flags = m->msg_flags;
  656. my_msg.msg_name = NULL;
  657. bytes_sent = 0;
  658. hdr_size = msg_hdr_sz(&tport->phdr);
  659. while (curr_iovlen--) {
  660. curr_start = curr_iov->iov_base;
  661. curr_left = curr_iov->iov_len;
  662. while (curr_left) {
  663. bytes_to_send = tport->max_pkt - hdr_size;
  664. if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
  665. bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
  666. if (curr_left < bytes_to_send)
  667. bytes_to_send = curr_left;
  668. my_iov.iov_base = curr_start;
  669. my_iov.iov_len = bytes_to_send;
  670. res = send_packet(NULL, sock, &my_msg, bytes_to_send);
  671. if (res < 0) {
  672. if (bytes_sent)
  673. res = bytes_sent;
  674. goto exit;
  675. }
  676. curr_left -= bytes_to_send;
  677. curr_start += bytes_to_send;
  678. bytes_sent += bytes_to_send;
  679. }
  680. curr_iov++;
  681. }
  682. res = bytes_sent;
  683. exit:
  684. release_sock(sk);
  685. return res;
  686. }
  687. /**
  688. * auto_connect - complete connection setup to a remote port
  689. * @sock: socket structure
  690. * @msg: peer's response message
  691. *
  692. * Returns 0 on success, errno otherwise
  693. */
  694. static int auto_connect(struct socket *sock, struct tipc_msg *msg)
  695. {
  696. struct tipc_sock *tsock = tipc_sk(sock->sk);
  697. struct tipc_port *p_ptr;
  698. tsock->peer_name.ref = msg_origport(msg);
  699. tsock->peer_name.node = msg_orignode(msg);
  700. p_ptr = tipc_port_deref(tsock->p->ref);
  701. if (!p_ptr)
  702. return -EINVAL;
  703. __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
  704. if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
  705. return -EINVAL;
  706. msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
  707. sock->state = SS_CONNECTED;
  708. return 0;
  709. }
  710. /**
  711. * set_orig_addr - capture sender's address for received message
  712. * @m: descriptor for message info
  713. * @msg: received message header
  714. *
  715. * Note: Address is not captured if not requested by receiver.
  716. */
  717. static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
  718. {
  719. struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
  720. if (addr) {
  721. addr->family = AF_TIPC;
  722. addr->addrtype = TIPC_ADDR_ID;
  723. addr->addr.id.ref = msg_origport(msg);
  724. addr->addr.id.node = msg_orignode(msg);
  725. addr->addr.name.domain = 0; /* could leave uninitialized */
  726. addr->scope = 0; /* could leave uninitialized */
  727. m->msg_namelen = sizeof(struct sockaddr_tipc);
  728. }
  729. }
  730. /**
  731. * anc_data_recv - optionally capture ancillary data for received message
  732. * @m: descriptor for message info
  733. * @msg: received message header
  734. * @tport: TIPC port associated with message
  735. *
  736. * Note: Ancillary data is not captured if not requested by receiver.
  737. *
  738. * Returns 0 if successful, otherwise errno
  739. */
  740. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  741. struct tipc_port *tport)
  742. {
  743. u32 anc_data[3];
  744. u32 err;
  745. u32 dest_type;
  746. int has_name;
  747. int res;
  748. if (likely(m->msg_controllen == 0))
  749. return 0;
  750. /* Optionally capture errored message object(s) */
  751. err = msg ? msg_errcode(msg) : 0;
  752. if (unlikely(err)) {
  753. anc_data[0] = err;
  754. anc_data[1] = msg_data_sz(msg);
  755. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  756. if (res)
  757. return res;
  758. if (anc_data[1]) {
  759. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  760. msg_data(msg));
  761. if (res)
  762. return res;
  763. }
  764. }
  765. /* Optionally capture message destination object */
  766. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  767. switch (dest_type) {
  768. case TIPC_NAMED_MSG:
  769. has_name = 1;
  770. anc_data[0] = msg_nametype(msg);
  771. anc_data[1] = msg_namelower(msg);
  772. anc_data[2] = msg_namelower(msg);
  773. break;
  774. case TIPC_MCAST_MSG:
  775. has_name = 1;
  776. anc_data[0] = msg_nametype(msg);
  777. anc_data[1] = msg_namelower(msg);
  778. anc_data[2] = msg_nameupper(msg);
  779. break;
  780. case TIPC_CONN_MSG:
  781. has_name = (tport->conn_type != 0);
  782. anc_data[0] = tport->conn_type;
  783. anc_data[1] = tport->conn_instance;
  784. anc_data[2] = tport->conn_instance;
  785. break;
  786. default:
  787. has_name = 0;
  788. }
  789. if (has_name) {
  790. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  791. if (res)
  792. return res;
  793. }
  794. return 0;
  795. }
  796. /**
  797. * recv_msg - receive packet-oriented message
  798. * @iocb: (unused)
  799. * @m: descriptor for message info
  800. * @buf_len: total size of user buffer area
  801. * @flags: receive flags
  802. *
  803. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  804. * If the complete message doesn't fit in user area, truncate it.
  805. *
  806. * Returns size of returned message data, errno otherwise
  807. */
  808. static int recv_msg(struct kiocb *iocb, struct socket *sock,
  809. struct msghdr *m, size_t buf_len, int flags)
  810. {
  811. struct sock *sk = sock->sk;
  812. struct tipc_port *tport = tipc_sk_port(sk);
  813. struct sk_buff *buf;
  814. struct tipc_msg *msg;
  815. long timeout;
  816. unsigned int sz;
  817. u32 err;
  818. int res;
  819. /* Catch invalid receive requests */
  820. if (unlikely(!buf_len))
  821. return -EINVAL;
  822. lock_sock(sk);
  823. if (unlikely(sock->state == SS_UNCONNECTED)) {
  824. res = -ENOTCONN;
  825. goto exit;
  826. }
  827. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  828. restart:
  829. /* Look for a message in receive queue; wait if necessary */
  830. while (skb_queue_empty(&sk->sk_receive_queue)) {
  831. if (sock->state == SS_DISCONNECTING) {
  832. res = -ENOTCONN;
  833. goto exit;
  834. }
  835. if (timeout <= 0L) {
  836. res = timeout ? timeout : -EWOULDBLOCK;
  837. goto exit;
  838. }
  839. release_sock(sk);
  840. timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
  841. tipc_rx_ready(sock),
  842. timeout);
  843. lock_sock(sk);
  844. }
  845. /* Look at first message in receive queue */
  846. buf = skb_peek(&sk->sk_receive_queue);
  847. msg = buf_msg(buf);
  848. sz = msg_data_sz(msg);
  849. err = msg_errcode(msg);
  850. /* Discard an empty non-errored message & try again */
  851. if ((!sz) && (!err)) {
  852. advance_rx_queue(sk);
  853. goto restart;
  854. }
  855. /* Capture sender's address (optional) */
  856. set_orig_addr(m, msg);
  857. /* Capture ancillary data (optional) */
  858. res = anc_data_recv(m, msg, tport);
  859. if (res)
  860. goto exit;
  861. /* Capture message data (if valid) & compute return value (always) */
  862. if (!err) {
  863. if (unlikely(buf_len < sz)) {
  864. sz = buf_len;
  865. m->msg_flags |= MSG_TRUNC;
  866. }
  867. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
  868. m->msg_iov, sz);
  869. if (res)
  870. goto exit;
  871. res = sz;
  872. } else {
  873. if ((sock->state == SS_READY) ||
  874. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  875. res = 0;
  876. else
  877. res = -ECONNRESET;
  878. }
  879. /* Consume received message (optional) */
  880. if (likely(!(flags & MSG_PEEK))) {
  881. if ((sock->state != SS_READY) &&
  882. (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  883. tipc_acknowledge(tport->ref, tport->conn_unacked);
  884. advance_rx_queue(sk);
  885. }
  886. exit:
  887. release_sock(sk);
  888. return res;
  889. }
  890. /**
  891. * recv_stream - receive stream-oriented data
  892. * @iocb: (unused)
  893. * @m: descriptor for message info
  894. * @buf_len: total size of user buffer area
  895. * @flags: receive flags
  896. *
  897. * Used for SOCK_STREAM messages only. If not enough data is available
  898. * will optionally wait for more; never truncates data.
  899. *
  900. * Returns size of returned message data, errno otherwise
  901. */
  902. static int recv_stream(struct kiocb *iocb, struct socket *sock,
  903. struct msghdr *m, size_t buf_len, int flags)
  904. {
  905. struct sock *sk = sock->sk;
  906. struct tipc_port *tport = tipc_sk_port(sk);
  907. struct sk_buff *buf;
  908. struct tipc_msg *msg;
  909. long timeout;
  910. unsigned int sz;
  911. int sz_to_copy, target, needed;
  912. int sz_copied = 0;
  913. u32 err;
  914. int res = 0;
  915. /* Catch invalid receive attempts */
  916. if (unlikely(!buf_len))
  917. return -EINVAL;
  918. lock_sock(sk);
  919. if (unlikely((sock->state == SS_UNCONNECTED) ||
  920. (sock->state == SS_CONNECTING))) {
  921. res = -ENOTCONN;
  922. goto exit;
  923. }
  924. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  925. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  926. restart:
  927. /* Look for a message in receive queue; wait if necessary */
  928. while (skb_queue_empty(&sk->sk_receive_queue)) {
  929. if (sock->state == SS_DISCONNECTING) {
  930. res = -ENOTCONN;
  931. goto exit;
  932. }
  933. if (timeout <= 0L) {
  934. res = timeout ? timeout : -EWOULDBLOCK;
  935. goto exit;
  936. }
  937. release_sock(sk);
  938. timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
  939. tipc_rx_ready(sock),
  940. timeout);
  941. lock_sock(sk);
  942. }
  943. /* Look at first message in receive queue */
  944. buf = skb_peek(&sk->sk_receive_queue);
  945. msg = buf_msg(buf);
  946. sz = msg_data_sz(msg);
  947. err = msg_errcode(msg);
  948. /* Discard an empty non-errored message & try again */
  949. if ((!sz) && (!err)) {
  950. advance_rx_queue(sk);
  951. goto restart;
  952. }
  953. /* Optionally capture sender's address & ancillary data of first msg */
  954. if (sz_copied == 0) {
  955. set_orig_addr(m, msg);
  956. res = anc_data_recv(m, msg, tport);
  957. if (res)
  958. goto exit;
  959. }
  960. /* Capture message data (if valid) & compute return value (always) */
  961. if (!err) {
  962. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  963. sz -= offset;
  964. needed = (buf_len - sz_copied);
  965. sz_to_copy = (sz <= needed) ? sz : needed;
  966. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
  967. m->msg_iov, sz_to_copy);
  968. if (res)
  969. goto exit;
  970. sz_copied += sz_to_copy;
  971. if (sz_to_copy < sz) {
  972. if (!(flags & MSG_PEEK))
  973. TIPC_SKB_CB(buf)->handle =
  974. (void *)(unsigned long)(offset + sz_to_copy);
  975. goto exit;
  976. }
  977. } else {
  978. if (sz_copied != 0)
  979. goto exit; /* can't add error msg to valid data */
  980. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  981. res = 0;
  982. else
  983. res = -ECONNRESET;
  984. }
  985. /* Consume received message (optional) */
  986. if (likely(!(flags & MSG_PEEK))) {
  987. if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  988. tipc_acknowledge(tport->ref, tport->conn_unacked);
  989. advance_rx_queue(sk);
  990. }
  991. /* Loop around if more data is required */
  992. if ((sz_copied < buf_len) && /* didn't get all requested data */
  993. (!skb_queue_empty(&sk->sk_receive_queue) ||
  994. (sz_copied < target)) && /* and more is ready or required */
  995. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  996. (!err)) /* and haven't reached a FIN */
  997. goto restart;
  998. exit:
  999. release_sock(sk);
  1000. return sz_copied ? sz_copied : res;
  1001. }
  1002. /**
  1003. * tipc_write_space - wake up thread if port congestion is released
  1004. * @sk: socket
  1005. */
  1006. static void tipc_write_space(struct sock *sk)
  1007. {
  1008. struct socket_wq *wq;
  1009. rcu_read_lock();
  1010. wq = rcu_dereference(sk->sk_wq);
  1011. if (wq_has_sleeper(wq))
  1012. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1013. POLLWRNORM | POLLWRBAND);
  1014. rcu_read_unlock();
  1015. }
  1016. /**
  1017. * tipc_data_ready - wake up threads to indicate messages have been received
  1018. * @sk: socket
  1019. * @len: the length of messages
  1020. */
  1021. static void tipc_data_ready(struct sock *sk, int len)
  1022. {
  1023. struct socket_wq *wq;
  1024. rcu_read_lock();
  1025. wq = rcu_dereference(sk->sk_wq);
  1026. if (wq_has_sleeper(wq))
  1027. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1028. POLLRDNORM | POLLRDBAND);
  1029. rcu_read_unlock();
  1030. }
  1031. /**
  1032. * rx_queue_full - determine if receive queue can accept another message
  1033. * @msg: message to be added to queue
  1034. * @queue_size: current size of queue
  1035. * @base: nominal maximum size of queue
  1036. *
  1037. * Returns 1 if queue is unable to accept message, 0 otherwise
  1038. */
  1039. static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
  1040. {
  1041. u32 threshold;
  1042. u32 imp = msg_importance(msg);
  1043. if (imp == TIPC_LOW_IMPORTANCE)
  1044. threshold = base;
  1045. else if (imp == TIPC_MEDIUM_IMPORTANCE)
  1046. threshold = base * 2;
  1047. else if (imp == TIPC_HIGH_IMPORTANCE)
  1048. threshold = base * 100;
  1049. else
  1050. return 0;
  1051. if (msg_connected(msg))
  1052. threshold *= 4;
  1053. return queue_size >= threshold;
  1054. }
  1055. /**
  1056. * filter_connect - Handle all incoming messages for a connection-based socket
  1057. * @tsock: TIPC socket
  1058. * @msg: message
  1059. *
  1060. * Returns TIPC error status code and socket error status code
  1061. * once it encounters some errors
  1062. */
  1063. static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
  1064. {
  1065. struct socket *sock = tsock->sk.sk_socket;
  1066. struct tipc_msg *msg = buf_msg(*buf);
  1067. struct sock *sk = &tsock->sk;
  1068. u32 retval = TIPC_ERR_NO_PORT;
  1069. int res;
  1070. if (msg_mcast(msg))
  1071. return retval;
  1072. switch ((int)sock->state) {
  1073. case SS_CONNECTED:
  1074. /* Accept only connection-based messages sent by peer */
  1075. if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
  1076. if (unlikely(msg_errcode(msg))) {
  1077. sock->state = SS_DISCONNECTING;
  1078. __tipc_disconnect(tsock->p);
  1079. }
  1080. retval = TIPC_OK;
  1081. }
  1082. break;
  1083. case SS_CONNECTING:
  1084. /* Accept only ACK or NACK message */
  1085. if (unlikely(msg_errcode(msg))) {
  1086. sock->state = SS_DISCONNECTING;
  1087. sk->sk_err = -ECONNREFUSED;
  1088. retval = TIPC_OK;
  1089. break;
  1090. }
  1091. if (unlikely(!msg_connected(msg)))
  1092. break;
  1093. res = auto_connect(sock, msg);
  1094. if (res) {
  1095. sock->state = SS_DISCONNECTING;
  1096. sk->sk_err = res;
  1097. retval = TIPC_OK;
  1098. break;
  1099. }
  1100. /* If an incoming message is an 'ACK-', it should be
  1101. * discarded here because it doesn't contain useful
  1102. * data. In addition, we should try to wake up
  1103. * connect() routine if sleeping.
  1104. */
  1105. if (msg_data_sz(msg) == 0) {
  1106. kfree_skb(*buf);
  1107. *buf = NULL;
  1108. if (waitqueue_active(sk_sleep(sk)))
  1109. wake_up_interruptible(sk_sleep(sk));
  1110. }
  1111. retval = TIPC_OK;
  1112. break;
  1113. case SS_LISTENING:
  1114. case SS_UNCONNECTED:
  1115. /* Accept only SYN message */
  1116. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1117. retval = TIPC_OK;
  1118. break;
  1119. case SS_DISCONNECTING:
  1120. break;
  1121. default:
  1122. pr_err("Unknown socket state %u\n", sock->state);
  1123. }
  1124. return retval;
  1125. }
  1126. /**
  1127. * filter_rcv - validate incoming message
  1128. * @sk: socket
  1129. * @buf: message
  1130. *
  1131. * Enqueues message on receive queue if acceptable; optionally handles
  1132. * disconnect indication for a connected socket.
  1133. *
  1134. * Called with socket lock already taken; port lock may also be taken.
  1135. *
  1136. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1137. */
  1138. static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
  1139. {
  1140. struct socket *sock = sk->sk_socket;
  1141. struct tipc_msg *msg = buf_msg(buf);
  1142. u32 recv_q_len;
  1143. u32 res = TIPC_OK;
  1144. /* Reject message if it is wrong sort of message for socket */
  1145. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1146. return TIPC_ERR_NO_PORT;
  1147. if (sock->state == SS_READY) {
  1148. if (msg_connected(msg))
  1149. return TIPC_ERR_NO_PORT;
  1150. } else {
  1151. res = filter_connect(tipc_sk(sk), &buf);
  1152. if (res != TIPC_OK || buf == NULL)
  1153. return res;
  1154. }
  1155. /* Reject message if there isn't room to queue it */
  1156. recv_q_len = skb_queue_len(&sk->sk_receive_queue);
  1157. if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
  1158. if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
  1159. return TIPC_ERR_OVERLOAD;
  1160. }
  1161. /* Enqueue message (finally!) */
  1162. TIPC_SKB_CB(buf)->handle = 0;
  1163. __skb_queue_tail(&sk->sk_receive_queue, buf);
  1164. sk->sk_data_ready(sk, 0);
  1165. return TIPC_OK;
  1166. }
  1167. /**
  1168. * backlog_rcv - handle incoming message from backlog queue
  1169. * @sk: socket
  1170. * @buf: message
  1171. *
  1172. * Caller must hold socket lock, but not port lock.
  1173. *
  1174. * Returns 0
  1175. */
  1176. static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
  1177. {
  1178. u32 res;
  1179. res = filter_rcv(sk, buf);
  1180. if (res)
  1181. tipc_reject_msg(buf, res);
  1182. return 0;
  1183. }
  1184. /**
  1185. * dispatch - handle incoming message
  1186. * @tport: TIPC port that received message
  1187. * @buf: message
  1188. *
  1189. * Called with port lock already taken.
  1190. *
  1191. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1192. */
  1193. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
  1194. {
  1195. struct sock *sk = (struct sock *)tport->usr_handle;
  1196. u32 res;
  1197. /*
  1198. * Process message if socket is unlocked; otherwise add to backlog queue
  1199. *
  1200. * This code is based on sk_receive_skb(), but must be distinct from it
  1201. * since a TIPC-specific filter/reject mechanism is utilized
  1202. */
  1203. bh_lock_sock(sk);
  1204. if (!sock_owned_by_user(sk)) {
  1205. res = filter_rcv(sk, buf);
  1206. } else {
  1207. if (sk_add_backlog(sk, buf, sk->sk_rcvbuf))
  1208. res = TIPC_ERR_OVERLOAD;
  1209. else
  1210. res = TIPC_OK;
  1211. }
  1212. bh_unlock_sock(sk);
  1213. return res;
  1214. }
  1215. /**
  1216. * wakeupdispatch - wake up port after congestion
  1217. * @tport: port to wakeup
  1218. *
  1219. * Called with port lock already taken.
  1220. */
  1221. static void wakeupdispatch(struct tipc_port *tport)
  1222. {
  1223. struct sock *sk = (struct sock *)tport->usr_handle;
  1224. sk->sk_write_space(sk);
  1225. }
  1226. /**
  1227. * connect - establish a connection to another TIPC port
  1228. * @sock: socket structure
  1229. * @dest: socket address for destination port
  1230. * @destlen: size of socket address data structure
  1231. * @flags: file-related flags associated with socket
  1232. *
  1233. * Returns 0 on success, errno otherwise
  1234. */
  1235. static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
  1236. int flags)
  1237. {
  1238. struct sock *sk = sock->sk;
  1239. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1240. struct msghdr m = {NULL,};
  1241. unsigned int timeout;
  1242. int res;
  1243. lock_sock(sk);
  1244. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1245. if (sock->state == SS_READY) {
  1246. res = -EOPNOTSUPP;
  1247. goto exit;
  1248. }
  1249. /*
  1250. * Reject connection attempt using multicast address
  1251. *
  1252. * Note: send_msg() validates the rest of the address fields,
  1253. * so there's no need to do it here
  1254. */
  1255. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1256. res = -EINVAL;
  1257. goto exit;
  1258. }
  1259. timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
  1260. switch (sock->state) {
  1261. case SS_UNCONNECTED:
  1262. /* Send a 'SYN-' to destination */
  1263. m.msg_name = dest;
  1264. m.msg_namelen = destlen;
  1265. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1266. * indicate send_msg() is never blocked.
  1267. */
  1268. if (!timeout)
  1269. m.msg_flags = MSG_DONTWAIT;
  1270. res = send_msg(NULL, sock, &m, 0);
  1271. if ((res < 0) && (res != -EWOULDBLOCK))
  1272. goto exit;
  1273. /* Just entered SS_CONNECTING state; the only
  1274. * difference is that return value in non-blocking
  1275. * case is EINPROGRESS, rather than EALREADY.
  1276. */
  1277. res = -EINPROGRESS;
  1278. break;
  1279. case SS_CONNECTING:
  1280. res = -EALREADY;
  1281. break;
  1282. case SS_CONNECTED:
  1283. res = -EISCONN;
  1284. break;
  1285. default:
  1286. res = -EINVAL;
  1287. goto exit;
  1288. }
  1289. if (sock->state == SS_CONNECTING) {
  1290. if (!timeout)
  1291. goto exit;
  1292. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1293. release_sock(sk);
  1294. res = wait_event_interruptible_timeout(*sk_sleep(sk),
  1295. sock->state != SS_CONNECTING,
  1296. timeout ? (long)msecs_to_jiffies(timeout)
  1297. : MAX_SCHEDULE_TIMEOUT);
  1298. lock_sock(sk);
  1299. if (res <= 0) {
  1300. if (res == 0)
  1301. res = -ETIMEDOUT;
  1302. else
  1303. ; /* leave "res" unchanged */
  1304. goto exit;
  1305. }
  1306. }
  1307. if (unlikely(sock->state == SS_DISCONNECTING))
  1308. res = sock_error(sk);
  1309. else
  1310. res = 0;
  1311. exit:
  1312. release_sock(sk);
  1313. return res;
  1314. }
  1315. /**
  1316. * listen - allow socket to listen for incoming connections
  1317. * @sock: socket structure
  1318. * @len: (unused)
  1319. *
  1320. * Returns 0 on success, errno otherwise
  1321. */
  1322. static int listen(struct socket *sock, int len)
  1323. {
  1324. struct sock *sk = sock->sk;
  1325. int res;
  1326. lock_sock(sk);
  1327. if (sock->state != SS_UNCONNECTED)
  1328. res = -EINVAL;
  1329. else {
  1330. sock->state = SS_LISTENING;
  1331. res = 0;
  1332. }
  1333. release_sock(sk);
  1334. return res;
  1335. }
  1336. /**
  1337. * accept - wait for connection request
  1338. * @sock: listening socket
  1339. * @newsock: new socket that is to be connected
  1340. * @flags: file-related flags associated with socket
  1341. *
  1342. * Returns 0 on success, errno otherwise
  1343. */
  1344. static int accept(struct socket *sock, struct socket *new_sock, int flags)
  1345. {
  1346. struct sock *sk = sock->sk;
  1347. struct sk_buff *buf;
  1348. int res;
  1349. lock_sock(sk);
  1350. if (sock->state != SS_LISTENING) {
  1351. res = -EINVAL;
  1352. goto exit;
  1353. }
  1354. while (skb_queue_empty(&sk->sk_receive_queue)) {
  1355. if (flags & O_NONBLOCK) {
  1356. res = -EWOULDBLOCK;
  1357. goto exit;
  1358. }
  1359. release_sock(sk);
  1360. res = wait_event_interruptible(*sk_sleep(sk),
  1361. (!skb_queue_empty(&sk->sk_receive_queue)));
  1362. lock_sock(sk);
  1363. if (res)
  1364. goto exit;
  1365. }
  1366. buf = skb_peek(&sk->sk_receive_queue);
  1367. res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
  1368. if (!res) {
  1369. struct sock *new_sk = new_sock->sk;
  1370. struct tipc_sock *new_tsock = tipc_sk(new_sk);
  1371. struct tipc_port *new_tport = new_tsock->p;
  1372. u32 new_ref = new_tport->ref;
  1373. struct tipc_msg *msg = buf_msg(buf);
  1374. lock_sock(new_sk);
  1375. /*
  1376. * Reject any stray messages received by new socket
  1377. * before the socket lock was taken (very, very unlikely)
  1378. */
  1379. reject_rx_queue(new_sk);
  1380. /* Connect new socket to it's peer */
  1381. new_tsock->peer_name.ref = msg_origport(msg);
  1382. new_tsock->peer_name.node = msg_orignode(msg);
  1383. tipc_connect(new_ref, &new_tsock->peer_name);
  1384. new_sock->state = SS_CONNECTED;
  1385. tipc_set_portimportance(new_ref, msg_importance(msg));
  1386. if (msg_named(msg)) {
  1387. new_tport->conn_type = msg_nametype(msg);
  1388. new_tport->conn_instance = msg_nameinst(msg);
  1389. }
  1390. /*
  1391. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1392. * Respond to 'SYN+' by queuing it on new socket.
  1393. */
  1394. if (!msg_data_sz(msg)) {
  1395. struct msghdr m = {NULL,};
  1396. advance_rx_queue(sk);
  1397. send_packet(NULL, new_sock, &m, 0);
  1398. } else {
  1399. __skb_dequeue(&sk->sk_receive_queue);
  1400. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1401. }
  1402. release_sock(new_sk);
  1403. }
  1404. exit:
  1405. release_sock(sk);
  1406. return res;
  1407. }
  1408. /**
  1409. * shutdown - shutdown socket connection
  1410. * @sock: socket structure
  1411. * @how: direction to close (must be SHUT_RDWR)
  1412. *
  1413. * Terminates connection (if necessary), then purges socket's receive queue.
  1414. *
  1415. * Returns 0 on success, errno otherwise
  1416. */
  1417. static int shutdown(struct socket *sock, int how)
  1418. {
  1419. struct sock *sk = sock->sk;
  1420. struct tipc_port *tport = tipc_sk_port(sk);
  1421. struct sk_buff *buf;
  1422. int res;
  1423. if (how != SHUT_RDWR)
  1424. return -EINVAL;
  1425. lock_sock(sk);
  1426. switch (sock->state) {
  1427. case SS_CONNECTING:
  1428. case SS_CONNECTED:
  1429. restart:
  1430. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1431. buf = __skb_dequeue(&sk->sk_receive_queue);
  1432. if (buf) {
  1433. if (TIPC_SKB_CB(buf)->handle != 0) {
  1434. kfree_skb(buf);
  1435. goto restart;
  1436. }
  1437. tipc_disconnect(tport->ref);
  1438. tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
  1439. } else {
  1440. tipc_shutdown(tport->ref);
  1441. }
  1442. sock->state = SS_DISCONNECTING;
  1443. /* fall through */
  1444. case SS_DISCONNECTING:
  1445. /* Discard any unreceived messages */
  1446. discard_rx_queue(sk);
  1447. /* Wake up anyone sleeping in poll */
  1448. sk->sk_state_change(sk);
  1449. res = 0;
  1450. break;
  1451. default:
  1452. res = -ENOTCONN;
  1453. }
  1454. release_sock(sk);
  1455. return res;
  1456. }
  1457. /**
  1458. * setsockopt - set socket option
  1459. * @sock: socket structure
  1460. * @lvl: option level
  1461. * @opt: option identifier
  1462. * @ov: pointer to new option value
  1463. * @ol: length of option value
  1464. *
  1465. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1466. * (to ease compatibility).
  1467. *
  1468. * Returns 0 on success, errno otherwise
  1469. */
  1470. static int setsockopt(struct socket *sock,
  1471. int lvl, int opt, char __user *ov, unsigned int ol)
  1472. {
  1473. struct sock *sk = sock->sk;
  1474. struct tipc_port *tport = tipc_sk_port(sk);
  1475. u32 value;
  1476. int res;
  1477. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1478. return 0;
  1479. if (lvl != SOL_TIPC)
  1480. return -ENOPROTOOPT;
  1481. if (ol < sizeof(value))
  1482. return -EINVAL;
  1483. res = get_user(value, (u32 __user *)ov);
  1484. if (res)
  1485. return res;
  1486. lock_sock(sk);
  1487. switch (opt) {
  1488. case TIPC_IMPORTANCE:
  1489. res = tipc_set_portimportance(tport->ref, value);
  1490. break;
  1491. case TIPC_SRC_DROPPABLE:
  1492. if (sock->type != SOCK_STREAM)
  1493. res = tipc_set_portunreliable(tport->ref, value);
  1494. else
  1495. res = -ENOPROTOOPT;
  1496. break;
  1497. case TIPC_DEST_DROPPABLE:
  1498. res = tipc_set_portunreturnable(tport->ref, value);
  1499. break;
  1500. case TIPC_CONN_TIMEOUT:
  1501. tipc_sk(sk)->conn_timeout = value;
  1502. /* no need to set "res", since already 0 at this point */
  1503. break;
  1504. default:
  1505. res = -EINVAL;
  1506. }
  1507. release_sock(sk);
  1508. return res;
  1509. }
  1510. /**
  1511. * getsockopt - get socket option
  1512. * @sock: socket structure
  1513. * @lvl: option level
  1514. * @opt: option identifier
  1515. * @ov: receptacle for option value
  1516. * @ol: receptacle for length of option value
  1517. *
  1518. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1519. * (to ease compatibility).
  1520. *
  1521. * Returns 0 on success, errno otherwise
  1522. */
  1523. static int getsockopt(struct socket *sock,
  1524. int lvl, int opt, char __user *ov, int __user *ol)
  1525. {
  1526. struct sock *sk = sock->sk;
  1527. struct tipc_port *tport = tipc_sk_port(sk);
  1528. int len;
  1529. u32 value;
  1530. int res;
  1531. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1532. return put_user(0, ol);
  1533. if (lvl != SOL_TIPC)
  1534. return -ENOPROTOOPT;
  1535. res = get_user(len, ol);
  1536. if (res)
  1537. return res;
  1538. lock_sock(sk);
  1539. switch (opt) {
  1540. case TIPC_IMPORTANCE:
  1541. res = tipc_portimportance(tport->ref, &value);
  1542. break;
  1543. case TIPC_SRC_DROPPABLE:
  1544. res = tipc_portunreliable(tport->ref, &value);
  1545. break;
  1546. case TIPC_DEST_DROPPABLE:
  1547. res = tipc_portunreturnable(tport->ref, &value);
  1548. break;
  1549. case TIPC_CONN_TIMEOUT:
  1550. value = tipc_sk(sk)->conn_timeout;
  1551. /* no need to set "res", since already 0 at this point */
  1552. break;
  1553. case TIPC_NODE_RECVQ_DEPTH:
  1554. value = 0; /* was tipc_queue_size, now obsolete */
  1555. break;
  1556. case TIPC_SOCK_RECVQ_DEPTH:
  1557. value = skb_queue_len(&sk->sk_receive_queue);
  1558. break;
  1559. default:
  1560. res = -EINVAL;
  1561. }
  1562. release_sock(sk);
  1563. if (res)
  1564. return res; /* "get" failed */
  1565. if (len < sizeof(value))
  1566. return -EINVAL;
  1567. if (copy_to_user(ov, &value, sizeof(value)))
  1568. return -EFAULT;
  1569. return put_user(sizeof(value), ol);
  1570. }
  1571. /* Protocol switches for the various types of TIPC sockets */
  1572. static const struct proto_ops msg_ops = {
  1573. .owner = THIS_MODULE,
  1574. .family = AF_TIPC,
  1575. .release = release,
  1576. .bind = bind,
  1577. .connect = connect,
  1578. .socketpair = sock_no_socketpair,
  1579. .accept = sock_no_accept,
  1580. .getname = get_name,
  1581. .poll = poll,
  1582. .ioctl = sock_no_ioctl,
  1583. .listen = sock_no_listen,
  1584. .shutdown = shutdown,
  1585. .setsockopt = setsockopt,
  1586. .getsockopt = getsockopt,
  1587. .sendmsg = send_msg,
  1588. .recvmsg = recv_msg,
  1589. .mmap = sock_no_mmap,
  1590. .sendpage = sock_no_sendpage
  1591. };
  1592. static const struct proto_ops packet_ops = {
  1593. .owner = THIS_MODULE,
  1594. .family = AF_TIPC,
  1595. .release = release,
  1596. .bind = bind,
  1597. .connect = connect,
  1598. .socketpair = sock_no_socketpair,
  1599. .accept = accept,
  1600. .getname = get_name,
  1601. .poll = poll,
  1602. .ioctl = sock_no_ioctl,
  1603. .listen = listen,
  1604. .shutdown = shutdown,
  1605. .setsockopt = setsockopt,
  1606. .getsockopt = getsockopt,
  1607. .sendmsg = send_packet,
  1608. .recvmsg = recv_msg,
  1609. .mmap = sock_no_mmap,
  1610. .sendpage = sock_no_sendpage
  1611. };
  1612. static const struct proto_ops stream_ops = {
  1613. .owner = THIS_MODULE,
  1614. .family = AF_TIPC,
  1615. .release = release,
  1616. .bind = bind,
  1617. .connect = connect,
  1618. .socketpair = sock_no_socketpair,
  1619. .accept = accept,
  1620. .getname = get_name,
  1621. .poll = poll,
  1622. .ioctl = sock_no_ioctl,
  1623. .listen = listen,
  1624. .shutdown = shutdown,
  1625. .setsockopt = setsockopt,
  1626. .getsockopt = getsockopt,
  1627. .sendmsg = send_stream,
  1628. .recvmsg = recv_stream,
  1629. .mmap = sock_no_mmap,
  1630. .sendpage = sock_no_sendpage
  1631. };
  1632. static const struct net_proto_family tipc_family_ops = {
  1633. .owner = THIS_MODULE,
  1634. .family = AF_TIPC,
  1635. .create = tipc_create
  1636. };
  1637. static struct proto tipc_proto = {
  1638. .name = "TIPC",
  1639. .owner = THIS_MODULE,
  1640. .obj_size = sizeof(struct tipc_sock)
  1641. };
  1642. /**
  1643. * tipc_socket_init - initialize TIPC socket interface
  1644. *
  1645. * Returns 0 on success, errno otherwise
  1646. */
  1647. int tipc_socket_init(void)
  1648. {
  1649. int res;
  1650. res = proto_register(&tipc_proto, 1);
  1651. if (res) {
  1652. pr_err("Failed to register TIPC protocol type\n");
  1653. goto out;
  1654. }
  1655. res = sock_register(&tipc_family_ops);
  1656. if (res) {
  1657. pr_err("Failed to register TIPC socket type\n");
  1658. proto_unregister(&tipc_proto);
  1659. goto out;
  1660. }
  1661. sockets_enabled = 1;
  1662. out:
  1663. return res;
  1664. }
  1665. /**
  1666. * tipc_socket_stop - stop TIPC socket interface
  1667. */
  1668. void tipc_socket_stop(void)
  1669. {
  1670. if (!sockets_enabled)
  1671. return;
  1672. sockets_enabled = 0;
  1673. sock_unregister(tipc_family_ops.family);
  1674. proto_unregister(&tipc_proto);
  1675. }