blob: 70eaceae1f8cb2daaabebbed7a41599480646b3d [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"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040042#include <linux/export.h>
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -040043#include "config.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040044#include "socket.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 Maloy2e84c602014-08-22 18:09:18 -0400117static u32 tsk_peer_node(struct tipc_port *p_ptr)
118{
119 return msg_destnode(&p_ptr->phdr);
120}
121
122static u32 tsk_peer_port(struct tipc_port *p_ptr)
123{
124 return msg_destport(&p_ptr->phdr);
125}
126
127static bool tsk_unreliable(struct tipc_port *port)
128{
129 return msg_src_droppable(&port->phdr) != 0;
130}
131
132static void tsk_set_unreliable(struct tipc_port *port, bool unreliable)
133{
134 msg_set_src_droppable(&port->phdr, unreliable ? 1 : 0);
135}
136
137static bool tsk_unreturnable(struct tipc_port *port)
138{
139 return msg_dest_droppable(&port->phdr) != 0;
140}
141
142static void tsk_set_unreturnable(struct tipc_port *port, bool unreturnable)
143{
144 msg_set_dest_droppable(&port->phdr, unreturnable ? 1 : 0);
145}
146
147static int tsk_importance(struct tipc_port *port)
148{
149 return msg_importance(&port->phdr);
150}
151
152static int tsk_set_importance(struct tipc_port *port, int imp)
153{
154 if (imp > TIPC_CRITICAL_IMPORTANCE)
155 return -EINVAL;
156 msg_set_importance(&port->phdr, (u32)imp);
157 return 0;
158}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400159
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400161 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700162 *
163 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400165static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400167 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168}
169
170/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400171 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700172 *
173 * Caller must hold socket lock
174 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400175static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700176{
177 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500178 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700179
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500180 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
181 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400182 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500183 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700184}
185
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400186/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400187 *
188 * Handles cases where the node's network address has changed from
189 * the default of <0.0.0> to its configured setting.
190 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400191static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400192{
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400193 u32 peer_port = tsk_peer_port(&tsk->port);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400194 u32 orig_node;
195 u32 peer_node;
196
197 if (unlikely(!tsk->port.connected))
198 return false;
199
200 if (unlikely(msg_origport(msg) != peer_port))
201 return false;
202
203 orig_node = msg_orignode(msg);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400204 peer_node = tsk_peer_node(&tsk->port);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400205
206 if (likely(orig_node == peer_node))
207 return true;
208
209 if (!orig_node && (peer_node == tipc_own_addr))
210 return true;
211
212 if (!peer_node && (orig_node == tipc_own_addr))
213 return true;
214
215 return false;
216}
217
Allan Stephens0c3141e2008-04-15 00:22:02 -0700218/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400219 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700220 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 * @sock: pre-allocated socket structure
222 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800223 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900224 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700225 * This routine creates additional data structures used by the TIPC socket,
226 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 *
228 * Returns 0 on success, errno otherwise
229 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400230static int tipc_sk_create(struct net *net, struct socket *sock,
231 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700233 const struct proto_ops *ops;
234 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400236 struct tipc_sock *tsk;
237 struct tipc_port *port;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400238 struct tipc_msg *msg;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400239 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700240
241 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 if (unlikely(protocol != 0))
243 return -EPROTONOSUPPORT;
244
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 switch (sock->type) {
246 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700247 ops = &stream_ops;
248 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 break;
250 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700251 ops = &packet_ops;
252 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 break;
254 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700256 ops = &msg_ops;
257 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 break;
Allan Stephens49978652006-06-25 23:47:18 -0700259 default:
Allan Stephens49978652006-06-25 23:47:18 -0700260 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 }
262
Allan Stephens0c3141e2008-04-15 00:22:02 -0700263 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400264 if (!kern)
265 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
266 else
267 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
268
Allan Stephens0c3141e2008-04-15 00:22:02 -0700269 if (sk == NULL)
270 return -ENOMEM;
271
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400272 tsk = tipc_sk(sk);
273 port = &tsk->port;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -0400274 ref = tipc_ref_acquire(tsk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400275 if (!ref) {
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400276 pr_warn("Socket create failed; reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277 return -ENOMEM;
278 }
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400279 port->max_pkt = MAX_PKT_DEFAULT;
280 port->ref = ref;
281 INIT_LIST_HEAD(&port->publications);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400282
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400283 msg = &port->phdr;
284 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
285 NAMED_H_SIZE, 0);
286 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287
Allan Stephens0c3141e2008-04-15 00:22:02 -0700288 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700289 sock->ops = ops;
290 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 sock_init_data(sock, sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -0400292 k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400293 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400294 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800295 sk->sk_data_ready = tipc_data_ready;
296 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400297 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500298 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400299 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700300
Allan Stephens0c3141e2008-04-15 00:22:02 -0700301 if (sock->state == SS_READY) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400302 tsk_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700303 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400304 tsk_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700305 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306 return 0;
307}
308
309/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400310 * tipc_sock_create_local - create TIPC socket from inside TIPC module
311 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
312 *
313 * We cannot use sock_creat_kern here because it bumps module user count.
314 * Since socket owner and creator is the same module we must make sure
315 * that module count remains zero for module local sockets, otherwise
316 * we cannot do rmmod.
317 *
318 * Returns 0 on success, errno otherwise
319 */
320int tipc_sock_create_local(int type, struct socket **res)
321{
322 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400323
324 rc = sock_create_lite(AF_TIPC, type, 0, res);
325 if (rc < 0) {
326 pr_err("Failed to create kernel socket\n");
327 return rc;
328 }
329 tipc_sk_create(&init_net, *res, 0, 1);
330
Ying Xuec5fa7b32013-06-17 10:54:39 -0400331 return 0;
332}
333
334/**
335 * tipc_sock_release_local - release socket created by tipc_sock_create_local
336 * @sock: the socket to be released.
337 *
338 * Module reference count is not incremented when such sockets are created,
339 * so we must keep it from being decremented when they are released.
340 */
341void tipc_sock_release_local(struct socket *sock)
342{
Ying Xue247f0f32014-02-18 16:06:46 +0800343 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400344 sock->ops = NULL;
345 sock_release(sock);
346}
347
348/**
349 * tipc_sock_accept_local - accept a connection on a socket created
350 * with tipc_sock_create_local. Use this function to avoid that
351 * module reference count is inadvertently incremented.
352 *
353 * @sock: the accepting socket
354 * @newsock: reference to the new socket to be created
355 * @flags: socket flags
356 */
357
358int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400359 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400360{
361 struct sock *sk = sock->sk;
362 int ret;
363
364 ret = sock_create_lite(sk->sk_family, sk->sk_type,
365 sk->sk_protocol, newsock);
366 if (ret < 0)
367 return ret;
368
Ying Xue247f0f32014-02-18 16:06:46 +0800369 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400370 if (ret < 0) {
371 sock_release(*newsock);
372 return ret;
373 }
374 (*newsock)->ops = sock->ops;
375 return ret;
376}
377
378/**
Ying Xue247f0f32014-02-18 16:06:46 +0800379 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 * @sock: socket to destroy
381 *
382 * This routine cleans up any messages that are still queued on the socket.
383 * For DGRAM and RDM socket types, all queued messages are rejected.
384 * For SEQPACKET and STREAM socket types, the first message is rejected
385 * and any others are discarded. (If the first message on a STREAM socket
386 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900387 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 * NOTE: Rejected messages are not necessarily returned to the sender! They
389 * are returned or discarded according to the "destination droppable" setting
390 * specified for the message by the sender.
391 *
392 * Returns 0 on success, errno otherwise
393 */
Ying Xue247f0f32014-02-18 16:06:46 +0800394static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400397 struct tipc_sock *tsk;
398 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500400 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401
Allan Stephens0c3141e2008-04-15 00:22:02 -0700402 /*
403 * Exit if socket isn't fully initialized (occurs when a failed accept()
404 * releases a pre-allocated child socket that was never used)
405 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700406 if (sk == NULL)
407 return 0;
408
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400409 tsk = tipc_sk(sk);
410 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700411 lock_sock(sk);
412
413 /*
414 * Reject all unreceived messages, except on an active connection
415 * (which disconnects locally & sends a 'FIN+' to peer)
416 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400417 dnode = tsk_peer_node(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700419 buf = __skb_dequeue(&sk->sk_receive_queue);
420 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 break;
Ying Xue40682432013-10-18 07:23:16 +0200422 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400423 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700424 else {
425 if ((sock->state == SS_CONNECTING) ||
426 (sock->state == SS_CONNECTED)) {
427 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -0400428 port->connected = 0;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400429 tipc_node_remove_conn(dnode, port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700430 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500431 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400432 tipc_link_xmit(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700433 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 }
435
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400436 tipc_sk_withdraw(port, 0, NULL);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400437 tipc_ref_discard(port->ref);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400438 k_cancel_timer(&port->timer);
439 if (port->connected) {
440 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
441 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400442 tsk_peer_port(port),
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400443 port->ref, TIPC_ERR_NO_PORT);
444 if (buf)
445 tipc_link_xmit(buf, dnode, port->ref);
446 tipc_node_remove_conn(dnode, port->ref);
447 }
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400448 k_term_timer(&port->timer);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449
Allan Stephens0c3141e2008-04-15 00:22:02 -0700450 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100451 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452
Allan Stephens0c3141e2008-04-15 00:22:02 -0700453 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700454 sock->state = SS_DISCONNECTING;
455 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700457 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200459 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460}
461
462/**
Ying Xue247f0f32014-02-18 16:06:46 +0800463 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 * @sock: socket structure
465 * @uaddr: socket address describing name(s) and desired operation
466 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900467 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468 * Name and name sequence binding is indicated using a positive scope value;
469 * a negative scope value unbinds the specified name. Specifying no name
470 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900471 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700473 *
474 * NOTE: This routine doesn't need to take the socket lock since it doesn't
475 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476 */
Ying Xue247f0f32014-02-18 16:06:46 +0800477static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
478 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479{
Ying Xue84602762013-12-27 10:18:28 +0800480 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400482 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800483 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484
Ying Xue84602762013-12-27 10:18:28 +0800485 lock_sock(sk);
486 if (unlikely(!uaddr_len)) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400487 res = tipc_sk_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800488 goto exit;
489 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900490
Ying Xue84602762013-12-27 10:18:28 +0800491 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
492 res = -EINVAL;
493 goto exit;
494 }
495 if (addr->family != AF_TIPC) {
496 res = -EAFNOSUPPORT;
497 goto exit;
498 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500 if (addr->addrtype == TIPC_ADDR_NAME)
501 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800502 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
503 res = -EAFNOSUPPORT;
504 goto exit;
505 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900506
Ying Xue13a2e892013-06-17 10:54:40 -0400507 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400508 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800509 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
510 res = -EACCES;
511 goto exit;
512 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400513
Ying Xue84602762013-12-27 10:18:28 +0800514 res = (addr->scope > 0) ?
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400515 tipc_sk_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
516 tipc_sk_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800517exit:
518 release_sock(sk);
519 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520}
521
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900522/**
Ying Xue247f0f32014-02-18 16:06:46 +0800523 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524 * @sock: socket structure
525 * @uaddr: area for returned socket address
526 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700527 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900528 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700530 *
Allan Stephens2da59912008-07-14 22:43:32 -0700531 * NOTE: This routine doesn't need to take the socket lock since it only
532 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000533 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 */
Ying Xue247f0f32014-02-18 16:06:46 +0800535static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
536 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400539 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000541 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700542 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700543 if ((sock->state != SS_CONNECTED) &&
544 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
545 return -ENOTCONN;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400546 addr->addr.id.ref = tsk_peer_port(&tsk->port);
547 addr->addr.id.node = tsk_peer_node(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700548 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400549 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000550 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700551 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552
553 *uaddr_len = sizeof(*addr);
554 addr->addrtype = TIPC_ADDR_ID;
555 addr->family = AF_TIPC;
556 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 addr->addr.name.domain = 0;
558
Allan Stephens0c3141e2008-04-15 00:22:02 -0700559 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560}
561
562/**
Ying Xue247f0f32014-02-18 16:06:46 +0800563 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 * @file: file structure associated with the socket
565 * @sock: socket for which to calculate the poll bits
566 * @wait: ???
567 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700568 * Returns pollmask value
569 *
570 * COMMENTARY:
571 * It appears that the usual socket locking mechanisms are not useful here
572 * since the pollmask info is potentially out-of-date the moment this routine
573 * exits. TCP and other protocols seem to rely on higher level poll routines
574 * to handle any preventable race conditions, so TIPC will do the same ...
575 *
576 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700577 *
Allan Stephensf662c072010-08-17 11:00:06 +0000578 * socket state flags set
579 * ------------ ---------
580 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200581 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000582 *
583 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
584 * no write flags
585 *
586 * connected POLLIN/POLLRDNORM if data in rx queue
587 * POLLOUT if port is not congested
588 *
589 * disconnecting POLLIN/POLLRDNORM/POLLHUP
590 * no write flags
591 *
592 * listening POLLIN if SYN in rx queue
593 * no write flags
594 *
595 * ready POLLIN/POLLRDNORM if data in rx queue
596 * [connectionless] POLLOUT (since port cannot be congested)
597 *
598 * IMPORTANT: The fact that a read or write operation is indicated does NOT
599 * imply that the operation will succeed, merely that it should be performed
600 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 */
Ying Xue247f0f32014-02-18 16:06:46 +0800602static unsigned int tipc_poll(struct file *file, struct socket *sock,
603 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604{
Allan Stephens9b674e82008-03-26 16:48:21 -0700605 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400606 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000607 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700608
Ying Xuef288bef2012-08-21 11:16:57 +0800609 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700610
Allan Stephensf662c072010-08-17 11:00:06 +0000611 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200612 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500613 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200614 mask |= POLLOUT;
615 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000616 case SS_READY:
617 case SS_CONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500618 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000619 mask |= POLLOUT;
620 /* fall thru' */
621 case SS_CONNECTING:
622 case SS_LISTENING:
623 if (!skb_queue_empty(&sk->sk_receive_queue))
624 mask |= (POLLIN | POLLRDNORM);
625 break;
626 case SS_DISCONNECTING:
627 mask = (POLLIN | POLLRDNORM | POLLHUP);
628 break;
629 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700630
631 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632}
633
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400634/**
635 * tipc_sendmcast - send multicast message
636 * @sock: socket structure
637 * @seq: destination address
638 * @iov: message data to send
639 * @dsz: total length of message data
640 * @timeo: timeout to wait for wakeup
641 *
642 * Called from function tipc_sendmsg(), which has done all sanity checks
643 * Returns the number of bytes sent on success, or errno
644 */
645static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
646 struct iovec *iov, size_t dsz, long timeo)
647{
648 struct sock *sk = sock->sk;
649 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
650 struct sk_buff *buf;
651 uint mtu;
652 int rc;
653
654 msg_set_type(mhdr, TIPC_MCAST_MSG);
655 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
656 msg_set_destport(mhdr, 0);
657 msg_set_destnode(mhdr, 0);
658 msg_set_nametype(mhdr, seq->type);
659 msg_set_namelower(mhdr, seq->lower);
660 msg_set_nameupper(mhdr, seq->upper);
661 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
662
663new_mtu:
664 mtu = tipc_bclink_get_mtu();
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400665 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400666 if (unlikely(rc < 0))
667 return rc;
668
669 do {
670 rc = tipc_bclink_xmit(buf);
671 if (likely(rc >= 0)) {
672 rc = dsz;
673 break;
674 }
675 if (rc == -EMSGSIZE)
676 goto new_mtu;
677 if (rc != -ELINKCONG)
678 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400679 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400680 rc = tipc_wait_for_sndmsg(sock, &timeo);
681 if (rc)
682 kfree_skb_list(buf);
683 } while (!rc);
684 return rc;
685}
686
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400687/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
688 */
689void tipc_sk_mcast_rcv(struct sk_buff *buf)
690{
691 struct tipc_msg *msg = buf_msg(buf);
692 struct tipc_port_list dports = {0, NULL, };
693 struct tipc_port_list *item;
694 struct sk_buff *b;
695 uint i, last, dst = 0;
696 u32 scope = TIPC_CLUSTER_SCOPE;
697
698 if (in_own_node(msg_orignode(msg)))
699 scope = TIPC_NODE_SCOPE;
700
701 /* Create destination port list: */
702 tipc_nametbl_mc_translate(msg_nametype(msg),
703 msg_namelower(msg),
704 msg_nameupper(msg),
705 scope,
706 &dports);
707 last = dports.count;
708 if (!last) {
709 kfree_skb(buf);
710 return;
711 }
712
713 for (item = &dports; item; item = item->next) {
714 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
715 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
716 if (!b) {
717 pr_warn("Failed do clone mcast rcv buffer\n");
718 continue;
719 }
720 msg_set_destport(msg, item->ports[i]);
721 tipc_sk_rcv(b);
722 }
723 }
724 tipc_port_list_free(&dports);
725}
726
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900727/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500728 * tipc_sk_proto_rcv - receive a connection mng protocol message
729 * @tsk: receiving socket
730 * @dnode: node to send response message to, if any
731 * @buf: buffer containing protocol message
732 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
733 * (CONN_PROBE_REPLY) message should be forwarded.
734 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800735static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
736 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500737{
738 struct tipc_msg *msg = buf_msg(buf);
739 struct tipc_port *port = &tsk->port;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500740 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500741
742 /* Ignore if connection cannot be validated: */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400743 if (!tsk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500744 goto exit;
745
746 port->probing_state = TIPC_CONN_OK;
747
748 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500749 conn_cong = tipc_sk_conn_cong(tsk);
750 tsk->sent_unacked -= msg_msgcnt(msg);
751 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400752 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500753 } else if (msg_type(msg) == CONN_PROBE) {
754 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
755 return TIPC_OK;
756 msg_set_type(msg, CONN_PROBE_REPLY);
757 return TIPC_FWD_MSG;
758 }
759 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
760exit:
761 kfree_skb(buf);
762 return TIPC_OK;
763}
764
765/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 * dest_name_check - verify user is permitted to send to specified port name
767 * @dest: destination address
768 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900769 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770 * Prevents restricted configuration commands from being issued by
771 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900772 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 * Returns 0 if permission is granted, otherwise errno
774 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800775static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776{
777 struct tipc_cfg_msg_hdr hdr;
778
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500779 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
780 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900781 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
782 return 0;
783 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
784 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900785 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
786 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787
Allan Stephens3f8dd942011-01-18 13:09:29 -0500788 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
789 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900790 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700792 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900794
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795 return 0;
796}
797
Ying Xue3f405042014-01-17 09:50:05 +0800798static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
799{
800 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400801 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800802 DEFINE_WAIT(wait);
803 int done;
804
805 do {
806 int err = sock_error(sk);
807 if (err)
808 return err;
809 if (sock->state == SS_DISCONNECTING)
810 return -EPIPE;
811 if (!*timeo_p)
812 return -EAGAIN;
813 if (signal_pending(current))
814 return sock_intr_errno(*timeo_p);
815
816 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500817 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800818 finish_wait(sk_sleep(sk), &wait);
819 } while (!done);
820 return 0;
821}
822
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500823/**
Ying Xue247f0f32014-02-18 16:06:46 +0800824 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700825 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 * @sock: socket structure
827 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500828 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900829 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900831 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
833 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900834 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835 * Returns the number of bytes sent on success, or errno otherwise
836 */
Ying Xue247f0f32014-02-18 16:06:46 +0800837static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500838 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500840 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700841 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400842 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400843 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500844 struct tipc_msg *mhdr = &port->phdr;
845 struct iovec *iov = m->msg_iov;
846 u32 dnode, dport;
847 struct sk_buff *buf;
848 struct tipc_name_seq *seq = &dest->addr.nameseq;
849 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800850 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500851 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100852
853 if (unlikely(!dest))
854 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500855
Allan Stephens51f9cc12006-06-25 23:49:06 -0700856 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
857 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500859
860 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400861 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862
Allan Stephens0c3141e2008-04-15 00:22:02 -0700863 if (iocb)
864 lock_sock(sk);
865
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500866 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700867 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500868 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700869 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700870 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700871 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500872 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700873 goto exit;
874 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400875 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500876 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700877 goto exit;
878 }
879 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400880 tsk->port.conn_type = dest->addr.name.name.type;
881 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700882 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500884 rc = dest_name_check(dest, m);
885 if (rc)
886 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887
Ying Xue3f405042014-01-17 09:50:05 +0800888 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500889
890 if (dest->addrtype == TIPC_ADDR_MCAST) {
891 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
892 goto exit;
893 } else if (dest->addrtype == TIPC_ADDR_NAME) {
894 u32 type = dest->addr.name.name.type;
895 u32 inst = dest->addr.name.name.instance;
896 u32 domain = dest->addr.name.domain;
897
898 dnode = domain;
899 msg_set_type(mhdr, TIPC_NAMED_MSG);
900 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
901 msg_set_nametype(mhdr, type);
902 msg_set_nameinst(mhdr, inst);
903 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
904 dport = tipc_nametbl_translate(type, inst, &dnode);
905 msg_set_destnode(mhdr, dnode);
906 msg_set_destport(mhdr, dport);
907 if (unlikely(!dport && !dnode)) {
908 rc = -EHOSTUNREACH;
909 goto exit;
910 }
911 } else if (dest->addrtype == TIPC_ADDR_ID) {
912 dnode = dest->addr.id.node;
913 msg_set_type(mhdr, TIPC_DIRECT_MSG);
914 msg_set_lookup_scope(mhdr, 0);
915 msg_set_destnode(mhdr, dnode);
916 msg_set_destport(mhdr, dest->addr.id.ref);
917 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
918 }
919
920new_mtu:
921 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400922 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500923 if (rc < 0)
924 goto exit;
925
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900926 do {
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400927 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400928 rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500929 if (likely(rc >= 0)) {
930 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700931 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500932 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700933 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900934 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500935 if (rc == -EMSGSIZE)
936 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500937 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700938 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400939 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500940 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400941 if (rc)
942 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500943 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700944exit:
945 if (iocb)
946 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500947
948 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949}
950
Ying Xue391a6dd2014-01-17 09:50:06 +0800951static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
952{
953 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400954 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800955 DEFINE_WAIT(wait);
956 int done;
957
958 do {
959 int err = sock_error(sk);
960 if (err)
961 return err;
962 if (sock->state == SS_DISCONNECTING)
963 return -EPIPE;
964 else if (sock->state != SS_CONNECTED)
965 return -ENOTCONN;
966 if (!*timeo_p)
967 return -EAGAIN;
968 if (signal_pending(current))
969 return sock_intr_errno(*timeo_p);
970
971 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
972 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500973 (!tsk->link_cong &&
974 !tipc_sk_conn_cong(tsk)) ||
975 !tsk->port.connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800976 finish_wait(sk_sleep(sk), &wait);
977 } while (!done);
978 return 0;
979}
980
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900981/**
Ying Xue247f0f32014-02-18 16:06:46 +0800982 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 * @iocb: (unused)
984 * @sock: socket structure
985 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500986 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900987 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900989 *
990 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700991 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992 */
Ying Xue247f0f32014-02-18 16:06:46 +0800993static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500994 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700996 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400997 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500998 struct tipc_port *port = &tsk->port;
999 struct tipc_msg *mhdr = &port->phdr;
1000 struct sk_buff *buf;
1001 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1002 u32 ref = port->ref;
1003 int rc = -EINVAL;
1004 long timeo;
1005 u32 dnode;
1006 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001007
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001008 /* Handle implied connection establishment */
1009 if (unlikely(dest)) {
1010 rc = tipc_sendmsg(iocb, sock, m, dsz);
1011 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001012 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001013 return rc;
1014 }
1015 if (dsz > (uint)INT_MAX)
1016 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001017
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001018 if (iocb)
1019 lock_sock(sk);
1020
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001021 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001022 if (sock->state == SS_DISCONNECTING)
1023 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001024 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001025 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +08001026 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001027 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001029 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001030 dnode = tsk_peer_node(port);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001031
1032next:
1033 mtu = port->max_pkt;
1034 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001035 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001036 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -07001037 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001038 do {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001039 if (likely(!tipc_sk_conn_cong(tsk))) {
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001040 rc = tipc_link_xmit(buf, dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001041 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001042 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001043 sent += send;
1044 if (sent == dsz)
1045 break;
1046 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001047 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001048 if (rc == -EMSGSIZE) {
1049 port->max_pkt = tipc_node_get_mtu(dnode, ref);
1050 goto next;
1051 }
1052 if (rc != -ELINKCONG)
1053 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001054 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001056 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001057 if (rc)
1058 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001059 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001060exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001061 if (iocb)
1062 release_sock(sk);
1063 return sent ? sent : rc;
1064}
1065
1066/**
1067 * tipc_send_packet - send a connection-oriented message
1068 * @iocb: if NULL, indicates that socket lock is already held
1069 * @sock: socket structure
1070 * @m: message to send
1071 * @dsz: length of data to be transmitted
1072 *
1073 * Used for SOCK_SEQPACKET messages.
1074 *
1075 * Returns the number of bytes sent on success, or errno otherwise
1076 */
1077static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1078 struct msghdr *m, size_t dsz)
1079{
1080 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1081 return -EMSGSIZE;
1082
1083 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084}
1085
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001086/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087 */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001088static void tipc_sk_finish_conn(struct tipc_port *port, u32 peer_port,
1089 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090{
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001091 struct tipc_msg *msg = &port->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001093 msg_set_destnode(msg, peer_node);
1094 msg_set_destport(msg, peer_port);
1095 msg_set_type(msg, TIPC_CONN_MSG);
1096 msg_set_lookup_scope(msg, 0);
1097 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001098
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001099 port->probing_interval = CONN_PROBING_INTERVAL;
1100 port->probing_state = TIPC_CONN_OK;
1101 port->connected = 1;
1102 k_start_timer(&port->timer, port->probing_interval);
1103 tipc_node_add_conn(peer_node, port->ref, peer_port);
1104 port->max_pkt = tipc_node_get_mtu(peer_node, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105}
1106
1107/**
1108 * set_orig_addr - capture sender's address for received message
1109 * @m: descriptor for message info
1110 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001111 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112 * Note: Address is not captured if not requested by receiver.
1113 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001114static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001116 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001118 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119 addr->family = AF_TIPC;
1120 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001121 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 addr->addr.id.ref = msg_origport(msg);
1123 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001124 addr->addr.name.domain = 0; /* could leave uninitialized */
1125 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126 m->msg_namelen = sizeof(struct sockaddr_tipc);
1127 }
1128}
1129
1130/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001131 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 * @m: descriptor for message info
1133 * @msg: received message header
1134 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001135 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001137 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 * Returns 0 if successful, otherwise errno
1139 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001140static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001141 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142{
1143 u32 anc_data[3];
1144 u32 err;
1145 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001146 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 int res;
1148
1149 if (likely(m->msg_controllen == 0))
1150 return 0;
1151
1152 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 err = msg ? msg_errcode(msg) : 0;
1154 if (unlikely(err)) {
1155 anc_data[0] = err;
1156 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001157 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1158 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001160 if (anc_data[1]) {
1161 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1162 msg_data(msg));
1163 if (res)
1164 return res;
1165 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 }
1167
1168 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1170 switch (dest_type) {
1171 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001172 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 anc_data[0] = msg_nametype(msg);
1174 anc_data[1] = msg_namelower(msg);
1175 anc_data[2] = msg_namelower(msg);
1176 break;
1177 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001178 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179 anc_data[0] = msg_nametype(msg);
1180 anc_data[1] = msg_namelower(msg);
1181 anc_data[2] = msg_nameupper(msg);
1182 break;
1183 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001184 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 anc_data[0] = tport->conn_type;
1186 anc_data[1] = tport->conn_instance;
1187 anc_data[2] = tport->conn_instance;
1188 break;
1189 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001190 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 }
Allan Stephens2db99832010-12-31 18:59:33 +00001192 if (has_name) {
1193 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1194 if (res)
1195 return res;
1196 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197
1198 return 0;
1199}
1200
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001201static void tipc_sk_send_ack(struct tipc_port *port, uint ack)
1202{
1203 struct sk_buff *buf = NULL;
1204 struct tipc_msg *msg;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001205 u32 peer_port = tsk_peer_port(port);
1206 u32 dnode = tsk_peer_node(port);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001207
1208 if (!port->connected)
1209 return;
1210 buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
1211 tipc_own_addr, peer_port, port->ref, TIPC_OK);
1212 if (!buf)
1213 return;
1214 msg = buf_msg(buf);
1215 msg_set_msgcnt(msg, ack);
1216 tipc_link_xmit(buf, dnode, msg_link_selector(msg));
1217}
1218
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001219static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001220{
1221 struct sock *sk = sock->sk;
1222 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001223 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001224 int err;
1225
1226 for (;;) {
1227 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001228 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001229 if (sock->state == SS_DISCONNECTING) {
1230 err = -ENOTCONN;
1231 break;
1232 }
1233 release_sock(sk);
1234 timeo = schedule_timeout(timeo);
1235 lock_sock(sk);
1236 }
1237 err = 0;
1238 if (!skb_queue_empty(&sk->sk_receive_queue))
1239 break;
1240 err = sock_intr_errno(timeo);
1241 if (signal_pending(current))
1242 break;
1243 err = -EAGAIN;
1244 if (!timeo)
1245 break;
1246 }
1247 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001248 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001249 return err;
1250}
1251
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001252/**
Ying Xue247f0f32014-02-18 16:06:46 +08001253 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254 * @iocb: (unused)
1255 * @m: descriptor for message info
1256 * @buf_len: total size of user buffer area
1257 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001258 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1260 * If the complete message doesn't fit in user area, truncate it.
1261 *
1262 * Returns size of returned message data, errno otherwise
1263 */
Ying Xue247f0f32014-02-18 16:06:46 +08001264static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1265 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001267 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001268 struct tipc_sock *tsk = tipc_sk(sk);
1269 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 struct sk_buff *buf;
1271 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001272 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001273 unsigned int sz;
1274 u32 err;
1275 int res;
1276
Allan Stephens0c3141e2008-04-15 00:22:02 -07001277 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 if (unlikely(!buf_len))
1279 return -EINVAL;
1280
Allan Stephens0c3141e2008-04-15 00:22:02 -07001281 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282
Allan Stephens0c3141e2008-04-15 00:22:02 -07001283 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001284 res = -ENOTCONN;
1285 goto exit;
1286 }
1287
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001288 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001289restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290
Allan Stephens0c3141e2008-04-15 00:22:02 -07001291 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001292 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001293 if (res)
1294 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001295
1296 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001297 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298 msg = buf_msg(buf);
1299 sz = msg_data_sz(msg);
1300 err = msg_errcode(msg);
1301
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001304 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 goto restart;
1306 }
1307
1308 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 set_orig_addr(m, msg);
1310
1311 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001312 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001313 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 goto exit;
1315
1316 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317 if (!err) {
1318 if (unlikely(buf_len < sz)) {
1319 sz = buf_len;
1320 m->msg_flags |= MSG_TRUNC;
1321 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001322 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1323 m->msg_iov, sz);
1324 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 res = sz;
1327 } else {
1328 if ((sock->state == SS_READY) ||
1329 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1330 res = 0;
1331 else
1332 res = -ECONNRESET;
1333 }
1334
1335 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001337 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001338 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001339 tipc_sk_send_ack(port, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001340 tsk->rcv_unacked = 0;
1341 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001342 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001343 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001345 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 return res;
1347}
1348
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001349/**
Ying Xue247f0f32014-02-18 16:06:46 +08001350 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 * @iocb: (unused)
1352 * @m: descriptor for message info
1353 * @buf_len: total size of user buffer area
1354 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001355 *
1356 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 * will optionally wait for more; never truncates data.
1358 *
1359 * Returns size of returned message data, errno otherwise
1360 */
Ying Xue247f0f32014-02-18 16:06:46 +08001361static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1362 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001364 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001365 struct tipc_sock *tsk = tipc_sk(sk);
1366 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 struct sk_buff *buf;
1368 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001369 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001371 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001373 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001374 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375
1376 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 if (unlikely(!buf_len))
1378 return -EINVAL;
1379
Allan Stephens0c3141e2008-04-15 00:22:02 -07001380 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001382 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 res = -ENOTCONN;
1384 goto exit;
1385 }
1386
Florian Westphal3720d402010-08-17 11:00:04 +00001387 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001388 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001389
Allan Stephens0c3141e2008-04-15 00:22:02 -07001390restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001391 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001392 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001393 if (res)
1394 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001395
1396 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001397 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 msg = buf_msg(buf);
1399 sz = msg_data_sz(msg);
1400 err = msg_errcode(msg);
1401
1402 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001404 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001405 goto restart;
1406 }
1407
1408 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409 if (sz_copied == 0) {
1410 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001411 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001412 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413 goto exit;
1414 }
1415
1416 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001418 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419
Allan Stephens0232fd02011-02-21 09:45:40 -05001420 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 needed = (buf_len - sz_copied);
1422 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001423
1424 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1425 m->msg_iov, sz_to_copy);
1426 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001428
Per Lidenb97bf3f2006-01-02 19:04:38 +01001429 sz_copied += sz_to_copy;
1430
1431 if (sz_to_copy < sz) {
1432 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001433 TIPC_SKB_CB(buf)->handle =
1434 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 goto exit;
1436 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437 } else {
1438 if (sz_copied != 0)
1439 goto exit; /* can't add error msg to valid data */
1440
1441 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1442 res = 0;
1443 else
1444 res = -ECONNRESET;
1445 }
1446
1447 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001449 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001450 tipc_sk_send_ack(port, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001451 tsk->rcv_unacked = 0;
1452 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001453 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001454 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455
1456 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001457 if ((sz_copied < buf_len) && /* didn't get all requested data */
1458 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001459 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001460 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1461 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462 goto restart;
1463
1464exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001465 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001466 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467}
1468
1469/**
Ying Xuef288bef2012-08-21 11:16:57 +08001470 * tipc_write_space - wake up thread if port congestion is released
1471 * @sk: socket
1472 */
1473static void tipc_write_space(struct sock *sk)
1474{
1475 struct socket_wq *wq;
1476
1477 rcu_read_lock();
1478 wq = rcu_dereference(sk->sk_wq);
1479 if (wq_has_sleeper(wq))
1480 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1481 POLLWRNORM | POLLWRBAND);
1482 rcu_read_unlock();
1483}
1484
1485/**
1486 * tipc_data_ready - wake up threads to indicate messages have been received
1487 * @sk: socket
1488 * @len: the length of messages
1489 */
David S. Miller676d2362014-04-11 16:15:36 -04001490static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001491{
1492 struct socket_wq *wq;
1493
1494 rcu_read_lock();
1495 wq = rcu_dereference(sk->sk_wq);
1496 if (wq_has_sleeper(wq))
1497 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1498 POLLRDNORM | POLLRDBAND);
1499 rcu_read_unlock();
1500}
1501
1502/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001503 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001504 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001505 * @msg: message
1506 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001507 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001508 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001509static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001510{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001511 struct sock *sk = &tsk->sk;
1512 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001513 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001514 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001515
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001516 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001517
1518 if (msg_mcast(msg))
1519 return retval;
1520
1521 switch ((int)sock->state) {
1522 case SS_CONNECTED:
1523 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001524 if (tsk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001525 if (unlikely(msg_errcode(msg))) {
1526 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001527 port->connected = 0;
1528 /* let timer expire on it's own */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001529 tipc_node_remove_conn(tsk_peer_node(port),
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001530 port->ref);
Ying Xue7e6c1312012-11-29 18:39:14 -05001531 }
1532 retval = TIPC_OK;
1533 }
1534 break;
1535 case SS_CONNECTING:
1536 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001537
1538 if (unlikely(!msg_connected(msg)))
1539 break;
1540
Ying Xue584d24b2012-11-29 18:51:19 -05001541 if (unlikely(msg_errcode(msg))) {
1542 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001543 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001544 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001545 break;
1546 }
1547
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001548 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001549 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001550 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001551 retval = TIPC_OK;
1552 break;
1553 }
1554
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001555 tipc_sk_finish_conn(port, msg_origport(msg), msg_orignode(msg));
1556 msg_set_importance(&port->phdr, msg_importance(msg));
1557 sock->state = SS_CONNECTED;
1558
Ying Xue584d24b2012-11-29 18:51:19 -05001559 /* If an incoming message is an 'ACK-', it should be
1560 * discarded here because it doesn't contain useful
1561 * data. In addition, we should try to wake up
1562 * connect() routine if sleeping.
1563 */
1564 if (msg_data_sz(msg) == 0) {
1565 kfree_skb(*buf);
1566 *buf = NULL;
1567 if (waitqueue_active(sk_sleep(sk)))
1568 wake_up_interruptible(sk_sleep(sk));
1569 }
1570 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001571 break;
1572 case SS_LISTENING:
1573 case SS_UNCONNECTED:
1574 /* Accept only SYN message */
1575 if (!msg_connected(msg) && !(msg_errcode(msg)))
1576 retval = TIPC_OK;
1577 break;
1578 case SS_DISCONNECTING:
1579 break;
1580 default:
1581 pr_err("Unknown socket state %u\n", sock->state);
1582 }
1583 return retval;
1584}
1585
1586/**
Ying Xueaba79f32013-01-20 23:30:09 +01001587 * rcvbuf_limit - get proper overload limit of socket receive queue
1588 * @sk: socket
1589 * @buf: message
1590 *
1591 * For all connection oriented messages, irrespective of importance,
1592 * the default overload value (i.e. 67MB) is set as limit.
1593 *
1594 * For all connectionless messages, by default new queue limits are
1595 * as belows:
1596 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001597 * TIPC_LOW_IMPORTANCE (4 MB)
1598 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1599 * TIPC_HIGH_IMPORTANCE (16 MB)
1600 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001601 *
1602 * Returns overload limit according to corresponding message importance
1603 */
1604static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1605{
1606 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001607
1608 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001609 return sysctl_tipc_rmem[2];
1610
1611 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1612 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001613}
1614
1615/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001616 * filter_rcv - validate incoming message
1617 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001618 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001619 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001620 * Enqueues message on receive queue if acceptable; optionally handles
1621 * disconnect indication for a connected socket.
1622 *
1623 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001624 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001625 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001626 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001627 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001628static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001629{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001630 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001631 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001633 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001634 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001635 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001636
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001637 if (unlikely(msg_user(msg) == CONN_MANAGER))
1638 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001639
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001640 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1641 kfree_skb(buf);
1642 tsk->link_cong = 0;
1643 sk->sk_write_space(sk);
1644 return TIPC_OK;
1645 }
1646
Per Lidenb97bf3f2006-01-02 19:04:38 +01001647 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001648 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001649 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001650
Per Lidenb97bf3f2006-01-02 19:04:38 +01001651 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001652 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001653 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001655 rc = filter_connect(tsk, &buf);
1656 if (rc != TIPC_OK || buf == NULL)
1657 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658 }
1659
1660 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001661 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001662 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663
Ying Xueaba79f32013-01-20 23:30:09 +01001664 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001665 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001666 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001667 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001668
David S. Miller676d2362014-04-11 16:15:36 -04001669 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670 return TIPC_OK;
1671}
1672
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001673/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001674 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001675 * @sk: socket
1676 * @buf: message
1677 *
1678 * Caller must hold socket lock, but not port lock.
1679 *
1680 * Returns 0
1681 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001682static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001683{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001684 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001685 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001686 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001687 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001688
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001689 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001690
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001691 if (likely(!rc)) {
1692 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1693 atomic_add(truesize, &tsk->dupl_rcvcnt);
1694 return 0;
1695 }
1696
1697 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1698 return 0;
1699
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001700 tipc_link_xmit(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001701
Allan Stephens0c3141e2008-04-15 00:22:02 -07001702 return 0;
1703}
1704
1705/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001706 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001707 * @buf: buffer containing arriving message
1708 * Consumes buffer
1709 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001710 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001711int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001712{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001713 struct tipc_sock *tsk;
1714 struct tipc_port *port;
1715 struct sock *sk;
1716 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001717 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001718 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001719 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001720
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001721 /* Validate destination and message */
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001722 tsk = tipc_sk_get(dport);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001723 if (unlikely(!tsk)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001724 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001725 goto exit;
1726 }
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001727 port = &tsk->port;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001728 sk = &tsk->sk;
1729
1730 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001732
Allan Stephens0c3141e2008-04-15 00:22:02 -07001733 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001734 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001735 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001736 if (sk->sk_backlog.len == 0)
1737 atomic_set(&tsk->dupl_rcvcnt, 0);
1738 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1739 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001740 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001741 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001742 bh_unlock_sock(sk);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001743 tipc_sk_put(tsk);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001744 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001745 return 0;
1746exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001747 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001748 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001749
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001750 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001751 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001752}
1753
Ying Xue78eb3a52014-01-17 09:50:03 +08001754static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1755{
1756 struct sock *sk = sock->sk;
1757 DEFINE_WAIT(wait);
1758 int done;
1759
1760 do {
1761 int err = sock_error(sk);
1762 if (err)
1763 return err;
1764 if (!*timeo_p)
1765 return -ETIMEDOUT;
1766 if (signal_pending(current))
1767 return sock_intr_errno(*timeo_p);
1768
1769 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1770 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1771 finish_wait(sk_sleep(sk), &wait);
1772 } while (!done);
1773 return 0;
1774}
1775
Per Lidenb97bf3f2006-01-02 19:04:38 +01001776/**
Ying Xue247f0f32014-02-18 16:06:46 +08001777 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778 * @sock: socket structure
1779 * @dest: socket address for destination port
1780 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001781 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782 *
1783 * Returns 0 on success, errno otherwise
1784 */
Ying Xue247f0f32014-02-18 16:06:46 +08001785static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1786 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001788 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001789 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1790 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001791 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1792 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001793 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794
Allan Stephens0c3141e2008-04-15 00:22:02 -07001795 lock_sock(sk);
1796
Allan Stephensb89741a2008-04-15 00:20:37 -07001797 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001798 if (sock->state == SS_READY) {
1799 res = -EOPNOTSUPP;
1800 goto exit;
1801 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001802
Allan Stephensb89741a2008-04-15 00:20:37 -07001803 /*
1804 * Reject connection attempt using multicast address
1805 *
1806 * Note: send_msg() validates the rest of the address fields,
1807 * so there's no need to do it here
1808 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001809 if (dst->addrtype == TIPC_ADDR_MCAST) {
1810 res = -EINVAL;
1811 goto exit;
1812 }
1813
Ying Xue78eb3a52014-01-17 09:50:03 +08001814 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001815 switch (sock->state) {
1816 case SS_UNCONNECTED:
1817 /* Send a 'SYN-' to destination */
1818 m.msg_name = dest;
1819 m.msg_namelen = destlen;
1820
1821 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1822 * indicate send_msg() is never blocked.
1823 */
1824 if (!timeout)
1825 m.msg_flags = MSG_DONTWAIT;
1826
Ying Xue247f0f32014-02-18 16:06:46 +08001827 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001828 if ((res < 0) && (res != -EWOULDBLOCK))
1829 goto exit;
1830
1831 /* Just entered SS_CONNECTING state; the only
1832 * difference is that return value in non-blocking
1833 * case is EINPROGRESS, rather than EALREADY.
1834 */
1835 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001836 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001837 if (previous == SS_CONNECTING)
1838 res = -EALREADY;
1839 if (!timeout)
1840 goto exit;
1841 timeout = msecs_to_jiffies(timeout);
1842 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1843 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001844 break;
1845 case SS_CONNECTED:
1846 res = -EISCONN;
1847 break;
1848 default:
1849 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001850 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001851 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001852exit:
1853 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001854 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855}
1856
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001857/**
Ying Xue247f0f32014-02-18 16:06:46 +08001858 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 * @sock: socket structure
1860 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001861 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862 * Returns 0 on success, errno otherwise
1863 */
Ying Xue247f0f32014-02-18 16:06:46 +08001864static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001866 struct sock *sk = sock->sk;
1867 int res;
1868
1869 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870
Ying Xue245f3d32011-07-06 06:01:13 -04001871 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001872 res = -EINVAL;
1873 else {
1874 sock->state = SS_LISTENING;
1875 res = 0;
1876 }
1877
1878 release_sock(sk);
1879 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880}
1881
Ying Xue6398e232014-01-17 09:50:04 +08001882static int tipc_wait_for_accept(struct socket *sock, long timeo)
1883{
1884 struct sock *sk = sock->sk;
1885 DEFINE_WAIT(wait);
1886 int err;
1887
1888 /* True wake-one mechanism for incoming connections: only
1889 * one process gets woken up, not the 'whole herd'.
1890 * Since we do not 'race & poll' for established sockets
1891 * anymore, the common case will execute the loop only once.
1892 */
1893 for (;;) {
1894 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1895 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001896 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001897 release_sock(sk);
1898 timeo = schedule_timeout(timeo);
1899 lock_sock(sk);
1900 }
1901 err = 0;
1902 if (!skb_queue_empty(&sk->sk_receive_queue))
1903 break;
1904 err = -EINVAL;
1905 if (sock->state != SS_LISTENING)
1906 break;
1907 err = sock_intr_errno(timeo);
1908 if (signal_pending(current))
1909 break;
1910 err = -EAGAIN;
1911 if (!timeo)
1912 break;
1913 }
1914 finish_wait(sk_sleep(sk), &wait);
1915 return err;
1916}
1917
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001918/**
Ying Xue247f0f32014-02-18 16:06:46 +08001919 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920 * @sock: listening socket
1921 * @newsock: new socket that is to be connected
1922 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001923 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001924 * Returns 0 on success, errno otherwise
1925 */
Ying Xue247f0f32014-02-18 16:06:46 +08001926static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001928 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001930 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001931 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001932 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001933 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934
Allan Stephens0c3141e2008-04-15 00:22:02 -07001935 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001936
Allan Stephens0c3141e2008-04-15 00:22:02 -07001937 if (sock->state != SS_LISTENING) {
1938 res = -EINVAL;
1939 goto exit;
1940 }
Ying Xue6398e232014-01-17 09:50:04 +08001941 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1942 res = tipc_wait_for_accept(sock, timeo);
1943 if (res)
1944 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001945
1946 buf = skb_peek(&sk->sk_receive_queue);
1947
Ying Xuec5fa7b32013-06-17 10:54:39 -04001948 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001949 if (res)
1950 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001951
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001952 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001953 new_port = &tipc_sk(new_sk)->port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001954 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001955
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001956 /* we lock on new_sk; but lockdep sees the lock on sk */
1957 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001958
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001959 /*
1960 * Reject any stray messages received by new socket
1961 * before the socket lock was taken (very, very unlikely)
1962 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001963 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001964
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001965 /* Connect new socket to it's peer */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001966 tipc_sk_finish_conn(new_port, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001967 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001969 tsk_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001970 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001971 new_port->conn_type = msg_nametype(msg);
1972 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001973 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001974
1975 /*
1976 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1977 * Respond to 'SYN+' by queuing it on new socket.
1978 */
1979 if (!msg_data_sz(msg)) {
1980 struct msghdr m = {NULL,};
1981
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001982 tsk_advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001983 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001984 } else {
1985 __skb_dequeue(&sk->sk_receive_queue);
1986 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001987 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001988 }
1989 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001991 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992 return res;
1993}
1994
1995/**
Ying Xue247f0f32014-02-18 16:06:46 +08001996 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001997 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001998 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001999 *
2000 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002001 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002002 * Returns 0 on success, errno otherwise
2003 */
Ying Xue247f0f32014-02-18 16:06:46 +08002004static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002005{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002006 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002007 struct tipc_sock *tsk = tipc_sk(sk);
2008 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002009 struct sk_buff *buf;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002010 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011 int res;
2012
Allan Stephense247a8f2008-03-06 15:05:38 -08002013 if (how != SHUT_RDWR)
2014 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002015
Allan Stephens0c3141e2008-04-15 00:22:02 -07002016 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002017
2018 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002019 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002020 case SS_CONNECTED:
2021
Per Lidenb97bf3f2006-01-02 19:04:38 +01002022restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002023 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07002024 buf = __skb_dequeue(&sk->sk_receive_queue);
2025 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02002026 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002027 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002028 goto restart;
2029 }
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002030 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
2031 tipc_link_xmit(buf, dnode, port->ref);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04002032 tipc_node_remove_conn(dnode, port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002033 } else {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002034 dnode = tsk_peer_node(port);
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002035 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
2036 TIPC_CONN_MSG, SHORT_H_SIZE,
2037 0, dnode, tipc_own_addr,
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002038 tsk_peer_port(port),
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002039 port->ref, TIPC_CONN_SHUTDOWN);
2040 tipc_link_xmit(buf, dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002041 }
Jon Paul Maloydadebc02014-08-22 18:09:11 -04002042 port->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002043 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04002044 tipc_node_remove_conn(dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002045 /* fall through */
2046
2047 case SS_DISCONNECTING:
2048
Ying Xue75031152012-10-29 09:38:15 -04002049 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002050 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002051
2052 /* Wake up anyone sleeping in poll */
2053 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002054 res = 0;
2055 break;
2056
2057 default:
2058 res = -ENOTCONN;
2059 }
2060
Allan Stephens0c3141e2008-04-15 00:22:02 -07002061 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002062 return res;
2063}
2064
Jon Paul Maloy57289012014-08-22 18:09:09 -04002065static void tipc_sk_timeout(unsigned long ref)
2066{
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002067 struct tipc_sock *tsk;
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04002068 struct tipc_port *port;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002069 struct sock *sk;
2070 struct sk_buff *buf = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002071 u32 peer_port, peer_node;
2072
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002073 tsk = tipc_sk_get(ref);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04002074 if (!tsk)
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002075 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002076 sk = &tsk->sk;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002077 port = &tsk->port;
2078
Jon Paul Maloy57289012014-08-22 18:09:09 -04002079 bh_lock_sock(sk);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002080 if (!port->connected) {
2081 bh_unlock_sock(sk);
2082 goto exit;
2083 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002084 peer_port = tsk_peer_port(port);
2085 peer_node = tsk_peer_node(port);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002086
2087 if (port->probing_state == TIPC_CONN_PROBING) {
2088 /* Previous probe not answered -> self abort */
2089 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2090 SHORT_H_SIZE, 0, tipc_own_addr,
2091 peer_node, ref, peer_port,
2092 TIPC_ERR_NO_PORT);
2093 } else {
2094 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2095 0, peer_node, tipc_own_addr,
2096 peer_port, ref, TIPC_OK);
2097 port->probing_state = TIPC_CONN_PROBING;
2098 k_start_timer(&port->timer, port->probing_interval);
2099 }
2100 bh_unlock_sock(sk);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002101 if (buf)
2102 tipc_link_xmit(buf, peer_node, ref);
2103exit:
2104 tipc_sk_put(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002105}
2106
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002107static int tipc_sk_publish(struct tipc_port *port, uint scope,
2108 struct tipc_name_seq const *seq)
2109{
2110 struct publication *publ;
2111 u32 key;
2112
2113 if (port->connected)
2114 return -EINVAL;
2115 key = port->ref + port->pub_count + 1;
2116 if (key == port->ref)
2117 return -EADDRINUSE;
2118
2119 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
2120 scope, port->ref, key);
2121 if (unlikely(!publ))
2122 return -EINVAL;
2123
2124 list_add(&publ->pport_list, &port->publications);
2125 port->pub_count++;
2126 port->published = 1;
2127 return 0;
2128}
2129
2130static int tipc_sk_withdraw(struct tipc_port *port, uint scope,
2131 struct tipc_name_seq const *seq)
2132{
2133 struct publication *publ;
2134 struct publication *safe;
2135 int rc = -EINVAL;
2136
2137 list_for_each_entry_safe(publ, safe, &port->publications, pport_list) {
2138 if (seq) {
2139 if (publ->scope != scope)
2140 continue;
2141 if (publ->type != seq->type)
2142 continue;
2143 if (publ->lower != seq->lower)
2144 continue;
2145 if (publ->upper != seq->upper)
2146 break;
2147 tipc_nametbl_withdraw(publ->type, publ->lower,
2148 publ->ref, publ->key);
2149 rc = 0;
2150 break;
2151 }
2152 tipc_nametbl_withdraw(publ->type, publ->lower,
2153 publ->ref, publ->key);
2154 rc = 0;
2155 }
2156 if (list_empty(&port->publications))
2157 port->published = 0;
2158 return rc;
2159}
2160
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002161static int tipc_sk_show(struct tipc_port *port, char *buf,
2162 int len, int full_id)
2163{
2164 struct publication *publ;
2165 int ret;
2166
2167 if (full_id)
2168 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2169 tipc_zone(tipc_own_addr),
2170 tipc_cluster(tipc_own_addr),
2171 tipc_node(tipc_own_addr), port->ref);
2172 else
2173 ret = tipc_snprintf(buf, len, "%-10u:", port->ref);
2174
2175 if (port->connected) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002176 u32 dport = tsk_peer_port(port);
2177 u32 destnode = tsk_peer_node(port);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002178
2179 ret += tipc_snprintf(buf + ret, len - ret,
2180 " connected to <%u.%u.%u:%u>",
2181 tipc_zone(destnode),
2182 tipc_cluster(destnode),
2183 tipc_node(destnode), dport);
2184 if (port->conn_type != 0)
2185 ret += tipc_snprintf(buf + ret, len - ret,
2186 " via {%u,%u}", port->conn_type,
2187 port->conn_instance);
2188 } else if (port->published) {
2189 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
2190 list_for_each_entry(publ, &port->publications, pport_list) {
2191 if (publ->lower == publ->upper)
2192 ret += tipc_snprintf(buf + ret, len - ret,
2193 " {%u,%u}", publ->type,
2194 publ->lower);
2195 else
2196 ret += tipc_snprintf(buf + ret, len - ret,
2197 " {%u,%u,%u}", publ->type,
2198 publ->lower, publ->upper);
2199 }
2200 }
2201 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2202 return ret;
2203}
2204
2205struct sk_buff *tipc_sk_socks_show(void)
2206{
2207 struct sk_buff *buf;
2208 struct tlv_desc *rep_tlv;
2209 char *pb;
2210 int pb_len;
2211 struct tipc_sock *tsk;
2212 int str_len = 0;
2213 u32 ref = 0;
2214
2215 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2216 if (!buf)
2217 return NULL;
2218 rep_tlv = (struct tlv_desc *)buf->data;
2219 pb = TLV_DATA(rep_tlv);
2220 pb_len = ULTRA_STRING_MAX_LEN;
2221
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002222 tsk = tipc_sk_get_next(&ref);
2223 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2224 lock_sock(&tsk->sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002225 str_len += tipc_sk_show(&tsk->port, pb + str_len,
2226 pb_len - str_len, 0);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002227 release_sock(&tsk->sk);
2228 tipc_sk_put(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002229 }
2230 str_len += 1; /* for "\0" */
2231 skb_put(buf, TLV_SPACE(str_len));
2232 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2233
2234 return buf;
2235}
2236
2237/* tipc_sk_reinit: set non-zero address in all existing sockets
2238 * when we go from standalone to network mode.
2239 */
2240void tipc_sk_reinit(void)
2241{
2242 struct tipc_msg *msg;
2243 u32 ref = 0;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002244 struct tipc_sock *tsk = tipc_sk_get_next(&ref);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002245
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002246 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2247 lock_sock(&tsk->sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002248 msg = &tsk->port.phdr;
2249 msg_set_prevnode(msg, tipc_own_addr);
2250 msg_set_orignode(msg, tipc_own_addr);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002251 release_sock(&tsk->sk);
2252 tipc_sk_put(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002253 }
2254}
2255
Per Lidenb97bf3f2006-01-02 19:04:38 +01002256/**
Ying Xue247f0f32014-02-18 16:06:46 +08002257 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002258 * @sock: socket structure
2259 * @lvl: option level
2260 * @opt: option identifier
2261 * @ov: pointer to new option value
2262 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002263 *
2264 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002265 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002266 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002267 * Returns 0 on success, errno otherwise
2268 */
Ying Xue247f0f32014-02-18 16:06:46 +08002269static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2270 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002271{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002272 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002273 struct tipc_sock *tsk = tipc_sk(sk);
2274 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002275 u32 value;
2276 int res;
2277
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002278 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2279 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002280 if (lvl != SOL_TIPC)
2281 return -ENOPROTOOPT;
2282 if (ol < sizeof(value))
2283 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002284 res = get_user(value, (u32 __user *)ov);
2285 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002286 return res;
2287
Allan Stephens0c3141e2008-04-15 00:22:02 -07002288 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002289
Per Lidenb97bf3f2006-01-02 19:04:38 +01002290 switch (opt) {
2291 case TIPC_IMPORTANCE:
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002292 res = tsk_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002293 break;
2294 case TIPC_SRC_DROPPABLE:
2295 if (sock->type != SOCK_STREAM)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002296 tsk_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002297 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002298 res = -ENOPROTOOPT;
2299 break;
2300 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002301 tsk_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002302 break;
2303 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002304 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002305 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002306 break;
2307 default:
2308 res = -EINVAL;
2309 }
2310
Allan Stephens0c3141e2008-04-15 00:22:02 -07002311 release_sock(sk);
2312
Per Lidenb97bf3f2006-01-02 19:04:38 +01002313 return res;
2314}
2315
2316/**
Ying Xue247f0f32014-02-18 16:06:46 +08002317 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002318 * @sock: socket structure
2319 * @lvl: option level
2320 * @opt: option identifier
2321 * @ov: receptacle for option value
2322 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002323 *
2324 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002325 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002326 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002327 * Returns 0 on success, errno otherwise
2328 */
Ying Xue247f0f32014-02-18 16:06:46 +08002329static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2330 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002331{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002332 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002333 struct tipc_sock *tsk = tipc_sk(sk);
2334 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002335 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002336 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002337 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002338
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002339 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2340 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002341 if (lvl != SOL_TIPC)
2342 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002343 res = get_user(len, ol);
2344 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002345 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002346
Allan Stephens0c3141e2008-04-15 00:22:02 -07002347 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002348
2349 switch (opt) {
2350 case TIPC_IMPORTANCE:
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002351 value = tsk_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002352 break;
2353 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002354 value = tsk_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002355 break;
2356 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002357 value = tsk_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002358 break;
2359 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002360 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002361 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002362 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002363 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002364 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002365 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002366 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002367 value = skb_queue_len(&sk->sk_receive_queue);
2368 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002369 default:
2370 res = -EINVAL;
2371 }
2372
Allan Stephens0c3141e2008-04-15 00:22:02 -07002373 release_sock(sk);
2374
Paul Gortmaker25860c32010-12-31 18:59:31 +00002375 if (res)
2376 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002377
Paul Gortmaker25860c32010-12-31 18:59:31 +00002378 if (len < sizeof(value))
2379 return -EINVAL;
2380
2381 if (copy_to_user(ov, &value, sizeof(value)))
2382 return -EFAULT;
2383
2384 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002385}
2386
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002387static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002388{
2389 struct tipc_sioc_ln_req lnr;
2390 void __user *argp = (void __user *)arg;
2391
2392 switch (cmd) {
2393 case SIOCGETLINKNAME:
2394 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2395 return -EFAULT;
2396 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2397 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2398 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2399 return -EFAULT;
2400 return 0;
2401 }
2402 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002403 default:
2404 return -ENOIOCTLCMD;
2405 }
2406}
2407
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002408/* Protocol switches for the various types of TIPC sockets */
2409
Florian Westphalbca65ea2008-02-07 18:18:01 -08002410static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002411 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002412 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002413 .release = tipc_release,
2414 .bind = tipc_bind,
2415 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002416 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002417 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002418 .getname = tipc_getname,
2419 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002420 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002421 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002422 .shutdown = tipc_shutdown,
2423 .setsockopt = tipc_setsockopt,
2424 .getsockopt = tipc_getsockopt,
2425 .sendmsg = tipc_sendmsg,
2426 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002427 .mmap = sock_no_mmap,
2428 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002429};
2430
Florian Westphalbca65ea2008-02-07 18:18:01 -08002431static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002432 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002433 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002434 .release = tipc_release,
2435 .bind = tipc_bind,
2436 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002437 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002438 .accept = tipc_accept,
2439 .getname = tipc_getname,
2440 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002441 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002442 .listen = tipc_listen,
2443 .shutdown = tipc_shutdown,
2444 .setsockopt = tipc_setsockopt,
2445 .getsockopt = tipc_getsockopt,
2446 .sendmsg = tipc_send_packet,
2447 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002448 .mmap = sock_no_mmap,
2449 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002450};
2451
Florian Westphalbca65ea2008-02-07 18:18:01 -08002452static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002453 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002454 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002455 .release = tipc_release,
2456 .bind = tipc_bind,
2457 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002458 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002459 .accept = tipc_accept,
2460 .getname = tipc_getname,
2461 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002462 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002463 .listen = tipc_listen,
2464 .shutdown = tipc_shutdown,
2465 .setsockopt = tipc_setsockopt,
2466 .getsockopt = tipc_getsockopt,
2467 .sendmsg = tipc_send_stream,
2468 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002469 .mmap = sock_no_mmap,
2470 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002471};
2472
Florian Westphalbca65ea2008-02-07 18:18:01 -08002473static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002474 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002475 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002476 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002477};
2478
2479static struct proto tipc_proto = {
2480 .name = "TIPC",
2481 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002482 .obj_size = sizeof(struct tipc_sock),
2483 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002484};
2485
Ying Xuec5fa7b32013-06-17 10:54:39 -04002486static struct proto tipc_proto_kern = {
2487 .name = "TIPC",
2488 .obj_size = sizeof(struct tipc_sock),
2489 .sysctl_rmem = sysctl_tipc_rmem
2490};
2491
Per Lidenb97bf3f2006-01-02 19:04:38 +01002492/**
Per Liden4323add2006-01-18 00:38:21 +01002493 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002494 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002495 * Returns 0 on success, errno otherwise
2496 */
Per Liden4323add2006-01-18 00:38:21 +01002497int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002498{
2499 int res;
2500
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002501 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002502 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002503 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002504 goto out;
2505 }
2506
2507 res = sock_register(&tipc_family_ops);
2508 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002509 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002510 proto_unregister(&tipc_proto);
2511 goto out;
2512 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002513 out:
2514 return res;
2515}
2516
2517/**
Per Liden4323add2006-01-18 00:38:21 +01002518 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002519 */
Per Liden4323add2006-01-18 00:38:21 +01002520void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002521{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002522 sock_unregister(tipc_family_ops.family);
2523 proto_unregister(&tipc_proto);
2524}