blob: 336e18d6cf4610e09879712e2020de36f5ea6b55 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Maloye643df12012-11-27 06:15:29 -05004 * Copyright (c) 2001-2007, 2012 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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Erik Hugne2cf8aa12012-06-29 00:16:37 -040040#include <linux/export.h>
41#include <net/sock.h>
42
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#define SS_LISTENING -1 /* socket is listening */
44#define SS_READY -2 /* socket is connectionless */
45
Allan Stephens3654ea02008-04-13 21:35:11 -070046#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010047
48struct tipc_sock {
49 struct sock sk;
50 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070051 struct tipc_portid peer_name;
Allan Stephensa0f40f02011-05-26 13:44:34 -040052 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010053};
54
Allan Stephens0c3141e2008-04-15 00:22:02 -070055#define tipc_sk(sk) ((struct tipc_sock *)(sk))
Joe Perchese3192692012-06-03 17:41:40 +000056#define tipc_sk_port(sk) (tipc_sk(sk)->p)
Per Lidenb97bf3f2006-01-02 19:04:38 +010057
Allan Stephens0c3141e2008-04-15 00:22:02 -070058static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010059static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
60static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080061static void tipc_data_ready(struct sock *sk, int len);
62static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080063static int tipc_release(struct socket *sock);
64static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010065
Florian Westphalbca65ea2008-02-07 18:18:01 -080066static const struct proto_ops packet_ops;
67static const struct proto_ops stream_ops;
68static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010069
70static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040071static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010072
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090073/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070074 * Revised TIPC socket locking policy:
75 *
76 * Most socket operations take the standard socket lock when they start
77 * and hold it until they finish (or until they need to sleep). Acquiring
78 * this lock grants the owner exclusive access to the fields of the socket
79 * data structures, with the exception of the backlog queue. A few socket
80 * operations can be done without taking the socket lock because they only
81 * read socket information that never changes during the life of the socket.
82 *
83 * Socket operations may acquire the lock for the associated TIPC port if they
84 * need to perform an operation on the port. If any routine needs to acquire
85 * both the socket lock and the port lock it must take the socket lock first
86 * to avoid the risk of deadlock.
87 *
88 * The dispatcher handling incoming messages cannot grab the socket lock in
89 * the standard fashion, since invoked it runs at the BH level and cannot block.
90 * Instead, it checks to see if the socket lock is currently owned by someone,
91 * and either handles the message itself or adds it to the socket's backlog
92 * queue; in the latter case the queued message is processed once the process
93 * owning the socket lock releases it.
94 *
95 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
96 * the problem of a blocked socket operation preventing any other operations
97 * from occurring. However, applications must be careful if they have
98 * multiple threads trying to send (or receive) on the same socket, as these
99 * operations might interfere with each other. For example, doing a connect
100 * and a receive at the same time might allow the receive to consume the
101 * ACK message meant for the connect. While additional work could be done
102 * to try and overcome this, it doesn't seem to be worthwhile at the present.
103 *
104 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
105 * that another operation that must be performed in a non-blocking manner is
106 * not delayed for very long because the lock has already been taken.
107 *
108 * NOTE: This code assumes that certain fields of a port/socket pair are
109 * constant over its lifetime; such fields can be examined without taking
110 * the socket lock and/or port lock, and do not need to be re-read even
111 * after resuming processing after waiting. These fields include:
112 * - socket type
113 * - pointer to socket sk structure (aka tipc_sock structure)
114 * - pointer to port structure
115 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117
118/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700119 * advance_rx_queue - discard first buffer in socket receive queue
120 *
121 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700123static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400125 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126}
127
128/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700129 * reject_rx_queue - reject all buffers in socket receive queue
130 *
131 * Caller must hold socket lock
132 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700133static void reject_rx_queue(struct sock *sk)
134{
135 struct sk_buff *buf;
136
Ying Xue9da3d472012-11-27 06:15:27 -0500137 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700138 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700139}
140
141/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400142 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700143 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 * @sock: pre-allocated socket structure
145 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800146 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900147 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700148 * This routine creates additional data structures used by the TIPC socket,
149 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 *
151 * Returns 0 on success, errno otherwise
152 */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400153static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
154 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700156 const struct proto_ops *ops;
157 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700159 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700160
161 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 if (unlikely(protocol != 0))
163 return -EPROTONOSUPPORT;
164
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 switch (sock->type) {
166 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700167 ops = &stream_ops;
168 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 break;
170 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700171 ops = &packet_ops;
172 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 break;
174 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700176 ops = &msg_ops;
177 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 break;
Allan Stephens49978652006-06-25 23:47:18 -0700179 default:
Allan Stephens49978652006-06-25 23:47:18 -0700180 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 }
182
Allan Stephens0c3141e2008-04-15 00:22:02 -0700183 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400184 if (!kern)
185 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
186 else
187 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
188
Allan Stephens0c3141e2008-04-15 00:22:02 -0700189 if (sk == NULL)
190 return -ENOMEM;
191
192 /* Allocate TIPC port for socket to use */
Ying Xue3c5db8e2013-06-17 10:54:44 -0400193 tp_ptr = tipc_createport(sk, &dispatch, &wakeupdispatch,
194 TIPC_LOW_IMPORTANCE);
Allan Stephens0ea52242008-07-14 22:42:19 -0700195 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700196 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 return -ENOMEM;
198 }
199
Allan Stephens0c3141e2008-04-15 00:22:02 -0700200 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 sock->ops = ops;
202 sock->state = state;
203
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700205 sk->sk_backlog_rcv = backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400206 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800207 sk->sk_data_ready = tipc_data_ready;
208 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700209 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400210 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700212 spin_unlock_bh(tp_ptr->lock);
213
Allan Stephens0c3141e2008-04-15 00:22:02 -0700214 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700215 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700216 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700217 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700218 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 return 0;
221}
222
223/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400224 * tipc_sock_create_local - create TIPC socket from inside TIPC module
225 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
226 *
227 * We cannot use sock_creat_kern here because it bumps module user count.
228 * Since socket owner and creator is the same module we must make sure
229 * that module count remains zero for module local sockets, otherwise
230 * we cannot do rmmod.
231 *
232 * Returns 0 on success, errno otherwise
233 */
234int tipc_sock_create_local(int type, struct socket **res)
235{
236 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400237
238 rc = sock_create_lite(AF_TIPC, type, 0, res);
239 if (rc < 0) {
240 pr_err("Failed to create kernel socket\n");
241 return rc;
242 }
243 tipc_sk_create(&init_net, *res, 0, 1);
244
Ying Xuec5fa7b32013-06-17 10:54:39 -0400245 return 0;
246}
247
248/**
249 * tipc_sock_release_local - release socket created by tipc_sock_create_local
250 * @sock: the socket to be released.
251 *
252 * Module reference count is not incremented when such sockets are created,
253 * so we must keep it from being decremented when they are released.
254 */
255void tipc_sock_release_local(struct socket *sock)
256{
Ying Xue247f0f32014-02-18 16:06:46 +0800257 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400258 sock->ops = NULL;
259 sock_release(sock);
260}
261
262/**
263 * tipc_sock_accept_local - accept a connection on a socket created
264 * with tipc_sock_create_local. Use this function to avoid that
265 * module reference count is inadvertently incremented.
266 *
267 * @sock: the accepting socket
268 * @newsock: reference to the new socket to be created
269 * @flags: socket flags
270 */
271
272int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400273 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400274{
275 struct sock *sk = sock->sk;
276 int ret;
277
278 ret = sock_create_lite(sk->sk_family, sk->sk_type,
279 sk->sk_protocol, newsock);
280 if (ret < 0)
281 return ret;
282
Ying Xue247f0f32014-02-18 16:06:46 +0800283 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400284 if (ret < 0) {
285 sock_release(*newsock);
286 return ret;
287 }
288 (*newsock)->ops = sock->ops;
289 return ret;
290}
291
292/**
Ying Xue247f0f32014-02-18 16:06:46 +0800293 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 * @sock: socket to destroy
295 *
296 * This routine cleans up any messages that are still queued on the socket.
297 * For DGRAM and RDM socket types, all queued messages are rejected.
298 * For SEQPACKET and STREAM socket types, the first message is rejected
299 * and any others are discarded. (If the first message on a STREAM socket
300 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900301 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 * NOTE: Rejected messages are not necessarily returned to the sender! They
303 * are returned or discarded according to the "destination droppable" setting
304 * specified for the message by the sender.
305 *
306 * Returns 0 on success, errno otherwise
307 */
Ying Xue247f0f32014-02-18 16:06:46 +0800308static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700311 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700313 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314
Allan Stephens0c3141e2008-04-15 00:22:02 -0700315 /*
316 * Exit if socket isn't fully initialized (occurs when a failed accept()
317 * releases a pre-allocated child socket that was never used)
318 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700319 if (sk == NULL)
320 return 0;
321
322 tport = tipc_sk_port(sk);
323 lock_sock(sk);
324
325 /*
326 * Reject all unreceived messages, except on an active connection
327 * (which disconnects locally & sends a 'FIN+' to peer)
328 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700330 buf = __skb_dequeue(&sk->sk_receive_queue);
331 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 break;
Ying Xue40682432013-10-18 07:23:16 +0200333 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400334 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700335 else {
336 if ((sock->state == SS_CONNECTING) ||
337 (sock->state == SS_CONNECTED)) {
338 sock->state = SS_DISCONNECTING;
Ying Xue247f0f32014-02-18 16:06:46 +0800339 tipc_port_disconnect(tport->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700340 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700342 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 }
344
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 /*
346 * Delete TIPC port; this ensures no more messages are queued
347 * (also disconnects an active connection & sends a 'FIN-' to peer)
348 */
Ying Xue84602762013-12-27 10:18:28 +0800349 res = tipc_deleteport(tport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350
Allan Stephens0c3141e2008-04-15 00:22:02 -0700351 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100352 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700355 sock->state = SS_DISCONNECTING;
356 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357
358 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700359 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361 return res;
362}
363
364/**
Ying Xue247f0f32014-02-18 16:06:46 +0800365 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 * @sock: socket structure
367 * @uaddr: socket address describing name(s) and desired operation
368 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900369 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 * Name and name sequence binding is indicated using a positive scope value;
371 * a negative scope value unbinds the specified name. Specifying no name
372 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900373 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700375 *
376 * NOTE: This routine doesn't need to take the socket lock since it doesn't
377 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 */
Ying Xue247f0f32014-02-18 16:06:46 +0800379static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
380 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381{
Ying Xue84602762013-12-27 10:18:28 +0800382 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Ying Xue84602762013-12-27 10:18:28 +0800384 struct tipc_port *tport = tipc_sk_port(sock->sk);
385 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386
Ying Xue84602762013-12-27 10:18:28 +0800387 lock_sock(sk);
388 if (unlikely(!uaddr_len)) {
389 res = tipc_withdraw(tport, 0, NULL);
390 goto exit;
391 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900392
Ying Xue84602762013-12-27 10:18:28 +0800393 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
394 res = -EINVAL;
395 goto exit;
396 }
397 if (addr->family != AF_TIPC) {
398 res = -EAFNOSUPPORT;
399 goto exit;
400 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 if (addr->addrtype == TIPC_ADDR_NAME)
403 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800404 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
405 res = -EAFNOSUPPORT;
406 goto exit;
407 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900408
Ying Xue13a2e892013-06-17 10:54:40 -0400409 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400410 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800411 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
412 res = -EACCES;
413 goto exit;
414 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400415
Ying Xue84602762013-12-27 10:18:28 +0800416 res = (addr->scope > 0) ?
417 tipc_publish(tport, addr->scope, &addr->addr.nameseq) :
418 tipc_withdraw(tport, -addr->scope, &addr->addr.nameseq);
419exit:
420 release_sock(sk);
421 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422}
423
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900424/**
Ying Xue247f0f32014-02-18 16:06:46 +0800425 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 * @sock: socket structure
427 * @uaddr: area for returned socket address
428 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700429 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900430 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700432 *
Allan Stephens2da59912008-07-14 22:43:32 -0700433 * NOTE: This routine doesn't need to take the socket lock since it only
434 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000435 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 */
Ying Xue247f0f32014-02-18 16:06:46 +0800437static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
438 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700441 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000443 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700444 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700445 if ((sock->state != SS_CONNECTED) &&
446 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
447 return -ENOTCONN;
448 addr->addr.id.ref = tsock->peer_name.ref;
449 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700450 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000451 addr->addr.id.ref = tsock->p->ref;
452 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700453 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454
455 *uaddr_len = sizeof(*addr);
456 addr->addrtype = TIPC_ADDR_ID;
457 addr->family = AF_TIPC;
458 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 addr->addr.name.domain = 0;
460
Allan Stephens0c3141e2008-04-15 00:22:02 -0700461 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462}
463
464/**
Ying Xue247f0f32014-02-18 16:06:46 +0800465 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466 * @file: file structure associated with the socket
467 * @sock: socket for which to calculate the poll bits
468 * @wait: ???
469 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700470 * Returns pollmask value
471 *
472 * COMMENTARY:
473 * It appears that the usual socket locking mechanisms are not useful here
474 * since the pollmask info is potentially out-of-date the moment this routine
475 * exits. TCP and other protocols seem to rely on higher level poll routines
476 * to handle any preventable race conditions, so TIPC will do the same ...
477 *
478 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700479 *
Allan Stephensf662c072010-08-17 11:00:06 +0000480 * socket state flags set
481 * ------------ ---------
482 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200483 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000484 *
485 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
486 * no write flags
487 *
488 * connected POLLIN/POLLRDNORM if data in rx queue
489 * POLLOUT if port is not congested
490 *
491 * disconnecting POLLIN/POLLRDNORM/POLLHUP
492 * no write flags
493 *
494 * listening POLLIN if SYN in rx queue
495 * no write flags
496 *
497 * ready POLLIN/POLLRDNORM if data in rx queue
498 * [connectionless] POLLOUT (since port cannot be congested)
499 *
500 * IMPORTANT: The fact that a read or write operation is indicated does NOT
501 * imply that the operation will succeed, merely that it should be performed
502 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 */
Ying Xue247f0f32014-02-18 16:06:46 +0800504static unsigned int tipc_poll(struct file *file, struct socket *sock,
505 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506{
Allan Stephens9b674e82008-03-26 16:48:21 -0700507 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000508 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700509
Ying Xuef288bef2012-08-21 11:16:57 +0800510 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700511
Allan Stephensf662c072010-08-17 11:00:06 +0000512 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200513 case SS_UNCONNECTED:
514 if (!tipc_sk_port(sk)->congested)
515 mask |= POLLOUT;
516 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000517 case SS_READY:
518 case SS_CONNECTED:
519 if (!tipc_sk_port(sk)->congested)
520 mask |= POLLOUT;
521 /* fall thru' */
522 case SS_CONNECTING:
523 case SS_LISTENING:
524 if (!skb_queue_empty(&sk->sk_receive_queue))
525 mask |= (POLLIN | POLLRDNORM);
526 break;
527 case SS_DISCONNECTING:
528 mask = (POLLIN | POLLRDNORM | POLLHUP);
529 break;
530 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700531
532 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533}
534
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900535/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 * dest_name_check - verify user is permitted to send to specified port name
537 * @dest: destination address
538 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900539 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 * Prevents restricted configuration commands from being issued by
541 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900542 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 * Returns 0 if permission is granted, otherwise errno
544 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800545static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546{
547 struct tipc_cfg_msg_hdr hdr;
548
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900549 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
550 return 0;
551 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
552 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900553 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
554 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555
Allan Stephens3f8dd942011-01-18 13:09:29 -0500556 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
557 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900558 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700560 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 return 0;
564}
565
Ying Xue3f405042014-01-17 09:50:05 +0800566static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
567{
568 struct sock *sk = sock->sk;
569 struct tipc_port *tport = tipc_sk_port(sk);
570 DEFINE_WAIT(wait);
571 int done;
572
573 do {
574 int err = sock_error(sk);
575 if (err)
576 return err;
577 if (sock->state == SS_DISCONNECTING)
578 return -EPIPE;
579 if (!*timeo_p)
580 return -EAGAIN;
581 if (signal_pending(current))
582 return sock_intr_errno(*timeo_p);
583
584 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
585 done = sk_wait_event(sk, timeo_p, !tport->congested);
586 finish_wait(sk_sleep(sk), &wait);
587 } while (!done);
588 return 0;
589}
590
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591/**
Ying Xue247f0f32014-02-18 16:06:46 +0800592 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700593 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 * @sock: socket structure
595 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700596 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
601 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900602 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 * Returns the number of bytes sent on success, or errno otherwise
604 */
Ying Xue247f0f32014-02-18 16:06:46 +0800605static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
606 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700608 struct sock *sk = sock->sk;
609 struct tipc_port *tport = tipc_sk_port(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100610 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 int needs_conn;
Ying Xue3f405042014-01-17 09:50:05 +0800612 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 int res = -EINVAL;
614
615 if (unlikely(!dest))
616 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700617 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
618 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100620 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400621 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622
Allan Stephens0c3141e2008-04-15 00:22:02 -0700623 if (iocb)
624 lock_sock(sk);
625
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 needs_conn = (sock->state != SS_READY);
627 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700628 if (sock->state == SS_LISTENING) {
629 res = -EPIPE;
630 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700631 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 if (sock->state != SS_UNCONNECTED) {
633 res = -EISCONN;
634 goto exit;
635 }
Erik Hugne5d21cb72013-06-17 10:54:38 -0400636 if (tport->published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 res = -EOPNOTSUPP;
638 goto exit;
639 }
640 if (dest->addrtype == TIPC_ADDR_NAME) {
641 tport->conn_type = dest->addr.name.name.type;
642 tport->conn_instance = dest->addr.name.name.instance;
643 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644
645 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700646 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 }
648
Ying Xue3f405042014-01-17 09:50:05 +0800649 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900650 do {
651 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000652 res = dest_name_check(dest, m);
653 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700654 break;
655 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900656 &dest->addr.name.name,
657 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500658 m->msg_iov,
659 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000660 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700661 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900662 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500663 m->msg_iov,
664 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000665 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 if (needs_conn) {
667 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700668 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 }
Allan Stephens2db99832010-12-31 18:59:33 +0000670 res = dest_name_check(dest, m);
671 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700672 break;
Ying Xue247f0f32014-02-18 16:06:46 +0800673 res = tipc_port_mcast_xmit(tport->ref,
674 &dest->addr.nameseq,
675 m->msg_iov,
676 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 }
678 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000679 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700680 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700681 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900682 }
Ying Xue3f405042014-01-17 09:50:05 +0800683 res = tipc_wait_for_sndmsg(sock, &timeo);
684 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700685 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900686 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700687
688exit:
689 if (iocb)
690 release_sock(sk);
691 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692}
693
Ying Xue391a6dd2014-01-17 09:50:06 +0800694static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
695{
696 struct sock *sk = sock->sk;
697 struct tipc_port *tport = tipc_sk_port(sk);
698 DEFINE_WAIT(wait);
699 int done;
700
701 do {
702 int err = sock_error(sk);
703 if (err)
704 return err;
705 if (sock->state == SS_DISCONNECTING)
706 return -EPIPE;
707 else if (sock->state != SS_CONNECTED)
708 return -ENOTCONN;
709 if (!*timeo_p)
710 return -EAGAIN;
711 if (signal_pending(current))
712 return sock_intr_errno(*timeo_p);
713
714 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
715 done = sk_wait_event(sk, timeo_p,
716 (!tport->congested || !tport->connected));
717 finish_wait(sk_sleep(sk), &wait);
718 } while (!done);
719 return 0;
720}
721
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900722/**
Ying Xue247f0f32014-02-18 16:06:46 +0800723 * tipc_send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700724 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725 * @sock: socket structure
726 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700727 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900728 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 * Returns the number of bytes sent on success, or errno otherwise
732 */
Ying Xue247f0f32014-02-18 16:06:46 +0800733static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
734 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700736 struct sock *sk = sock->sk;
737 struct tipc_port *tport = tipc_sk_port(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100738 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue391a6dd2014-01-17 09:50:06 +0800739 int res = -EINVAL;
740 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741
742 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 if (unlikely(dest))
Ying Xue247f0f32014-02-18 16:06:46 +0800744 return tipc_sendmsg(iocb, sock, m, total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745
Ying Xue97f8b872013-01-31 21:51:47 +0100746 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400747 return -EMSGSIZE;
748
Allan Stephens0c3141e2008-04-15 00:22:02 -0700749 if (iocb)
750 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751
Ying Xue391a6dd2014-01-17 09:50:06 +0800752 if (unlikely(sock->state != SS_CONNECTED)) {
753 if (sock->state == SS_DISCONNECTING)
754 res = -EPIPE;
755 else
756 res = -ENOTCONN;
757 goto exit;
758 }
Ying Xue1d835872011-07-06 05:53:15 -0400759
Ying Xue391a6dd2014-01-17 09:50:06 +0800760 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900761 do {
Ying Xue9446b872013-10-18 07:23:15 +0200762 res = tipc_send(tport->ref, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000763 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700764 break;
Ying Xue391a6dd2014-01-17 09:50:06 +0800765 res = tipc_wait_for_sndpkt(sock, &timeo);
766 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700767 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900768 } while (1);
Ying Xue391a6dd2014-01-17 09:50:06 +0800769exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700770 if (iocb)
771 release_sock(sk);
772 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773}
774
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900775/**
Ying Xue247f0f32014-02-18 16:06:46 +0800776 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 * @iocb: (unused)
778 * @sock: socket structure
779 * @m: data to send
780 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900781 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900783 *
784 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700785 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786 */
Ying Xue247f0f32014-02-18 16:06:46 +0800787static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
788 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700790 struct sock *sk = sock->sk;
791 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 struct msghdr my_msg;
793 struct iovec my_iov;
794 struct iovec *curr_iov;
795 int curr_iovlen;
796 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700797 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 int curr_left;
799 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700800 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900802
Allan Stephens0c3141e2008-04-15 00:22:02 -0700803 lock_sock(sk);
804
Allan Stephens05646c92007-06-10 17:25:24 -0700805 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900806 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800807 if (sock->state == SS_UNCONNECTED)
Ying Xue247f0f32014-02-18 16:06:46 +0800808 res = tipc_send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800809 else
810 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800811 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900812 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813
Allan Stephens0c3141e2008-04-15 00:22:02 -0700814 if (unlikely(m->msg_name)) {
815 res = -EISCONN;
816 goto exit;
817 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700818
Ying Xue97f8b872013-01-31 21:51:47 +0100819 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400820 res = -EMSGSIZE;
821 goto exit;
822 }
823
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900824 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 * Send each iovec entry using one or more messages
826 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900827 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 * (i.e. one large iovec entry), but could be improved to pass sets
829 * of small iovec entries into send_packet().
830 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700831 curr_iov = m->msg_iov;
832 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833 my_msg.msg_iov = &my_iov;
834 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700835 my_msg.msg_flags = m->msg_flags;
836 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700837 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838
Allan Stephens05646c92007-06-10 17:25:24 -0700839 hdr_size = msg_hdr_sz(&tport->phdr);
840
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841 while (curr_iovlen--) {
842 curr_start = curr_iov->iov_base;
843 curr_left = curr_iov->iov_len;
844
845 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700846 bytes_to_send = tport->max_pkt - hdr_size;
847 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
848 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
849 if (curr_left < bytes_to_send)
850 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 my_iov.iov_base = curr_start;
852 my_iov.iov_len = bytes_to_send;
Ying Xue247f0f32014-02-18 16:06:46 +0800853 res = tipc_send_packet(NULL, sock, &my_msg,
854 bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000855 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700856 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700857 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700858 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700859 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860 curr_left -= bytes_to_send;
861 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700862 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 }
864
865 curr_iov++;
866 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700867 res = bytes_sent;
868exit:
869 release_sock(sk);
870 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871}
872
873/**
874 * auto_connect - complete connection setup to a remote port
875 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900877 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 * Returns 0 on success, errno otherwise
879 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700880static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881{
Allan Stephens2da59912008-07-14 22:43:32 -0700882 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500883 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884
Allan Stephens2da59912008-07-14 22:43:32 -0700885 tsock->peer_name.ref = msg_origport(msg);
886 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500887 p_ptr = tipc_port_deref(tsock->p->ref);
888 if (!p_ptr)
889 return -EINVAL;
890
Ying Xue247f0f32014-02-18 16:06:46 +0800891 __tipc_port_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
Ying Xue584d24b2012-11-29 18:51:19 -0500892
893 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
894 return -EINVAL;
895 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 sock->state = SS_CONNECTED;
897 return 0;
898}
899
900/**
901 * set_orig_addr - capture sender's address for received message
902 * @m: descriptor for message info
903 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900904 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 * Note: Address is not captured if not requested by receiver.
906 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800907static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100909 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900911 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912 addr->family = AF_TIPC;
913 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000914 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100915 addr->addr.id.ref = msg_origport(msg);
916 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000917 addr->addr.name.domain = 0; /* could leave uninitialized */
918 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919 m->msg_namelen = sizeof(struct sockaddr_tipc);
920 }
921}
922
923/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900924 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925 * @m: descriptor for message info
926 * @msg: received message header
927 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900928 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900930 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 * Returns 0 if successful, otherwise errno
932 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800933static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400934 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935{
936 u32 anc_data[3];
937 u32 err;
938 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700939 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 int res;
941
942 if (likely(m->msg_controllen == 0))
943 return 0;
944
945 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 err = msg ? msg_errcode(msg) : 0;
947 if (unlikely(err)) {
948 anc_data[0] = err;
949 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000950 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
951 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000953 if (anc_data[1]) {
954 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
955 msg_data(msg));
956 if (res)
957 return res;
958 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 }
960
961 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100962 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
963 switch (dest_type) {
964 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700965 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 anc_data[0] = msg_nametype(msg);
967 anc_data[1] = msg_namelower(msg);
968 anc_data[2] = msg_namelower(msg);
969 break;
970 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700971 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 anc_data[0] = msg_nametype(msg);
973 anc_data[1] = msg_namelower(msg);
974 anc_data[2] = msg_nameupper(msg);
975 break;
976 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700977 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 anc_data[0] = tport->conn_type;
979 anc_data[1] = tport->conn_instance;
980 anc_data[2] = tport->conn_instance;
981 break;
982 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700983 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 }
Allan Stephens2db99832010-12-31 18:59:33 +0000985 if (has_name) {
986 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
987 if (res)
988 return res;
989 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990
991 return 0;
992}
993
Ying Xue9bbb4ec2014-01-17 09:50:07 +0800994static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
995{
996 struct sock *sk = sock->sk;
997 DEFINE_WAIT(wait);
998 int err;
999
1000 for (;;) {
1001 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1002 if (skb_queue_empty(&sk->sk_receive_queue)) {
1003 if (sock->state == SS_DISCONNECTING) {
1004 err = -ENOTCONN;
1005 break;
1006 }
1007 release_sock(sk);
1008 timeo = schedule_timeout(timeo);
1009 lock_sock(sk);
1010 }
1011 err = 0;
1012 if (!skb_queue_empty(&sk->sk_receive_queue))
1013 break;
1014 err = sock_intr_errno(timeo);
1015 if (signal_pending(current))
1016 break;
1017 err = -EAGAIN;
1018 if (!timeo)
1019 break;
1020 }
1021 finish_wait(sk_sleep(sk), &wait);
1022 return err;
1023}
1024
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001025/**
Ying Xue247f0f32014-02-18 16:06:46 +08001026 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 * @iocb: (unused)
1028 * @m: descriptor for message info
1029 * @buf_len: total size of user buffer area
1030 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001031 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1033 * If the complete message doesn't fit in user area, truncate it.
1034 *
1035 * Returns size of returned message data, errno otherwise
1036 */
Ying Xue247f0f32014-02-18 16:06:46 +08001037static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1038 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001040 struct sock *sk = sock->sk;
1041 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042 struct sk_buff *buf;
1043 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001044 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 unsigned int sz;
1046 u32 err;
1047 int res;
1048
Allan Stephens0c3141e2008-04-15 00:22:02 -07001049 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 if (unlikely(!buf_len))
1051 return -EINVAL;
1052
Allan Stephens0c3141e2008-04-15 00:22:02 -07001053 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054
Allan Stephens0c3141e2008-04-15 00:22:02 -07001055 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056 res = -ENOTCONN;
1057 goto exit;
1058 }
1059
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001060 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001061restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062
Allan Stephens0c3141e2008-04-15 00:22:02 -07001063 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001064 res = tipc_wait_for_rcvmsg(sock, timeo);
1065 if (res)
1066 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001067
1068 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001069 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 msg = buf_msg(buf);
1071 sz = msg_data_sz(msg);
1072 err = msg_errcode(msg);
1073
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001076 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 goto restart;
1078 }
1079
1080 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 set_orig_addr(m, msg);
1082
1083 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001084 res = anc_data_recv(m, msg, tport);
1085 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086 goto exit;
1087
1088 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 if (!err) {
1090 if (unlikely(buf_len < sz)) {
1091 sz = buf_len;
1092 m->msg_flags |= MSG_TRUNC;
1093 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001094 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1095 m->msg_iov, sz);
1096 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 res = sz;
1099 } else {
1100 if ((sock->state == SS_READY) ||
1101 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1102 res = 0;
1103 else
1104 res = -ECONNRESET;
1105 }
1106
1107 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001109 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001110 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1111 tipc_acknowledge(tport->ref, tport->conn_unacked);
1112 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001113 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001115 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116 return res;
1117}
1118
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001119/**
Ying Xue247f0f32014-02-18 16:06:46 +08001120 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 * @iocb: (unused)
1122 * @m: descriptor for message info
1123 * @buf_len: total size of user buffer area
1124 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001125 *
1126 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 * will optionally wait for more; never truncates data.
1128 *
1129 * Returns size of returned message data, errno otherwise
1130 */
Ying Xue247f0f32014-02-18 16:06:46 +08001131static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1132 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001134 struct sock *sk = sock->sk;
1135 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 struct sk_buff *buf;
1137 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001138 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001140 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001143 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144
1145 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 if (unlikely(!buf_len))
1147 return -EINVAL;
1148
Allan Stephens0c3141e2008-04-15 00:22:02 -07001149 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001151 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152 res = -ENOTCONN;
1153 goto exit;
1154 }
1155
Florian Westphal3720d402010-08-17 11:00:04 +00001156 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001157 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001158
Allan Stephens0c3141e2008-04-15 00:22:02 -07001159restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001160 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001161 res = tipc_wait_for_rcvmsg(sock, timeo);
1162 if (res)
1163 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001164
1165 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001166 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 msg = buf_msg(buf);
1168 sz = msg_data_sz(msg);
1169 err = msg_errcode(msg);
1170
1171 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001173 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 goto restart;
1175 }
1176
1177 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 if (sz_copied == 0) {
1179 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001180 res = anc_data_recv(m, msg, tport);
1181 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 goto exit;
1183 }
1184
1185 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001187 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188
Allan Stephens0232fd02011-02-21 09:45:40 -05001189 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 needed = (buf_len - sz_copied);
1191 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001192
1193 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1194 m->msg_iov, sz_to_copy);
1195 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001197
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 sz_copied += sz_to_copy;
1199
1200 if (sz_to_copy < sz) {
1201 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001202 TIPC_SKB_CB(buf)->handle =
1203 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 goto exit;
1205 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 } else {
1207 if (sz_copied != 0)
1208 goto exit; /* can't add error msg to valid data */
1209
1210 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1211 res = 0;
1212 else
1213 res = -ECONNRESET;
1214 }
1215
1216 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001218 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1219 tipc_acknowledge(tport->ref, tport->conn_unacked);
1220 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001221 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222
1223 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001224 if ((sz_copied < buf_len) && /* didn't get all requested data */
1225 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001226 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001227 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1228 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 goto restart;
1230
1231exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001232 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001233 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234}
1235
1236/**
Ying Xuef288bef2012-08-21 11:16:57 +08001237 * tipc_write_space - wake up thread if port congestion is released
1238 * @sk: socket
1239 */
1240static void tipc_write_space(struct sock *sk)
1241{
1242 struct socket_wq *wq;
1243
1244 rcu_read_lock();
1245 wq = rcu_dereference(sk->sk_wq);
1246 if (wq_has_sleeper(wq))
1247 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1248 POLLWRNORM | POLLWRBAND);
1249 rcu_read_unlock();
1250}
1251
1252/**
1253 * tipc_data_ready - wake up threads to indicate messages have been received
1254 * @sk: socket
1255 * @len: the length of messages
1256 */
1257static void tipc_data_ready(struct sock *sk, int len)
1258{
1259 struct socket_wq *wq;
1260
1261 rcu_read_lock();
1262 wq = rcu_dereference(sk->sk_wq);
1263 if (wq_has_sleeper(wq))
1264 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1265 POLLRDNORM | POLLRDBAND);
1266 rcu_read_unlock();
1267}
1268
1269/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001270 * filter_connect - Handle all incoming messages for a connection-based socket
1271 * @tsock: TIPC socket
1272 * @msg: message
1273 *
1274 * Returns TIPC error status code and socket error status code
1275 * once it encounters some errors
1276 */
1277static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1278{
1279 struct socket *sock = tsock->sk.sk_socket;
1280 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001281 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001282 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001283 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001284
1285 if (msg_mcast(msg))
1286 return retval;
1287
1288 switch ((int)sock->state) {
1289 case SS_CONNECTED:
1290 /* Accept only connection-based messages sent by peer */
1291 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1292 if (unlikely(msg_errcode(msg))) {
1293 sock->state = SS_DISCONNECTING;
Ying Xue247f0f32014-02-18 16:06:46 +08001294 __tipc_port_disconnect(tsock->p);
Ying Xue7e6c1312012-11-29 18:39:14 -05001295 }
1296 retval = TIPC_OK;
1297 }
1298 break;
1299 case SS_CONNECTING:
1300 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001301 if (unlikely(msg_errcode(msg))) {
1302 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001303 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001304 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001305 break;
1306 }
1307
1308 if (unlikely(!msg_connected(msg)))
1309 break;
1310
1311 res = auto_connect(sock, msg);
1312 if (res) {
1313 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001314 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001315 retval = TIPC_OK;
1316 break;
1317 }
1318
1319 /* If an incoming message is an 'ACK-', it should be
1320 * discarded here because it doesn't contain useful
1321 * data. In addition, we should try to wake up
1322 * connect() routine if sleeping.
1323 */
1324 if (msg_data_sz(msg) == 0) {
1325 kfree_skb(*buf);
1326 *buf = NULL;
1327 if (waitqueue_active(sk_sleep(sk)))
1328 wake_up_interruptible(sk_sleep(sk));
1329 }
1330 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001331 break;
1332 case SS_LISTENING:
1333 case SS_UNCONNECTED:
1334 /* Accept only SYN message */
1335 if (!msg_connected(msg) && !(msg_errcode(msg)))
1336 retval = TIPC_OK;
1337 break;
1338 case SS_DISCONNECTING:
1339 break;
1340 default:
1341 pr_err("Unknown socket state %u\n", sock->state);
1342 }
1343 return retval;
1344}
1345
1346/**
Ying Xueaba79f32013-01-20 23:30:09 +01001347 * rcvbuf_limit - get proper overload limit of socket receive queue
1348 * @sk: socket
1349 * @buf: message
1350 *
1351 * For all connection oriented messages, irrespective of importance,
1352 * the default overload value (i.e. 67MB) is set as limit.
1353 *
1354 * For all connectionless messages, by default new queue limits are
1355 * as belows:
1356 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001357 * TIPC_LOW_IMPORTANCE (4 MB)
1358 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1359 * TIPC_HIGH_IMPORTANCE (16 MB)
1360 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001361 *
1362 * Returns overload limit according to corresponding message importance
1363 */
1364static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1365{
1366 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001367
1368 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001369 return sysctl_tipc_rmem[2];
1370
1371 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1372 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001373}
1374
1375/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001376 * filter_rcv - validate incoming message
1377 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001379 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001380 * Enqueues message on receive queue if acceptable; optionally handles
1381 * disconnect indication for a connected socket.
1382 *
1383 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001384 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1386 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001387static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001389 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001391 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001392 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001395 if (msg_type(msg) > TIPC_DIRECT_MSG)
1396 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001397
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001399 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001402 res = filter_connect(tipc_sk(sk), &buf);
1403 if (res != TIPC_OK || buf == NULL)
1404 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001405 }
1406
1407 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001408 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1409 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410
Ying Xueaba79f32013-01-20 23:30:09 +01001411 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001412 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001413 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001414 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001415
Ying Xuef288bef2012-08-21 11:16:57 +08001416 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 return TIPC_OK;
1418}
1419
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001420/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001421 * backlog_rcv - handle incoming message from backlog queue
1422 * @sk: socket
1423 * @buf: message
1424 *
1425 * Caller must hold socket lock, but not port lock.
1426 *
1427 * Returns 0
1428 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001429static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1430{
1431 u32 res;
1432
1433 res = filter_rcv(sk, buf);
1434 if (res)
1435 tipc_reject_msg(buf, res);
1436 return 0;
1437}
1438
1439/**
1440 * dispatch - handle incoming message
1441 * @tport: TIPC port that received message
1442 * @buf: message
1443 *
1444 * Called with port lock already taken.
1445 *
1446 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1447 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001448static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1449{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001450 struct sock *sk = tport->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001451 u32 res;
1452
1453 /*
1454 * Process message if socket is unlocked; otherwise add to backlog queue
1455 *
1456 * This code is based on sk_receive_skb(), but must be distinct from it
1457 * since a TIPC-specific filter/reject mechanism is utilized
1458 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001459 bh_lock_sock(sk);
1460 if (!sock_owned_by_user(sk)) {
1461 res = filter_rcv(sk, buf);
1462 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001463 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001464 res = TIPC_ERR_OVERLOAD;
1465 else
1466 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001467 }
1468 bh_unlock_sock(sk);
1469
1470 return res;
1471}
1472
1473/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001474 * wakeupdispatch - wake up port after congestion
1475 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001476 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001477 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479static void wakeupdispatch(struct tipc_port *tport)
1480{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001481 struct sock *sk = tport->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482
Ying Xuef288bef2012-08-21 11:16:57 +08001483 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484}
1485
Ying Xue78eb3a52014-01-17 09:50:03 +08001486static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1487{
1488 struct sock *sk = sock->sk;
1489 DEFINE_WAIT(wait);
1490 int done;
1491
1492 do {
1493 int err = sock_error(sk);
1494 if (err)
1495 return err;
1496 if (!*timeo_p)
1497 return -ETIMEDOUT;
1498 if (signal_pending(current))
1499 return sock_intr_errno(*timeo_p);
1500
1501 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1502 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1503 finish_wait(sk_sleep(sk), &wait);
1504 } while (!done);
1505 return 0;
1506}
1507
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508/**
Ying Xue247f0f32014-02-18 16:06:46 +08001509 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510 * @sock: socket structure
1511 * @dest: socket address for destination port
1512 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001513 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514 *
1515 * Returns 0 on success, errno otherwise
1516 */
Ying Xue247f0f32014-02-18 16:06:46 +08001517static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1518 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001519{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001520 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001521 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1522 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001523 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1524 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001525 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526
Allan Stephens0c3141e2008-04-15 00:22:02 -07001527 lock_sock(sk);
1528
Allan Stephensb89741a2008-04-15 00:20:37 -07001529 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001530 if (sock->state == SS_READY) {
1531 res = -EOPNOTSUPP;
1532 goto exit;
1533 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534
Allan Stephensb89741a2008-04-15 00:20:37 -07001535 /*
1536 * Reject connection attempt using multicast address
1537 *
1538 * Note: send_msg() validates the rest of the address fields,
1539 * so there's no need to do it here
1540 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001541 if (dst->addrtype == TIPC_ADDR_MCAST) {
1542 res = -EINVAL;
1543 goto exit;
1544 }
1545
Ying Xue78eb3a52014-01-17 09:50:03 +08001546 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001547 switch (sock->state) {
1548 case SS_UNCONNECTED:
1549 /* Send a 'SYN-' to destination */
1550 m.msg_name = dest;
1551 m.msg_namelen = destlen;
1552
1553 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1554 * indicate send_msg() is never blocked.
1555 */
1556 if (!timeout)
1557 m.msg_flags = MSG_DONTWAIT;
1558
Ying Xue247f0f32014-02-18 16:06:46 +08001559 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001560 if ((res < 0) && (res != -EWOULDBLOCK))
1561 goto exit;
1562
1563 /* Just entered SS_CONNECTING state; the only
1564 * difference is that return value in non-blocking
1565 * case is EINPROGRESS, rather than EALREADY.
1566 */
1567 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001568 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001569 if (previous == SS_CONNECTING)
1570 res = -EALREADY;
1571 if (!timeout)
1572 goto exit;
1573 timeout = msecs_to_jiffies(timeout);
1574 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1575 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001576 break;
1577 case SS_CONNECTED:
1578 res = -EISCONN;
1579 break;
1580 default:
1581 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001582 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001583 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001584exit:
1585 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001586 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587}
1588
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001589/**
Ying Xue247f0f32014-02-18 16:06:46 +08001590 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001591 * @sock: socket structure
1592 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001593 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594 * Returns 0 on success, errno otherwise
1595 */
Ying Xue247f0f32014-02-18 16:06:46 +08001596static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001598 struct sock *sk = sock->sk;
1599 int res;
1600
1601 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602
Ying Xue245f3d32011-07-06 06:01:13 -04001603 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001604 res = -EINVAL;
1605 else {
1606 sock->state = SS_LISTENING;
1607 res = 0;
1608 }
1609
1610 release_sock(sk);
1611 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001612}
1613
Ying Xue6398e232014-01-17 09:50:04 +08001614static int tipc_wait_for_accept(struct socket *sock, long timeo)
1615{
1616 struct sock *sk = sock->sk;
1617 DEFINE_WAIT(wait);
1618 int err;
1619
1620 /* True wake-one mechanism for incoming connections: only
1621 * one process gets woken up, not the 'whole herd'.
1622 * Since we do not 'race & poll' for established sockets
1623 * anymore, the common case will execute the loop only once.
1624 */
1625 for (;;) {
1626 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1627 TASK_INTERRUPTIBLE);
1628 if (skb_queue_empty(&sk->sk_receive_queue)) {
1629 release_sock(sk);
1630 timeo = schedule_timeout(timeo);
1631 lock_sock(sk);
1632 }
1633 err = 0;
1634 if (!skb_queue_empty(&sk->sk_receive_queue))
1635 break;
1636 err = -EINVAL;
1637 if (sock->state != SS_LISTENING)
1638 break;
1639 err = sock_intr_errno(timeo);
1640 if (signal_pending(current))
1641 break;
1642 err = -EAGAIN;
1643 if (!timeo)
1644 break;
1645 }
1646 finish_wait(sk_sleep(sk), &wait);
1647 return err;
1648}
1649
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001650/**
Ying Xue247f0f32014-02-18 16:06:46 +08001651 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 * @sock: listening socket
1653 * @newsock: new socket that is to be connected
1654 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001655 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656 * Returns 0 on success, errno otherwise
1657 */
Ying Xue247f0f32014-02-18 16:06:46 +08001658static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001660 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001662 struct tipc_sock *new_tsock;
1663 struct tipc_port *new_tport;
1664 struct tipc_msg *msg;
1665 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001666 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001667 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668
Allan Stephens0c3141e2008-04-15 00:22:02 -07001669 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001670
Allan Stephens0c3141e2008-04-15 00:22:02 -07001671 if (sock->state != SS_LISTENING) {
1672 res = -EINVAL;
1673 goto exit;
1674 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675
Ying Xue6398e232014-01-17 09:50:04 +08001676 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1677 res = tipc_wait_for_accept(sock, timeo);
1678 if (res)
1679 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001680
1681 buf = skb_peek(&sk->sk_receive_queue);
1682
Ying Xuec5fa7b32013-06-17 10:54:39 -04001683 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001684 if (res)
1685 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001686
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001687 new_sk = new_sock->sk;
1688 new_tsock = tipc_sk(new_sk);
1689 new_tport = new_tsock->p;
1690 new_ref = new_tport->ref;
1691 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001692
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001693 /* we lock on new_sk; but lockdep sees the lock on sk */
1694 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001695
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001696 /*
1697 * Reject any stray messages received by new socket
1698 * before the socket lock was taken (very, very unlikely)
1699 */
1700 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001702 /* Connect new socket to it's peer */
1703 new_tsock->peer_name.ref = msg_origport(msg);
1704 new_tsock->peer_name.node = msg_orignode(msg);
Ying Xue247f0f32014-02-18 16:06:46 +08001705 tipc_port_connect(new_ref, &new_tsock->peer_name);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001706 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001708 tipc_set_portimportance(new_ref, msg_importance(msg));
1709 if (msg_named(msg)) {
1710 new_tport->conn_type = msg_nametype(msg);
1711 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001713
1714 /*
1715 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1716 * Respond to 'SYN+' by queuing it on new socket.
1717 */
1718 if (!msg_data_sz(msg)) {
1719 struct msghdr m = {NULL,};
1720
1721 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001722 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001723 } else {
1724 __skb_dequeue(&sk->sk_receive_queue);
1725 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001726 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001727 }
1728 release_sock(new_sk);
1729
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732 return res;
1733}
1734
1735/**
Ying Xue247f0f32014-02-18 16:06:46 +08001736 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001738 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 *
1740 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001741 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742 * Returns 0 on success, errno otherwise
1743 */
Ying Xue247f0f32014-02-18 16:06:46 +08001744static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001746 struct sock *sk = sock->sk;
1747 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748 struct sk_buff *buf;
1749 int res;
1750
Allan Stephense247a8f2008-03-06 15:05:38 -08001751 if (how != SHUT_RDWR)
1752 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753
Allan Stephens0c3141e2008-04-15 00:22:02 -07001754 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755
1756 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001757 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001758 case SS_CONNECTED:
1759
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001761 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001762 buf = __skb_dequeue(&sk->sk_receive_queue);
1763 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001764 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001765 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001766 goto restart;
1767 }
Ying Xue247f0f32014-02-18 16:06:46 +08001768 tipc_port_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001769 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001770 } else {
Ying Xue247f0f32014-02-18 16:06:46 +08001771 tipc_port_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001773
1774 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001775
1776 /* fall through */
1777
1778 case SS_DISCONNECTING:
1779
Ying Xue75031152012-10-29 09:38:15 -04001780 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001781 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001782
1783 /* Wake up anyone sleeping in poll */
1784 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001785 res = 0;
1786 break;
1787
1788 default:
1789 res = -ENOTCONN;
1790 }
1791
Allan Stephens0c3141e2008-04-15 00:22:02 -07001792 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793 return res;
1794}
1795
1796/**
Ying Xue247f0f32014-02-18 16:06:46 +08001797 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001798 * @sock: socket structure
1799 * @lvl: option level
1800 * @opt: option identifier
1801 * @ov: pointer to new option value
1802 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001803 *
1804 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001806 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001807 * Returns 0 on success, errno otherwise
1808 */
Ying Xue247f0f32014-02-18 16:06:46 +08001809static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1810 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001811{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001812 struct sock *sk = sock->sk;
1813 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814 u32 value;
1815 int res;
1816
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001817 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1818 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001819 if (lvl != SOL_TIPC)
1820 return -ENOPROTOOPT;
1821 if (ol < sizeof(value))
1822 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001823 res = get_user(value, (u32 __user *)ov);
1824 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825 return res;
1826
Allan Stephens0c3141e2008-04-15 00:22:02 -07001827 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001828
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 switch (opt) {
1830 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001831 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001832 break;
1833 case TIPC_SRC_DROPPABLE:
1834 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001835 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001836 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837 res = -ENOPROTOOPT;
1838 break;
1839 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001840 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841 break;
1842 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001843 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001844 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001845 break;
1846 default:
1847 res = -EINVAL;
1848 }
1849
Allan Stephens0c3141e2008-04-15 00:22:02 -07001850 release_sock(sk);
1851
Per Lidenb97bf3f2006-01-02 19:04:38 +01001852 return res;
1853}
1854
1855/**
Ying Xue247f0f32014-02-18 16:06:46 +08001856 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001857 * @sock: socket structure
1858 * @lvl: option level
1859 * @opt: option identifier
1860 * @ov: receptacle for option value
1861 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001862 *
1863 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001864 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001865 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001866 * Returns 0 on success, errno otherwise
1867 */
Ying Xue247f0f32014-02-18 16:06:46 +08001868static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1869 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001871 struct sock *sk = sock->sk;
1872 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001873 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001874 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001875 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001877 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1878 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879 if (lvl != SOL_TIPC)
1880 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001881 res = get_user(len, ol);
1882 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001883 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884
Allan Stephens0c3141e2008-04-15 00:22:02 -07001885 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886
1887 switch (opt) {
1888 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001889 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890 break;
1891 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001892 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893 break;
1894 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001895 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896 break;
1897 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001898 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001899 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001901 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001902 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001903 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001904 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001905 value = skb_queue_len(&sk->sk_receive_queue);
1906 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907 default:
1908 res = -EINVAL;
1909 }
1910
Allan Stephens0c3141e2008-04-15 00:22:02 -07001911 release_sock(sk);
1912
Paul Gortmaker25860c32010-12-31 18:59:31 +00001913 if (res)
1914 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915
Paul Gortmaker25860c32010-12-31 18:59:31 +00001916 if (len < sizeof(value))
1917 return -EINVAL;
1918
1919 if (copy_to_user(ov, &value, sizeof(value)))
1920 return -EFAULT;
1921
1922 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001923}
1924
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001925/* Protocol switches for the various types of TIPC sockets */
1926
Florian Westphalbca65ea2008-02-07 18:18:01 -08001927static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001928 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001930 .release = tipc_release,
1931 .bind = tipc_bind,
1932 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001933 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001934 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08001935 .getname = tipc_getname,
1936 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001937 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001938 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08001939 .shutdown = tipc_shutdown,
1940 .setsockopt = tipc_setsockopt,
1941 .getsockopt = tipc_getsockopt,
1942 .sendmsg = tipc_sendmsg,
1943 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001944 .mmap = sock_no_mmap,
1945 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946};
1947
Florian Westphalbca65ea2008-02-07 18:18:01 -08001948static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001949 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001950 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001951 .release = tipc_release,
1952 .bind = tipc_bind,
1953 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001954 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001955 .accept = tipc_accept,
1956 .getname = tipc_getname,
1957 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001958 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001959 .listen = tipc_listen,
1960 .shutdown = tipc_shutdown,
1961 .setsockopt = tipc_setsockopt,
1962 .getsockopt = tipc_getsockopt,
1963 .sendmsg = tipc_send_packet,
1964 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001965 .mmap = sock_no_mmap,
1966 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001967};
1968
Florian Westphalbca65ea2008-02-07 18:18:01 -08001969static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001970 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001971 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001972 .release = tipc_release,
1973 .bind = tipc_bind,
1974 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001975 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001976 .accept = tipc_accept,
1977 .getname = tipc_getname,
1978 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001979 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001980 .listen = tipc_listen,
1981 .shutdown = tipc_shutdown,
1982 .setsockopt = tipc_setsockopt,
1983 .getsockopt = tipc_getsockopt,
1984 .sendmsg = tipc_send_stream,
1985 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001986 .mmap = sock_no_mmap,
1987 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001988};
1989
Florian Westphalbca65ea2008-02-07 18:18:01 -08001990static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001991 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001993 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001994};
1995
1996static struct proto tipc_proto = {
1997 .name = "TIPC",
1998 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04001999 .obj_size = sizeof(struct tipc_sock),
2000 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002001};
2002
Ying Xuec5fa7b32013-06-17 10:54:39 -04002003static struct proto tipc_proto_kern = {
2004 .name = "TIPC",
2005 .obj_size = sizeof(struct tipc_sock),
2006 .sysctl_rmem = sysctl_tipc_rmem
2007};
2008
Per Lidenb97bf3f2006-01-02 19:04:38 +01002009/**
Per Liden4323add2006-01-18 00:38:21 +01002010 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002011 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002012 * Returns 0 on success, errno otherwise
2013 */
Per Liden4323add2006-01-18 00:38:21 +01002014int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002015{
2016 int res;
2017
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002018 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002019 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002020 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002021 goto out;
2022 }
2023
2024 res = sock_register(&tipc_family_ops);
2025 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002026 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027 proto_unregister(&tipc_proto);
2028 goto out;
2029 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002030 out:
2031 return res;
2032}
2033
2034/**
Per Liden4323add2006-01-18 00:38:21 +01002035 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002036 */
Per Liden4323add2006-01-18 00:38:21 +01002037void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002038{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039 sock_unregister(tipc_family_ops.family);
2040 proto_unregister(&tipc_proto);
2041}