blob: d5fa708f037d239111dbbc329b6e6f5f94ef3092 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Maloye643df12012-11-27 06:15:29 -05004 * Copyright (c) 2001-2007, 2012 Ericsson AB
Ying Xue9da3d472012-11-27 06:15:27 -05005 * Copyright (c) 2004-2008, 2010-2012, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Erik Hugne2cf8aa12012-06-29 00:16:37 -040040#include <linux/export.h>
41#include <net/sock.h>
42
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#define SS_LISTENING -1 /* socket is listening */
44#define SS_READY -2 /* socket is connectionless */
45
Allan Stephens3654ea02008-04-13 21:35:11 -070046#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010047
48struct tipc_sock {
49 struct sock sk;
50 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070051 struct tipc_portid peer_name;
Allan Stephensa0f40f02011-05-26 13:44:34 -040052 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010053};
54
Allan Stephens0c3141e2008-04-15 00:22:02 -070055#define tipc_sk(sk) ((struct tipc_sock *)(sk))
Joe Perchese3192692012-06-03 17:41:40 +000056#define tipc_sk_port(sk) (tipc_sk(sk)->p)
Per Lidenb97bf3f2006-01-02 19:04:38 +010057
Allan Stephens71092ea2011-02-23 14:52:14 -050058#define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
59 (sock->state == SS_DISCONNECTING))
60
Allan Stephens0c3141e2008-04-15 00:22:02 -070061static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010062static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
63static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080064static void tipc_data_ready(struct sock *sk, int len);
65static void tipc_write_space(struct sock *sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +010066
Florian Westphalbca65ea2008-02-07 18:18:01 -080067static const struct proto_ops packet_ops;
68static const struct proto_ops stream_ops;
69static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010070
71static struct proto tipc_proto;
72
Allan Stephense3ec9c72010-12-31 18:59:34 +000073static int sockets_enabled;
Per Lidenb97bf3f2006-01-02 19:04:38 +010074
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090075/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070076 * Revised TIPC socket locking policy:
77 *
78 * Most socket operations take the standard socket lock when they start
79 * and hold it until they finish (or until they need to sleep). Acquiring
80 * this lock grants the owner exclusive access to the fields of the socket
81 * data structures, with the exception of the backlog queue. A few socket
82 * operations can be done without taking the socket lock because they only
83 * read socket information that never changes during the life of the socket.
84 *
85 * Socket operations may acquire the lock for the associated TIPC port if they
86 * need to perform an operation on the port. If any routine needs to acquire
87 * both the socket lock and the port lock it must take the socket lock first
88 * to avoid the risk of deadlock.
89 *
90 * The dispatcher handling incoming messages cannot grab the socket lock in
91 * the standard fashion, since invoked it runs at the BH level and cannot block.
92 * Instead, it checks to see if the socket lock is currently owned by someone,
93 * and either handles the message itself or adds it to the socket's backlog
94 * queue; in the latter case the queued message is processed once the process
95 * owning the socket lock releases it.
96 *
97 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
98 * the problem of a blocked socket operation preventing any other operations
99 * from occurring. However, applications must be careful if they have
100 * multiple threads trying to send (or receive) on the same socket, as these
101 * operations might interfere with each other. For example, doing a connect
102 * and a receive at the same time might allow the receive to consume the
103 * ACK message meant for the connect. While additional work could be done
104 * to try and overcome this, it doesn't seem to be worthwhile at the present.
105 *
106 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
107 * that another operation that must be performed in a non-blocking manner is
108 * not delayed for very long because the lock has already been taken.
109 *
110 * NOTE: This code assumes that certain fields of a port/socket pair are
111 * constant over its lifetime; such fields can be examined without taking
112 * the socket lock and/or port lock, and do not need to be re-read even
113 * after resuming processing after waiting. These fields include:
114 * - socket type
115 * - pointer to socket sk structure (aka tipc_sock structure)
116 * - pointer to port structure
117 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119
120/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700121 * advance_rx_queue - discard first buffer in socket receive queue
122 *
123 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700125static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400127 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128}
129
130/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700131 * reject_rx_queue - reject all buffers in socket receive queue
132 *
133 * Caller must hold socket lock
134 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700135static void reject_rx_queue(struct sock *sk)
136{
137 struct sk_buff *buf;
138
Ying Xue9da3d472012-11-27 06:15:27 -0500139 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700140 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700141}
142
143/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 * tipc_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700145 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 * @sock: pre-allocated socket structure
147 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800148 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900149 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700150 * This routine creates additional data structures used by the TIPC socket,
151 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152 *
153 * Returns 0 on success, errno otherwise
154 */
Eric Paris3f378b62009-11-05 22:18:14 -0800155static int tipc_create(struct net *net, struct socket *sock, int protocol,
156 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700158 const struct proto_ops *ops;
159 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700161 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700162
163 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 if (unlikely(protocol != 0))
165 return -EPROTONOSUPPORT;
166
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 switch (sock->type) {
168 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700169 ops = &stream_ops;
170 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 break;
172 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700173 ops = &packet_ops;
174 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 break;
176 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700178 ops = &msg_ops;
179 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 break;
Allan Stephens49978652006-06-25 23:47:18 -0700181 default:
Allan Stephens49978652006-06-25 23:47:18 -0700182 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 }
184
Allan Stephens0c3141e2008-04-15 00:22:02 -0700185 /* Allocate socket's protocol area */
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700186 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187 if (sk == NULL)
188 return -ENOMEM;
189
190 /* Allocate TIPC port for socket to use */
Allan Stephens0ea52242008-07-14 22:42:19 -0700191 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
192 TIPC_LOW_IMPORTANCE);
193 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700194 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195 return -ENOMEM;
196 }
197
Allan Stephens0c3141e2008-04-15 00:22:02 -0700198 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199 sock->ops = ops;
200 sock->state = state;
201
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700203 sk->sk_backlog_rcv = backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400204 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800205 sk->sk_data_ready = tipc_data_ready;
206 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700207 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400208 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700210 spin_unlock_bh(tp_ptr->lock);
211
Allan Stephens0c3141e2008-04-15 00:22:02 -0700212 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700213 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700214 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700215 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700216 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218 return 0;
219}
220
221/**
222 * release - destroy a TIPC socket
223 * @sock: socket to destroy
224 *
225 * This routine cleans up any messages that are still queued on the socket.
226 * For DGRAM and RDM socket types, all queued messages are rejected.
227 * For SEQPACKET and STREAM socket types, the first message is rejected
228 * and any others are discarded. (If the first message on a STREAM socket
229 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900230 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 * NOTE: Rejected messages are not necessarily returned to the sender! They
232 * are returned or discarded according to the "destination droppable" setting
233 * specified for the message by the sender.
234 *
235 * Returns 0 on success, errno otherwise
236 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100237static int release(struct socket *sock)
238{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700240 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700242 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243
Allan Stephens0c3141e2008-04-15 00:22:02 -0700244 /*
245 * Exit if socket isn't fully initialized (occurs when a failed accept()
246 * releases a pre-allocated child socket that was never used)
247 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700248 if (sk == NULL)
249 return 0;
250
251 tport = tipc_sk_port(sk);
252 lock_sock(sk);
253
254 /*
255 * Reject all unreceived messages, except on an active connection
256 * (which disconnects locally & sends a 'FIN+' to peer)
257 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700259 buf = __skb_dequeue(&sk->sk_receive_queue);
260 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100261 break;
Allan Stephens0232fd02011-02-21 09:45:40 -0500262 if (TIPC_SKB_CB(buf)->handle != 0)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400263 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700264 else {
265 if ((sock->state == SS_CONNECTING) ||
266 (sock->state == SS_CONNECTED)) {
267 sock->state = SS_DISCONNECTING;
268 tipc_disconnect(tport->ref);
269 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700271 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272 }
273
Allan Stephens0c3141e2008-04-15 00:22:02 -0700274 /*
275 * Delete TIPC port; this ensures no more messages are queued
276 * (also disconnects an active connection & sends a 'FIN-' to peer)
277 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700278 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279
Allan Stephens0c3141e2008-04-15 00:22:02 -0700280 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100281 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282
Allan Stephens0c3141e2008-04-15 00:22:02 -0700283 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700284 sock->state = SS_DISCONNECTING;
285 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286
287 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700288 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290 return res;
291}
292
293/**
294 * bind - associate or disassocate TIPC name(s) with a socket
295 * @sock: socket structure
296 * @uaddr: socket address describing name(s) and desired operation
297 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900298 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 * Name and name sequence binding is indicated using a positive scope value;
300 * a negative scope value unbinds the specified name. Specifying no name
301 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900302 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700304 *
305 * NOTE: This routine doesn't need to take the socket lock since it doesn't
306 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
309{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700311 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312
Allan Stephens0c3141e2008-04-15 00:22:02 -0700313 if (unlikely(!uaddr_len))
314 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900315
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 if (uaddr_len < sizeof(struct sockaddr_tipc))
317 return -EINVAL;
318 if (addr->family != AF_TIPC)
319 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 if (addr->addrtype == TIPC_ADDR_NAME)
322 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700323 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
324 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900325
Allan Stephensc422f1b2011-11-02 15:49:40 -0400326 if (addr->addr.nameseq.type < TIPC_RESERVED_TYPES)
327 return -EACCES;
328
Allan Stephens0c3141e2008-04-15 00:22:02 -0700329 return (addr->scope > 0) ?
330 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
331 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332}
333
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900334/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 * get_name - get port ID of socket or peer socket
336 * @sock: socket structure
337 * @uaddr: area for returned socket address
338 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700339 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900340 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700342 *
Allan Stephens2da59912008-07-14 22:43:32 -0700343 * NOTE: This routine doesn't need to take the socket lock since it only
344 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000345 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900347static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 int *uaddr_len, int peer)
349{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700351 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000353 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700355 if ((sock->state != SS_CONNECTED) &&
356 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
357 return -ENOTCONN;
358 addr->addr.id.ref = tsock->peer_name.ref;
359 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000361 addr->addr.id.ref = tsock->p->ref;
362 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700363 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364
365 *uaddr_len = sizeof(*addr);
366 addr->addrtype = TIPC_ADDR_ID;
367 addr->family = AF_TIPC;
368 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 addr->addr.name.domain = 0;
370
Allan Stephens0c3141e2008-04-15 00:22:02 -0700371 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372}
373
374/**
375 * poll - read and possibly block on pollmask
376 * @file: file structure associated with the socket
377 * @sock: socket for which to calculate the poll bits
378 * @wait: ???
379 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700380 * Returns pollmask value
381 *
382 * COMMENTARY:
383 * It appears that the usual socket locking mechanisms are not useful here
384 * since the pollmask info is potentially out-of-date the moment this routine
385 * exits. TCP and other protocols seem to rely on higher level poll routines
386 * to handle any preventable race conditions, so TIPC will do the same ...
387 *
388 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700389 *
Allan Stephensf662c072010-08-17 11:00:06 +0000390 * socket state flags set
391 * ------------ ---------
392 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200393 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000394 *
395 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
396 * no write flags
397 *
398 * connected POLLIN/POLLRDNORM if data in rx queue
399 * POLLOUT if port is not congested
400 *
401 * disconnecting POLLIN/POLLRDNORM/POLLHUP
402 * no write flags
403 *
404 * listening POLLIN if SYN in rx queue
405 * no write flags
406 *
407 * ready POLLIN/POLLRDNORM if data in rx queue
408 * [connectionless] POLLOUT (since port cannot be congested)
409 *
410 * IMPORTANT: The fact that a read or write operation is indicated does NOT
411 * imply that the operation will succeed, merely that it should be performed
412 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900414static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 poll_table *wait)
416{
Allan Stephens9b674e82008-03-26 16:48:21 -0700417 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000418 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700419
Ying Xuef288bef2012-08-21 11:16:57 +0800420 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700421
Allan Stephensf662c072010-08-17 11:00:06 +0000422 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200423 case SS_UNCONNECTED:
424 if (!tipc_sk_port(sk)->congested)
425 mask |= POLLOUT;
426 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000427 case SS_READY:
428 case SS_CONNECTED:
429 if (!tipc_sk_port(sk)->congested)
430 mask |= POLLOUT;
431 /* fall thru' */
432 case SS_CONNECTING:
433 case SS_LISTENING:
434 if (!skb_queue_empty(&sk->sk_receive_queue))
435 mask |= (POLLIN | POLLRDNORM);
436 break;
437 case SS_DISCONNECTING:
438 mask = (POLLIN | POLLRDNORM | POLLHUP);
439 break;
440 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700441
442 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443}
444
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900445/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 * dest_name_check - verify user is permitted to send to specified port name
447 * @dest: destination address
448 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900449 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 * Prevents restricted configuration commands from being issued by
451 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900452 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453 * Returns 0 if permission is granted, otherwise errno
454 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800455static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456{
457 struct tipc_cfg_msg_hdr hdr;
458
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900459 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
460 return 0;
461 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
462 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900463 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
464 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465
Allan Stephens3f8dd942011-01-18 13:09:29 -0500466 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
467 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900468 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700470 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900472
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473 return 0;
474}
475
476/**
477 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700478 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479 * @sock: socket structure
480 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700481 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900482 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900484 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
486 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900487 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 * Returns the number of bytes sent on success, or errno otherwise
489 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490static int send_msg(struct kiocb *iocb, struct socket *sock,
491 struct msghdr *m, size_t total_len)
492{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700493 struct sock *sk = sock->sk;
494 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900495 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400497 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498 int res = -EINVAL;
499
500 if (unlikely(!dest))
501 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700502 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
503 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100505 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400506 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507
Allan Stephens0c3141e2008-04-15 00:22:02 -0700508 if (iocb)
509 lock_sock(sk);
510
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 needs_conn = (sock->state != SS_READY);
512 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700513 if (sock->state == SS_LISTENING) {
514 res = -EPIPE;
515 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700516 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700517 if (sock->state != SS_UNCONNECTED) {
518 res = -EISCONN;
519 goto exit;
520 }
Erik Hugne5d21cb72013-06-17 10:54:38 -0400521 if (tport->published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700522 res = -EOPNOTSUPP;
523 goto exit;
524 }
525 if (dest->addrtype == TIPC_ADDR_NAME) {
526 tport->conn_type = dest->addr.name.name.type;
527 tport->conn_instance = dest->addr.name.name.instance;
528 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529
530 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700531 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532 }
533
Ying Xue1d835872011-07-06 05:53:15 -0400534 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
535
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900536 do {
537 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000538 res = dest_name_check(dest, m);
539 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700540 break;
541 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900542 &dest->addr.name.name,
543 dest->addr.name.domain,
544 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500545 m->msg_iov,
546 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000547 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700548 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900549 &dest->addr.id,
550 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500551 m->msg_iov,
552 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000553 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 if (needs_conn) {
555 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700556 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 }
Allan Stephens2db99832010-12-31 18:59:33 +0000558 res = dest_name_check(dest, m);
559 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700560 break;
561 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562 &dest->addr.nameseq,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900563 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500564 m->msg_iov,
565 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900566 }
567 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000568 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700569 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700570 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900571 }
Ying Xue1d835872011-07-06 05:53:15 -0400572 if (timeout_val <= 0L) {
573 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700574 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700576 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400577 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
578 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700579 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900580 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700581
582exit:
583 if (iocb)
584 release_sock(sk);
585 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586}
587
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900588/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700590 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 * @sock: socket structure
592 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700593 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900594 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 * Returns the number of bytes sent on success, or errno otherwise
598 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599static int send_packet(struct kiocb *iocb, struct socket *sock,
600 struct msghdr *m, size_t total_len)
601{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700602 struct sock *sk = sock->sk;
603 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400605 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 int res;
607
608 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 if (unlikely(dest))
610 return send_msg(iocb, sock, m, total_len);
611
Ying Xue97f8b872013-01-31 21:51:47 +0100612 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400613 return -EMSGSIZE;
614
Allan Stephens0c3141e2008-04-15 00:22:02 -0700615 if (iocb)
616 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617
Ying Xue1d835872011-07-06 05:53:15 -0400618 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
619
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700621 if (unlikely(sock->state != SS_CONNECTED)) {
622 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900623 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700624 else
625 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700626 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700627 }
628
Allan Stephens26896902011-04-21 10:42:07 -0500629 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
630 total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000631 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 break;
Ying Xue1d835872011-07-06 05:53:15 -0400633 if (timeout_val <= 0L) {
634 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700635 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400638 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
639 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700640 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700642
643 if (iocb)
644 release_sock(sk);
645 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646}
647
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900648/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 * send_stream - send stream-oriented data
650 * @iocb: (unused)
651 * @sock: socket structure
652 * @m: data to send
653 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900654 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900656 *
657 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700658 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660static int send_stream(struct kiocb *iocb, struct socket *sock,
661 struct msghdr *m, size_t total_len)
662{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700663 struct sock *sk = sock->sk;
664 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 struct msghdr my_msg;
666 struct iovec my_iov;
667 struct iovec *curr_iov;
668 int curr_iovlen;
669 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700670 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 int curr_left;
672 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700673 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900675
Allan Stephens0c3141e2008-04-15 00:22:02 -0700676 lock_sock(sk);
677
Allan Stephens05646c92007-06-10 17:25:24 -0700678 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900679 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700680 if (sock->state == SS_UNCONNECTED) {
681 res = send_packet(NULL, sock, m, total_len);
682 goto exit;
683 } else if (sock->state == SS_DISCONNECTING) {
684 res = -EPIPE;
685 goto exit;
686 } else {
687 res = -ENOTCONN;
688 goto exit;
689 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900690 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691
Allan Stephens0c3141e2008-04-15 00:22:02 -0700692 if (unlikely(m->msg_name)) {
693 res = -EISCONN;
694 goto exit;
695 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700696
Ying Xue97f8b872013-01-31 21:51:47 +0100697 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400698 res = -EMSGSIZE;
699 goto exit;
700 }
701
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900702 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703 * Send each iovec entry using one or more messages
704 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900705 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706 * (i.e. one large iovec entry), but could be improved to pass sets
707 * of small iovec entries into send_packet().
708 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700709 curr_iov = m->msg_iov;
710 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711 my_msg.msg_iov = &my_iov;
712 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700713 my_msg.msg_flags = m->msg_flags;
714 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700715 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716
Allan Stephens05646c92007-06-10 17:25:24 -0700717 hdr_size = msg_hdr_sz(&tport->phdr);
718
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 while (curr_iovlen--) {
720 curr_start = curr_iov->iov_base;
721 curr_left = curr_iov->iov_len;
722
723 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700724 bytes_to_send = tport->max_pkt - hdr_size;
725 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
726 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
727 if (curr_left < bytes_to_send)
728 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 my_iov.iov_base = curr_start;
730 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500731 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000732 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700733 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700734 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700735 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700736 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 curr_left -= bytes_to_send;
738 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700739 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 }
741
742 curr_iov++;
743 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700744 res = bytes_sent;
745exit:
746 release_sock(sk);
747 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748}
749
750/**
751 * auto_connect - complete connection setup to a remote port
752 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900754 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 * Returns 0 on success, errno otherwise
756 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700757static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758{
Allan Stephens2da59912008-07-14 22:43:32 -0700759 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500760 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761
Allan Stephens2da59912008-07-14 22:43:32 -0700762 tsock->peer_name.ref = msg_origport(msg);
763 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500764 p_ptr = tipc_port_deref(tsock->p->ref);
765 if (!p_ptr)
766 return -EINVAL;
767
768 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
769
770 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
771 return -EINVAL;
772 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 sock->state = SS_CONNECTED;
774 return 0;
775}
776
777/**
778 * set_orig_addr - capture sender's address for received message
779 * @m: descriptor for message info
780 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900781 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 * Note: Address is not captured if not requested by receiver.
783 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800784static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900786 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900788 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 addr->family = AF_TIPC;
790 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000791 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 addr->addr.id.ref = msg_origport(msg);
793 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000794 addr->addr.name.domain = 0; /* could leave uninitialized */
795 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 m->msg_namelen = sizeof(struct sockaddr_tipc);
797 }
798}
799
800/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900801 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 * @m: descriptor for message info
803 * @msg: received message header
804 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900805 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900807 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 * Returns 0 if successful, otherwise errno
809 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800810static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100811 struct tipc_port *tport)
812{
813 u32 anc_data[3];
814 u32 err;
815 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700816 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817 int res;
818
819 if (likely(m->msg_controllen == 0))
820 return 0;
821
822 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823 err = msg ? msg_errcode(msg) : 0;
824 if (unlikely(err)) {
825 anc_data[0] = err;
826 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000827 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
828 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000830 if (anc_data[1]) {
831 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
832 msg_data(msg));
833 if (res)
834 return res;
835 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 }
837
838 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
840 switch (dest_type) {
841 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700842 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 anc_data[0] = msg_nametype(msg);
844 anc_data[1] = msg_namelower(msg);
845 anc_data[2] = msg_namelower(msg);
846 break;
847 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700848 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849 anc_data[0] = msg_nametype(msg);
850 anc_data[1] = msg_namelower(msg);
851 anc_data[2] = msg_nameupper(msg);
852 break;
853 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700854 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 anc_data[0] = tport->conn_type;
856 anc_data[1] = tport->conn_instance;
857 anc_data[2] = tport->conn_instance;
858 break;
859 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700860 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 }
Allan Stephens2db99832010-12-31 18:59:33 +0000862 if (has_name) {
863 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
864 if (res)
865 return res;
866 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867
868 return 0;
869}
870
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900871/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872 * recv_msg - receive packet-oriented message
873 * @iocb: (unused)
874 * @m: descriptor for message info
875 * @buf_len: total size of user buffer area
876 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900877 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
879 * If the complete message doesn't fit in user area, truncate it.
880 *
881 * Returns size of returned message data, errno otherwise
882 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883static int recv_msg(struct kiocb *iocb, struct socket *sock,
884 struct msghdr *m, size_t buf_len, int flags)
885{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700886 struct sock *sk = sock->sk;
887 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 struct sk_buff *buf;
889 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500890 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 unsigned int sz;
892 u32 err;
893 int res;
894
Allan Stephens0c3141e2008-04-15 00:22:02 -0700895 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 if (unlikely(!buf_len))
897 return -EINVAL;
898
Allan Stephens0c3141e2008-04-15 00:22:02 -0700899 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900
Allan Stephens0c3141e2008-04-15 00:22:02 -0700901 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 res = -ENOTCONN;
903 goto exit;
904 }
905
Mathias Krause60085c32013-04-07 01:52:00 +0000906 /* will be updated in set_orig_addr() if needed */
907 m->msg_namelen = 0;
908
Allan Stephens71092ea2011-02-23 14:52:14 -0500909 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700910restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911
Allan Stephens0c3141e2008-04-15 00:22:02 -0700912 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700913 while (skb_queue_empty(&sk->sk_receive_queue)) {
914 if (sock->state == SS_DISCONNECTING) {
915 res = -ENOTCONN;
916 goto exit;
917 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500918 if (timeout <= 0L) {
919 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700920 goto exit;
921 }
922 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500923 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
924 tipc_rx_ready(sock),
925 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700926 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700927 }
928
929 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700930 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 msg = buf_msg(buf);
932 sz = msg_data_sz(msg);
933 err = msg_errcode(msg);
934
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700937 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100938 goto restart;
939 }
940
941 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 set_orig_addr(m, msg);
943
944 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700945 res = anc_data_recv(m, msg, tport);
946 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 goto exit;
948
949 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950 if (!err) {
951 if (unlikely(buf_len < sz)) {
952 sz = buf_len;
953 m->msg_flags |= MSG_TRUNC;
954 }
Allan Stephens0232fd02011-02-21 09:45:40 -0500955 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
956 m->msg_iov, sz);
957 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 res = sz;
960 } else {
961 if ((sock->state == SS_READY) ||
962 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
963 res = 0;
964 else
965 res = -ECONNRESET;
966 }
967
968 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -0700970 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700971 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
972 tipc_acknowledge(tport->ref, tport->conn_unacked);
973 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900974 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700976 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100977 return res;
978}
979
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900980/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 * recv_stream - receive stream-oriented data
982 * @iocb: (unused)
983 * @m: descriptor for message info
984 * @buf_len: total size of user buffer area
985 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900986 *
987 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 * will optionally wait for more; never truncates data.
989 *
990 * Returns size of returned message data, errno otherwise
991 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992static int recv_stream(struct kiocb *iocb, struct socket *sock,
993 struct msghdr *m, size_t buf_len, int flags)
994{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700995 struct sock *sk = sock->sk;
996 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997 struct sk_buff *buf;
998 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500999 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001001 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001004 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005
1006 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 if (unlikely(!buf_len))
1008 return -EINVAL;
1009
Allan Stephens0c3141e2008-04-15 00:22:02 -07001010 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011
Erik Hugne5d21cb72013-06-17 10:54:38 -04001012 if (unlikely((sock->state == SS_UNCONNECTED))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 res = -ENOTCONN;
1014 goto exit;
1015 }
1016
Mathias Krause60085c32013-04-07 01:52:00 +00001017 /* will be updated in set_orig_addr() if needed */
1018 m->msg_namelen = 0;
1019
Florian Westphal3720d402010-08-17 11:00:04 +00001020 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001021 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001022
Allan Stephens0c3141e2008-04-15 00:22:02 -07001023restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001024 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001025 while (skb_queue_empty(&sk->sk_receive_queue)) {
1026 if (sock->state == SS_DISCONNECTING) {
1027 res = -ENOTCONN;
1028 goto exit;
1029 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001030 if (timeout <= 0L) {
1031 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001032 goto exit;
1033 }
1034 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001035 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1036 tipc_rx_ready(sock),
1037 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001038 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001039 }
1040
1041 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001042 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 msg = buf_msg(buf);
1044 sz = msg_data_sz(msg);
1045 err = msg_errcode(msg);
1046
1047 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001049 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 goto restart;
1051 }
1052
1053 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 if (sz_copied == 0) {
1055 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001056 res = anc_data_recv(m, msg, tport);
1057 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058 goto exit;
1059 }
1060
1061 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001063 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064
Allan Stephens0232fd02011-02-21 09:45:40 -05001065 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 needed = (buf_len - sz_copied);
1067 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001068
1069 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1070 m->msg_iov, sz_to_copy);
1071 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001073
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 sz_copied += sz_to_copy;
1075
1076 if (sz_to_copy < sz) {
1077 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001078 TIPC_SKB_CB(buf)->handle =
1079 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001080 goto exit;
1081 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 } else {
1083 if (sz_copied != 0)
1084 goto exit; /* can't add error msg to valid data */
1085
1086 if ((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 Stephens0c3141e2008-04-15 00:22:02 -07001094 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1095 tipc_acknowledge(tport->ref, tport->conn_unacked);
1096 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001097 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098
1099 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001100 if ((sz_copied < buf_len) && /* didn't get all requested data */
1101 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001102 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001103 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1104 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105 goto restart;
1106
1107exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001108 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001109 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110}
1111
1112/**
Ying Xuef288bef2012-08-21 11:16:57 +08001113 * tipc_write_space - wake up thread if port congestion is released
1114 * @sk: socket
1115 */
1116static void tipc_write_space(struct sock *sk)
1117{
1118 struct socket_wq *wq;
1119
1120 rcu_read_lock();
1121 wq = rcu_dereference(sk->sk_wq);
1122 if (wq_has_sleeper(wq))
1123 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1124 POLLWRNORM | POLLWRBAND);
1125 rcu_read_unlock();
1126}
1127
1128/**
1129 * tipc_data_ready - wake up threads to indicate messages have been received
1130 * @sk: socket
1131 * @len: the length of messages
1132 */
1133static void tipc_data_ready(struct sock *sk, int len)
1134{
1135 struct socket_wq *wq;
1136
1137 rcu_read_lock();
1138 wq = rcu_dereference(sk->sk_wq);
1139 if (wq_has_sleeper(wq))
1140 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1141 POLLRDNORM | POLLRDBAND);
1142 rcu_read_unlock();
1143}
1144
1145/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001146 * filter_connect - Handle all incoming messages for a connection-based socket
1147 * @tsock: TIPC socket
1148 * @msg: message
1149 *
1150 * Returns TIPC error status code and socket error status code
1151 * once it encounters some errors
1152 */
1153static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1154{
1155 struct socket *sock = tsock->sk.sk_socket;
1156 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001157 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001158 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001159 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001160
1161 if (msg_mcast(msg))
1162 return retval;
1163
1164 switch ((int)sock->state) {
1165 case SS_CONNECTED:
1166 /* Accept only connection-based messages sent by peer */
1167 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1168 if (unlikely(msg_errcode(msg))) {
1169 sock->state = SS_DISCONNECTING;
1170 __tipc_disconnect(tsock->p);
1171 }
1172 retval = TIPC_OK;
1173 }
1174 break;
1175 case SS_CONNECTING:
1176 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001177 if (unlikely(msg_errcode(msg))) {
1178 sock->state = SS_DISCONNECTING;
1179 sk->sk_err = -ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001180 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001181 break;
1182 }
1183
1184 if (unlikely(!msg_connected(msg)))
1185 break;
1186
1187 res = auto_connect(sock, msg);
1188 if (res) {
1189 sock->state = SS_DISCONNECTING;
1190 sk->sk_err = res;
1191 retval = TIPC_OK;
1192 break;
1193 }
1194
1195 /* If an incoming message is an 'ACK-', it should be
1196 * discarded here because it doesn't contain useful
1197 * data. In addition, we should try to wake up
1198 * connect() routine if sleeping.
1199 */
1200 if (msg_data_sz(msg) == 0) {
1201 kfree_skb(*buf);
1202 *buf = NULL;
1203 if (waitqueue_active(sk_sleep(sk)))
1204 wake_up_interruptible(sk_sleep(sk));
1205 }
1206 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001207 break;
1208 case SS_LISTENING:
1209 case SS_UNCONNECTED:
1210 /* Accept only SYN message */
1211 if (!msg_connected(msg) && !(msg_errcode(msg)))
1212 retval = TIPC_OK;
1213 break;
1214 case SS_DISCONNECTING:
1215 break;
1216 default:
1217 pr_err("Unknown socket state %u\n", sock->state);
1218 }
1219 return retval;
1220}
1221
1222/**
Ying Xueaba79f32013-01-20 23:30:09 +01001223 * rcvbuf_limit - get proper overload limit of socket receive queue
1224 * @sk: socket
1225 * @buf: message
1226 *
1227 * For all connection oriented messages, irrespective of importance,
1228 * the default overload value (i.e. 67MB) is set as limit.
1229 *
1230 * For all connectionless messages, by default new queue limits are
1231 * as belows:
1232 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001233 * TIPC_LOW_IMPORTANCE (4 MB)
1234 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1235 * TIPC_HIGH_IMPORTANCE (16 MB)
1236 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001237 *
1238 * Returns overload limit according to corresponding message importance
1239 */
1240static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1241{
1242 struct tipc_msg *msg = buf_msg(buf);
1243 unsigned int limit;
1244
1245 if (msg_connected(msg))
Ying Xuecc79dd12013-06-17 10:54:37 -04001246 limit = sysctl_tipc_rmem[2];
Ying Xueaba79f32013-01-20 23:30:09 +01001247 else
Ying Xuecc79dd12013-06-17 10:54:37 -04001248 limit = sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1249 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001250 return limit;
1251}
1252
1253/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001254 * filter_rcv - validate incoming message
1255 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001257 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001258 * Enqueues message on receive queue if acceptable; optionally handles
1259 * disconnect indication for a connected socket.
1260 *
1261 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001262 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001263 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1264 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001265static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001267 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001269 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001270 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001273 if (msg_type(msg) > TIPC_DIRECT_MSG)
1274 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001275
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001277 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001280 res = filter_connect(tipc_sk(sk), &buf);
1281 if (res != TIPC_OK || buf == NULL)
1282 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283 }
1284
1285 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001286 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1287 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288
Ying Xueaba79f32013-01-20 23:30:09 +01001289 /* Enqueue message */
Allan Stephens0232fd02011-02-21 09:45:40 -05001290 TIPC_SKB_CB(buf)->handle = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001291 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001292 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001293
Ying Xuef288bef2012-08-21 11:16:57 +08001294 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 return TIPC_OK;
1296}
1297
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001298/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001299 * backlog_rcv - handle incoming message from backlog queue
1300 * @sk: socket
1301 * @buf: message
1302 *
1303 * Caller must hold socket lock, but not port lock.
1304 *
1305 * Returns 0
1306 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001307static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1308{
1309 u32 res;
1310
1311 res = filter_rcv(sk, buf);
1312 if (res)
1313 tipc_reject_msg(buf, res);
1314 return 0;
1315}
1316
1317/**
1318 * dispatch - handle incoming message
1319 * @tport: TIPC port that received message
1320 * @buf: message
1321 *
1322 * Called with port lock already taken.
1323 *
1324 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1325 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001326static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1327{
1328 struct sock *sk = (struct sock *)tport->usr_handle;
1329 u32 res;
1330
1331 /*
1332 * Process message if socket is unlocked; otherwise add to backlog queue
1333 *
1334 * This code is based on sk_receive_skb(), but must be distinct from it
1335 * since a TIPC-specific filter/reject mechanism is utilized
1336 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001337 bh_lock_sock(sk);
1338 if (!sock_owned_by_user(sk)) {
1339 res = filter_rcv(sk, buf);
1340 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001341 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001342 res = TIPC_ERR_OVERLOAD;
1343 else
1344 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001345 }
1346 bh_unlock_sock(sk);
1347
1348 return res;
1349}
1350
1351/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 * wakeupdispatch - wake up port after congestion
1353 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001354 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001355 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357static void wakeupdispatch(struct tipc_port *tport)
1358{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001359 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360
Ying Xuef288bef2012-08-21 11:16:57 +08001361 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362}
1363
1364/**
1365 * connect - establish a connection to another TIPC port
1366 * @sock: socket structure
1367 * @dest: socket address for destination port
1368 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001369 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 *
1371 * Returns 0 on success, errno otherwise
1372 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001373static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 int flags)
1375{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001376 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001377 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1378 struct msghdr m = {NULL,};
Allan Stephensa0f40f02011-05-26 13:44:34 -04001379 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001380 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381
Allan Stephens0c3141e2008-04-15 00:22:02 -07001382 lock_sock(sk);
1383
Allan Stephensb89741a2008-04-15 00:20:37 -07001384 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001385 if (sock->state == SS_READY) {
1386 res = -EOPNOTSUPP;
1387 goto exit;
1388 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389
Allan Stephensb89741a2008-04-15 00:20:37 -07001390 /*
1391 * Reject connection attempt using multicast address
1392 *
1393 * Note: send_msg() validates the rest of the address fields,
1394 * so there's no need to do it here
1395 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001396 if (dst->addrtype == TIPC_ADDR_MCAST) {
1397 res = -EINVAL;
1398 goto exit;
1399 }
1400
Ying Xue584d24b2012-11-29 18:51:19 -05001401 timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402
Ying Xue584d24b2012-11-29 18:51:19 -05001403 switch (sock->state) {
1404 case SS_UNCONNECTED:
1405 /* Send a 'SYN-' to destination */
1406 m.msg_name = dest;
1407 m.msg_namelen = destlen;
1408
1409 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1410 * indicate send_msg() is never blocked.
1411 */
1412 if (!timeout)
1413 m.msg_flags = MSG_DONTWAIT;
1414
1415 res = send_msg(NULL, sock, &m, 0);
1416 if ((res < 0) && (res != -EWOULDBLOCK))
1417 goto exit;
1418
1419 /* Just entered SS_CONNECTING state; the only
1420 * difference is that return value in non-blocking
1421 * case is EINPROGRESS, rather than EALREADY.
1422 */
1423 res = -EINPROGRESS;
1424 break;
1425 case SS_CONNECTING:
1426 res = -EALREADY;
1427 break;
1428 case SS_CONNECTED:
1429 res = -EISCONN;
1430 break;
1431 default:
1432 res = -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001433 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001434 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435
Ying Xue584d24b2012-11-29 18:51:19 -05001436 if (sock->state == SS_CONNECTING) {
1437 if (!timeout)
1438 goto exit;
1439
1440 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1441 release_sock(sk);
1442 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1443 sock->state != SS_CONNECTING,
1444 timeout ? (long)msecs_to_jiffies(timeout)
1445 : MAX_SCHEDULE_TIMEOUT);
1446 lock_sock(sk);
1447 if (res <= 0) {
1448 if (res == 0)
1449 res = -ETIMEDOUT;
1450 else
1451 ; /* leave "res" unchanged */
1452 goto exit;
1453 }
1454 }
1455
1456 if (unlikely(sock->state == SS_DISCONNECTING))
1457 res = sock_error(sk);
1458 else
1459 res = 0;
1460
Allan Stephens0c3141e2008-04-15 00:22:02 -07001461exit:
1462 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001463 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001464}
1465
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001466/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 * listen - allow socket to listen for incoming connections
1468 * @sock: socket structure
1469 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001470 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 * Returns 0 on success, errno otherwise
1472 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473static int listen(struct socket *sock, int len)
1474{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001475 struct sock *sk = sock->sk;
1476 int res;
1477
1478 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479
Ying Xue245f3d32011-07-06 06:01:13 -04001480 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001481 res = -EINVAL;
1482 else {
1483 sock->state = SS_LISTENING;
1484 res = 0;
1485 }
1486
1487 release_sock(sk);
1488 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001489}
1490
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001491/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001492 * accept - wait for connection request
1493 * @sock: listening socket
1494 * @newsock: new socket that is to be connected
1495 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001496 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497 * Returns 0 on success, errno otherwise
1498 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001499static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001501 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001503 struct tipc_sock *new_tsock;
1504 struct tipc_port *new_tport;
1505 struct tipc_msg *msg;
1506 u32 new_ref;
1507
Allan Stephens0c3141e2008-04-15 00:22:02 -07001508 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001509
Allan Stephens0c3141e2008-04-15 00:22:02 -07001510 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001511
Allan Stephens0c3141e2008-04-15 00:22:02 -07001512 if (sock->state != SS_LISTENING) {
1513 res = -EINVAL;
1514 goto exit;
1515 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516
Allan Stephens0c3141e2008-04-15 00:22:02 -07001517 while (skb_queue_empty(&sk->sk_receive_queue)) {
1518 if (flags & O_NONBLOCK) {
1519 res = -EWOULDBLOCK;
1520 goto exit;
1521 }
1522 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001523 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001524 (!skb_queue_empty(&sk->sk_receive_queue)));
1525 lock_sock(sk);
1526 if (res)
1527 goto exit;
1528 }
1529
1530 buf = skb_peek(&sk->sk_receive_queue);
1531
Eric Paris3f378b62009-11-05 22:18:14 -08001532 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001533 if (res)
1534 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001535
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001536 new_sk = new_sock->sk;
1537 new_tsock = tipc_sk(new_sk);
1538 new_tport = new_tsock->p;
1539 new_ref = new_tport->ref;
1540 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001541
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001542 /* we lock on new_sk; but lockdep sees the lock on sk */
1543 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001544
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001545 /*
1546 * Reject any stray messages received by new socket
1547 * before the socket lock was taken (very, very unlikely)
1548 */
1549 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001550
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001551 /* Connect new socket to it's peer */
1552 new_tsock->peer_name.ref = msg_origport(msg);
1553 new_tsock->peer_name.node = msg_orignode(msg);
1554 tipc_connect(new_ref, &new_tsock->peer_name);
1555 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001556
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001557 tipc_set_portimportance(new_ref, msg_importance(msg));
1558 if (msg_named(msg)) {
1559 new_tport->conn_type = msg_nametype(msg);
1560 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001562
1563 /*
1564 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1565 * Respond to 'SYN+' by queuing it on new socket.
1566 */
1567 if (!msg_data_sz(msg)) {
1568 struct msghdr m = {NULL,};
1569
1570 advance_rx_queue(sk);
1571 send_packet(NULL, new_sock, &m, 0);
1572 } else {
1573 __skb_dequeue(&sk->sk_receive_queue);
1574 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001575 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001576 }
1577 release_sock(new_sk);
1578
Per Lidenb97bf3f2006-01-02 19:04:38 +01001579exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001580 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581 return res;
1582}
1583
1584/**
1585 * shutdown - shutdown socket connection
1586 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001587 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001588 *
1589 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001590 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001591 * Returns 0 on success, errno otherwise
1592 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593static int shutdown(struct socket *sock, int how)
1594{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001595 struct sock *sk = sock->sk;
1596 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597 struct sk_buff *buf;
1598 int res;
1599
Allan Stephense247a8f2008-03-06 15:05:38 -08001600 if (how != SHUT_RDWR)
1601 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602
Allan Stephens0c3141e2008-04-15 00:22:02 -07001603 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001604
1605 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001606 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607 case SS_CONNECTED:
1608
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001610 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001611 buf = __skb_dequeue(&sk->sk_receive_queue);
1612 if (buf) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001613 if (TIPC_SKB_CB(buf)->handle != 0) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001614 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615 goto restart;
1616 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001617 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001618 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001619 } else {
1620 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001621 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001622
1623 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624
1625 /* fall through */
1626
1627 case SS_DISCONNECTING:
1628
Ying Xue75031152012-10-29 09:38:15 -04001629 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001630 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001631
1632 /* Wake up anyone sleeping in poll */
1633 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634 res = 0;
1635 break;
1636
1637 default:
1638 res = -ENOTCONN;
1639 }
1640
Allan Stephens0c3141e2008-04-15 00:22:02 -07001641 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 return res;
1643}
1644
1645/**
1646 * setsockopt - set socket option
1647 * @sock: socket structure
1648 * @lvl: option level
1649 * @opt: option identifier
1650 * @ov: pointer to new option value
1651 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001652 *
1653 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001655 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656 * Returns 0 on success, errno otherwise
1657 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001658static int setsockopt(struct socket *sock,
David S. Millerb7058842009-09-30 16:12:20 -07001659 int lvl, int opt, char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001661 struct sock *sk = sock->sk;
1662 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663 u32 value;
1664 int res;
1665
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001666 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1667 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668 if (lvl != SOL_TIPC)
1669 return -ENOPROTOOPT;
1670 if (ol < sizeof(value))
1671 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001672 res = get_user(value, (u32 __user *)ov);
1673 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 return res;
1675
Allan Stephens0c3141e2008-04-15 00:22:02 -07001676 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001677
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 switch (opt) {
1679 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001680 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681 break;
1682 case TIPC_SRC_DROPPABLE:
1683 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001684 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001685 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686 res = -ENOPROTOOPT;
1687 break;
1688 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 break;
1691 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001692 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001693 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694 break;
1695 default:
1696 res = -EINVAL;
1697 }
1698
Allan Stephens0c3141e2008-04-15 00:22:02 -07001699 release_sock(sk);
1700
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 return res;
1702}
1703
1704/**
1705 * getsockopt - get socket option
1706 * @sock: socket structure
1707 * @lvl: option level
1708 * @opt: option identifier
1709 * @ov: receptacle for option value
1710 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001711 *
1712 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001714 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715 * Returns 0 on success, errno otherwise
1716 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001717static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001718 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001720 struct sock *sk = sock->sk;
1721 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001722 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001724 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001725
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001726 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1727 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728 if (lvl != SOL_TIPC)
1729 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001730 res = get_user(len, ol);
1731 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001732 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733
Allan Stephens0c3141e2008-04-15 00:22:02 -07001734 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735
1736 switch (opt) {
1737 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001738 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 break;
1740 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001741 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742 break;
1743 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001744 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745 break;
1746 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001747 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001750 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001751 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001752 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001753 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001754 value = skb_queue_len(&sk->sk_receive_queue);
1755 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756 default:
1757 res = -EINVAL;
1758 }
1759
Allan Stephens0c3141e2008-04-15 00:22:02 -07001760 release_sock(sk);
1761
Paul Gortmaker25860c32010-12-31 18:59:31 +00001762 if (res)
1763 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001764
Paul Gortmaker25860c32010-12-31 18:59:31 +00001765 if (len < sizeof(value))
1766 return -EINVAL;
1767
1768 if (copy_to_user(ov, &value, sizeof(value)))
1769 return -EFAULT;
1770
1771 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772}
1773
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001774/* Protocol switches for the various types of TIPC sockets */
1775
Florian Westphalbca65ea2008-02-07 18:18:01 -08001776static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001777 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778 .family = AF_TIPC,
1779 .release = release,
1780 .bind = bind,
1781 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001782 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001783 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784 .getname = get_name,
1785 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001786 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001787 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788 .shutdown = shutdown,
1789 .setsockopt = setsockopt,
1790 .getsockopt = getsockopt,
1791 .sendmsg = send_msg,
1792 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001793 .mmap = sock_no_mmap,
1794 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001795};
1796
Florian Westphalbca65ea2008-02-07 18:18:01 -08001797static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001798 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001799 .family = AF_TIPC,
1800 .release = release,
1801 .bind = bind,
1802 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001803 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001804 .accept = accept,
1805 .getname = get_name,
1806 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001807 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808 .listen = listen,
1809 .shutdown = shutdown,
1810 .setsockopt = setsockopt,
1811 .getsockopt = getsockopt,
1812 .sendmsg = send_packet,
1813 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001814 .mmap = sock_no_mmap,
1815 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816};
1817
Florian Westphalbca65ea2008-02-07 18:18:01 -08001818static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001819 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001820 .family = AF_TIPC,
1821 .release = release,
1822 .bind = bind,
1823 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001824 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825 .accept = accept,
1826 .getname = get_name,
1827 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001828 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 .listen = listen,
1830 .shutdown = shutdown,
1831 .setsockopt = setsockopt,
1832 .getsockopt = getsockopt,
1833 .sendmsg = send_stream,
1834 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001835 .mmap = sock_no_mmap,
1836 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837};
1838
Florian Westphalbca65ea2008-02-07 18:18:01 -08001839static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001840 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841 .family = AF_TIPC,
1842 .create = tipc_create
1843};
1844
1845static struct proto tipc_proto = {
1846 .name = "TIPC",
1847 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04001848 .obj_size = sizeof(struct tipc_sock),
1849 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850};
1851
1852/**
Per Liden4323add2006-01-18 00:38:21 +01001853 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001854 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855 * Returns 0 on success, errno otherwise
1856 */
Per Liden4323add2006-01-18 00:38:21 +01001857int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001858{
1859 int res;
1860
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001861 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001863 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001864 goto out;
1865 }
1866
1867 res = sock_register(&tipc_family_ops);
1868 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001869 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870 proto_unregister(&tipc_proto);
1871 goto out;
1872 }
1873
1874 sockets_enabled = 1;
1875 out:
1876 return res;
1877}
1878
1879/**
Per Liden4323add2006-01-18 00:38:21 +01001880 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881 */
Per Liden4323add2006-01-18 00:38:21 +01001882void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883{
1884 if (!sockets_enabled)
1885 return;
1886
1887 sockets_enabled = 0;
1888 sock_unregister(tipc_family_ops.family);
1889 proto_unregister(&tipc_proto);
1890}