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