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