tipc: eliminate functions tipc_port_init and tipc_port_destroy
[deliverable/linux.git] / net / tipc / socket.c
CommitLineData
b97bf3fd 1/*
02c00c2a 2 * net/tipc/socket.c: TIPC socket API
c4307285 3 *
8826cde6 4 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
c5fa7b3c 5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
11 * 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.
b97bf3fd 19 *
9ea1fd3c
PL
20 * 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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
b97bf3fd 37#include "core.h"
d265fef6 38#include "port.h"
e2dafe87 39#include "name_table.h"
78acb1f9 40#include "node.h"
e2dafe87 41#include "link.h"
2cf8aa19 42#include <linux/export.h>
2cf8aa19 43
b97bf3fd
PL
44#define SS_LISTENING -1 /* socket is listening */
45#define SS_READY -2 /* socket is connectionless */
46
3654ea02 47#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
dadebc00 48#define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
ac0074ee 49#define TIPC_FWD_MSG 1
b97bf3fd 50
4f4482dc 51static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
676d2369 52static void tipc_data_ready(struct sock *sk);
f288bef4 53static void tipc_write_space(struct sock *sk);
247f0f3c
YX
54static int tipc_release(struct socket *sock);
55static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
0abd8ff2 56static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
57289015 57static void tipc_sk_timeout(unsigned long ref);
b97bf3fd 58
bca65eae
FW
59static const struct proto_ops packet_ops;
60static const struct proto_ops stream_ops;
61static const struct proto_ops msg_ops;
b97bf3fd
PL
62
63static struct proto tipc_proto;
c5fa7b3c 64static struct proto tipc_proto_kern;
b97bf3fd 65
5b8fa7ce
JPM
66DEFINE_SPINLOCK(tipc_port_list_lock);
67LIST_HEAD(tipc_socks);
68
c4307285 69/*
0c3141e9
AS
70 * Revised TIPC socket locking policy:
71 *
72 * Most socket operations take the standard socket lock when they start
73 * and hold it until they finish (or until they need to sleep). Acquiring
74 * this lock grants the owner exclusive access to the fields of the socket
75 * data structures, with the exception of the backlog queue. A few socket
76 * operations can be done without taking the socket lock because they only
77 * read socket information that never changes during the life of the socket.
78 *
79 * Socket operations may acquire the lock for the associated TIPC port if they
80 * need to perform an operation on the port. If any routine needs to acquire
81 * both the socket lock and the port lock it must take the socket lock first
82 * to avoid the risk of deadlock.
83 *
84 * The dispatcher handling incoming messages cannot grab the socket lock in
85 * the standard fashion, since invoked it runs at the BH level and cannot block.
86 * Instead, it checks to see if the socket lock is currently owned by someone,
87 * and either handles the message itself or adds it to the socket's backlog
88 * queue; in the latter case the queued message is processed once the process
89 * owning the socket lock releases it.
90 *
91 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
92 * the problem of a blocked socket operation preventing any other operations
93 * from occurring. However, applications must be careful if they have
94 * multiple threads trying to send (or receive) on the same socket, as these
95 * operations might interfere with each other. For example, doing a connect
96 * and a receive at the same time might allow the receive to consume the
97 * ACK message meant for the connect. While additional work could be done
98 * to try and overcome this, it doesn't seem to be worthwhile at the present.
99 *
100 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
101 * that another operation that must be performed in a non-blocking manner is
102 * not delayed for very long because the lock has already been taken.
103 *
104 * NOTE: This code assumes that certain fields of a port/socket pair are
105 * constant over its lifetime; such fields can be examined without taking
106 * the socket lock and/or port lock, and do not need to be re-read even
107 * after resuming processing after waiting. These fields include:
108 * - socket type
109 * - pointer to socket sk structure (aka tipc_sock structure)
110 * - pointer to port structure
111 * - port reference
112 */
113
8826cde6
JPM
114#include "socket.h"
115
0c3141e9
AS
116/**
117 * advance_rx_queue - discard first buffer in socket receive queue
118 *
119 * Caller must hold socket lock
b97bf3fd 120 */
0c3141e9 121static void advance_rx_queue(struct sock *sk)
b97bf3fd 122{
5f6d9123 123 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
b97bf3fd
PL
124}
125
b97bf3fd 126/**
0c3141e9
AS
127 * reject_rx_queue - reject all buffers in socket receive queue
128 *
129 * Caller must hold socket lock
b97bf3fd 130 */
0c3141e9 131static void reject_rx_queue(struct sock *sk)
b97bf3fd 132{
0c3141e9 133 struct sk_buff *buf;
8db1bae3 134 u32 dnode;
0c3141e9 135
8db1bae3
JPM
136 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
137 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
9fbfb8b1 138 tipc_link_xmit(buf, dnode, 0);
8db1bae3 139 }
b97bf3fd
PL
140}
141
142/**
c5fa7b3c 143 * tipc_sk_create - create a TIPC socket
0c3141e9 144 * @net: network namespace (must be default network)
b97bf3fd
PL
145 * @sock: pre-allocated socket structure
146 * @protocol: protocol indicator (must be 0)
3f378b68 147 * @kern: caused by kernel or by userspace?
c4307285 148 *
0c3141e9
AS
149 * This routine creates additional data structures used by the TIPC socket,
150 * initializes them, and links them together.
b97bf3fd
PL
151 *
152 * Returns 0 on success, errno otherwise
153 */
58ed9442
JPM
154static int tipc_sk_create(struct net *net, struct socket *sock,
155 int protocol, int kern)
b97bf3fd 156{
0c3141e9
AS
157 const struct proto_ops *ops;
158 socket_state state;
b97bf3fd 159 struct sock *sk;
58ed9442
JPM
160 struct tipc_sock *tsk;
161 struct tipc_port *port;
5b8fa7ce 162 struct tipc_msg *msg;
58ed9442 163 u32 ref;
0c3141e9
AS
164
165 /* Validate arguments */
b97bf3fd
PL
166 if (unlikely(protocol != 0))
167 return -EPROTONOSUPPORT;
168
b97bf3fd
PL
169 switch (sock->type) {
170 case SOCK_STREAM:
0c3141e9
AS
171 ops = &stream_ops;
172 state = SS_UNCONNECTED;
b97bf3fd
PL
173 break;
174 case SOCK_SEQPACKET:
0c3141e9
AS
175 ops = &packet_ops;
176 state = SS_UNCONNECTED;
b97bf3fd
PL
177 break;
178 case SOCK_DGRAM:
b97bf3fd 179 case SOCK_RDM:
0c3141e9
AS
180 ops = &msg_ops;
181 state = SS_READY;
b97bf3fd 182 break;
49978651 183 default:
49978651 184 return -EPROTOTYPE;
b97bf3fd
PL
185 }
186
0c3141e9 187 /* Allocate socket's protocol area */
c5fa7b3c
YX
188 if (!kern)
189 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
190 else
191 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
192
0c3141e9 193 if (sk == NULL)
b97bf3fd 194 return -ENOMEM;
b97bf3fd 195
58ed9442
JPM
196 tsk = tipc_sk(sk);
197 port = &tsk->port;
5b8fa7ce 198 ref = tipc_ref_acquire(port, &port->lock);
58ed9442 199 if (!ref) {
5b8fa7ce 200 pr_warn("Socket create failed; reference table exhausted\n");
0c3141e9
AS
201 return -ENOMEM;
202 }
5b8fa7ce
JPM
203 port->max_pkt = MAX_PKT_DEFAULT;
204 port->ref = ref;
205 INIT_LIST_HEAD(&port->publications);
206 INIT_LIST_HEAD(&port->port_list);
207
208 /* Guard against race during node address update */
209 spin_lock_bh(&tipc_port_list_lock);
210 msg = &port->phdr;
211 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
212 NAMED_H_SIZE, 0);
213 msg_set_origport(msg, ref);
214 list_add_tail(&port->port_list, &tipc_socks);
215 spin_unlock_bh(&tipc_port_list_lock);
b97bf3fd 216
0c3141e9 217 /* Finish initializing socket data structures */
0c3141e9
AS
218 sock->ops = ops;
219 sock->state = state;
0c3141e9 220 sock_init_data(sock, sk);
57289015 221 k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref);
4f4482dc 222 sk->sk_backlog_rcv = tipc_backlog_rcv;
cc79dd1b 223 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
f288bef4
YX
224 sk->sk_data_ready = tipc_data_ready;
225 sk->sk_write_space = tipc_write_space;
4f4482dc 226 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
60120526 227 tsk->sent_unacked = 0;
4f4482dc 228 atomic_set(&tsk->dupl_rcvcnt, 0);
58ed9442 229 tipc_port_unlock(port);
7ef43eba 230
0c3141e9 231 if (sock->state == SS_READY) {
58ed9442 232 tipc_port_set_unreturnable(port, true);
0c3141e9 233 if (sock->type == SOCK_DGRAM)
58ed9442 234 tipc_port_set_unreliable(port, true);
0c3141e9 235 }
b97bf3fd
PL
236 return 0;
237}
238
c5fa7b3c
YX
239/**
240 * tipc_sock_create_local - create TIPC socket from inside TIPC module
241 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
242 *
243 * We cannot use sock_creat_kern here because it bumps module user count.
244 * Since socket owner and creator is the same module we must make sure
245 * that module count remains zero for module local sockets, otherwise
246 * we cannot do rmmod.
247 *
248 * Returns 0 on success, errno otherwise
249 */
250int tipc_sock_create_local(int type, struct socket **res)
251{
252 int rc;
c5fa7b3c
YX
253
254 rc = sock_create_lite(AF_TIPC, type, 0, res);
255 if (rc < 0) {
256 pr_err("Failed to create kernel socket\n");
257 return rc;
258 }
259 tipc_sk_create(&init_net, *res, 0, 1);
260
c5fa7b3c
YX
261 return 0;
262}
263
264/**
265 * tipc_sock_release_local - release socket created by tipc_sock_create_local
266 * @sock: the socket to be released.
267 *
268 * Module reference count is not incremented when such sockets are created,
269 * so we must keep it from being decremented when they are released.
270 */
271void tipc_sock_release_local(struct socket *sock)
272{
247f0f3c 273 tipc_release(sock);
c5fa7b3c
YX
274 sock->ops = NULL;
275 sock_release(sock);
276}
277
278/**
279 * tipc_sock_accept_local - accept a connection on a socket created
280 * with tipc_sock_create_local. Use this function to avoid that
281 * module reference count is inadvertently incremented.
282 *
283 * @sock: the accepting socket
284 * @newsock: reference to the new socket to be created
285 * @flags: socket flags
286 */
287
288int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
ae8509c4 289 int flags)
c5fa7b3c
YX
290{
291 struct sock *sk = sock->sk;
292 int ret;
293
294 ret = sock_create_lite(sk->sk_family, sk->sk_type,
295 sk->sk_protocol, newsock);
296 if (ret < 0)
297 return ret;
298
247f0f3c 299 ret = tipc_accept(sock, *newsock, flags);
c5fa7b3c
YX
300 if (ret < 0) {
301 sock_release(*newsock);
302 return ret;
303 }
304 (*newsock)->ops = sock->ops;
305 return ret;
306}
307
b97bf3fd 308/**
247f0f3c 309 * tipc_release - destroy a TIPC socket
b97bf3fd
PL
310 * @sock: socket to destroy
311 *
312 * This routine cleans up any messages that are still queued on the socket.
313 * For DGRAM and RDM socket types, all queued messages are rejected.
314 * For SEQPACKET and STREAM socket types, the first message is rejected
315 * and any others are discarded. (If the first message on a STREAM socket
316 * is partially-read, it is discarded and the next one is rejected instead.)
c4307285 317 *
b97bf3fd
PL
318 * NOTE: Rejected messages are not necessarily returned to the sender! They
319 * are returned or discarded according to the "destination droppable" setting
320 * specified for the message by the sender.
321 *
322 * Returns 0 on success, errno otherwise
323 */
247f0f3c 324static int tipc_release(struct socket *sock)
b97bf3fd 325{
b97bf3fd 326 struct sock *sk = sock->sk;
58ed9442
JPM
327 struct tipc_sock *tsk;
328 struct tipc_port *port;
b97bf3fd 329 struct sk_buff *buf;
8db1bae3 330 u32 dnode;
b97bf3fd 331
0c3141e9
AS
332 /*
333 * Exit if socket isn't fully initialized (occurs when a failed accept()
334 * releases a pre-allocated child socket that was never used)
335 */
0c3141e9 336 if (sk == NULL)
b97bf3fd 337 return 0;
c4307285 338
58ed9442
JPM
339 tsk = tipc_sk(sk);
340 port = &tsk->port;
0c3141e9
AS
341 lock_sock(sk);
342
343 /*
344 * Reject all unreceived messages, except on an active connection
345 * (which disconnects locally & sends a 'FIN+' to peer)
346 */
5b8fa7ce 347 dnode = tipc_port_peernode(port);
b97bf3fd 348 while (sock->state != SS_DISCONNECTING) {
0c3141e9
AS
349 buf = __skb_dequeue(&sk->sk_receive_queue);
350 if (buf == NULL)
b97bf3fd 351 break;
40682432 352 if (TIPC_SKB_CB(buf)->handle != NULL)
5f6d9123 353 kfree_skb(buf);
0c3141e9
AS
354 else {
355 if ((sock->state == SS_CONNECTING) ||
356 (sock->state == SS_CONNECTED)) {
357 sock->state = SS_DISCONNECTING;
dadebc00 358 port->connected = 0;
5b8fa7ce 359 tipc_node_remove_conn(dnode, port->ref);
0c3141e9 360 }
8db1bae3 361 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
9fbfb8b1 362 tipc_link_xmit(buf, dnode, 0);
0c3141e9 363 }
b97bf3fd
PL
364 }
365
5b8fa7ce
JPM
366 tipc_withdraw(port, 0, NULL);
367 spin_lock_bh(port->lock);
368 tipc_ref_discard(port->ref);
369 spin_unlock_bh(port->lock);
370 k_cancel_timer(&port->timer);
371 if (port->connected) {
372 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
373 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
374 tipc_port_peerport(port),
375 port->ref, TIPC_ERR_NO_PORT);
376 if (buf)
377 tipc_link_xmit(buf, dnode, port->ref);
378 tipc_node_remove_conn(dnode, port->ref);
379 }
380 spin_lock_bh(&tipc_port_list_lock);
381 list_del(&port->port_list);
382 spin_unlock_bh(&tipc_port_list_lock);
383 k_term_timer(&port->timer);
b97bf3fd 384
0c3141e9 385 /* Discard any remaining (connection-based) messages in receive queue */
57467e56 386 __skb_queue_purge(&sk->sk_receive_queue);
b97bf3fd 387
0c3141e9 388 /* Reject any messages that accumulated in backlog queue */
0c3141e9
AS
389 sock->state = SS_DISCONNECTING;
390 release_sock(sk);
b97bf3fd 391 sock_put(sk);
0c3141e9 392 sock->sk = NULL;
b97bf3fd 393
065d7e39 394 return 0;
b97bf3fd
PL
395}
396
397/**
247f0f3c 398 * tipc_bind - associate or disassocate TIPC name(s) with a socket
b97bf3fd
PL
399 * @sock: socket structure
400 * @uaddr: socket address describing name(s) and desired operation
401 * @uaddr_len: size of socket address data structure
c4307285 402 *
b97bf3fd
PL
403 * Name and name sequence binding is indicated using a positive scope value;
404 * a negative scope value unbinds the specified name. Specifying no name
405 * (i.e. a socket address length of 0) unbinds all names from the socket.
c4307285 406 *
b97bf3fd 407 * Returns 0 on success, errno otherwise
0c3141e9
AS
408 *
409 * NOTE: This routine doesn't need to take the socket lock since it doesn't
410 * access any non-constant socket information.
b97bf3fd 411 */
247f0f3c
YX
412static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
413 int uaddr_len)
b97bf3fd 414{
84602761 415 struct sock *sk = sock->sk;
b97bf3fd 416 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
58ed9442 417 struct tipc_sock *tsk = tipc_sk(sk);
84602761 418 int res = -EINVAL;
b97bf3fd 419
84602761
YX
420 lock_sock(sk);
421 if (unlikely(!uaddr_len)) {
58ed9442 422 res = tipc_withdraw(&tsk->port, 0, NULL);
84602761
YX
423 goto exit;
424 }
c4307285 425
84602761
YX
426 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
427 res = -EINVAL;
428 goto exit;
429 }
430 if (addr->family != AF_TIPC) {
431 res = -EAFNOSUPPORT;
432 goto exit;
433 }
b97bf3fd 434
b97bf3fd
PL
435 if (addr->addrtype == TIPC_ADDR_NAME)
436 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
84602761
YX
437 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
438 res = -EAFNOSUPPORT;
439 goto exit;
440 }
c4307285 441
13a2e898 442 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
7d0ab17b 443 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
84602761
YX
444 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
445 res = -EACCES;
446 goto exit;
447 }
c422f1bd 448
84602761 449 res = (addr->scope > 0) ?
58ed9442
JPM
450 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
451 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
84602761
YX
452exit:
453 release_sock(sk);
454 return res;
b97bf3fd
PL
455}
456
c4307285 457/**
247f0f3c 458 * tipc_getname - get port ID of socket or peer socket
b97bf3fd
PL
459 * @sock: socket structure
460 * @uaddr: area for returned socket address
461 * @uaddr_len: area for returned length of socket address
2da59918 462 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
c4307285 463 *
b97bf3fd 464 * Returns 0 on success, errno otherwise
0c3141e9 465 *
2da59918
AS
466 * NOTE: This routine doesn't need to take the socket lock since it only
467 * accesses socket information that is unchanging (or which changes in
0e65967e 468 * a completely predictable manner).
b97bf3fd 469 */
247f0f3c
YX
470static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
471 int *uaddr_len, int peer)
b97bf3fd 472{
b97bf3fd 473 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
58ed9442 474 struct tipc_sock *tsk = tipc_sk(sock->sk);
b97bf3fd 475
88f8a5e3 476 memset(addr, 0, sizeof(*addr));
0c3141e9 477 if (peer) {
2da59918
AS
478 if ((sock->state != SS_CONNECTED) &&
479 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
480 return -ENOTCONN;
58ed9442
JPM
481 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
482 addr->addr.id.node = tipc_port_peernode(&tsk->port);
0c3141e9 483 } else {
58ed9442 484 addr->addr.id.ref = tsk->port.ref;
b924dcf0 485 addr->addr.id.node = tipc_own_addr;
0c3141e9 486 }
b97bf3fd
PL
487
488 *uaddr_len = sizeof(*addr);
489 addr->addrtype = TIPC_ADDR_ID;
490 addr->family = AF_TIPC;
491 addr->scope = 0;
b97bf3fd
PL
492 addr->addr.name.domain = 0;
493
0c3141e9 494 return 0;
b97bf3fd
PL
495}
496
497/**
247f0f3c 498 * tipc_poll - read and possibly block on pollmask
b97bf3fd
PL
499 * @file: file structure associated with the socket
500 * @sock: socket for which to calculate the poll bits
501 * @wait: ???
502 *
9b674e82
AS
503 * Returns pollmask value
504 *
505 * COMMENTARY:
506 * It appears that the usual socket locking mechanisms are not useful here
507 * since the pollmask info is potentially out-of-date the moment this routine
508 * exits. TCP and other protocols seem to rely on higher level poll routines
509 * to handle any preventable race conditions, so TIPC will do the same ...
510 *
511 * TIPC sets the returned events as follows:
f662c070
AS
512 *
513 * socket state flags set
514 * ------------ ---------
515 * unconnected no read flags
c4fc298a 516 * POLLOUT if port is not congested
f662c070
AS
517 *
518 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
519 * no write flags
520 *
521 * connected POLLIN/POLLRDNORM if data in rx queue
522 * POLLOUT if port is not congested
523 *
524 * disconnecting POLLIN/POLLRDNORM/POLLHUP
525 * no write flags
526 *
527 * listening POLLIN if SYN in rx queue
528 * no write flags
529 *
530 * ready POLLIN/POLLRDNORM if data in rx queue
531 * [connectionless] POLLOUT (since port cannot be congested)
532 *
533 * IMPORTANT: The fact that a read or write operation is indicated does NOT
534 * imply that the operation will succeed, merely that it should be performed
535 * and will not block.
b97bf3fd 536 */
247f0f3c
YX
537static unsigned int tipc_poll(struct file *file, struct socket *sock,
538 poll_table *wait)
b97bf3fd 539{
9b674e82 540 struct sock *sk = sock->sk;
58ed9442 541 struct tipc_sock *tsk = tipc_sk(sk);
f662c070 542 u32 mask = 0;
9b674e82 543
f288bef4 544 sock_poll_wait(file, sk_sleep(sk), wait);
9b674e82 545
f662c070 546 switch ((int)sock->state) {
c4fc298a 547 case SS_UNCONNECTED:
60120526 548 if (!tsk->link_cong)
c4fc298a
EH
549 mask |= POLLOUT;
550 break;
f662c070
AS
551 case SS_READY:
552 case SS_CONNECTED:
60120526 553 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
f662c070
AS
554 mask |= POLLOUT;
555 /* fall thru' */
556 case SS_CONNECTING:
557 case SS_LISTENING:
558 if (!skb_queue_empty(&sk->sk_receive_queue))
559 mask |= (POLLIN | POLLRDNORM);
560 break;
561 case SS_DISCONNECTING:
562 mask = (POLLIN | POLLRDNORM | POLLHUP);
563 break;
564 }
9b674e82
AS
565
566 return mask;
b97bf3fd
PL
567}
568
0abd8ff2
JPM
569/**
570 * tipc_sendmcast - send multicast message
571 * @sock: socket structure
572 * @seq: destination address
573 * @iov: message data to send
574 * @dsz: total length of message data
575 * @timeo: timeout to wait for wakeup
576 *
577 * Called from function tipc_sendmsg(), which has done all sanity checks
578 * Returns the number of bytes sent on success, or errno
579 */
580static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
581 struct iovec *iov, size_t dsz, long timeo)
582{
583 struct sock *sk = sock->sk;
584 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
585 struct sk_buff *buf;
586 uint mtu;
587 int rc;
588
589 msg_set_type(mhdr, TIPC_MCAST_MSG);
590 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
591 msg_set_destport(mhdr, 0);
592 msg_set_destnode(mhdr, 0);
593 msg_set_nametype(mhdr, seq->type);
594 msg_set_namelower(mhdr, seq->lower);
595 msg_set_nameupper(mhdr, seq->upper);
596 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
597
598new_mtu:
599 mtu = tipc_bclink_get_mtu();
9fbfb8b1 600 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
0abd8ff2
JPM
601 if (unlikely(rc < 0))
602 return rc;
603
604 do {
605 rc = tipc_bclink_xmit(buf);
606 if (likely(rc >= 0)) {
607 rc = dsz;
608 break;
609 }
610 if (rc == -EMSGSIZE)
611 goto new_mtu;
612 if (rc != -ELINKCONG)
613 break;
50100a5e 614 tipc_sk(sk)->link_cong = 1;
0abd8ff2
JPM
615 rc = tipc_wait_for_sndmsg(sock, &timeo);
616 if (rc)
617 kfree_skb_list(buf);
618 } while (!rc);
619 return rc;
620}
621
078bec82
JPM
622/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
623 */
624void tipc_sk_mcast_rcv(struct sk_buff *buf)
625{
626 struct tipc_msg *msg = buf_msg(buf);
627 struct tipc_port_list dports = {0, NULL, };
628 struct tipc_port_list *item;
629 struct sk_buff *b;
630 uint i, last, dst = 0;
631 u32 scope = TIPC_CLUSTER_SCOPE;
632
633 if (in_own_node(msg_orignode(msg)))
634 scope = TIPC_NODE_SCOPE;
635
636 /* Create destination port list: */
637 tipc_nametbl_mc_translate(msg_nametype(msg),
638 msg_namelower(msg),
639 msg_nameupper(msg),
640 scope,
641 &dports);
642 last = dports.count;
643 if (!last) {
644 kfree_skb(buf);
645 return;
646 }
647
648 for (item = &dports; item; item = item->next) {
649 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
650 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
651 if (!b) {
652 pr_warn("Failed do clone mcast rcv buffer\n");
653 continue;
654 }
655 msg_set_destport(msg, item->ports[i]);
656 tipc_sk_rcv(b);
657 }
658 }
659 tipc_port_list_free(&dports);
660}
661
ac0074ee
JPM
662/**
663 * tipc_sk_proto_rcv - receive a connection mng protocol message
664 * @tsk: receiving socket
665 * @dnode: node to send response message to, if any
666 * @buf: buffer containing protocol message
667 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
668 * (CONN_PROBE_REPLY) message should be forwarded.
669 */
52f50ce5
WY
670static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
671 struct sk_buff *buf)
ac0074ee
JPM
672{
673 struct tipc_msg *msg = buf_msg(buf);
674 struct tipc_port *port = &tsk->port;
60120526 675 int conn_cong;
ac0074ee
JPM
676
677 /* Ignore if connection cannot be validated: */
678 if (!port->connected || !tipc_port_peer_msg(port, msg))
679 goto exit;
680
681 port->probing_state = TIPC_CONN_OK;
682
683 if (msg_type(msg) == CONN_ACK) {
60120526
JPM
684 conn_cong = tipc_sk_conn_cong(tsk);
685 tsk->sent_unacked -= msg_msgcnt(msg);
686 if (conn_cong)
50100a5e 687 tsk->sk.sk_write_space(&tsk->sk);
ac0074ee
JPM
688 } else if (msg_type(msg) == CONN_PROBE) {
689 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
690 return TIPC_OK;
691 msg_set_type(msg, CONN_PROBE_REPLY);
692 return TIPC_FWD_MSG;
693 }
694 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
695exit:
696 kfree_skb(buf);
697 return TIPC_OK;
698}
699
c4307285 700/**
b97bf3fd
PL
701 * dest_name_check - verify user is permitted to send to specified port name
702 * @dest: destination address
703 * @m: descriptor for message to be sent
c4307285 704 *
b97bf3fd
PL
705 * Prevents restricted configuration commands from being issued by
706 * unauthorized users.
c4307285 707 *
b97bf3fd
PL
708 * Returns 0 if permission is granted, otherwise errno
709 */
05790c64 710static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
b97bf3fd
PL
711{
712 struct tipc_cfg_msg_hdr hdr;
713
e2dafe87
JPM
714 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
715 return 0;
c4307285
YH
716 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
717 return 0;
718 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
719 return 0;
c4307285
YH
720 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
721 return -EACCES;
b97bf3fd 722
3f8dd944
AS
723 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
724 return -EMSGSIZE;
c4307285 725 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
b97bf3fd 726 return -EFAULT;
70cb2347 727 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
b97bf3fd 728 return -EACCES;
c4307285 729
b97bf3fd
PL
730 return 0;
731}
732
3f40504f
YX
733static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
734{
735 struct sock *sk = sock->sk;
58ed9442 736 struct tipc_sock *tsk = tipc_sk(sk);
3f40504f
YX
737 DEFINE_WAIT(wait);
738 int done;
739
740 do {
741 int err = sock_error(sk);
742 if (err)
743 return err;
744 if (sock->state == SS_DISCONNECTING)
745 return -EPIPE;
746 if (!*timeo_p)
747 return -EAGAIN;
748 if (signal_pending(current))
749 return sock_intr_errno(*timeo_p);
750
751 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
60120526 752 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
3f40504f
YX
753 finish_wait(sk_sleep(sk), &wait);
754 } while (!done);
755 return 0;
756}
757
b97bf3fd 758/**
247f0f3c 759 * tipc_sendmsg - send message in connectionless manner
0c3141e9 760 * @iocb: if NULL, indicates that socket lock is already held
b97bf3fd
PL
761 * @sock: socket structure
762 * @m: message to send
e2dafe87 763 * @dsz: amount of user data to be sent
c4307285 764 *
b97bf3fd 765 * Message must have an destination specified explicitly.
c4307285 766 * Used for SOCK_RDM and SOCK_DGRAM messages,
b97bf3fd
PL
767 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
768 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
c4307285 769 *
b97bf3fd
PL
770 * Returns the number of bytes sent on success, or errno otherwise
771 */
247f0f3c 772static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
e2dafe87 773 struct msghdr *m, size_t dsz)
b97bf3fd 774{
e2dafe87 775 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
0c3141e9 776 struct sock *sk = sock->sk;
58ed9442 777 struct tipc_sock *tsk = tipc_sk(sk);
5c311421 778 struct tipc_port *port = &tsk->port;
e2dafe87
JPM
779 struct tipc_msg *mhdr = &port->phdr;
780 struct iovec *iov = m->msg_iov;
781 u32 dnode, dport;
782 struct sk_buff *buf;
783 struct tipc_name_seq *seq = &dest->addr.nameseq;
784 u32 mtu;
3f40504f 785 long timeo;
e2dafe87 786 int rc = -EINVAL;
b97bf3fd
PL
787
788 if (unlikely(!dest))
789 return -EDESTADDRREQ;
e2dafe87 790
51f9cc1f
AS
791 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
792 (dest->family != AF_TIPC)))
b97bf3fd 793 return -EINVAL;
e2dafe87
JPM
794
795 if (dsz > TIPC_MAX_USER_MSG_SIZE)
c29c3f70 796 return -EMSGSIZE;
b97bf3fd 797
0c3141e9
AS
798 if (iocb)
799 lock_sock(sk);
800
e2dafe87 801 if (unlikely(sock->state != SS_READY)) {
0c3141e9 802 if (sock->state == SS_LISTENING) {
e2dafe87 803 rc = -EPIPE;
0c3141e9
AS
804 goto exit;
805 }
806 if (sock->state != SS_UNCONNECTED) {
e2dafe87 807 rc = -EISCONN;
0c3141e9
AS
808 goto exit;
809 }
58ed9442 810 if (tsk->port.published) {
e2dafe87 811 rc = -EOPNOTSUPP;
0c3141e9
AS
812 goto exit;
813 }
3388007b 814 if (dest->addrtype == TIPC_ADDR_NAME) {
58ed9442
JPM
815 tsk->port.conn_type = dest->addr.name.name.type;
816 tsk->port.conn_instance = dest->addr.name.name.instance;
3388007b 817 }
b97bf3fd 818 }
e2dafe87
JPM
819 rc = dest_name_check(dest, m);
820 if (rc)
821 goto exit;
b97bf3fd 822
3f40504f 823 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
e2dafe87
JPM
824
825 if (dest->addrtype == TIPC_ADDR_MCAST) {
826 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
827 goto exit;
828 } else if (dest->addrtype == TIPC_ADDR_NAME) {
829 u32 type = dest->addr.name.name.type;
830 u32 inst = dest->addr.name.name.instance;
831 u32 domain = dest->addr.name.domain;
832
833 dnode = domain;
834 msg_set_type(mhdr, TIPC_NAMED_MSG);
835 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
836 msg_set_nametype(mhdr, type);
837 msg_set_nameinst(mhdr, inst);
838 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
839 dport = tipc_nametbl_translate(type, inst, &dnode);
840 msg_set_destnode(mhdr, dnode);
841 msg_set_destport(mhdr, dport);
842 if (unlikely(!dport && !dnode)) {
843 rc = -EHOSTUNREACH;
844 goto exit;
c4307285 845 }
e2dafe87
JPM
846 } else if (dest->addrtype == TIPC_ADDR_ID) {
847 dnode = dest->addr.id.node;
848 msg_set_type(mhdr, TIPC_DIRECT_MSG);
849 msg_set_lookup_scope(mhdr, 0);
850 msg_set_destnode(mhdr, dnode);
851 msg_set_destport(mhdr, dest->addr.id.ref);
852 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
853 }
854
855new_mtu:
856 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
9fbfb8b1 857 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
e2dafe87
JPM
858 if (rc < 0)
859 goto exit;
860
861 do {
50100a5e 862 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
9fbfb8b1 863 rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
e2dafe87
JPM
864 if (likely(rc >= 0)) {
865 if (sock->state != SS_READY)
0c3141e9 866 sock->state = SS_CONNECTING;
e2dafe87 867 rc = dsz;
0c3141e9 868 break;
c4307285 869 }
e2dafe87
JPM
870 if (rc == -EMSGSIZE)
871 goto new_mtu;
e2dafe87 872 if (rc != -ELINKCONG)
0c3141e9 873 break;
50100a5e 874 tsk->link_cong = 1;
e2dafe87 875 rc = tipc_wait_for_sndmsg(sock, &timeo);
70452dcb
EH
876 if (rc)
877 kfree_skb_list(buf);
e2dafe87 878 } while (!rc);
0c3141e9
AS
879exit:
880 if (iocb)
881 release_sock(sk);
e2dafe87
JPM
882
883 return rc;
b97bf3fd
PL
884}
885
391a6dd1
YX
886static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
887{
888 struct sock *sk = sock->sk;
58ed9442 889 struct tipc_sock *tsk = tipc_sk(sk);
391a6dd1
YX
890 DEFINE_WAIT(wait);
891 int done;
892
893 do {
894 int err = sock_error(sk);
895 if (err)
896 return err;
897 if (sock->state == SS_DISCONNECTING)
898 return -EPIPE;
899 else if (sock->state != SS_CONNECTED)
900 return -ENOTCONN;
901 if (!*timeo_p)
902 return -EAGAIN;
903 if (signal_pending(current))
904 return sock_intr_errno(*timeo_p);
905
906 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
907 done = sk_wait_event(sk, timeo_p,
60120526
JPM
908 (!tsk->link_cong &&
909 !tipc_sk_conn_cong(tsk)) ||
910 !tsk->port.connected);
391a6dd1
YX
911 finish_wait(sk_sleep(sk), &wait);
912 } while (!done);
913 return 0;
914}
915
c4307285 916/**
4ccfe5e0
JPM
917 * tipc_send_stream - send stream-oriented data
918 * @iocb: (unused)
b97bf3fd 919 * @sock: socket structure
4ccfe5e0
JPM
920 * @m: data to send
921 * @dsz: total length of data to be transmitted
c4307285 922 *
4ccfe5e0 923 * Used for SOCK_STREAM data.
c4307285 924 *
4ccfe5e0
JPM
925 * Returns the number of bytes sent on success (or partial success),
926 * or errno if no data sent
b97bf3fd 927 */
4ccfe5e0
JPM
928static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
929 struct msghdr *m, size_t dsz)
b97bf3fd 930{
0c3141e9 931 struct sock *sk = sock->sk;
58ed9442 932 struct tipc_sock *tsk = tipc_sk(sk);
4ccfe5e0
JPM
933 struct tipc_port *port = &tsk->port;
934 struct tipc_msg *mhdr = &port->phdr;
935 struct sk_buff *buf;
342dfc30 936 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
4ccfe5e0
JPM
937 u32 ref = port->ref;
938 int rc = -EINVAL;
391a6dd1 939 long timeo;
4ccfe5e0
JPM
940 u32 dnode;
941 uint mtu, send, sent = 0;
b97bf3fd
PL
942
943 /* Handle implied connection establishment */
4ccfe5e0
JPM
944 if (unlikely(dest)) {
945 rc = tipc_sendmsg(iocb, sock, m, dsz);
946 if (dsz && (dsz == rc))
60120526 947 tsk->sent_unacked = 1;
4ccfe5e0
JPM
948 return rc;
949 }
950 if (dsz > (uint)INT_MAX)
c29c3f70
AS
951 return -EMSGSIZE;
952
0c3141e9
AS
953 if (iocb)
954 lock_sock(sk);
b97bf3fd 955
391a6dd1
YX
956 if (unlikely(sock->state != SS_CONNECTED)) {
957 if (sock->state == SS_DISCONNECTING)
4ccfe5e0 958 rc = -EPIPE;
391a6dd1 959 else
4ccfe5e0 960 rc = -ENOTCONN;
391a6dd1
YX
961 goto exit;
962 }
1d835874 963
391a6dd1 964 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
4ccfe5e0 965 dnode = tipc_port_peernode(port);
4ccfe5e0
JPM
966
967next:
968 mtu = port->max_pkt;
969 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
9fbfb8b1 970 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
4ccfe5e0
JPM
971 if (unlikely(rc < 0))
972 goto exit;
c4307285 973 do {
60120526 974 if (likely(!tipc_sk_conn_cong(tsk))) {
9fbfb8b1 975 rc = tipc_link_xmit(buf, dnode, ref);
4ccfe5e0 976 if (likely(!rc)) {
60120526 977 tsk->sent_unacked++;
4ccfe5e0
JPM
978 sent += send;
979 if (sent == dsz)
980 break;
981 goto next;
982 }
983 if (rc == -EMSGSIZE) {
984 port->max_pkt = tipc_node_get_mtu(dnode, ref);
985 goto next;
986 }
987 if (rc != -ELINKCONG)
988 break;
50100a5e 989 tsk->link_cong = 1;
4ccfe5e0
JPM
990 }
991 rc = tipc_wait_for_sndpkt(sock, &timeo);
70452dcb
EH
992 if (rc)
993 kfree_skb_list(buf);
4ccfe5e0 994 } while (!rc);
391a6dd1 995exit:
0c3141e9
AS
996 if (iocb)
997 release_sock(sk);
4ccfe5e0 998 return sent ? sent : rc;
b97bf3fd
PL
999}
1000
c4307285 1001/**
4ccfe5e0
JPM
1002 * tipc_send_packet - send a connection-oriented message
1003 * @iocb: if NULL, indicates that socket lock is already held
b97bf3fd 1004 * @sock: socket structure
4ccfe5e0
JPM
1005 * @m: message to send
1006 * @dsz: length of data to be transmitted
c4307285 1007 *
4ccfe5e0 1008 * Used for SOCK_SEQPACKET messages.
c4307285 1009 *
4ccfe5e0 1010 * Returns the number of bytes sent on success, or errno otherwise
b97bf3fd 1011 */
4ccfe5e0
JPM
1012static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1013 struct msghdr *m, size_t dsz)
b97bf3fd 1014{
4ccfe5e0
JPM
1015 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1016 return -EMSGSIZE;
b97bf3fd 1017
4ccfe5e0 1018 return tipc_send_stream(iocb, sock, m, dsz);
b97bf3fd
PL
1019}
1020
dadebc00 1021/* tipc_sk_finish_conn - complete the setup of a connection
b97bf3fd 1022 */
dadebc00
JPM
1023static void tipc_sk_finish_conn(struct tipc_port *port, u32 peer_port,
1024 u32 peer_node)
b97bf3fd 1025{
dadebc00 1026 struct tipc_msg *msg = &port->phdr;
b97bf3fd 1027
dadebc00
JPM
1028 msg_set_destnode(msg, peer_node);
1029 msg_set_destport(msg, peer_port);
1030 msg_set_type(msg, TIPC_CONN_MSG);
1031 msg_set_lookup_scope(msg, 0);
1032 msg_set_hdr_sz(msg, SHORT_H_SIZE);
584d24b3 1033
dadebc00
JPM
1034 port->probing_interval = CONN_PROBING_INTERVAL;
1035 port->probing_state = TIPC_CONN_OK;
1036 port->connected = 1;
1037 k_start_timer(&port->timer, port->probing_interval);
1038 tipc_node_add_conn(peer_node, port->ref, peer_port);
1039 port->max_pkt = tipc_node_get_mtu(peer_node, port->ref);
b97bf3fd
PL
1040}
1041
1042/**
1043 * set_orig_addr - capture sender's address for received message
1044 * @m: descriptor for message info
1045 * @msg: received message header
c4307285 1046 *
b97bf3fd
PL
1047 * Note: Address is not captured if not requested by receiver.
1048 */
05790c64 1049static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
b97bf3fd 1050{
342dfc30 1051 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
b97bf3fd 1052
c4307285 1053 if (addr) {
b97bf3fd
PL
1054 addr->family = AF_TIPC;
1055 addr->addrtype = TIPC_ADDR_ID;
60085c3d 1056 memset(&addr->addr, 0, sizeof(addr->addr));
b97bf3fd
PL
1057 addr->addr.id.ref = msg_origport(msg);
1058 addr->addr.id.node = msg_orignode(msg);
0e65967e
AS
1059 addr->addr.name.domain = 0; /* could leave uninitialized */
1060 addr->scope = 0; /* could leave uninitialized */
b97bf3fd
PL
1061 m->msg_namelen = sizeof(struct sockaddr_tipc);
1062 }
1063}
1064
1065/**
c4307285 1066 * anc_data_recv - optionally capture ancillary data for received message
b97bf3fd
PL
1067 * @m: descriptor for message info
1068 * @msg: received message header
1069 * @tport: TIPC port associated with message
c4307285 1070 *
b97bf3fd 1071 * Note: Ancillary data is not captured if not requested by receiver.
c4307285 1072 *
b97bf3fd
PL
1073 * Returns 0 if successful, otherwise errno
1074 */
05790c64 1075static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
ae8509c4 1076 struct tipc_port *tport)
b97bf3fd
PL
1077{
1078 u32 anc_data[3];
1079 u32 err;
1080 u32 dest_type;
3546c750 1081 int has_name;
b97bf3fd
PL
1082 int res;
1083
1084 if (likely(m->msg_controllen == 0))
1085 return 0;
1086
1087 /* Optionally capture errored message object(s) */
b97bf3fd
PL
1088 err = msg ? msg_errcode(msg) : 0;
1089 if (unlikely(err)) {
1090 anc_data[0] = err;
1091 anc_data[1] = msg_data_sz(msg);
2db9983a
AS
1092 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1093 if (res)
b97bf3fd 1094 return res;
2db9983a
AS
1095 if (anc_data[1]) {
1096 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1097 msg_data(msg));
1098 if (res)
1099 return res;
1100 }
b97bf3fd
PL
1101 }
1102
1103 /* Optionally capture message destination object */
b97bf3fd
PL
1104 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1105 switch (dest_type) {
1106 case TIPC_NAMED_MSG:
3546c750 1107 has_name = 1;
b97bf3fd
PL
1108 anc_data[0] = msg_nametype(msg);
1109 anc_data[1] = msg_namelower(msg);
1110 anc_data[2] = msg_namelower(msg);
1111 break;
1112 case TIPC_MCAST_MSG:
3546c750 1113 has_name = 1;
b97bf3fd
PL
1114 anc_data[0] = msg_nametype(msg);
1115 anc_data[1] = msg_namelower(msg);
1116 anc_data[2] = msg_nameupper(msg);
1117 break;
1118 case TIPC_CONN_MSG:
3546c750 1119 has_name = (tport->conn_type != 0);
b97bf3fd
PL
1120 anc_data[0] = tport->conn_type;
1121 anc_data[1] = tport->conn_instance;
1122 anc_data[2] = tport->conn_instance;
1123 break;
1124 default:
3546c750 1125 has_name = 0;
b97bf3fd 1126 }
2db9983a
AS
1127 if (has_name) {
1128 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1129 if (res)
1130 return res;
1131 }
b97bf3fd
PL
1132
1133 return 0;
1134}
1135
739f5e4e
JPM
1136static void tipc_sk_send_ack(struct tipc_port *port, uint ack)
1137{
1138 struct sk_buff *buf = NULL;
1139 struct tipc_msg *msg;
1140 u32 peer_port = tipc_port_peerport(port);
1141 u32 dnode = tipc_port_peernode(port);
1142
1143 if (!port->connected)
1144 return;
1145 buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
1146 tipc_own_addr, peer_port, port->ref, TIPC_OK);
1147 if (!buf)
1148 return;
1149 msg = buf_msg(buf);
1150 msg_set_msgcnt(msg, ack);
1151 tipc_link_xmit(buf, dnode, msg_link_selector(msg));
1152}
1153
85d3fc94 1154static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
9bbb4ecc
YX
1155{
1156 struct sock *sk = sock->sk;
1157 DEFINE_WAIT(wait);
85d3fc94 1158 long timeo = *timeop;
9bbb4ecc
YX
1159 int err;
1160
1161 for (;;) {
1162 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
fe8e4649 1163 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
9bbb4ecc
YX
1164 if (sock->state == SS_DISCONNECTING) {
1165 err = -ENOTCONN;
1166 break;
1167 }
1168 release_sock(sk);
1169 timeo = schedule_timeout(timeo);
1170 lock_sock(sk);
1171 }
1172 err = 0;
1173 if (!skb_queue_empty(&sk->sk_receive_queue))
1174 break;
1175 err = sock_intr_errno(timeo);
1176 if (signal_pending(current))
1177 break;
1178 err = -EAGAIN;
1179 if (!timeo)
1180 break;
1181 }
1182 finish_wait(sk_sleep(sk), &wait);
85d3fc94 1183 *timeop = timeo;
9bbb4ecc
YX
1184 return err;
1185}
1186
c4307285 1187/**
247f0f3c 1188 * tipc_recvmsg - receive packet-oriented message
b97bf3fd
PL
1189 * @iocb: (unused)
1190 * @m: descriptor for message info
1191 * @buf_len: total size of user buffer area
1192 * @flags: receive flags
c4307285 1193 *
b97bf3fd
PL
1194 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1195 * If the complete message doesn't fit in user area, truncate it.
1196 *
1197 * Returns size of returned message data, errno otherwise
1198 */
247f0f3c
YX
1199static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1200 struct msghdr *m, size_t buf_len, int flags)
b97bf3fd 1201{
0c3141e9 1202 struct sock *sk = sock->sk;
58ed9442
JPM
1203 struct tipc_sock *tsk = tipc_sk(sk);
1204 struct tipc_port *port = &tsk->port;
b97bf3fd
PL
1205 struct sk_buff *buf;
1206 struct tipc_msg *msg;
9bbb4ecc 1207 long timeo;
b97bf3fd
PL
1208 unsigned int sz;
1209 u32 err;
1210 int res;
1211
0c3141e9 1212 /* Catch invalid receive requests */
b97bf3fd
PL
1213 if (unlikely(!buf_len))
1214 return -EINVAL;
1215
0c3141e9 1216 lock_sock(sk);
b97bf3fd 1217
0c3141e9
AS
1218 if (unlikely(sock->state == SS_UNCONNECTED)) {
1219 res = -ENOTCONN;
b97bf3fd
PL
1220 goto exit;
1221 }
1222
9bbb4ecc 1223 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
0c3141e9 1224restart:
b97bf3fd 1225
0c3141e9 1226 /* Look for a message in receive queue; wait if necessary */
85d3fc94 1227 res = tipc_wait_for_rcvmsg(sock, &timeo);
9bbb4ecc
YX
1228 if (res)
1229 goto exit;
b97bf3fd 1230
0c3141e9 1231 /* Look at first message in receive queue */
0c3141e9 1232 buf = skb_peek(&sk->sk_receive_queue);
b97bf3fd
PL
1233 msg = buf_msg(buf);
1234 sz = msg_data_sz(msg);
1235 err = msg_errcode(msg);
1236
b97bf3fd 1237 /* Discard an empty non-errored message & try again */
b97bf3fd 1238 if ((!sz) && (!err)) {
0c3141e9 1239 advance_rx_queue(sk);
b97bf3fd
PL
1240 goto restart;
1241 }
1242
1243 /* Capture sender's address (optional) */
b97bf3fd
PL
1244 set_orig_addr(m, msg);
1245
1246 /* Capture ancillary data (optional) */
58ed9442 1247 res = anc_data_recv(m, msg, port);
0c3141e9 1248 if (res)
b97bf3fd
PL
1249 goto exit;
1250
1251 /* Capture message data (if valid) & compute return value (always) */
b97bf3fd
PL
1252 if (!err) {
1253 if (unlikely(buf_len < sz)) {
1254 sz = buf_len;
1255 m->msg_flags |= MSG_TRUNC;
1256 }
0232fd0a
AS
1257 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1258 m->msg_iov, sz);
1259 if (res)
b97bf3fd 1260 goto exit;
b97bf3fd
PL
1261 res = sz;
1262 } else {
1263 if ((sock->state == SS_READY) ||
1264 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1265 res = 0;
1266 else
1267 res = -ECONNRESET;
1268 }
1269
1270 /* Consume received message (optional) */
b97bf3fd 1271 if (likely(!(flags & MSG_PEEK))) {
99009806 1272 if ((sock->state != SS_READY) &&
60120526 1273 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
739f5e4e 1274 tipc_sk_send_ack(port, tsk->rcv_unacked);
60120526
JPM
1275 tsk->rcv_unacked = 0;
1276 }
0c3141e9 1277 advance_rx_queue(sk);
c4307285 1278 }
b97bf3fd 1279exit:
0c3141e9 1280 release_sock(sk);
b97bf3fd
PL
1281 return res;
1282}
1283
c4307285 1284/**
247f0f3c 1285 * tipc_recv_stream - receive stream-oriented data
b97bf3fd
PL
1286 * @iocb: (unused)
1287 * @m: descriptor for message info
1288 * @buf_len: total size of user buffer area
1289 * @flags: receive flags
c4307285
YH
1290 *
1291 * Used for SOCK_STREAM messages only. If not enough data is available
b97bf3fd
PL
1292 * will optionally wait for more; never truncates data.
1293 *
1294 * Returns size of returned message data, errno otherwise
1295 */
247f0f3c
YX
1296static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1297 struct msghdr *m, size_t buf_len, int flags)
b97bf3fd 1298{
0c3141e9 1299 struct sock *sk = sock->sk;
58ed9442
JPM
1300 struct tipc_sock *tsk = tipc_sk(sk);
1301 struct tipc_port *port = &tsk->port;
b97bf3fd
PL
1302 struct sk_buff *buf;
1303 struct tipc_msg *msg;
9bbb4ecc 1304 long timeo;
b97bf3fd 1305 unsigned int sz;
3720d40b 1306 int sz_to_copy, target, needed;
b97bf3fd 1307 int sz_copied = 0;
b97bf3fd 1308 u32 err;
0c3141e9 1309 int res = 0;
b97bf3fd 1310
0c3141e9 1311 /* Catch invalid receive attempts */
b97bf3fd
PL
1312 if (unlikely(!buf_len))
1313 return -EINVAL;
1314
0c3141e9 1315 lock_sock(sk);
b97bf3fd 1316
9bbb4ecc 1317 if (unlikely(sock->state == SS_UNCONNECTED)) {
0c3141e9 1318 res = -ENOTCONN;
b97bf3fd
PL
1319 goto exit;
1320 }
1321
3720d40b 1322 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
9bbb4ecc 1323 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
b97bf3fd 1324
617d3c7a 1325restart:
0c3141e9 1326 /* Look for a message in receive queue; wait if necessary */
85d3fc94 1327 res = tipc_wait_for_rcvmsg(sock, &timeo);
9bbb4ecc
YX
1328 if (res)
1329 goto exit;
b97bf3fd 1330
0c3141e9 1331 /* Look at first message in receive queue */
0c3141e9 1332 buf = skb_peek(&sk->sk_receive_queue);
b97bf3fd
PL
1333 msg = buf_msg(buf);
1334 sz = msg_data_sz(msg);
1335 err = msg_errcode(msg);
1336
1337 /* Discard an empty non-errored message & try again */
b97bf3fd 1338 if ((!sz) && (!err)) {
0c3141e9 1339 advance_rx_queue(sk);
b97bf3fd
PL
1340 goto restart;
1341 }
1342
1343 /* Optionally capture sender's address & ancillary data of first msg */
b97bf3fd
PL
1344 if (sz_copied == 0) {
1345 set_orig_addr(m, msg);
58ed9442 1346 res = anc_data_recv(m, msg, port);
0c3141e9 1347 if (res)
b97bf3fd
PL
1348 goto exit;
1349 }
1350
1351 /* Capture message data (if valid) & compute return value (always) */
b97bf3fd 1352 if (!err) {
0232fd0a 1353 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
b97bf3fd 1354
0232fd0a 1355 sz -= offset;
b97bf3fd
PL
1356 needed = (buf_len - sz_copied);
1357 sz_to_copy = (sz <= needed) ? sz : needed;
0232fd0a
AS
1358
1359 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1360 m->msg_iov, sz_to_copy);
1361 if (res)
b97bf3fd 1362 goto exit;
0232fd0a 1363
b97bf3fd
PL
1364 sz_copied += sz_to_copy;
1365
1366 if (sz_to_copy < sz) {
1367 if (!(flags & MSG_PEEK))
0232fd0a
AS
1368 TIPC_SKB_CB(buf)->handle =
1369 (void *)(unsigned long)(offset + sz_to_copy);
b97bf3fd
PL
1370 goto exit;
1371 }
b97bf3fd
PL
1372 } else {
1373 if (sz_copied != 0)
1374 goto exit; /* can't add error msg to valid data */
1375
1376 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1377 res = 0;
1378 else
1379 res = -ECONNRESET;
1380 }
1381
1382 /* Consume received message (optional) */
b97bf3fd 1383 if (likely(!(flags & MSG_PEEK))) {
60120526 1384 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
739f5e4e 1385 tipc_sk_send_ack(port, tsk->rcv_unacked);
60120526
JPM
1386 tsk->rcv_unacked = 0;
1387 }
0c3141e9 1388 advance_rx_queue(sk);
c4307285 1389 }
b97bf3fd
PL
1390
1391 /* Loop around if more data is required */
f64f9e71
JP
1392 if ((sz_copied < buf_len) && /* didn't get all requested data */
1393 (!skb_queue_empty(&sk->sk_receive_queue) ||
3720d40b 1394 (sz_copied < target)) && /* and more is ready or required */
f64f9e71
JP
1395 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1396 (!err)) /* and haven't reached a FIN */
b97bf3fd
PL
1397 goto restart;
1398
1399exit:
0c3141e9 1400 release_sock(sk);
a3b0a5a9 1401 return sz_copied ? sz_copied : res;
b97bf3fd
PL
1402}
1403
f288bef4
YX
1404/**
1405 * tipc_write_space - wake up thread if port congestion is released
1406 * @sk: socket
1407 */
1408static void tipc_write_space(struct sock *sk)
1409{
1410 struct socket_wq *wq;
1411
1412 rcu_read_lock();
1413 wq = rcu_dereference(sk->sk_wq);
1414 if (wq_has_sleeper(wq))
1415 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1416 POLLWRNORM | POLLWRBAND);
1417 rcu_read_unlock();
1418}
1419
1420/**
1421 * tipc_data_ready - wake up threads to indicate messages have been received
1422 * @sk: socket
1423 * @len: the length of messages
1424 */
676d2369 1425static void tipc_data_ready(struct sock *sk)
f288bef4
YX
1426{
1427 struct socket_wq *wq;
1428
1429 rcu_read_lock();
1430 wq = rcu_dereference(sk->sk_wq);
1431 if (wq_has_sleeper(wq))
1432 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1433 POLLRDNORM | POLLRDBAND);
1434 rcu_read_unlock();
1435}
1436
7e6c131e
YX
1437/**
1438 * filter_connect - Handle all incoming messages for a connection-based socket
58ed9442 1439 * @tsk: TIPC socket
7e6c131e
YX
1440 * @msg: message
1441 *
e4de5fab 1442 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
7e6c131e 1443 */
e4de5fab 1444static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
7e6c131e 1445{
58ed9442
JPM
1446 struct sock *sk = &tsk->sk;
1447 struct tipc_port *port = &tsk->port;
8826cde6 1448 struct socket *sock = sk->sk_socket;
7e6c131e 1449 struct tipc_msg *msg = buf_msg(*buf);
8826cde6 1450
e4de5fab 1451 int retval = -TIPC_ERR_NO_PORT;
7e6c131e
YX
1452
1453 if (msg_mcast(msg))
1454 return retval;
1455
1456 switch ((int)sock->state) {
1457 case SS_CONNECTED:
1458 /* Accept only connection-based messages sent by peer */
8826cde6 1459 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
7e6c131e
YX
1460 if (unlikely(msg_errcode(msg))) {
1461 sock->state = SS_DISCONNECTING;
dadebc00
JPM
1462 port->connected = 0;
1463 /* let timer expire on it's own */
1464 tipc_node_remove_conn(tipc_port_peernode(port),
1465 port->ref);
7e6c131e
YX
1466 }
1467 retval = TIPC_OK;
1468 }
1469 break;
1470 case SS_CONNECTING:
1471 /* Accept only ACK or NACK message */
dadebc00
JPM
1472
1473 if (unlikely(!msg_connected(msg)))
1474 break;
1475
584d24b3
YX
1476 if (unlikely(msg_errcode(msg))) {
1477 sock->state = SS_DISCONNECTING;
2c8d8518 1478 sk->sk_err = ECONNREFUSED;
584d24b3
YX
1479 retval = TIPC_OK;
1480 break;
1481 }
1482
dadebc00 1483 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
584d24b3 1484 sock->state = SS_DISCONNECTING;
dadebc00 1485 sk->sk_err = EINVAL;
7e6c131e 1486 retval = TIPC_OK;
584d24b3
YX
1487 break;
1488 }
1489
dadebc00
JPM
1490 tipc_sk_finish_conn(port, msg_origport(msg), msg_orignode(msg));
1491 msg_set_importance(&port->phdr, msg_importance(msg));
1492 sock->state = SS_CONNECTED;
1493
584d24b3
YX
1494 /* If an incoming message is an 'ACK-', it should be
1495 * discarded here because it doesn't contain useful
1496 * data. In addition, we should try to wake up
1497 * connect() routine if sleeping.
1498 */
1499 if (msg_data_sz(msg) == 0) {
1500 kfree_skb(*buf);
1501 *buf = NULL;
1502 if (waitqueue_active(sk_sleep(sk)))
1503 wake_up_interruptible(sk_sleep(sk));
1504 }
1505 retval = TIPC_OK;
7e6c131e
YX
1506 break;
1507 case SS_LISTENING:
1508 case SS_UNCONNECTED:
1509 /* Accept only SYN message */
1510 if (!msg_connected(msg) && !(msg_errcode(msg)))
1511 retval = TIPC_OK;
1512 break;
1513 case SS_DISCONNECTING:
1514 break;
1515 default:
1516 pr_err("Unknown socket state %u\n", sock->state);
1517 }
1518 return retval;
1519}
1520
aba79f33
YX
1521/**
1522 * rcvbuf_limit - get proper overload limit of socket receive queue
1523 * @sk: socket
1524 * @buf: message
1525 *
1526 * For all connection oriented messages, irrespective of importance,
1527 * the default overload value (i.e. 67MB) is set as limit.
1528 *
1529 * For all connectionless messages, by default new queue limits are
1530 * as belows:
1531 *
cc79dd1b
YX
1532 * TIPC_LOW_IMPORTANCE (4 MB)
1533 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1534 * TIPC_HIGH_IMPORTANCE (16 MB)
1535 * TIPC_CRITICAL_IMPORTANCE (32 MB)
aba79f33
YX
1536 *
1537 * Returns overload limit according to corresponding message importance
1538 */
1539static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1540{
1541 struct tipc_msg *msg = buf_msg(buf);
aba79f33
YX
1542
1543 if (msg_connected(msg))
0cee6bbe 1544 return sysctl_tipc_rmem[2];
1545
1546 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1547 msg_importance(msg);
aba79f33
YX
1548}
1549
c4307285 1550/**
0c3141e9
AS
1551 * filter_rcv - validate incoming message
1552 * @sk: socket
b97bf3fd 1553 * @buf: message
c4307285 1554 *
0c3141e9
AS
1555 * Enqueues message on receive queue if acceptable; optionally handles
1556 * disconnect indication for a connected socket.
1557 *
1558 * Called with socket lock already taken; port lock may also be taken.
c4307285 1559 *
e4de5fab 1560 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
ac0074ee 1561 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
b97bf3fd 1562 */
e4de5fab 1563static int filter_rcv(struct sock *sk, struct sk_buff *buf)
b97bf3fd 1564{
0c3141e9 1565 struct socket *sock = sk->sk_socket;
58ed9442 1566 struct tipc_sock *tsk = tipc_sk(sk);
b97bf3fd 1567 struct tipc_msg *msg = buf_msg(buf);
aba79f33 1568 unsigned int limit = rcvbuf_limit(sk, buf);
ac0074ee 1569 u32 onode;
e4de5fab 1570 int rc = TIPC_OK;
b97bf3fd 1571
ac0074ee
JPM
1572 if (unlikely(msg_user(msg) == CONN_MANAGER))
1573 return tipc_sk_proto_rcv(tsk, &onode, buf);
ec8a2e56 1574
50100a5e
JPM
1575 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1576 kfree_skb(buf);
1577 tsk->link_cong = 0;
1578 sk->sk_write_space(sk);
1579 return TIPC_OK;
1580 }
1581
b97bf3fd 1582 /* Reject message if it is wrong sort of message for socket */
aad58547 1583 if (msg_type(msg) > TIPC_DIRECT_MSG)
e4de5fab 1584 return -TIPC_ERR_NO_PORT;
0c3141e9 1585
b97bf3fd 1586 if (sock->state == SS_READY) {
b29f1428 1587 if (msg_connected(msg))
e4de5fab 1588 return -TIPC_ERR_NO_PORT;
b97bf3fd 1589 } else {
e4de5fab
JPM
1590 rc = filter_connect(tsk, &buf);
1591 if (rc != TIPC_OK || buf == NULL)
1592 return rc;
b97bf3fd
PL
1593 }
1594
1595 /* Reject message if there isn't room to queue it */
aba79f33 1596 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
e4de5fab 1597 return -TIPC_ERR_OVERLOAD;
b97bf3fd 1598
aba79f33 1599 /* Enqueue message */
40682432 1600 TIPC_SKB_CB(buf)->handle = NULL;
0c3141e9 1601 __skb_queue_tail(&sk->sk_receive_queue, buf);
aba79f33 1602 skb_set_owner_r(buf, sk);
0c3141e9 1603
676d2369 1604 sk->sk_data_ready(sk);
0c3141e9
AS
1605 return TIPC_OK;
1606}
b97bf3fd 1607
0c3141e9 1608/**
4f4482dc 1609 * tipc_backlog_rcv - handle incoming message from backlog queue
0c3141e9
AS
1610 * @sk: socket
1611 * @buf: message
1612 *
1613 * Caller must hold socket lock, but not port lock.
1614 *
1615 * Returns 0
1616 */
4f4482dc 1617static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
0c3141e9 1618{
e4de5fab 1619 int rc;
8db1bae3 1620 u32 onode;
4f4482dc 1621 struct tipc_sock *tsk = tipc_sk(sk);
02c00c2a 1622 uint truesize = buf->truesize;
0c3141e9 1623
e4de5fab 1624 rc = filter_rcv(sk, buf);
4f4482dc 1625
ac0074ee
JPM
1626 if (likely(!rc)) {
1627 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1628 atomic_add(truesize, &tsk->dupl_rcvcnt);
1629 return 0;
1630 }
1631
1632 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1633 return 0;
1634
9fbfb8b1 1635 tipc_link_xmit(buf, onode, 0);
4f4482dc 1636
0c3141e9
AS
1637 return 0;
1638}
1639
1640/**
24be34b5 1641 * tipc_sk_rcv - handle incoming message
9816f061
JPM
1642 * @buf: buffer containing arriving message
1643 * Consumes buffer
1644 * Returns 0 if success, or errno: -EHOSTUNREACH
0c3141e9 1645 */
9816f061 1646int tipc_sk_rcv(struct sk_buff *buf)
0c3141e9 1647{
9816f061
JPM
1648 struct tipc_sock *tsk;
1649 struct tipc_port *port;
1650 struct sock *sk;
1651 u32 dport = msg_destport(buf_msg(buf));
e4de5fab 1652 int rc = TIPC_OK;
4f4482dc 1653 uint limit;
8db1bae3 1654 u32 dnode;
9816f061 1655
5a379074 1656 /* Validate destination and message */
9816f061
JPM
1657 port = tipc_port_lock(dport);
1658 if (unlikely(!port)) {
5a379074 1659 rc = tipc_msg_eval(buf, &dnode);
9816f061
JPM
1660 goto exit;
1661 }
1662
1663 tsk = tipc_port_to_sock(port);
1664 sk = &tsk->sk;
1665
1666 /* Queue message */
0c3141e9 1667 bh_lock_sock(sk);
9816f061 1668
0c3141e9 1669 if (!sock_owned_by_user(sk)) {
e4de5fab 1670 rc = filter_rcv(sk, buf);
0c3141e9 1671 } else {
4f4482dc
JPM
1672 if (sk->sk_backlog.len == 0)
1673 atomic_set(&tsk->dupl_rcvcnt, 0);
1674 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1675 if (sk_add_backlog(sk, buf, limit))
e4de5fab 1676 rc = -TIPC_ERR_OVERLOAD;
0c3141e9
AS
1677 }
1678 bh_unlock_sock(sk);
9816f061 1679 tipc_port_unlock(port);
0c3141e9 1680
e4de5fab 1681 if (likely(!rc))
9816f061
JPM
1682 return 0;
1683exit:
5a379074 1684 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
8db1bae3 1685 return -EHOSTUNREACH;
5a379074 1686
9fbfb8b1 1687 tipc_link_xmit(buf, dnode, 0);
5a379074 1688 return (rc < 0) ? -EHOSTUNREACH : 0;
b97bf3fd
PL
1689}
1690
78eb3a53
YX
1691static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1692{
1693 struct sock *sk = sock->sk;
1694 DEFINE_WAIT(wait);
1695 int done;
1696
1697 do {
1698 int err = sock_error(sk);
1699 if (err)
1700 return err;
1701 if (!*timeo_p)
1702 return -ETIMEDOUT;
1703 if (signal_pending(current))
1704 return sock_intr_errno(*timeo_p);
1705
1706 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1707 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1708 finish_wait(sk_sleep(sk), &wait);
1709 } while (!done);
1710 return 0;
1711}
1712
b97bf3fd 1713/**
247f0f3c 1714 * tipc_connect - establish a connection to another TIPC port
b97bf3fd
PL
1715 * @sock: socket structure
1716 * @dest: socket address for destination port
1717 * @destlen: size of socket address data structure
0c3141e9 1718 * @flags: file-related flags associated with socket
b97bf3fd
PL
1719 *
1720 * Returns 0 on success, errno otherwise
1721 */
247f0f3c
YX
1722static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1723 int destlen, int flags)
b97bf3fd 1724{
0c3141e9 1725 struct sock *sk = sock->sk;
b89741a0
AS
1726 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1727 struct msghdr m = {NULL,};
78eb3a53
YX
1728 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1729 socket_state previous;
b89741a0
AS
1730 int res;
1731
0c3141e9
AS
1732 lock_sock(sk);
1733
b89741a0 1734 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
0c3141e9
AS
1735 if (sock->state == SS_READY) {
1736 res = -EOPNOTSUPP;
1737 goto exit;
1738 }
b89741a0 1739
b89741a0
AS
1740 /*
1741 * Reject connection attempt using multicast address
1742 *
1743 * Note: send_msg() validates the rest of the address fields,
1744 * so there's no need to do it here
1745 */
0c3141e9
AS
1746 if (dst->addrtype == TIPC_ADDR_MCAST) {
1747 res = -EINVAL;
1748 goto exit;
1749 }
1750
78eb3a53 1751 previous = sock->state;
584d24b3
YX
1752 switch (sock->state) {
1753 case SS_UNCONNECTED:
1754 /* Send a 'SYN-' to destination */
1755 m.msg_name = dest;
1756 m.msg_namelen = destlen;
1757
1758 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1759 * indicate send_msg() is never blocked.
1760 */
1761 if (!timeout)
1762 m.msg_flags = MSG_DONTWAIT;
1763
247f0f3c 1764 res = tipc_sendmsg(NULL, sock, &m, 0);
584d24b3
YX
1765 if ((res < 0) && (res != -EWOULDBLOCK))
1766 goto exit;
1767
1768 /* Just entered SS_CONNECTING state; the only
1769 * difference is that return value in non-blocking
1770 * case is EINPROGRESS, rather than EALREADY.
1771 */
1772 res = -EINPROGRESS;
584d24b3 1773 case SS_CONNECTING:
78eb3a53
YX
1774 if (previous == SS_CONNECTING)
1775 res = -EALREADY;
1776 if (!timeout)
1777 goto exit;
1778 timeout = msecs_to_jiffies(timeout);
1779 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1780 res = tipc_wait_for_connect(sock, &timeout);
584d24b3
YX
1781 break;
1782 case SS_CONNECTED:
1783 res = -EISCONN;
1784 break;
1785 default:
1786 res = -EINVAL;
78eb3a53 1787 break;
b89741a0 1788 }
0c3141e9
AS
1789exit:
1790 release_sock(sk);
b89741a0 1791 return res;
b97bf3fd
PL
1792}
1793
c4307285 1794/**
247f0f3c 1795 * tipc_listen - allow socket to listen for incoming connections
b97bf3fd
PL
1796 * @sock: socket structure
1797 * @len: (unused)
c4307285 1798 *
b97bf3fd
PL
1799 * Returns 0 on success, errno otherwise
1800 */
247f0f3c 1801static int tipc_listen(struct socket *sock, int len)
b97bf3fd 1802{
0c3141e9
AS
1803 struct sock *sk = sock->sk;
1804 int res;
1805
1806 lock_sock(sk);
b97bf3fd 1807
245f3d34 1808 if (sock->state != SS_UNCONNECTED)
0c3141e9
AS
1809 res = -EINVAL;
1810 else {
1811 sock->state = SS_LISTENING;
1812 res = 0;
1813 }
1814
1815 release_sock(sk);
1816 return res;
b97bf3fd
PL
1817}
1818
6398e23c
YX
1819static int tipc_wait_for_accept(struct socket *sock, long timeo)
1820{
1821 struct sock *sk = sock->sk;
1822 DEFINE_WAIT(wait);
1823 int err;
1824
1825 /* True wake-one mechanism for incoming connections: only
1826 * one process gets woken up, not the 'whole herd'.
1827 * Since we do not 'race & poll' for established sockets
1828 * anymore, the common case will execute the loop only once.
1829 */
1830 for (;;) {
1831 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1832 TASK_INTERRUPTIBLE);
fe8e4649 1833 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
6398e23c
YX
1834 release_sock(sk);
1835 timeo = schedule_timeout(timeo);
1836 lock_sock(sk);
1837 }
1838 err = 0;
1839 if (!skb_queue_empty(&sk->sk_receive_queue))
1840 break;
1841 err = -EINVAL;
1842 if (sock->state != SS_LISTENING)
1843 break;
1844 err = sock_intr_errno(timeo);
1845 if (signal_pending(current))
1846 break;
1847 err = -EAGAIN;
1848 if (!timeo)
1849 break;
1850 }
1851 finish_wait(sk_sleep(sk), &wait);
1852 return err;
1853}
1854
c4307285 1855/**
247f0f3c 1856 * tipc_accept - wait for connection request
b97bf3fd
PL
1857 * @sock: listening socket
1858 * @newsock: new socket that is to be connected
1859 * @flags: file-related flags associated with socket
c4307285 1860 *
b97bf3fd
PL
1861 * Returns 0 on success, errno otherwise
1862 */
247f0f3c 1863static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
b97bf3fd 1864{
0fef8f20 1865 struct sock *new_sk, *sk = sock->sk;
b97bf3fd 1866 struct sk_buff *buf;
8826cde6 1867 struct tipc_port *new_port;
0fef8f20 1868 struct tipc_msg *msg;
6398e23c 1869 long timeo;
0c3141e9 1870 int res;
b97bf3fd 1871
0c3141e9 1872 lock_sock(sk);
b97bf3fd 1873
0c3141e9
AS
1874 if (sock->state != SS_LISTENING) {
1875 res = -EINVAL;
b97bf3fd
PL
1876 goto exit;
1877 }
6398e23c
YX
1878 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1879 res = tipc_wait_for_accept(sock, timeo);
1880 if (res)
1881 goto exit;
0c3141e9
AS
1882
1883 buf = skb_peek(&sk->sk_receive_queue);
1884
c5fa7b3c 1885 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
0fef8f20
PG
1886 if (res)
1887 goto exit;
b97bf3fd 1888
0fef8f20 1889 new_sk = new_sock->sk;
58ed9442 1890 new_port = &tipc_sk(new_sk)->port;
0fef8f20 1891 msg = buf_msg(buf);
b97bf3fd 1892
0fef8f20
PG
1893 /* we lock on new_sk; but lockdep sees the lock on sk */
1894 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
1895
1896 /*
1897 * Reject any stray messages received by new socket
1898 * before the socket lock was taken (very, very unlikely)
1899 */
1900 reject_rx_queue(new_sk);
1901
1902 /* Connect new socket to it's peer */
dadebc00 1903 tipc_sk_finish_conn(new_port, msg_origport(msg), msg_orignode(msg));
0fef8f20
PG
1904 new_sock->state = SS_CONNECTED;
1905
3b4f302d 1906 tipc_port_set_importance(new_port, msg_importance(msg));
0fef8f20 1907 if (msg_named(msg)) {
8826cde6
JPM
1908 new_port->conn_type = msg_nametype(msg);
1909 new_port->conn_instance = msg_nameinst(msg);
b97bf3fd 1910 }
0fef8f20
PG
1911
1912 /*
1913 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1914 * Respond to 'SYN+' by queuing it on new socket.
1915 */
1916 if (!msg_data_sz(msg)) {
1917 struct msghdr m = {NULL,};
1918
1919 advance_rx_queue(sk);
247f0f3c 1920 tipc_send_packet(NULL, new_sock, &m, 0);
0fef8f20
PG
1921 } else {
1922 __skb_dequeue(&sk->sk_receive_queue);
1923 __skb_queue_head(&new_sk->sk_receive_queue, buf);
aba79f33 1924 skb_set_owner_r(buf, new_sk);
0fef8f20
PG
1925 }
1926 release_sock(new_sk);
b97bf3fd 1927exit:
0c3141e9 1928 release_sock(sk);
b97bf3fd
PL
1929 return res;
1930}
1931
1932/**
247f0f3c 1933 * tipc_shutdown - shutdown socket connection
b97bf3fd 1934 * @sock: socket structure
e247a8f5 1935 * @how: direction to close (must be SHUT_RDWR)
b97bf3fd
PL
1936 *
1937 * Terminates connection (if necessary), then purges socket's receive queue.
c4307285 1938 *
b97bf3fd
PL
1939 * Returns 0 on success, errno otherwise
1940 */
247f0f3c 1941static int tipc_shutdown(struct socket *sock, int how)
b97bf3fd 1942{
0c3141e9 1943 struct sock *sk = sock->sk;
58ed9442
JPM
1944 struct tipc_sock *tsk = tipc_sk(sk);
1945 struct tipc_port *port = &tsk->port;
b97bf3fd 1946 struct sk_buff *buf;
80e44c22 1947 u32 dnode;
b97bf3fd
PL
1948 int res;
1949
e247a8f5
AS
1950 if (how != SHUT_RDWR)
1951 return -EINVAL;
b97bf3fd 1952
0c3141e9 1953 lock_sock(sk);
b97bf3fd
PL
1954
1955 switch (sock->state) {
0c3141e9 1956 case SS_CONNECTING:
b97bf3fd
PL
1957 case SS_CONNECTED:
1958
b97bf3fd 1959restart:
617d3c7a 1960 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
0c3141e9
AS
1961 buf = __skb_dequeue(&sk->sk_receive_queue);
1962 if (buf) {
40682432 1963 if (TIPC_SKB_CB(buf)->handle != NULL) {
5f6d9123 1964 kfree_skb(buf);
b97bf3fd
PL
1965 goto restart;
1966 }
80e44c22
JPM
1967 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
1968 tipc_link_xmit(buf, dnode, port->ref);
dadebc00 1969 tipc_node_remove_conn(dnode, port->ref);
0c3141e9 1970 } else {
80e44c22
JPM
1971 dnode = tipc_port_peernode(port);
1972 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
1973 TIPC_CONN_MSG, SHORT_H_SIZE,
1974 0, dnode, tipc_own_addr,
1975 tipc_port_peerport(port),
1976 port->ref, TIPC_CONN_SHUTDOWN);
1977 tipc_link_xmit(buf, dnode, port->ref);
b97bf3fd 1978 }
dadebc00 1979 port->connected = 0;
0c3141e9 1980 sock->state = SS_DISCONNECTING;
dadebc00 1981 tipc_node_remove_conn(dnode, port->ref);
b97bf3fd
PL
1982 /* fall through */
1983
1984 case SS_DISCONNECTING:
1985
75031151 1986 /* Discard any unreceived messages */
57467e56 1987 __skb_queue_purge(&sk->sk_receive_queue);
75031151
YX
1988
1989 /* Wake up anyone sleeping in poll */
1990 sk->sk_state_change(sk);
b97bf3fd
PL
1991 res = 0;
1992 break;
1993
1994 default:
1995 res = -ENOTCONN;
1996 }
1997
0c3141e9 1998 release_sock(sk);
b97bf3fd
PL
1999 return res;
2000}
2001
57289015
JPM
2002static void tipc_sk_timeout(unsigned long ref)
2003{
2004 struct tipc_port *port = tipc_port_lock(ref);
2005 struct tipc_sock *tsk;
2006 struct sock *sk;
2007 struct sk_buff *buf = NULL;
2008 struct tipc_msg *msg = NULL;
2009 u32 peer_port, peer_node;
2010
2011 if (!port)
2012 return;
2013
2014 if (!port->connected) {
2015 tipc_port_unlock(port);
2016 return;
2017 }
2018 tsk = tipc_port_to_sock(port);
2019 sk = &tsk->sk;
2020 bh_lock_sock(sk);
2021 peer_port = tipc_port_peerport(port);
2022 peer_node = tipc_port_peernode(port);
2023
2024 if (port->probing_state == TIPC_CONN_PROBING) {
2025 /* Previous probe not answered -> self abort */
2026 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2027 SHORT_H_SIZE, 0, tipc_own_addr,
2028 peer_node, ref, peer_port,
2029 TIPC_ERR_NO_PORT);
2030 } else {
2031 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2032 0, peer_node, tipc_own_addr,
2033 peer_port, ref, TIPC_OK);
2034 port->probing_state = TIPC_CONN_PROBING;
2035 k_start_timer(&port->timer, port->probing_interval);
2036 }
2037 bh_unlock_sock(sk);
2038 tipc_port_unlock(port);
2039 if (!buf)
2040 return;
2041
2042 msg = buf_msg(buf);
2043 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
2044}
2045
b97bf3fd 2046/**
247f0f3c 2047 * tipc_setsockopt - set socket option
b97bf3fd
PL
2048 * @sock: socket structure
2049 * @lvl: option level
2050 * @opt: option identifier
2051 * @ov: pointer to new option value
2052 * @ol: length of option value
c4307285
YH
2053 *
2054 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
b97bf3fd 2055 * (to ease compatibility).
c4307285 2056 *
b97bf3fd
PL
2057 * Returns 0 on success, errno otherwise
2058 */
247f0f3c
YX
2059static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2060 char __user *ov, unsigned int ol)
b97bf3fd 2061{
0c3141e9 2062 struct sock *sk = sock->sk;
58ed9442
JPM
2063 struct tipc_sock *tsk = tipc_sk(sk);
2064 struct tipc_port *port = &tsk->port;
b97bf3fd
PL
2065 u32 value;
2066 int res;
2067
c4307285
YH
2068 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2069 return 0;
b97bf3fd
PL
2070 if (lvl != SOL_TIPC)
2071 return -ENOPROTOOPT;
2072 if (ol < sizeof(value))
2073 return -EINVAL;
2db9983a
AS
2074 res = get_user(value, (u32 __user *)ov);
2075 if (res)
b97bf3fd
PL
2076 return res;
2077
0c3141e9 2078 lock_sock(sk);
c4307285 2079
b97bf3fd
PL
2080 switch (opt) {
2081 case TIPC_IMPORTANCE:
ac32c7f7 2082 res = tipc_port_set_importance(port, value);
b97bf3fd
PL
2083 break;
2084 case TIPC_SRC_DROPPABLE:
2085 if (sock->type != SOCK_STREAM)
58ed9442 2086 tipc_port_set_unreliable(port, value);
c4307285 2087 else
b97bf3fd
PL
2088 res = -ENOPROTOOPT;
2089 break;
2090 case TIPC_DEST_DROPPABLE:
58ed9442 2091 tipc_port_set_unreturnable(port, value);
b97bf3fd
PL
2092 break;
2093 case TIPC_CONN_TIMEOUT:
a0f40f02 2094 tipc_sk(sk)->conn_timeout = value;
0c3141e9 2095 /* no need to set "res", since already 0 at this point */
b97bf3fd
PL
2096 break;
2097 default:
2098 res = -EINVAL;
2099 }
2100
0c3141e9
AS
2101 release_sock(sk);
2102
b97bf3fd
PL
2103 return res;
2104}
2105
2106/**
247f0f3c 2107 * tipc_getsockopt - get socket option
b97bf3fd
PL
2108 * @sock: socket structure
2109 * @lvl: option level
2110 * @opt: option identifier
2111 * @ov: receptacle for option value
2112 * @ol: receptacle for length of option value
c4307285
YH
2113 *
2114 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
b97bf3fd 2115 * (to ease compatibility).
c4307285 2116 *
b97bf3fd
PL
2117 * Returns 0 on success, errno otherwise
2118 */
247f0f3c
YX
2119static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2120 char __user *ov, int __user *ol)
b97bf3fd 2121{
0c3141e9 2122 struct sock *sk = sock->sk;
58ed9442
JPM
2123 struct tipc_sock *tsk = tipc_sk(sk);
2124 struct tipc_port *port = &tsk->port;
c4307285 2125 int len;
b97bf3fd 2126 u32 value;
c4307285 2127 int res;
b97bf3fd 2128
c4307285
YH
2129 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2130 return put_user(0, ol);
b97bf3fd
PL
2131 if (lvl != SOL_TIPC)
2132 return -ENOPROTOOPT;
2db9983a
AS
2133 res = get_user(len, ol);
2134 if (res)
c4307285 2135 return res;
b97bf3fd 2136
0c3141e9 2137 lock_sock(sk);
b97bf3fd
PL
2138
2139 switch (opt) {
2140 case TIPC_IMPORTANCE:
58ed9442 2141 value = tipc_port_importance(port);
b97bf3fd
PL
2142 break;
2143 case TIPC_SRC_DROPPABLE:
58ed9442 2144 value = tipc_port_unreliable(port);
b97bf3fd
PL
2145 break;
2146 case TIPC_DEST_DROPPABLE:
58ed9442 2147 value = tipc_port_unreturnable(port);
b97bf3fd
PL
2148 break;
2149 case TIPC_CONN_TIMEOUT:
a0f40f02 2150 value = tipc_sk(sk)->conn_timeout;
0c3141e9 2151 /* no need to set "res", since already 0 at this point */
b97bf3fd 2152 break;
0e65967e 2153 case TIPC_NODE_RECVQ_DEPTH:
9da3d475 2154 value = 0; /* was tipc_queue_size, now obsolete */
6650613d 2155 break;
0e65967e 2156 case TIPC_SOCK_RECVQ_DEPTH:
6650613d 2157 value = skb_queue_len(&sk->sk_receive_queue);
2158 break;
b97bf3fd
PL
2159 default:
2160 res = -EINVAL;
2161 }
2162
0c3141e9
AS
2163 release_sock(sk);
2164
25860c3b
PG
2165 if (res)
2166 return res; /* "get" failed */
b97bf3fd 2167
25860c3b
PG
2168 if (len < sizeof(value))
2169 return -EINVAL;
2170
2171 if (copy_to_user(ov, &value, sizeof(value)))
2172 return -EFAULT;
2173
2174 return put_user(sizeof(value), ol);
b97bf3fd
PL
2175}
2176
52f50ce5 2177static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
78acb1f9
EH
2178{
2179 struct tipc_sioc_ln_req lnr;
2180 void __user *argp = (void __user *)arg;
2181
2182 switch (cmd) {
2183 case SIOCGETLINKNAME:
2184 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2185 return -EFAULT;
2186 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2187 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2188 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2189 return -EFAULT;
2190 return 0;
2191 }
2192 return -EADDRNOTAVAIL;
78acb1f9
EH
2193 default:
2194 return -ENOIOCTLCMD;
2195 }
2196}
2197
ae86b9e3
BH
2198/* Protocol switches for the various types of TIPC sockets */
2199
bca65eae 2200static const struct proto_ops msg_ops = {
0e65967e 2201 .owner = THIS_MODULE,
b97bf3fd 2202 .family = AF_TIPC,
247f0f3c
YX
2203 .release = tipc_release,
2204 .bind = tipc_bind,
2205 .connect = tipc_connect,
5eee6a6d 2206 .socketpair = sock_no_socketpair,
245f3d34 2207 .accept = sock_no_accept,
247f0f3c
YX
2208 .getname = tipc_getname,
2209 .poll = tipc_poll,
78acb1f9 2210 .ioctl = tipc_ioctl,
245f3d34 2211 .listen = sock_no_listen,
247f0f3c
YX
2212 .shutdown = tipc_shutdown,
2213 .setsockopt = tipc_setsockopt,
2214 .getsockopt = tipc_getsockopt,
2215 .sendmsg = tipc_sendmsg,
2216 .recvmsg = tipc_recvmsg,
8238745a
YH
2217 .mmap = sock_no_mmap,
2218 .sendpage = sock_no_sendpage
b97bf3fd
PL
2219};
2220
bca65eae 2221static const struct proto_ops packet_ops = {
0e65967e 2222 .owner = THIS_MODULE,
b97bf3fd 2223 .family = AF_TIPC,
247f0f3c
YX
2224 .release = tipc_release,
2225 .bind = tipc_bind,
2226 .connect = tipc_connect,
5eee6a6d 2227 .socketpair = sock_no_socketpair,
247f0f3c
YX
2228 .accept = tipc_accept,
2229 .getname = tipc_getname,
2230 .poll = tipc_poll,
78acb1f9 2231 .ioctl = tipc_ioctl,
247f0f3c
YX
2232 .listen = tipc_listen,
2233 .shutdown = tipc_shutdown,
2234 .setsockopt = tipc_setsockopt,
2235 .getsockopt = tipc_getsockopt,
2236 .sendmsg = tipc_send_packet,
2237 .recvmsg = tipc_recvmsg,
8238745a
YH
2238 .mmap = sock_no_mmap,
2239 .sendpage = sock_no_sendpage
b97bf3fd
PL
2240};
2241
bca65eae 2242static const struct proto_ops stream_ops = {
0e65967e 2243 .owner = THIS_MODULE,
b97bf3fd 2244 .family = AF_TIPC,
247f0f3c
YX
2245 .release = tipc_release,
2246 .bind = tipc_bind,
2247 .connect = tipc_connect,
5eee6a6d 2248 .socketpair = sock_no_socketpair,
247f0f3c
YX
2249 .accept = tipc_accept,
2250 .getname = tipc_getname,
2251 .poll = tipc_poll,
78acb1f9 2252 .ioctl = tipc_ioctl,
247f0f3c
YX
2253 .listen = tipc_listen,
2254 .shutdown = tipc_shutdown,
2255 .setsockopt = tipc_setsockopt,
2256 .getsockopt = tipc_getsockopt,
2257 .sendmsg = tipc_send_stream,
2258 .recvmsg = tipc_recv_stream,
8238745a
YH
2259 .mmap = sock_no_mmap,
2260 .sendpage = sock_no_sendpage
b97bf3fd
PL
2261};
2262
bca65eae 2263static const struct net_proto_family tipc_family_ops = {
0e65967e 2264 .owner = THIS_MODULE,
b97bf3fd 2265 .family = AF_TIPC,
c5fa7b3c 2266 .create = tipc_sk_create
b97bf3fd
PL
2267};
2268
2269static struct proto tipc_proto = {
2270 .name = "TIPC",
2271 .owner = THIS_MODULE,
cc79dd1b
YX
2272 .obj_size = sizeof(struct tipc_sock),
2273 .sysctl_rmem = sysctl_tipc_rmem
b97bf3fd
PL
2274};
2275
c5fa7b3c
YX
2276static struct proto tipc_proto_kern = {
2277 .name = "TIPC",
2278 .obj_size = sizeof(struct tipc_sock),
2279 .sysctl_rmem = sysctl_tipc_rmem
2280};
2281
b97bf3fd 2282/**
4323add6 2283 * tipc_socket_init - initialize TIPC socket interface
c4307285 2284 *
b97bf3fd
PL
2285 * Returns 0 on success, errno otherwise
2286 */
4323add6 2287int tipc_socket_init(void)
b97bf3fd
PL
2288{
2289 int res;
2290
c4307285 2291 res = proto_register(&tipc_proto, 1);
b97bf3fd 2292 if (res) {
2cf8aa19 2293 pr_err("Failed to register TIPC protocol type\n");
b97bf3fd
PL
2294 goto out;
2295 }
2296
2297 res = sock_register(&tipc_family_ops);
2298 if (res) {
2cf8aa19 2299 pr_err("Failed to register TIPC socket type\n");
b97bf3fd
PL
2300 proto_unregister(&tipc_proto);
2301 goto out;
2302 }
b97bf3fd
PL
2303 out:
2304 return res;
2305}
2306
2307/**
4323add6 2308 * tipc_socket_stop - stop TIPC socket interface
b97bf3fd 2309 */
4323add6 2310void tipc_socket_stop(void)
b97bf3fd 2311{
b97bf3fd
PL
2312 sock_unregister(tipc_family_ops.family);
2313 proto_unregister(&tipc_proto);
2314}
This page took 1.381552 seconds and 5 git commands to generate.