blob: 9e95c1ea556406e3b1713501a0bfaf4ff4f05ee1 [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 Maloye2dafe82014-06-25 20:41:37 -050038#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020039#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050040#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040041#include <linux/export.h>
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -040042#include "config.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040043#include "socket.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040044
Per Lidenb97bf3f2006-01-02 19:04:38 +010045#define SS_LISTENING -1 /* socket is listening */
46#define SS_READY -2 /* socket is connectionless */
47
Jon Paul Maloy301bae52014-08-22 18:09:20 -040048#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloydadebc02014-08-22 18:09:11 -040049#define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
Jon Paul Maloy301bae52014-08-22 18:09:20 -040050#define TIPC_FWD_MSG 1
51#define TIPC_CONN_OK 0
52#define TIPC_CONN_PROBING 1
53
54/**
55 * struct tipc_sock - TIPC socket structure
56 * @sk: socket - interacts with 'port' and with user via the socket API
57 * @connected: non-zero if port is currently connected to a peer port
58 * @conn_type: TIPC type used when connection was established
59 * @conn_instance: TIPC instance used when connection was established
60 * @published: non-zero if port has one or more associated names
61 * @max_pkt: maximum packet size "hint" used when building messages sent by port
62 * @ref: unique reference to port in TIPC object registry
63 * @phdr: preformatted message header used when sending messages
64 * @port_list: adjacent ports in TIPC's global list of ports
65 * @publications: list of publications for port
66 * @pub_count: total # of publications port has made during its lifetime
67 * @probing_state:
68 * @probing_interval:
69 * @timer:
70 * @port: port - interacts with 'sk' and with the rest of the TIPC stack
71 * @peer_name: the peer of the connection, if any
72 * @conn_timeout: the time we can wait for an unresponded setup request
73 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
74 * @link_cong: non-zero if owner must sleep because of link congestion
75 * @sent_unacked: # messages sent by socket, and not yet acked by peer
76 * @rcv_unacked: # messages read by user, but not yet acked back to peer
77 */
78struct tipc_sock {
79 struct sock sk;
80 int connected;
81 u32 conn_type;
82 u32 conn_instance;
83 int published;
84 u32 max_pkt;
85 u32 ref;
86 struct tipc_msg phdr;
87 struct list_head sock_list;
88 struct list_head publications;
89 u32 pub_count;
90 u32 probing_state;
91 u32 probing_interval;
92 struct timer_list timer;
93 uint conn_timeout;
94 atomic_t dupl_rcvcnt;
95 bool link_cong;
96 uint sent_unacked;
97 uint rcv_unacked;
98};
Per Lidenb97bf3f2006-01-02 19:04:38 +010099
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400100static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400101static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800102static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800103static int tipc_release(struct socket *sock);
104static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400105static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Jon Paul Maloy57289012014-08-22 18:09:09 -0400106static void tipc_sk_timeout(unsigned long ref);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400107static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400108 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400109static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400110 struct tipc_name_seq const *seq);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -0400111static u32 tipc_sk_ref_acquire(struct tipc_sock *tsk);
112static void tipc_sk_ref_discard(u32 ref);
113static struct tipc_sock *tipc_sk_get(u32 ref);
114static struct tipc_sock *tipc_sk_get_next(u32 *ref);
115static void tipc_sk_put(struct tipc_sock *tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116
Florian Westphalbca65ea2008-02-07 18:18:01 -0800117static const struct proto_ops packet_ops;
118static const struct proto_ops stream_ops;
119static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120
121static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400122static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900124/*
Allan Stephens0c3141e2008-04-15 00:22:02 -0700125 * Revised TIPC socket locking policy:
126 *
127 * Most socket operations take the standard socket lock when they start
128 * and hold it until they finish (or until they need to sleep). Acquiring
129 * this lock grants the owner exclusive access to the fields of the socket
130 * data structures, with the exception of the backlog queue. A few socket
131 * operations can be done without taking the socket lock because they only
132 * read socket information that never changes during the life of the socket.
133 *
134 * Socket operations may acquire the lock for the associated TIPC port if they
135 * need to perform an operation on the port. If any routine needs to acquire
136 * both the socket lock and the port lock it must take the socket lock first
137 * to avoid the risk of deadlock.
138 *
139 * The dispatcher handling incoming messages cannot grab the socket lock in
140 * the standard fashion, since invoked it runs at the BH level and cannot block.
141 * Instead, it checks to see if the socket lock is currently owned by someone,
142 * and either handles the message itself or adds it to the socket's backlog
143 * queue; in the latter case the queued message is processed once the process
144 * owning the socket lock releases it.
145 *
146 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
147 * the problem of a blocked socket operation preventing any other operations
148 * from occurring. However, applications must be careful if they have
149 * multiple threads trying to send (or receive) on the same socket, as these
150 * operations might interfere with each other. For example, doing a connect
151 * and a receive at the same time might allow the receive to consume the
152 * ACK message meant for the connect. While additional work could be done
153 * to try and overcome this, it doesn't seem to be worthwhile at the present.
154 *
155 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
156 * that another operation that must be performed in a non-blocking manner is
157 * not delayed for very long because the lock has already been taken.
158 *
159 * NOTE: This code assumes that certain fields of a port/socket pair are
160 * constant over its lifetime; such fields can be examined without taking
161 * the socket lock and/or port lock, and do not need to be re-read even
162 * after resuming processing after waiting. These fields include:
163 * - socket type
164 * - pointer to socket sk structure (aka tipc_sock structure)
165 * - pointer to port structure
166 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400169static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400170{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400171 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400172}
173
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400174static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400175{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400176 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400177}
178
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400179static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400180{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400181 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400182}
183
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400184static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400185{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400186 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400187}
188
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400189static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400190{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400191 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400192}
193
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400194static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400195{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400196 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400197}
198
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400199static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400200{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400201 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400202}
203
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400204static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400205{
206 if (imp > TIPC_CRITICAL_IMPORTANCE)
207 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400208 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400209 return 0;
210}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400211
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400212static struct tipc_sock *tipc_sk(const struct sock *sk)
213{
214 return container_of(sk, struct tipc_sock, sk);
215}
216
217static int tsk_conn_cong(struct tipc_sock *tsk)
218{
219 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
220}
221
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400223 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700224 *
225 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400227static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400229 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230}
231
232/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400233 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700234 *
235 * Caller must hold socket lock
236 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400237static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700238{
239 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500240 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700241
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500242 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
243 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400244 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500245 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700246}
247
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400248/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400249 *
250 * Handles cases where the node's network address has changed from
251 * the default of <0.0.0> to its configured setting.
252 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400253static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400254{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400255 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400256 u32 orig_node;
257 u32 peer_node;
258
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400259 if (unlikely(!tsk->connected))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400260 return false;
261
262 if (unlikely(msg_origport(msg) != peer_port))
263 return false;
264
265 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400266 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400267
268 if (likely(orig_node == peer_node))
269 return true;
270
271 if (!orig_node && (peer_node == tipc_own_addr))
272 return true;
273
274 if (!peer_node && (orig_node == tipc_own_addr))
275 return true;
276
277 return false;
278}
279
Allan Stephens0c3141e2008-04-15 00:22:02 -0700280/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400281 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700282 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283 * @sock: pre-allocated socket structure
284 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800285 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900286 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700287 * This routine creates additional data structures used by the TIPC socket,
288 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 *
290 * Returns 0 on success, errno otherwise
291 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400292static int tipc_sk_create(struct net *net, struct socket *sock,
293 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700295 const struct proto_ops *ops;
296 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400298 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400299 struct tipc_msg *msg;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400300 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700301
302 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 if (unlikely(protocol != 0))
304 return -EPROTONOSUPPORT;
305
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306 switch (sock->type) {
307 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700308 ops = &stream_ops;
309 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 break;
311 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700312 ops = &packet_ops;
313 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 break;
315 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700317 ops = &msg_ops;
318 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319 break;
Allan Stephens49978652006-06-25 23:47:18 -0700320 default:
Allan Stephens49978652006-06-25 23:47:18 -0700321 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 }
323
Allan Stephens0c3141e2008-04-15 00:22:02 -0700324 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400325 if (!kern)
326 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
327 else
328 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
329
Allan Stephens0c3141e2008-04-15 00:22:02 -0700330 if (sk == NULL)
331 return -ENOMEM;
332
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400333 tsk = tipc_sk(sk);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -0400334 ref = tipc_sk_ref_acquire(tsk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400335 if (!ref) {
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400336 pr_warn("Socket create failed; reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 return -ENOMEM;
338 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400339 tsk->max_pkt = MAX_PKT_DEFAULT;
340 tsk->ref = ref;
341 INIT_LIST_HEAD(&tsk->publications);
342 msg = &tsk->phdr;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400343 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
344 NAMED_H_SIZE, 0);
345 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346
Allan Stephens0c3141e2008-04-15 00:22:02 -0700347 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700348 sock->ops = ops;
349 sock->state = state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350 sock_init_data(sock, sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400351 k_init_timer(&tsk->timer, (Handler)tipc_sk_timeout, ref);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400352 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400353 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800354 sk->sk_data_ready = tipc_data_ready;
355 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400356 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500357 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400358 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700359
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 if (sock->state == SS_READY) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400361 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700362 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400363 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700364 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 return 0;
366}
367
368/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400369 * tipc_sock_create_local - create TIPC socket from inside TIPC module
370 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
371 *
372 * We cannot use sock_creat_kern here because it bumps module user count.
373 * Since socket owner and creator is the same module we must make sure
374 * that module count remains zero for module local sockets, otherwise
375 * we cannot do rmmod.
376 *
377 * Returns 0 on success, errno otherwise
378 */
379int tipc_sock_create_local(int type, struct socket **res)
380{
381 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400382
383 rc = sock_create_lite(AF_TIPC, type, 0, res);
384 if (rc < 0) {
385 pr_err("Failed to create kernel socket\n");
386 return rc;
387 }
388 tipc_sk_create(&init_net, *res, 0, 1);
389
Ying Xuec5fa7b32013-06-17 10:54:39 -0400390 return 0;
391}
392
393/**
394 * tipc_sock_release_local - release socket created by tipc_sock_create_local
395 * @sock: the socket to be released.
396 *
397 * Module reference count is not incremented when such sockets are created,
398 * so we must keep it from being decremented when they are released.
399 */
400void tipc_sock_release_local(struct socket *sock)
401{
Ying Xue247f0f32014-02-18 16:06:46 +0800402 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400403 sock->ops = NULL;
404 sock_release(sock);
405}
406
407/**
408 * tipc_sock_accept_local - accept a connection on a socket created
409 * with tipc_sock_create_local. Use this function to avoid that
410 * module reference count is inadvertently incremented.
411 *
412 * @sock: the accepting socket
413 * @newsock: reference to the new socket to be created
414 * @flags: socket flags
415 */
416
417int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400418 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400419{
420 struct sock *sk = sock->sk;
421 int ret;
422
423 ret = sock_create_lite(sk->sk_family, sk->sk_type,
424 sk->sk_protocol, newsock);
425 if (ret < 0)
426 return ret;
427
Ying Xue247f0f32014-02-18 16:06:46 +0800428 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400429 if (ret < 0) {
430 sock_release(*newsock);
431 return ret;
432 }
433 (*newsock)->ops = sock->ops;
434 return ret;
435}
436
437/**
Ying Xue247f0f32014-02-18 16:06:46 +0800438 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 * @sock: socket to destroy
440 *
441 * This routine cleans up any messages that are still queued on the socket.
442 * For DGRAM and RDM socket types, all queued messages are rejected.
443 * For SEQPACKET and STREAM socket types, the first message is rejected
444 * and any others are discarded. (If the first message on a STREAM socket
445 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900446 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 * NOTE: Rejected messages are not necessarily returned to the sender! They
448 * are returned or discarded according to the "destination droppable" setting
449 * specified for the message by the sender.
450 *
451 * Returns 0 on success, errno otherwise
452 */
Ying Xue247f0f32014-02-18 16:06:46 +0800453static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400456 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500458 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459
Allan Stephens0c3141e2008-04-15 00:22:02 -0700460 /*
461 * Exit if socket isn't fully initialized (occurs when a failed accept()
462 * releases a pre-allocated child socket that was never used)
463 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700464 if (sk == NULL)
465 return 0;
466
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400467 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700468 lock_sock(sk);
469
470 /*
471 * Reject all unreceived messages, except on an active connection
472 * (which disconnects locally & sends a 'FIN+' to peer)
473 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400474 dnode = tsk_peer_node(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700476 buf = __skb_dequeue(&sk->sk_receive_queue);
477 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478 break;
Ying Xue40682432013-10-18 07:23:16 +0200479 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400480 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700481 else {
482 if ((sock->state == SS_CONNECTING) ||
483 (sock->state == SS_CONNECTED)) {
484 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400485 tsk->connected = 0;
486 tipc_node_remove_conn(dnode, tsk->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700487 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500488 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400489 tipc_link_xmit(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700490 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 }
492
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400493 tipc_sk_withdraw(tsk, 0, NULL);
494 tipc_sk_ref_discard(tsk->ref);
495 k_cancel_timer(&tsk->timer);
496 if (tsk->connected) {
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400497 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
498 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400499 tsk_peer_port(tsk),
500 tsk->ref, TIPC_ERR_NO_PORT);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400501 if (buf)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400502 tipc_link_xmit(buf, dnode, tsk->ref);
503 tipc_node_remove_conn(dnode, tsk->ref);
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400504 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400505 k_term_timer(&tsk->timer);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506
Allan Stephens0c3141e2008-04-15 00:22:02 -0700507 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100508 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509
Allan Stephens0c3141e2008-04-15 00:22:02 -0700510 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700511 sock->state = SS_DISCONNECTING;
512 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700514 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200516 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517}
518
519/**
Ying Xue247f0f32014-02-18 16:06:46 +0800520 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 * @sock: socket structure
522 * @uaddr: socket address describing name(s) and desired operation
523 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900524 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 * Name and name sequence binding is indicated using a positive scope value;
526 * a negative scope value unbinds the specified name. Specifying no name
527 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900528 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700530 *
531 * NOTE: This routine doesn't need to take the socket lock since it doesn't
532 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 */
Ying Xue247f0f32014-02-18 16:06:46 +0800534static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
535 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536{
Ying Xue84602762013-12-27 10:18:28 +0800537 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400539 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800540 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541
Ying Xue84602762013-12-27 10:18:28 +0800542 lock_sock(sk);
543 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400544 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800545 goto exit;
546 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900547
Ying Xue84602762013-12-27 10:18:28 +0800548 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
549 res = -EINVAL;
550 goto exit;
551 }
552 if (addr->family != AF_TIPC) {
553 res = -EAFNOSUPPORT;
554 goto exit;
555 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 if (addr->addrtype == TIPC_ADDR_NAME)
558 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800559 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
560 res = -EAFNOSUPPORT;
561 goto exit;
562 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900563
Ying Xue13a2e892013-06-17 10:54:40 -0400564 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400565 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800566 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
567 res = -EACCES;
568 goto exit;
569 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400570
Ying Xue84602762013-12-27 10:18:28 +0800571 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400572 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
573 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800574exit:
575 release_sock(sk);
576 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577}
578
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900579/**
Ying Xue247f0f32014-02-18 16:06:46 +0800580 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 * @sock: socket structure
582 * @uaddr: area for returned socket address
583 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700584 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900585 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700587 *
Allan Stephens2da59912008-07-14 22:43:32 -0700588 * NOTE: This routine doesn't need to take the socket lock since it only
589 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000590 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 */
Ying Xue247f0f32014-02-18 16:06:46 +0800592static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
593 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400596 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000598 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700599 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700600 if ((sock->state != SS_CONNECTED) &&
601 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
602 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400603 addr->addr.id.ref = tsk_peer_port(tsk);
604 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700605 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400606 addr->addr.id.ref = tsk->ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000607 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700608 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609
610 *uaddr_len = sizeof(*addr);
611 addr->addrtype = TIPC_ADDR_ID;
612 addr->family = AF_TIPC;
613 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 addr->addr.name.domain = 0;
615
Allan Stephens0c3141e2008-04-15 00:22:02 -0700616 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617}
618
619/**
Ying Xue247f0f32014-02-18 16:06:46 +0800620 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 * @file: file structure associated with the socket
622 * @sock: socket for which to calculate the poll bits
623 * @wait: ???
624 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700625 * Returns pollmask value
626 *
627 * COMMENTARY:
628 * It appears that the usual socket locking mechanisms are not useful here
629 * since the pollmask info is potentially out-of-date the moment this routine
630 * exits. TCP and other protocols seem to rely on higher level poll routines
631 * to handle any preventable race conditions, so TIPC will do the same ...
632 *
633 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700634 *
Allan Stephensf662c072010-08-17 11:00:06 +0000635 * socket state flags set
636 * ------------ ---------
637 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200638 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000639 *
640 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
641 * no write flags
642 *
643 * connected POLLIN/POLLRDNORM if data in rx queue
644 * POLLOUT if port is not congested
645 *
646 * disconnecting POLLIN/POLLRDNORM/POLLHUP
647 * no write flags
648 *
649 * listening POLLIN if SYN in rx queue
650 * no write flags
651 *
652 * ready POLLIN/POLLRDNORM if data in rx queue
653 * [connectionless] POLLOUT (since port cannot be congested)
654 *
655 * IMPORTANT: The fact that a read or write operation is indicated does NOT
656 * imply that the operation will succeed, merely that it should be performed
657 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658 */
Ying Xue247f0f32014-02-18 16:06:46 +0800659static unsigned int tipc_poll(struct file *file, struct socket *sock,
660 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661{
Allan Stephens9b674e82008-03-26 16:48:21 -0700662 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400663 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000664 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700665
Ying Xuef288bef2012-08-21 11:16:57 +0800666 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700667
Allan Stephensf662c072010-08-17 11:00:06 +0000668 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200669 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500670 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200671 mask |= POLLOUT;
672 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000673 case SS_READY:
674 case SS_CONNECTED:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400675 if (!tsk->link_cong && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000676 mask |= POLLOUT;
677 /* fall thru' */
678 case SS_CONNECTING:
679 case SS_LISTENING:
680 if (!skb_queue_empty(&sk->sk_receive_queue))
681 mask |= (POLLIN | POLLRDNORM);
682 break;
683 case SS_DISCONNECTING:
684 mask = (POLLIN | POLLRDNORM | POLLHUP);
685 break;
686 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700687
688 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689}
690
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400691/**
692 * tipc_sendmcast - send multicast message
693 * @sock: socket structure
694 * @seq: destination address
695 * @iov: message data to send
696 * @dsz: total length of message data
697 * @timeo: timeout to wait for wakeup
698 *
699 * Called from function tipc_sendmsg(), which has done all sanity checks
700 * Returns the number of bytes sent on success, or errno
701 */
702static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
703 struct iovec *iov, size_t dsz, long timeo)
704{
705 struct sock *sk = sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400706 struct tipc_msg *mhdr = &tipc_sk(sk)->phdr;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400707 struct sk_buff *buf;
708 uint mtu;
709 int rc;
710
711 msg_set_type(mhdr, TIPC_MCAST_MSG);
712 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
713 msg_set_destport(mhdr, 0);
714 msg_set_destnode(mhdr, 0);
715 msg_set_nametype(mhdr, seq->type);
716 msg_set_namelower(mhdr, seq->lower);
717 msg_set_nameupper(mhdr, seq->upper);
718 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
719
720new_mtu:
721 mtu = tipc_bclink_get_mtu();
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400722 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400723 if (unlikely(rc < 0))
724 return rc;
725
726 do {
727 rc = tipc_bclink_xmit(buf);
728 if (likely(rc >= 0)) {
729 rc = dsz;
730 break;
731 }
732 if (rc == -EMSGSIZE)
733 goto new_mtu;
734 if (rc != -ELINKCONG)
735 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400736 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400737 rc = tipc_wait_for_sndmsg(sock, &timeo);
738 if (rc)
739 kfree_skb_list(buf);
740 } while (!rc);
741 return rc;
742}
743
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400744/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
745 */
746void tipc_sk_mcast_rcv(struct sk_buff *buf)
747{
748 struct tipc_msg *msg = buf_msg(buf);
749 struct tipc_port_list dports = {0, NULL, };
750 struct tipc_port_list *item;
751 struct sk_buff *b;
752 uint i, last, dst = 0;
753 u32 scope = TIPC_CLUSTER_SCOPE;
754
755 if (in_own_node(msg_orignode(msg)))
756 scope = TIPC_NODE_SCOPE;
757
758 /* Create destination port list: */
759 tipc_nametbl_mc_translate(msg_nametype(msg),
760 msg_namelower(msg),
761 msg_nameupper(msg),
762 scope,
763 &dports);
764 last = dports.count;
765 if (!last) {
766 kfree_skb(buf);
767 return;
768 }
769
770 for (item = &dports; item; item = item->next) {
771 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
772 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
773 if (!b) {
774 pr_warn("Failed do clone mcast rcv buffer\n");
775 continue;
776 }
777 msg_set_destport(msg, item->ports[i]);
778 tipc_sk_rcv(b);
779 }
780 }
781 tipc_port_list_free(&dports);
782}
783
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900784/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500785 * tipc_sk_proto_rcv - receive a connection mng protocol message
786 * @tsk: receiving socket
787 * @dnode: node to send response message to, if any
788 * @buf: buffer containing protocol message
789 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
790 * (CONN_PROBE_REPLY) message should be forwarded.
791 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800792static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
793 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500794{
795 struct tipc_msg *msg = buf_msg(buf);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500796 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500797
798 /* Ignore if connection cannot be validated: */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400799 if (!tsk_peer_msg(tsk, msg))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500800 goto exit;
801
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400802 tsk->probing_state = TIPC_CONN_OK;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500803
804 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400805 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500806 tsk->sent_unacked -= msg_msgcnt(msg);
807 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400808 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500809 } else if (msg_type(msg) == CONN_PROBE) {
810 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
811 return TIPC_OK;
812 msg_set_type(msg, CONN_PROBE_REPLY);
813 return TIPC_FWD_MSG;
814 }
815 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
816exit:
817 kfree_skb(buf);
818 return TIPC_OK;
819}
820
821/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 * dest_name_check - verify user is permitted to send to specified port name
823 * @dest: destination address
824 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900825 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 * Prevents restricted configuration commands from being issued by
827 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900828 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 * Returns 0 if permission is granted, otherwise errno
830 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800831static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832{
833 struct tipc_cfg_msg_hdr hdr;
834
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500835 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
836 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900837 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
838 return 0;
839 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
840 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900841 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
842 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843
Allan Stephens3f8dd942011-01-18 13:09:29 -0500844 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
845 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900846 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700848 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900850
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 return 0;
852}
853
Ying Xue3f405042014-01-17 09:50:05 +0800854static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
855{
856 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400857 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800858 DEFINE_WAIT(wait);
859 int done;
860
861 do {
862 int err = sock_error(sk);
863 if (err)
864 return err;
865 if (sock->state == SS_DISCONNECTING)
866 return -EPIPE;
867 if (!*timeo_p)
868 return -EAGAIN;
869 if (signal_pending(current))
870 return sock_intr_errno(*timeo_p);
871
872 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500873 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800874 finish_wait(sk_sleep(sk), &wait);
875 } while (!done);
876 return 0;
877}
878
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500879/**
Ying Xue247f0f32014-02-18 16:06:46 +0800880 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700881 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 * @sock: socket structure
883 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500884 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900885 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900887 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
889 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900890 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 * Returns the number of bytes sent on success, or errno otherwise
892 */
Ying Xue247f0f32014-02-18 16:06:46 +0800893static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500894 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500896 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700897 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400898 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400899 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500900 struct iovec *iov = m->msg_iov;
901 u32 dnode, dport;
902 struct sk_buff *buf;
903 struct tipc_name_seq *seq = &dest->addr.nameseq;
904 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800905 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500906 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907
908 if (unlikely(!dest))
909 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500910
Allan Stephens51f9cc12006-06-25 23:49:06 -0700911 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
912 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500914
915 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400916 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917
Allan Stephens0c3141e2008-04-15 00:22:02 -0700918 if (iocb)
919 lock_sock(sk);
920
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500921 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700922 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500923 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700924 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700925 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700926 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500927 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700928 goto exit;
929 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400930 if (tsk->published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500931 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700932 goto exit;
933 }
934 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400935 tsk->conn_type = dest->addr.name.name.type;
936 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700937 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100938 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500939 rc = dest_name_check(dest, m);
940 if (rc)
941 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942
Ying Xue3f405042014-01-17 09:50:05 +0800943 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500944
945 if (dest->addrtype == TIPC_ADDR_MCAST) {
946 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
947 goto exit;
948 } else if (dest->addrtype == TIPC_ADDR_NAME) {
949 u32 type = dest->addr.name.name.type;
950 u32 inst = dest->addr.name.name.instance;
951 u32 domain = dest->addr.name.domain;
952
953 dnode = domain;
954 msg_set_type(mhdr, TIPC_NAMED_MSG);
955 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
956 msg_set_nametype(mhdr, type);
957 msg_set_nameinst(mhdr, inst);
958 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
959 dport = tipc_nametbl_translate(type, inst, &dnode);
960 msg_set_destnode(mhdr, dnode);
961 msg_set_destport(mhdr, dport);
962 if (unlikely(!dport && !dnode)) {
963 rc = -EHOSTUNREACH;
964 goto exit;
965 }
966 } else if (dest->addrtype == TIPC_ADDR_ID) {
967 dnode = dest->addr.id.node;
968 msg_set_type(mhdr, TIPC_DIRECT_MSG);
969 msg_set_lookup_scope(mhdr, 0);
970 msg_set_destnode(mhdr, dnode);
971 msg_set_destport(mhdr, dest->addr.id.ref);
972 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
973 }
974
975new_mtu:
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400976 mtu = tipc_node_get_mtu(dnode, tsk->ref);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400977 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500978 if (rc < 0)
979 goto exit;
980
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900981 do {
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400982 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400983 rc = tipc_link_xmit(buf, dnode, tsk->ref);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500984 if (likely(rc >= 0)) {
985 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700986 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500987 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700988 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900989 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500990 if (rc == -EMSGSIZE)
991 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500992 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700993 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400994 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500995 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400996 if (rc)
997 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500998 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700999exit:
1000 if (iocb)
1001 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001002
1003 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001004}
1005
Ying Xue391a6dd2014-01-17 09:50:06 +08001006static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
1007{
1008 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001009 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +08001010 DEFINE_WAIT(wait);
1011 int done;
1012
1013 do {
1014 int err = sock_error(sk);
1015 if (err)
1016 return err;
1017 if (sock->state == SS_DISCONNECTING)
1018 return -EPIPE;
1019 else if (sock->state != SS_CONNECTED)
1020 return -ENOTCONN;
1021 if (!*timeo_p)
1022 return -EAGAIN;
1023 if (signal_pending(current))
1024 return sock_intr_errno(*timeo_p);
1025
1026 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1027 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -05001028 (!tsk->link_cong &&
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001029 !tsk_conn_cong(tsk)) ||
1030 !tsk->connected);
Ying Xue391a6dd2014-01-17 09:50:06 +08001031 finish_wait(sk_sleep(sk), &wait);
1032 } while (!done);
1033 return 0;
1034}
1035
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001036/**
Ying Xue247f0f32014-02-18 16:06:46 +08001037 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038 * @iocb: (unused)
1039 * @sock: socket structure
1040 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001041 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001042 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001044 *
1045 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001046 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 */
Ying Xue247f0f32014-02-18 16:06:46 +08001048static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001049 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001051 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001052 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001053 struct tipc_msg *mhdr = &tsk->phdr;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001054 struct sk_buff *buf;
1055 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001056 u32 ref = tsk->ref;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001057 int rc = -EINVAL;
1058 long timeo;
1059 u32 dnode;
1060 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001061
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001062 /* Handle implied connection establishment */
1063 if (unlikely(dest)) {
1064 rc = tipc_sendmsg(iocb, sock, m, dsz);
1065 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -05001066 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001067 return rc;
1068 }
1069 if (dsz > (uint)INT_MAX)
1070 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001071
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001072 if (iocb)
1073 lock_sock(sk);
1074
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001075 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001076 if (sock->state == SS_DISCONNECTING)
1077 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +08001078 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001079 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +08001080 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001081 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001083 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001084 dnode = tsk_peer_node(tsk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001085
1086next:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001087 mtu = tsk->max_pkt;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001088 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001089 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001090 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -07001091 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001092 do {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001093 if (likely(!tsk_conn_cong(tsk))) {
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001094 rc = tipc_link_xmit(buf, dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001095 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001096 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001097 sent += send;
1098 if (sent == dsz)
1099 break;
1100 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -07001101 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001102 if (rc == -EMSGSIZE) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001103 tsk->max_pkt = tipc_node_get_mtu(dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001104 goto next;
1105 }
1106 if (rc != -ELINKCONG)
1107 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001108 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001110 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -04001111 if (rc)
1112 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001113 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001114exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001115 if (iocb)
1116 release_sock(sk);
1117 return sent ? sent : rc;
1118}
1119
1120/**
1121 * tipc_send_packet - send a connection-oriented message
1122 * @iocb: if NULL, indicates that socket lock is already held
1123 * @sock: socket structure
1124 * @m: message to send
1125 * @dsz: length of data to be transmitted
1126 *
1127 * Used for SOCK_SEQPACKET messages.
1128 *
1129 * Returns the number of bytes sent on success, or errno otherwise
1130 */
1131static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1132 struct msghdr *m, size_t dsz)
1133{
1134 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1135 return -EMSGSIZE;
1136
1137 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138}
1139
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001140/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001142static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001143 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144{
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001145 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001147 msg_set_destnode(msg, peer_node);
1148 msg_set_destport(msg, peer_port);
1149 msg_set_type(msg, TIPC_CONN_MSG);
1150 msg_set_lookup_scope(msg, 0);
1151 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001152
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001153 tsk->probing_interval = CONN_PROBING_INTERVAL;
1154 tsk->probing_state = TIPC_CONN_OK;
1155 tsk->connected = 1;
1156 k_start_timer(&tsk->timer, tsk->probing_interval);
1157 tipc_node_add_conn(peer_node, tsk->ref, peer_port);
1158 tsk->max_pkt = tipc_node_get_mtu(peer_node, tsk->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159}
1160
1161/**
1162 * set_orig_addr - capture sender's address for received message
1163 * @m: descriptor for message info
1164 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001165 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 * Note: Address is not captured if not requested by receiver.
1167 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001168static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001170 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001172 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 addr->family = AF_TIPC;
1174 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001175 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 addr->addr.id.ref = msg_origport(msg);
1177 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001178 addr->addr.name.domain = 0; /* could leave uninitialized */
1179 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 m->msg_namelen = sizeof(struct sockaddr_tipc);
1181 }
1182}
1183
1184/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001185 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 * @m: descriptor for message info
1187 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001188 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001189 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001191 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 * Returns 0 if successful, otherwise errno
1193 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001194static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1195 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196{
1197 u32 anc_data[3];
1198 u32 err;
1199 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001200 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 int res;
1202
1203 if (likely(m->msg_controllen == 0))
1204 return 0;
1205
1206 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 err = msg ? msg_errcode(msg) : 0;
1208 if (unlikely(err)) {
1209 anc_data[0] = err;
1210 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001211 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1212 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001214 if (anc_data[1]) {
1215 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1216 msg_data(msg));
1217 if (res)
1218 return res;
1219 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 }
1221
1222 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1224 switch (dest_type) {
1225 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001226 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001227 anc_data[0] = msg_nametype(msg);
1228 anc_data[1] = msg_namelower(msg);
1229 anc_data[2] = msg_namelower(msg);
1230 break;
1231 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001232 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 anc_data[0] = msg_nametype(msg);
1234 anc_data[1] = msg_namelower(msg);
1235 anc_data[2] = msg_nameupper(msg);
1236 break;
1237 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001238 has_name = (tsk->conn_type != 0);
1239 anc_data[0] = tsk->conn_type;
1240 anc_data[1] = tsk->conn_instance;
1241 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 break;
1243 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001244 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245 }
Allan Stephens2db99832010-12-31 18:59:33 +00001246 if (has_name) {
1247 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1248 if (res)
1249 return res;
1250 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001251
1252 return 0;
1253}
1254
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001255static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001256{
1257 struct sk_buff *buf = NULL;
1258 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001259 u32 peer_port = tsk_peer_port(tsk);
1260 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001261
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001262 if (!tsk->connected)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001263 return;
1264 buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001265 tipc_own_addr, peer_port, tsk->ref, TIPC_OK);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001266 if (!buf)
1267 return;
1268 msg = buf_msg(buf);
1269 msg_set_msgcnt(msg, ack);
1270 tipc_link_xmit(buf, dnode, msg_link_selector(msg));
1271}
1272
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001273static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001274{
1275 struct sock *sk = sock->sk;
1276 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001277 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001278 int err;
1279
1280 for (;;) {
1281 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001282 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001283 if (sock->state == SS_DISCONNECTING) {
1284 err = -ENOTCONN;
1285 break;
1286 }
1287 release_sock(sk);
1288 timeo = schedule_timeout(timeo);
1289 lock_sock(sk);
1290 }
1291 err = 0;
1292 if (!skb_queue_empty(&sk->sk_receive_queue))
1293 break;
1294 err = sock_intr_errno(timeo);
1295 if (signal_pending(current))
1296 break;
1297 err = -EAGAIN;
1298 if (!timeo)
1299 break;
1300 }
1301 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001302 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001303 return err;
1304}
1305
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001306/**
Ying Xue247f0f32014-02-18 16:06:46 +08001307 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 * @iocb: (unused)
1309 * @m: descriptor for message info
1310 * @buf_len: total size of user buffer area
1311 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001312 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1314 * If the complete message doesn't fit in user area, truncate it.
1315 *
1316 * Returns size of returned message data, errno otherwise
1317 */
Ying Xue247f0f32014-02-18 16:06:46 +08001318static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1319 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001321 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001322 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 struct sk_buff *buf;
1324 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001325 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 unsigned int sz;
1327 u32 err;
1328 int res;
1329
Allan Stephens0c3141e2008-04-15 00:22:02 -07001330 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331 if (unlikely(!buf_len))
1332 return -EINVAL;
1333
Allan Stephens0c3141e2008-04-15 00:22:02 -07001334 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335
Allan Stephens0c3141e2008-04-15 00:22:02 -07001336 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 res = -ENOTCONN;
1338 goto exit;
1339 }
1340
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001341 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001342restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343
Allan Stephens0c3141e2008-04-15 00:22:02 -07001344 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001345 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001346 if (res)
1347 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001348
1349 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001350 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 msg = buf_msg(buf);
1352 sz = msg_data_sz(msg);
1353 err = msg_errcode(msg);
1354
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001357 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 goto restart;
1359 }
1360
1361 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 set_orig_addr(m, msg);
1363
1364 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001365 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001366 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 goto exit;
1368
1369 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 if (!err) {
1371 if (unlikely(buf_len < sz)) {
1372 sz = buf_len;
1373 m->msg_flags |= MSG_TRUNC;
1374 }
David S. Miller51f3d022014-11-05 16:46:40 -05001375 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001376 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 res = sz;
1379 } else {
1380 if ((sock->state == SS_READY) ||
1381 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1382 res = 0;
1383 else
1384 res = -ECONNRESET;
1385 }
1386
1387 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001389 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001390 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001391 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001392 tsk->rcv_unacked = 0;
1393 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001394 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001395 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001397 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 return res;
1399}
1400
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001401/**
Ying Xue247f0f32014-02-18 16:06:46 +08001402 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 * @iocb: (unused)
1404 * @m: descriptor for message info
1405 * @buf_len: total size of user buffer area
1406 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001407 *
1408 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409 * will optionally wait for more; never truncates data.
1410 *
1411 * Returns size of returned message data, errno otherwise
1412 */
Ying Xue247f0f32014-02-18 16:06:46 +08001413static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1414 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001416 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001417 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 struct sk_buff *buf;
1419 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001420 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001422 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001425 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426
1427 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428 if (unlikely(!buf_len))
1429 return -EINVAL;
1430
Allan Stephens0c3141e2008-04-15 00:22:02 -07001431 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001433 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434 res = -ENOTCONN;
1435 goto exit;
1436 }
1437
Florian Westphal3720d402010-08-17 11:00:04 +00001438 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001439 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001440
Allan Stephens0c3141e2008-04-15 00:22:02 -07001441restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001442 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001443 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001444 if (res)
1445 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001446
1447 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001448 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 msg = buf_msg(buf);
1450 sz = msg_data_sz(msg);
1451 err = msg_errcode(msg);
1452
1453 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001455 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456 goto restart;
1457 }
1458
1459 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460 if (sz_copied == 0) {
1461 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001462 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001463 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001464 goto exit;
1465 }
1466
1467 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001469 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470
Allan Stephens0232fd02011-02-21 09:45:40 -05001471 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001472 needed = (buf_len - sz_copied);
1473 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001474
David S. Miller51f3d022014-11-05 16:46:40 -05001475 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1476 m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001477 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001479
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480 sz_copied += sz_to_copy;
1481
1482 if (sz_to_copy < sz) {
1483 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001484 TIPC_SKB_CB(buf)->handle =
1485 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486 goto exit;
1487 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 } else {
1489 if (sz_copied != 0)
1490 goto exit; /* can't add error msg to valid data */
1491
1492 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1493 res = 0;
1494 else
1495 res = -ECONNRESET;
1496 }
1497
1498 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001500 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001501 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
Jon Paul Maloy60120522014-06-25 20:41:42 -05001502 tsk->rcv_unacked = 0;
1503 }
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001504 tsk_advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001505 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506
1507 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001508 if ((sz_copied < buf_len) && /* didn't get all requested data */
1509 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001510 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001511 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1512 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513 goto restart;
1514
1515exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001516 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001517 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518}
1519
1520/**
Ying Xuef288bef2012-08-21 11:16:57 +08001521 * tipc_write_space - wake up thread if port congestion is released
1522 * @sk: socket
1523 */
1524static void tipc_write_space(struct sock *sk)
1525{
1526 struct socket_wq *wq;
1527
1528 rcu_read_lock();
1529 wq = rcu_dereference(sk->sk_wq);
1530 if (wq_has_sleeper(wq))
1531 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1532 POLLWRNORM | POLLWRBAND);
1533 rcu_read_unlock();
1534}
1535
1536/**
1537 * tipc_data_ready - wake up threads to indicate messages have been received
1538 * @sk: socket
1539 * @len: the length of messages
1540 */
David S. Miller676d2362014-04-11 16:15:36 -04001541static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001542{
1543 struct socket_wq *wq;
1544
1545 rcu_read_lock();
1546 wq = rcu_dereference(sk->sk_wq);
1547 if (wq_has_sleeper(wq))
1548 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1549 POLLRDNORM | POLLRDBAND);
1550 rcu_read_unlock();
1551}
1552
1553/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001554 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001555 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001556 * @msg: message
1557 *
stephen hemmingerb2ad5e52014-10-29 22:58:51 -07001558 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001559 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001560static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001561{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001562 struct sock *sk = &tsk->sk;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001563 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001564 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001565 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001566
1567 if (msg_mcast(msg))
1568 return retval;
1569
1570 switch ((int)sock->state) {
1571 case SS_CONNECTED:
1572 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001573 if (tsk_peer_msg(tsk, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001574 if (unlikely(msg_errcode(msg))) {
1575 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001576 tsk->connected = 0;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001577 /* let timer expire on it's own */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001578 tipc_node_remove_conn(tsk_peer_node(tsk),
1579 tsk->ref);
Ying Xue7e6c1312012-11-29 18:39:14 -05001580 }
1581 retval = TIPC_OK;
1582 }
1583 break;
1584 case SS_CONNECTING:
1585 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001586
1587 if (unlikely(!msg_connected(msg)))
1588 break;
1589
Ying Xue584d24b2012-11-29 18:51:19 -05001590 if (unlikely(msg_errcode(msg))) {
1591 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001592 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001593 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001594 break;
1595 }
1596
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001597 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001598 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001599 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001600 retval = TIPC_OK;
1601 break;
1602 }
1603
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001604 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1605 msg_set_importance(&tsk->phdr, msg_importance(msg));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001606 sock->state = SS_CONNECTED;
1607
Ying Xue584d24b2012-11-29 18:51:19 -05001608 /* If an incoming message is an 'ACK-', it should be
1609 * discarded here because it doesn't contain useful
1610 * data. In addition, we should try to wake up
1611 * connect() routine if sleeping.
1612 */
1613 if (msg_data_sz(msg) == 0) {
1614 kfree_skb(*buf);
1615 *buf = NULL;
1616 if (waitqueue_active(sk_sleep(sk)))
1617 wake_up_interruptible(sk_sleep(sk));
1618 }
1619 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001620 break;
1621 case SS_LISTENING:
1622 case SS_UNCONNECTED:
1623 /* Accept only SYN message */
1624 if (!msg_connected(msg) && !(msg_errcode(msg)))
1625 retval = TIPC_OK;
1626 break;
1627 case SS_DISCONNECTING:
1628 break;
1629 default:
1630 pr_err("Unknown socket state %u\n", sock->state);
1631 }
1632 return retval;
1633}
1634
1635/**
Ying Xueaba79f32013-01-20 23:30:09 +01001636 * rcvbuf_limit - get proper overload limit of socket receive queue
1637 * @sk: socket
1638 * @buf: message
1639 *
1640 * For all connection oriented messages, irrespective of importance,
1641 * the default overload value (i.e. 67MB) is set as limit.
1642 *
1643 * For all connectionless messages, by default new queue limits are
1644 * as belows:
1645 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001646 * TIPC_LOW_IMPORTANCE (4 MB)
1647 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1648 * TIPC_HIGH_IMPORTANCE (16 MB)
1649 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001650 *
1651 * Returns overload limit according to corresponding message importance
1652 */
1653static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1654{
1655 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001656
1657 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001658 return sysctl_tipc_rmem[2];
1659
1660 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1661 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001662}
1663
1664/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001665 * filter_rcv - validate incoming message
1666 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001668 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001669 * Enqueues message on receive queue if acceptable; optionally handles
1670 * disconnect indication for a connected socket.
1671 *
1672 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001673 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001674 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001675 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001677static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001679 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001680 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001682 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001683 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001684 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001686 if (unlikely(msg_user(msg) == CONN_MANAGER))
1687 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001688
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001689 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1690 kfree_skb(buf);
1691 tsk->link_cong = 0;
1692 sk->sk_write_space(sk);
1693 return TIPC_OK;
1694 }
1695
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001697 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001698 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001699
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001701 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001702 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001704 rc = filter_connect(tsk, &buf);
1705 if (rc != TIPC_OK || buf == NULL)
1706 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 }
1708
1709 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001710 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001711 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712
Ying Xueaba79f32013-01-20 23:30:09 +01001713 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001714 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001715 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001716 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001717
David S. Miller676d2362014-04-11 16:15:36 -04001718 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 return TIPC_OK;
1720}
1721
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001722/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001723 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001724 * @sk: socket
1725 * @buf: message
1726 *
1727 * Caller must hold socket lock, but not port lock.
1728 *
1729 * Returns 0
1730 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001731static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001732{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001733 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001734 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001735 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001736 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001737
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001738 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001739
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001740 if (likely(!rc)) {
1741 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1742 atomic_add(truesize, &tsk->dupl_rcvcnt);
1743 return 0;
1744 }
1745
1746 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1747 return 0;
1748
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001749 tipc_link_xmit(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001750
Allan Stephens0c3141e2008-04-15 00:22:02 -07001751 return 0;
1752}
1753
1754/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001755 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001756 * @buf: buffer containing arriving message
1757 * Consumes buffer
1758 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001759 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001760int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001761{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001762 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001763 struct sock *sk;
1764 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001765 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001766 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001767 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001768
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001769 /* Validate destination and message */
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001770 tsk = tipc_sk_get(dport);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04001771 if (unlikely(!tsk)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001772 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001773 goto exit;
1774 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001775 sk = &tsk->sk;
1776
1777 /* Queue message */
Ying Xue1a194c22014-10-20 14:46:35 +08001778 spin_lock_bh(&sk->sk_lock.slock);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001779
Allan Stephens0c3141e2008-04-15 00:22:02 -07001780 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001781 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001782 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001783 if (sk->sk_backlog.len == 0)
1784 atomic_set(&tsk->dupl_rcvcnt, 0);
1785 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1786 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001787 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001788 }
Ying Xue1a194c22014-10-20 14:46:35 +08001789 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04001790 tipc_sk_put(tsk);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001791 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001792 return 0;
1793exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001794 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001795 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001796
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001797 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001798 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001799}
1800
Ying Xue78eb3a52014-01-17 09:50:03 +08001801static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1802{
1803 struct sock *sk = sock->sk;
1804 DEFINE_WAIT(wait);
1805 int done;
1806
1807 do {
1808 int err = sock_error(sk);
1809 if (err)
1810 return err;
1811 if (!*timeo_p)
1812 return -ETIMEDOUT;
1813 if (signal_pending(current))
1814 return sock_intr_errno(*timeo_p);
1815
1816 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1817 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1818 finish_wait(sk_sleep(sk), &wait);
1819 } while (!done);
1820 return 0;
1821}
1822
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823/**
Ying Xue247f0f32014-02-18 16:06:46 +08001824 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825 * @sock: socket structure
1826 * @dest: socket address for destination port
1827 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001828 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 *
1830 * Returns 0 on success, errno otherwise
1831 */
Ying Xue247f0f32014-02-18 16:06:46 +08001832static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1833 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001835 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001836 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1837 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001838 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1839 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001840 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841
Allan Stephens0c3141e2008-04-15 00:22:02 -07001842 lock_sock(sk);
1843
Allan Stephensb89741a2008-04-15 00:20:37 -07001844 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001845 if (sock->state == SS_READY) {
1846 res = -EOPNOTSUPP;
1847 goto exit;
1848 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001849
Allan Stephensb89741a2008-04-15 00:20:37 -07001850 /*
1851 * Reject connection attempt using multicast address
1852 *
1853 * Note: send_msg() validates the rest of the address fields,
1854 * so there's no need to do it here
1855 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001856 if (dst->addrtype == TIPC_ADDR_MCAST) {
1857 res = -EINVAL;
1858 goto exit;
1859 }
1860
Ying Xue78eb3a52014-01-17 09:50:03 +08001861 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001862 switch (sock->state) {
1863 case SS_UNCONNECTED:
1864 /* Send a 'SYN-' to destination */
1865 m.msg_name = dest;
1866 m.msg_namelen = destlen;
1867
1868 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1869 * indicate send_msg() is never blocked.
1870 */
1871 if (!timeout)
1872 m.msg_flags = MSG_DONTWAIT;
1873
Ying Xue247f0f32014-02-18 16:06:46 +08001874 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001875 if ((res < 0) && (res != -EWOULDBLOCK))
1876 goto exit;
1877
1878 /* Just entered SS_CONNECTING state; the only
1879 * difference is that return value in non-blocking
1880 * case is EINPROGRESS, rather than EALREADY.
1881 */
1882 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001883 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001884 if (previous == SS_CONNECTING)
1885 res = -EALREADY;
1886 if (!timeout)
1887 goto exit;
1888 timeout = msecs_to_jiffies(timeout);
1889 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1890 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001891 break;
1892 case SS_CONNECTED:
1893 res = -EISCONN;
1894 break;
1895 default:
1896 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001897 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001898 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001899exit:
1900 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001901 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902}
1903
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001904/**
Ying Xue247f0f32014-02-18 16:06:46 +08001905 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906 * @sock: socket structure
1907 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001908 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909 * Returns 0 on success, errno otherwise
1910 */
Ying Xue247f0f32014-02-18 16:06:46 +08001911static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001913 struct sock *sk = sock->sk;
1914 int res;
1915
1916 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917
Ying Xue245f3d32011-07-06 06:01:13 -04001918 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001919 res = -EINVAL;
1920 else {
1921 sock->state = SS_LISTENING;
1922 res = 0;
1923 }
1924
1925 release_sock(sk);
1926 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927}
1928
Ying Xue6398e232014-01-17 09:50:04 +08001929static int tipc_wait_for_accept(struct socket *sock, long timeo)
1930{
1931 struct sock *sk = sock->sk;
1932 DEFINE_WAIT(wait);
1933 int err;
1934
1935 /* True wake-one mechanism for incoming connections: only
1936 * one process gets woken up, not the 'whole herd'.
1937 * Since we do not 'race & poll' for established sockets
1938 * anymore, the common case will execute the loop only once.
1939 */
1940 for (;;) {
1941 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1942 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001943 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001944 release_sock(sk);
1945 timeo = schedule_timeout(timeo);
1946 lock_sock(sk);
1947 }
1948 err = 0;
1949 if (!skb_queue_empty(&sk->sk_receive_queue))
1950 break;
1951 err = -EINVAL;
1952 if (sock->state != SS_LISTENING)
1953 break;
1954 err = sock_intr_errno(timeo);
1955 if (signal_pending(current))
1956 break;
1957 err = -EAGAIN;
1958 if (!timeo)
1959 break;
1960 }
1961 finish_wait(sk_sleep(sk), &wait);
1962 return err;
1963}
1964
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001965/**
Ying Xue247f0f32014-02-18 16:06:46 +08001966 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001967 * @sock: listening socket
1968 * @newsock: new socket that is to be connected
1969 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001970 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001971 * Returns 0 on success, errno otherwise
1972 */
Ying Xue247f0f32014-02-18 16:06:46 +08001973static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001974{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001975 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001976 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001977 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001978 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001979 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001980 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981
Allan Stephens0c3141e2008-04-15 00:22:02 -07001982 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001983
Allan Stephens0c3141e2008-04-15 00:22:02 -07001984 if (sock->state != SS_LISTENING) {
1985 res = -EINVAL;
1986 goto exit;
1987 }
Ying Xue6398e232014-01-17 09:50:04 +08001988 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1989 res = tipc_wait_for_accept(sock, timeo);
1990 if (res)
1991 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001992
1993 buf = skb_peek(&sk->sk_receive_queue);
1994
Ying Xuec5fa7b32013-06-17 10:54:39 -04001995 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001996 if (res)
1997 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001998
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001999 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002000 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002001 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002002
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002003 /* we lock on new_sk; but lockdep sees the lock on sk */
2004 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002005
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002006 /*
2007 * Reject any stray messages received by new socket
2008 * before the socket lock was taken (very, very unlikely)
2009 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002010 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002012 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002013 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002014 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002015
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002016 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002017 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002018 new_tsock->conn_type = msg_nametype(msg);
2019 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002020 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002021
2022 /*
2023 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2024 * Respond to 'SYN+' by queuing it on new socket.
2025 */
2026 if (!msg_data_sz(msg)) {
2027 struct msghdr m = {NULL,};
2028
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002029 tsk_advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08002030 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002031 } else {
2032 __skb_dequeue(&sk->sk_receive_queue);
2033 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002034 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002035 }
2036 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002037exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002038 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039 return res;
2040}
2041
2042/**
Ying Xue247f0f32014-02-18 16:06:46 +08002043 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002044 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002045 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002046 *
2047 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002048 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002049 * Returns 0 on success, errno otherwise
2050 */
Ying Xue247f0f32014-02-18 16:06:46 +08002051static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002052{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002053 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002054 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002055 struct sk_buff *buf;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002056 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002057 int res;
2058
Allan Stephense247a8f2008-03-06 15:05:38 -08002059 if (how != SHUT_RDWR)
2060 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002061
Allan Stephens0c3141e2008-04-15 00:22:02 -07002062 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002063
2064 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002065 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01002066 case SS_CONNECTED:
2067
Per Lidenb97bf3f2006-01-02 19:04:38 +01002068restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002069 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07002070 buf = __skb_dequeue(&sk->sk_receive_queue);
2071 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02002072 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04002073 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002074 goto restart;
2075 }
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002076 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002077 tipc_link_xmit(buf, dnode, tsk->ref);
2078 tipc_node_remove_conn(dnode, tsk->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002079 } else {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002080 dnode = tsk_peer_node(tsk);
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04002081 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
2082 TIPC_CONN_MSG, SHORT_H_SIZE,
2083 0, dnode, tipc_own_addr,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002084 tsk_peer_port(tsk),
2085 tsk->ref, TIPC_CONN_SHUTDOWN);
2086 tipc_link_xmit(buf, dnode, tsk->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002087 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002088 tsk->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002089 sock->state = SS_DISCONNECTING;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002090 tipc_node_remove_conn(dnode, tsk->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002091 /* fall through */
2092
2093 case SS_DISCONNECTING:
2094
Ying Xue75031152012-10-29 09:38:15 -04002095 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002096 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002097
2098 /* Wake up anyone sleeping in poll */
2099 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100 res = 0;
2101 break;
2102
2103 default:
2104 res = -ENOTCONN;
2105 }
2106
Allan Stephens0c3141e2008-04-15 00:22:02 -07002107 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108 return res;
2109}
2110
Jon Paul Maloy57289012014-08-22 18:09:09 -04002111static void tipc_sk_timeout(unsigned long ref)
2112{
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002113 struct tipc_sock *tsk;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002114 struct sock *sk;
2115 struct sk_buff *buf = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002116 u32 peer_port, peer_node;
2117
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002118 tsk = tipc_sk_get(ref);
Jon Paul Maloy9b50fd02014-08-22 18:09:15 -04002119 if (!tsk)
Ying Xuecc086fc2014-08-28 10:02:41 +08002120 return;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002121
Ying Xuecc086fc2014-08-28 10:02:41 +08002122 sk = &tsk->sk;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002123 bh_lock_sock(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002124 if (!tsk->connected) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002125 bh_unlock_sock(sk);
2126 goto exit;
2127 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002128 peer_port = tsk_peer_port(tsk);
2129 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002130
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002131 if (tsk->probing_state == TIPC_CONN_PROBING) {
Jon Paul Maloy57289012014-08-22 18:09:09 -04002132 /* Previous probe not answered -> self abort */
2133 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2134 SHORT_H_SIZE, 0, tipc_own_addr,
2135 peer_node, ref, peer_port,
2136 TIPC_ERR_NO_PORT);
2137 } else {
2138 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2139 0, peer_node, tipc_own_addr,
2140 peer_port, ref, TIPC_OK);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002141 tsk->probing_state = TIPC_CONN_PROBING;
2142 k_start_timer(&tsk->timer, tsk->probing_interval);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002143 }
2144 bh_unlock_sock(sk);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002145 if (buf)
2146 tipc_link_xmit(buf, peer_node, ref);
2147exit:
2148 tipc_sk_put(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002149}
2150
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002151static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002152 struct tipc_name_seq const *seq)
2153{
2154 struct publication *publ;
2155 u32 key;
2156
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002157 if (tsk->connected)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002158 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002159 key = tsk->ref + tsk->pub_count + 1;
2160 if (key == tsk->ref)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002161 return -EADDRINUSE;
2162
2163 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002164 scope, tsk->ref, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002165 if (unlikely(!publ))
2166 return -EINVAL;
2167
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002168 list_add(&publ->pport_list, &tsk->publications);
2169 tsk->pub_count++;
2170 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002171 return 0;
2172}
2173
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002174static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002175 struct tipc_name_seq const *seq)
2176{
2177 struct publication *publ;
2178 struct publication *safe;
2179 int rc = -EINVAL;
2180
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002181 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002182 if (seq) {
2183 if (publ->scope != scope)
2184 continue;
2185 if (publ->type != seq->type)
2186 continue;
2187 if (publ->lower != seq->lower)
2188 continue;
2189 if (publ->upper != seq->upper)
2190 break;
2191 tipc_nametbl_withdraw(publ->type, publ->lower,
2192 publ->ref, publ->key);
2193 rc = 0;
2194 break;
2195 }
2196 tipc_nametbl_withdraw(publ->type, publ->lower,
2197 publ->ref, publ->key);
2198 rc = 0;
2199 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002200 if (list_empty(&tsk->publications))
2201 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002202 return rc;
2203}
2204
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002205static int tipc_sk_show(struct tipc_sock *tsk, char *buf,
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002206 int len, int full_id)
2207{
2208 struct publication *publ;
2209 int ret;
2210
2211 if (full_id)
2212 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2213 tipc_zone(tipc_own_addr),
2214 tipc_cluster(tipc_own_addr),
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002215 tipc_node(tipc_own_addr), tsk->ref);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002216 else
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002217 ret = tipc_snprintf(buf, len, "%-10u:", tsk->ref);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002218
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002219 if (tsk->connected) {
2220 u32 dport = tsk_peer_port(tsk);
2221 u32 destnode = tsk_peer_node(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002222
2223 ret += tipc_snprintf(buf + ret, len - ret,
2224 " connected to <%u.%u.%u:%u>",
2225 tipc_zone(destnode),
2226 tipc_cluster(destnode),
2227 tipc_node(destnode), dport);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002228 if (tsk->conn_type != 0)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002229 ret += tipc_snprintf(buf + ret, len - ret,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002230 " via {%u,%u}", tsk->conn_type,
2231 tsk->conn_instance);
2232 } else if (tsk->published) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002233 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002234 list_for_each_entry(publ, &tsk->publications, pport_list) {
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002235 if (publ->lower == publ->upper)
2236 ret += tipc_snprintf(buf + ret, len - ret,
2237 " {%u,%u}", publ->type,
2238 publ->lower);
2239 else
2240 ret += tipc_snprintf(buf + ret, len - ret,
2241 " {%u,%u,%u}", publ->type,
2242 publ->lower, publ->upper);
2243 }
2244 }
2245 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2246 return ret;
2247}
2248
2249struct sk_buff *tipc_sk_socks_show(void)
2250{
2251 struct sk_buff *buf;
2252 struct tlv_desc *rep_tlv;
2253 char *pb;
2254 int pb_len;
2255 struct tipc_sock *tsk;
2256 int str_len = 0;
2257 u32 ref = 0;
2258
2259 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2260 if (!buf)
2261 return NULL;
2262 rep_tlv = (struct tlv_desc *)buf->data;
2263 pb = TLV_DATA(rep_tlv);
2264 pb_len = ULTRA_STRING_MAX_LEN;
2265
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002266 tsk = tipc_sk_get_next(&ref);
2267 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2268 lock_sock(&tsk->sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002269 str_len += tipc_sk_show(tsk, pb + str_len,
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002270 pb_len - str_len, 0);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002271 release_sock(&tsk->sk);
2272 tipc_sk_put(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002273 }
2274 str_len += 1; /* for "\0" */
2275 skb_put(buf, TLV_SPACE(str_len));
2276 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2277
2278 return buf;
2279}
2280
2281/* tipc_sk_reinit: set non-zero address in all existing sockets
2282 * when we go from standalone to network mode.
2283 */
2284void tipc_sk_reinit(void)
2285{
2286 struct tipc_msg *msg;
2287 u32 ref = 0;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002288 struct tipc_sock *tsk = tipc_sk_get_next(&ref);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002289
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002290 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2291 lock_sock(&tsk->sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002292 msg = &tsk->phdr;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002293 msg_set_prevnode(msg, tipc_own_addr);
2294 msg_set_orignode(msg, tipc_own_addr);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002295 release_sock(&tsk->sk);
2296 tipc_sk_put(tsk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002297 }
2298}
2299
Per Lidenb97bf3f2006-01-02 19:04:38 +01002300/**
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002301 * struct reference - TIPC socket reference entry
2302 * @tsk: pointer to socket associated with reference entry
2303 * @ref: reference value for socket (combines instance & array index info)
2304 */
2305struct reference {
2306 struct tipc_sock *tsk;
2307 u32 ref;
2308};
2309
2310/**
2311 * struct tipc_ref_table - table of TIPC socket reference entries
2312 * @entries: pointer to array of reference entries
2313 * @capacity: array index of first unusable entry
2314 * @init_point: array index of first uninitialized entry
2315 * @first_free: array index of first unused socket reference entry
2316 * @last_free: array index of last unused socket reference entry
2317 * @index_mask: bitmask for array index portion of reference values
2318 * @start_mask: initial value for instance value portion of reference values
2319 */
2320struct ref_table {
2321 struct reference *entries;
2322 u32 capacity;
2323 u32 init_point;
2324 u32 first_free;
2325 u32 last_free;
2326 u32 index_mask;
2327 u32 start_mask;
2328};
2329
2330/* Socket reference table consists of 2**N entries.
2331 *
2332 * State Socket ptr Reference
2333 * ----- ---------- ---------
2334 * In use non-NULL XXXX|own index
2335 * (XXXX changes each time entry is acquired)
2336 * Free NULL YYYY|next free index
2337 * (YYYY is one more than last used XXXX)
2338 * Uninitialized NULL 0
2339 *
2340 * Entry 0 is not used; this allows index 0 to denote the end of the free list.
2341 *
2342 * Note that a reference value of 0 does not necessarily indicate that an
2343 * entry is uninitialized, since the last entry in the free list could also
2344 * have a reference value of 0 (although this is unlikely).
2345 */
2346
2347static struct ref_table tipc_ref_table;
2348
2349static DEFINE_RWLOCK(ref_table_lock);
2350
2351/**
2352 * tipc_ref_table_init - create reference table for sockets
2353 */
2354int tipc_sk_ref_table_init(u32 req_sz, u32 start)
2355{
2356 struct reference *table;
2357 u32 actual_sz;
2358
2359 /* account for unused entry, then round up size to a power of 2 */
2360
2361 req_sz++;
2362 for (actual_sz = 16; actual_sz < req_sz; actual_sz <<= 1) {
2363 /* do nothing */
2364 };
2365
2366 /* allocate table & mark all entries as uninitialized */
2367 table = vzalloc(actual_sz * sizeof(struct reference));
2368 if (table == NULL)
2369 return -ENOMEM;
2370
2371 tipc_ref_table.entries = table;
2372 tipc_ref_table.capacity = req_sz;
2373 tipc_ref_table.init_point = 1;
2374 tipc_ref_table.first_free = 0;
2375 tipc_ref_table.last_free = 0;
2376 tipc_ref_table.index_mask = actual_sz - 1;
2377 tipc_ref_table.start_mask = start & ~tipc_ref_table.index_mask;
2378
2379 return 0;
2380}
2381
2382/**
2383 * tipc_ref_table_stop - destroy reference table for sockets
2384 */
2385void tipc_sk_ref_table_stop(void)
2386{
2387 if (!tipc_ref_table.entries)
2388 return;
2389 vfree(tipc_ref_table.entries);
2390 tipc_ref_table.entries = NULL;
2391}
2392
2393/* tipc_ref_acquire - create reference to a socket
2394 *
2395 * Register an socket pointer in the reference table.
2396 * Returns a unique reference value that is used from then on to retrieve the
2397 * socket pointer, or to determine if the socket has been deregistered.
2398 */
2399u32 tipc_sk_ref_acquire(struct tipc_sock *tsk)
2400{
2401 u32 index;
2402 u32 index_mask;
2403 u32 next_plus_upper;
2404 u32 ref = 0;
2405 struct reference *entry;
2406
2407 if (unlikely(!tsk)) {
2408 pr_err("Attempt to acquire ref. to non-existent obj\n");
2409 return 0;
2410 }
2411 if (unlikely(!tipc_ref_table.entries)) {
2412 pr_err("Ref. table not found in acquisition attempt\n");
2413 return 0;
2414 }
2415
2416 /* Take a free entry, if available; otherwise initialize a new one */
2417 write_lock_bh(&ref_table_lock);
2418 index = tipc_ref_table.first_free;
2419 entry = &tipc_ref_table.entries[index];
2420
2421 if (likely(index)) {
2422 index = tipc_ref_table.first_free;
2423 entry = &tipc_ref_table.entries[index];
2424 index_mask = tipc_ref_table.index_mask;
2425 next_plus_upper = entry->ref;
2426 tipc_ref_table.first_free = next_plus_upper & index_mask;
2427 ref = (next_plus_upper & ~index_mask) + index;
2428 entry->tsk = tsk;
2429 } else if (tipc_ref_table.init_point < tipc_ref_table.capacity) {
2430 index = tipc_ref_table.init_point++;
2431 entry = &tipc_ref_table.entries[index];
2432 ref = tipc_ref_table.start_mask + index;
2433 }
2434
2435 if (ref) {
2436 entry->ref = ref;
2437 entry->tsk = tsk;
2438 }
2439 write_unlock_bh(&ref_table_lock);
2440 return ref;
2441}
2442
2443/* tipc_sk_ref_discard - invalidate reference to an socket
2444 *
2445 * Disallow future references to an socket and free up the entry for re-use.
2446 */
2447void tipc_sk_ref_discard(u32 ref)
2448{
2449 struct reference *entry;
2450 u32 index;
2451 u32 index_mask;
2452
2453 if (unlikely(!tipc_ref_table.entries)) {
2454 pr_err("Ref. table not found during discard attempt\n");
2455 return;
2456 }
2457
2458 index_mask = tipc_ref_table.index_mask;
2459 index = ref & index_mask;
2460 entry = &tipc_ref_table.entries[index];
2461
2462 write_lock_bh(&ref_table_lock);
2463
2464 if (unlikely(!entry->tsk)) {
2465 pr_err("Attempt to discard ref. to non-existent socket\n");
2466 goto exit;
2467 }
2468 if (unlikely(entry->ref != ref)) {
2469 pr_err("Attempt to discard non-existent reference\n");
2470 goto exit;
2471 }
2472
2473 /* Mark entry as unused; increment instance part of entry's
2474 * reference to invalidate any subsequent references
2475 */
2476
2477 entry->tsk = NULL;
2478 entry->ref = (ref & ~index_mask) + (index_mask + 1);
2479
2480 /* Append entry to free entry list */
2481 if (unlikely(tipc_ref_table.first_free == 0))
2482 tipc_ref_table.first_free = index;
2483 else
2484 tipc_ref_table.entries[tipc_ref_table.last_free].ref |= index;
2485 tipc_ref_table.last_free = index;
2486exit:
2487 write_unlock_bh(&ref_table_lock);
2488}
2489
2490/* tipc_sk_get - find referenced socket and return pointer to it
2491 */
2492struct tipc_sock *tipc_sk_get(u32 ref)
2493{
2494 struct reference *entry;
2495 struct tipc_sock *tsk;
2496
2497 if (unlikely(!tipc_ref_table.entries))
2498 return NULL;
2499 read_lock_bh(&ref_table_lock);
2500 entry = &tipc_ref_table.entries[ref & tipc_ref_table.index_mask];
2501 tsk = entry->tsk;
2502 if (likely(tsk && (entry->ref == ref)))
2503 sock_hold(&tsk->sk);
2504 else
2505 tsk = NULL;
2506 read_unlock_bh(&ref_table_lock);
2507 return tsk;
2508}
2509
2510/* tipc_sk_get_next - lock & return next socket after referenced one
2511*/
2512struct tipc_sock *tipc_sk_get_next(u32 *ref)
2513{
2514 struct reference *entry;
2515 struct tipc_sock *tsk = NULL;
2516 uint index = *ref & tipc_ref_table.index_mask;
2517
2518 read_lock_bh(&ref_table_lock);
2519 while (++index < tipc_ref_table.capacity) {
2520 entry = &tipc_ref_table.entries[index];
2521 if (!entry->tsk)
2522 continue;
2523 tsk = entry->tsk;
2524 sock_hold(&tsk->sk);
2525 *ref = entry->ref;
2526 break;
2527 }
2528 read_unlock_bh(&ref_table_lock);
2529 return tsk;
2530}
2531
2532static void tipc_sk_put(struct tipc_sock *tsk)
2533{
2534 sock_put(&tsk->sk);
2535}
2536
2537/**
Ying Xue247f0f32014-02-18 16:06:46 +08002538 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002539 * @sock: socket structure
2540 * @lvl: option level
2541 * @opt: option identifier
2542 * @ov: pointer to new option value
2543 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002544 *
2545 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002546 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002547 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002548 * Returns 0 on success, errno otherwise
2549 */
Ying Xue247f0f32014-02-18 16:06:46 +08002550static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2551 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002552{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002553 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002554 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002555 u32 value;
2556 int res;
2557
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002558 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2559 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002560 if (lvl != SOL_TIPC)
2561 return -ENOPROTOOPT;
2562 if (ol < sizeof(value))
2563 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002564 res = get_user(value, (u32 __user *)ov);
2565 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002566 return res;
2567
Allan Stephens0c3141e2008-04-15 00:22:02 -07002568 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002569
Per Lidenb97bf3f2006-01-02 19:04:38 +01002570 switch (opt) {
2571 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002572 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002573 break;
2574 case TIPC_SRC_DROPPABLE:
2575 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002576 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002577 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002578 res = -ENOPROTOOPT;
2579 break;
2580 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002581 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002582 break;
2583 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002584 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002585 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002586 break;
2587 default:
2588 res = -EINVAL;
2589 }
2590
Allan Stephens0c3141e2008-04-15 00:22:02 -07002591 release_sock(sk);
2592
Per Lidenb97bf3f2006-01-02 19:04:38 +01002593 return res;
2594}
2595
2596/**
Ying Xue247f0f32014-02-18 16:06:46 +08002597 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002598 * @sock: socket structure
2599 * @lvl: option level
2600 * @opt: option identifier
2601 * @ov: receptacle for option value
2602 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002603 *
2604 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002605 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002606 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002607 * Returns 0 on success, errno otherwise
2608 */
Ying Xue247f0f32014-02-18 16:06:46 +08002609static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2610 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002611{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002612 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002613 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002614 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002615 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002616 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002617
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002618 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2619 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002620 if (lvl != SOL_TIPC)
2621 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002622 res = get_user(len, ol);
2623 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002624 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002625
Allan Stephens0c3141e2008-04-15 00:22:02 -07002626 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002627
2628 switch (opt) {
2629 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002630 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002631 break;
2632 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002633 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002634 break;
2635 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002636 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002637 break;
2638 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002639 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002640 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002641 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002642 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002643 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002644 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002645 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002646 value = skb_queue_len(&sk->sk_receive_queue);
2647 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002648 default:
2649 res = -EINVAL;
2650 }
2651
Allan Stephens0c3141e2008-04-15 00:22:02 -07002652 release_sock(sk);
2653
Paul Gortmaker25860c32010-12-31 18:59:31 +00002654 if (res)
2655 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002656
Paul Gortmaker25860c32010-12-31 18:59:31 +00002657 if (len < sizeof(value))
2658 return -EINVAL;
2659
2660 if (copy_to_user(ov, &value, sizeof(value)))
2661 return -EFAULT;
2662
2663 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002664}
2665
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002666static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002667{
2668 struct tipc_sioc_ln_req lnr;
2669 void __user *argp = (void __user *)arg;
2670
2671 switch (cmd) {
2672 case SIOCGETLINKNAME:
2673 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2674 return -EFAULT;
Ying Xue7b8613e2014-10-20 14:44:25 +08002675 if (!tipc_node_get_linkname(lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002676 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2677 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2678 return -EFAULT;
2679 return 0;
2680 }
2681 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002682 default:
2683 return -ENOIOCTLCMD;
2684 }
2685}
2686
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002687/* Protocol switches for the various types of TIPC sockets */
2688
Florian Westphalbca65ea2008-02-07 18:18:01 -08002689static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002690 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002691 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002692 .release = tipc_release,
2693 .bind = tipc_bind,
2694 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002695 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002696 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002697 .getname = tipc_getname,
2698 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002699 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002700 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002701 .shutdown = tipc_shutdown,
2702 .setsockopt = tipc_setsockopt,
2703 .getsockopt = tipc_getsockopt,
2704 .sendmsg = tipc_sendmsg,
2705 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002706 .mmap = sock_no_mmap,
2707 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002708};
2709
Florian Westphalbca65ea2008-02-07 18:18:01 -08002710static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002711 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002712 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002713 .release = tipc_release,
2714 .bind = tipc_bind,
2715 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002716 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002717 .accept = tipc_accept,
2718 .getname = tipc_getname,
2719 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002720 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002721 .listen = tipc_listen,
2722 .shutdown = tipc_shutdown,
2723 .setsockopt = tipc_setsockopt,
2724 .getsockopt = tipc_getsockopt,
2725 .sendmsg = tipc_send_packet,
2726 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002727 .mmap = sock_no_mmap,
2728 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002729};
2730
Florian Westphalbca65ea2008-02-07 18:18:01 -08002731static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002732 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002733 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002734 .release = tipc_release,
2735 .bind = tipc_bind,
2736 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002737 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002738 .accept = tipc_accept,
2739 .getname = tipc_getname,
2740 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002741 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002742 .listen = tipc_listen,
2743 .shutdown = tipc_shutdown,
2744 .setsockopt = tipc_setsockopt,
2745 .getsockopt = tipc_getsockopt,
2746 .sendmsg = tipc_send_stream,
2747 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002748 .mmap = sock_no_mmap,
2749 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002750};
2751
Florian Westphalbca65ea2008-02-07 18:18:01 -08002752static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002753 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002754 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002755 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002756};
2757
2758static struct proto tipc_proto = {
2759 .name = "TIPC",
2760 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002761 .obj_size = sizeof(struct tipc_sock),
2762 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002763};
2764
Ying Xuec5fa7b32013-06-17 10:54:39 -04002765static struct proto tipc_proto_kern = {
2766 .name = "TIPC",
2767 .obj_size = sizeof(struct tipc_sock),
2768 .sysctl_rmem = sysctl_tipc_rmem
2769};
2770
Per Lidenb97bf3f2006-01-02 19:04:38 +01002771/**
Per Liden4323add2006-01-18 00:38:21 +01002772 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002773 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002774 * Returns 0 on success, errno otherwise
2775 */
Per Liden4323add2006-01-18 00:38:21 +01002776int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002777{
2778 int res;
2779
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002780 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002781 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002782 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002783 goto out;
2784 }
2785
2786 res = sock_register(&tipc_family_ops);
2787 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002788 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002789 proto_unregister(&tipc_proto);
2790 goto out;
2791 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002792 out:
2793 return res;
2794}
2795
2796/**
Per Liden4323add2006-01-18 00:38:21 +01002797 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002798 */
Per Liden4323add2006-01-18 00:38:21 +01002799void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002800{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002801 sock_unregister(tipc_family_ops.family);
2802 proto_unregister(&tipc_proto);
2803}
Richard Alpe34b78a122014-11-20 10:29:10 +01002804
2805/* Caller should hold socket lock for the passed tipc socket. */
2806int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
2807{
2808 u32 peer_node;
2809 u32 peer_port;
2810 struct nlattr *nest;
2811
2812 peer_node = tsk_peer_node(tsk);
2813 peer_port = tsk_peer_port(tsk);
2814
2815 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2816
2817 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2818 goto msg_full;
2819 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2820 goto msg_full;
2821
2822 if (tsk->conn_type != 0) {
2823 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2824 goto msg_full;
2825 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2826 goto msg_full;
2827 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2828 goto msg_full;
2829 }
2830 nla_nest_end(skb, nest);
2831
2832 return 0;
2833
2834msg_full:
2835 nla_nest_cancel(skb, nest);
2836
2837 return -EMSGSIZE;
2838}
2839
2840/* Caller should hold socket lock for the passed tipc socket. */
2841int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2842 struct tipc_sock *tsk)
2843{
2844 int err;
2845 void *hdr;
2846 struct nlattr *attrs;
2847
2848 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2849 &tipc_genl_v2_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2850 if (!hdr)
2851 goto msg_cancel;
2852
2853 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2854 if (!attrs)
2855 goto genlmsg_cancel;
2856 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->ref))
2857 goto attr_msg_cancel;
2858 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr))
2859 goto attr_msg_cancel;
2860
2861 if (tsk->connected) {
2862 err = __tipc_nl_add_sk_con(skb, tsk);
2863 if (err)
2864 goto attr_msg_cancel;
2865 } else if (!list_empty(&tsk->publications)) {
2866 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2867 goto attr_msg_cancel;
2868 }
2869 nla_nest_end(skb, attrs);
2870 genlmsg_end(skb, hdr);
2871
2872 return 0;
2873
2874attr_msg_cancel:
2875 nla_nest_cancel(skb, attrs);
2876genlmsg_cancel:
2877 genlmsg_cancel(skb, hdr);
2878msg_cancel:
2879 return -EMSGSIZE;
2880}
2881
2882int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2883{
2884 int err;
2885 struct tipc_sock *tsk;
2886 u32 prev_ref = cb->args[0];
2887 u32 ref = prev_ref;
2888
2889 tsk = tipc_sk_get_next(&ref);
2890 for (; tsk; tsk = tipc_sk_get_next(&ref)) {
2891 lock_sock(&tsk->sk);
2892 err = __tipc_nl_add_sk(skb, cb, tsk);
2893 release_sock(&tsk->sk);
2894 tipc_sk_put(tsk);
2895 if (err)
2896 break;
2897
2898 prev_ref = ref;
2899 }
2900
2901 cb->args[0] = prev_ref;
2902
2903 return skb->len;
2904}