blob: f2be4c2e20bbb5dec4a25fe1e0d7894a1770beb3 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040042#include <linux/export.h>
Erik Hugne2cf8aa12012-06-29 00:16:37 -040043
Per Lidenb97bf3f2006-01-02 19:04:38 +010044#define SS_LISTENING -1 /* socket is listening */
45#define SS_READY -2 /* socket is connectionless */
46
Allan Stephens3654ea02008-04-13 21:35:11 -070047#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloydadebc02014-08-22 18:09:11 -040048#define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
Jon Paul Maloyac0074e2014-06-25 20:41:41 -050049#define TIPC_FWD_MSG 1
Per Lidenb97bf3f2006-01-02 19:04:38 +010050
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040051static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040052static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080053static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080054static int tipc_release(struct socket *sock);
55static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -040056static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Jon Paul Maloy57289012014-08-22 18:09:09 -040057static void tipc_sk_timeout(unsigned long ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010058
Florian Westphalbca65ea2008-02-07 18:18:01 -080059static const struct proto_ops packet_ops;
60static const struct proto_ops stream_ops;
61static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010062
63static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040064static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010065
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -040066DEFINE_SPINLOCK(tipc_port_list_lock);
67LIST_HEAD(tipc_socks);
68
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090069/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070070 * Revised TIPC socket locking policy:
71 *
72 * Most socket operations take the standard socket lock when they start
73 * and hold it until they finish (or until they need to sleep). Acquiring
74 * this lock grants the owner exclusive access to the fields of the socket
75 * data structures, with the exception of the backlog queue. A few socket
76 * operations can be done without taking the socket lock because they only
77 * read socket information that never changes during the life of the socket.
78 *
79 * Socket operations may acquire the lock for the associated TIPC port if they
80 * need to perform an operation on the port. If any routine needs to acquire
81 * both the socket lock and the port lock it must take the socket lock first
82 * to avoid the risk of deadlock.
83 *
84 * The dispatcher handling incoming messages cannot grab the socket lock in
85 * the standard fashion, since invoked it runs at the BH level and cannot block.
86 * Instead, it checks to see if the socket lock is currently owned by someone,
87 * and either handles the message itself or adds it to the socket's backlog
88 * queue; in the latter case the queued message is processed once the process
89 * owning the socket lock releases it.
90 *
91 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
92 * the problem of a blocked socket operation preventing any other operations
93 * from occurring. However, applications must be careful if they have
94 * multiple threads trying to send (or receive) on the same socket, as these
95 * operations might interfere with each other. For example, doing a connect
96 * and a receive at the same time might allow the receive to consume the
97 * ACK message meant for the connect. While additional work could be done
98 * to try and overcome this, it doesn't seem to be worthwhile at the present.
99 *
100 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
101 * that another operation that must be performed in a non-blocking manner is
102 * not delayed for very long because the lock has already been taken.
103 *
104 * NOTE: This code assumes that certain fields of a port/socket pair are
105 * constant over its lifetime; such fields can be examined without taking
106 * the socket lock and/or port lock, and do not need to be re-read even
107 * after resuming processing after waiting. These fields include:
108 * - socket type
109 * - pointer to socket sk structure (aka tipc_sock structure)
110 * - pointer to port structure
111 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400114#include "socket.h"
115
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700117 * advance_rx_queue - discard first buffer in socket receive queue
118 *
119 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700121static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400123 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124}
125
126/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700127 * reject_rx_queue - reject all buffers in socket receive queue
128 *
129 * Caller must hold socket lock
130 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700131static void reject_rx_queue(struct sock *sk)
132{
133 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500134 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700135
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500136 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
137 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400138 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500139 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700140}
141
142/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400143 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700144 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145 * @sock: pre-allocated socket structure
146 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800147 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900148 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700149 * This routine creates additional data structures used by the TIPC socket,
150 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 *
152 * Returns 0 on success, errno otherwise
153 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400154static int tipc_sk_create(struct net *net, struct socket *sock,
155 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700157 const struct proto_ops *ops;
158 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400160 struct tipc_sock *tsk;
161 struct tipc_port *port;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400162 struct tipc_msg *msg;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400163 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700164
165 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 if (unlikely(protocol != 0))
167 return -EPROTONOSUPPORT;
168
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 switch (sock->type) {
170 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700171 ops = &stream_ops;
172 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 break;
174 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700175 ops = &packet_ops;
176 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 break;
178 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700180 ops = &msg_ops;
181 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 break;
Allan Stephens49978652006-06-25 23:47:18 -0700183 default:
Allan Stephens49978652006-06-25 23:47:18 -0700184 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 }
186
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400188 if (!kern)
189 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
190 else
191 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
192
Allan Stephens0c3141e2008-04-15 00:22:02 -0700193 if (sk == NULL)
194 return -ENOMEM;
195
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400196 tsk = tipc_sk(sk);
197 port = &tsk->port;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400198 ref = tipc_ref_acquire(port, &port->lock);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400199 if (!ref) {
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400200 pr_warn("Socket create failed; reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 return -ENOMEM;
202 }
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400203 port->max_pkt = MAX_PKT_DEFAULT;
204 port->ref = ref;
205 INIT_LIST_HEAD(&port->publications);
206 INIT_LIST_HEAD(&port->port_list);
207
208 /* Guard against race during node address update */
209 spin_lock_bh(&tipc_port_list_lock);
210 msg = &port->phdr;
211 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
212 NAMED_H_SIZE, 0);
213 msg_set_origport(msg, ref);
214 list_add_tail(&port->port_list, &tipc_socks);
215 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216
Allan Stephens0c3141e2008-04-15 00:22:02 -0700217 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700218 sock->ops = ops;
219 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 sock_init_data(sock, sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -0400221 k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400222 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400223 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800224 sk->sk_data_ready = tipc_data_ready;
225 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400226 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500227 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400228 atomic_set(&tsk->dupl_rcvcnt, 0);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400229 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700230
Allan Stephens0c3141e2008-04-15 00:22:02 -0700231 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400232 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700233 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400234 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700235 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 return 0;
237}
238
239/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400240 * tipc_sock_create_local - create TIPC socket from inside TIPC module
241 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
242 *
243 * We cannot use sock_creat_kern here because it bumps module user count.
244 * Since socket owner and creator is the same module we must make sure
245 * that module count remains zero for module local sockets, otherwise
246 * we cannot do rmmod.
247 *
248 * Returns 0 on success, errno otherwise
249 */
250int tipc_sock_create_local(int type, struct socket **res)
251{
252 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400253
254 rc = sock_create_lite(AF_TIPC, type, 0, res);
255 if (rc < 0) {
256 pr_err("Failed to create kernel socket\n");
257 return rc;
258 }
259 tipc_sk_create(&init_net, *res, 0, 1);
260
Ying Xuec5fa7b32013-06-17 10:54:39 -0400261 return 0;
262}
263
264/**
265 * tipc_sock_release_local - release socket created by tipc_sock_create_local
266 * @sock: the socket to be released.
267 *
268 * Module reference count is not incremented when such sockets are created,
269 * so we must keep it from being decremented when they are released.
270 */
271void tipc_sock_release_local(struct socket *sock)
272{
Ying Xue247f0f32014-02-18 16:06:46 +0800273 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400274 sock->ops = NULL;
275 sock_release(sock);
276}
277
278/**
279 * tipc_sock_accept_local - accept a connection on a socket created
280 * with tipc_sock_create_local. Use this function to avoid that
281 * module reference count is inadvertently incremented.
282 *
283 * @sock: the accepting socket
284 * @newsock: reference to the new socket to be created
285 * @flags: socket flags
286 */
287
288int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400289 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400290{
291 struct sock *sk = sock->sk;
292 int ret;
293
294 ret = sock_create_lite(sk->sk_family, sk->sk_type,
295 sk->sk_protocol, newsock);
296 if (ret < 0)
297 return ret;
298
Ying Xue247f0f32014-02-18 16:06:46 +0800299 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400300 if (ret < 0) {
301 sock_release(*newsock);
302 return ret;
303 }
304 (*newsock)->ops = sock->ops;
305 return ret;
306}
307
308/**
Ying Xue247f0f32014-02-18 16:06:46 +0800309 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 * @sock: socket to destroy
311 *
312 * This routine cleans up any messages that are still queued on the socket.
313 * For DGRAM and RDM socket types, all queued messages are rejected.
314 * For SEQPACKET and STREAM socket types, the first message is rejected
315 * and any others are discarded. (If the first message on a STREAM socket
316 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900317 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 * NOTE: Rejected messages are not necessarily returned to the sender! They
319 * are returned or discarded according to the "destination droppable" setting
320 * specified for the message by the sender.
321 *
322 * Returns 0 on success, errno otherwise
323 */
Ying Xue247f0f32014-02-18 16:06:46 +0800324static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400327 struct tipc_sock *tsk;
328 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500330 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331
Allan Stephens0c3141e2008-04-15 00:22:02 -0700332 /*
333 * Exit if socket isn't fully initialized (occurs when a failed accept()
334 * releases a pre-allocated child socket that was never used)
335 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700336 if (sk == NULL)
337 return 0;
338
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400339 tsk = tipc_sk(sk);
340 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700341 lock_sock(sk);
342
343 /*
344 * Reject all unreceived messages, except on an active connection
345 * (which disconnects locally & sends a 'FIN+' to peer)
346 */
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400347 dnode = tipc_port_peernode(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700349 buf = __skb_dequeue(&sk->sk_receive_queue);
350 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 break;
Ying Xue40682432013-10-18 07:23:16 +0200352 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400353 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354 else {
355 if ((sock->state == SS_CONNECTING) ||
356 (sock->state == SS_CONNECTED)) {
357 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -0400358 port->connected = 0;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400359 tipc_node_remove_conn(dnode, port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500361 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400362 tipc_link_xmit(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700363 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 }
365
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400366 tipc_withdraw(port, 0, NULL);
367 spin_lock_bh(port->lock);
368 tipc_ref_discard(port->ref);
369 spin_unlock_bh(port->lock);
370 k_cancel_timer(&port->timer);
371 if (port->connected) {
372 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
373 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
374 tipc_port_peerport(port),
375 port->ref, TIPC_ERR_NO_PORT);
376 if (buf)
377 tipc_link_xmit(buf, dnode, port->ref);
378 tipc_node_remove_conn(dnode, port->ref);
379 }
380 spin_lock_bh(&tipc_port_list_lock);
381 list_del(&port->port_list);
382 spin_unlock_bh(&tipc_port_list_lock);
383 k_term_timer(&port->timer);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384
Allan Stephens0c3141e2008-04-15 00:22:02 -0700385 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100386 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387
Allan Stephens0c3141e2008-04-15 00:22:02 -0700388 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700389 sock->state = SS_DISCONNECTING;
390 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700392 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200394 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395}
396
397/**
Ying Xue247f0f32014-02-18 16:06:46 +0800398 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 * @sock: socket structure
400 * @uaddr: socket address describing name(s) and desired operation
401 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900402 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 * Name and name sequence binding is indicated using a positive scope value;
404 * a negative scope value unbinds the specified name. Specifying no name
405 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900406 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700408 *
409 * NOTE: This routine doesn't need to take the socket lock since it doesn't
410 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 */
Ying Xue247f0f32014-02-18 16:06:46 +0800412static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
413 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414{
Ying Xue84602762013-12-27 10:18:28 +0800415 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400417 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800418 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419
Ying Xue84602762013-12-27 10:18:28 +0800420 lock_sock(sk);
421 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400422 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800423 goto exit;
424 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900425
Ying Xue84602762013-12-27 10:18:28 +0800426 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
427 res = -EINVAL;
428 goto exit;
429 }
430 if (addr->family != AF_TIPC) {
431 res = -EAFNOSUPPORT;
432 goto exit;
433 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435 if (addr->addrtype == TIPC_ADDR_NAME)
436 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800437 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
438 res = -EAFNOSUPPORT;
439 goto exit;
440 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900441
Ying Xue13a2e892013-06-17 10:54:40 -0400442 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400443 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800444 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
445 res = -EACCES;
446 goto exit;
447 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400448
Ying Xue84602762013-12-27 10:18:28 +0800449 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400450 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
451 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800452exit:
453 release_sock(sk);
454 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455}
456
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900457/**
Ying Xue247f0f32014-02-18 16:06:46 +0800458 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 * @sock: socket structure
460 * @uaddr: area for returned socket address
461 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700462 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900463 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700465 *
Allan Stephens2da59912008-07-14 22:43:32 -0700466 * NOTE: This routine doesn't need to take the socket lock since it only
467 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000468 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 */
Ying Xue247f0f32014-02-18 16:06:46 +0800470static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
471 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400474 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000476 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700477 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700478 if ((sock->state != SS_CONNECTED) &&
479 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
480 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400481 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
482 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700483 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400484 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000485 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700486 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487
488 *uaddr_len = sizeof(*addr);
489 addr->addrtype = TIPC_ADDR_ID;
490 addr->family = AF_TIPC;
491 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 addr->addr.name.domain = 0;
493
Allan Stephens0c3141e2008-04-15 00:22:02 -0700494 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495}
496
497/**
Ying Xue247f0f32014-02-18 16:06:46 +0800498 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 * @file: file structure associated with the socket
500 * @sock: socket for which to calculate the poll bits
501 * @wait: ???
502 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700503 * Returns pollmask value
504 *
505 * COMMENTARY:
506 * It appears that the usual socket locking mechanisms are not useful here
507 * since the pollmask info is potentially out-of-date the moment this routine
508 * exits. TCP and other protocols seem to rely on higher level poll routines
509 * to handle any preventable race conditions, so TIPC will do the same ...
510 *
511 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700512 *
Allan Stephensf662c072010-08-17 11:00:06 +0000513 * socket state flags set
514 * ------------ ---------
515 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200516 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000517 *
518 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
519 * no write flags
520 *
521 * connected POLLIN/POLLRDNORM if data in rx queue
522 * POLLOUT if port is not congested
523 *
524 * disconnecting POLLIN/POLLRDNORM/POLLHUP
525 * no write flags
526 *
527 * listening POLLIN if SYN in rx queue
528 * no write flags
529 *
530 * ready POLLIN/POLLRDNORM if data in rx queue
531 * [connectionless] POLLOUT (since port cannot be congested)
532 *
533 * IMPORTANT: The fact that a read or write operation is indicated does NOT
534 * imply that the operation will succeed, merely that it should be performed
535 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 */
Ying Xue247f0f32014-02-18 16:06:46 +0800537static unsigned int tipc_poll(struct file *file, struct socket *sock,
538 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539{
Allan Stephens9b674e82008-03-26 16:48:21 -0700540 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400541 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000542 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700543
Ying Xuef288bef2012-08-21 11:16:57 +0800544 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700545
Allan Stephensf662c072010-08-17 11:00:06 +0000546 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200547 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500548 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200549 mask |= POLLOUT;
550 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000551 case SS_READY:
552 case SS_CONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500553 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000554 mask |= POLLOUT;
555 /* fall thru' */
556 case SS_CONNECTING:
557 case SS_LISTENING:
558 if (!skb_queue_empty(&sk->sk_receive_queue))
559 mask |= (POLLIN | POLLRDNORM);
560 break;
561 case SS_DISCONNECTING:
562 mask = (POLLIN | POLLRDNORM | POLLHUP);
563 break;
564 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700565
566 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567}
568
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400569/**
570 * tipc_sendmcast - send multicast message
571 * @sock: socket structure
572 * @seq: destination address
573 * @iov: message data to send
574 * @dsz: total length of message data
575 * @timeo: timeout to wait for wakeup
576 *
577 * Called from function tipc_sendmsg(), which has done all sanity checks
578 * Returns the number of bytes sent on success, or errno
579 */
580static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
581 struct iovec *iov, size_t dsz, long timeo)
582{
583 struct sock *sk = sock->sk;
584 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
585 struct sk_buff *buf;
586 uint mtu;
587 int rc;
588
589 msg_set_type(mhdr, TIPC_MCAST_MSG);
590 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
591 msg_set_destport(mhdr, 0);
592 msg_set_destnode(mhdr, 0);
593 msg_set_nametype(mhdr, seq->type);
594 msg_set_namelower(mhdr, seq->lower);
595 msg_set_nameupper(mhdr, seq->upper);
596 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
597
598new_mtu:
599 mtu = tipc_bclink_get_mtu();
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400600 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400601 if (unlikely(rc < 0))
602 return rc;
603
604 do {
605 rc = tipc_bclink_xmit(buf);
606 if (likely(rc >= 0)) {
607 rc = dsz;
608 break;
609 }
610 if (rc == -EMSGSIZE)
611 goto new_mtu;
612 if (rc != -ELINKCONG)
613 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400614 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400615 rc = tipc_wait_for_sndmsg(sock, &timeo);
616 if (rc)
617 kfree_skb_list(buf);
618 } while (!rc);
619 return rc;
620}
621
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400622/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
623 */
624void tipc_sk_mcast_rcv(struct sk_buff *buf)
625{
626 struct tipc_msg *msg = buf_msg(buf);
627 struct tipc_port_list dports = {0, NULL, };
628 struct tipc_port_list *item;
629 struct sk_buff *b;
630 uint i, last, dst = 0;
631 u32 scope = TIPC_CLUSTER_SCOPE;
632
633 if (in_own_node(msg_orignode(msg)))
634 scope = TIPC_NODE_SCOPE;
635
636 /* Create destination port list: */
637 tipc_nametbl_mc_translate(msg_nametype(msg),
638 msg_namelower(msg),
639 msg_nameupper(msg),
640 scope,
641 &dports);
642 last = dports.count;
643 if (!last) {
644 kfree_skb(buf);
645 return;
646 }
647
648 for (item = &dports; item; item = item->next) {
649 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
650 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
651 if (!b) {
652 pr_warn("Failed do clone mcast rcv buffer\n");
653 continue;
654 }
655 msg_set_destport(msg, item->ports[i]);
656 tipc_sk_rcv(b);
657 }
658 }
659 tipc_port_list_free(&dports);
660}
661
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900662/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500663 * tipc_sk_proto_rcv - receive a connection mng protocol message
664 * @tsk: receiving socket
665 * @dnode: node to send response message to, if any
666 * @buf: buffer containing protocol message
667 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
668 * (CONN_PROBE_REPLY) message should be forwarded.
669 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800670static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
671 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500672{
673 struct tipc_msg *msg = buf_msg(buf);
674 struct tipc_port *port = &tsk->port;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500675 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500676
677 /* Ignore if connection cannot be validated: */
678 if (!port->connected || !tipc_port_peer_msg(port, msg))
679 goto exit;
680
681 port->probing_state = TIPC_CONN_OK;
682
683 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500684 conn_cong = tipc_sk_conn_cong(tsk);
685 tsk->sent_unacked -= msg_msgcnt(msg);
686 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400687 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500688 } else if (msg_type(msg) == CONN_PROBE) {
689 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
690 return TIPC_OK;
691 msg_set_type(msg, CONN_PROBE_REPLY);
692 return TIPC_FWD_MSG;
693 }
694 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
695exit:
696 kfree_skb(buf);
697 return TIPC_OK;
698}
699
700/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701 * dest_name_check - verify user is permitted to send to specified port name
702 * @dest: destination address
703 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900704 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705 * Prevents restricted configuration commands from being issued by
706 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900707 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708 * Returns 0 if permission is granted, otherwise errno
709 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800710static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711{
712 struct tipc_cfg_msg_hdr hdr;
713
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500714 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
715 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900716 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
717 return 0;
718 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
719 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900720 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
721 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722
Allan Stephens3f8dd942011-01-18 13:09:29 -0500723 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
724 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900725 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700727 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100728 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900729
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 return 0;
731}
732
Ying Xue3f405042014-01-17 09:50:05 +0800733static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
734{
735 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400736 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800737 DEFINE_WAIT(wait);
738 int done;
739
740 do {
741 int err = sock_error(sk);
742 if (err)
743 return err;
744 if (sock->state == SS_DISCONNECTING)
745 return -EPIPE;
746 if (!*timeo_p)
747 return -EAGAIN;
748 if (signal_pending(current))
749 return sock_intr_errno(*timeo_p);
750
751 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500752 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800753 finish_wait(sk_sleep(sk), &wait);
754 } while (!done);
755 return 0;
756}
757
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500758/**
Ying Xue247f0f32014-02-18 16:06:46 +0800759 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700760 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 * @sock: socket structure
762 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500763 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900764 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900766 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
768 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900769 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770 * Returns the number of bytes sent on success, or errno otherwise
771 */
Ying Xue247f0f32014-02-18 16:06:46 +0800772static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500773 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500775 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700776 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400777 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400778 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500779 struct tipc_msg *mhdr = &port->phdr;
780 struct iovec *iov = m->msg_iov;
781 u32 dnode, dport;
782 struct sk_buff *buf;
783 struct tipc_name_seq *seq = &dest->addr.nameseq;
784 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800785 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500786 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787
788 if (unlikely(!dest))
789 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500790
Allan Stephens51f9cc12006-06-25 23:49:06 -0700791 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
792 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500794
795 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400796 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797
Allan Stephens0c3141e2008-04-15 00:22:02 -0700798 if (iocb)
799 lock_sock(sk);
800
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500801 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700802 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500803 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700804 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700805 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700806 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500807 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700808 goto exit;
809 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400810 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500811 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700812 goto exit;
813 }
814 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400815 tsk->port.conn_type = dest->addr.name.name.type;
816 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700817 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500819 rc = dest_name_check(dest, m);
820 if (rc)
821 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822
Ying Xue3f405042014-01-17 09:50:05 +0800823 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500824
825 if (dest->addrtype == TIPC_ADDR_MCAST) {
826 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
827 goto exit;
828 } else if (dest->addrtype == TIPC_ADDR_NAME) {
829 u32 type = dest->addr.name.name.type;
830 u32 inst = dest->addr.name.name.instance;
831 u32 domain = dest->addr.name.domain;
832
833 dnode = domain;
834 msg_set_type(mhdr, TIPC_NAMED_MSG);
835 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
836 msg_set_nametype(mhdr, type);
837 msg_set_nameinst(mhdr, inst);
838 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
839 dport = tipc_nametbl_translate(type, inst, &dnode);
840 msg_set_destnode(mhdr, dnode);
841 msg_set_destport(mhdr, dport);
842 if (unlikely(!dport && !dnode)) {
843 rc = -EHOSTUNREACH;
844 goto exit;
845 }
846 } else if (dest->addrtype == TIPC_ADDR_ID) {
847 dnode = dest->addr.id.node;
848 msg_set_type(mhdr, TIPC_DIRECT_MSG);
849 msg_set_lookup_scope(mhdr, 0);
850 msg_set_destnode(mhdr, dnode);
851 msg_set_destport(mhdr, dest->addr.id.ref);
852 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
853 }
854
855new_mtu:
856 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400857 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500858 if (rc < 0)
859 goto exit;
860
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900861 do {
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400862 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400863 rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500864 if (likely(rc >= 0)) {
865 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700866 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500867 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700868 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900869 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500870 if (rc == -EMSGSIZE)
871 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500872 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700873 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400874 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500875 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400876 if (rc)
877 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500878 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700879exit:
880 if (iocb)
881 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500882
883 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884}
885
Ying Xue391a6dd2014-01-17 09:50:06 +0800886static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
887{
888 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400889 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800890 DEFINE_WAIT(wait);
891 int done;
892
893 do {
894 int err = sock_error(sk);
895 if (err)
896 return err;
897 if (sock->state == SS_DISCONNECTING)
898 return -EPIPE;
899 else if (sock->state != SS_CONNECTED)
900 return -ENOTCONN;
901 if (!*timeo_p)
902 return -EAGAIN;
903 if (signal_pending(current))
904 return sock_intr_errno(*timeo_p);
905
906 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
907 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500908 (!tsk->link_cong &&
909 !tipc_sk_conn_cong(tsk)) ||
910 !tsk->port.connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800911 finish_wait(sk_sleep(sk), &wait);
912 } while (!done);
913 return 0;
914}
915
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900916/**
Ying Xue247f0f32014-02-18 16:06:46 +0800917 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918 * @iocb: (unused)
919 * @sock: socket structure
920 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500921 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900922 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900924 *
925 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700926 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 */
Ying Xue247f0f32014-02-18 16:06:46 +0800928static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500929 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700931 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400932 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500933 struct tipc_port *port = &tsk->port;
934 struct tipc_msg *mhdr = &port->phdr;
935 struct sk_buff *buf;
936 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
937 u32 ref = port->ref;
938 int rc = -EINVAL;
939 long timeo;
940 u32 dnode;
941 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900942
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500943 /* Handle implied connection establishment */
944 if (unlikely(dest)) {
945 rc = tipc_sendmsg(iocb, sock, m, dsz);
946 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -0500947 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500948 return rc;
949 }
950 if (dsz > (uint)INT_MAX)
951 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700952
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500953 if (iocb)
954 lock_sock(sk);
955
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900956 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500957 if (sock->state == SS_DISCONNECTING)
958 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +0800959 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500960 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800961 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900962 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500964 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
965 dnode = tipc_port_peernode(port);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500966
967next:
968 mtu = port->max_pkt;
969 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400970 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500971 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700972 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500973 do {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500974 if (likely(!tipc_sk_conn_cong(tsk))) {
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400975 rc = tipc_link_xmit(buf, dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500976 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500977 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500978 sent += send;
979 if (sent == dsz)
980 break;
981 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700982 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500983 if (rc == -EMSGSIZE) {
984 port->max_pkt = tipc_node_get_mtu(dnode, ref);
985 goto next;
986 }
987 if (rc != -ELINKCONG)
988 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400989 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500991 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400992 if (rc)
993 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500994 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700995exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500996 if (iocb)
997 release_sock(sk);
998 return sent ? sent : rc;
999}
1000
1001/**
1002 * tipc_send_packet - send a connection-oriented message
1003 * @iocb: if NULL, indicates that socket lock is already held
1004 * @sock: socket structure
1005 * @m: message to send
1006 * @dsz: length of data to be transmitted
1007 *
1008 * Used for SOCK_SEQPACKET messages.
1009 *
1010 * Returns the number of bytes sent on success, or errno otherwise
1011 */
1012static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1013 struct msghdr *m, size_t dsz)
1014{
1015 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1016 return -EMSGSIZE;
1017
1018 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019}
1020
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001021/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022 */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001023static void tipc_sk_finish_conn(struct tipc_port *port, u32 peer_port,
1024 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025{
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001026 struct tipc_msg *msg = &port->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001028 msg_set_destnode(msg, peer_node);
1029 msg_set_destport(msg, peer_port);
1030 msg_set_type(msg, TIPC_CONN_MSG);
1031 msg_set_lookup_scope(msg, 0);
1032 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001033
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001034 port->probing_interval = CONN_PROBING_INTERVAL;
1035 port->probing_state = TIPC_CONN_OK;
1036 port->connected = 1;
1037 k_start_timer(&port->timer, port->probing_interval);
1038 tipc_node_add_conn(peer_node, port->ref, peer_port);
1039 port->max_pkt = tipc_node_get_mtu(peer_node, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040}
1041
1042/**
1043 * set_orig_addr - capture sender's address for received message
1044 * @m: descriptor for message info
1045 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001046 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 * Note: Address is not captured if not requested by receiver.
1048 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001049static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001051 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001053 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 addr->family = AF_TIPC;
1055 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001056 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057 addr->addr.id.ref = msg_origport(msg);
1058 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001059 addr->addr.name.domain = 0; /* could leave uninitialized */
1060 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 m->msg_namelen = sizeof(struct sockaddr_tipc);
1062 }
1063}
1064
1065/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001066 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 * @m: descriptor for message info
1068 * @msg: received message header
1069 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001070 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001072 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 * Returns 0 if successful, otherwise errno
1074 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001075static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001076 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077{
1078 u32 anc_data[3];
1079 u32 err;
1080 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001081 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 int res;
1083
1084 if (likely(m->msg_controllen == 0))
1085 return 0;
1086
1087 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 err = msg ? msg_errcode(msg) : 0;
1089 if (unlikely(err)) {
1090 anc_data[0] = err;
1091 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001092 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1093 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001094 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001095 if (anc_data[1]) {
1096 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1097 msg_data(msg));
1098 if (res)
1099 return res;
1100 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 }
1102
1103 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1105 switch (dest_type) {
1106 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001107 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 anc_data[0] = msg_nametype(msg);
1109 anc_data[1] = msg_namelower(msg);
1110 anc_data[2] = msg_namelower(msg);
1111 break;
1112 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001113 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114 anc_data[0] = msg_nametype(msg);
1115 anc_data[1] = msg_namelower(msg);
1116 anc_data[2] = msg_nameupper(msg);
1117 break;
1118 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001119 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120 anc_data[0] = tport->conn_type;
1121 anc_data[1] = tport->conn_instance;
1122 anc_data[2] = tport->conn_instance;
1123 break;
1124 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001125 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126 }
Allan Stephens2db99832010-12-31 18:59:33 +00001127 if (has_name) {
1128 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1129 if (res)
1130 return res;
1131 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132
1133 return 0;
1134}
1135
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001136static void tipc_sk_send_ack(struct tipc_port *port, uint ack)
1137{
1138 struct sk_buff *buf = NULL;
1139 struct tipc_msg *msg;
1140 u32 peer_port = tipc_port_peerport(port);
1141 u32 dnode = tipc_port_peernode(port);
1142
1143 if (!port->connected)
1144 return;
1145 buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
1146 tipc_own_addr, peer_port, port->ref, TIPC_OK);
1147 if (!buf)
1148 return;
1149 msg = buf_msg(buf);
1150 msg_set_msgcnt(msg, ack);
1151 tipc_link_xmit(buf, dnode, msg_link_selector(msg));
1152}
1153
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001154static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001155{
1156 struct sock *sk = sock->sk;
1157 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001158 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001159 int err;
1160
1161 for (;;) {
1162 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001163 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001164 if (sock->state == SS_DISCONNECTING) {
1165 err = -ENOTCONN;
1166 break;
1167 }
1168 release_sock(sk);
1169 timeo = schedule_timeout(timeo);
1170 lock_sock(sk);
1171 }
1172 err = 0;
1173 if (!skb_queue_empty(&sk->sk_receive_queue))
1174 break;
1175 err = sock_intr_errno(timeo);
1176 if (signal_pending(current))
1177 break;
1178 err = -EAGAIN;
1179 if (!timeo)
1180 break;
1181 }
1182 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001183 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001184 return err;
1185}
1186
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001187/**
Ying Xue247f0f32014-02-18 16:06:46 +08001188 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 * @iocb: (unused)
1190 * @m: descriptor for message info
1191 * @buf_len: total size of user buffer area
1192 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001193 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1195 * If the complete message doesn't fit in user area, truncate it.
1196 *
1197 * Returns size of returned message data, errno otherwise
1198 */
Ying Xue247f0f32014-02-18 16:06:46 +08001199static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1200 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001202 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001203 struct tipc_sock *tsk = tipc_sk(sk);
1204 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 struct sk_buff *buf;
1206 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001207 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 unsigned int sz;
1209 u32 err;
1210 int res;
1211
Allan Stephens0c3141e2008-04-15 00:22:02 -07001212 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 if (unlikely(!buf_len))
1214 return -EINVAL;
1215
Allan Stephens0c3141e2008-04-15 00:22:02 -07001216 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217
Allan Stephens0c3141e2008-04-15 00:22:02 -07001218 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 res = -ENOTCONN;
1220 goto exit;
1221 }
1222
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001223 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001224restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225
Allan Stephens0c3141e2008-04-15 00:22:02 -07001226 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001227 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001228 if (res)
1229 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001230
1231 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001232 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 msg = buf_msg(buf);
1234 sz = msg_data_sz(msg);
1235 err = msg_errcode(msg);
1236
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001239 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 goto restart;
1241 }
1242
1243 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001244 set_orig_addr(m, msg);
1245
1246 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001247 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001248 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249 goto exit;
1250
1251 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 if (!err) {
1253 if (unlikely(buf_len < sz)) {
1254 sz = buf_len;
1255 m->msg_flags |= MSG_TRUNC;
1256 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001257 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1258 m->msg_iov, sz);
1259 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 res = sz;
1262 } else {
1263 if ((sock->state == SS_READY) ||
1264 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1265 res = 0;
1266 else
1267 res = -ECONNRESET;
1268 }
1269
1270 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001272 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001273 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001274 tipc_sk_send_ack(port, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001275 tsk->rcv_unacked = 0;
1276 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001277 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001278 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001280 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281 return res;
1282}
1283
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001284/**
Ying Xue247f0f32014-02-18 16:06:46 +08001285 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286 * @iocb: (unused)
1287 * @m: descriptor for message info
1288 * @buf_len: total size of user buffer area
1289 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001290 *
1291 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 * will optionally wait for more; never truncates data.
1293 *
1294 * Returns size of returned message data, errno otherwise
1295 */
Ying Xue247f0f32014-02-18 16:06:46 +08001296static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1297 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001299 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001300 struct tipc_sock *tsk = tipc_sk(sk);
1301 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 struct sk_buff *buf;
1303 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001304 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001306 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001309 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310
1311 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 if (unlikely(!buf_len))
1313 return -EINVAL;
1314
Allan Stephens0c3141e2008-04-15 00:22:02 -07001315 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001317 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318 res = -ENOTCONN;
1319 goto exit;
1320 }
1321
Florian Westphal3720d402010-08-17 11:00:04 +00001322 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001323 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001324
Allan Stephens0c3141e2008-04-15 00:22:02 -07001325restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001326 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001327 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001328 if (res)
1329 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001330
1331 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001332 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 msg = buf_msg(buf);
1334 sz = msg_data_sz(msg);
1335 err = msg_errcode(msg);
1336
1337 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001339 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 goto restart;
1341 }
1342
1343 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 if (sz_copied == 0) {
1345 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001346 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001347 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 goto exit;
1349 }
1350
1351 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001353 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354
Allan Stephens0232fd02011-02-21 09:45:40 -05001355 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 needed = (buf_len - sz_copied);
1357 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001358
1359 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1360 m->msg_iov, sz_to_copy);
1361 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001363
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 sz_copied += sz_to_copy;
1365
1366 if (sz_to_copy < sz) {
1367 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001368 TIPC_SKB_CB(buf)->handle =
1369 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 goto exit;
1371 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 } else {
1373 if (sz_copied != 0)
1374 goto exit; /* can't add error msg to valid data */
1375
1376 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1377 res = 0;
1378 else
1379 res = -ECONNRESET;
1380 }
1381
1382 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001384 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001385 tipc_sk_send_ack(port, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001386 tsk->rcv_unacked = 0;
1387 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001388 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001389 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390
1391 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001392 if ((sz_copied < buf_len) && /* didn't get all requested data */
1393 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001394 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001395 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1396 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397 goto restart;
1398
1399exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001400 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001401 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402}
1403
1404/**
Ying Xuef288bef2012-08-21 11:16:57 +08001405 * tipc_write_space - wake up thread if port congestion is released
1406 * @sk: socket
1407 */
1408static void tipc_write_space(struct sock *sk)
1409{
1410 struct socket_wq *wq;
1411
1412 rcu_read_lock();
1413 wq = rcu_dereference(sk->sk_wq);
1414 if (wq_has_sleeper(wq))
1415 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1416 POLLWRNORM | POLLWRBAND);
1417 rcu_read_unlock();
1418}
1419
1420/**
1421 * tipc_data_ready - wake up threads to indicate messages have been received
1422 * @sk: socket
1423 * @len: the length of messages
1424 */
David S. Miller676d2362014-04-11 16:15:36 -04001425static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001426{
1427 struct socket_wq *wq;
1428
1429 rcu_read_lock();
1430 wq = rcu_dereference(sk->sk_wq);
1431 if (wq_has_sleeper(wq))
1432 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1433 POLLRDNORM | POLLRDBAND);
1434 rcu_read_unlock();
1435}
1436
1437/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001438 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001439 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001440 * @msg: message
1441 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001442 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001443 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001444static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001445{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001446 struct sock *sk = &tsk->sk;
1447 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001448 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001449 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001450
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001451 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001452
1453 if (msg_mcast(msg))
1454 return retval;
1455
1456 switch ((int)sock->state) {
1457 case SS_CONNECTED:
1458 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001459 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001460 if (unlikely(msg_errcode(msg))) {
1461 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001462 port->connected = 0;
1463 /* let timer expire on it's own */
1464 tipc_node_remove_conn(tipc_port_peernode(port),
1465 port->ref);
Ying Xue7e6c1312012-11-29 18:39:14 -05001466 }
1467 retval = TIPC_OK;
1468 }
1469 break;
1470 case SS_CONNECTING:
1471 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001472
1473 if (unlikely(!msg_connected(msg)))
1474 break;
1475
Ying Xue584d24b2012-11-29 18:51:19 -05001476 if (unlikely(msg_errcode(msg))) {
1477 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001478 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001479 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001480 break;
1481 }
1482
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001483 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001484 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001485 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001486 retval = TIPC_OK;
1487 break;
1488 }
1489
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001490 tipc_sk_finish_conn(port, msg_origport(msg), msg_orignode(msg));
1491 msg_set_importance(&port->phdr, msg_importance(msg));
1492 sock->state = SS_CONNECTED;
1493
Ying Xue584d24b2012-11-29 18:51:19 -05001494 /* If an incoming message is an 'ACK-', it should be
1495 * discarded here because it doesn't contain useful
1496 * data. In addition, we should try to wake up
1497 * connect() routine if sleeping.
1498 */
1499 if (msg_data_sz(msg) == 0) {
1500 kfree_skb(*buf);
1501 *buf = NULL;
1502 if (waitqueue_active(sk_sleep(sk)))
1503 wake_up_interruptible(sk_sleep(sk));
1504 }
1505 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001506 break;
1507 case SS_LISTENING:
1508 case SS_UNCONNECTED:
1509 /* Accept only SYN message */
1510 if (!msg_connected(msg) && !(msg_errcode(msg)))
1511 retval = TIPC_OK;
1512 break;
1513 case SS_DISCONNECTING:
1514 break;
1515 default:
1516 pr_err("Unknown socket state %u\n", sock->state);
1517 }
1518 return retval;
1519}
1520
1521/**
Ying Xueaba79f32013-01-20 23:30:09 +01001522 * rcvbuf_limit - get proper overload limit of socket receive queue
1523 * @sk: socket
1524 * @buf: message
1525 *
1526 * For all connection oriented messages, irrespective of importance,
1527 * the default overload value (i.e. 67MB) is set as limit.
1528 *
1529 * For all connectionless messages, by default new queue limits are
1530 * as belows:
1531 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001532 * TIPC_LOW_IMPORTANCE (4 MB)
1533 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1534 * TIPC_HIGH_IMPORTANCE (16 MB)
1535 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001536 *
1537 * Returns overload limit according to corresponding message importance
1538 */
1539static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1540{
1541 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001542
1543 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001544 return sysctl_tipc_rmem[2];
1545
1546 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1547 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001548}
1549
1550/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001551 * filter_rcv - validate incoming message
1552 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001554 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001555 * Enqueues message on receive queue if acceptable; optionally handles
1556 * disconnect indication for a connected socket.
1557 *
1558 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001559 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001560 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001561 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001563static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001564{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001565 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001566 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001568 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001569 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001570 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001571
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001572 if (unlikely(msg_user(msg) == CONN_MANAGER))
1573 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001574
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001575 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1576 kfree_skb(buf);
1577 tsk->link_cong = 0;
1578 sk->sk_write_space(sk);
1579 return TIPC_OK;
1580 }
1581
Per Lidenb97bf3f2006-01-02 19:04:38 +01001582 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001583 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001584 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001585
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001587 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001588 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001590 rc = filter_connect(tsk, &buf);
1591 if (rc != TIPC_OK || buf == NULL)
1592 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593 }
1594
1595 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001596 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001597 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598
Ying Xueaba79f32013-01-20 23:30:09 +01001599 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001600 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001601 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001602 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001603
David S. Miller676d2362014-04-11 16:15:36 -04001604 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605 return TIPC_OK;
1606}
1607
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001608/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001609 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001610 * @sk: socket
1611 * @buf: message
1612 *
1613 * Caller must hold socket lock, but not port lock.
1614 *
1615 * Returns 0
1616 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001617static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001618{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001619 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001620 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001621 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001622 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001623
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001624 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001625
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001626 if (likely(!rc)) {
1627 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1628 atomic_add(truesize, &tsk->dupl_rcvcnt);
1629 return 0;
1630 }
1631
1632 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1633 return 0;
1634
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001635 tipc_link_xmit(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001636
Allan Stephens0c3141e2008-04-15 00:22:02 -07001637 return 0;
1638}
1639
1640/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001641 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001642 * @buf: buffer containing arriving message
1643 * Consumes buffer
1644 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001645 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001646int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001647{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001648 struct tipc_sock *tsk;
1649 struct tipc_port *port;
1650 struct sock *sk;
1651 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001652 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001653 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001654 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001655
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001656 /* Validate destination and message */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001657 port = tipc_port_lock(dport);
1658 if (unlikely(!port)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001659 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001660 goto exit;
1661 }
1662
1663 tsk = tipc_port_to_sock(port);
1664 sk = &tsk->sk;
1665
1666 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001667 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001668
Allan Stephens0c3141e2008-04-15 00:22:02 -07001669 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001670 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001671 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001672 if (sk->sk_backlog.len == 0)
1673 atomic_set(&tsk->dupl_rcvcnt, 0);
1674 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1675 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001676 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001677 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001678 bh_unlock_sock(sk);
1679 tipc_port_unlock(port);
1680
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001681 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001682 return 0;
1683exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001684 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001685 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001686
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001687 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001688 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689}
1690
Ying Xue78eb3a52014-01-17 09:50:03 +08001691static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1692{
1693 struct sock *sk = sock->sk;
1694 DEFINE_WAIT(wait);
1695 int done;
1696
1697 do {
1698 int err = sock_error(sk);
1699 if (err)
1700 return err;
1701 if (!*timeo_p)
1702 return -ETIMEDOUT;
1703 if (signal_pending(current))
1704 return sock_intr_errno(*timeo_p);
1705
1706 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1707 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1708 finish_wait(sk_sleep(sk), &wait);
1709 } while (!done);
1710 return 0;
1711}
1712
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713/**
Ying Xue247f0f32014-02-18 16:06:46 +08001714 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715 * @sock: socket structure
1716 * @dest: socket address for destination port
1717 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001718 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 *
1720 * Returns 0 on success, errno otherwise
1721 */
Ying Xue247f0f32014-02-18 16:06:46 +08001722static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1723 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001725 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001726 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1727 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001728 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1729 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001730 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001731
Allan Stephens0c3141e2008-04-15 00:22:02 -07001732 lock_sock(sk);
1733
Allan Stephensb89741a2008-04-15 00:20:37 -07001734 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001735 if (sock->state == SS_READY) {
1736 res = -EOPNOTSUPP;
1737 goto exit;
1738 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739
Allan Stephensb89741a2008-04-15 00:20:37 -07001740 /*
1741 * Reject connection attempt using multicast address
1742 *
1743 * Note: send_msg() validates the rest of the address fields,
1744 * so there's no need to do it here
1745 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001746 if (dst->addrtype == TIPC_ADDR_MCAST) {
1747 res = -EINVAL;
1748 goto exit;
1749 }
1750
Ying Xue78eb3a52014-01-17 09:50:03 +08001751 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001752 switch (sock->state) {
1753 case SS_UNCONNECTED:
1754 /* Send a 'SYN-' to destination */
1755 m.msg_name = dest;
1756 m.msg_namelen = destlen;
1757
1758 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1759 * indicate send_msg() is never blocked.
1760 */
1761 if (!timeout)
1762 m.msg_flags = MSG_DONTWAIT;
1763
Ying Xue247f0f32014-02-18 16:06:46 +08001764 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001765 if ((res < 0) && (res != -EWOULDBLOCK))
1766 goto exit;
1767
1768 /* Just entered SS_CONNECTING state; the only
1769 * difference is that return value in non-blocking
1770 * case is EINPROGRESS, rather than EALREADY.
1771 */
1772 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001773 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001774 if (previous == SS_CONNECTING)
1775 res = -EALREADY;
1776 if (!timeout)
1777 goto exit;
1778 timeout = msecs_to_jiffies(timeout);
1779 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1780 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001781 break;
1782 case SS_CONNECTED:
1783 res = -EISCONN;
1784 break;
1785 default:
1786 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001787 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001788 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001789exit:
1790 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001791 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001792}
1793
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001794/**
Ying Xue247f0f32014-02-18 16:06:46 +08001795 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796 * @sock: socket structure
1797 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001798 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001799 * Returns 0 on success, errno otherwise
1800 */
Ying Xue247f0f32014-02-18 16:06:46 +08001801static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001802{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001803 struct sock *sk = sock->sk;
1804 int res;
1805
1806 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001807
Ying Xue245f3d32011-07-06 06:01:13 -04001808 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001809 res = -EINVAL;
1810 else {
1811 sock->state = SS_LISTENING;
1812 res = 0;
1813 }
1814
1815 release_sock(sk);
1816 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817}
1818
Ying Xue6398e232014-01-17 09:50:04 +08001819static int tipc_wait_for_accept(struct socket *sock, long timeo)
1820{
1821 struct sock *sk = sock->sk;
1822 DEFINE_WAIT(wait);
1823 int err;
1824
1825 /* True wake-one mechanism for incoming connections: only
1826 * one process gets woken up, not the 'whole herd'.
1827 * Since we do not 'race & poll' for established sockets
1828 * anymore, the common case will execute the loop only once.
1829 */
1830 for (;;) {
1831 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1832 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001833 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001834 release_sock(sk);
1835 timeo = schedule_timeout(timeo);
1836 lock_sock(sk);
1837 }
1838 err = 0;
1839 if (!skb_queue_empty(&sk->sk_receive_queue))
1840 break;
1841 err = -EINVAL;
1842 if (sock->state != SS_LISTENING)
1843 break;
1844 err = sock_intr_errno(timeo);
1845 if (signal_pending(current))
1846 break;
1847 err = -EAGAIN;
1848 if (!timeo)
1849 break;
1850 }
1851 finish_wait(sk_sleep(sk), &wait);
1852 return err;
1853}
1854
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001855/**
Ying Xue247f0f32014-02-18 16:06:46 +08001856 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001857 * @sock: listening socket
1858 * @newsock: new socket that is to be connected
1859 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001860 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001861 * Returns 0 on success, errno otherwise
1862 */
Ying Xue247f0f32014-02-18 16:06:46 +08001863static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001864{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001865 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001866 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001867 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001868 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001869 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001870 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001871
Allan Stephens0c3141e2008-04-15 00:22:02 -07001872 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001873
Allan Stephens0c3141e2008-04-15 00:22:02 -07001874 if (sock->state != SS_LISTENING) {
1875 res = -EINVAL;
1876 goto exit;
1877 }
Ying Xue6398e232014-01-17 09:50:04 +08001878 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1879 res = tipc_wait_for_accept(sock, timeo);
1880 if (res)
1881 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001882
1883 buf = skb_peek(&sk->sk_receive_queue);
1884
Ying Xuec5fa7b32013-06-17 10:54:39 -04001885 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001886 if (res)
1887 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001888
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001889 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001890 new_port = &tipc_sk(new_sk)->port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001891 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001892
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001893 /* we lock on new_sk; but lockdep sees the lock on sk */
1894 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001895
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001896 /*
1897 * Reject any stray messages received by new socket
1898 * before the socket lock was taken (very, very unlikely)
1899 */
1900 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001902 /* Connect new socket to it's peer */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001903 tipc_sk_finish_conn(new_port, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001904 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001906 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001907 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001908 new_port->conn_type = msg_nametype(msg);
1909 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001911
1912 /*
1913 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1914 * Respond to 'SYN+' by queuing it on new socket.
1915 */
1916 if (!msg_data_sz(msg)) {
1917 struct msghdr m = {NULL,};
1918
1919 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001920 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001921 } else {
1922 __skb_dequeue(&sk->sk_receive_queue);
1923 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001924 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001925 }
1926 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001928 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929 return res;
1930}
1931
1932/**
Ying Xue247f0f32014-02-18 16:06:46 +08001933 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001935 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001936 *
1937 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001938 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001939 * Returns 0 on success, errno otherwise
1940 */
Ying Xue247f0f32014-02-18 16:06:46 +08001941static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001942{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001943 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001944 struct tipc_sock *tsk = tipc_sk(sk);
1945 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946 struct sk_buff *buf;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001947 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001948 int res;
1949
Allan Stephense247a8f2008-03-06 15:05:38 -08001950 if (how != SHUT_RDWR)
1951 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001952
Allan Stephens0c3141e2008-04-15 00:22:02 -07001953 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001954
1955 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001956 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001957 case SS_CONNECTED:
1958
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001960 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001961 buf = __skb_dequeue(&sk->sk_receive_queue);
1962 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001963 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001964 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001965 goto restart;
1966 }
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001967 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
1968 tipc_link_xmit(buf, dnode, port->ref);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001969 tipc_node_remove_conn(dnode, port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001970 } else {
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001971 dnode = tipc_port_peernode(port);
1972 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
1973 TIPC_CONN_MSG, SHORT_H_SIZE,
1974 0, dnode, tipc_own_addr,
1975 tipc_port_peerport(port),
1976 port->ref, TIPC_CONN_SHUTDOWN);
1977 tipc_link_xmit(buf, dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001978 }
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001979 port->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001980 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001981 tipc_node_remove_conn(dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001982 /* fall through */
1983
1984 case SS_DISCONNECTING:
1985
Ying Xue75031152012-10-29 09:38:15 -04001986 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001987 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001988
1989 /* Wake up anyone sleeping in poll */
1990 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001991 res = 0;
1992 break;
1993
1994 default:
1995 res = -ENOTCONN;
1996 }
1997
Allan Stephens0c3141e2008-04-15 00:22:02 -07001998 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001999 return res;
2000}
2001
Jon Paul Maloy57289012014-08-22 18:09:09 -04002002static void tipc_sk_timeout(unsigned long ref)
2003{
2004 struct tipc_port *port = tipc_port_lock(ref);
2005 struct tipc_sock *tsk;
2006 struct sock *sk;
2007 struct sk_buff *buf = NULL;
2008 struct tipc_msg *msg = NULL;
2009 u32 peer_port, peer_node;
2010
2011 if (!port)
2012 return;
2013
2014 if (!port->connected) {
2015 tipc_port_unlock(port);
2016 return;
2017 }
2018 tsk = tipc_port_to_sock(port);
2019 sk = &tsk->sk;
2020 bh_lock_sock(sk);
2021 peer_port = tipc_port_peerport(port);
2022 peer_node = tipc_port_peernode(port);
2023
2024 if (port->probing_state == TIPC_CONN_PROBING) {
2025 /* Previous probe not answered -> self abort */
2026 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2027 SHORT_H_SIZE, 0, tipc_own_addr,
2028 peer_node, ref, peer_port,
2029 TIPC_ERR_NO_PORT);
2030 } else {
2031 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2032 0, peer_node, tipc_own_addr,
2033 peer_port, ref, TIPC_OK);
2034 port->probing_state = TIPC_CONN_PROBING;
2035 k_start_timer(&port->timer, port->probing_interval);
2036 }
2037 bh_unlock_sock(sk);
2038 tipc_port_unlock(port);
2039 if (!buf)
2040 return;
2041
2042 msg = buf_msg(buf);
2043 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
2044}
2045
Per Lidenb97bf3f2006-01-02 19:04:38 +01002046/**
Ying Xue247f0f32014-02-18 16:06:46 +08002047 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002048 * @sock: socket structure
2049 * @lvl: option level
2050 * @opt: option identifier
2051 * @ov: pointer to new option value
2052 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002053 *
2054 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002055 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002056 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002057 * Returns 0 on success, errno otherwise
2058 */
Ying Xue247f0f32014-02-18 16:06:46 +08002059static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2060 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002061{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002062 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002063 struct tipc_sock *tsk = tipc_sk(sk);
2064 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002065 u32 value;
2066 int res;
2067
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002068 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2069 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002070 if (lvl != SOL_TIPC)
2071 return -ENOPROTOOPT;
2072 if (ol < sizeof(value))
2073 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002074 res = get_user(value, (u32 __user *)ov);
2075 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076 return res;
2077
Allan Stephens0c3141e2008-04-15 00:22:02 -07002078 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002079
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080 switch (opt) {
2081 case TIPC_IMPORTANCE:
Erik Hugneac32c7f2014-08-15 16:44:35 +02002082 res = tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083 break;
2084 case TIPC_SRC_DROPPABLE:
2085 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002086 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002087 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002088 res = -ENOPROTOOPT;
2089 break;
2090 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002091 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092 break;
2093 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002094 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002095 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096 break;
2097 default:
2098 res = -EINVAL;
2099 }
2100
Allan Stephens0c3141e2008-04-15 00:22:02 -07002101 release_sock(sk);
2102
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103 return res;
2104}
2105
2106/**
Ying Xue247f0f32014-02-18 16:06:46 +08002107 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108 * @sock: socket structure
2109 * @lvl: option level
2110 * @opt: option identifier
2111 * @ov: receptacle for option value
2112 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002113 *
2114 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002116 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117 * Returns 0 on success, errno otherwise
2118 */
Ying Xue247f0f32014-02-18 16:06:46 +08002119static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2120 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002122 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002123 struct tipc_sock *tsk = tipc_sk(sk);
2124 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002125 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002127 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002128
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002129 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2130 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131 if (lvl != SOL_TIPC)
2132 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002133 res = get_user(len, ol);
2134 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002135 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002136
Allan Stephens0c3141e2008-04-15 00:22:02 -07002137 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002138
2139 switch (opt) {
2140 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002141 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142 break;
2143 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002144 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002145 break;
2146 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002147 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002148 break;
2149 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002150 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002151 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002152 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002153 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002154 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002155 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002156 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002157 value = skb_queue_len(&sk->sk_receive_queue);
2158 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002159 default:
2160 res = -EINVAL;
2161 }
2162
Allan Stephens0c3141e2008-04-15 00:22:02 -07002163 release_sock(sk);
2164
Paul Gortmaker25860c32010-12-31 18:59:31 +00002165 if (res)
2166 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002167
Paul Gortmaker25860c32010-12-31 18:59:31 +00002168 if (len < sizeof(value))
2169 return -EINVAL;
2170
2171 if (copy_to_user(ov, &value, sizeof(value)))
2172 return -EFAULT;
2173
2174 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002175}
2176
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002177static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002178{
2179 struct tipc_sioc_ln_req lnr;
2180 void __user *argp = (void __user *)arg;
2181
2182 switch (cmd) {
2183 case SIOCGETLINKNAME:
2184 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2185 return -EFAULT;
2186 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2187 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2188 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2189 return -EFAULT;
2190 return 0;
2191 }
2192 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002193 default:
2194 return -ENOIOCTLCMD;
2195 }
2196}
2197
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002198/* Protocol switches for the various types of TIPC sockets */
2199
Florian Westphalbca65ea2008-02-07 18:18:01 -08002200static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002201 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002202 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002203 .release = tipc_release,
2204 .bind = tipc_bind,
2205 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002206 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002207 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002208 .getname = tipc_getname,
2209 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002210 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002211 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002212 .shutdown = tipc_shutdown,
2213 .setsockopt = tipc_setsockopt,
2214 .getsockopt = tipc_getsockopt,
2215 .sendmsg = tipc_sendmsg,
2216 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002217 .mmap = sock_no_mmap,
2218 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002219};
2220
Florian Westphalbca65ea2008-02-07 18:18:01 -08002221static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002222 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002223 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002224 .release = tipc_release,
2225 .bind = tipc_bind,
2226 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002227 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002228 .accept = tipc_accept,
2229 .getname = tipc_getname,
2230 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002231 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002232 .listen = tipc_listen,
2233 .shutdown = tipc_shutdown,
2234 .setsockopt = tipc_setsockopt,
2235 .getsockopt = tipc_getsockopt,
2236 .sendmsg = tipc_send_packet,
2237 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002238 .mmap = sock_no_mmap,
2239 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002240};
2241
Florian Westphalbca65ea2008-02-07 18:18:01 -08002242static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002243 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002244 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002245 .release = tipc_release,
2246 .bind = tipc_bind,
2247 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002248 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002249 .accept = tipc_accept,
2250 .getname = tipc_getname,
2251 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002252 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002253 .listen = tipc_listen,
2254 .shutdown = tipc_shutdown,
2255 .setsockopt = tipc_setsockopt,
2256 .getsockopt = tipc_getsockopt,
2257 .sendmsg = tipc_send_stream,
2258 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002259 .mmap = sock_no_mmap,
2260 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002261};
2262
Florian Westphalbca65ea2008-02-07 18:18:01 -08002263static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002264 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002265 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002266 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002267};
2268
2269static struct proto tipc_proto = {
2270 .name = "TIPC",
2271 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002272 .obj_size = sizeof(struct tipc_sock),
2273 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002274};
2275
Ying Xuec5fa7b32013-06-17 10:54:39 -04002276static struct proto tipc_proto_kern = {
2277 .name = "TIPC",
2278 .obj_size = sizeof(struct tipc_sock),
2279 .sysctl_rmem = sysctl_tipc_rmem
2280};
2281
Per Lidenb97bf3f2006-01-02 19:04:38 +01002282/**
Per Liden4323add2006-01-18 00:38:21 +01002283 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002284 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002285 * Returns 0 on success, errno otherwise
2286 */
Per Liden4323add2006-01-18 00:38:21 +01002287int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002288{
2289 int res;
2290
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002291 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002292 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002293 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002294 goto out;
2295 }
2296
2297 res = sock_register(&tipc_family_ops);
2298 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002299 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002300 proto_unregister(&tipc_proto);
2301 goto out;
2302 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002303 out:
2304 return res;
2305}
2306
2307/**
Per Liden4323add2006-01-18 00:38:21 +01002308 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002309 */
Per Liden4323add2006-01-18 00:38:21 +01002310void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002311{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002312 sock_unregister(tipc_family_ops.family);
2313 proto_unregister(&tipc_proto);
2314}