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