PageRenderTime 71ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/net/tipc/socket.c

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