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