PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/kernel/linux-source-2.6.32/net/tipc/socket.c

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