blob: ea33eab4fb9d7f082e993f2db6feceda2beb9562 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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 Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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 Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -040038#include "ref.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000039#include "port.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050040#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020041#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050042#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040043#include <linux/export.h>
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -040044#include "config.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040045
Per Lidenb97bf3f2006-01-02 19:04:38 +010046#define SS_LISTENING -1 /* socket is listening */
47#define SS_READY -2 /* socket is connectionless */
48
Allan Stephens3654ea02008-04-13 21:35:11 -070049#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloydadebc02014-08-22 18:09:11 -040050#define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
Jon Paul Maloyac0074e2014-06-25 20:41:41 -050051#define TIPC_FWD_MSG 1
Per Lidenb97bf3f2006-01-02 19:04:38 +010052
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040053static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040054static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080055static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080056static int tipc_release(struct socket *sock);
57static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -040058static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Jon Paul Maloy57289012014-08-22 18:09:09 -040059static void tipc_sk_timeout(unsigned long ref);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -040060static int tipc_sk_publish(struct tipc_port *port, uint scope,
61 struct tipc_name_seq const *seq);
62static int tipc_sk_withdraw(struct tipc_port *port, uint scope,
63 struct tipc_name_seq const *seq);
Per Lidenb97bf3f2006-01-02 19:04:38 +010064
Florian Westphalbca65ea2008-02-07 18:18:01 -080065static const struct proto_ops packet_ops;
66static const struct proto_ops stream_ops;
67static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010068
69static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040070static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010071
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090072/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070073 * 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 Lidenb97bf3f2006-01-02 19:04:38 +0100115 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400117#include "socket.h"
118
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700120 * advance_rx_queue - discard first buffer in socket receive queue
121 *
122 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700124static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400126 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127}
128
129/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700130 * reject_rx_queue - reject all buffers in socket receive queue
131 *
132 * Caller must hold socket lock
133 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700134static void reject_rx_queue(struct sock *sk)
135{
136 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500137 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700138
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500139 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
140 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400141 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500142 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700143}
144
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400145/* tipc_sk_peer_msg - verify if message was sent by connected port's peer
146 *
147 * Handles cases where the node's network address has changed from
148 * the default of <0.0.0> to its configured setting.
149 */
150static bool tipc_sk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
151{
152 u32 peer_port = tipc_port_peerport(&tsk->port);
153 u32 orig_node;
154 u32 peer_node;
155
156 if (unlikely(!tsk->port.connected))
157 return false;
158
159 if (unlikely(msg_origport(msg) != peer_port))
160 return false;
161
162 orig_node = msg_orignode(msg);
163 peer_node = tipc_port_peernode(&tsk->port);
164
165 if (likely(orig_node == peer_node))
166 return true;
167
168 if (!orig_node && (peer_node == tipc_own_addr))
169 return true;
170
171 if (!peer_node && (orig_node == tipc_own_addr))
172 return true;
173
174 return false;
175}
176
Allan Stephens0c3141e2008-04-15 00:22:02 -0700177/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400178 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700179 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 * @sock: pre-allocated socket structure
181 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800182 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900183 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700184 * This routine creates additional data structures used by the TIPC socket,
185 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 *
187 * Returns 0 on success, errno otherwise
188 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400189static int tipc_sk_create(struct net *net, struct socket *sock,
190 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700192 const struct proto_ops *ops;
193 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400195 struct tipc_sock *tsk;
196 struct tipc_port *port;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400197 struct tipc_msg *msg;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400198 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199
200 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 if (unlikely(protocol != 0))
202 return -EPROTONOSUPPORT;
203
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 switch (sock->type) {
205 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700206 ops = &stream_ops;
207 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208 break;
209 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700210 ops = &packet_ops;
211 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 break;
213 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700215 ops = &msg_ops;
216 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 break;
Allan Stephens49978652006-06-25 23:47:18 -0700218 default:
Allan Stephens49978652006-06-25 23:47:18 -0700219 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 }
221
Allan Stephens0c3141e2008-04-15 00:22:02 -0700222 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400223 if (!kern)
224 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
225 else
226 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
227
Allan Stephens0c3141e2008-04-15 00:22:02 -0700228 if (sk == NULL)
229 return -ENOMEM;
230
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400231 tsk = tipc_sk(sk);
232 port = &tsk->port;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -0400233 ref = tipc_ref_acquire(tsk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400234 if (!ref) {
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400235 pr_warn("Socket create failed; reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 return -ENOMEM;
237 }
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400238 port->max_pkt = MAX_PKT_DEFAULT;
239 port->ref = ref;
240 INIT_LIST_HEAD(&port->publications);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400241
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400242 msg = &port->phdr;
243 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
244 NAMED_H_SIZE, 0);
245 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246
Allan Stephens0c3141e2008-04-15 00:22:02 -0700247 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700248 sock->ops = ops;
249 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100250 sock_init_data(sock, sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -0400251 k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400252 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400253 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800254 sk->sk_data_ready = tipc_data_ready;
255 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400256 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500257 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400258 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700259
Allan Stephens0c3141e2008-04-15 00:22:02 -0700260 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400261 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700262 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400263 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700264 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 return 0;
266}
267
268/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400269 * tipc_sock_create_local - create TIPC socket from inside TIPC module
270 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
271 *
272 * We cannot use sock_creat_kern here because it bumps module user count.
273 * Since socket owner and creator is the same module we must make sure
274 * that module count remains zero for module local sockets, otherwise
275 * we cannot do rmmod.
276 *
277 * Returns 0 on success, errno otherwise
278 */
279int tipc_sock_create_local(int type, struct socket **res)
280{
281 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400282
283 rc = sock_create_lite(AF_TIPC, type, 0, res);
284 if (rc < 0) {
285 pr_err("Failed to create kernel socket\n");
286 return rc;
287 }
288 tipc_sk_create(&init_net, *res, 0, 1);
289
Ying Xuec5fa7b32013-06-17 10:54:39 -0400290 return 0;
291}
292
293/**
294 * tipc_sock_release_local - release socket created by tipc_sock_create_local
295 * @sock: the socket to be released.
296 *
297 * Module reference count is not incremented when such sockets are created,
298 * so we must keep it from being decremented when they are released.
299 */
300void tipc_sock_release_local(struct socket *sock)
301{
Ying Xue247f0f32014-02-18 16:06:46 +0800302 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400303 sock->ops = NULL;
304 sock_release(sock);
305}
306
307/**
308 * tipc_sock_accept_local - accept a connection on a socket created
309 * with tipc_sock_create_local. Use this function to avoid that
310 * module reference count is inadvertently incremented.
311 *
312 * @sock: the accepting socket
313 * @newsock: reference to the new socket to be created
314 * @flags: socket flags
315 */
316
317int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400318 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400319{
320 struct sock *sk = sock->sk;
321 int ret;
322
323 ret = sock_create_lite(sk->sk_family, sk->sk_type,
324 sk->sk_protocol, newsock);
325 if (ret < 0)
326 return ret;
327
Ying Xue247f0f32014-02-18 16:06:46 +0800328 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400329 if (ret < 0) {
330 sock_release(*newsock);
331 return ret;
332 }
333 (*newsock)->ops = sock->ops;
334 return ret;
335}
336
337/**
Ying Xue247f0f32014-02-18 16:06:46 +0800338 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 * @sock: socket to destroy
340 *
341 * This routine cleans up any messages that are still queued on the socket.
342 * For DGRAM and RDM socket types, all queued messages are rejected.
343 * For SEQPACKET and STREAM socket types, the first message is rejected
344 * and any others are discarded. (If the first message on a STREAM socket
345 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900346 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 * NOTE: Rejected messages are not necessarily returned to the sender! They
348 * are returned or discarded according to the "destination droppable" setting
349 * specified for the message by the sender.
350 *
351 * Returns 0 on success, errno otherwise
352 */
Ying Xue247f0f32014-02-18 16:06:46 +0800353static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400356 struct tipc_sock *tsk;
357 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500359 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360
Allan Stephens0c3141e2008-04-15 00:22:02 -0700361 /*
362 * Exit if socket isn't fully initialized (occurs when a failed accept()
363 * releases a pre-allocated child socket that was never used)
364 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700365 if (sk == NULL)
366 return 0;
367
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400368 tsk = tipc_sk(sk);
369 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700370 lock_sock(sk);
371
372 /*
373 * Reject all unreceived messages, except on an active connection
374 * (which disconnects locally & sends a 'FIN+' to peer)
375 */
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400376 dnode = tipc_port_peernode(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700378 buf = __skb_dequeue(&sk->sk_receive_queue);
379 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 break;
Ying Xue40682432013-10-18 07:23:16 +0200381 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400382 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700383 else {
384 if ((sock->state == SS_CONNECTING) ||
385 (sock->state == SS_CONNECTED)) {
386 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -0400387 port->connected = 0;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400388 tipc_node_remove_conn(dnode, port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700389 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500390 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400391 tipc_link_xmit(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700392 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 }
394
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400395 tipc_sk_withdraw(port, 0, NULL);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400396 tipc_ref_discard(port->ref);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400397 k_cancel_timer(&port->timer);
398 if (port->connected) {
399 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
400 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
401 tipc_port_peerport(port),
402 port->ref, TIPC_ERR_NO_PORT);
403 if (buf)
404 tipc_link_xmit(buf, dnode, port->ref);
405 tipc_node_remove_conn(dnode, port->ref);
406 }
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400407 k_term_timer(&port->timer);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408
Allan Stephens0c3141e2008-04-15 00:22:02 -0700409 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100410 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411
Allan Stephens0c3141e2008-04-15 00:22:02 -0700412 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700413 sock->state = SS_DISCONNECTING;
414 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700416 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200418 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419}
420
421/**
Ying Xue247f0f32014-02-18 16:06:46 +0800422 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 * @sock: socket structure
424 * @uaddr: socket address describing name(s) and desired operation
425 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900426 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 * Name and name sequence binding is indicated using a positive scope value;
428 * a negative scope value unbinds the specified name. Specifying no name
429 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900430 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700432 *
433 * NOTE: This routine doesn't need to take the socket lock since it doesn't
434 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435 */
Ying Xue247f0f32014-02-18 16:06:46 +0800436static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
437 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438{
Ying Xue84602762013-12-27 10:18:28 +0800439 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400441 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800442 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443
Ying Xue84602762013-12-27 10:18:28 +0800444 lock_sock(sk);
445 if (unlikely(!uaddr_len)) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400446 res = tipc_sk_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800447 goto exit;
448 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900449
Ying Xue84602762013-12-27 10:18:28 +0800450 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
451 res = -EINVAL;
452 goto exit;
453 }
454 if (addr->family != AF_TIPC) {
455 res = -EAFNOSUPPORT;
456 goto exit;
457 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 if (addr->addrtype == TIPC_ADDR_NAME)
460 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800461 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
462 res = -EAFNOSUPPORT;
463 goto exit;
464 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900465
Ying Xue13a2e892013-06-17 10:54:40 -0400466 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400467 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800468 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
469 res = -EACCES;
470 goto exit;
471 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400472
Ying Xue84602762013-12-27 10:18:28 +0800473 res = (addr->scope > 0) ?
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400474 tipc_sk_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
475 tipc_sk_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800476exit:
477 release_sock(sk);
478 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479}
480
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900481/**
Ying Xue247f0f32014-02-18 16:06:46 +0800482 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 * @sock: socket structure
484 * @uaddr: area for returned socket address
485 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700486 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900487 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700489 *
Allan Stephens2da59912008-07-14 22:43:32 -0700490 * NOTE: This routine doesn't need to take the socket lock since it only
491 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000492 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 */
Ying Xue247f0f32014-02-18 16:06:46 +0800494static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
495 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400498 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000500 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700501 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700502 if ((sock->state != SS_CONNECTED) &&
503 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
504 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400505 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
506 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700507 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400508 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000509 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700510 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511
512 *uaddr_len = sizeof(*addr);
513 addr->addrtype = TIPC_ADDR_ID;
514 addr->family = AF_TIPC;
515 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 addr->addr.name.domain = 0;
517
Allan Stephens0c3141e2008-04-15 00:22:02 -0700518 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519}
520
521/**
Ying Xue247f0f32014-02-18 16:06:46 +0800522 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 * @file: file structure associated with the socket
524 * @sock: socket for which to calculate the poll bits
525 * @wait: ???
526 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700527 * Returns pollmask value
528 *
529 * COMMENTARY:
530 * It appears that the usual socket locking mechanisms are not useful here
531 * since the pollmask info is potentially out-of-date the moment this routine
532 * exits. TCP and other protocols seem to rely on higher level poll routines
533 * to handle any preventable race conditions, so TIPC will do the same ...
534 *
535 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700536 *
Allan Stephensf662c072010-08-17 11:00:06 +0000537 * socket state flags set
538 * ------------ ---------
539 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200540 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000541 *
542 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
543 * no write flags
544 *
545 * connected POLLIN/POLLRDNORM if data in rx queue
546 * POLLOUT if port is not congested
547 *
548 * disconnecting POLLIN/POLLRDNORM/POLLHUP
549 * no write flags
550 *
551 * listening POLLIN if SYN in rx queue
552 * no write flags
553 *
554 * ready POLLIN/POLLRDNORM if data in rx queue
555 * [connectionless] POLLOUT (since port cannot be congested)
556 *
557 * IMPORTANT: The fact that a read or write operation is indicated does NOT
558 * imply that the operation will succeed, merely that it should be performed
559 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 */
Ying Xue247f0f32014-02-18 16:06:46 +0800561static unsigned int tipc_poll(struct file *file, struct socket *sock,
562 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563{
Allan Stephens9b674e82008-03-26 16:48:21 -0700564 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400565 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000566 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700567
Ying Xuef288bef2012-08-21 11:16:57 +0800568 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700569
Allan Stephensf662c072010-08-17 11:00:06 +0000570 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200571 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500572 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200573 mask |= POLLOUT;
574 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000575 case SS_READY:
576 case SS_CONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500577 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000578 mask |= POLLOUT;
579 /* fall thru' */
580 case SS_CONNECTING:
581 case SS_LISTENING:
582 if (!skb_queue_empty(&sk->sk_receive_queue))
583 mask |= (POLLIN | POLLRDNORM);
584 break;
585 case SS_DISCONNECTING:
586 mask = (POLLIN | POLLRDNORM | POLLHUP);
587 break;
588 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700589
590 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591}
592
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400593/**
594 * tipc_sendmcast - send multicast message
595 * @sock: socket structure
596 * @seq: destination address
597 * @iov: message data to send
598 * @dsz: total length of message data
599 * @timeo: timeout to wait for wakeup
600 *
601 * Called from function tipc_sendmsg(), which has done all sanity checks
602 * Returns the number of bytes sent on success, or errno
603 */
604static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
605 struct iovec *iov, size_t dsz, long timeo)
606{
607 struct sock *sk = sock->sk;
608 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
609 struct sk_buff *buf;
610 uint mtu;
611 int rc;
612
613 msg_set_type(mhdr, TIPC_MCAST_MSG);
614 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
615 msg_set_destport(mhdr, 0);
616 msg_set_destnode(mhdr, 0);
617 msg_set_nametype(mhdr, seq->type);
618 msg_set_namelower(mhdr, seq->lower);
619 msg_set_nameupper(mhdr, seq->upper);
620 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
621
622new_mtu:
623 mtu = tipc_bclink_get_mtu();
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400624 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400625 if (unlikely(rc < 0))
626 return rc;
627
628 do {
629 rc = tipc_bclink_xmit(buf);
630 if (likely(rc >= 0)) {
631 rc = dsz;
632 break;
633 }
634 if (rc == -EMSGSIZE)
635 goto new_mtu;
636 if (rc != -ELINKCONG)
637 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400638 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400639 rc = tipc_wait_for_sndmsg(sock, &timeo);
640 if (rc)
641 kfree_skb_list(buf);
642 } while (!rc);
643 return rc;
644}
645
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400646/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
647 */
648void tipc_sk_mcast_rcv(struct sk_buff *buf)
649{
650 struct tipc_msg *msg = buf_msg(buf);
651 struct tipc_port_list dports = {0, NULL, };
652 struct tipc_port_list *item;
653 struct sk_buff *b;
654 uint i, last, dst = 0;
655 u32 scope = TIPC_CLUSTER_SCOPE;
656
657 if (in_own_node(msg_orignode(msg)))
658 scope = TIPC_NODE_SCOPE;
659
660 /* Create destination port list: */
661 tipc_nametbl_mc_translate(msg_nametype(msg),
662 msg_namelower(msg),
663 msg_nameupper(msg),
664 scope,
665 &dports);
666 last = dports.count;
667 if (!last) {
668 kfree_skb(buf);
669 return;
670 }
671
672 for (item = &dports; item; item = item->next) {
673 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
674 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
675 if (!b) {
676 pr_warn("Failed do clone mcast rcv buffer\n");
677 continue;
678 }
679 msg_set_destport(msg, item->ports[i]);
680 tipc_sk_rcv(b);
681 }
682 }
683 tipc_port_list_free(&dports);
684}
685
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900686/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500687 * tipc_sk_proto_rcv - receive a connection mng protocol message
688 * @tsk: receiving socket
689 * @dnode: node to send response message to, if any
690 * @buf: buffer containing protocol message
691 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
692 * (CONN_PROBE_REPLY) message should be forwarded.
693 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800694static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
695 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500696{
697 struct tipc_msg *msg = buf_msg(buf);
698 struct tipc_port *port = &tsk->port;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500699 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500700
701 /* Ignore if connection cannot be validated: */
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400702 if (!tipc_sk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500703 goto exit;
704
705 port->probing_state = TIPC_CONN_OK;
706
707 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500708 conn_cong = tipc_sk_conn_cong(tsk);
709 tsk->sent_unacked -= msg_msgcnt(msg);
710 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400711 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500712 } else if (msg_type(msg) == CONN_PROBE) {
713 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
714 return TIPC_OK;
715 msg_set_type(msg, CONN_PROBE_REPLY);
716 return TIPC_FWD_MSG;
717 }
718 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
719exit:
720 kfree_skb(buf);
721 return TIPC_OK;
722}
723
724/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725 * dest_name_check - verify user is permitted to send to specified port name
726 * @dest: destination address
727 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900728 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 * Prevents restricted configuration commands from being issued by
730 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900731 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 * Returns 0 if permission is granted, otherwise errno
733 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800734static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735{
736 struct tipc_cfg_msg_hdr hdr;
737
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500738 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
739 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900740 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
741 return 0;
742 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
743 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900744 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
745 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746
Allan Stephens3f8dd942011-01-18 13:09:29 -0500747 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
748 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900749 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700751 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900753
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 return 0;
755}
756
Ying Xue3f405042014-01-17 09:50:05 +0800757static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
758{
759 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400760 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800761 DEFINE_WAIT(wait);
762 int done;
763
764 do {
765 int err = sock_error(sk);
766 if (err)
767 return err;
768 if (sock->state == SS_DISCONNECTING)
769 return -EPIPE;
770 if (!*timeo_p)
771 return -EAGAIN;
772 if (signal_pending(current))
773 return sock_intr_errno(*timeo_p);
774
775 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500776 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800777 finish_wait(sk_sleep(sk), &wait);
778 } while (!done);
779 return 0;
780}
781
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500782/**
Ying Xue247f0f32014-02-18 16:06:46 +0800783 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700784 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 * @sock: socket structure
786 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500787 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900788 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900790 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
792 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900793 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 * Returns the number of bytes sent on success, or errno otherwise
795 */
Ying Xue247f0f32014-02-18 16:06:46 +0800796static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500797 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500799 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700800 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400801 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400802 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500803 struct tipc_msg *mhdr = &port->phdr;
804 struct iovec *iov = m->msg_iov;
805 u32 dnode, dport;
806 struct sk_buff *buf;
807 struct tipc_name_seq *seq = &dest->addr.nameseq;
808 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800809 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500810 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100811
812 if (unlikely(!dest))
813 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500814
Allan Stephens51f9cc12006-06-25 23:49:06 -0700815 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
816 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500818
819 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400820 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821
Allan Stephens0c3141e2008-04-15 00:22:02 -0700822 if (iocb)
823 lock_sock(sk);
824
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500825 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700826 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500827 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700828 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700829 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700830 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500831 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700832 goto exit;
833 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400834 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500835 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700836 goto exit;
837 }
838 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400839 tsk->port.conn_type = dest->addr.name.name.type;
840 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700841 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500843 rc = dest_name_check(dest, m);
844 if (rc)
845 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846
Ying Xue3f405042014-01-17 09:50:05 +0800847 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500848
849 if (dest->addrtype == TIPC_ADDR_MCAST) {
850 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
851 goto exit;
852 } else if (dest->addrtype == TIPC_ADDR_NAME) {
853 u32 type = dest->addr.name.name.type;
854 u32 inst = dest->addr.name.name.instance;
855 u32 domain = dest->addr.name.domain;
856
857 dnode = domain;
858 msg_set_type(mhdr, TIPC_NAMED_MSG);
859 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
860 msg_set_nametype(mhdr, type);
861 msg_set_nameinst(mhdr, inst);
862 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
863 dport = tipc_nametbl_translate(type, inst, &dnode);
864 msg_set_destnode(mhdr, dnode);
865 msg_set_destport(mhdr, dport);
866 if (unlikely(!dport && !dnode)) {
867 rc = -EHOSTUNREACH;
868 goto exit;
869 }
870 } else if (dest->addrtype == TIPC_ADDR_ID) {
871 dnode = dest->addr.id.node;
872 msg_set_type(mhdr, TIPC_DIRECT_MSG);
873 msg_set_lookup_scope(mhdr, 0);
874 msg_set_destnode(mhdr, dnode);
875 msg_set_destport(mhdr, dest->addr.id.ref);
876 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
877 }
878
879new_mtu:
880 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400881 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500882 if (rc < 0)
883 goto exit;
884
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900885 do {
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400886 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400887 rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500888 if (likely(rc >= 0)) {
889 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700890 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500891 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700892 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900893 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500894 if (rc == -EMSGSIZE)
895 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500896 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700897 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400898 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500899 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400900 if (rc)
901 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500902 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700903exit:
904 if (iocb)
905 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500906
907 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908}
909
Ying Xue391a6dd2014-01-17 09:50:06 +0800910static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
911{
912 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400913 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800914 DEFINE_WAIT(wait);
915 int done;
916
917 do {
918 int err = sock_error(sk);
919 if (err)
920 return err;
921 if (sock->state == SS_DISCONNECTING)
922 return -EPIPE;
923 else if (sock->state != SS_CONNECTED)
924 return -ENOTCONN;
925 if (!*timeo_p)
926 return -EAGAIN;
927 if (signal_pending(current))
928 return sock_intr_errno(*timeo_p);
929
930 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
931 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500932 (!tsk->link_cong &&
933 !tipc_sk_conn_cong(tsk)) ||
934 !tsk->port.connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800935 finish_wait(sk_sleep(sk), &wait);
936 } while (!done);
937 return 0;
938}
939
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900940/**
Ying Xue247f0f32014-02-18 16:06:46 +0800941 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 * @iocb: (unused)
943 * @sock: socket structure
944 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500945 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900946 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900948 *
949 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700950 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951 */
Ying Xue247f0f32014-02-18 16:06:46 +0800952static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500953 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700955 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400956 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500957 struct tipc_port *port = &tsk->port;
958 struct tipc_msg *mhdr = &port->phdr;
959 struct sk_buff *buf;
960 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
961 u32 ref = port->ref;
962 int rc = -EINVAL;
963 long timeo;
964 u32 dnode;
965 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900966
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500967 /* Handle implied connection establishment */
968 if (unlikely(dest)) {
969 rc = tipc_sendmsg(iocb, sock, m, dsz);
970 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -0500971 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500972 return rc;
973 }
974 if (dsz > (uint)INT_MAX)
975 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700976
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500977 if (iocb)
978 lock_sock(sk);
979
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900980 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500981 if (sock->state == SS_DISCONNECTING)
982 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +0800983 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500984 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800985 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900986 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500988 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
989 dnode = tipc_port_peernode(port);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500990
991next:
992 mtu = port->max_pkt;
993 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400994 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500995 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700996 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500997 do {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500998 if (likely(!tipc_sk_conn_cong(tsk))) {
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400999 rc = tipc_link_xmit(buf, dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001000 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001001 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001002 sent += send;
1003 if (sent == dsz)
1004 break;
1005 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001006 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001007 if (rc == -EMSGSIZE) {
1008 port->max_pkt = tipc_node_get_mtu(dnode, ref);
1009 goto next;
1010 }
1011 if (rc != -ELINKCONG)
1012 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001013 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001015 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001016 if (rc)
1017 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001018 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001019exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001020 if (iocb)
1021 release_sock(sk);
1022 return sent ? sent : rc;
1023}
1024
1025/**
1026 * tipc_send_packet - send a connection-oriented message
1027 * @iocb: if NULL, indicates that socket lock is already held
1028 * @sock: socket structure
1029 * @m: message to send
1030 * @dsz: length of data to be transmitted
1031 *
1032 * Used for SOCK_SEQPACKET messages.
1033 *
1034 * Returns the number of bytes sent on success, or errno otherwise
1035 */
1036static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1037 struct msghdr *m, size_t dsz)
1038{
1039 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1040 return -EMSGSIZE;
1041
1042 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043}
1044
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001045/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001047static void tipc_sk_finish_conn(struct tipc_port *port, u32 peer_port,
1048 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049{
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001050 struct tipc_msg *msg = &port->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001052 msg_set_destnode(msg, peer_node);
1053 msg_set_destport(msg, peer_port);
1054 msg_set_type(msg, TIPC_CONN_MSG);
1055 msg_set_lookup_scope(msg, 0);
1056 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001057
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001058 port->probing_interval = CONN_PROBING_INTERVAL;
1059 port->probing_state = TIPC_CONN_OK;
1060 port->connected = 1;
1061 k_start_timer(&port->timer, port->probing_interval);
1062 tipc_node_add_conn(peer_node, port->ref, peer_port);
1063 port->max_pkt = tipc_node_get_mtu(peer_node, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064}
1065
1066/**
1067 * set_orig_addr - capture sender's address for received message
1068 * @m: descriptor for message info
1069 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001070 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 * Note: Address is not captured if not requested by receiver.
1072 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001073static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001075 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001077 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 addr->family = AF_TIPC;
1079 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001080 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 addr->addr.id.ref = msg_origport(msg);
1082 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001083 addr->addr.name.domain = 0; /* could leave uninitialized */
1084 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085 m->msg_namelen = sizeof(struct sockaddr_tipc);
1086 }
1087}
1088
1089/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001090 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091 * @m: descriptor for message info
1092 * @msg: received message header
1093 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001094 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001096 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097 * Returns 0 if successful, otherwise errno
1098 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001099static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001100 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101{
1102 u32 anc_data[3];
1103 u32 err;
1104 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001105 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106 int res;
1107
1108 if (likely(m->msg_controllen == 0))
1109 return 0;
1110
1111 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112 err = msg ? msg_errcode(msg) : 0;
1113 if (unlikely(err)) {
1114 anc_data[0] = err;
1115 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001116 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1117 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001119 if (anc_data[1]) {
1120 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1121 msg_data(msg));
1122 if (res)
1123 return res;
1124 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125 }
1126
1127 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1129 switch (dest_type) {
1130 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001131 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 anc_data[0] = msg_nametype(msg);
1133 anc_data[1] = msg_namelower(msg);
1134 anc_data[2] = msg_namelower(msg);
1135 break;
1136 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001137 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 anc_data[0] = msg_nametype(msg);
1139 anc_data[1] = msg_namelower(msg);
1140 anc_data[2] = msg_nameupper(msg);
1141 break;
1142 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001143 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 anc_data[0] = tport->conn_type;
1145 anc_data[1] = tport->conn_instance;
1146 anc_data[2] = tport->conn_instance;
1147 break;
1148 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001149 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 }
Allan Stephens2db99832010-12-31 18:59:33 +00001151 if (has_name) {
1152 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1153 if (res)
1154 return res;
1155 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156
1157 return 0;
1158}
1159
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001160static void tipc_sk_send_ack(struct tipc_port *port, uint ack)
1161{
1162 struct sk_buff *buf = NULL;
1163 struct tipc_msg *msg;
1164 u32 peer_port = tipc_port_peerport(port);
1165 u32 dnode = tipc_port_peernode(port);
1166
1167 if (!port->connected)
1168 return;
1169 buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
1170 tipc_own_addr, peer_port, port->ref, TIPC_OK);
1171 if (!buf)
1172 return;
1173 msg = buf_msg(buf);
1174 msg_set_msgcnt(msg, ack);
1175 tipc_link_xmit(buf, dnode, msg_link_selector(msg));
1176}
1177
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001178static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001179{
1180 struct sock *sk = sock->sk;
1181 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001182 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001183 int err;
1184
1185 for (;;) {
1186 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001187 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001188 if (sock->state == SS_DISCONNECTING) {
1189 err = -ENOTCONN;
1190 break;
1191 }
1192 release_sock(sk);
1193 timeo = schedule_timeout(timeo);
1194 lock_sock(sk);
1195 }
1196 err = 0;
1197 if (!skb_queue_empty(&sk->sk_receive_queue))
1198 break;
1199 err = sock_intr_errno(timeo);
1200 if (signal_pending(current))
1201 break;
1202 err = -EAGAIN;
1203 if (!timeo)
1204 break;
1205 }
1206 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001207 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001208 return err;
1209}
1210
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001211/**
Ying Xue247f0f32014-02-18 16:06:46 +08001212 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 * @iocb: (unused)
1214 * @m: descriptor for message info
1215 * @buf_len: total size of user buffer area
1216 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001217 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1219 * If the complete message doesn't fit in user area, truncate it.
1220 *
1221 * Returns size of returned message data, errno otherwise
1222 */
Ying Xue247f0f32014-02-18 16:06:46 +08001223static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1224 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001226 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001227 struct tipc_sock *tsk = tipc_sk(sk);
1228 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 struct sk_buff *buf;
1230 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001231 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 unsigned int sz;
1233 u32 err;
1234 int res;
1235
Allan Stephens0c3141e2008-04-15 00:22:02 -07001236 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 if (unlikely(!buf_len))
1238 return -EINVAL;
1239
Allan Stephens0c3141e2008-04-15 00:22:02 -07001240 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241
Allan Stephens0c3141e2008-04-15 00:22:02 -07001242 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243 res = -ENOTCONN;
1244 goto exit;
1245 }
1246
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001247 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001248restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249
Allan Stephens0c3141e2008-04-15 00:22:02 -07001250 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001251 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001252 if (res)
1253 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001254
1255 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001256 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 msg = buf_msg(buf);
1258 sz = msg_data_sz(msg);
1259 err = msg_errcode(msg);
1260
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001263 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 goto restart;
1265 }
1266
1267 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 set_orig_addr(m, msg);
1269
1270 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001271 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001272 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001273 goto exit;
1274
1275 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 if (!err) {
1277 if (unlikely(buf_len < sz)) {
1278 sz = buf_len;
1279 m->msg_flags |= MSG_TRUNC;
1280 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001281 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1282 m->msg_iov, sz);
1283 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001284 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 res = sz;
1286 } else {
1287 if ((sock->state == SS_READY) ||
1288 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1289 res = 0;
1290 else
1291 res = -ECONNRESET;
1292 }
1293
1294 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001296 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001297 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001298 tipc_sk_send_ack(port, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001299 tsk->rcv_unacked = 0;
1300 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001301 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001302 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001304 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 return res;
1306}
1307
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001308/**
Ying Xue247f0f32014-02-18 16:06:46 +08001309 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 * @iocb: (unused)
1311 * @m: descriptor for message info
1312 * @buf_len: total size of user buffer area
1313 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001314 *
1315 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 * will optionally wait for more; never truncates data.
1317 *
1318 * Returns size of returned message data, errno otherwise
1319 */
Ying Xue247f0f32014-02-18 16:06:46 +08001320static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1321 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001323 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001324 struct tipc_sock *tsk = tipc_sk(sk);
1325 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 struct sk_buff *buf;
1327 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001328 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001330 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001333 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334
1335 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 if (unlikely(!buf_len))
1337 return -EINVAL;
1338
Allan Stephens0c3141e2008-04-15 00:22:02 -07001339 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001341 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 res = -ENOTCONN;
1343 goto exit;
1344 }
1345
Florian Westphal3720d402010-08-17 11:00:04 +00001346 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001347 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001348
Allan Stephens0c3141e2008-04-15 00:22:02 -07001349restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001350 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001351 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001352 if (res)
1353 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001354
1355 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001356 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 msg = buf_msg(buf);
1358 sz = msg_data_sz(msg);
1359 err = msg_errcode(msg);
1360
1361 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001363 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 goto restart;
1365 }
1366
1367 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368 if (sz_copied == 0) {
1369 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001370 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001371 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 goto exit;
1373 }
1374
1375 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001377 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378
Allan Stephens0232fd02011-02-21 09:45:40 -05001379 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 needed = (buf_len - sz_copied);
1381 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001382
1383 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1384 m->msg_iov, sz_to_copy);
1385 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001387
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 sz_copied += sz_to_copy;
1389
1390 if (sz_to_copy < sz) {
1391 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001392 TIPC_SKB_CB(buf)->handle =
1393 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394 goto exit;
1395 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 } else {
1397 if (sz_copied != 0)
1398 goto exit; /* can't add error msg to valid data */
1399
1400 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1401 res = 0;
1402 else
1403 res = -ECONNRESET;
1404 }
1405
1406 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001408 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001409 tipc_sk_send_ack(port, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001410 tsk->rcv_unacked = 0;
1411 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001412 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001413 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001414
1415 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001416 if ((sz_copied < buf_len) && /* didn't get all requested data */
1417 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001418 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001419 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1420 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 goto restart;
1422
1423exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001424 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001425 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426}
1427
1428/**
Ying Xuef288bef2012-08-21 11:16:57 +08001429 * tipc_write_space - wake up thread if port congestion is released
1430 * @sk: socket
1431 */
1432static void tipc_write_space(struct sock *sk)
1433{
1434 struct socket_wq *wq;
1435
1436 rcu_read_lock();
1437 wq = rcu_dereference(sk->sk_wq);
1438 if (wq_has_sleeper(wq))
1439 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1440 POLLWRNORM | POLLWRBAND);
1441 rcu_read_unlock();
1442}
1443
1444/**
1445 * tipc_data_ready - wake up threads to indicate messages have been received
1446 * @sk: socket
1447 * @len: the length of messages
1448 */
David S. Miller676d2362014-04-11 16:15:36 -04001449static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001450{
1451 struct socket_wq *wq;
1452
1453 rcu_read_lock();
1454 wq = rcu_dereference(sk->sk_wq);
1455 if (wq_has_sleeper(wq))
1456 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1457 POLLRDNORM | POLLRDBAND);
1458 rcu_read_unlock();
1459}
1460
1461/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001462 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001463 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001464 * @msg: message
1465 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001466 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001467 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001468static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001469{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001470 struct sock *sk = &tsk->sk;
1471 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001472 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001473 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001474
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001475 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001476
1477 if (msg_mcast(msg))
1478 return retval;
1479
1480 switch ((int)sock->state) {
1481 case SS_CONNECTED:
1482 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04001483 if (tipc_sk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001484 if (unlikely(msg_errcode(msg))) {
1485 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001486 port->connected = 0;
1487 /* let timer expire on it's own */
1488 tipc_node_remove_conn(tipc_port_peernode(port),
1489 port->ref);
Ying Xue7e6c1312012-11-29 18:39:14 -05001490 }
1491 retval = TIPC_OK;
1492 }
1493 break;
1494 case SS_CONNECTING:
1495 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001496
1497 if (unlikely(!msg_connected(msg)))
1498 break;
1499
Ying Xue584d24b2012-11-29 18:51:19 -05001500 if (unlikely(msg_errcode(msg))) {
1501 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001502 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001503 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001504 break;
1505 }
1506
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001507 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001508 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001509 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001510 retval = TIPC_OK;
1511 break;
1512 }
1513
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001514 tipc_sk_finish_conn(port, msg_origport(msg), msg_orignode(msg));
1515 msg_set_importance(&port->phdr, msg_importance(msg));
1516 sock->state = SS_CONNECTED;
1517
Ying Xue584d24b2012-11-29 18:51:19 -05001518 /* If an incoming message is an 'ACK-', it should be
1519 * discarded here because it doesn't contain useful
1520 * data. In addition, we should try to wake up
1521 * connect() routine if sleeping.
1522 */
1523 if (msg_data_sz(msg) == 0) {
1524 kfree_skb(*buf);
1525 *buf = NULL;
1526 if (waitqueue_active(sk_sleep(sk)))
1527 wake_up_interruptible(sk_sleep(sk));
1528 }
1529 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001530 break;
1531 case SS_LISTENING:
1532 case SS_UNCONNECTED:
1533 /* Accept only SYN message */
1534 if (!msg_connected(msg) && !(msg_errcode(msg)))
1535 retval = TIPC_OK;
1536 break;
1537 case SS_DISCONNECTING:
1538 break;
1539 default:
1540 pr_err("Unknown socket state %u\n", sock->state);
1541 }
1542 return retval;
1543}
1544
1545/**
Ying Xueaba79f32013-01-20 23:30:09 +01001546 * rcvbuf_limit - get proper overload limit of socket receive queue
1547 * @sk: socket
1548 * @buf: message
1549 *
1550 * For all connection oriented messages, irrespective of importance,
1551 * the default overload value (i.e. 67MB) is set as limit.
1552 *
1553 * For all connectionless messages, by default new queue limits are
1554 * as belows:
1555 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001556 * TIPC_LOW_IMPORTANCE (4 MB)
1557 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1558 * TIPC_HIGH_IMPORTANCE (16 MB)
1559 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001560 *
1561 * Returns overload limit according to corresponding message importance
1562 */
1563static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1564{
1565 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001566
1567 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001568 return sysctl_tipc_rmem[2];
1569
1570 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1571 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001572}
1573
1574/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001575 * filter_rcv - validate incoming message
1576 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001578 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001579 * Enqueues message on receive queue if acceptable; optionally handles
1580 * disconnect indication for a connected socket.
1581 *
1582 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001583 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001584 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001585 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001587static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001588{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001589 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001590 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001591 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001592 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001593 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001594 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001596 if (unlikely(msg_user(msg) == CONN_MANAGER))
1597 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001598
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001599 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1600 kfree_skb(buf);
1601 tsk->link_cong = 0;
1602 sk->sk_write_space(sk);
1603 return TIPC_OK;
1604 }
1605
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001607 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001608 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001609
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001611 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001612 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001614 rc = filter_connect(tsk, &buf);
1615 if (rc != TIPC_OK || buf == NULL)
1616 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001617 }
1618
1619 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001620 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001621 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622
Ying Xueaba79f32013-01-20 23:30:09 +01001623 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001624 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001625 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001626 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001627
David S. Miller676d2362014-04-11 16:15:36 -04001628 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001629 return TIPC_OK;
1630}
1631
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001632/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001633 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001634 * @sk: socket
1635 * @buf: message
1636 *
1637 * Caller must hold socket lock, but not port lock.
1638 *
1639 * Returns 0
1640 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001641static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001642{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001643 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001644 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001645 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001646 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001647
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001648 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001649
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001650 if (likely(!rc)) {
1651 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1652 atomic_add(truesize, &tsk->dupl_rcvcnt);
1653 return 0;
1654 }
1655
1656 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1657 return 0;
1658
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001659 tipc_link_xmit(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001660
Allan Stephens0c3141e2008-04-15 00:22:02 -07001661 return 0;
1662}
1663
1664/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001665 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001666 * @buf: buffer containing arriving message
1667 * Consumes buffer
1668 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001669 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001670int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001671{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001672 struct tipc_sock *tsk;
1673 struct tipc_port *port;
1674 struct sock *sk;
1675 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001676 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001677 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001678 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001679
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001680 /* Validate destination and message */
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001681 tsk = tipc_sk_get(dport);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001682 if (unlikely(!tsk)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001683 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001684 goto exit;
1685 }
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001686 port = &tsk->port;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001687 sk = &tsk->sk;
1688
1689 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001690 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001691
Allan Stephens0c3141e2008-04-15 00:22:02 -07001692 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001693 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001694 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001695 if (sk->sk_backlog.len == 0)
1696 atomic_set(&tsk->dupl_rcvcnt, 0);
1697 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1698 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001699 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001700 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001701 bh_unlock_sock(sk);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001702 tipc_sk_put(tsk);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001703 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001704 return 0;
1705exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001706 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001707 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001708
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001709 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001710 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001711}
1712
Ying Xue78eb3a52014-01-17 09:50:03 +08001713static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1714{
1715 struct sock *sk = sock->sk;
1716 DEFINE_WAIT(wait);
1717 int done;
1718
1719 do {
1720 int err = sock_error(sk);
1721 if (err)
1722 return err;
1723 if (!*timeo_p)
1724 return -ETIMEDOUT;
1725 if (signal_pending(current))
1726 return sock_intr_errno(*timeo_p);
1727
1728 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1729 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1730 finish_wait(sk_sleep(sk), &wait);
1731 } while (!done);
1732 return 0;
1733}
1734
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735/**
Ying Xue247f0f32014-02-18 16:06:46 +08001736 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 * @sock: socket structure
1738 * @dest: socket address for destination port
1739 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001740 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 *
1742 * Returns 0 on success, errno otherwise
1743 */
Ying Xue247f0f32014-02-18 16:06:46 +08001744static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1745 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001747 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001748 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1749 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001750 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1751 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001752 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753
Allan Stephens0c3141e2008-04-15 00:22:02 -07001754 lock_sock(sk);
1755
Allan Stephensb89741a2008-04-15 00:20:37 -07001756 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001757 if (sock->state == SS_READY) {
1758 res = -EOPNOTSUPP;
1759 goto exit;
1760 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001761
Allan Stephensb89741a2008-04-15 00:20:37 -07001762 /*
1763 * Reject connection attempt using multicast address
1764 *
1765 * Note: send_msg() validates the rest of the address fields,
1766 * so there's no need to do it here
1767 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001768 if (dst->addrtype == TIPC_ADDR_MCAST) {
1769 res = -EINVAL;
1770 goto exit;
1771 }
1772
Ying Xue78eb3a52014-01-17 09:50:03 +08001773 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001774 switch (sock->state) {
1775 case SS_UNCONNECTED:
1776 /* Send a 'SYN-' to destination */
1777 m.msg_name = dest;
1778 m.msg_namelen = destlen;
1779
1780 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1781 * indicate send_msg() is never blocked.
1782 */
1783 if (!timeout)
1784 m.msg_flags = MSG_DONTWAIT;
1785
Ying Xue247f0f32014-02-18 16:06:46 +08001786 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001787 if ((res < 0) && (res != -EWOULDBLOCK))
1788 goto exit;
1789
1790 /* Just entered SS_CONNECTING state; the only
1791 * difference is that return value in non-blocking
1792 * case is EINPROGRESS, rather than EALREADY.
1793 */
1794 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001795 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001796 if (previous == SS_CONNECTING)
1797 res = -EALREADY;
1798 if (!timeout)
1799 goto exit;
1800 timeout = msecs_to_jiffies(timeout);
1801 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1802 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001803 break;
1804 case SS_CONNECTED:
1805 res = -EISCONN;
1806 break;
1807 default:
1808 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001809 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001810 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001811exit:
1812 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001813 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814}
1815
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001816/**
Ying Xue247f0f32014-02-18 16:06:46 +08001817 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001818 * @sock: socket structure
1819 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001820 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821 * Returns 0 on success, errno otherwise
1822 */
Ying Xue247f0f32014-02-18 16:06:46 +08001823static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001824{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001825 struct sock *sk = sock->sk;
1826 int res;
1827
1828 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829
Ying Xue245f3d32011-07-06 06:01:13 -04001830 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001831 res = -EINVAL;
1832 else {
1833 sock->state = SS_LISTENING;
1834 res = 0;
1835 }
1836
1837 release_sock(sk);
1838 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839}
1840
Ying Xue6398e232014-01-17 09:50:04 +08001841static int tipc_wait_for_accept(struct socket *sock, long timeo)
1842{
1843 struct sock *sk = sock->sk;
1844 DEFINE_WAIT(wait);
1845 int err;
1846
1847 /* True wake-one mechanism for incoming connections: only
1848 * one process gets woken up, not the 'whole herd'.
1849 * Since we do not 'race & poll' for established sockets
1850 * anymore, the common case will execute the loop only once.
1851 */
1852 for (;;) {
1853 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1854 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001855 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001856 release_sock(sk);
1857 timeo = schedule_timeout(timeo);
1858 lock_sock(sk);
1859 }
1860 err = 0;
1861 if (!skb_queue_empty(&sk->sk_receive_queue))
1862 break;
1863 err = -EINVAL;
1864 if (sock->state != SS_LISTENING)
1865 break;
1866 err = sock_intr_errno(timeo);
1867 if (signal_pending(current))
1868 break;
1869 err = -EAGAIN;
1870 if (!timeo)
1871 break;
1872 }
1873 finish_wait(sk_sleep(sk), &wait);
1874 return err;
1875}
1876
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001877/**
Ying Xue247f0f32014-02-18 16:06:46 +08001878 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879 * @sock: listening socket
1880 * @newsock: new socket that is to be connected
1881 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001882 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883 * Returns 0 on success, errno otherwise
1884 */
Ying Xue247f0f32014-02-18 16:06:46 +08001885static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001887 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001888 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001889 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001890 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001891 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001892 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893
Allan Stephens0c3141e2008-04-15 00:22:02 -07001894 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001895
Allan Stephens0c3141e2008-04-15 00:22:02 -07001896 if (sock->state != SS_LISTENING) {
1897 res = -EINVAL;
1898 goto exit;
1899 }
Ying Xue6398e232014-01-17 09:50:04 +08001900 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1901 res = tipc_wait_for_accept(sock, timeo);
1902 if (res)
1903 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001904
1905 buf = skb_peek(&sk->sk_receive_queue);
1906
Ying Xuec5fa7b32013-06-17 10:54:39 -04001907 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001908 if (res)
1909 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001910
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001911 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001912 new_port = &tipc_sk(new_sk)->port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001913 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001914
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001915 /* we lock on new_sk; but lockdep sees the lock on sk */
1916 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001917
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001918 /*
1919 * Reject any stray messages received by new socket
1920 * before the socket lock was taken (very, very unlikely)
1921 */
1922 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001923
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001924 /* Connect new socket to it's peer */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001925 tipc_sk_finish_conn(new_port, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001926 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001928 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001929 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001930 new_port->conn_type = msg_nametype(msg);
1931 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001932 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001933
1934 /*
1935 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1936 * Respond to 'SYN+' by queuing it on new socket.
1937 */
1938 if (!msg_data_sz(msg)) {
1939 struct msghdr m = {NULL,};
1940
1941 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001942 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001943 } else {
1944 __skb_dequeue(&sk->sk_receive_queue);
1945 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001946 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001947 }
1948 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001949exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001950 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951 return res;
1952}
1953
1954/**
Ying Xue247f0f32014-02-18 16:06:46 +08001955 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001956 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001957 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958 *
1959 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001960 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001961 * Returns 0 on success, errno otherwise
1962 */
Ying Xue247f0f32014-02-18 16:06:46 +08001963static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001964{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001965 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001966 struct tipc_sock *tsk = tipc_sk(sk);
1967 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968 struct sk_buff *buf;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001969 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970 int res;
1971
Allan Stephense247a8f2008-03-06 15:05:38 -08001972 if (how != SHUT_RDWR)
1973 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001974
Allan Stephens0c3141e2008-04-15 00:22:02 -07001975 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001976
1977 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001978 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979 case SS_CONNECTED:
1980
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001982 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001983 buf = __skb_dequeue(&sk->sk_receive_queue);
1984 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001985 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001986 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001987 goto restart;
1988 }
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001989 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
1990 tipc_link_xmit(buf, dnode, port->ref);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001991 tipc_node_remove_conn(dnode, port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001992 } else {
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001993 dnode = tipc_port_peernode(port);
1994 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
1995 TIPC_CONN_MSG, SHORT_H_SIZE,
1996 0, dnode, tipc_own_addr,
1997 tipc_port_peerport(port),
1998 port->ref, TIPC_CONN_SHUTDOWN);
1999 tipc_link_xmit(buf, dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002000 }
Jon Paul Maloydadebc02014-08-22 18:09:11 -04002001 port->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002002 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04002003 tipc_node_remove_conn(dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004 /* fall through */
2005
2006 case SS_DISCONNECTING:
2007
Ying Xue75031152012-10-29 09:38:15 -04002008 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002009 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002010
2011 /* Wake up anyone sleeping in poll */
2012 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002013 res = 0;
2014 break;
2015
2016 default:
2017 res = -ENOTCONN;
2018 }
2019
Allan Stephens0c3141e2008-04-15 00:22:02 -07002020 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002021 return res;
2022}
2023
Jon Paul Maloy57289012014-08-22 18:09:09 -04002024static void tipc_sk_timeout(unsigned long ref)
2025{
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002026 struct tipc_sock *tsk;
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04002027 struct tipc_port *port;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002028 struct sock *sk;
2029 struct sk_buff *buf = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002030 u32 peer_port, peer_node;
2031
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002032 tsk = tipc_sk_get(ref);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04002033 if (!tsk)
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002034 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002035 sk = &tsk->sk;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002036 port = &tsk->port;
2037
Jon Paul Maloy57289012014-08-22 18:09:09 -04002038 bh_lock_sock(sk);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002039 if (!port->connected) {
2040 bh_unlock_sock(sk);
2041 goto exit;
2042 }
Jon Paul Maloy57289012014-08-22 18:09:09 -04002043 peer_port = tipc_port_peerport(port);
2044 peer_node = tipc_port_peernode(port);
2045
2046 if (port->probing_state == TIPC_CONN_PROBING) {
2047 /* Previous probe not answered -> self abort */
2048 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2049 SHORT_H_SIZE, 0, tipc_own_addr,
2050 peer_node, ref, peer_port,
2051 TIPC_ERR_NO_PORT);
2052 } else {
2053 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2054 0, peer_node, tipc_own_addr,
2055 peer_port, ref, TIPC_OK);
2056 port->probing_state = TIPC_CONN_PROBING;
2057 k_start_timer(&port->timer, port->probing_interval);
2058 }
2059 bh_unlock_sock(sk);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002060 if (buf)
2061 tipc_link_xmit(buf, peer_node, ref);
2062exit:
2063 tipc_sk_put(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002064}
2065
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002066static int tipc_sk_publish(struct tipc_port *port, uint scope,
2067 struct tipc_name_seq const *seq)
2068{
2069 struct publication *publ;
2070 u32 key;
2071
2072 if (port->connected)
2073 return -EINVAL;
2074 key = port->ref + port->pub_count + 1;
2075 if (key == port->ref)
2076 return -EADDRINUSE;
2077
2078 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
2079 scope, port->ref, key);
2080 if (unlikely(!publ))
2081 return -EINVAL;
2082
2083 list_add(&publ->pport_list, &port->publications);
2084 port->pub_count++;
2085 port->published = 1;
2086 return 0;
2087}
2088
2089static int tipc_sk_withdraw(struct tipc_port *port, uint scope,
2090 struct tipc_name_seq const *seq)
2091{
2092 struct publication *publ;
2093 struct publication *safe;
2094 int rc = -EINVAL;
2095
2096 list_for_each_entry_safe(publ, safe, &port->publications, pport_list) {
2097 if (seq) {
2098 if (publ->scope != scope)
2099 continue;
2100 if (publ->type != seq->type)
2101 continue;
2102 if (publ->lower != seq->lower)
2103 continue;
2104 if (publ->upper != seq->upper)
2105 break;
2106 tipc_nametbl_withdraw(publ->type, publ->lower,
2107 publ->ref, publ->key);
2108 rc = 0;
2109 break;
2110 }
2111 tipc_nametbl_withdraw(publ->type, publ->lower,
2112 publ->ref, publ->key);
2113 rc = 0;
2114 }
2115 if (list_empty(&port->publications))
2116 port->published = 0;
2117 return rc;
2118}
2119
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002120static int tipc_sk_show(struct tipc_port *port, char *buf,
2121 int len, int full_id)
2122{
2123 struct publication *publ;
2124 int ret;
2125
2126 if (full_id)
2127 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2128 tipc_zone(tipc_own_addr),
2129 tipc_cluster(tipc_own_addr),
2130 tipc_node(tipc_own_addr), port->ref);
2131 else
2132 ret = tipc_snprintf(buf, len, "%-10u:", port->ref);
2133
2134 if (port->connected) {
2135 u32 dport = tipc_port_peerport(port);
2136 u32 destnode = tipc_port_peernode(port);
2137
2138 ret += tipc_snprintf(buf + ret, len - ret,
2139 " connected to <%u.%u.%u:%u>",
2140 tipc_zone(destnode),
2141 tipc_cluster(destnode),
2142 tipc_node(destnode), dport);
2143 if (port->conn_type != 0)
2144 ret += tipc_snprintf(buf + ret, len - ret,
2145 " via {%u,%u}", port->conn_type,
2146 port->conn_instance);
2147 } else if (port->published) {
2148 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
2149 list_for_each_entry(publ, &port->publications, pport_list) {
2150 if (publ->lower == publ->upper)
2151 ret += tipc_snprintf(buf + ret, len - ret,
2152 " {%u,%u}", publ->type,
2153 publ->lower);
2154 else
2155 ret += tipc_snprintf(buf + ret, len - ret,
2156 " {%u,%u,%u}", publ->type,
2157 publ->lower, publ->upper);
2158 }
2159 }
2160 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2161 return ret;
2162}
2163
2164struct sk_buff *tipc_sk_socks_show(void)
2165{
2166 struct sk_buff *buf;
2167 struct tlv_desc *rep_tlv;
2168 char *pb;
2169 int pb_len;
2170 struct tipc_sock *tsk;
2171 int str_len = 0;
2172 u32 ref = 0;
2173
2174 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2175 if (!buf)
2176 return NULL;
2177 rep_tlv = (struct tlv_desc *)buf->data;
2178 pb = TLV_DATA(rep_tlv);
2179 pb_len = ULTRA_STRING_MAX_LEN;
2180
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002181 tsk = tipc_sk_get_next(&ref);
2182 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2183 lock_sock(&tsk->sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002184 str_len += tipc_sk_show(&tsk->port, pb + str_len,
2185 pb_len - str_len, 0);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002186 release_sock(&tsk->sk);
2187 tipc_sk_put(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002188 }
2189 str_len += 1; /* for "\0" */
2190 skb_put(buf, TLV_SPACE(str_len));
2191 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2192
2193 return buf;
2194}
2195
2196/* tipc_sk_reinit: set non-zero address in all existing sockets
2197 * when we go from standalone to network mode.
2198 */
2199void tipc_sk_reinit(void)
2200{
2201 struct tipc_msg *msg;
2202 u32 ref = 0;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002203 struct tipc_sock *tsk = tipc_sk_get_next(&ref);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002204
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002205 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2206 lock_sock(&tsk->sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002207 msg = &tsk->port.phdr;
2208 msg_set_prevnode(msg, tipc_own_addr);
2209 msg_set_orignode(msg, tipc_own_addr);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002210 release_sock(&tsk->sk);
2211 tipc_sk_put(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002212 }
2213}
2214
Per Lidenb97bf3f2006-01-02 19:04:38 +01002215/**
Ying Xue247f0f32014-02-18 16:06:46 +08002216 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002217 * @sock: socket structure
2218 * @lvl: option level
2219 * @opt: option identifier
2220 * @ov: pointer to new option value
2221 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002222 *
2223 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002224 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002225 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002226 * Returns 0 on success, errno otherwise
2227 */
Ying Xue247f0f32014-02-18 16:06:46 +08002228static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2229 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002230{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002231 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002232 struct tipc_sock *tsk = tipc_sk(sk);
2233 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002234 u32 value;
2235 int res;
2236
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002237 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2238 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002239 if (lvl != SOL_TIPC)
2240 return -ENOPROTOOPT;
2241 if (ol < sizeof(value))
2242 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002243 res = get_user(value, (u32 __user *)ov);
2244 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002245 return res;
2246
Allan Stephens0c3141e2008-04-15 00:22:02 -07002247 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002248
Per Lidenb97bf3f2006-01-02 19:04:38 +01002249 switch (opt) {
2250 case TIPC_IMPORTANCE:
Erik Hugneac32c7f2014-08-15 16:44:35 +02002251 res = tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002252 break;
2253 case TIPC_SRC_DROPPABLE:
2254 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002255 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002256 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002257 res = -ENOPROTOOPT;
2258 break;
2259 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002260 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002261 break;
2262 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002263 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002264 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002265 break;
2266 default:
2267 res = -EINVAL;
2268 }
2269
Allan Stephens0c3141e2008-04-15 00:22:02 -07002270 release_sock(sk);
2271
Per Lidenb97bf3f2006-01-02 19:04:38 +01002272 return res;
2273}
2274
2275/**
Ying Xue247f0f32014-02-18 16:06:46 +08002276 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002277 * @sock: socket structure
2278 * @lvl: option level
2279 * @opt: option identifier
2280 * @ov: receptacle for option value
2281 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002282 *
2283 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002284 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002285 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002286 * Returns 0 on success, errno otherwise
2287 */
Ying Xue247f0f32014-02-18 16:06:46 +08002288static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2289 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002290{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002291 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002292 struct tipc_sock *tsk = tipc_sk(sk);
2293 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002294 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002295 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002296 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002297
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002298 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2299 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002300 if (lvl != SOL_TIPC)
2301 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002302 res = get_user(len, ol);
2303 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002304 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002305
Allan Stephens0c3141e2008-04-15 00:22:02 -07002306 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002307
2308 switch (opt) {
2309 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002310 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002311 break;
2312 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002313 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002314 break;
2315 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002316 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002317 break;
2318 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002319 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002320 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002321 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002322 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002323 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002324 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002325 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002326 value = skb_queue_len(&sk->sk_receive_queue);
2327 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002328 default:
2329 res = -EINVAL;
2330 }
2331
Allan Stephens0c3141e2008-04-15 00:22:02 -07002332 release_sock(sk);
2333
Paul Gortmaker25860c32010-12-31 18:59:31 +00002334 if (res)
2335 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002336
Paul Gortmaker25860c32010-12-31 18:59:31 +00002337 if (len < sizeof(value))
2338 return -EINVAL;
2339
2340 if (copy_to_user(ov, &value, sizeof(value)))
2341 return -EFAULT;
2342
2343 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002344}
2345
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002346static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002347{
2348 struct tipc_sioc_ln_req lnr;
2349 void __user *argp = (void __user *)arg;
2350
2351 switch (cmd) {
2352 case SIOCGETLINKNAME:
2353 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2354 return -EFAULT;
2355 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2356 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2357 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2358 return -EFAULT;
2359 return 0;
2360 }
2361 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002362 default:
2363 return -ENOIOCTLCMD;
2364 }
2365}
2366
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002367/* Protocol switches for the various types of TIPC sockets */
2368
Florian Westphalbca65ea2008-02-07 18:18:01 -08002369static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002370 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002371 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002372 .release = tipc_release,
2373 .bind = tipc_bind,
2374 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002375 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002376 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002377 .getname = tipc_getname,
2378 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002379 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002380 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002381 .shutdown = tipc_shutdown,
2382 .setsockopt = tipc_setsockopt,
2383 .getsockopt = tipc_getsockopt,
2384 .sendmsg = tipc_sendmsg,
2385 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002386 .mmap = sock_no_mmap,
2387 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002388};
2389
Florian Westphalbca65ea2008-02-07 18:18:01 -08002390static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002391 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002392 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002393 .release = tipc_release,
2394 .bind = tipc_bind,
2395 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002396 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002397 .accept = tipc_accept,
2398 .getname = tipc_getname,
2399 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002400 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002401 .listen = tipc_listen,
2402 .shutdown = tipc_shutdown,
2403 .setsockopt = tipc_setsockopt,
2404 .getsockopt = tipc_getsockopt,
2405 .sendmsg = tipc_send_packet,
2406 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002407 .mmap = sock_no_mmap,
2408 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409};
2410
Florian Westphalbca65ea2008-02-07 18:18:01 -08002411static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002412 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002413 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002414 .release = tipc_release,
2415 .bind = tipc_bind,
2416 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002417 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002418 .accept = tipc_accept,
2419 .getname = tipc_getname,
2420 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002421 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002422 .listen = tipc_listen,
2423 .shutdown = tipc_shutdown,
2424 .setsockopt = tipc_setsockopt,
2425 .getsockopt = tipc_getsockopt,
2426 .sendmsg = tipc_send_stream,
2427 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002428 .mmap = sock_no_mmap,
2429 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002430};
2431
Florian Westphalbca65ea2008-02-07 18:18:01 -08002432static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002433 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002434 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002435 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002436};
2437
2438static struct proto tipc_proto = {
2439 .name = "TIPC",
2440 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002441 .obj_size = sizeof(struct tipc_sock),
2442 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002443};
2444
Ying Xuec5fa7b32013-06-17 10:54:39 -04002445static struct proto tipc_proto_kern = {
2446 .name = "TIPC",
2447 .obj_size = sizeof(struct tipc_sock),
2448 .sysctl_rmem = sysctl_tipc_rmem
2449};
2450
Per Lidenb97bf3f2006-01-02 19:04:38 +01002451/**
Per Liden4323add2006-01-18 00:38:21 +01002452 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002453 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002454 * Returns 0 on success, errno otherwise
2455 */
Per Liden4323add2006-01-18 00:38:21 +01002456int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002457{
2458 int res;
2459
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002460 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002461 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002462 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002463 goto out;
2464 }
2465
2466 res = sock_register(&tipc_family_ops);
2467 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002468 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002469 proto_unregister(&tipc_proto);
2470 goto out;
2471 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002472 out:
2473 return res;
2474}
2475
2476/**
Per Liden4323add2006-01-18 00:38:21 +01002477 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002478 */
Per Liden4323add2006-01-18 00:38:21 +01002479void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002480{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002481 sock_unregister(tipc_family_ops.family);
2482 proto_unregister(&tipc_proto);
2483}