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