blob: ddc2f8c6fced8affafeaa4eab8bed904fb92c401 [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"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.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"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040044
Per Lidenb97bf3f2006-01-02 19:04:38 +010045#define SS_LISTENING -1 /* socket is listening */
46#define SS_READY -2 /* socket is connectionless */
47
Allan Stephens3654ea02008-04-13 21:35:11 -070048#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloydadebc02014-08-22 18:09:11 -040049#define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
Jon Paul Maloyac0074e2014-06-25 20:41:41 -050050#define TIPC_FWD_MSG 1
Per Lidenb97bf3f2006-01-02 19:04:38 +010051
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040052static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040053static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080054static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080055static int tipc_release(struct socket *sock);
56static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -040057static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Jon Paul Maloy57289012014-08-22 18:09:09 -040058static void tipc_sk_timeout(unsigned long ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010059
Florian Westphalbca65ea2008-02-07 18:18:01 -080060static const struct proto_ops packet_ops;
61static const struct proto_ops stream_ops;
62static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010063
64static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040065static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010066
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090067/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070068 * Revised TIPC socket locking policy:
69 *
70 * Most socket operations take the standard socket lock when they start
71 * and hold it until they finish (or until they need to sleep). Acquiring
72 * this lock grants the owner exclusive access to the fields of the socket
73 * data structures, with the exception of the backlog queue. A few socket
74 * operations can be done without taking the socket lock because they only
75 * read socket information that never changes during the life of the socket.
76 *
77 * Socket operations may acquire the lock for the associated TIPC port if they
78 * need to perform an operation on the port. If any routine needs to acquire
79 * both the socket lock and the port lock it must take the socket lock first
80 * to avoid the risk of deadlock.
81 *
82 * The dispatcher handling incoming messages cannot grab the socket lock in
83 * the standard fashion, since invoked it runs at the BH level and cannot block.
84 * Instead, it checks to see if the socket lock is currently owned by someone,
85 * and either handles the message itself or adds it to the socket's backlog
86 * queue; in the latter case the queued message is processed once the process
87 * owning the socket lock releases it.
88 *
89 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
90 * the problem of a blocked socket operation preventing any other operations
91 * from occurring. However, applications must be careful if they have
92 * multiple threads trying to send (or receive) on the same socket, as these
93 * operations might interfere with each other. For example, doing a connect
94 * and a receive at the same time might allow the receive to consume the
95 * ACK message meant for the connect. While additional work could be done
96 * to try and overcome this, it doesn't seem to be worthwhile at the present.
97 *
98 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
99 * that another operation that must be performed in a non-blocking manner is
100 * not delayed for very long because the lock has already been taken.
101 *
102 * NOTE: This code assumes that certain fields of a port/socket pair are
103 * constant over its lifetime; such fields can be examined without taking
104 * the socket lock and/or port lock, and do not need to be re-read even
105 * after resuming processing after waiting. These fields include:
106 * - socket type
107 * - pointer to socket sk structure (aka tipc_sock structure)
108 * - pointer to port structure
109 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100110 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400112#include "socket.h"
113
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -0400114/* tipc_sk_lock_next: find & lock next socket in registry from given port number
115*/
116static struct tipc_sock *tipc_sk_lock_next(u32 *ref)
117{
118 struct tipc_port *port = (struct tipc_port *)tipc_ref_lock_next(ref);
119
120 if (!port)
121 return NULL;
122 return tipc_port_to_sock(port);
123}
124
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700126 * advance_rx_queue - discard first buffer in socket receive queue
127 *
128 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700130static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400132 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133}
134
135/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700136 * reject_rx_queue - reject all buffers in socket receive queue
137 *
138 * Caller must hold socket lock
139 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700140static void reject_rx_queue(struct sock *sk)
141{
142 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500143 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700144
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500145 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
146 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400147 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500148 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700149}
150
151/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400152 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700153 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 * @sock: pre-allocated socket structure
155 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800156 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900157 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700158 * This routine creates additional data structures used by the TIPC socket,
159 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 *
161 * Returns 0 on success, errno otherwise
162 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400163static int tipc_sk_create(struct net *net, struct socket *sock,
164 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700166 const struct proto_ops *ops;
167 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400169 struct tipc_sock *tsk;
170 struct tipc_port *port;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400171 struct tipc_msg *msg;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400172 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700173
174 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 if (unlikely(protocol != 0))
176 return -EPROTONOSUPPORT;
177
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 switch (sock->type) {
179 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700180 ops = &stream_ops;
181 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 break;
183 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700184 ops = &packet_ops;
185 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 break;
187 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100188 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700189 ops = &msg_ops;
190 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 break;
Allan Stephens49978652006-06-25 23:47:18 -0700192 default:
Allan Stephens49978652006-06-25 23:47:18 -0700193 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194 }
195
Allan Stephens0c3141e2008-04-15 00:22:02 -0700196 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400197 if (!kern)
198 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
199 else
200 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
201
Allan Stephens0c3141e2008-04-15 00:22:02 -0700202 if (sk == NULL)
203 return -ENOMEM;
204
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400205 tsk = tipc_sk(sk);
206 port = &tsk->port;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400207 ref = tipc_ref_acquire(port, &port->lock);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400208 if (!ref) {
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400209 pr_warn("Socket create failed; reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 return -ENOMEM;
211 }
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400212 port->max_pkt = MAX_PKT_DEFAULT;
213 port->ref = ref;
214 INIT_LIST_HEAD(&port->publications);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400215
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400216 msg = &port->phdr;
217 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
218 NAMED_H_SIZE, 0);
219 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220
Allan Stephens0c3141e2008-04-15 00:22:02 -0700221 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700222 sock->ops = ops;
223 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 sock_init_data(sock, sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -0400225 k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400226 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400227 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800228 sk->sk_data_ready = tipc_data_ready;
229 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400230 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500231 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400232 atomic_set(&tsk->dupl_rcvcnt, 0);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400233 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700234
Allan Stephens0c3141e2008-04-15 00:22:02 -0700235 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400236 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700237 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400238 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700239 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 return 0;
241}
242
243/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400244 * tipc_sock_create_local - create TIPC socket from inside TIPC module
245 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
246 *
247 * We cannot use sock_creat_kern here because it bumps module user count.
248 * Since socket owner and creator is the same module we must make sure
249 * that module count remains zero for module local sockets, otherwise
250 * we cannot do rmmod.
251 *
252 * Returns 0 on success, errno otherwise
253 */
254int tipc_sock_create_local(int type, struct socket **res)
255{
256 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400257
258 rc = sock_create_lite(AF_TIPC, type, 0, res);
259 if (rc < 0) {
260 pr_err("Failed to create kernel socket\n");
261 return rc;
262 }
263 tipc_sk_create(&init_net, *res, 0, 1);
264
Ying Xuec5fa7b32013-06-17 10:54:39 -0400265 return 0;
266}
267
268/**
269 * tipc_sock_release_local - release socket created by tipc_sock_create_local
270 * @sock: the socket to be released.
271 *
272 * Module reference count is not incremented when such sockets are created,
273 * so we must keep it from being decremented when they are released.
274 */
275void tipc_sock_release_local(struct socket *sock)
276{
Ying Xue247f0f32014-02-18 16:06:46 +0800277 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400278 sock->ops = NULL;
279 sock_release(sock);
280}
281
282/**
283 * tipc_sock_accept_local - accept a connection on a socket created
284 * with tipc_sock_create_local. Use this function to avoid that
285 * module reference count is inadvertently incremented.
286 *
287 * @sock: the accepting socket
288 * @newsock: reference to the new socket to be created
289 * @flags: socket flags
290 */
291
292int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400293 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400294{
295 struct sock *sk = sock->sk;
296 int ret;
297
298 ret = sock_create_lite(sk->sk_family, sk->sk_type,
299 sk->sk_protocol, newsock);
300 if (ret < 0)
301 return ret;
302
Ying Xue247f0f32014-02-18 16:06:46 +0800303 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400304 if (ret < 0) {
305 sock_release(*newsock);
306 return ret;
307 }
308 (*newsock)->ops = sock->ops;
309 return ret;
310}
311
312/**
Ying Xue247f0f32014-02-18 16:06:46 +0800313 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 * @sock: socket to destroy
315 *
316 * This routine cleans up any messages that are still queued on the socket.
317 * For DGRAM and RDM socket types, all queued messages are rejected.
318 * For SEQPACKET and STREAM socket types, the first message is rejected
319 * and any others are discarded. (If the first message on a STREAM socket
320 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900321 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 * NOTE: Rejected messages are not necessarily returned to the sender! They
323 * are returned or discarded according to the "destination droppable" setting
324 * specified for the message by the sender.
325 *
326 * Returns 0 on success, errno otherwise
327 */
Ying Xue247f0f32014-02-18 16:06:46 +0800328static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400331 struct tipc_sock *tsk;
332 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500334 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335
Allan Stephens0c3141e2008-04-15 00:22:02 -0700336 /*
337 * Exit if socket isn't fully initialized (occurs when a failed accept()
338 * releases a pre-allocated child socket that was never used)
339 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700340 if (sk == NULL)
341 return 0;
342
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400343 tsk = tipc_sk(sk);
344 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 lock_sock(sk);
346
347 /*
348 * Reject all unreceived messages, except on an active connection
349 * (which disconnects locally & sends a 'FIN+' to peer)
350 */
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400351 dnode = tipc_port_peernode(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 buf = __skb_dequeue(&sk->sk_receive_queue);
354 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 break;
Ying Xue40682432013-10-18 07:23:16 +0200356 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400357 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700358 else {
359 if ((sock->state == SS_CONNECTING) ||
360 (sock->state == SS_CONNECTED)) {
361 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -0400362 port->connected = 0;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400363 tipc_node_remove_conn(dnode, port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700364 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500365 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400366 tipc_link_xmit(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700367 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 }
369
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400370 tipc_withdraw(port, 0, NULL);
371 spin_lock_bh(port->lock);
372 tipc_ref_discard(port->ref);
373 spin_unlock_bh(port->lock);
374 k_cancel_timer(&port->timer);
375 if (port->connected) {
376 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
377 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
378 tipc_port_peerport(port),
379 port->ref, TIPC_ERR_NO_PORT);
380 if (buf)
381 tipc_link_xmit(buf, dnode, port->ref);
382 tipc_node_remove_conn(dnode, port->ref);
383 }
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400384 k_term_timer(&port->timer);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385
Allan Stephens0c3141e2008-04-15 00:22:02 -0700386 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100387 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388
Allan Stephens0c3141e2008-04-15 00:22:02 -0700389 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700390 sock->state = SS_DISCONNECTING;
391 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700393 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200395 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396}
397
398/**
Ying Xue247f0f32014-02-18 16:06:46 +0800399 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 * @sock: socket structure
401 * @uaddr: socket address describing name(s) and desired operation
402 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900403 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 * Name and name sequence binding is indicated using a positive scope value;
405 * a negative scope value unbinds the specified name. Specifying no name
406 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900407 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700409 *
410 * NOTE: This routine doesn't need to take the socket lock since it doesn't
411 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 */
Ying Xue247f0f32014-02-18 16:06:46 +0800413static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
414 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415{
Ying Xue84602762013-12-27 10:18:28 +0800416 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400418 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800419 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420
Ying Xue84602762013-12-27 10:18:28 +0800421 lock_sock(sk);
422 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400423 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800424 goto exit;
425 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900426
Ying Xue84602762013-12-27 10:18:28 +0800427 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
428 res = -EINVAL;
429 goto exit;
430 }
431 if (addr->family != AF_TIPC) {
432 res = -EAFNOSUPPORT;
433 goto exit;
434 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 if (addr->addrtype == TIPC_ADDR_NAME)
437 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800438 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
439 res = -EAFNOSUPPORT;
440 goto exit;
441 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900442
Ying Xue13a2e892013-06-17 10:54:40 -0400443 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400444 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800445 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
446 res = -EACCES;
447 goto exit;
448 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400449
Ying Xue84602762013-12-27 10:18:28 +0800450 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400451 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
452 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800453exit:
454 release_sock(sk);
455 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456}
457
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900458/**
Ying Xue247f0f32014-02-18 16:06:46 +0800459 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 * @sock: socket structure
461 * @uaddr: area for returned socket address
462 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700463 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900464 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700466 *
Allan Stephens2da59912008-07-14 22:43:32 -0700467 * NOTE: This routine doesn't need to take the socket lock since it only
468 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000469 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 */
Ying Xue247f0f32014-02-18 16:06:46 +0800471static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
472 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400475 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000477 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700478 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700479 if ((sock->state != SS_CONNECTED) &&
480 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
481 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400482 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
483 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700484 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400485 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000486 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700487 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488
489 *uaddr_len = sizeof(*addr);
490 addr->addrtype = TIPC_ADDR_ID;
491 addr->family = AF_TIPC;
492 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 addr->addr.name.domain = 0;
494
Allan Stephens0c3141e2008-04-15 00:22:02 -0700495 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496}
497
498/**
Ying Xue247f0f32014-02-18 16:06:46 +0800499 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500 * @file: file structure associated with the socket
501 * @sock: socket for which to calculate the poll bits
502 * @wait: ???
503 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700504 * Returns pollmask value
505 *
506 * COMMENTARY:
507 * It appears that the usual socket locking mechanisms are not useful here
508 * since the pollmask info is potentially out-of-date the moment this routine
509 * exits. TCP and other protocols seem to rely on higher level poll routines
510 * to handle any preventable race conditions, so TIPC will do the same ...
511 *
512 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700513 *
Allan Stephensf662c072010-08-17 11:00:06 +0000514 * socket state flags set
515 * ------------ ---------
516 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200517 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000518 *
519 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
520 * no write flags
521 *
522 * connected POLLIN/POLLRDNORM if data in rx queue
523 * POLLOUT if port is not congested
524 *
525 * disconnecting POLLIN/POLLRDNORM/POLLHUP
526 * no write flags
527 *
528 * listening POLLIN if SYN in rx queue
529 * no write flags
530 *
531 * ready POLLIN/POLLRDNORM if data in rx queue
532 * [connectionless] POLLOUT (since port cannot be congested)
533 *
534 * IMPORTANT: The fact that a read or write operation is indicated does NOT
535 * imply that the operation will succeed, merely that it should be performed
536 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 */
Ying Xue247f0f32014-02-18 16:06:46 +0800538static unsigned int tipc_poll(struct file *file, struct socket *sock,
539 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540{
Allan Stephens9b674e82008-03-26 16:48:21 -0700541 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400542 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000543 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700544
Ying Xuef288bef2012-08-21 11:16:57 +0800545 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700546
Allan Stephensf662c072010-08-17 11:00:06 +0000547 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200548 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500549 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200550 mask |= POLLOUT;
551 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000552 case SS_READY:
553 case SS_CONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500554 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000555 mask |= POLLOUT;
556 /* fall thru' */
557 case SS_CONNECTING:
558 case SS_LISTENING:
559 if (!skb_queue_empty(&sk->sk_receive_queue))
560 mask |= (POLLIN | POLLRDNORM);
561 break;
562 case SS_DISCONNECTING:
563 mask = (POLLIN | POLLRDNORM | POLLHUP);
564 break;
565 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700566
567 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568}
569
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400570/**
571 * tipc_sendmcast - send multicast message
572 * @sock: socket structure
573 * @seq: destination address
574 * @iov: message data to send
575 * @dsz: total length of message data
576 * @timeo: timeout to wait for wakeup
577 *
578 * Called from function tipc_sendmsg(), which has done all sanity checks
579 * Returns the number of bytes sent on success, or errno
580 */
581static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
582 struct iovec *iov, size_t dsz, long timeo)
583{
584 struct sock *sk = sock->sk;
585 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
586 struct sk_buff *buf;
587 uint mtu;
588 int rc;
589
590 msg_set_type(mhdr, TIPC_MCAST_MSG);
591 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
592 msg_set_destport(mhdr, 0);
593 msg_set_destnode(mhdr, 0);
594 msg_set_nametype(mhdr, seq->type);
595 msg_set_namelower(mhdr, seq->lower);
596 msg_set_nameupper(mhdr, seq->upper);
597 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
598
599new_mtu:
600 mtu = tipc_bclink_get_mtu();
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400601 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400602 if (unlikely(rc < 0))
603 return rc;
604
605 do {
606 rc = tipc_bclink_xmit(buf);
607 if (likely(rc >= 0)) {
608 rc = dsz;
609 break;
610 }
611 if (rc == -EMSGSIZE)
612 goto new_mtu;
613 if (rc != -ELINKCONG)
614 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400615 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400616 rc = tipc_wait_for_sndmsg(sock, &timeo);
617 if (rc)
618 kfree_skb_list(buf);
619 } while (!rc);
620 return rc;
621}
622
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400623/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
624 */
625void tipc_sk_mcast_rcv(struct sk_buff *buf)
626{
627 struct tipc_msg *msg = buf_msg(buf);
628 struct tipc_port_list dports = {0, NULL, };
629 struct tipc_port_list *item;
630 struct sk_buff *b;
631 uint i, last, dst = 0;
632 u32 scope = TIPC_CLUSTER_SCOPE;
633
634 if (in_own_node(msg_orignode(msg)))
635 scope = TIPC_NODE_SCOPE;
636
637 /* Create destination port list: */
638 tipc_nametbl_mc_translate(msg_nametype(msg),
639 msg_namelower(msg),
640 msg_nameupper(msg),
641 scope,
642 &dports);
643 last = dports.count;
644 if (!last) {
645 kfree_skb(buf);
646 return;
647 }
648
649 for (item = &dports; item; item = item->next) {
650 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
651 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
652 if (!b) {
653 pr_warn("Failed do clone mcast rcv buffer\n");
654 continue;
655 }
656 msg_set_destport(msg, item->ports[i]);
657 tipc_sk_rcv(b);
658 }
659 }
660 tipc_port_list_free(&dports);
661}
662
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900663/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500664 * tipc_sk_proto_rcv - receive a connection mng protocol message
665 * @tsk: receiving socket
666 * @dnode: node to send response message to, if any
667 * @buf: buffer containing protocol message
668 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
669 * (CONN_PROBE_REPLY) message should be forwarded.
670 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800671static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
672 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500673{
674 struct tipc_msg *msg = buf_msg(buf);
675 struct tipc_port *port = &tsk->port;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500676 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500677
678 /* Ignore if connection cannot be validated: */
679 if (!port->connected || !tipc_port_peer_msg(port, msg))
680 goto exit;
681
682 port->probing_state = TIPC_CONN_OK;
683
684 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500685 conn_cong = tipc_sk_conn_cong(tsk);
686 tsk->sent_unacked -= msg_msgcnt(msg);
687 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400688 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500689 } else if (msg_type(msg) == CONN_PROBE) {
690 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
691 return TIPC_OK;
692 msg_set_type(msg, CONN_PROBE_REPLY);
693 return TIPC_FWD_MSG;
694 }
695 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
696exit:
697 kfree_skb(buf);
698 return TIPC_OK;
699}
700
701/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702 * dest_name_check - verify user is permitted to send to specified port name
703 * @dest: destination address
704 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900705 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706 * Prevents restricted configuration commands from being issued by
707 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900708 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709 * Returns 0 if permission is granted, otherwise errno
710 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800711static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712{
713 struct tipc_cfg_msg_hdr hdr;
714
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500715 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
716 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900717 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
718 return 0;
719 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
720 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900721 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
722 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100723
Allan Stephens3f8dd942011-01-18 13:09:29 -0500724 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
725 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900726 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700728 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 return 0;
732}
733
Ying Xue3f405042014-01-17 09:50:05 +0800734static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
735{
736 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400737 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800738 DEFINE_WAIT(wait);
739 int done;
740
741 do {
742 int err = sock_error(sk);
743 if (err)
744 return err;
745 if (sock->state == SS_DISCONNECTING)
746 return -EPIPE;
747 if (!*timeo_p)
748 return -EAGAIN;
749 if (signal_pending(current))
750 return sock_intr_errno(*timeo_p);
751
752 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500753 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800754 finish_wait(sk_sleep(sk), &wait);
755 } while (!done);
756 return 0;
757}
758
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500759/**
Ying Xue247f0f32014-02-18 16:06:46 +0800760 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700761 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762 * @sock: socket structure
763 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500764 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900765 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900767 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
769 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900770 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 * Returns the number of bytes sent on success, or errno otherwise
772 */
Ying Xue247f0f32014-02-18 16:06:46 +0800773static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500774 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500776 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700777 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400778 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400779 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500780 struct tipc_msg *mhdr = &port->phdr;
781 struct iovec *iov = m->msg_iov;
782 u32 dnode, dport;
783 struct sk_buff *buf;
784 struct tipc_name_seq *seq = &dest->addr.nameseq;
785 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800786 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500787 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788
789 if (unlikely(!dest))
790 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500791
Allan Stephens51f9cc12006-06-25 23:49:06 -0700792 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
793 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500795
796 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400797 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798
Allan Stephens0c3141e2008-04-15 00:22:02 -0700799 if (iocb)
800 lock_sock(sk);
801
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500802 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700803 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500804 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700805 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700806 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700807 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500808 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700809 goto exit;
810 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400811 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500812 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700813 goto exit;
814 }
815 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400816 tsk->port.conn_type = dest->addr.name.name.type;
817 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700818 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500820 rc = dest_name_check(dest, m);
821 if (rc)
822 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823
Ying Xue3f405042014-01-17 09:50:05 +0800824 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500825
826 if (dest->addrtype == TIPC_ADDR_MCAST) {
827 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
828 goto exit;
829 } else if (dest->addrtype == TIPC_ADDR_NAME) {
830 u32 type = dest->addr.name.name.type;
831 u32 inst = dest->addr.name.name.instance;
832 u32 domain = dest->addr.name.domain;
833
834 dnode = domain;
835 msg_set_type(mhdr, TIPC_NAMED_MSG);
836 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
837 msg_set_nametype(mhdr, type);
838 msg_set_nameinst(mhdr, inst);
839 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
840 dport = tipc_nametbl_translate(type, inst, &dnode);
841 msg_set_destnode(mhdr, dnode);
842 msg_set_destport(mhdr, dport);
843 if (unlikely(!dport && !dnode)) {
844 rc = -EHOSTUNREACH;
845 goto exit;
846 }
847 } else if (dest->addrtype == TIPC_ADDR_ID) {
848 dnode = dest->addr.id.node;
849 msg_set_type(mhdr, TIPC_DIRECT_MSG);
850 msg_set_lookup_scope(mhdr, 0);
851 msg_set_destnode(mhdr, dnode);
852 msg_set_destport(mhdr, dest->addr.id.ref);
853 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
854 }
855
856new_mtu:
857 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400858 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500859 if (rc < 0)
860 goto exit;
861
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900862 do {
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400863 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400864 rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500865 if (likely(rc >= 0)) {
866 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700867 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500868 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700869 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900870 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500871 if (rc == -EMSGSIZE)
872 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500873 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700874 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400875 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500876 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400877 if (rc)
878 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500879 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700880exit:
881 if (iocb)
882 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500883
884 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885}
886
Ying Xue391a6dd2014-01-17 09:50:06 +0800887static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
888{
889 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400890 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800891 DEFINE_WAIT(wait);
892 int done;
893
894 do {
895 int err = sock_error(sk);
896 if (err)
897 return err;
898 if (sock->state == SS_DISCONNECTING)
899 return -EPIPE;
900 else if (sock->state != SS_CONNECTED)
901 return -ENOTCONN;
902 if (!*timeo_p)
903 return -EAGAIN;
904 if (signal_pending(current))
905 return sock_intr_errno(*timeo_p);
906
907 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
908 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500909 (!tsk->link_cong &&
910 !tipc_sk_conn_cong(tsk)) ||
911 !tsk->port.connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800912 finish_wait(sk_sleep(sk), &wait);
913 } while (!done);
914 return 0;
915}
916
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900917/**
Ying Xue247f0f32014-02-18 16:06:46 +0800918 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919 * @iocb: (unused)
920 * @sock: socket structure
921 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500922 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900923 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900925 *
926 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700927 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 */
Ying Xue247f0f32014-02-18 16:06:46 +0800929static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500930 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700932 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400933 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500934 struct tipc_port *port = &tsk->port;
935 struct tipc_msg *mhdr = &port->phdr;
936 struct sk_buff *buf;
937 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
938 u32 ref = port->ref;
939 int rc = -EINVAL;
940 long timeo;
941 u32 dnode;
942 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900943
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500944 /* Handle implied connection establishment */
945 if (unlikely(dest)) {
946 rc = tipc_sendmsg(iocb, sock, m, dsz);
947 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -0500948 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500949 return rc;
950 }
951 if (dsz > (uint)INT_MAX)
952 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700953
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500954 if (iocb)
955 lock_sock(sk);
956
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900957 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500958 if (sock->state == SS_DISCONNECTING)
959 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +0800960 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500961 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800962 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900963 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500965 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
966 dnode = tipc_port_peernode(port);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500967
968next:
969 mtu = port->max_pkt;
970 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400971 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500972 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700973 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500974 do {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500975 if (likely(!tipc_sk_conn_cong(tsk))) {
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400976 rc = tipc_link_xmit(buf, dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500977 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500978 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500979 sent += send;
980 if (sent == dsz)
981 break;
982 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700983 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500984 if (rc == -EMSGSIZE) {
985 port->max_pkt = tipc_node_get_mtu(dnode, ref);
986 goto next;
987 }
988 if (rc != -ELINKCONG)
989 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400990 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500992 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400993 if (rc)
994 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500995 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700996exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500997 if (iocb)
998 release_sock(sk);
999 return sent ? sent : rc;
1000}
1001
1002/**
1003 * tipc_send_packet - send a connection-oriented message
1004 * @iocb: if NULL, indicates that socket lock is already held
1005 * @sock: socket structure
1006 * @m: message to send
1007 * @dsz: length of data to be transmitted
1008 *
1009 * Used for SOCK_SEQPACKET messages.
1010 *
1011 * Returns the number of bytes sent on success, or errno otherwise
1012 */
1013static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1014 struct msghdr *m, size_t dsz)
1015{
1016 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1017 return -EMSGSIZE;
1018
1019 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020}
1021
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001022/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001024static void tipc_sk_finish_conn(struct tipc_port *port, u32 peer_port,
1025 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026{
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001027 struct tipc_msg *msg = &port->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001029 msg_set_destnode(msg, peer_node);
1030 msg_set_destport(msg, peer_port);
1031 msg_set_type(msg, TIPC_CONN_MSG);
1032 msg_set_lookup_scope(msg, 0);
1033 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001034
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001035 port->probing_interval = CONN_PROBING_INTERVAL;
1036 port->probing_state = TIPC_CONN_OK;
1037 port->connected = 1;
1038 k_start_timer(&port->timer, port->probing_interval);
1039 tipc_node_add_conn(peer_node, port->ref, peer_port);
1040 port->max_pkt = tipc_node_get_mtu(peer_node, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041}
1042
1043/**
1044 * set_orig_addr - capture sender's address for received message
1045 * @m: descriptor for message info
1046 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001047 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 * Note: Address is not captured if not requested by receiver.
1049 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001050static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001052 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001053
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001054 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 addr->family = AF_TIPC;
1056 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001057 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058 addr->addr.id.ref = msg_origport(msg);
1059 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001060 addr->addr.name.domain = 0; /* could leave uninitialized */
1061 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 m->msg_namelen = sizeof(struct sockaddr_tipc);
1063 }
1064}
1065
1066/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001067 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 * @m: descriptor for message info
1069 * @msg: received message header
1070 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001071 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001073 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 * Returns 0 if successful, otherwise errno
1075 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001076static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001077 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078{
1079 u32 anc_data[3];
1080 u32 err;
1081 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001082 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 int res;
1084
1085 if (likely(m->msg_controllen == 0))
1086 return 0;
1087
1088 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 err = msg ? msg_errcode(msg) : 0;
1090 if (unlikely(err)) {
1091 anc_data[0] = err;
1092 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001093 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1094 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001096 if (anc_data[1]) {
1097 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1098 msg_data(msg));
1099 if (res)
1100 return res;
1101 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001102 }
1103
1104 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1106 switch (dest_type) {
1107 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001108 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109 anc_data[0] = msg_nametype(msg);
1110 anc_data[1] = msg_namelower(msg);
1111 anc_data[2] = msg_namelower(msg);
1112 break;
1113 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001114 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 anc_data[0] = msg_nametype(msg);
1116 anc_data[1] = msg_namelower(msg);
1117 anc_data[2] = msg_nameupper(msg);
1118 break;
1119 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001120 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 anc_data[0] = tport->conn_type;
1122 anc_data[1] = tport->conn_instance;
1123 anc_data[2] = tport->conn_instance;
1124 break;
1125 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001126 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 }
Allan Stephens2db99832010-12-31 18:59:33 +00001128 if (has_name) {
1129 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1130 if (res)
1131 return res;
1132 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133
1134 return 0;
1135}
1136
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001137static void tipc_sk_send_ack(struct tipc_port *port, uint ack)
1138{
1139 struct sk_buff *buf = NULL;
1140 struct tipc_msg *msg;
1141 u32 peer_port = tipc_port_peerport(port);
1142 u32 dnode = tipc_port_peernode(port);
1143
1144 if (!port->connected)
1145 return;
1146 buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
1147 tipc_own_addr, peer_port, port->ref, TIPC_OK);
1148 if (!buf)
1149 return;
1150 msg = buf_msg(buf);
1151 msg_set_msgcnt(msg, ack);
1152 tipc_link_xmit(buf, dnode, msg_link_selector(msg));
1153}
1154
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001155static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001156{
1157 struct sock *sk = sock->sk;
1158 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001159 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001160 int err;
1161
1162 for (;;) {
1163 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001164 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001165 if (sock->state == SS_DISCONNECTING) {
1166 err = -ENOTCONN;
1167 break;
1168 }
1169 release_sock(sk);
1170 timeo = schedule_timeout(timeo);
1171 lock_sock(sk);
1172 }
1173 err = 0;
1174 if (!skb_queue_empty(&sk->sk_receive_queue))
1175 break;
1176 err = sock_intr_errno(timeo);
1177 if (signal_pending(current))
1178 break;
1179 err = -EAGAIN;
1180 if (!timeo)
1181 break;
1182 }
1183 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001184 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001185 return err;
1186}
1187
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001188/**
Ying Xue247f0f32014-02-18 16:06:46 +08001189 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 * @iocb: (unused)
1191 * @m: descriptor for message info
1192 * @buf_len: total size of user buffer area
1193 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001194 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1196 * If the complete message doesn't fit in user area, truncate it.
1197 *
1198 * Returns size of returned message data, errno otherwise
1199 */
Ying Xue247f0f32014-02-18 16:06:46 +08001200static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1201 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001203 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001204 struct tipc_sock *tsk = tipc_sk(sk);
1205 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 struct sk_buff *buf;
1207 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001208 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 unsigned int sz;
1210 u32 err;
1211 int res;
1212
Allan Stephens0c3141e2008-04-15 00:22:02 -07001213 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 if (unlikely(!buf_len))
1215 return -EINVAL;
1216
Allan Stephens0c3141e2008-04-15 00:22:02 -07001217 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218
Allan Stephens0c3141e2008-04-15 00:22:02 -07001219 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 res = -ENOTCONN;
1221 goto exit;
1222 }
1223
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001224 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001225restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226
Allan Stephens0c3141e2008-04-15 00:22:02 -07001227 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001228 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001229 if (res)
1230 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001231
1232 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001233 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 msg = buf_msg(buf);
1235 sz = msg_data_sz(msg);
1236 err = msg_errcode(msg);
1237
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001240 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 goto restart;
1242 }
1243
1244 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245 set_orig_addr(m, msg);
1246
1247 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001248 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001249 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250 goto exit;
1251
1252 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 if (!err) {
1254 if (unlikely(buf_len < sz)) {
1255 sz = buf_len;
1256 m->msg_flags |= MSG_TRUNC;
1257 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001258 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1259 m->msg_iov, sz);
1260 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 res = sz;
1263 } else {
1264 if ((sock->state == SS_READY) ||
1265 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1266 res = 0;
1267 else
1268 res = -ECONNRESET;
1269 }
1270
1271 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001273 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001274 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001275 tipc_sk_send_ack(port, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001276 tsk->rcv_unacked = 0;
1277 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001278 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001279 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001281 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282 return res;
1283}
1284
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001285/**
Ying Xue247f0f32014-02-18 16:06:46 +08001286 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001287 * @iocb: (unused)
1288 * @m: descriptor for message info
1289 * @buf_len: total size of user buffer area
1290 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001291 *
1292 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 * will optionally wait for more; never truncates data.
1294 *
1295 * Returns size of returned message data, errno otherwise
1296 */
Ying Xue247f0f32014-02-18 16:06:46 +08001297static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1298 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001299{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001300 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001301 struct tipc_sock *tsk = tipc_sk(sk);
1302 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303 struct sk_buff *buf;
1304 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001305 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001307 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001310 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311
1312 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313 if (unlikely(!buf_len))
1314 return -EINVAL;
1315
Allan Stephens0c3141e2008-04-15 00:22:02 -07001316 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001318 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 res = -ENOTCONN;
1320 goto exit;
1321 }
1322
Florian Westphal3720d402010-08-17 11:00:04 +00001323 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001324 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001325
Allan Stephens0c3141e2008-04-15 00:22:02 -07001326restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001327 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001328 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001329 if (res)
1330 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001331
1332 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001333 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 msg = buf_msg(buf);
1335 sz = msg_data_sz(msg);
1336 err = msg_errcode(msg);
1337
1338 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001340 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 goto restart;
1342 }
1343
1344 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345 if (sz_copied == 0) {
1346 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001347 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001348 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 goto exit;
1350 }
1351
1352 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001354 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355
Allan Stephens0232fd02011-02-21 09:45:40 -05001356 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 needed = (buf_len - sz_copied);
1358 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001359
1360 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1361 m->msg_iov, sz_to_copy);
1362 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001364
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 sz_copied += sz_to_copy;
1366
1367 if (sz_to_copy < sz) {
1368 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001369 TIPC_SKB_CB(buf)->handle =
1370 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 goto exit;
1372 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001373 } else {
1374 if (sz_copied != 0)
1375 goto exit; /* can't add error msg to valid data */
1376
1377 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1378 res = 0;
1379 else
1380 res = -ECONNRESET;
1381 }
1382
1383 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001385 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001386 tipc_sk_send_ack(port, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001387 tsk->rcv_unacked = 0;
1388 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001389 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001390 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391
1392 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001393 if ((sz_copied < buf_len) && /* didn't get all requested data */
1394 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001395 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001396 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1397 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 goto restart;
1399
1400exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001401 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001402 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403}
1404
1405/**
Ying Xuef288bef2012-08-21 11:16:57 +08001406 * tipc_write_space - wake up thread if port congestion is released
1407 * @sk: socket
1408 */
1409static void tipc_write_space(struct sock *sk)
1410{
1411 struct socket_wq *wq;
1412
1413 rcu_read_lock();
1414 wq = rcu_dereference(sk->sk_wq);
1415 if (wq_has_sleeper(wq))
1416 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1417 POLLWRNORM | POLLWRBAND);
1418 rcu_read_unlock();
1419}
1420
1421/**
1422 * tipc_data_ready - wake up threads to indicate messages have been received
1423 * @sk: socket
1424 * @len: the length of messages
1425 */
David S. Miller676d2362014-04-11 16:15:36 -04001426static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001427{
1428 struct socket_wq *wq;
1429
1430 rcu_read_lock();
1431 wq = rcu_dereference(sk->sk_wq);
1432 if (wq_has_sleeper(wq))
1433 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1434 POLLRDNORM | POLLRDBAND);
1435 rcu_read_unlock();
1436}
1437
1438/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001439 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001440 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001441 * @msg: message
1442 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001443 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001444 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001445static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001446{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001447 struct sock *sk = &tsk->sk;
1448 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001449 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001450 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001451
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001452 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001453
1454 if (msg_mcast(msg))
1455 return retval;
1456
1457 switch ((int)sock->state) {
1458 case SS_CONNECTED:
1459 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001460 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001461 if (unlikely(msg_errcode(msg))) {
1462 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001463 port->connected = 0;
1464 /* let timer expire on it's own */
1465 tipc_node_remove_conn(tipc_port_peernode(port),
1466 port->ref);
Ying Xue7e6c1312012-11-29 18:39:14 -05001467 }
1468 retval = TIPC_OK;
1469 }
1470 break;
1471 case SS_CONNECTING:
1472 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001473
1474 if (unlikely(!msg_connected(msg)))
1475 break;
1476
Ying Xue584d24b2012-11-29 18:51:19 -05001477 if (unlikely(msg_errcode(msg))) {
1478 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001479 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001480 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001481 break;
1482 }
1483
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001484 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001485 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001486 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001487 retval = TIPC_OK;
1488 break;
1489 }
1490
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001491 tipc_sk_finish_conn(port, msg_origport(msg), msg_orignode(msg));
1492 msg_set_importance(&port->phdr, msg_importance(msg));
1493 sock->state = SS_CONNECTED;
1494
Ying Xue584d24b2012-11-29 18:51:19 -05001495 /* If an incoming message is an 'ACK-', it should be
1496 * discarded here because it doesn't contain useful
1497 * data. In addition, we should try to wake up
1498 * connect() routine if sleeping.
1499 */
1500 if (msg_data_sz(msg) == 0) {
1501 kfree_skb(*buf);
1502 *buf = NULL;
1503 if (waitqueue_active(sk_sleep(sk)))
1504 wake_up_interruptible(sk_sleep(sk));
1505 }
1506 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001507 break;
1508 case SS_LISTENING:
1509 case SS_UNCONNECTED:
1510 /* Accept only SYN message */
1511 if (!msg_connected(msg) && !(msg_errcode(msg)))
1512 retval = TIPC_OK;
1513 break;
1514 case SS_DISCONNECTING:
1515 break;
1516 default:
1517 pr_err("Unknown socket state %u\n", sock->state);
1518 }
1519 return retval;
1520}
1521
1522/**
Ying Xueaba79f32013-01-20 23:30:09 +01001523 * rcvbuf_limit - get proper overload limit of socket receive queue
1524 * @sk: socket
1525 * @buf: message
1526 *
1527 * For all connection oriented messages, irrespective of importance,
1528 * the default overload value (i.e. 67MB) is set as limit.
1529 *
1530 * For all connectionless messages, by default new queue limits are
1531 * as belows:
1532 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001533 * TIPC_LOW_IMPORTANCE (4 MB)
1534 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1535 * TIPC_HIGH_IMPORTANCE (16 MB)
1536 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001537 *
1538 * Returns overload limit according to corresponding message importance
1539 */
1540static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1541{
1542 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001543
1544 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001545 return sysctl_tipc_rmem[2];
1546
1547 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1548 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001549}
1550
1551/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001552 * filter_rcv - validate incoming message
1553 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001555 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001556 * Enqueues message on receive queue if acceptable; optionally handles
1557 * disconnect indication for a connected socket.
1558 *
1559 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001560 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001561 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001562 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001564static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001566 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001567 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001568 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001569 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001570 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001571 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001572
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001573 if (unlikely(msg_user(msg) == CONN_MANAGER))
1574 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001575
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001576 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1577 kfree_skb(buf);
1578 tsk->link_cong = 0;
1579 sk->sk_write_space(sk);
1580 return TIPC_OK;
1581 }
1582
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001584 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001585 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001586
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001588 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001589 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001591 rc = filter_connect(tsk, &buf);
1592 if (rc != TIPC_OK || buf == NULL)
1593 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594 }
1595
1596 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001597 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001598 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001599
Ying Xueaba79f32013-01-20 23:30:09 +01001600 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001601 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001602 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001603 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001604
David S. Miller676d2362014-04-11 16:15:36 -04001605 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 return TIPC_OK;
1607}
1608
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001609/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001610 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001611 * @sk: socket
1612 * @buf: message
1613 *
1614 * Caller must hold socket lock, but not port lock.
1615 *
1616 * Returns 0
1617 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001618static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001619{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001620 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001621 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001622 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001623 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001624
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001625 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001626
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001627 if (likely(!rc)) {
1628 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1629 atomic_add(truesize, &tsk->dupl_rcvcnt);
1630 return 0;
1631 }
1632
1633 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1634 return 0;
1635
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001636 tipc_link_xmit(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001637
Allan Stephens0c3141e2008-04-15 00:22:02 -07001638 return 0;
1639}
1640
1641/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001642 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001643 * @buf: buffer containing arriving message
1644 * Consumes buffer
1645 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001646 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001647int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001648{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001649 struct tipc_sock *tsk;
1650 struct tipc_port *port;
1651 struct sock *sk;
1652 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001653 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001654 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001655 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001656
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001657 /* Validate destination and message */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001658 port = tipc_port_lock(dport);
1659 if (unlikely(!port)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001660 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001661 goto exit;
1662 }
1663
1664 tsk = tipc_port_to_sock(port);
1665 sk = &tsk->sk;
1666
1667 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001668 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001669
Allan Stephens0c3141e2008-04-15 00:22:02 -07001670 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001671 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001672 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001673 if (sk->sk_backlog.len == 0)
1674 atomic_set(&tsk->dupl_rcvcnt, 0);
1675 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1676 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001677 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001678 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001679 bh_unlock_sock(sk);
1680 tipc_port_unlock(port);
1681
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001682 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001683 return 0;
1684exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001685 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001686 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001687
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001688 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001689 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001690}
1691
Ying Xue78eb3a52014-01-17 09:50:03 +08001692static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1693{
1694 struct sock *sk = sock->sk;
1695 DEFINE_WAIT(wait);
1696 int done;
1697
1698 do {
1699 int err = sock_error(sk);
1700 if (err)
1701 return err;
1702 if (!*timeo_p)
1703 return -ETIMEDOUT;
1704 if (signal_pending(current))
1705 return sock_intr_errno(*timeo_p);
1706
1707 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1708 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1709 finish_wait(sk_sleep(sk), &wait);
1710 } while (!done);
1711 return 0;
1712}
1713
Per Lidenb97bf3f2006-01-02 19:04:38 +01001714/**
Ying Xue247f0f32014-02-18 16:06:46 +08001715 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716 * @sock: socket structure
1717 * @dest: socket address for destination port
1718 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001719 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 *
1721 * Returns 0 on success, errno otherwise
1722 */
Ying Xue247f0f32014-02-18 16:06:46 +08001723static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1724 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001725{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001726 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001727 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1728 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001729 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1730 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001731 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732
Allan Stephens0c3141e2008-04-15 00:22:02 -07001733 lock_sock(sk);
1734
Allan Stephensb89741a2008-04-15 00:20:37 -07001735 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001736 if (sock->state == SS_READY) {
1737 res = -EOPNOTSUPP;
1738 goto exit;
1739 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740
Allan Stephensb89741a2008-04-15 00:20:37 -07001741 /*
1742 * Reject connection attempt using multicast address
1743 *
1744 * Note: send_msg() validates the rest of the address fields,
1745 * so there's no need to do it here
1746 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001747 if (dst->addrtype == TIPC_ADDR_MCAST) {
1748 res = -EINVAL;
1749 goto exit;
1750 }
1751
Ying Xue78eb3a52014-01-17 09:50:03 +08001752 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001753 switch (sock->state) {
1754 case SS_UNCONNECTED:
1755 /* Send a 'SYN-' to destination */
1756 m.msg_name = dest;
1757 m.msg_namelen = destlen;
1758
1759 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1760 * indicate send_msg() is never blocked.
1761 */
1762 if (!timeout)
1763 m.msg_flags = MSG_DONTWAIT;
1764
Ying Xue247f0f32014-02-18 16:06:46 +08001765 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001766 if ((res < 0) && (res != -EWOULDBLOCK))
1767 goto exit;
1768
1769 /* Just entered SS_CONNECTING state; the only
1770 * difference is that return value in non-blocking
1771 * case is EINPROGRESS, rather than EALREADY.
1772 */
1773 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001774 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001775 if (previous == SS_CONNECTING)
1776 res = -EALREADY;
1777 if (!timeout)
1778 goto exit;
1779 timeout = msecs_to_jiffies(timeout);
1780 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1781 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001782 break;
1783 case SS_CONNECTED:
1784 res = -EISCONN;
1785 break;
1786 default:
1787 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001788 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001789 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001790exit:
1791 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001792 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793}
1794
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001795/**
Ying Xue247f0f32014-02-18 16:06:46 +08001796 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797 * @sock: socket structure
1798 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001799 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800 * Returns 0 on success, errno otherwise
1801 */
Ying Xue247f0f32014-02-18 16:06:46 +08001802static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001804 struct sock *sk = sock->sk;
1805 int res;
1806
1807 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808
Ying Xue245f3d32011-07-06 06:01:13 -04001809 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001810 res = -EINVAL;
1811 else {
1812 sock->state = SS_LISTENING;
1813 res = 0;
1814 }
1815
1816 release_sock(sk);
1817 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001818}
1819
Ying Xue6398e232014-01-17 09:50:04 +08001820static int tipc_wait_for_accept(struct socket *sock, long timeo)
1821{
1822 struct sock *sk = sock->sk;
1823 DEFINE_WAIT(wait);
1824 int err;
1825
1826 /* True wake-one mechanism for incoming connections: only
1827 * one process gets woken up, not the 'whole herd'.
1828 * Since we do not 'race & poll' for established sockets
1829 * anymore, the common case will execute the loop only once.
1830 */
1831 for (;;) {
1832 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1833 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001834 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001835 release_sock(sk);
1836 timeo = schedule_timeout(timeo);
1837 lock_sock(sk);
1838 }
1839 err = 0;
1840 if (!skb_queue_empty(&sk->sk_receive_queue))
1841 break;
1842 err = -EINVAL;
1843 if (sock->state != SS_LISTENING)
1844 break;
1845 err = sock_intr_errno(timeo);
1846 if (signal_pending(current))
1847 break;
1848 err = -EAGAIN;
1849 if (!timeo)
1850 break;
1851 }
1852 finish_wait(sk_sleep(sk), &wait);
1853 return err;
1854}
1855
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001856/**
Ying Xue247f0f32014-02-18 16:06:46 +08001857 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001858 * @sock: listening socket
1859 * @newsock: new socket that is to be connected
1860 * @flags: file-related flags associated with socket
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_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001866 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001868 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001869 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001870 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001871 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872
Allan Stephens0c3141e2008-04-15 00:22:02 -07001873 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001874
Allan Stephens0c3141e2008-04-15 00:22:02 -07001875 if (sock->state != SS_LISTENING) {
1876 res = -EINVAL;
1877 goto exit;
1878 }
Ying Xue6398e232014-01-17 09:50:04 +08001879 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1880 res = tipc_wait_for_accept(sock, timeo);
1881 if (res)
1882 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001883
1884 buf = skb_peek(&sk->sk_receive_queue);
1885
Ying Xuec5fa7b32013-06-17 10:54:39 -04001886 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001887 if (res)
1888 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001889
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001890 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001891 new_port = &tipc_sk(new_sk)->port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001892 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001893
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001894 /* we lock on new_sk; but lockdep sees the lock on sk */
1895 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001896
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001897 /*
1898 * Reject any stray messages received by new socket
1899 * before the socket lock was taken (very, very unlikely)
1900 */
1901 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001903 /* Connect new socket to it's peer */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001904 tipc_sk_finish_conn(new_port, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001905 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001907 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001908 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001909 new_port->conn_type = msg_nametype(msg);
1910 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001911 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001912
1913 /*
1914 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1915 * Respond to 'SYN+' by queuing it on new socket.
1916 */
1917 if (!msg_data_sz(msg)) {
1918 struct msghdr m = {NULL,};
1919
1920 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001921 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001922 } else {
1923 __skb_dequeue(&sk->sk_receive_queue);
1924 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001925 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001926 }
1927 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001929 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001930 return res;
1931}
1932
1933/**
Ying Xue247f0f32014-02-18 16:06:46 +08001934 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001935 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001936 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001937 *
1938 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001939 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940 * Returns 0 on success, errno otherwise
1941 */
Ying Xue247f0f32014-02-18 16:06:46 +08001942static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001943{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001944 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001945 struct tipc_sock *tsk = tipc_sk(sk);
1946 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001947 struct sk_buff *buf;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001948 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001949 int res;
1950
Allan Stephense247a8f2008-03-06 15:05:38 -08001951 if (how != SHUT_RDWR)
1952 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001953
Allan Stephens0c3141e2008-04-15 00:22:02 -07001954 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955
1956 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001957 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958 case SS_CONNECTED:
1959
Per Lidenb97bf3f2006-01-02 19:04:38 +01001960restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001961 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001962 buf = __skb_dequeue(&sk->sk_receive_queue);
1963 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001964 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001965 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001966 goto restart;
1967 }
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001968 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
1969 tipc_link_xmit(buf, dnode, port->ref);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001970 tipc_node_remove_conn(dnode, port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001971 } else {
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001972 dnode = tipc_port_peernode(port);
1973 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
1974 TIPC_CONN_MSG, SHORT_H_SIZE,
1975 0, dnode, tipc_own_addr,
1976 tipc_port_peerport(port),
1977 port->ref, TIPC_CONN_SHUTDOWN);
1978 tipc_link_xmit(buf, dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979 }
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001980 port->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001981 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001982 tipc_node_remove_conn(dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001983 /* fall through */
1984
1985 case SS_DISCONNECTING:
1986
Ying Xue75031152012-10-29 09:38:15 -04001987 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001988 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001989
1990 /* Wake up anyone sleeping in poll */
1991 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992 res = 0;
1993 break;
1994
1995 default:
1996 res = -ENOTCONN;
1997 }
1998
Allan Stephens0c3141e2008-04-15 00:22:02 -07001999 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002000 return res;
2001}
2002
Jon Paul Maloy57289012014-08-22 18:09:09 -04002003static void tipc_sk_timeout(unsigned long ref)
2004{
2005 struct tipc_port *port = tipc_port_lock(ref);
2006 struct tipc_sock *tsk;
2007 struct sock *sk;
2008 struct sk_buff *buf = NULL;
2009 struct tipc_msg *msg = NULL;
2010 u32 peer_port, peer_node;
2011
2012 if (!port)
2013 return;
2014
2015 if (!port->connected) {
2016 tipc_port_unlock(port);
2017 return;
2018 }
2019 tsk = tipc_port_to_sock(port);
2020 sk = &tsk->sk;
2021 bh_lock_sock(sk);
2022 peer_port = tipc_port_peerport(port);
2023 peer_node = tipc_port_peernode(port);
2024
2025 if (port->probing_state == TIPC_CONN_PROBING) {
2026 /* Previous probe not answered -> self abort */
2027 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2028 SHORT_H_SIZE, 0, tipc_own_addr,
2029 peer_node, ref, peer_port,
2030 TIPC_ERR_NO_PORT);
2031 } else {
2032 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2033 0, peer_node, tipc_own_addr,
2034 peer_port, ref, TIPC_OK);
2035 port->probing_state = TIPC_CONN_PROBING;
2036 k_start_timer(&port->timer, port->probing_interval);
2037 }
2038 bh_unlock_sock(sk);
2039 tipc_port_unlock(port);
2040 if (!buf)
2041 return;
2042
2043 msg = buf_msg(buf);
2044 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
2045}
2046
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002047static int tipc_sk_show(struct tipc_port *port, char *buf,
2048 int len, int full_id)
2049{
2050 struct publication *publ;
2051 int ret;
2052
2053 if (full_id)
2054 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2055 tipc_zone(tipc_own_addr),
2056 tipc_cluster(tipc_own_addr),
2057 tipc_node(tipc_own_addr), port->ref);
2058 else
2059 ret = tipc_snprintf(buf, len, "%-10u:", port->ref);
2060
2061 if (port->connected) {
2062 u32 dport = tipc_port_peerport(port);
2063 u32 destnode = tipc_port_peernode(port);
2064
2065 ret += tipc_snprintf(buf + ret, len - ret,
2066 " connected to <%u.%u.%u:%u>",
2067 tipc_zone(destnode),
2068 tipc_cluster(destnode),
2069 tipc_node(destnode), dport);
2070 if (port->conn_type != 0)
2071 ret += tipc_snprintf(buf + ret, len - ret,
2072 " via {%u,%u}", port->conn_type,
2073 port->conn_instance);
2074 } else if (port->published) {
2075 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
2076 list_for_each_entry(publ, &port->publications, pport_list) {
2077 if (publ->lower == publ->upper)
2078 ret += tipc_snprintf(buf + ret, len - ret,
2079 " {%u,%u}", publ->type,
2080 publ->lower);
2081 else
2082 ret += tipc_snprintf(buf + ret, len - ret,
2083 " {%u,%u,%u}", publ->type,
2084 publ->lower, publ->upper);
2085 }
2086 }
2087 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2088 return ret;
2089}
2090
2091struct sk_buff *tipc_sk_socks_show(void)
2092{
2093 struct sk_buff *buf;
2094 struct tlv_desc *rep_tlv;
2095 char *pb;
2096 int pb_len;
2097 struct tipc_sock *tsk;
2098 int str_len = 0;
2099 u32 ref = 0;
2100
2101 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2102 if (!buf)
2103 return NULL;
2104 rep_tlv = (struct tlv_desc *)buf->data;
2105 pb = TLV_DATA(rep_tlv);
2106 pb_len = ULTRA_STRING_MAX_LEN;
2107
2108 tsk = tipc_sk_lock_next(&ref);
2109 for (; tsk; tsk = tipc_sk_lock_next(&ref)) {
2110 bh_lock_sock(&tsk->sk);
2111 str_len += tipc_sk_show(&tsk->port, pb + str_len,
2112 pb_len - str_len, 0);
2113 bh_unlock_sock(&tsk->sk);
2114 tipc_port_unlock(&tsk->port);
2115 }
2116 str_len += 1; /* for "\0" */
2117 skb_put(buf, TLV_SPACE(str_len));
2118 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2119
2120 return buf;
2121}
2122
2123/* tipc_sk_reinit: set non-zero address in all existing sockets
2124 * when we go from standalone to network mode.
2125 */
2126void tipc_sk_reinit(void)
2127{
2128 struct tipc_msg *msg;
2129 u32 ref = 0;
2130 struct tipc_sock *tsk = tipc_sk_lock_next(&ref);
2131
2132 for (; tsk; tsk = tipc_sk_lock_next(&ref)) {
2133 bh_lock_sock(&tsk->sk);
2134 msg = &tsk->port.phdr;
2135 msg_set_prevnode(msg, tipc_own_addr);
2136 msg_set_orignode(msg, tipc_own_addr);
2137 bh_unlock_sock(&tsk->sk);
2138 tipc_port_unlock(&tsk->port);
2139 }
2140}
2141
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142/**
Ying Xue247f0f32014-02-18 16:06:46 +08002143 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002144 * @sock: socket structure
2145 * @lvl: option level
2146 * @opt: option identifier
2147 * @ov: pointer to new option value
2148 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002149 *
2150 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002151 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002152 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002153 * Returns 0 on success, errno otherwise
2154 */
Ying Xue247f0f32014-02-18 16:06:46 +08002155static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2156 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002158 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002159 struct tipc_sock *tsk = tipc_sk(sk);
2160 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002161 u32 value;
2162 int res;
2163
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002164 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2165 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002166 if (lvl != SOL_TIPC)
2167 return -ENOPROTOOPT;
2168 if (ol < sizeof(value))
2169 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002170 res = get_user(value, (u32 __user *)ov);
2171 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002172 return res;
2173
Allan Stephens0c3141e2008-04-15 00:22:02 -07002174 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002175
Per Lidenb97bf3f2006-01-02 19:04:38 +01002176 switch (opt) {
2177 case TIPC_IMPORTANCE:
Erik Hugneac32c7f2014-08-15 16:44:35 +02002178 res = tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002179 break;
2180 case TIPC_SRC_DROPPABLE:
2181 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002182 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002183 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002184 res = -ENOPROTOOPT;
2185 break;
2186 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002187 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002188 break;
2189 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002190 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002191 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002192 break;
2193 default:
2194 res = -EINVAL;
2195 }
2196
Allan Stephens0c3141e2008-04-15 00:22:02 -07002197 release_sock(sk);
2198
Per Lidenb97bf3f2006-01-02 19:04:38 +01002199 return res;
2200}
2201
2202/**
Ying Xue247f0f32014-02-18 16:06:46 +08002203 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002204 * @sock: socket structure
2205 * @lvl: option level
2206 * @opt: option identifier
2207 * @ov: receptacle for option value
2208 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002209 *
2210 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002211 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002212 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002213 * Returns 0 on success, errno otherwise
2214 */
Ying Xue247f0f32014-02-18 16:06:46 +08002215static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2216 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002217{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002218 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002219 struct tipc_sock *tsk = tipc_sk(sk);
2220 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002221 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002222 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002223 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002224
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002225 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2226 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002227 if (lvl != SOL_TIPC)
2228 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002229 res = get_user(len, ol);
2230 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002231 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002232
Allan Stephens0c3141e2008-04-15 00:22:02 -07002233 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002234
2235 switch (opt) {
2236 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002237 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002238 break;
2239 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002240 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002241 break;
2242 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002243 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002244 break;
2245 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002246 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002247 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002248 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002249 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002250 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002251 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002252 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002253 value = skb_queue_len(&sk->sk_receive_queue);
2254 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002255 default:
2256 res = -EINVAL;
2257 }
2258
Allan Stephens0c3141e2008-04-15 00:22:02 -07002259 release_sock(sk);
2260
Paul Gortmaker25860c32010-12-31 18:59:31 +00002261 if (res)
2262 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002263
Paul Gortmaker25860c32010-12-31 18:59:31 +00002264 if (len < sizeof(value))
2265 return -EINVAL;
2266
2267 if (copy_to_user(ov, &value, sizeof(value)))
2268 return -EFAULT;
2269
2270 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002271}
2272
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002273static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002274{
2275 struct tipc_sioc_ln_req lnr;
2276 void __user *argp = (void __user *)arg;
2277
2278 switch (cmd) {
2279 case SIOCGETLINKNAME:
2280 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2281 return -EFAULT;
2282 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2283 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2284 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2285 return -EFAULT;
2286 return 0;
2287 }
2288 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002289 default:
2290 return -ENOIOCTLCMD;
2291 }
2292}
2293
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002294/* Protocol switches for the various types of TIPC sockets */
2295
Florian Westphalbca65ea2008-02-07 18:18:01 -08002296static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002297 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002298 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002299 .release = tipc_release,
2300 .bind = tipc_bind,
2301 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002302 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002303 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002304 .getname = tipc_getname,
2305 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002306 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002307 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002308 .shutdown = tipc_shutdown,
2309 .setsockopt = tipc_setsockopt,
2310 .getsockopt = tipc_getsockopt,
2311 .sendmsg = tipc_sendmsg,
2312 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002313 .mmap = sock_no_mmap,
2314 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002315};
2316
Florian Westphalbca65ea2008-02-07 18:18:01 -08002317static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002318 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002319 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002320 .release = tipc_release,
2321 .bind = tipc_bind,
2322 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002323 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002324 .accept = tipc_accept,
2325 .getname = tipc_getname,
2326 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002327 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002328 .listen = tipc_listen,
2329 .shutdown = tipc_shutdown,
2330 .setsockopt = tipc_setsockopt,
2331 .getsockopt = tipc_getsockopt,
2332 .sendmsg = tipc_send_packet,
2333 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002334 .mmap = sock_no_mmap,
2335 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002336};
2337
Florian Westphalbca65ea2008-02-07 18:18:01 -08002338static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002339 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002340 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002341 .release = tipc_release,
2342 .bind = tipc_bind,
2343 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002344 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002345 .accept = tipc_accept,
2346 .getname = tipc_getname,
2347 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002348 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002349 .listen = tipc_listen,
2350 .shutdown = tipc_shutdown,
2351 .setsockopt = tipc_setsockopt,
2352 .getsockopt = tipc_getsockopt,
2353 .sendmsg = tipc_send_stream,
2354 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002355 .mmap = sock_no_mmap,
2356 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002357};
2358
Florian Westphalbca65ea2008-02-07 18:18:01 -08002359static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002360 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002361 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002362 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002363};
2364
2365static struct proto tipc_proto = {
2366 .name = "TIPC",
2367 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002368 .obj_size = sizeof(struct tipc_sock),
2369 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002370};
2371
Ying Xuec5fa7b32013-06-17 10:54:39 -04002372static struct proto tipc_proto_kern = {
2373 .name = "TIPC",
2374 .obj_size = sizeof(struct tipc_sock),
2375 .sysctl_rmem = sysctl_tipc_rmem
2376};
2377
Per Lidenb97bf3f2006-01-02 19:04:38 +01002378/**
Per Liden4323add2006-01-18 00:38:21 +01002379 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002380 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002381 * Returns 0 on success, errno otherwise
2382 */
Per Liden4323add2006-01-18 00:38:21 +01002383int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002384{
2385 int res;
2386
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002387 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002388 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002389 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002390 goto out;
2391 }
2392
2393 res = sock_register(&tipc_family_ops);
2394 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002395 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002396 proto_unregister(&tipc_proto);
2397 goto out;
2398 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002399 out:
2400 return res;
2401}
2402
2403/**
Per Liden4323add2006-01-18 00:38:21 +01002404 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002405 */
Per Liden4323add2006-01-18 00:38:21 +01002406void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002407{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002408 sock_unregister(tipc_family_ops.family);
2409 proto_unregister(&tipc_proto);
2410}