blob: 7e6240e41e695de4c0b9f4528b9b81a342ab567c [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"
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -040038#include "ref.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000039#include "port.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050040#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020041#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050042#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040043#include <linux/export.h>
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -040044#include "config.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040045
Per Lidenb97bf3f2006-01-02 19:04:38 +010046#define SS_LISTENING -1 /* socket is listening */
47#define SS_READY -2 /* socket is connectionless */
48
Allan Stephens3654ea02008-04-13 21:35:11 -070049#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloydadebc02014-08-22 18:09:11 -040050#define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
Jon Paul Maloyac0074e2014-06-25 20:41:41 -050051#define TIPC_FWD_MSG 1
Per Lidenb97bf3f2006-01-02 19:04:38 +010052
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040053static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040054static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080055static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080056static int tipc_release(struct socket *sock);
57static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -040058static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Jon Paul Maloy57289012014-08-22 18:09:09 -040059static void tipc_sk_timeout(unsigned long ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010060
Florian Westphalbca65ea2008-02-07 18:18:01 -080061static const struct proto_ops packet_ops;
62static const struct proto_ops stream_ops;
63static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010064
65static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040066static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010067
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090068/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070069 * Revised TIPC socket locking policy:
70 *
71 * Most socket operations take the standard socket lock when they start
72 * and hold it until they finish (or until they need to sleep). Acquiring
73 * this lock grants the owner exclusive access to the fields of the socket
74 * data structures, with the exception of the backlog queue. A few socket
75 * operations can be done without taking the socket lock because they only
76 * read socket information that never changes during the life of the socket.
77 *
78 * Socket operations may acquire the lock for the associated TIPC port if they
79 * need to perform an operation on the port. If any routine needs to acquire
80 * both the socket lock and the port lock it must take the socket lock first
81 * to avoid the risk of deadlock.
82 *
83 * The dispatcher handling incoming messages cannot grab the socket lock in
84 * the standard fashion, since invoked it runs at the BH level and cannot block.
85 * Instead, it checks to see if the socket lock is currently owned by someone,
86 * and either handles the message itself or adds it to the socket's backlog
87 * queue; in the latter case the queued message is processed once the process
88 * owning the socket lock releases it.
89 *
90 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
91 * the problem of a blocked socket operation preventing any other operations
92 * from occurring. However, applications must be careful if they have
93 * multiple threads trying to send (or receive) on the same socket, as these
94 * operations might interfere with each other. For example, doing a connect
95 * and a receive at the same time might allow the receive to consume the
96 * ACK message meant for the connect. While additional work could be done
97 * to try and overcome this, it doesn't seem to be worthwhile at the present.
98 *
99 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
100 * that another operation that must be performed in a non-blocking manner is
101 * not delayed for very long because the lock has already been taken.
102 *
103 * NOTE: This code assumes that certain fields of a port/socket pair are
104 * constant over its lifetime; such fields can be examined without taking
105 * the socket lock and/or port lock, and do not need to be re-read even
106 * after resuming processing after waiting. These fields include:
107 * - socket type
108 * - pointer to socket sk structure (aka tipc_sock structure)
109 * - pointer to port structure
110 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400113#include "socket.h"
114
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700116 * advance_rx_queue - discard first buffer in socket receive queue
117 *
118 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700120static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400122 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123}
124
125/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700126 * reject_rx_queue - reject all buffers in socket receive queue
127 *
128 * Caller must hold socket lock
129 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700130static void reject_rx_queue(struct sock *sk)
131{
132 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500133 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700134
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500135 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
136 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400137 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500138 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700139}
140
141/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400142 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700143 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 * @sock: pre-allocated socket structure
145 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800146 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900147 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700148 * This routine creates additional data structures used by the TIPC socket,
149 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 *
151 * Returns 0 on success, errno otherwise
152 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400153static int tipc_sk_create(struct net *net, struct socket *sock,
154 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700156 const struct proto_ops *ops;
157 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400159 struct tipc_sock *tsk;
160 struct tipc_port *port;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400161 struct tipc_msg *msg;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400162 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700163
164 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 if (unlikely(protocol != 0))
166 return -EPROTONOSUPPORT;
167
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168 switch (sock->type) {
169 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700170 ops = &stream_ops;
171 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 break;
173 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700174 ops = &packet_ops;
175 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 break;
177 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700179 ops = &msg_ops;
180 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 break;
Allan Stephens49978652006-06-25 23:47:18 -0700182 default:
Allan Stephens49978652006-06-25 23:47:18 -0700183 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 }
185
Allan Stephens0c3141e2008-04-15 00:22:02 -0700186 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400187 if (!kern)
188 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
189 else
190 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
191
Allan Stephens0c3141e2008-04-15 00:22:02 -0700192 if (sk == NULL)
193 return -ENOMEM;
194
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400195 tsk = tipc_sk(sk);
196 port = &tsk->port;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -0400197 ref = tipc_ref_acquire(tsk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400198 if (!ref) {
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400199 pr_warn("Socket create failed; reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200 return -ENOMEM;
201 }
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400202 port->max_pkt = MAX_PKT_DEFAULT;
203 port->ref = ref;
204 INIT_LIST_HEAD(&port->publications);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400205
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400206 msg = &port->phdr;
207 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
208 NAMED_H_SIZE, 0);
209 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210
Allan Stephens0c3141e2008-04-15 00:22:02 -0700211 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700212 sock->ops = ops;
213 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 sock_init_data(sock, sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -0400215 k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400216 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400217 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800218 sk->sk_data_ready = tipc_data_ready;
219 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400220 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500221 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400222 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700223
Allan Stephens0c3141e2008-04-15 00:22:02 -0700224 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400225 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700226 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400227 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700228 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 return 0;
230}
231
232/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400233 * tipc_sock_create_local - create TIPC socket from inside TIPC module
234 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
235 *
236 * We cannot use sock_creat_kern here because it bumps module user count.
237 * Since socket owner and creator is the same module we must make sure
238 * that module count remains zero for module local sockets, otherwise
239 * we cannot do rmmod.
240 *
241 * Returns 0 on success, errno otherwise
242 */
243int tipc_sock_create_local(int type, struct socket **res)
244{
245 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400246
247 rc = sock_create_lite(AF_TIPC, type, 0, res);
248 if (rc < 0) {
249 pr_err("Failed to create kernel socket\n");
250 return rc;
251 }
252 tipc_sk_create(&init_net, *res, 0, 1);
253
Ying Xuec5fa7b32013-06-17 10:54:39 -0400254 return 0;
255}
256
257/**
258 * tipc_sock_release_local - release socket created by tipc_sock_create_local
259 * @sock: the socket to be released.
260 *
261 * Module reference count is not incremented when such sockets are created,
262 * so we must keep it from being decremented when they are released.
263 */
264void tipc_sock_release_local(struct socket *sock)
265{
Ying Xue247f0f32014-02-18 16:06:46 +0800266 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400267 sock->ops = NULL;
268 sock_release(sock);
269}
270
271/**
272 * tipc_sock_accept_local - accept a connection on a socket created
273 * with tipc_sock_create_local. Use this function to avoid that
274 * module reference count is inadvertently incremented.
275 *
276 * @sock: the accepting socket
277 * @newsock: reference to the new socket to be created
278 * @flags: socket flags
279 */
280
281int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400282 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400283{
284 struct sock *sk = sock->sk;
285 int ret;
286
287 ret = sock_create_lite(sk->sk_family, sk->sk_type,
288 sk->sk_protocol, newsock);
289 if (ret < 0)
290 return ret;
291
Ying Xue247f0f32014-02-18 16:06:46 +0800292 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400293 if (ret < 0) {
294 sock_release(*newsock);
295 return ret;
296 }
297 (*newsock)->ops = sock->ops;
298 return ret;
299}
300
301/**
Ying Xue247f0f32014-02-18 16:06:46 +0800302 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 * @sock: socket to destroy
304 *
305 * This routine cleans up any messages that are still queued on the socket.
306 * For DGRAM and RDM socket types, all queued messages are rejected.
307 * For SEQPACKET and STREAM socket types, the first message is rejected
308 * and any others are discarded. (If the first message on a STREAM socket
309 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900310 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 * NOTE: Rejected messages are not necessarily returned to the sender! They
312 * are returned or discarded according to the "destination droppable" setting
313 * specified for the message by the sender.
314 *
315 * Returns 0 on success, errno otherwise
316 */
Ying Xue247f0f32014-02-18 16:06:46 +0800317static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400320 struct tipc_sock *tsk;
321 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500323 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324
Allan Stephens0c3141e2008-04-15 00:22:02 -0700325 /*
326 * Exit if socket isn't fully initialized (occurs when a failed accept()
327 * releases a pre-allocated child socket that was never used)
328 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700329 if (sk == NULL)
330 return 0;
331
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400332 tsk = tipc_sk(sk);
333 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700334 lock_sock(sk);
335
336 /*
337 * Reject all unreceived messages, except on an active connection
338 * (which disconnects locally & sends a 'FIN+' to peer)
339 */
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400340 dnode = tipc_port_peernode(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700342 buf = __skb_dequeue(&sk->sk_receive_queue);
343 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344 break;
Ying Xue40682432013-10-18 07:23:16 +0200345 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400346 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700347 else {
348 if ((sock->state == SS_CONNECTING) ||
349 (sock->state == SS_CONNECTED)) {
350 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -0400351 port->connected = 0;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400352 tipc_node_remove_conn(dnode, port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500354 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400355 tipc_link_xmit(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 }
358
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400359 tipc_withdraw(port, 0, NULL);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400360 tipc_ref_discard(port->ref);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400361 k_cancel_timer(&port->timer);
362 if (port->connected) {
363 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
364 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
365 tipc_port_peerport(port),
366 port->ref, TIPC_ERR_NO_PORT);
367 if (buf)
368 tipc_link_xmit(buf, dnode, port->ref);
369 tipc_node_remove_conn(dnode, port->ref);
370 }
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400371 k_term_timer(&port->timer);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372
Allan Stephens0c3141e2008-04-15 00:22:02 -0700373 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100374 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375
Allan Stephens0c3141e2008-04-15 00:22:02 -0700376 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700377 sock->state = SS_DISCONNECTING;
378 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700380 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200382 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383}
384
385/**
Ying Xue247f0f32014-02-18 16:06:46 +0800386 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 * @sock: socket structure
388 * @uaddr: socket address describing name(s) and desired operation
389 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900390 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 * Name and name sequence binding is indicated using a positive scope value;
392 * a negative scope value unbinds the specified name. Specifying no name
393 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900394 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700396 *
397 * NOTE: This routine doesn't need to take the socket lock since it doesn't
398 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 */
Ying Xue247f0f32014-02-18 16:06:46 +0800400static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
401 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402{
Ying Xue84602762013-12-27 10:18:28 +0800403 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400405 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800406 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407
Ying Xue84602762013-12-27 10:18:28 +0800408 lock_sock(sk);
409 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400410 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800411 goto exit;
412 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900413
Ying Xue84602762013-12-27 10:18:28 +0800414 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
415 res = -EINVAL;
416 goto exit;
417 }
418 if (addr->family != AF_TIPC) {
419 res = -EAFNOSUPPORT;
420 goto exit;
421 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 if (addr->addrtype == TIPC_ADDR_NAME)
424 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800425 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
426 res = -EAFNOSUPPORT;
427 goto exit;
428 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900429
Ying Xue13a2e892013-06-17 10:54:40 -0400430 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400431 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800432 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
433 res = -EACCES;
434 goto exit;
435 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400436
Ying Xue84602762013-12-27 10:18:28 +0800437 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400438 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
439 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800440exit:
441 release_sock(sk);
442 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443}
444
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900445/**
Ying Xue247f0f32014-02-18 16:06:46 +0800446 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 * @sock: socket structure
448 * @uaddr: area for returned socket address
449 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700450 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900451 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700453 *
Allan Stephens2da59912008-07-14 22:43:32 -0700454 * NOTE: This routine doesn't need to take the socket lock since it only
455 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000456 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 */
Ying Xue247f0f32014-02-18 16:06:46 +0800458static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
459 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400462 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000464 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700465 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700466 if ((sock->state != SS_CONNECTED) &&
467 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
468 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400469 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
470 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700471 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400472 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000473 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700474 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475
476 *uaddr_len = sizeof(*addr);
477 addr->addrtype = TIPC_ADDR_ID;
478 addr->family = AF_TIPC;
479 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 addr->addr.name.domain = 0;
481
Allan Stephens0c3141e2008-04-15 00:22:02 -0700482 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483}
484
485/**
Ying Xue247f0f32014-02-18 16:06:46 +0800486 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 * @file: file structure associated with the socket
488 * @sock: socket for which to calculate the poll bits
489 * @wait: ???
490 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700491 * Returns pollmask value
492 *
493 * COMMENTARY:
494 * It appears that the usual socket locking mechanisms are not useful here
495 * since the pollmask info is potentially out-of-date the moment this routine
496 * exits. TCP and other protocols seem to rely on higher level poll routines
497 * to handle any preventable race conditions, so TIPC will do the same ...
498 *
499 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700500 *
Allan Stephensf662c072010-08-17 11:00:06 +0000501 * socket state flags set
502 * ------------ ---------
503 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200504 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000505 *
506 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
507 * no write flags
508 *
509 * connected POLLIN/POLLRDNORM if data in rx queue
510 * POLLOUT if port is not congested
511 *
512 * disconnecting POLLIN/POLLRDNORM/POLLHUP
513 * no write flags
514 *
515 * listening POLLIN if SYN in rx queue
516 * no write flags
517 *
518 * ready POLLIN/POLLRDNORM if data in rx queue
519 * [connectionless] POLLOUT (since port cannot be congested)
520 *
521 * IMPORTANT: The fact that a read or write operation is indicated does NOT
522 * imply that the operation will succeed, merely that it should be performed
523 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524 */
Ying Xue247f0f32014-02-18 16:06:46 +0800525static unsigned int tipc_poll(struct file *file, struct socket *sock,
526 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527{
Allan Stephens9b674e82008-03-26 16:48:21 -0700528 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400529 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000530 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700531
Ying Xuef288bef2012-08-21 11:16:57 +0800532 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700533
Allan Stephensf662c072010-08-17 11:00:06 +0000534 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200535 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500536 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200537 mask |= POLLOUT;
538 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000539 case SS_READY:
540 case SS_CONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500541 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000542 mask |= POLLOUT;
543 /* fall thru' */
544 case SS_CONNECTING:
545 case SS_LISTENING:
546 if (!skb_queue_empty(&sk->sk_receive_queue))
547 mask |= (POLLIN | POLLRDNORM);
548 break;
549 case SS_DISCONNECTING:
550 mask = (POLLIN | POLLRDNORM | POLLHUP);
551 break;
552 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700553
554 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555}
556
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400557/**
558 * tipc_sendmcast - send multicast message
559 * @sock: socket structure
560 * @seq: destination address
561 * @iov: message data to send
562 * @dsz: total length of message data
563 * @timeo: timeout to wait for wakeup
564 *
565 * Called from function tipc_sendmsg(), which has done all sanity checks
566 * Returns the number of bytes sent on success, or errno
567 */
568static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
569 struct iovec *iov, size_t dsz, long timeo)
570{
571 struct sock *sk = sock->sk;
572 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
573 struct sk_buff *buf;
574 uint mtu;
575 int rc;
576
577 msg_set_type(mhdr, TIPC_MCAST_MSG);
578 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
579 msg_set_destport(mhdr, 0);
580 msg_set_destnode(mhdr, 0);
581 msg_set_nametype(mhdr, seq->type);
582 msg_set_namelower(mhdr, seq->lower);
583 msg_set_nameupper(mhdr, seq->upper);
584 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
585
586new_mtu:
587 mtu = tipc_bclink_get_mtu();
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400588 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400589 if (unlikely(rc < 0))
590 return rc;
591
592 do {
593 rc = tipc_bclink_xmit(buf);
594 if (likely(rc >= 0)) {
595 rc = dsz;
596 break;
597 }
598 if (rc == -EMSGSIZE)
599 goto new_mtu;
600 if (rc != -ELINKCONG)
601 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400602 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400603 rc = tipc_wait_for_sndmsg(sock, &timeo);
604 if (rc)
605 kfree_skb_list(buf);
606 } while (!rc);
607 return rc;
608}
609
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400610/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
611 */
612void tipc_sk_mcast_rcv(struct sk_buff *buf)
613{
614 struct tipc_msg *msg = buf_msg(buf);
615 struct tipc_port_list dports = {0, NULL, };
616 struct tipc_port_list *item;
617 struct sk_buff *b;
618 uint i, last, dst = 0;
619 u32 scope = TIPC_CLUSTER_SCOPE;
620
621 if (in_own_node(msg_orignode(msg)))
622 scope = TIPC_NODE_SCOPE;
623
624 /* Create destination port list: */
625 tipc_nametbl_mc_translate(msg_nametype(msg),
626 msg_namelower(msg),
627 msg_nameupper(msg),
628 scope,
629 &dports);
630 last = dports.count;
631 if (!last) {
632 kfree_skb(buf);
633 return;
634 }
635
636 for (item = &dports; item; item = item->next) {
637 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
638 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
639 if (!b) {
640 pr_warn("Failed do clone mcast rcv buffer\n");
641 continue;
642 }
643 msg_set_destport(msg, item->ports[i]);
644 tipc_sk_rcv(b);
645 }
646 }
647 tipc_port_list_free(&dports);
648}
649
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900650/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500651 * tipc_sk_proto_rcv - receive a connection mng protocol message
652 * @tsk: receiving socket
653 * @dnode: node to send response message to, if any
654 * @buf: buffer containing protocol message
655 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
656 * (CONN_PROBE_REPLY) message should be forwarded.
657 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800658static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
659 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500660{
661 struct tipc_msg *msg = buf_msg(buf);
662 struct tipc_port *port = &tsk->port;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500663 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500664
665 /* Ignore if connection cannot be validated: */
666 if (!port->connected || !tipc_port_peer_msg(port, msg))
667 goto exit;
668
669 port->probing_state = TIPC_CONN_OK;
670
671 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500672 conn_cong = tipc_sk_conn_cong(tsk);
673 tsk->sent_unacked -= msg_msgcnt(msg);
674 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400675 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500676 } else if (msg_type(msg) == CONN_PROBE) {
677 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
678 return TIPC_OK;
679 msg_set_type(msg, CONN_PROBE_REPLY);
680 return TIPC_FWD_MSG;
681 }
682 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
683exit:
684 kfree_skb(buf);
685 return TIPC_OK;
686}
687
688/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689 * dest_name_check - verify user is permitted to send to specified port name
690 * @dest: destination address
691 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900692 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693 * Prevents restricted configuration commands from being issued by
694 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900695 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 * Returns 0 if permission is granted, otherwise errno
697 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800698static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100699{
700 struct tipc_cfg_msg_hdr hdr;
701
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500702 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
703 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900704 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
705 return 0;
706 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
707 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900708 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
709 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710
Allan Stephens3f8dd942011-01-18 13:09:29 -0500711 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
712 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900713 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100714 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700715 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900717
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718 return 0;
719}
720
Ying Xue3f405042014-01-17 09:50:05 +0800721static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
722{
723 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400724 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800725 DEFINE_WAIT(wait);
726 int done;
727
728 do {
729 int err = sock_error(sk);
730 if (err)
731 return err;
732 if (sock->state == SS_DISCONNECTING)
733 return -EPIPE;
734 if (!*timeo_p)
735 return -EAGAIN;
736 if (signal_pending(current))
737 return sock_intr_errno(*timeo_p);
738
739 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500740 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800741 finish_wait(sk_sleep(sk), &wait);
742 } while (!done);
743 return 0;
744}
745
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500746/**
Ying Xue247f0f32014-02-18 16:06:46 +0800747 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700748 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 * @sock: socket structure
750 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500751 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900752 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900754 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
756 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900757 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758 * Returns the number of bytes sent on success, or errno otherwise
759 */
Ying Xue247f0f32014-02-18 16:06:46 +0800760static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500761 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500763 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700764 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400765 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400766 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500767 struct tipc_msg *mhdr = &port->phdr;
768 struct iovec *iov = m->msg_iov;
769 u32 dnode, dport;
770 struct sk_buff *buf;
771 struct tipc_name_seq *seq = &dest->addr.nameseq;
772 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800773 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500774 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775
776 if (unlikely(!dest))
777 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500778
Allan Stephens51f9cc12006-06-25 23:49:06 -0700779 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
780 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500782
783 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400784 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785
Allan Stephens0c3141e2008-04-15 00:22:02 -0700786 if (iocb)
787 lock_sock(sk);
788
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500789 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700790 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500791 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700792 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700793 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700794 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500795 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700796 goto exit;
797 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400798 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500799 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700800 goto exit;
801 }
802 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400803 tsk->port.conn_type = dest->addr.name.name.type;
804 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700805 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500807 rc = dest_name_check(dest, m);
808 if (rc)
809 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810
Ying Xue3f405042014-01-17 09:50:05 +0800811 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500812
813 if (dest->addrtype == TIPC_ADDR_MCAST) {
814 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
815 goto exit;
816 } else if (dest->addrtype == TIPC_ADDR_NAME) {
817 u32 type = dest->addr.name.name.type;
818 u32 inst = dest->addr.name.name.instance;
819 u32 domain = dest->addr.name.domain;
820
821 dnode = domain;
822 msg_set_type(mhdr, TIPC_NAMED_MSG);
823 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
824 msg_set_nametype(mhdr, type);
825 msg_set_nameinst(mhdr, inst);
826 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
827 dport = tipc_nametbl_translate(type, inst, &dnode);
828 msg_set_destnode(mhdr, dnode);
829 msg_set_destport(mhdr, dport);
830 if (unlikely(!dport && !dnode)) {
831 rc = -EHOSTUNREACH;
832 goto exit;
833 }
834 } else if (dest->addrtype == TIPC_ADDR_ID) {
835 dnode = dest->addr.id.node;
836 msg_set_type(mhdr, TIPC_DIRECT_MSG);
837 msg_set_lookup_scope(mhdr, 0);
838 msg_set_destnode(mhdr, dnode);
839 msg_set_destport(mhdr, dest->addr.id.ref);
840 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
841 }
842
843new_mtu:
844 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400845 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500846 if (rc < 0)
847 goto exit;
848
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900849 do {
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400850 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400851 rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500852 if (likely(rc >= 0)) {
853 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700854 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500855 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700856 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900857 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500858 if (rc == -EMSGSIZE)
859 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500860 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700861 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400862 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500863 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400864 if (rc)
865 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500866 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700867exit:
868 if (iocb)
869 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500870
871 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872}
873
Ying Xue391a6dd2014-01-17 09:50:06 +0800874static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
875{
876 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400877 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800878 DEFINE_WAIT(wait);
879 int done;
880
881 do {
882 int err = sock_error(sk);
883 if (err)
884 return err;
885 if (sock->state == SS_DISCONNECTING)
886 return -EPIPE;
887 else if (sock->state != SS_CONNECTED)
888 return -ENOTCONN;
889 if (!*timeo_p)
890 return -EAGAIN;
891 if (signal_pending(current))
892 return sock_intr_errno(*timeo_p);
893
894 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
895 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500896 (!tsk->link_cong &&
897 !tipc_sk_conn_cong(tsk)) ||
898 !tsk->port.connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800899 finish_wait(sk_sleep(sk), &wait);
900 } while (!done);
901 return 0;
902}
903
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900904/**
Ying Xue247f0f32014-02-18 16:06:46 +0800905 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906 * @iocb: (unused)
907 * @sock: socket structure
908 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500909 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900910 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900912 *
913 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700914 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100915 */
Ying Xue247f0f32014-02-18 16:06:46 +0800916static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500917 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700919 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400920 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500921 struct tipc_port *port = &tsk->port;
922 struct tipc_msg *mhdr = &port->phdr;
923 struct sk_buff *buf;
924 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
925 u32 ref = port->ref;
926 int rc = -EINVAL;
927 long timeo;
928 u32 dnode;
929 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900930
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500931 /* Handle implied connection establishment */
932 if (unlikely(dest)) {
933 rc = tipc_sendmsg(iocb, sock, m, dsz);
934 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -0500935 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500936 return rc;
937 }
938 if (dsz > (uint)INT_MAX)
939 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700940
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500941 if (iocb)
942 lock_sock(sk);
943
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900944 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500945 if (sock->state == SS_DISCONNECTING)
946 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +0800947 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500948 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800949 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900950 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500952 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
953 dnode = tipc_port_peernode(port);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500954
955next:
956 mtu = port->max_pkt;
957 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400958 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500959 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700960 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500961 do {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500962 if (likely(!tipc_sk_conn_cong(tsk))) {
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400963 rc = tipc_link_xmit(buf, dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500964 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500965 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500966 sent += send;
967 if (sent == dsz)
968 break;
969 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700970 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500971 if (rc == -EMSGSIZE) {
972 port->max_pkt = tipc_node_get_mtu(dnode, ref);
973 goto next;
974 }
975 if (rc != -ELINKCONG)
976 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400977 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500979 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400980 if (rc)
981 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500982 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700983exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500984 if (iocb)
985 release_sock(sk);
986 return sent ? sent : rc;
987}
988
989/**
990 * tipc_send_packet - send a connection-oriented message
991 * @iocb: if NULL, indicates that socket lock is already held
992 * @sock: socket structure
993 * @m: message to send
994 * @dsz: length of data to be transmitted
995 *
996 * Used for SOCK_SEQPACKET messages.
997 *
998 * Returns the number of bytes sent on success, or errno otherwise
999 */
1000static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1001 struct msghdr *m, size_t dsz)
1002{
1003 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1004 return -EMSGSIZE;
1005
1006 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007}
1008
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001009/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001011static void tipc_sk_finish_conn(struct tipc_port *port, u32 peer_port,
1012 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013{
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001014 struct tipc_msg *msg = &port->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001016 msg_set_destnode(msg, peer_node);
1017 msg_set_destport(msg, peer_port);
1018 msg_set_type(msg, TIPC_CONN_MSG);
1019 msg_set_lookup_scope(msg, 0);
1020 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001021
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001022 port->probing_interval = CONN_PROBING_INTERVAL;
1023 port->probing_state = TIPC_CONN_OK;
1024 port->connected = 1;
1025 k_start_timer(&port->timer, port->probing_interval);
1026 tipc_node_add_conn(peer_node, port->ref, peer_port);
1027 port->max_pkt = tipc_node_get_mtu(peer_node, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028}
1029
1030/**
1031 * set_orig_addr - capture sender's address for received message
1032 * @m: descriptor for message info
1033 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001034 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 * Note: Address is not captured if not requested by receiver.
1036 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001037static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001039 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001041 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042 addr->family = AF_TIPC;
1043 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001044 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 addr->addr.id.ref = msg_origport(msg);
1046 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001047 addr->addr.name.domain = 0; /* could leave uninitialized */
1048 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 m->msg_namelen = sizeof(struct sockaddr_tipc);
1050 }
1051}
1052
1053/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001054 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 * @m: descriptor for message info
1056 * @msg: received message header
1057 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001058 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001060 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 * Returns 0 if successful, otherwise errno
1062 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001063static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001064 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065{
1066 u32 anc_data[3];
1067 u32 err;
1068 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001069 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 int res;
1071
1072 if (likely(m->msg_controllen == 0))
1073 return 0;
1074
1075 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 err = msg ? msg_errcode(msg) : 0;
1077 if (unlikely(err)) {
1078 anc_data[0] = err;
1079 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001080 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1081 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001083 if (anc_data[1]) {
1084 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1085 msg_data(msg));
1086 if (res)
1087 return res;
1088 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 }
1090
1091 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1093 switch (dest_type) {
1094 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001095 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 anc_data[0] = msg_nametype(msg);
1097 anc_data[1] = msg_namelower(msg);
1098 anc_data[2] = msg_namelower(msg);
1099 break;
1100 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001101 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001102 anc_data[0] = msg_nametype(msg);
1103 anc_data[1] = msg_namelower(msg);
1104 anc_data[2] = msg_nameupper(msg);
1105 break;
1106 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001107 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 anc_data[0] = tport->conn_type;
1109 anc_data[1] = tport->conn_instance;
1110 anc_data[2] = tport->conn_instance;
1111 break;
1112 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001113 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114 }
Allan Stephens2db99832010-12-31 18:59:33 +00001115 if (has_name) {
1116 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1117 if (res)
1118 return res;
1119 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120
1121 return 0;
1122}
1123
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001124static void tipc_sk_send_ack(struct tipc_port *port, uint ack)
1125{
1126 struct sk_buff *buf = NULL;
1127 struct tipc_msg *msg;
1128 u32 peer_port = tipc_port_peerport(port);
1129 u32 dnode = tipc_port_peernode(port);
1130
1131 if (!port->connected)
1132 return;
1133 buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
1134 tipc_own_addr, peer_port, port->ref, TIPC_OK);
1135 if (!buf)
1136 return;
1137 msg = buf_msg(buf);
1138 msg_set_msgcnt(msg, ack);
1139 tipc_link_xmit(buf, dnode, msg_link_selector(msg));
1140}
1141
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001142static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001143{
1144 struct sock *sk = sock->sk;
1145 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001146 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001147 int err;
1148
1149 for (;;) {
1150 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001151 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001152 if (sock->state == SS_DISCONNECTING) {
1153 err = -ENOTCONN;
1154 break;
1155 }
1156 release_sock(sk);
1157 timeo = schedule_timeout(timeo);
1158 lock_sock(sk);
1159 }
1160 err = 0;
1161 if (!skb_queue_empty(&sk->sk_receive_queue))
1162 break;
1163 err = sock_intr_errno(timeo);
1164 if (signal_pending(current))
1165 break;
1166 err = -EAGAIN;
1167 if (!timeo)
1168 break;
1169 }
1170 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001171 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001172 return err;
1173}
1174
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001175/**
Ying Xue247f0f32014-02-18 16:06:46 +08001176 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 * @iocb: (unused)
1178 * @m: descriptor for message info
1179 * @buf_len: total size of user buffer area
1180 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001181 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1183 * If the complete message doesn't fit in user area, truncate it.
1184 *
1185 * Returns size of returned message data, errno otherwise
1186 */
Ying Xue247f0f32014-02-18 16:06:46 +08001187static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1188 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001190 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001191 struct tipc_sock *tsk = tipc_sk(sk);
1192 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 struct sk_buff *buf;
1194 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001195 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 unsigned int sz;
1197 u32 err;
1198 int res;
1199
Allan Stephens0c3141e2008-04-15 00:22:02 -07001200 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 if (unlikely(!buf_len))
1202 return -EINVAL;
1203
Allan Stephens0c3141e2008-04-15 00:22:02 -07001204 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205
Allan Stephens0c3141e2008-04-15 00:22:02 -07001206 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 res = -ENOTCONN;
1208 goto exit;
1209 }
1210
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001211 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001212restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213
Allan Stephens0c3141e2008-04-15 00:22:02 -07001214 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001215 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001216 if (res)
1217 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001218
1219 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001220 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 msg = buf_msg(buf);
1222 sz = msg_data_sz(msg);
1223 err = msg_errcode(msg);
1224
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001227 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 goto restart;
1229 }
1230
1231 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 set_orig_addr(m, msg);
1233
1234 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001235 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001236 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 goto exit;
1238
1239 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 if (!err) {
1241 if (unlikely(buf_len < sz)) {
1242 sz = buf_len;
1243 m->msg_flags |= MSG_TRUNC;
1244 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001245 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1246 m->msg_iov, sz);
1247 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249 res = sz;
1250 } else {
1251 if ((sock->state == SS_READY) ||
1252 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1253 res = 0;
1254 else
1255 res = -ECONNRESET;
1256 }
1257
1258 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001260 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001261 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001262 tipc_sk_send_ack(port, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001263 tsk->rcv_unacked = 0;
1264 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001265 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001266 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001267exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001268 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269 return res;
1270}
1271
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001272/**
Ying Xue247f0f32014-02-18 16:06:46 +08001273 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001274 * @iocb: (unused)
1275 * @m: descriptor for message info
1276 * @buf_len: total size of user buffer area
1277 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001278 *
1279 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280 * will optionally wait for more; never truncates data.
1281 *
1282 * Returns size of returned message data, errno otherwise
1283 */
Ying Xue247f0f32014-02-18 16:06:46 +08001284static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1285 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001287 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001288 struct tipc_sock *tsk = tipc_sk(sk);
1289 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290 struct sk_buff *buf;
1291 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001292 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001294 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001297 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298
1299 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 if (unlikely(!buf_len))
1301 return -EINVAL;
1302
Allan Stephens0c3141e2008-04-15 00:22:02 -07001303 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001305 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 res = -ENOTCONN;
1307 goto exit;
1308 }
1309
Florian Westphal3720d402010-08-17 11:00:04 +00001310 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001311 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001312
Allan Stephens0c3141e2008-04-15 00:22:02 -07001313restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001314 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001315 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001316 if (res)
1317 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001318
1319 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001320 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001321 msg = buf_msg(buf);
1322 sz = msg_data_sz(msg);
1323 err = msg_errcode(msg);
1324
1325 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001327 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 goto restart;
1329 }
1330
1331 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 if (sz_copied == 0) {
1333 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001334 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001335 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 goto exit;
1337 }
1338
1339 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001341 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342
Allan Stephens0232fd02011-02-21 09:45:40 -05001343 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 needed = (buf_len - sz_copied);
1345 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001346
1347 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1348 m->msg_iov, sz_to_copy);
1349 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001351
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 sz_copied += sz_to_copy;
1353
1354 if (sz_to_copy < sz) {
1355 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001356 TIPC_SKB_CB(buf)->handle =
1357 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 goto exit;
1359 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 } else {
1361 if (sz_copied != 0)
1362 goto exit; /* can't add error msg to valid data */
1363
1364 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1365 res = 0;
1366 else
1367 res = -ECONNRESET;
1368 }
1369
1370 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001372 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001373 tipc_sk_send_ack(port, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001374 tsk->rcv_unacked = 0;
1375 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001376 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001377 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378
1379 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001380 if ((sz_copied < buf_len) && /* didn't get all requested data */
1381 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001382 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001383 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1384 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 goto restart;
1386
1387exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001388 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001389 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390}
1391
1392/**
Ying Xuef288bef2012-08-21 11:16:57 +08001393 * tipc_write_space - wake up thread if port congestion is released
1394 * @sk: socket
1395 */
1396static void tipc_write_space(struct sock *sk)
1397{
1398 struct socket_wq *wq;
1399
1400 rcu_read_lock();
1401 wq = rcu_dereference(sk->sk_wq);
1402 if (wq_has_sleeper(wq))
1403 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1404 POLLWRNORM | POLLWRBAND);
1405 rcu_read_unlock();
1406}
1407
1408/**
1409 * tipc_data_ready - wake up threads to indicate messages have been received
1410 * @sk: socket
1411 * @len: the length of messages
1412 */
David S. Miller676d2362014-04-11 16:15:36 -04001413static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001414{
1415 struct socket_wq *wq;
1416
1417 rcu_read_lock();
1418 wq = rcu_dereference(sk->sk_wq);
1419 if (wq_has_sleeper(wq))
1420 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1421 POLLRDNORM | POLLRDBAND);
1422 rcu_read_unlock();
1423}
1424
1425/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001426 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001427 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001428 * @msg: message
1429 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001430 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001431 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001432static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001433{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001434 struct sock *sk = &tsk->sk;
1435 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001436 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001437 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001438
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001439 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001440
1441 if (msg_mcast(msg))
1442 return retval;
1443
1444 switch ((int)sock->state) {
1445 case SS_CONNECTED:
1446 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001447 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001448 if (unlikely(msg_errcode(msg))) {
1449 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001450 port->connected = 0;
1451 /* let timer expire on it's own */
1452 tipc_node_remove_conn(tipc_port_peernode(port),
1453 port->ref);
Ying Xue7e6c1312012-11-29 18:39:14 -05001454 }
1455 retval = TIPC_OK;
1456 }
1457 break;
1458 case SS_CONNECTING:
1459 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001460
1461 if (unlikely(!msg_connected(msg)))
1462 break;
1463
Ying Xue584d24b2012-11-29 18:51:19 -05001464 if (unlikely(msg_errcode(msg))) {
1465 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001466 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001467 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001468 break;
1469 }
1470
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001471 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001472 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001473 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001474 retval = TIPC_OK;
1475 break;
1476 }
1477
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001478 tipc_sk_finish_conn(port, msg_origport(msg), msg_orignode(msg));
1479 msg_set_importance(&port->phdr, msg_importance(msg));
1480 sock->state = SS_CONNECTED;
1481
Ying Xue584d24b2012-11-29 18:51:19 -05001482 /* If an incoming message is an 'ACK-', it should be
1483 * discarded here because it doesn't contain useful
1484 * data. In addition, we should try to wake up
1485 * connect() routine if sleeping.
1486 */
1487 if (msg_data_sz(msg) == 0) {
1488 kfree_skb(*buf);
1489 *buf = NULL;
1490 if (waitqueue_active(sk_sleep(sk)))
1491 wake_up_interruptible(sk_sleep(sk));
1492 }
1493 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001494 break;
1495 case SS_LISTENING:
1496 case SS_UNCONNECTED:
1497 /* Accept only SYN message */
1498 if (!msg_connected(msg) && !(msg_errcode(msg)))
1499 retval = TIPC_OK;
1500 break;
1501 case SS_DISCONNECTING:
1502 break;
1503 default:
1504 pr_err("Unknown socket state %u\n", sock->state);
1505 }
1506 return retval;
1507}
1508
1509/**
Ying Xueaba79f32013-01-20 23:30:09 +01001510 * rcvbuf_limit - get proper overload limit of socket receive queue
1511 * @sk: socket
1512 * @buf: message
1513 *
1514 * For all connection oriented messages, irrespective of importance,
1515 * the default overload value (i.e. 67MB) is set as limit.
1516 *
1517 * For all connectionless messages, by default new queue limits are
1518 * as belows:
1519 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001520 * TIPC_LOW_IMPORTANCE (4 MB)
1521 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1522 * TIPC_HIGH_IMPORTANCE (16 MB)
1523 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001524 *
1525 * Returns overload limit according to corresponding message importance
1526 */
1527static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1528{
1529 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001530
1531 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001532 return sysctl_tipc_rmem[2];
1533
1534 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1535 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001536}
1537
1538/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001539 * filter_rcv - validate incoming message
1540 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001541 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001542 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001543 * Enqueues message on receive queue if acceptable; optionally handles
1544 * disconnect indication for a connected socket.
1545 *
1546 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001547 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001548 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001549 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001550 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001551static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001552{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001553 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001554 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001555 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001556 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001557 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001558 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001560 if (unlikely(msg_user(msg) == CONN_MANAGER))
1561 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001562
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001563 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1564 kfree_skb(buf);
1565 tsk->link_cong = 0;
1566 sk->sk_write_space(sk);
1567 return TIPC_OK;
1568 }
1569
Per Lidenb97bf3f2006-01-02 19:04:38 +01001570 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001571 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001572 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001573
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001575 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001576 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001578 rc = filter_connect(tsk, &buf);
1579 if (rc != TIPC_OK || buf == NULL)
1580 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581 }
1582
1583 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001584 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001585 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586
Ying Xueaba79f32013-01-20 23:30:09 +01001587 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001588 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001589 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001590 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001591
David S. Miller676d2362014-04-11 16:15:36 -04001592 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593 return TIPC_OK;
1594}
1595
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001596/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001597 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001598 * @sk: socket
1599 * @buf: message
1600 *
1601 * Caller must hold socket lock, but not port lock.
1602 *
1603 * Returns 0
1604 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001605static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001606{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001607 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001608 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001609 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001610 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001611
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001612 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001613
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001614 if (likely(!rc)) {
1615 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1616 atomic_add(truesize, &tsk->dupl_rcvcnt);
1617 return 0;
1618 }
1619
1620 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1621 return 0;
1622
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001623 tipc_link_xmit(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001624
Allan Stephens0c3141e2008-04-15 00:22:02 -07001625 return 0;
1626}
1627
1628/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001629 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001630 * @buf: buffer containing arriving message
1631 * Consumes buffer
1632 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001633 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001634int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001635{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001636 struct tipc_sock *tsk;
1637 struct tipc_port *port;
1638 struct sock *sk;
1639 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001640 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001641 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001642 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001643
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001644 /* Validate destination and message */
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001645 tsk = tipc_sk_get(dport);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001646 if (unlikely(!tsk)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001647 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001648 goto exit;
1649 }
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001650 port = &tsk->port;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001651 sk = &tsk->sk;
1652
1653 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001654 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001655
Allan Stephens0c3141e2008-04-15 00:22:02 -07001656 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001657 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001658 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001659 if (sk->sk_backlog.len == 0)
1660 atomic_set(&tsk->dupl_rcvcnt, 0);
1661 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1662 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001663 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001664 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001665 bh_unlock_sock(sk);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001666 tipc_sk_put(tsk);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001667 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001668 return 0;
1669exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001670 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001671 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001672
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001673 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001674 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001675}
1676
Ying Xue78eb3a52014-01-17 09:50:03 +08001677static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1678{
1679 struct sock *sk = sock->sk;
1680 DEFINE_WAIT(wait);
1681 int done;
1682
1683 do {
1684 int err = sock_error(sk);
1685 if (err)
1686 return err;
1687 if (!*timeo_p)
1688 return -ETIMEDOUT;
1689 if (signal_pending(current))
1690 return sock_intr_errno(*timeo_p);
1691
1692 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1693 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1694 finish_wait(sk_sleep(sk), &wait);
1695 } while (!done);
1696 return 0;
1697}
1698
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699/**
Ying Xue247f0f32014-02-18 16:06:46 +08001700 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 * @sock: socket structure
1702 * @dest: socket address for destination port
1703 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001704 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 *
1706 * Returns 0 on success, errno otherwise
1707 */
Ying Xue247f0f32014-02-18 16:06:46 +08001708static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1709 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001711 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001712 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1713 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001714 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1715 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001716 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717
Allan Stephens0c3141e2008-04-15 00:22:02 -07001718 lock_sock(sk);
1719
Allan Stephensb89741a2008-04-15 00:20:37 -07001720 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001721 if (sock->state == SS_READY) {
1722 res = -EOPNOTSUPP;
1723 goto exit;
1724 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001725
Allan Stephensb89741a2008-04-15 00:20:37 -07001726 /*
1727 * Reject connection attempt using multicast address
1728 *
1729 * Note: send_msg() validates the rest of the address fields,
1730 * so there's no need to do it here
1731 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001732 if (dst->addrtype == TIPC_ADDR_MCAST) {
1733 res = -EINVAL;
1734 goto exit;
1735 }
1736
Ying Xue78eb3a52014-01-17 09:50:03 +08001737 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001738 switch (sock->state) {
1739 case SS_UNCONNECTED:
1740 /* Send a 'SYN-' to destination */
1741 m.msg_name = dest;
1742 m.msg_namelen = destlen;
1743
1744 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1745 * indicate send_msg() is never blocked.
1746 */
1747 if (!timeout)
1748 m.msg_flags = MSG_DONTWAIT;
1749
Ying Xue247f0f32014-02-18 16:06:46 +08001750 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001751 if ((res < 0) && (res != -EWOULDBLOCK))
1752 goto exit;
1753
1754 /* Just entered SS_CONNECTING state; the only
1755 * difference is that return value in non-blocking
1756 * case is EINPROGRESS, rather than EALREADY.
1757 */
1758 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001759 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001760 if (previous == SS_CONNECTING)
1761 res = -EALREADY;
1762 if (!timeout)
1763 goto exit;
1764 timeout = msecs_to_jiffies(timeout);
1765 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1766 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001767 break;
1768 case SS_CONNECTED:
1769 res = -EISCONN;
1770 break;
1771 default:
1772 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001773 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001774 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001775exit:
1776 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001777 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778}
1779
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001780/**
Ying Xue247f0f32014-02-18 16:06:46 +08001781 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782 * @sock: socket structure
1783 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001784 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001785 * Returns 0 on success, errno otherwise
1786 */
Ying Xue247f0f32014-02-18 16:06:46 +08001787static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001789 struct sock *sk = sock->sk;
1790 int res;
1791
1792 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793
Ying Xue245f3d32011-07-06 06:01:13 -04001794 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001795 res = -EINVAL;
1796 else {
1797 sock->state = SS_LISTENING;
1798 res = 0;
1799 }
1800
1801 release_sock(sk);
1802 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803}
1804
Ying Xue6398e232014-01-17 09:50:04 +08001805static int tipc_wait_for_accept(struct socket *sock, long timeo)
1806{
1807 struct sock *sk = sock->sk;
1808 DEFINE_WAIT(wait);
1809 int err;
1810
1811 /* True wake-one mechanism for incoming connections: only
1812 * one process gets woken up, not the 'whole herd'.
1813 * Since we do not 'race & poll' for established sockets
1814 * anymore, the common case will execute the loop only once.
1815 */
1816 for (;;) {
1817 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1818 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001819 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001820 release_sock(sk);
1821 timeo = schedule_timeout(timeo);
1822 lock_sock(sk);
1823 }
1824 err = 0;
1825 if (!skb_queue_empty(&sk->sk_receive_queue))
1826 break;
1827 err = -EINVAL;
1828 if (sock->state != SS_LISTENING)
1829 break;
1830 err = sock_intr_errno(timeo);
1831 if (signal_pending(current))
1832 break;
1833 err = -EAGAIN;
1834 if (!timeo)
1835 break;
1836 }
1837 finish_wait(sk_sleep(sk), &wait);
1838 return err;
1839}
1840
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001841/**
Ying Xue247f0f32014-02-18 16:06:46 +08001842 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001843 * @sock: listening socket
1844 * @newsock: new socket that is to be connected
1845 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001846 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847 * Returns 0 on success, errno otherwise
1848 */
Ying Xue247f0f32014-02-18 16:06:46 +08001849static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001851 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001852 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001853 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001854 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001855 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001856 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001857
Allan Stephens0c3141e2008-04-15 00:22:02 -07001858 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001859
Allan Stephens0c3141e2008-04-15 00:22:02 -07001860 if (sock->state != SS_LISTENING) {
1861 res = -EINVAL;
1862 goto exit;
1863 }
Ying Xue6398e232014-01-17 09:50:04 +08001864 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1865 res = tipc_wait_for_accept(sock, timeo);
1866 if (res)
1867 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001868
1869 buf = skb_peek(&sk->sk_receive_queue);
1870
Ying Xuec5fa7b32013-06-17 10:54:39 -04001871 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001872 if (res)
1873 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001874
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001875 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001876 new_port = &tipc_sk(new_sk)->port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001877 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001878
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001879 /* we lock on new_sk; but lockdep sees the lock on sk */
1880 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001881
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001882 /*
1883 * Reject any stray messages received by new socket
1884 * before the socket lock was taken (very, very unlikely)
1885 */
1886 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001888 /* Connect new socket to it's peer */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001889 tipc_sk_finish_conn(new_port, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001890 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001892 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001893 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001894 new_port->conn_type = msg_nametype(msg);
1895 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001897
1898 /*
1899 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1900 * Respond to 'SYN+' by queuing it on new socket.
1901 */
1902 if (!msg_data_sz(msg)) {
1903 struct msghdr m = {NULL,};
1904
1905 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001906 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001907 } else {
1908 __skb_dequeue(&sk->sk_receive_queue);
1909 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001910 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001911 }
1912 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001913exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001914 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915 return res;
1916}
1917
1918/**
Ying Xue247f0f32014-02-18 16:06:46 +08001919 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001921 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001922 *
1923 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001924 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001925 * Returns 0 on success, errno otherwise
1926 */
Ying Xue247f0f32014-02-18 16:06:46 +08001927static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001929 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001930 struct tipc_sock *tsk = tipc_sk(sk);
1931 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001932 struct sk_buff *buf;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001933 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934 int res;
1935
Allan Stephense247a8f2008-03-06 15:05:38 -08001936 if (how != SHUT_RDWR)
1937 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001938
Allan Stephens0c3141e2008-04-15 00:22:02 -07001939 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940
1941 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001942 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001943 case SS_CONNECTED:
1944
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001946 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001947 buf = __skb_dequeue(&sk->sk_receive_queue);
1948 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001949 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001950 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951 goto restart;
1952 }
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001953 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
1954 tipc_link_xmit(buf, dnode, port->ref);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001955 tipc_node_remove_conn(dnode, port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001956 } else {
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001957 dnode = tipc_port_peernode(port);
1958 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
1959 TIPC_CONN_MSG, SHORT_H_SIZE,
1960 0, dnode, tipc_own_addr,
1961 tipc_port_peerport(port),
1962 port->ref, TIPC_CONN_SHUTDOWN);
1963 tipc_link_xmit(buf, dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001964 }
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001965 port->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001966 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001967 tipc_node_remove_conn(dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968 /* fall through */
1969
1970 case SS_DISCONNECTING:
1971
Ying Xue75031152012-10-29 09:38:15 -04001972 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001973 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001974
1975 /* Wake up anyone sleeping in poll */
1976 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001977 res = 0;
1978 break;
1979
1980 default:
1981 res = -ENOTCONN;
1982 }
1983
Allan Stephens0c3141e2008-04-15 00:22:02 -07001984 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001985 return res;
1986}
1987
Jon Paul Maloy57289012014-08-22 18:09:09 -04001988static void tipc_sk_timeout(unsigned long ref)
1989{
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001990 struct tipc_sock *tsk;
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001991 struct tipc_port *port;
Jon Paul Maloy57289012014-08-22 18:09:09 -04001992 struct sock *sk;
1993 struct sk_buff *buf = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04001994 u32 peer_port, peer_node;
1995
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001996 tsk = tipc_sk_get(ref);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001997 if (!tsk)
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001998 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04001999 sk = &tsk->sk;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002000 port = &tsk->port;
2001
Jon Paul Maloy57289012014-08-22 18:09:09 -04002002 bh_lock_sock(sk);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002003 if (!port->connected) {
2004 bh_unlock_sock(sk);
2005 goto exit;
2006 }
Jon Paul Maloy57289012014-08-22 18:09:09 -04002007 peer_port = tipc_port_peerport(port);
2008 peer_node = tipc_port_peernode(port);
2009
2010 if (port->probing_state == TIPC_CONN_PROBING) {
2011 /* Previous probe not answered -> self abort */
2012 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2013 SHORT_H_SIZE, 0, tipc_own_addr,
2014 peer_node, ref, peer_port,
2015 TIPC_ERR_NO_PORT);
2016 } else {
2017 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2018 0, peer_node, tipc_own_addr,
2019 peer_port, ref, TIPC_OK);
2020 port->probing_state = TIPC_CONN_PROBING;
2021 k_start_timer(&port->timer, port->probing_interval);
2022 }
2023 bh_unlock_sock(sk);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002024 if (buf)
2025 tipc_link_xmit(buf, peer_node, ref);
2026exit:
2027 tipc_sk_put(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002028}
2029
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002030static int tipc_sk_show(struct tipc_port *port, char *buf,
2031 int len, int full_id)
2032{
2033 struct publication *publ;
2034 int ret;
2035
2036 if (full_id)
2037 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2038 tipc_zone(tipc_own_addr),
2039 tipc_cluster(tipc_own_addr),
2040 tipc_node(tipc_own_addr), port->ref);
2041 else
2042 ret = tipc_snprintf(buf, len, "%-10u:", port->ref);
2043
2044 if (port->connected) {
2045 u32 dport = tipc_port_peerport(port);
2046 u32 destnode = tipc_port_peernode(port);
2047
2048 ret += tipc_snprintf(buf + ret, len - ret,
2049 " connected to <%u.%u.%u:%u>",
2050 tipc_zone(destnode),
2051 tipc_cluster(destnode),
2052 tipc_node(destnode), dport);
2053 if (port->conn_type != 0)
2054 ret += tipc_snprintf(buf + ret, len - ret,
2055 " via {%u,%u}", port->conn_type,
2056 port->conn_instance);
2057 } else if (port->published) {
2058 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
2059 list_for_each_entry(publ, &port->publications, pport_list) {
2060 if (publ->lower == publ->upper)
2061 ret += tipc_snprintf(buf + ret, len - ret,
2062 " {%u,%u}", publ->type,
2063 publ->lower);
2064 else
2065 ret += tipc_snprintf(buf + ret, len - ret,
2066 " {%u,%u,%u}", publ->type,
2067 publ->lower, publ->upper);
2068 }
2069 }
2070 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2071 return ret;
2072}
2073
2074struct sk_buff *tipc_sk_socks_show(void)
2075{
2076 struct sk_buff *buf;
2077 struct tlv_desc *rep_tlv;
2078 char *pb;
2079 int pb_len;
2080 struct tipc_sock *tsk;
2081 int str_len = 0;
2082 u32 ref = 0;
2083
2084 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2085 if (!buf)
2086 return NULL;
2087 rep_tlv = (struct tlv_desc *)buf->data;
2088 pb = TLV_DATA(rep_tlv);
2089 pb_len = ULTRA_STRING_MAX_LEN;
2090
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002091 tsk = tipc_sk_get_next(&ref);
2092 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2093 lock_sock(&tsk->sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002094 str_len += tipc_sk_show(&tsk->port, pb + str_len,
2095 pb_len - str_len, 0);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002096 release_sock(&tsk->sk);
2097 tipc_sk_put(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002098 }
2099 str_len += 1; /* for "\0" */
2100 skb_put(buf, TLV_SPACE(str_len));
2101 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2102
2103 return buf;
2104}
2105
2106/* tipc_sk_reinit: set non-zero address in all existing sockets
2107 * when we go from standalone to network mode.
2108 */
2109void tipc_sk_reinit(void)
2110{
2111 struct tipc_msg *msg;
2112 u32 ref = 0;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002113 struct tipc_sock *tsk = tipc_sk_get_next(&ref);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002114
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002115 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2116 lock_sock(&tsk->sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002117 msg = &tsk->port.phdr;
2118 msg_set_prevnode(msg, tipc_own_addr);
2119 msg_set_orignode(msg, tipc_own_addr);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002120 release_sock(&tsk->sk);
2121 tipc_sk_put(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002122 }
2123}
2124
Per Lidenb97bf3f2006-01-02 19:04:38 +01002125/**
Ying Xue247f0f32014-02-18 16:06:46 +08002126 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002127 * @sock: socket structure
2128 * @lvl: option level
2129 * @opt: option identifier
2130 * @ov: pointer to new option value
2131 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002132 *
2133 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002134 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002135 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002136 * Returns 0 on success, errno otherwise
2137 */
Ying Xue247f0f32014-02-18 16:06:46 +08002138static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2139 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002140{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002141 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002142 struct tipc_sock *tsk = tipc_sk(sk);
2143 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002144 u32 value;
2145 int res;
2146
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002147 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2148 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002149 if (lvl != SOL_TIPC)
2150 return -ENOPROTOOPT;
2151 if (ol < sizeof(value))
2152 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002153 res = get_user(value, (u32 __user *)ov);
2154 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002155 return res;
2156
Allan Stephens0c3141e2008-04-15 00:22:02 -07002157 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002158
Per Lidenb97bf3f2006-01-02 19:04:38 +01002159 switch (opt) {
2160 case TIPC_IMPORTANCE:
Erik Hugneac32c7f2014-08-15 16:44:35 +02002161 res = tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002162 break;
2163 case TIPC_SRC_DROPPABLE:
2164 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002165 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002166 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002167 res = -ENOPROTOOPT;
2168 break;
2169 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002170 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002171 break;
2172 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002173 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002174 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002175 break;
2176 default:
2177 res = -EINVAL;
2178 }
2179
Allan Stephens0c3141e2008-04-15 00:22:02 -07002180 release_sock(sk);
2181
Per Lidenb97bf3f2006-01-02 19:04:38 +01002182 return res;
2183}
2184
2185/**
Ying Xue247f0f32014-02-18 16:06:46 +08002186 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002187 * @sock: socket structure
2188 * @lvl: option level
2189 * @opt: option identifier
2190 * @ov: receptacle for option value
2191 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002192 *
2193 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002194 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002195 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196 * Returns 0 on success, errno otherwise
2197 */
Ying Xue247f0f32014-02-18 16:06:46 +08002198static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2199 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002200{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002201 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002202 struct tipc_sock *tsk = tipc_sk(sk);
2203 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002204 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002205 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002206 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002207
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002208 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2209 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002210 if (lvl != SOL_TIPC)
2211 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002212 res = get_user(len, ol);
2213 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002214 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002215
Allan Stephens0c3141e2008-04-15 00:22:02 -07002216 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002217
2218 switch (opt) {
2219 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002220 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002221 break;
2222 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002223 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002224 break;
2225 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002226 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002227 break;
2228 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002229 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002230 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002231 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002232 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002233 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002234 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002235 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002236 value = skb_queue_len(&sk->sk_receive_queue);
2237 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002238 default:
2239 res = -EINVAL;
2240 }
2241
Allan Stephens0c3141e2008-04-15 00:22:02 -07002242 release_sock(sk);
2243
Paul Gortmaker25860c32010-12-31 18:59:31 +00002244 if (res)
2245 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002246
Paul Gortmaker25860c32010-12-31 18:59:31 +00002247 if (len < sizeof(value))
2248 return -EINVAL;
2249
2250 if (copy_to_user(ov, &value, sizeof(value)))
2251 return -EFAULT;
2252
2253 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002254}
2255
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002256static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002257{
2258 struct tipc_sioc_ln_req lnr;
2259 void __user *argp = (void __user *)arg;
2260
2261 switch (cmd) {
2262 case SIOCGETLINKNAME:
2263 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2264 return -EFAULT;
2265 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2266 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2267 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2268 return -EFAULT;
2269 return 0;
2270 }
2271 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002272 default:
2273 return -ENOIOCTLCMD;
2274 }
2275}
2276
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002277/* Protocol switches for the various types of TIPC sockets */
2278
Florian Westphalbca65ea2008-02-07 18:18:01 -08002279static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002280 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002281 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002282 .release = tipc_release,
2283 .bind = tipc_bind,
2284 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002285 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002286 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002287 .getname = tipc_getname,
2288 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002289 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002290 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002291 .shutdown = tipc_shutdown,
2292 .setsockopt = tipc_setsockopt,
2293 .getsockopt = tipc_getsockopt,
2294 .sendmsg = tipc_sendmsg,
2295 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002296 .mmap = sock_no_mmap,
2297 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002298};
2299
Florian Westphalbca65ea2008-02-07 18:18:01 -08002300static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002301 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002302 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002303 .release = tipc_release,
2304 .bind = tipc_bind,
2305 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002306 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002307 .accept = tipc_accept,
2308 .getname = tipc_getname,
2309 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002310 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002311 .listen = tipc_listen,
2312 .shutdown = tipc_shutdown,
2313 .setsockopt = tipc_setsockopt,
2314 .getsockopt = tipc_getsockopt,
2315 .sendmsg = tipc_send_packet,
2316 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002317 .mmap = sock_no_mmap,
2318 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002319};
2320
Florian Westphalbca65ea2008-02-07 18:18:01 -08002321static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002322 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002323 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002324 .release = tipc_release,
2325 .bind = tipc_bind,
2326 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002327 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002328 .accept = tipc_accept,
2329 .getname = tipc_getname,
2330 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002331 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002332 .listen = tipc_listen,
2333 .shutdown = tipc_shutdown,
2334 .setsockopt = tipc_setsockopt,
2335 .getsockopt = tipc_getsockopt,
2336 .sendmsg = tipc_send_stream,
2337 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002338 .mmap = sock_no_mmap,
2339 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002340};
2341
Florian Westphalbca65ea2008-02-07 18:18:01 -08002342static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002343 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002344 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002345 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002346};
2347
2348static struct proto tipc_proto = {
2349 .name = "TIPC",
2350 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002351 .obj_size = sizeof(struct tipc_sock),
2352 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002353};
2354
Ying Xuec5fa7b32013-06-17 10:54:39 -04002355static struct proto tipc_proto_kern = {
2356 .name = "TIPC",
2357 .obj_size = sizeof(struct tipc_sock),
2358 .sysctl_rmem = sysctl_tipc_rmem
2359};
2360
Per Lidenb97bf3f2006-01-02 19:04:38 +01002361/**
Per Liden4323add2006-01-18 00:38:21 +01002362 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002363 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002364 * Returns 0 on success, errno otherwise
2365 */
Per Liden4323add2006-01-18 00:38:21 +01002366int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002367{
2368 int res;
2369
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002370 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002371 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002372 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002373 goto out;
2374 }
2375
2376 res = sock_register(&tipc_family_ops);
2377 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002378 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002379 proto_unregister(&tipc_proto);
2380 goto out;
2381 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002382 out:
2383 return res;
2384}
2385
2386/**
Per Liden4323add2006-01-18 00:38:21 +01002387 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002388 */
Per Liden4323add2006-01-18 00:38:21 +01002389void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002390{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002391 sock_unregister(tipc_family_ops.family);
2392 proto_unregister(&tipc_proto);
2393}