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