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