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