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