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