blob: 6c71988298053885308ebc61fea70554292e0324 [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 Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Erik Hugne2cf8aa12012-06-29 00:16:37 -040040#include <linux/export.h>
Erik Hugne2cf8aa12012-06-29 00:16:37 -040041
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#define SS_LISTENING -1 /* socket is listening */
43#define SS_READY -2 /* socket is connectionless */
44
Allan Stephens3654ea02008-04-13 21:35:11 -070045#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010046
Allan Stephens0c3141e2008-04-15 00:22:02 -070047static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Ying Xuef288bef2012-08-21 11:16:57 +080048static void tipc_data_ready(struct sock *sk, int len);
49static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080050static int tipc_release(struct socket *sock);
51static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010052
Florian Westphalbca65ea2008-02-07 18:18:01 -080053static const struct proto_ops packet_ops;
54static const struct proto_ops stream_ops;
55static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010056
57static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040058static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010059
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090060/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070061 * Revised TIPC socket locking policy:
62 *
63 * Most socket operations take the standard socket lock when they start
64 * and hold it until they finish (or until they need to sleep). Acquiring
65 * this lock grants the owner exclusive access to the fields of the socket
66 * data structures, with the exception of the backlog queue. A few socket
67 * operations can be done without taking the socket lock because they only
68 * read socket information that never changes during the life of the socket.
69 *
70 * Socket operations may acquire the lock for the associated TIPC port if they
71 * need to perform an operation on the port. If any routine needs to acquire
72 * both the socket lock and the port lock it must take the socket lock first
73 * to avoid the risk of deadlock.
74 *
75 * The dispatcher handling incoming messages cannot grab the socket lock in
76 * the standard fashion, since invoked it runs at the BH level and cannot block.
77 * Instead, it checks to see if the socket lock is currently owned by someone,
78 * and either handles the message itself or adds it to the socket's backlog
79 * queue; in the latter case the queued message is processed once the process
80 * owning the socket lock releases it.
81 *
82 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
83 * the problem of a blocked socket operation preventing any other operations
84 * from occurring. However, applications must be careful if they have
85 * multiple threads trying to send (or receive) on the same socket, as these
86 * operations might interfere with each other. For example, doing a connect
87 * and a receive at the same time might allow the receive to consume the
88 * ACK message meant for the connect. While additional work could be done
89 * to try and overcome this, it doesn't seem to be worthwhile at the present.
90 *
91 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
92 * that another operation that must be performed in a non-blocking manner is
93 * not delayed for very long because the lock has already been taken.
94 *
95 * NOTE: This code assumes that certain fields of a port/socket pair are
96 * constant over its lifetime; such fields can be examined without taking
97 * the socket lock and/or port lock, and do not need to be re-read even
98 * after resuming processing after waiting. These fields include:
99 * - socket type
100 * - pointer to socket sk structure (aka tipc_sock structure)
101 * - pointer to port structure
102 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400105#include "socket.h"
106
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700108 * advance_rx_queue - discard first buffer in socket receive queue
109 *
110 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700112static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400114 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115}
116
117/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700118 * reject_rx_queue - reject all buffers in socket receive queue
119 *
120 * Caller must hold socket lock
121 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700122static void reject_rx_queue(struct sock *sk)
123{
124 struct sk_buff *buf;
125
Ying Xue9da3d472012-11-27 06:15:27 -0500126 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700127 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700128}
129
130/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400131 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700132 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133 * @sock: pre-allocated socket structure
134 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800135 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900136 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700137 * This routine creates additional data structures used by the TIPC socket,
138 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 *
140 * Returns 0 on success, errno otherwise
141 */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400142static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
143 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700145 const struct proto_ops *ops;
146 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700148 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700149
150 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 if (unlikely(protocol != 0))
152 return -EPROTONOSUPPORT;
153
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 switch (sock->type) {
155 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700156 ops = &stream_ops;
157 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 break;
159 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700160 ops = &packet_ops;
161 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 break;
163 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700165 ops = &msg_ops;
166 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 break;
Allan Stephens49978652006-06-25 23:47:18 -0700168 default:
Allan Stephens49978652006-06-25 23:47:18 -0700169 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 }
171
Allan Stephens0c3141e2008-04-15 00:22:02 -0700172 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400173 if (!kern)
174 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
175 else
176 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
177
Allan Stephens0c3141e2008-04-15 00:22:02 -0700178 if (sk == NULL)
179 return -ENOMEM;
180
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400181 tp_ptr = tipc_sk_port(sk);
182 if (!tipc_port_init(tp_ptr, TIPC_LOW_IMPORTANCE)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700183 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 return -ENOMEM;
185 }
186
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700188 sock->ops = ops;
189 sock->state = state;
190
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700192 sk->sk_backlog_rcv = backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400193 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800194 sk->sk_data_ready = tipc_data_ready;
195 sk->sk_write_space = tipc_write_space;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400196 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700197 spin_unlock_bh(tp_ptr->lock);
198
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199 if (sock->state == SS_READY) {
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -0400200 tipc_port_set_unreturnable(tp_ptr, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -0400202 tipc_port_set_unreliable(tp_ptr, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700203 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 return 0;
205}
206
207/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400208 * tipc_sock_create_local - create TIPC socket from inside TIPC module
209 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
210 *
211 * We cannot use sock_creat_kern here because it bumps module user count.
212 * Since socket owner and creator is the same module we must make sure
213 * that module count remains zero for module local sockets, otherwise
214 * we cannot do rmmod.
215 *
216 * Returns 0 on success, errno otherwise
217 */
218int tipc_sock_create_local(int type, struct socket **res)
219{
220 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400221
222 rc = sock_create_lite(AF_TIPC, type, 0, res);
223 if (rc < 0) {
224 pr_err("Failed to create kernel socket\n");
225 return rc;
226 }
227 tipc_sk_create(&init_net, *res, 0, 1);
228
Ying Xuec5fa7b32013-06-17 10:54:39 -0400229 return 0;
230}
231
232/**
233 * tipc_sock_release_local - release socket created by tipc_sock_create_local
234 * @sock: the socket to be released.
235 *
236 * Module reference count is not incremented when such sockets are created,
237 * so we must keep it from being decremented when they are released.
238 */
239void tipc_sock_release_local(struct socket *sock)
240{
Ying Xue247f0f32014-02-18 16:06:46 +0800241 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400242 sock->ops = NULL;
243 sock_release(sock);
244}
245
246/**
247 * tipc_sock_accept_local - accept a connection on a socket created
248 * with tipc_sock_create_local. Use this function to avoid that
249 * module reference count is inadvertently incremented.
250 *
251 * @sock: the accepting socket
252 * @newsock: reference to the new socket to be created
253 * @flags: socket flags
254 */
255
256int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400257 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400258{
259 struct sock *sk = sock->sk;
260 int ret;
261
262 ret = sock_create_lite(sk->sk_family, sk->sk_type,
263 sk->sk_protocol, newsock);
264 if (ret < 0)
265 return ret;
266
Ying Xue247f0f32014-02-18 16:06:46 +0800267 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400268 if (ret < 0) {
269 sock_release(*newsock);
270 return ret;
271 }
272 (*newsock)->ops = sock->ops;
273 return ret;
274}
275
276/**
Ying Xue247f0f32014-02-18 16:06:46 +0800277 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 * @sock: socket to destroy
279 *
280 * This routine cleans up any messages that are still queued on the socket.
281 * For DGRAM and RDM socket types, all queued messages are rejected.
282 * For SEQPACKET and STREAM socket types, the first message is rejected
283 * and any others are discarded. (If the first message on a STREAM socket
284 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900285 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 * NOTE: Rejected messages are not necessarily returned to the sender! They
287 * are returned or discarded according to the "destination droppable" setting
288 * specified for the message by the sender.
289 *
290 * Returns 0 on success, errno otherwise
291 */
Ying Xue247f0f32014-02-18 16:06:46 +0800292static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700295 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700297 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298
Allan Stephens0c3141e2008-04-15 00:22:02 -0700299 /*
300 * Exit if socket isn't fully initialized (occurs when a failed accept()
301 * releases a pre-allocated child socket that was never used)
302 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700303 if (sk == NULL)
304 return 0;
305
306 tport = tipc_sk_port(sk);
307 lock_sock(sk);
308
309 /*
310 * Reject all unreceived messages, except on an active connection
311 * (which disconnects locally & sends a 'FIN+' to peer)
312 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700314 buf = __skb_dequeue(&sk->sk_receive_queue);
315 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316 break;
Ying Xue40682432013-10-18 07:23:16 +0200317 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400318 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700319 else {
320 if ((sock->state == SS_CONNECTING) ||
321 (sock->state == SS_CONNECTED)) {
322 sock->state = SS_DISCONNECTING;
Ying Xue247f0f32014-02-18 16:06:46 +0800323 tipc_port_disconnect(tport->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700324 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700326 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 }
328
Allan Stephens0c3141e2008-04-15 00:22:02 -0700329 /*
330 * Delete TIPC port; this ensures no more messages are queued
331 * (also disconnects an active connection & sends a 'FIN-' to peer)
332 */
Jon Paul Maloy24be34b2014-03-12 11:31:10 -0400333 tipc_port_destroy(tport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334
Allan Stephens0c3141e2008-04-15 00:22:02 -0700335 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100336 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337
Allan Stephens0c3141e2008-04-15 00:22:02 -0700338 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700339 sock->state = SS_DISCONNECTING;
340 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341
342 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700343 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 return res;
346}
347
348/**
Ying Xue247f0f32014-02-18 16:06:46 +0800349 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350 * @sock: socket structure
351 * @uaddr: socket address describing name(s) and desired operation
352 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900353 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354 * Name and name sequence binding is indicated using a positive scope value;
355 * a negative scope value unbinds the specified name. Specifying no name
356 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900357 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700359 *
360 * NOTE: This routine doesn't need to take the socket lock since it doesn't
361 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 */
Ying Xue247f0f32014-02-18 16:06:46 +0800363static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
364 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365{
Ying Xue84602762013-12-27 10:18:28 +0800366 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Ying Xue84602762013-12-27 10:18:28 +0800368 struct tipc_port *tport = tipc_sk_port(sock->sk);
369 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370
Ying Xue84602762013-12-27 10:18:28 +0800371 lock_sock(sk);
372 if (unlikely(!uaddr_len)) {
373 res = tipc_withdraw(tport, 0, NULL);
374 goto exit;
375 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900376
Ying Xue84602762013-12-27 10:18:28 +0800377 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
378 res = -EINVAL;
379 goto exit;
380 }
381 if (addr->family != AF_TIPC) {
382 res = -EAFNOSUPPORT;
383 goto exit;
384 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 if (addr->addrtype == TIPC_ADDR_NAME)
387 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800388 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
389 res = -EAFNOSUPPORT;
390 goto exit;
391 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900392
Ying Xue13a2e892013-06-17 10:54:40 -0400393 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400394 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800395 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
396 res = -EACCES;
397 goto exit;
398 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400399
Ying Xue84602762013-12-27 10:18:28 +0800400 res = (addr->scope > 0) ?
401 tipc_publish(tport, addr->scope, &addr->addr.nameseq) :
402 tipc_withdraw(tport, -addr->scope, &addr->addr.nameseq);
403exit:
404 release_sock(sk);
405 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406}
407
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900408/**
Ying Xue247f0f32014-02-18 16:06:46 +0800409 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 * @sock: socket structure
411 * @uaddr: area for returned socket address
412 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700413 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900414 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700416 *
Allan Stephens2da59912008-07-14 22:43:32 -0700417 * NOTE: This routine doesn't need to take the socket lock since it only
418 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000419 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 */
Ying Xue247f0f32014-02-18 16:06:46 +0800421static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
422 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400425 struct tipc_port *port = tipc_sk_port(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000427 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700428 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700429 if ((sock->state != SS_CONNECTED) &&
430 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
431 return -ENOTCONN;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400432 addr->addr.id.ref = tipc_port_peerport(port);
433 addr->addr.id.node = tipc_port_peernode(port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700434 } else {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400435 addr->addr.id.ref = port->ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000436 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700437 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438
439 *uaddr_len = sizeof(*addr);
440 addr->addrtype = TIPC_ADDR_ID;
441 addr->family = AF_TIPC;
442 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 addr->addr.name.domain = 0;
444
Allan Stephens0c3141e2008-04-15 00:22:02 -0700445 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446}
447
448/**
Ying Xue247f0f32014-02-18 16:06:46 +0800449 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 * @file: file structure associated with the socket
451 * @sock: socket for which to calculate the poll bits
452 * @wait: ???
453 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700454 * Returns pollmask value
455 *
456 * COMMENTARY:
457 * It appears that the usual socket locking mechanisms are not useful here
458 * since the pollmask info is potentially out-of-date the moment this routine
459 * exits. TCP and other protocols seem to rely on higher level poll routines
460 * to handle any preventable race conditions, so TIPC will do the same ...
461 *
462 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700463 *
Allan Stephensf662c072010-08-17 11:00:06 +0000464 * socket state flags set
465 * ------------ ---------
466 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200467 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000468 *
469 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
470 * no write flags
471 *
472 * connected POLLIN/POLLRDNORM if data in rx queue
473 * POLLOUT if port is not congested
474 *
475 * disconnecting POLLIN/POLLRDNORM/POLLHUP
476 * no write flags
477 *
478 * listening POLLIN if SYN in rx queue
479 * no write flags
480 *
481 * ready POLLIN/POLLRDNORM if data in rx queue
482 * [connectionless] POLLOUT (since port cannot be congested)
483 *
484 * IMPORTANT: The fact that a read or write operation is indicated does NOT
485 * imply that the operation will succeed, merely that it should be performed
486 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 */
Ying Xue247f0f32014-02-18 16:06:46 +0800488static unsigned int tipc_poll(struct file *file, struct socket *sock,
489 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490{
Allan Stephens9b674e82008-03-26 16:48:21 -0700491 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000492 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700493
Ying Xuef288bef2012-08-21 11:16:57 +0800494 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700495
Allan Stephensf662c072010-08-17 11:00:06 +0000496 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200497 case SS_UNCONNECTED:
498 if (!tipc_sk_port(sk)->congested)
499 mask |= POLLOUT;
500 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000501 case SS_READY:
502 case SS_CONNECTED:
503 if (!tipc_sk_port(sk)->congested)
504 mask |= POLLOUT;
505 /* fall thru' */
506 case SS_CONNECTING:
507 case SS_LISTENING:
508 if (!skb_queue_empty(&sk->sk_receive_queue))
509 mask |= (POLLIN | POLLRDNORM);
510 break;
511 case SS_DISCONNECTING:
512 mask = (POLLIN | POLLRDNORM | POLLHUP);
513 break;
514 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700515
516 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517}
518
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900519/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 * dest_name_check - verify user is permitted to send to specified port name
521 * @dest: destination address
522 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900523 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524 * Prevents restricted configuration commands from being issued by
525 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900526 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 * Returns 0 if permission is granted, otherwise errno
528 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800529static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530{
531 struct tipc_cfg_msg_hdr hdr;
532
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900533 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
534 return 0;
535 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
536 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900537 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
538 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539
Allan Stephens3f8dd942011-01-18 13:09:29 -0500540 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
541 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900542 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700544 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900546
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 return 0;
548}
549
Ying Xue3f405042014-01-17 09:50:05 +0800550static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
551{
552 struct sock *sk = sock->sk;
553 struct tipc_port *tport = tipc_sk_port(sk);
554 DEFINE_WAIT(wait);
555 int done;
556
557 do {
558 int err = sock_error(sk);
559 if (err)
560 return err;
561 if (sock->state == SS_DISCONNECTING)
562 return -EPIPE;
563 if (!*timeo_p)
564 return -EAGAIN;
565 if (signal_pending(current))
566 return sock_intr_errno(*timeo_p);
567
568 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
569 done = sk_wait_event(sk, timeo_p, !tport->congested);
570 finish_wait(sk_sleep(sk), &wait);
571 } while (!done);
572 return 0;
573}
574
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575/**
Ying Xue247f0f32014-02-18 16:06:46 +0800576 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700577 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 * @sock: socket structure
579 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700580 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900583 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
585 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900586 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 * Returns the number of bytes sent on success, or errno otherwise
588 */
Ying Xue247f0f32014-02-18 16:06:46 +0800589static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
590 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700592 struct sock *sk = sock->sk;
593 struct tipc_port *tport = tipc_sk_port(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100594 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 int needs_conn;
Ying Xue3f405042014-01-17 09:50:05 +0800596 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 int res = -EINVAL;
598
599 if (unlikely(!dest))
600 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700601 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
602 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100604 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400605 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606
Allan Stephens0c3141e2008-04-15 00:22:02 -0700607 if (iocb)
608 lock_sock(sk);
609
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 needs_conn = (sock->state != SS_READY);
611 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700612 if (sock->state == SS_LISTENING) {
613 res = -EPIPE;
614 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700615 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700616 if (sock->state != SS_UNCONNECTED) {
617 res = -EISCONN;
618 goto exit;
619 }
Erik Hugne5d21cb72013-06-17 10:54:38 -0400620 if (tport->published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700621 res = -EOPNOTSUPP;
622 goto exit;
623 }
624 if (dest->addrtype == TIPC_ADDR_NAME) {
625 tport->conn_type = dest->addr.name.name.type;
626 tport->conn_instance = dest->addr.name.name.instance;
627 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628
629 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700630 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 }
632
Ying Xue3f405042014-01-17 09:50:05 +0800633 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900634 do {
635 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000636 res = dest_name_check(dest, m);
637 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700638 break;
639 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900640 &dest->addr.name.name,
641 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500642 m->msg_iov,
643 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000644 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700645 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900646 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500647 m->msg_iov,
648 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000649 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 if (needs_conn) {
651 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700652 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 }
Allan Stephens2db99832010-12-31 18:59:33 +0000654 res = dest_name_check(dest, m);
655 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700656 break;
Ying Xue247f0f32014-02-18 16:06:46 +0800657 res = tipc_port_mcast_xmit(tport->ref,
658 &dest->addr.nameseq,
659 m->msg_iov,
660 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900661 }
662 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000663 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700664 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700665 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900666 }
Ying Xue3f405042014-01-17 09:50:05 +0800667 res = tipc_wait_for_sndmsg(sock, &timeo);
668 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700669 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900670 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700671
672exit:
673 if (iocb)
674 release_sock(sk);
675 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676}
677
Ying Xue391a6dd2014-01-17 09:50:06 +0800678static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
679{
680 struct sock *sk = sock->sk;
681 struct tipc_port *tport = tipc_sk_port(sk);
682 DEFINE_WAIT(wait);
683 int done;
684
685 do {
686 int err = sock_error(sk);
687 if (err)
688 return err;
689 if (sock->state == SS_DISCONNECTING)
690 return -EPIPE;
691 else if (sock->state != SS_CONNECTED)
692 return -ENOTCONN;
693 if (!*timeo_p)
694 return -EAGAIN;
695 if (signal_pending(current))
696 return sock_intr_errno(*timeo_p);
697
698 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
699 done = sk_wait_event(sk, timeo_p,
700 (!tport->congested || !tport->connected));
701 finish_wait(sk_sleep(sk), &wait);
702 } while (!done);
703 return 0;
704}
705
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900706/**
Ying Xue247f0f32014-02-18 16:06:46 +0800707 * tipc_send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700708 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709 * @sock: socket structure
710 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700711 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900712 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900714 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715 * Returns the number of bytes sent on success, or errno otherwise
716 */
Ying Xue247f0f32014-02-18 16:06:46 +0800717static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
718 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700720 struct sock *sk = sock->sk;
721 struct tipc_port *tport = tipc_sk_port(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100722 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue391a6dd2014-01-17 09:50:06 +0800723 int res = -EINVAL;
724 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725
726 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727 if (unlikely(dest))
Ying Xue247f0f32014-02-18 16:06:46 +0800728 return tipc_sendmsg(iocb, sock, m, total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729
Ying Xue97f8b872013-01-31 21:51:47 +0100730 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400731 return -EMSGSIZE;
732
Allan Stephens0c3141e2008-04-15 00:22:02 -0700733 if (iocb)
734 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735
Ying Xue391a6dd2014-01-17 09:50:06 +0800736 if (unlikely(sock->state != SS_CONNECTED)) {
737 if (sock->state == SS_DISCONNECTING)
738 res = -EPIPE;
739 else
740 res = -ENOTCONN;
741 goto exit;
742 }
Ying Xue1d835872011-07-06 05:53:15 -0400743
Ying Xue391a6dd2014-01-17 09:50:06 +0800744 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900745 do {
Ying Xue9446b872013-10-18 07:23:15 +0200746 res = tipc_send(tport->ref, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000747 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700748 break;
Ying Xue391a6dd2014-01-17 09:50:06 +0800749 res = tipc_wait_for_sndpkt(sock, &timeo);
750 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700751 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900752 } while (1);
Ying Xue391a6dd2014-01-17 09:50:06 +0800753exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700754 if (iocb)
755 release_sock(sk);
756 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757}
758
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900759/**
Ying Xue247f0f32014-02-18 16:06:46 +0800760 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 * @iocb: (unused)
762 * @sock: socket structure
763 * @m: data to send
764 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900765 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900767 *
768 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700769 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770 */
Ying Xue247f0f32014-02-18 16:06:46 +0800771static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
772 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700774 struct sock *sk = sock->sk;
775 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776 struct msghdr my_msg;
777 struct iovec my_iov;
778 struct iovec *curr_iov;
779 int curr_iovlen;
780 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700781 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 int curr_left;
783 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700784 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900786
Allan Stephens0c3141e2008-04-15 00:22:02 -0700787 lock_sock(sk);
788
Allan Stephens05646c92007-06-10 17:25:24 -0700789 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900790 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800791 if (sock->state == SS_UNCONNECTED)
Ying Xue247f0f32014-02-18 16:06:46 +0800792 res = tipc_send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800793 else
794 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800795 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900796 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797
Allan Stephens0c3141e2008-04-15 00:22:02 -0700798 if (unlikely(m->msg_name)) {
799 res = -EISCONN;
800 goto exit;
801 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700802
Ying Xue97f8b872013-01-31 21:51:47 +0100803 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400804 res = -EMSGSIZE;
805 goto exit;
806 }
807
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900808 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809 * Send each iovec entry using one or more messages
810 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900811 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 * (i.e. one large iovec entry), but could be improved to pass sets
813 * of small iovec entries into send_packet().
814 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700815 curr_iov = m->msg_iov;
816 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817 my_msg.msg_iov = &my_iov;
818 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700819 my_msg.msg_flags = m->msg_flags;
820 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700821 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822
Allan Stephens05646c92007-06-10 17:25:24 -0700823 hdr_size = msg_hdr_sz(&tport->phdr);
824
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 while (curr_iovlen--) {
826 curr_start = curr_iov->iov_base;
827 curr_left = curr_iov->iov_len;
828
829 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700830 bytes_to_send = tport->max_pkt - hdr_size;
831 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
832 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
833 if (curr_left < bytes_to_send)
834 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835 my_iov.iov_base = curr_start;
836 my_iov.iov_len = bytes_to_send;
Ying Xue247f0f32014-02-18 16:06:46 +0800837 res = tipc_send_packet(NULL, sock, &my_msg,
838 bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000839 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700840 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700841 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700842 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700843 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100844 curr_left -= bytes_to_send;
845 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700846 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 }
848
849 curr_iov++;
850 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700851 res = bytes_sent;
852exit:
853 release_sock(sk);
854 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855}
856
857/**
858 * auto_connect - complete connection setup to a remote port
859 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900861 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 * Returns 0 on success, errno otherwise
863 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700864static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865{
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400866 struct tipc_port *p_ptr = tipc_sk_port(sock->sk);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400867 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400869 peer.ref = msg_origport(msg);
870 peer.node = msg_orignode(msg);
871
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400872 p_ptr = tipc_port_deref(p_ptr->ref);
Ying Xue584d24b2012-11-29 18:51:19 -0500873 if (!p_ptr)
874 return -EINVAL;
875
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400876 __tipc_port_connect(p_ptr->ref, p_ptr, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -0500877
878 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
879 return -EINVAL;
880 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 sock->state = SS_CONNECTED;
882 return 0;
883}
884
885/**
886 * set_orig_addr - capture sender's address for received message
887 * @m: descriptor for message info
888 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900889 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890 * Note: Address is not captured if not requested by receiver.
891 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800892static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100894 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900896 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 addr->family = AF_TIPC;
898 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000899 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900 addr->addr.id.ref = msg_origport(msg);
901 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000902 addr->addr.name.domain = 0; /* could leave uninitialized */
903 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904 m->msg_namelen = sizeof(struct sockaddr_tipc);
905 }
906}
907
908/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900909 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 * @m: descriptor for message info
911 * @msg: received message header
912 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900913 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900915 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 * Returns 0 if successful, otherwise errno
917 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800918static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400919 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920{
921 u32 anc_data[3];
922 u32 err;
923 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700924 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925 int res;
926
927 if (likely(m->msg_controllen == 0))
928 return 0;
929
930 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 err = msg ? msg_errcode(msg) : 0;
932 if (unlikely(err)) {
933 anc_data[0] = err;
934 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000935 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
936 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000938 if (anc_data[1]) {
939 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
940 msg_data(msg));
941 if (res)
942 return res;
943 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100944 }
945
946 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
948 switch (dest_type) {
949 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700950 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951 anc_data[0] = msg_nametype(msg);
952 anc_data[1] = msg_namelower(msg);
953 anc_data[2] = msg_namelower(msg);
954 break;
955 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700956 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957 anc_data[0] = msg_nametype(msg);
958 anc_data[1] = msg_namelower(msg);
959 anc_data[2] = msg_nameupper(msg);
960 break;
961 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700962 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 anc_data[0] = tport->conn_type;
964 anc_data[1] = tport->conn_instance;
965 anc_data[2] = tport->conn_instance;
966 break;
967 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700968 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969 }
Allan Stephens2db99832010-12-31 18:59:33 +0000970 if (has_name) {
971 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
972 if (res)
973 return res;
974 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975
976 return 0;
977}
978
Ying Xue9bbb4ecc2014-01-17 09:50:07 +0800979static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
980{
981 struct sock *sk = sock->sk;
982 DEFINE_WAIT(wait);
983 int err;
984
985 for (;;) {
986 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
987 if (skb_queue_empty(&sk->sk_receive_queue)) {
988 if (sock->state == SS_DISCONNECTING) {
989 err = -ENOTCONN;
990 break;
991 }
992 release_sock(sk);
993 timeo = schedule_timeout(timeo);
994 lock_sock(sk);
995 }
996 err = 0;
997 if (!skb_queue_empty(&sk->sk_receive_queue))
998 break;
999 err = sock_intr_errno(timeo);
1000 if (signal_pending(current))
1001 break;
1002 err = -EAGAIN;
1003 if (!timeo)
1004 break;
1005 }
1006 finish_wait(sk_sleep(sk), &wait);
1007 return err;
1008}
1009
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001010/**
Ying Xue247f0f32014-02-18 16:06:46 +08001011 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 * @iocb: (unused)
1013 * @m: descriptor for message info
1014 * @buf_len: total size of user buffer area
1015 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001016 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1018 * If the complete message doesn't fit in user area, truncate it.
1019 *
1020 * Returns size of returned message data, errno otherwise
1021 */
Ying Xue247f0f32014-02-18 16:06:46 +08001022static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1023 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001025 struct sock *sk = sock->sk;
1026 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 struct sk_buff *buf;
1028 struct tipc_msg *msg;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001029 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 unsigned int sz;
1031 u32 err;
1032 int res;
1033
Allan Stephens0c3141e2008-04-15 00:22:02 -07001034 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 if (unlikely(!buf_len))
1036 return -EINVAL;
1037
Allan Stephens0c3141e2008-04-15 00:22:02 -07001038 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039
Allan Stephens0c3141e2008-04-15 00:22:02 -07001040 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 res = -ENOTCONN;
1042 goto exit;
1043 }
1044
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001045 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001046restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047
Allan Stephens0c3141e2008-04-15 00:22:02 -07001048 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001049 res = tipc_wait_for_rcvmsg(sock, timeo);
1050 if (res)
1051 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001052
1053 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001054 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 msg = buf_msg(buf);
1056 sz = msg_data_sz(msg);
1057 err = msg_errcode(msg);
1058
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001061 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 goto restart;
1063 }
1064
1065 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 set_orig_addr(m, msg);
1067
1068 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001069 res = anc_data_recv(m, msg, tport);
1070 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 goto exit;
1072
1073 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 if (!err) {
1075 if (unlikely(buf_len < sz)) {
1076 sz = buf_len;
1077 m->msg_flags |= MSG_TRUNC;
1078 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001079 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1080 m->msg_iov, sz);
1081 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 res = sz;
1084 } else {
1085 if ((sock->state == SS_READY) ||
1086 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1087 res = 0;
1088 else
1089 res = -ECONNRESET;
1090 }
1091
1092 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001094 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001095 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1096 tipc_acknowledge(tport->ref, tport->conn_unacked);
1097 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001098 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001100 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 return res;
1102}
1103
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001104/**
Ying Xue247f0f32014-02-18 16:06:46 +08001105 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106 * @iocb: (unused)
1107 * @m: descriptor for message info
1108 * @buf_len: total size of user buffer area
1109 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001110 *
1111 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112 * will optionally wait for more; never truncates data.
1113 *
1114 * Returns size of returned message data, errno otherwise
1115 */
Ying Xue247f0f32014-02-18 16:06:46 +08001116static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1117 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001119 struct sock *sk = sock->sk;
1120 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 struct sk_buff *buf;
1122 struct tipc_msg *msg;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001123 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001125 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001128 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129
1130 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131 if (unlikely(!buf_len))
1132 return -EINVAL;
1133
Allan Stephens0c3141e2008-04-15 00:22:02 -07001134 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001136 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 res = -ENOTCONN;
1138 goto exit;
1139 }
1140
Florian Westphal3720d402010-08-17 11:00:04 +00001141 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001142 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001143
Allan Stephens0c3141e2008-04-15 00:22:02 -07001144restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001145 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001146 res = tipc_wait_for_rcvmsg(sock, timeo);
1147 if (res)
1148 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001149
1150 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001151 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152 msg = buf_msg(buf);
1153 sz = msg_data_sz(msg);
1154 err = msg_errcode(msg);
1155
1156 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001158 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 goto restart;
1160 }
1161
1162 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 if (sz_copied == 0) {
1164 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001165 res = anc_data_recv(m, msg, tport);
1166 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 goto exit;
1168 }
1169
1170 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001172 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173
Allan Stephens0232fd02011-02-21 09:45:40 -05001174 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 needed = (buf_len - sz_copied);
1176 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001177
1178 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1179 m->msg_iov, sz_to_copy);
1180 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001182
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 sz_copied += sz_to_copy;
1184
1185 if (sz_to_copy < sz) {
1186 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001187 TIPC_SKB_CB(buf)->handle =
1188 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 goto exit;
1190 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 } else {
1192 if (sz_copied != 0)
1193 goto exit; /* can't add error msg to valid data */
1194
1195 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1196 res = 0;
1197 else
1198 res = -ECONNRESET;
1199 }
1200
1201 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001203 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1204 tipc_acknowledge(tport->ref, tport->conn_unacked);
1205 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001206 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207
1208 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001209 if ((sz_copied < buf_len) && /* didn't get all requested data */
1210 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001211 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001212 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1213 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 goto restart;
1215
1216exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001217 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001218 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219}
1220
1221/**
Ying Xuef288bef2012-08-21 11:16:57 +08001222 * tipc_write_space - wake up thread if port congestion is released
1223 * @sk: socket
1224 */
1225static void tipc_write_space(struct sock *sk)
1226{
1227 struct socket_wq *wq;
1228
1229 rcu_read_lock();
1230 wq = rcu_dereference(sk->sk_wq);
1231 if (wq_has_sleeper(wq))
1232 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1233 POLLWRNORM | POLLWRBAND);
1234 rcu_read_unlock();
1235}
1236
1237/**
1238 * tipc_data_ready - wake up threads to indicate messages have been received
1239 * @sk: socket
1240 * @len: the length of messages
1241 */
1242static void tipc_data_ready(struct sock *sk, int len)
1243{
1244 struct socket_wq *wq;
1245
1246 rcu_read_lock();
1247 wq = rcu_dereference(sk->sk_wq);
1248 if (wq_has_sleeper(wq))
1249 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1250 POLLRDNORM | POLLRDBAND);
1251 rcu_read_unlock();
1252}
1253
1254/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001255 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001256 * @port: TIPC port
Ying Xue7e6c1312012-11-29 18:39:14 -05001257 * @msg: message
1258 *
1259 * Returns TIPC error status code and socket error status code
1260 * once it encounters some errors
1261 */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001262static u32 filter_connect(struct tipc_port *port, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001263{
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001264 struct sock *sk = tipc_port_to_sk(port);
1265 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001266 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001267
Ying Xue7e6c1312012-11-29 18:39:14 -05001268 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001269 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001270
1271 if (msg_mcast(msg))
1272 return retval;
1273
1274 switch ((int)sock->state) {
1275 case SS_CONNECTED:
1276 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001277 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001278 if (unlikely(msg_errcode(msg))) {
1279 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001280 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001281 }
1282 retval = TIPC_OK;
1283 }
1284 break;
1285 case SS_CONNECTING:
1286 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001287 if (unlikely(msg_errcode(msg))) {
1288 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001289 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001290 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001291 break;
1292 }
1293
1294 if (unlikely(!msg_connected(msg)))
1295 break;
1296
1297 res = auto_connect(sock, msg);
1298 if (res) {
1299 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001300 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001301 retval = TIPC_OK;
1302 break;
1303 }
1304
1305 /* If an incoming message is an 'ACK-', it should be
1306 * discarded here because it doesn't contain useful
1307 * data. In addition, we should try to wake up
1308 * connect() routine if sleeping.
1309 */
1310 if (msg_data_sz(msg) == 0) {
1311 kfree_skb(*buf);
1312 *buf = NULL;
1313 if (waitqueue_active(sk_sleep(sk)))
1314 wake_up_interruptible(sk_sleep(sk));
1315 }
1316 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001317 break;
1318 case SS_LISTENING:
1319 case SS_UNCONNECTED:
1320 /* Accept only SYN message */
1321 if (!msg_connected(msg) && !(msg_errcode(msg)))
1322 retval = TIPC_OK;
1323 break;
1324 case SS_DISCONNECTING:
1325 break;
1326 default:
1327 pr_err("Unknown socket state %u\n", sock->state);
1328 }
1329 return retval;
1330}
1331
1332/**
Ying Xueaba79f32013-01-20 23:30:09 +01001333 * rcvbuf_limit - get proper overload limit of socket receive queue
1334 * @sk: socket
1335 * @buf: message
1336 *
1337 * For all connection oriented messages, irrespective of importance,
1338 * the default overload value (i.e. 67MB) is set as limit.
1339 *
1340 * For all connectionless messages, by default new queue limits are
1341 * as belows:
1342 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001343 * TIPC_LOW_IMPORTANCE (4 MB)
1344 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1345 * TIPC_HIGH_IMPORTANCE (16 MB)
1346 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001347 *
1348 * Returns overload limit according to corresponding message importance
1349 */
1350static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1351{
1352 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001353
1354 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001355 return sysctl_tipc_rmem[2];
1356
1357 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1358 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001359}
1360
1361/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001362 * filter_rcv - validate incoming message
1363 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001365 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001366 * Enqueues message on receive queue if acceptable; optionally handles
1367 * disconnect indication for a connected socket.
1368 *
1369 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001370 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1372 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001373static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001375 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001377 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001378 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001381 if (msg_type(msg) > TIPC_DIRECT_MSG)
1382 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001383
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001385 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 } else {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001388 res = filter_connect(tipc_sk_port(sk), &buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001389 if (res != TIPC_OK || buf == NULL)
1390 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391 }
1392
1393 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001394 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1395 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396
Ying Xueaba79f32013-01-20 23:30:09 +01001397 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001398 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001399 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001400 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001401
Ying Xuef288bef2012-08-21 11:16:57 +08001402 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 return TIPC_OK;
1404}
1405
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001406/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001407 * backlog_rcv - handle incoming message from backlog queue
1408 * @sk: socket
1409 * @buf: message
1410 *
1411 * Caller must hold socket lock, but not port lock.
1412 *
1413 * Returns 0
1414 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001415static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1416{
1417 u32 res;
1418
1419 res = filter_rcv(sk, buf);
1420 if (res)
1421 tipc_reject_msg(buf, res);
1422 return 0;
1423}
1424
1425/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001426 * tipc_sk_rcv - handle incoming message
1427 * @sk: socket receiving message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001428 * @buf: message
1429 *
1430 * Called with port lock already taken.
1431 *
1432 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1433 */
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001434u32 tipc_sk_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001435{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001436 u32 res;
1437
1438 /*
1439 * Process message if socket is unlocked; otherwise add to backlog queue
1440 *
1441 * This code is based on sk_receive_skb(), but must be distinct from it
1442 * since a TIPC-specific filter/reject mechanism is utilized
1443 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001444 bh_lock_sock(sk);
1445 if (!sock_owned_by_user(sk)) {
1446 res = filter_rcv(sk, buf);
1447 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001448 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001449 res = TIPC_ERR_OVERLOAD;
1450 else
1451 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001452 }
1453 bh_unlock_sock(sk);
1454
1455 return res;
1456}
1457
Ying Xue78eb3a52014-01-17 09:50:03 +08001458static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1459{
1460 struct sock *sk = sock->sk;
1461 DEFINE_WAIT(wait);
1462 int done;
1463
1464 do {
1465 int err = sock_error(sk);
1466 if (err)
1467 return err;
1468 if (!*timeo_p)
1469 return -ETIMEDOUT;
1470 if (signal_pending(current))
1471 return sock_intr_errno(*timeo_p);
1472
1473 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1474 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1475 finish_wait(sk_sleep(sk), &wait);
1476 } while (!done);
1477 return 0;
1478}
1479
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480/**
Ying Xue247f0f32014-02-18 16:06:46 +08001481 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482 * @sock: socket structure
1483 * @dest: socket address for destination port
1484 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001485 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486 *
1487 * Returns 0 on success, errno otherwise
1488 */
Ying Xue247f0f32014-02-18 16:06:46 +08001489static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1490 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001492 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001493 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1494 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001495 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1496 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001497 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498
Allan Stephens0c3141e2008-04-15 00:22:02 -07001499 lock_sock(sk);
1500
Allan Stephensb89741a2008-04-15 00:20:37 -07001501 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001502 if (sock->state == SS_READY) {
1503 res = -EOPNOTSUPP;
1504 goto exit;
1505 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506
Allan Stephensb89741a2008-04-15 00:20:37 -07001507 /*
1508 * Reject connection attempt using multicast address
1509 *
1510 * Note: send_msg() validates the rest of the address fields,
1511 * so there's no need to do it here
1512 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001513 if (dst->addrtype == TIPC_ADDR_MCAST) {
1514 res = -EINVAL;
1515 goto exit;
1516 }
1517
Ying Xue78eb3a52014-01-17 09:50:03 +08001518 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001519 switch (sock->state) {
1520 case SS_UNCONNECTED:
1521 /* Send a 'SYN-' to destination */
1522 m.msg_name = dest;
1523 m.msg_namelen = destlen;
1524
1525 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1526 * indicate send_msg() is never blocked.
1527 */
1528 if (!timeout)
1529 m.msg_flags = MSG_DONTWAIT;
1530
Ying Xue247f0f32014-02-18 16:06:46 +08001531 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001532 if ((res < 0) && (res != -EWOULDBLOCK))
1533 goto exit;
1534
1535 /* Just entered SS_CONNECTING state; the only
1536 * difference is that return value in non-blocking
1537 * case is EINPROGRESS, rather than EALREADY.
1538 */
1539 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001540 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001541 if (previous == SS_CONNECTING)
1542 res = -EALREADY;
1543 if (!timeout)
1544 goto exit;
1545 timeout = msecs_to_jiffies(timeout);
1546 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1547 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001548 break;
1549 case SS_CONNECTED:
1550 res = -EISCONN;
1551 break;
1552 default:
1553 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001554 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001555 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001556exit:
1557 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001558 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559}
1560
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001561/**
Ying Xue247f0f32014-02-18 16:06:46 +08001562 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563 * @sock: socket structure
1564 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001565 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001566 * Returns 0 on success, errno otherwise
1567 */
Ying Xue247f0f32014-02-18 16:06:46 +08001568static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001569{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001570 struct sock *sk = sock->sk;
1571 int res;
1572
1573 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574
Ying Xue245f3d32011-07-06 06:01:13 -04001575 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001576 res = -EINVAL;
1577 else {
1578 sock->state = SS_LISTENING;
1579 res = 0;
1580 }
1581
1582 release_sock(sk);
1583 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001584}
1585
Ying Xue6398e232014-01-17 09:50:04 +08001586static int tipc_wait_for_accept(struct socket *sock, long timeo)
1587{
1588 struct sock *sk = sock->sk;
1589 DEFINE_WAIT(wait);
1590 int err;
1591
1592 /* True wake-one mechanism for incoming connections: only
1593 * one process gets woken up, not the 'whole herd'.
1594 * Since we do not 'race & poll' for established sockets
1595 * anymore, the common case will execute the loop only once.
1596 */
1597 for (;;) {
1598 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1599 TASK_INTERRUPTIBLE);
1600 if (skb_queue_empty(&sk->sk_receive_queue)) {
1601 release_sock(sk);
1602 timeo = schedule_timeout(timeo);
1603 lock_sock(sk);
1604 }
1605 err = 0;
1606 if (!skb_queue_empty(&sk->sk_receive_queue))
1607 break;
1608 err = -EINVAL;
1609 if (sock->state != SS_LISTENING)
1610 break;
1611 err = sock_intr_errno(timeo);
1612 if (signal_pending(current))
1613 break;
1614 err = -EAGAIN;
1615 if (!timeo)
1616 break;
1617 }
1618 finish_wait(sk_sleep(sk), &wait);
1619 return err;
1620}
1621
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001622/**
Ying Xue247f0f32014-02-18 16:06:46 +08001623 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624 * @sock: listening socket
1625 * @newsock: new socket that is to be connected
1626 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001627 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628 * Returns 0 on success, errno otherwise
1629 */
Ying Xue247f0f32014-02-18 16:06:46 +08001630static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001631{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001632 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001633 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001634 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001635 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001636 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001637 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001638 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001639 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001640
Allan Stephens0c3141e2008-04-15 00:22:02 -07001641 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001642
Allan Stephens0c3141e2008-04-15 00:22:02 -07001643 if (sock->state != SS_LISTENING) {
1644 res = -EINVAL;
1645 goto exit;
1646 }
Ying Xue6398e232014-01-17 09:50:04 +08001647 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1648 res = tipc_wait_for_accept(sock, timeo);
1649 if (res)
1650 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001651
1652 buf = skb_peek(&sk->sk_receive_queue);
1653
Ying Xuec5fa7b32013-06-17 10:54:39 -04001654 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001655 if (res)
1656 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001657
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001658 new_sk = new_sock->sk;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001659 new_port = tipc_sk_port(new_sk);
1660 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001661 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001662
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001663 /* we lock on new_sk; but lockdep sees the lock on sk */
1664 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001665
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001666 /*
1667 * Reject any stray messages received by new socket
1668 * before the socket lock was taken (very, very unlikely)
1669 */
1670 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001672 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001673 peer.ref = msg_origport(msg);
1674 peer.node = msg_orignode(msg);
1675 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001676 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001678 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001679 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001680 new_port->conn_type = msg_nametype(msg);
1681 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001683
1684 /*
1685 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1686 * Respond to 'SYN+' by queuing it on new socket.
1687 */
1688 if (!msg_data_sz(msg)) {
1689 struct msghdr m = {NULL,};
1690
1691 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001692 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001693 } else {
1694 __skb_dequeue(&sk->sk_receive_queue);
1695 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001696 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001697 }
1698 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001700 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 return res;
1702}
1703
1704/**
Ying Xue247f0f32014-02-18 16:06:46 +08001705 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001707 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708 *
1709 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001710 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711 * Returns 0 on success, errno otherwise
1712 */
Ying Xue247f0f32014-02-18 16:06:46 +08001713static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001714{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001715 struct sock *sk = sock->sk;
1716 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717 struct sk_buff *buf;
1718 int res;
1719
Allan Stephense247a8f2008-03-06 15:05:38 -08001720 if (how != SHUT_RDWR)
1721 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722
Allan Stephens0c3141e2008-04-15 00:22:02 -07001723 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724
1725 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001726 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001727 case SS_CONNECTED:
1728
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001730 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731 buf = __skb_dequeue(&sk->sk_receive_queue);
1732 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001733 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001734 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735 goto restart;
1736 }
Ying Xue247f0f32014-02-18 16:06:46 +08001737 tipc_port_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739 } else {
Ying Xue247f0f32014-02-18 16:06:46 +08001740 tipc_port_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001742
1743 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744
1745 /* fall through */
1746
1747 case SS_DISCONNECTING:
1748
Ying Xue75031152012-10-29 09:38:15 -04001749 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001750 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001751
1752 /* Wake up anyone sleeping in poll */
1753 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 res = 0;
1755 break;
1756
1757 default:
1758 res = -ENOTCONN;
1759 }
1760
Allan Stephens0c3141e2008-04-15 00:22:02 -07001761 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762 return res;
1763}
1764
1765/**
Ying Xue247f0f32014-02-18 16:06:46 +08001766 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767 * @sock: socket structure
1768 * @lvl: option level
1769 * @opt: option identifier
1770 * @ov: pointer to new option value
1771 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001772 *
1773 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001775 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001776 * Returns 0 on success, errno otherwise
1777 */
Ying Xue247f0f32014-02-18 16:06:46 +08001778static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1779 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001781 struct sock *sk = sock->sk;
1782 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001783 u32 value;
1784 int res;
1785
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001786 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1787 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788 if (lvl != SOL_TIPC)
1789 return -ENOPROTOOPT;
1790 if (ol < sizeof(value))
1791 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001792 res = get_user(value, (u32 __user *)ov);
1793 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794 return res;
1795
Allan Stephens0c3141e2008-04-15 00:22:02 -07001796 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001797
Per Lidenb97bf3f2006-01-02 19:04:38 +01001798 switch (opt) {
1799 case TIPC_IMPORTANCE:
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001800 tipc_port_set_importance(tport, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801 break;
1802 case TIPC_SRC_DROPPABLE:
1803 if (sock->type != SOCK_STREAM)
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001804 tipc_port_set_unreliable(tport, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001805 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806 res = -ENOPROTOOPT;
1807 break;
1808 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001809 tipc_port_set_unreturnable(tport, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001810 break;
1811 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001812 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001813 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814 break;
1815 default:
1816 res = -EINVAL;
1817 }
1818
Allan Stephens0c3141e2008-04-15 00:22:02 -07001819 release_sock(sk);
1820
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821 return res;
1822}
1823
1824/**
Ying Xue247f0f32014-02-18 16:06:46 +08001825 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 * @sock: socket structure
1827 * @lvl: option level
1828 * @opt: option identifier
1829 * @ov: receptacle for option value
1830 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001831 *
1832 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001834 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835 * Returns 0 on success, errno otherwise
1836 */
Ying Xue247f0f32014-02-18 16:06:46 +08001837static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1838 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001840 struct sock *sk = sock->sk;
1841 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001842 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001843 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001844 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001845
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001846 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1847 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001848 if (lvl != SOL_TIPC)
1849 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001850 res = get_user(len, ol);
1851 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001852 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853
Allan Stephens0c3141e2008-04-15 00:22:02 -07001854 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855
1856 switch (opt) {
1857 case TIPC_IMPORTANCE:
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001858 value = tipc_port_importance(tport);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 break;
1860 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001861 value = tipc_port_unreliable(tport);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862 break;
1863 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001864 value = tipc_port_unreturnable(tport);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865 break;
1866 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001867 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001868 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001870 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001871 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001872 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001873 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001874 value = skb_queue_len(&sk->sk_receive_queue);
1875 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876 default:
1877 res = -EINVAL;
1878 }
1879
Allan Stephens0c3141e2008-04-15 00:22:02 -07001880 release_sock(sk);
1881
Paul Gortmaker25860c32010-12-31 18:59:31 +00001882 if (res)
1883 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884
Paul Gortmaker25860c32010-12-31 18:59:31 +00001885 if (len < sizeof(value))
1886 return -EINVAL;
1887
1888 if (copy_to_user(ov, &value, sizeof(value)))
1889 return -EFAULT;
1890
1891 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892}
1893
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001894/* Protocol switches for the various types of TIPC sockets */
1895
Florian Westphalbca65ea2008-02-07 18:18:01 -08001896static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001897 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001899 .release = tipc_release,
1900 .bind = tipc_bind,
1901 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001902 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001903 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08001904 .getname = tipc_getname,
1905 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001906 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001907 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08001908 .shutdown = tipc_shutdown,
1909 .setsockopt = tipc_setsockopt,
1910 .getsockopt = tipc_getsockopt,
1911 .sendmsg = tipc_sendmsg,
1912 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001913 .mmap = sock_no_mmap,
1914 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915};
1916
Florian Westphalbca65ea2008-02-07 18:18:01 -08001917static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001918 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001920 .release = tipc_release,
1921 .bind = tipc_bind,
1922 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001923 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001924 .accept = tipc_accept,
1925 .getname = tipc_getname,
1926 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001927 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001928 .listen = tipc_listen,
1929 .shutdown = tipc_shutdown,
1930 .setsockopt = tipc_setsockopt,
1931 .getsockopt = tipc_getsockopt,
1932 .sendmsg = tipc_send_packet,
1933 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001934 .mmap = sock_no_mmap,
1935 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001936};
1937
Florian Westphalbca65ea2008-02-07 18:18:01 -08001938static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001939 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001941 .release = tipc_release,
1942 .bind = tipc_bind,
1943 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001944 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001945 .accept = tipc_accept,
1946 .getname = tipc_getname,
1947 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001948 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001949 .listen = tipc_listen,
1950 .shutdown = tipc_shutdown,
1951 .setsockopt = tipc_setsockopt,
1952 .getsockopt = tipc_getsockopt,
1953 .sendmsg = tipc_send_stream,
1954 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001955 .mmap = sock_no_mmap,
1956 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001957};
1958
Florian Westphalbca65ea2008-02-07 18:18:01 -08001959static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001960 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001961 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001962 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001963};
1964
1965static struct proto tipc_proto = {
1966 .name = "TIPC",
1967 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04001968 .obj_size = sizeof(struct tipc_sock),
1969 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970};
1971
Ying Xuec5fa7b32013-06-17 10:54:39 -04001972static struct proto tipc_proto_kern = {
1973 .name = "TIPC",
1974 .obj_size = sizeof(struct tipc_sock),
1975 .sysctl_rmem = sysctl_tipc_rmem
1976};
1977
Per Lidenb97bf3f2006-01-02 19:04:38 +01001978/**
Per Liden4323add2006-01-18 00:38:21 +01001979 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001980 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981 * Returns 0 on success, errno otherwise
1982 */
Per Liden4323add2006-01-18 00:38:21 +01001983int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001984{
1985 int res;
1986
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001987 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001988 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001989 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990 goto out;
1991 }
1992
1993 res = sock_register(&tipc_family_ops);
1994 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001995 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001996 proto_unregister(&tipc_proto);
1997 goto out;
1998 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001999 out:
2000 return res;
2001}
2002
2003/**
Per Liden4323add2006-01-18 00:38:21 +01002004 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002005 */
Per Liden4323add2006-01-18 00:38:21 +01002006void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002007{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002008 sock_unregister(tipc_family_ops.family);
2009 proto_unregister(&tipc_proto);
2010}