PageRenderTime 57ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/net/tipc/socket.c

https://bitbucket.org/digetx/picasso-kernel
C | 1900 lines | 1171 code | 219 blank | 510 comment | 282 complexity | 8789eb6f9de918a2dd8f70a8b3c8e0f9 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  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. memset(&addr->addr, 0, sizeof(addr->addr));
  724. addr->addr.id.ref = msg_origport(msg);
  725. addr->addr.id.node = msg_orignode(msg);
  726. addr->addr.name.domain = 0; /* could leave uninitialized */
  727. addr->scope = 0; /* could leave uninitialized */
  728. m->msg_namelen = sizeof(struct sockaddr_tipc);
  729. }
  730. }
  731. /**
  732. * anc_data_recv - optionally capture ancillary data for received message
  733. * @m: descriptor for message info
  734. * @msg: received message header
  735. * @tport: TIPC port associated with message
  736. *
  737. * Note: Ancillary data is not captured if not requested by receiver.
  738. *
  739. * Returns 0 if successful, otherwise errno
  740. */
  741. static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
  742. struct tipc_port *tport)
  743. {
  744. u32 anc_data[3];
  745. u32 err;
  746. u32 dest_type;
  747. int has_name;
  748. int res;
  749. if (likely(m->msg_controllen == 0))
  750. return 0;
  751. /* Optionally capture errored message object(s) */
  752. err = msg ? msg_errcode(msg) : 0;
  753. if (unlikely(err)) {
  754. anc_data[0] = err;
  755. anc_data[1] = msg_data_sz(msg);
  756. res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
  757. if (res)
  758. return res;
  759. if (anc_data[1]) {
  760. res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
  761. msg_data(msg));
  762. if (res)
  763. return res;
  764. }
  765. }
  766. /* Optionally capture message destination object */
  767. dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
  768. switch (dest_type) {
  769. case TIPC_NAMED_MSG:
  770. has_name = 1;
  771. anc_data[0] = msg_nametype(msg);
  772. anc_data[1] = msg_namelower(msg);
  773. anc_data[2] = msg_namelower(msg);
  774. break;
  775. case TIPC_MCAST_MSG:
  776. has_name = 1;
  777. anc_data[0] = msg_nametype(msg);
  778. anc_data[1] = msg_namelower(msg);
  779. anc_data[2] = msg_nameupper(msg);
  780. break;
  781. case TIPC_CONN_MSG:
  782. has_name = (tport->conn_type != 0);
  783. anc_data[0] = tport->conn_type;
  784. anc_data[1] = tport->conn_instance;
  785. anc_data[2] = tport->conn_instance;
  786. break;
  787. default:
  788. has_name = 0;
  789. }
  790. if (has_name) {
  791. res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
  792. if (res)
  793. return res;
  794. }
  795. return 0;
  796. }
  797. /**
  798. * recv_msg - receive packet-oriented message
  799. * @iocb: (unused)
  800. * @m: descriptor for message info
  801. * @buf_len: total size of user buffer area
  802. * @flags: receive flags
  803. *
  804. * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
  805. * If the complete message doesn't fit in user area, truncate it.
  806. *
  807. * Returns size of returned message data, errno otherwise
  808. */
  809. static int recv_msg(struct kiocb *iocb, struct socket *sock,
  810. struct msghdr *m, size_t buf_len, int flags)
  811. {
  812. struct sock *sk = sock->sk;
  813. struct tipc_port *tport = tipc_sk_port(sk);
  814. struct sk_buff *buf;
  815. struct tipc_msg *msg;
  816. long timeout;
  817. unsigned int sz;
  818. u32 err;
  819. int res;
  820. /* Catch invalid receive requests */
  821. if (unlikely(!buf_len))
  822. return -EINVAL;
  823. lock_sock(sk);
  824. if (unlikely(sock->state == SS_UNCONNECTED)) {
  825. res = -ENOTCONN;
  826. goto exit;
  827. }
  828. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  829. restart:
  830. /* Look for a message in receive queue; wait if necessary */
  831. while (skb_queue_empty(&sk->sk_receive_queue)) {
  832. if (sock->state == SS_DISCONNECTING) {
  833. res = -ENOTCONN;
  834. goto exit;
  835. }
  836. if (timeout <= 0L) {
  837. res = timeout ? timeout : -EWOULDBLOCK;
  838. goto exit;
  839. }
  840. release_sock(sk);
  841. timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
  842. tipc_rx_ready(sock),
  843. timeout);
  844. lock_sock(sk);
  845. }
  846. /* Look at first message in receive queue */
  847. buf = skb_peek(&sk->sk_receive_queue);
  848. msg = buf_msg(buf);
  849. sz = msg_data_sz(msg);
  850. err = msg_errcode(msg);
  851. /* Discard an empty non-errored message & try again */
  852. if ((!sz) && (!err)) {
  853. advance_rx_queue(sk);
  854. goto restart;
  855. }
  856. /* Capture sender's address (optional) */
  857. set_orig_addr(m, msg);
  858. /* Capture ancillary data (optional) */
  859. res = anc_data_recv(m, msg, tport);
  860. if (res)
  861. goto exit;
  862. /* Capture message data (if valid) & compute return value (always) */
  863. if (!err) {
  864. if (unlikely(buf_len < sz)) {
  865. sz = buf_len;
  866. m->msg_flags |= MSG_TRUNC;
  867. }
  868. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
  869. m->msg_iov, sz);
  870. if (res)
  871. goto exit;
  872. res = sz;
  873. } else {
  874. if ((sock->state == SS_READY) ||
  875. ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
  876. res = 0;
  877. else
  878. res = -ECONNRESET;
  879. }
  880. /* Consume received message (optional) */
  881. if (likely(!(flags & MSG_PEEK))) {
  882. if ((sock->state != SS_READY) &&
  883. (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  884. tipc_acknowledge(tport->ref, tport->conn_unacked);
  885. advance_rx_queue(sk);
  886. }
  887. exit:
  888. release_sock(sk);
  889. return res;
  890. }
  891. /**
  892. * recv_stream - receive stream-oriented data
  893. * @iocb: (unused)
  894. * @m: descriptor for message info
  895. * @buf_len: total size of user buffer area
  896. * @flags: receive flags
  897. *
  898. * Used for SOCK_STREAM messages only. If not enough data is available
  899. * will optionally wait for more; never truncates data.
  900. *
  901. * Returns size of returned message data, errno otherwise
  902. */
  903. static int recv_stream(struct kiocb *iocb, struct socket *sock,
  904. struct msghdr *m, size_t buf_len, int flags)
  905. {
  906. struct sock *sk = sock->sk;
  907. struct tipc_port *tport = tipc_sk_port(sk);
  908. struct sk_buff *buf;
  909. struct tipc_msg *msg;
  910. long timeout;
  911. unsigned int sz;
  912. int sz_to_copy, target, needed;
  913. int sz_copied = 0;
  914. u32 err;
  915. int res = 0;
  916. /* Catch invalid receive attempts */
  917. if (unlikely(!buf_len))
  918. return -EINVAL;
  919. lock_sock(sk);
  920. if (unlikely((sock->state == SS_UNCONNECTED) ||
  921. (sock->state == SS_CONNECTING))) {
  922. res = -ENOTCONN;
  923. goto exit;
  924. }
  925. target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
  926. timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  927. restart:
  928. /* Look for a message in receive queue; wait if necessary */
  929. while (skb_queue_empty(&sk->sk_receive_queue)) {
  930. if (sock->state == SS_DISCONNECTING) {
  931. res = -ENOTCONN;
  932. goto exit;
  933. }
  934. if (timeout <= 0L) {
  935. res = timeout ? timeout : -EWOULDBLOCK;
  936. goto exit;
  937. }
  938. release_sock(sk);
  939. timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
  940. tipc_rx_ready(sock),
  941. timeout);
  942. lock_sock(sk);
  943. }
  944. /* Look at first message in receive queue */
  945. buf = skb_peek(&sk->sk_receive_queue);
  946. msg = buf_msg(buf);
  947. sz = msg_data_sz(msg);
  948. err = msg_errcode(msg);
  949. /* Discard an empty non-errored message & try again */
  950. if ((!sz) && (!err)) {
  951. advance_rx_queue(sk);
  952. goto restart;
  953. }
  954. /* Optionally capture sender's address & ancillary data of first msg */
  955. if (sz_copied == 0) {
  956. set_orig_addr(m, msg);
  957. res = anc_data_recv(m, msg, tport);
  958. if (res)
  959. goto exit;
  960. }
  961. /* Capture message data (if valid) & compute return value (always) */
  962. if (!err) {
  963. u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
  964. sz -= offset;
  965. needed = (buf_len - sz_copied);
  966. sz_to_copy = (sz <= needed) ? sz : needed;
  967. res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
  968. m->msg_iov, sz_to_copy);
  969. if (res)
  970. goto exit;
  971. sz_copied += sz_to_copy;
  972. if (sz_to_copy < sz) {
  973. if (!(flags & MSG_PEEK))
  974. TIPC_SKB_CB(buf)->handle =
  975. (void *)(unsigned long)(offset + sz_to_copy);
  976. goto exit;
  977. }
  978. } else {
  979. if (sz_copied != 0)
  980. goto exit; /* can't add error msg to valid data */
  981. if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
  982. res = 0;
  983. else
  984. res = -ECONNRESET;
  985. }
  986. /* Consume received message (optional) */
  987. if (likely(!(flags & MSG_PEEK))) {
  988. if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
  989. tipc_acknowledge(tport->ref, tport->conn_unacked);
  990. advance_rx_queue(sk);
  991. }
  992. /* Loop around if more data is required */
  993. if ((sz_copied < buf_len) && /* didn't get all requested data */
  994. (!skb_queue_empty(&sk->sk_receive_queue) ||
  995. (sz_copied < target)) && /* and more is ready or required */
  996. (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
  997. (!err)) /* and haven't reached a FIN */
  998. goto restart;
  999. exit:
  1000. release_sock(sk);
  1001. return sz_copied ? sz_copied : res;
  1002. }
  1003. /**
  1004. * tipc_write_space - wake up thread if port congestion is released
  1005. * @sk: socket
  1006. */
  1007. static void tipc_write_space(struct sock *sk)
  1008. {
  1009. struct socket_wq *wq;
  1010. rcu_read_lock();
  1011. wq = rcu_dereference(sk->sk_wq);
  1012. if (wq_has_sleeper(wq))
  1013. wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
  1014. POLLWRNORM | POLLWRBAND);
  1015. rcu_read_unlock();
  1016. }
  1017. /**
  1018. * tipc_data_ready - wake up threads to indicate messages have been received
  1019. * @sk: socket
  1020. * @len: the length of messages
  1021. */
  1022. static void tipc_data_ready(struct sock *sk, int len)
  1023. {
  1024. struct socket_wq *wq;
  1025. rcu_read_lock();
  1026. wq = rcu_dereference(sk->sk_wq);
  1027. if (wq_has_sleeper(wq))
  1028. wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
  1029. POLLRDNORM | POLLRDBAND);
  1030. rcu_read_unlock();
  1031. }
  1032. /**
  1033. * rx_queue_full - determine if receive queue can accept another message
  1034. * @msg: message to be added to queue
  1035. * @queue_size: current size of queue
  1036. * @base: nominal maximum size of queue
  1037. *
  1038. * Returns 1 if queue is unable to accept message, 0 otherwise
  1039. */
  1040. static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
  1041. {
  1042. u32 threshold;
  1043. u32 imp = msg_importance(msg);
  1044. if (imp == TIPC_LOW_IMPORTANCE)
  1045. threshold = base;
  1046. else if (imp == TIPC_MEDIUM_IMPORTANCE)
  1047. threshold = base * 2;
  1048. else if (imp == TIPC_HIGH_IMPORTANCE)
  1049. threshold = base * 100;
  1050. else
  1051. return 0;
  1052. if (msg_connected(msg))
  1053. threshold *= 4;
  1054. return queue_size >= threshold;
  1055. }
  1056. /**
  1057. * filter_connect - Handle all incoming messages for a connection-based socket
  1058. * @tsock: TIPC socket
  1059. * @msg: message
  1060. *
  1061. * Returns TIPC error status code and socket error status code
  1062. * once it encounters some errors
  1063. */
  1064. static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
  1065. {
  1066. struct socket *sock = tsock->sk.sk_socket;
  1067. struct tipc_msg *msg = buf_msg(*buf);
  1068. struct sock *sk = &tsock->sk;
  1069. u32 retval = TIPC_ERR_NO_PORT;
  1070. int res;
  1071. if (msg_mcast(msg))
  1072. return retval;
  1073. switch ((int)sock->state) {
  1074. case SS_CONNECTED:
  1075. /* Accept only connection-based messages sent by peer */
  1076. if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
  1077. if (unlikely(msg_errcode(msg))) {
  1078. sock->state = SS_DISCONNECTING;
  1079. __tipc_disconnect(tsock->p);
  1080. }
  1081. retval = TIPC_OK;
  1082. }
  1083. break;
  1084. case SS_CONNECTING:
  1085. /* Accept only ACK or NACK message */
  1086. if (unlikely(msg_errcode(msg))) {
  1087. sock->state = SS_DISCONNECTING;
  1088. sk->sk_err = ECONNREFUSED;
  1089. retval = TIPC_OK;
  1090. break;
  1091. }
  1092. if (unlikely(!msg_connected(msg)))
  1093. break;
  1094. res = auto_connect(sock, msg);
  1095. if (res) {
  1096. sock->state = SS_DISCONNECTING;
  1097. sk->sk_err = -res;
  1098. retval = TIPC_OK;
  1099. break;
  1100. }
  1101. /* If an incoming message is an 'ACK-', it should be
  1102. * discarded here because it doesn't contain useful
  1103. * data. In addition, we should try to wake up
  1104. * connect() routine if sleeping.
  1105. */
  1106. if (msg_data_sz(msg) == 0) {
  1107. kfree_skb(*buf);
  1108. *buf = NULL;
  1109. if (waitqueue_active(sk_sleep(sk)))
  1110. wake_up_interruptible(sk_sleep(sk));
  1111. }
  1112. retval = TIPC_OK;
  1113. break;
  1114. case SS_LISTENING:
  1115. case SS_UNCONNECTED:
  1116. /* Accept only SYN message */
  1117. if (!msg_connected(msg) && !(msg_errcode(msg)))
  1118. retval = TIPC_OK;
  1119. break;
  1120. case SS_DISCONNECTING:
  1121. break;
  1122. default:
  1123. pr_err("Unknown socket state %u\n", sock->state);
  1124. }
  1125. return retval;
  1126. }
  1127. /**
  1128. * filter_rcv - validate incoming message
  1129. * @sk: socket
  1130. * @buf: message
  1131. *
  1132. * Enqueues message on receive queue if acceptable; optionally handles
  1133. * disconnect indication for a connected socket.
  1134. *
  1135. * Called with socket lock already taken; port lock may also be taken.
  1136. *
  1137. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1138. */
  1139. static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
  1140. {
  1141. struct socket *sock = sk->sk_socket;
  1142. struct tipc_msg *msg = buf_msg(buf);
  1143. u32 recv_q_len;
  1144. u32 res = TIPC_OK;
  1145. /* Reject message if it is wrong sort of message for socket */
  1146. if (msg_type(msg) > TIPC_DIRECT_MSG)
  1147. return TIPC_ERR_NO_PORT;
  1148. if (sock->state == SS_READY) {
  1149. if (msg_connected(msg))
  1150. return TIPC_ERR_NO_PORT;
  1151. } else {
  1152. res = filter_connect(tipc_sk(sk), &buf);
  1153. if (res != TIPC_OK || buf == NULL)
  1154. return res;
  1155. }
  1156. /* Reject message if there isn't room to queue it */
  1157. recv_q_len = skb_queue_len(&sk->sk_receive_queue);
  1158. if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
  1159. if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
  1160. return TIPC_ERR_OVERLOAD;
  1161. }
  1162. /* Enqueue message (finally!) */
  1163. TIPC_SKB_CB(buf)->handle = 0;
  1164. __skb_queue_tail(&sk->sk_receive_queue, buf);
  1165. sk->sk_data_ready(sk, 0);
  1166. return TIPC_OK;
  1167. }
  1168. /**
  1169. * backlog_rcv - handle incoming message from backlog queue
  1170. * @sk: socket
  1171. * @buf: message
  1172. *
  1173. * Caller must hold socket lock, but not port lock.
  1174. *
  1175. * Returns 0
  1176. */
  1177. static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
  1178. {
  1179. u32 res;
  1180. res = filter_rcv(sk, buf);
  1181. if (res)
  1182. tipc_reject_msg(buf, res);
  1183. return 0;
  1184. }
  1185. /**
  1186. * dispatch - handle incoming message
  1187. * @tport: TIPC port that received message
  1188. * @buf: message
  1189. *
  1190. * Called with port lock already taken.
  1191. *
  1192. * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
  1193. */
  1194. static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
  1195. {
  1196. struct sock *sk = (struct sock *)tport->usr_handle;
  1197. u32 res;
  1198. /*
  1199. * Process message if socket is unlocked; otherwise add to backlog queue
  1200. *
  1201. * This code is based on sk_receive_skb(), but must be distinct from it
  1202. * since a TIPC-specific filter/reject mechanism is utilized
  1203. */
  1204. bh_lock_sock(sk);
  1205. if (!sock_owned_by_user(sk)) {
  1206. res = filter_rcv(sk, buf);
  1207. } else {
  1208. if (sk_add_backlog(sk, buf, sk->sk_rcvbuf))
  1209. res = TIPC_ERR_OVERLOAD;
  1210. else
  1211. res = TIPC_OK;
  1212. }
  1213. bh_unlock_sock(sk);
  1214. return res;
  1215. }
  1216. /**
  1217. * wakeupdispatch - wake up port after congestion
  1218. * @tport: port to wakeup
  1219. *
  1220. * Called with port lock already taken.
  1221. */
  1222. static void wakeupdispatch(struct tipc_port *tport)
  1223. {
  1224. struct sock *sk = (struct sock *)tport->usr_handle;
  1225. sk->sk_write_space(sk);
  1226. }
  1227. /**
  1228. * connect - establish a connection to another TIPC port
  1229. * @sock: socket structure
  1230. * @dest: socket address for destination port
  1231. * @destlen: size of socket address data structure
  1232. * @flags: file-related flags associated with socket
  1233. *
  1234. * Returns 0 on success, errno otherwise
  1235. */
  1236. static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
  1237. int flags)
  1238. {
  1239. struct sock *sk = sock->sk;
  1240. struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
  1241. struct msghdr m = {NULL,};
  1242. unsigned int timeout;
  1243. int res;
  1244. lock_sock(sk);
  1245. /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
  1246. if (sock->state == SS_READY) {
  1247. res = -EOPNOTSUPP;
  1248. goto exit;
  1249. }
  1250. /*
  1251. * Reject connection attempt using multicast address
  1252. *
  1253. * Note: send_msg() validates the rest of the address fields,
  1254. * so there's no need to do it here
  1255. */
  1256. if (dst->addrtype == TIPC_ADDR_MCAST) {
  1257. res = -EINVAL;
  1258. goto exit;
  1259. }
  1260. timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
  1261. switch (sock->state) {
  1262. case SS_UNCONNECTED:
  1263. /* Send a 'SYN-' to destination */
  1264. m.msg_name = dest;
  1265. m.msg_namelen = destlen;
  1266. /* If connect is in non-blocking case, set MSG_DONTWAIT to
  1267. * indicate send_msg() is never blocked.
  1268. */
  1269. if (!timeout)
  1270. m.msg_flags = MSG_DONTWAIT;
  1271. res = send_msg(NULL, sock, &m, 0);
  1272. if ((res < 0) && (res != -EWOULDBLOCK))
  1273. goto exit;
  1274. /* Just entered SS_CONNECTING state; the only
  1275. * difference is that return value in non-blocking
  1276. * case is EINPROGRESS, rather than EALREADY.
  1277. */
  1278. res = -EINPROGRESS;
  1279. break;
  1280. case SS_CONNECTING:
  1281. res = -EALREADY;
  1282. break;
  1283. case SS_CONNECTED:
  1284. res = -EISCONN;
  1285. break;
  1286. default:
  1287. res = -EINVAL;
  1288. goto exit;
  1289. }
  1290. if (sock->state == SS_CONNECTING) {
  1291. if (!timeout)
  1292. goto exit;
  1293. /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
  1294. release_sock(sk);
  1295. res = wait_event_interruptible_timeout(*sk_sleep(sk),
  1296. sock->state != SS_CONNECTING,
  1297. timeout ? (long)msecs_to_jiffies(timeout)
  1298. : MAX_SCHEDULE_TIMEOUT);
  1299. lock_sock(sk);
  1300. if (res <= 0) {
  1301. if (res == 0)
  1302. res = -ETIMEDOUT;
  1303. else
  1304. ; /* leave "res" unchanged */
  1305. goto exit;
  1306. }
  1307. }
  1308. if (unlikely(sock->state == SS_DISCONNECTING))
  1309. res = sock_error(sk);
  1310. else
  1311. res = 0;
  1312. exit:
  1313. release_sock(sk);
  1314. return res;
  1315. }
  1316. /**
  1317. * listen - allow socket to listen for incoming connections
  1318. * @sock: socket structure
  1319. * @len: (unused)
  1320. *
  1321. * Returns 0 on success, errno otherwise
  1322. */
  1323. static int listen(struct socket *sock, int len)
  1324. {
  1325. struct sock *sk = sock->sk;
  1326. int res;
  1327. lock_sock(sk);
  1328. if (sock->state != SS_UNCONNECTED)
  1329. res = -EINVAL;
  1330. else {
  1331. sock->state = SS_LISTENING;
  1332. res = 0;
  1333. }
  1334. release_sock(sk);
  1335. return res;
  1336. }
  1337. /**
  1338. * accept - wait for connection request
  1339. * @sock: listening socket
  1340. * @newsock: new socket that is to be connected
  1341. * @flags: file-related flags associated with socket
  1342. *
  1343. * Returns 0 on success, errno otherwise
  1344. */
  1345. static int accept(struct socket *sock, struct socket *new_sock, int flags)
  1346. {
  1347. struct sock *new_sk, *sk = sock->sk;
  1348. struct sk_buff *buf;
  1349. struct tipc_sock *new_tsock;
  1350. struct tipc_port *new_tport;
  1351. struct tipc_msg *msg;
  1352. u32 new_ref;
  1353. int res;
  1354. lock_sock(sk);
  1355. if (sock->state != SS_LISTENING) {
  1356. res = -EINVAL;
  1357. goto exit;
  1358. }
  1359. while (skb_queue_empty(&sk->sk_receive_queue)) {
  1360. if (flags & O_NONBLOCK) {
  1361. res = -EWOULDBLOCK;
  1362. goto exit;
  1363. }
  1364. release_sock(sk);
  1365. res = wait_event_interruptible(*sk_sleep(sk),
  1366. (!skb_queue_empty(&sk->sk_receive_queue)));
  1367. lock_sock(sk);
  1368. if (res)
  1369. goto exit;
  1370. }
  1371. buf = skb_peek(&sk->sk_receive_queue);
  1372. res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
  1373. if (res)
  1374. goto exit;
  1375. new_sk = new_sock->sk;
  1376. new_tsock = tipc_sk(new_sk);
  1377. new_tport = new_tsock->p;
  1378. new_ref = new_tport->ref;
  1379. msg = buf_msg(buf);
  1380. /* we lock on new_sk; but lockdep sees the lock on sk */
  1381. lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
  1382. /*
  1383. * Reject any stray messages received by new socket
  1384. * before the socket lock was taken (very, very unlikely)
  1385. */
  1386. reject_rx_queue(new_sk);
  1387. /* Connect new socket to it's peer */
  1388. new_tsock->peer_name.ref = msg_origport(msg);
  1389. new_tsock->peer_name.node = msg_orignode(msg);
  1390. tipc_connect(new_ref, &new_tsock->peer_name);
  1391. new_sock->state = SS_CONNECTED;
  1392. tipc_set_portimportance(new_ref, msg_importance(msg));
  1393. if (msg_named(msg)) {
  1394. new_tport->conn_type = msg_nametype(msg);
  1395. new_tport->conn_instance = msg_nameinst(msg);
  1396. }
  1397. /*
  1398. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
  1399. * Respond to 'SYN+' by queuing it on new socket.
  1400. */
  1401. if (!msg_data_sz(msg)) {
  1402. struct msghdr m = {NULL,};
  1403. advance_rx_queue(sk);
  1404. send_packet(NULL, new_sock, &m, 0);
  1405. } else {
  1406. __skb_dequeue(&sk->sk_receive_queue);
  1407. __skb_queue_head(&new_sk->sk_receive_queue, buf);
  1408. }
  1409. release_sock(new_sk);
  1410. exit:
  1411. release_sock(sk);
  1412. return res;
  1413. }
  1414. /**
  1415. * shutdown - shutdown socket connection
  1416. * @sock: socket structure
  1417. * @how: direction to close (must be SHUT_RDWR)
  1418. *
  1419. * Terminates connection (if necessary), then purges socket's receive queue.
  1420. *
  1421. * Returns 0 on success, errno otherwise
  1422. */
  1423. static int shutdown(struct socket *sock, int how)
  1424. {
  1425. struct sock *sk = sock->sk;
  1426. struct tipc_port *tport = tipc_sk_port(sk);
  1427. struct sk_buff *buf;
  1428. int res;
  1429. if (how != SHUT_RDWR)
  1430. return -EINVAL;
  1431. lock_sock(sk);
  1432. switch (sock->state) {
  1433. case SS_CONNECTING:
  1434. case SS_CONNECTED:
  1435. restart:
  1436. /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
  1437. buf = __skb_dequeue(&sk->sk_receive_queue);
  1438. if (buf) {
  1439. if (TIPC_SKB_CB(buf)->handle != 0) {
  1440. kfree_skb(buf);
  1441. goto restart;
  1442. }
  1443. tipc_disconnect(tport->ref);
  1444. tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
  1445. } else {
  1446. tipc_shutdown(tport->ref);
  1447. }
  1448. sock->state = SS_DISCONNECTING;
  1449. /* fall through */
  1450. case SS_DISCONNECTING:
  1451. /* Discard any unreceived messages */
  1452. discard_rx_queue(sk);
  1453. /* Wake up anyone sleeping in poll */
  1454. sk->sk_state_change(sk);
  1455. res = 0;
  1456. break;
  1457. default:
  1458. res = -ENOTCONN;
  1459. }
  1460. release_sock(sk);
  1461. return res;
  1462. }
  1463. /**
  1464. * setsockopt - set socket option
  1465. * @sock: socket structure
  1466. * @lvl: option level
  1467. * @opt: option identifier
  1468. * @ov: pointer to new option value
  1469. * @ol: length of option value
  1470. *
  1471. * For stream sockets only, accepts and ignores all IPPROTO_TCP options
  1472. * (to ease compatibility).
  1473. *
  1474. * Returns 0 on success, errno otherwise
  1475. */
  1476. static int setsockopt(struct socket *sock,
  1477. int lvl, int opt, char __user *ov, unsigned int ol)
  1478. {
  1479. struct sock *sk = sock->sk;
  1480. struct tipc_port *tport = tipc_sk_port(sk);
  1481. u32 value;
  1482. int res;
  1483. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1484. return 0;
  1485. if (lvl != SOL_TIPC)
  1486. return -ENOPROTOOPT;
  1487. if (ol < sizeof(value))
  1488. return -EINVAL;
  1489. res = get_user(value, (u32 __user *)ov);
  1490. if (res)
  1491. return res;
  1492. lock_sock(sk);
  1493. switch (opt) {
  1494. case TIPC_IMPORTANCE:
  1495. res = tipc_set_portimportance(tport->ref, value);
  1496. break;
  1497. case TIPC_SRC_DROPPABLE:
  1498. if (sock->type != SOCK_STREAM)
  1499. res = tipc_set_portunreliable(tport->ref, value);
  1500. else
  1501. res = -ENOPROTOOPT;
  1502. break;
  1503. case TIPC_DEST_DROPPABLE:
  1504. res = tipc_set_portunreturnable(tport->ref, value);
  1505. break;
  1506. case TIPC_CONN_TIMEOUT:
  1507. tipc_sk(sk)->conn_timeout = value;
  1508. /* no need to set "res", since already 0 at this point */
  1509. break;
  1510. default:
  1511. res = -EINVAL;
  1512. }
  1513. release_sock(sk);
  1514. return res;
  1515. }
  1516. /**
  1517. * getsockopt - get socket option
  1518. * @sock: socket structure
  1519. * @lvl: option level
  1520. * @opt: option identifier
  1521. * @ov: receptacle for option value
  1522. * @ol: receptacle for length of option value
  1523. *
  1524. * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
  1525. * (to ease compatibility).
  1526. *
  1527. * Returns 0 on success, errno otherwise
  1528. */
  1529. static int getsockopt(struct socket *sock,
  1530. int lvl, int opt, char __user *ov, int __user *ol)
  1531. {
  1532. struct sock *sk = sock->sk;
  1533. struct tipc_port *tport = tipc_sk_port(sk);
  1534. int len;
  1535. u32 value;
  1536. int res;
  1537. if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
  1538. return put_user(0, ol);
  1539. if (lvl != SOL_TIPC)
  1540. return -ENOPROTOOPT;
  1541. res = get_user(len, ol);
  1542. if (res)
  1543. return res;
  1544. lock_sock(sk);
  1545. switch (opt) {
  1546. case TIPC_IMPORTANCE:
  1547. res = tipc_portimportance(tport->ref, &value);
  1548. break;
  1549. case TIPC_SRC_DROPPABLE:
  1550. res = tipc_portunreliable(tport->ref, &value);
  1551. break;
  1552. case TIPC_DEST_DROPPABLE:
  1553. res = tipc_portunreturnable(tport->ref, &value);
  1554. break;
  1555. case TIPC_CONN_TIMEOUT:
  1556. value = tipc_sk(sk)->conn_timeout;
  1557. /* no need to set "res", since already 0 at this point */
  1558. break;
  1559. case TIPC_NODE_RECVQ_DEPTH:
  1560. value = 0; /* was tipc_queue_size, now obsolete */
  1561. break;
  1562. case TIPC_SOCK_RECVQ_DEPTH:
  1563. value = skb_queue_len(&sk->sk_receive_queue);
  1564. break;
  1565. default:
  1566. res = -EINVAL;
  1567. }
  1568. release_sock(sk);
  1569. if (res)
  1570. return res; /* "get" failed */
  1571. if (len < sizeof(value))
  1572. return -EINVAL;
  1573. if (copy_to_user(ov, &value, sizeof(value)))
  1574. return -EFAULT;
  1575. return put_user(sizeof(value), ol);
  1576. }
  1577. /* Protocol switches for the various types of TIPC sockets */
  1578. static const struct proto_ops msg_ops = {
  1579. .owner = THIS_MODULE,
  1580. .family = AF_TIPC,
  1581. .release = release,
  1582. .bind = bind,
  1583. .connect = connect,
  1584. .socketpair = sock_no_socketpair,
  1585. .accept = sock_no_accept,
  1586. .getname = get_name,
  1587. .poll = poll,
  1588. .ioctl = sock_no_ioctl,
  1589. .listen = sock_no_listen,
  1590. .shutdown = shutdown,
  1591. .setsockopt = setsockopt,
  1592. .getsockopt = getsockopt,
  1593. .sendmsg = send_msg,
  1594. .recvmsg = recv_msg,
  1595. .mmap = sock_no_mmap,
  1596. .sendpage = sock_no_sendpage
  1597. };
  1598. static const struct proto_ops packet_ops = {
  1599. .owner = THIS_MODULE,
  1600. .family = AF_TIPC,
  1601. .release = release,
  1602. .bind = bind,
  1603. .connect = connect,
  1604. .socketpair = sock_no_socketpair,
  1605. .accept = accept,
  1606. .getname = get_name,
  1607. .poll = poll,
  1608. .ioctl = sock_no_ioctl,
  1609. .listen = listen,
  1610. .shutdown = shutdown,
  1611. .setsockopt = setsockopt,
  1612. .getsockopt = getsockopt,
  1613. .sendmsg = send_packet,
  1614. .recvmsg = recv_msg,
  1615. .mmap = sock_no_mmap,
  1616. .sendpage = sock_no_sendpage
  1617. };
  1618. static const struct proto_ops stream_ops = {
  1619. .owner = THIS_MODULE,
  1620. .family = AF_TIPC,
  1621. .release = release,
  1622. .bind = bind,
  1623. .connect = connect,
  1624. .socketpair = sock_no_socketpair,
  1625. .accept = accept,
  1626. .getname = get_name,
  1627. .poll = poll,
  1628. .ioctl = sock_no_ioctl,
  1629. .listen = listen,
  1630. .shutdown = shutdown,
  1631. .setsockopt = setsockopt,
  1632. .getsockopt = getsockopt,
  1633. .sendmsg = send_stream,
  1634. .recvmsg = recv_stream,
  1635. .mmap = sock_no_mmap,
  1636. .sendpage = sock_no_sendpage
  1637. };
  1638. static const struct net_proto_family tipc_family_ops = {
  1639. .owner = THIS_MODULE,
  1640. .family = AF_TIPC,
  1641. .create = tipc_create
  1642. };
  1643. static struct proto tipc_proto = {
  1644. .name = "TIPC",
  1645. .owner = THIS_MODULE,
  1646. .obj_size = sizeof(struct tipc_sock)
  1647. };
  1648. /**
  1649. * tipc_socket_init - initialize TIPC socket interface
  1650. *
  1651. * Returns 0 on success, errno otherwise
  1652. */
  1653. int tipc_socket_init(void)
  1654. {
  1655. int res;
  1656. res = proto_register(&tipc_proto, 1);
  1657. if (res) {
  1658. pr_err("Failed to register TIPC protocol type\n");
  1659. goto out;
  1660. }
  1661. res = sock_register(&tipc_family_ops);
  1662. if (res) {
  1663. pr_err("Failed to register TIPC socket type\n");
  1664. proto_unregister(&tipc_proto);
  1665. goto out;
  1666. }
  1667. sockets_enabled = 1;
  1668. out:
  1669. return res;
  1670. }
  1671. /**
  1672. * tipc_socket_stop - stop TIPC socket interface
  1673. */
  1674. void tipc_socket_stop(void)
  1675. {
  1676. if (!sockets_enabled)
  1677. return;
  1678. sockets_enabled = 0;
  1679. sock_unregister(tipc_family_ops.family);
  1680. proto_unregister(&tipc_proto);
  1681. }