tipc: convert topology server to use new server facility
[deliverable/linux.git] / net / tipc / socket.c
... / ...
CommitLineData
1/*
2 * net/tipc/socket.c: TIPC socket API
3 *
4 * Copyright (c) 2001-2007, 2012 Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
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.
19 *
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
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "port.h"
39
40#include <linux/export.h>
41#include <net/sock.h>
42
43#define SS_LISTENING -1 /* socket is listening */
44#define SS_READY -2 /* socket is connectionless */
45
46#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
47
48struct tipc_sock {
49 struct sock sk;
50 struct tipc_port *p;
51 struct tipc_portid peer_name;
52 unsigned int conn_timeout;
53};
54
55#define tipc_sk(sk) ((struct tipc_sock *)(sk))
56#define tipc_sk_port(sk) (tipc_sk(sk)->p)
57
58#define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
59 (sock->state == SS_DISCONNECTING))
60
61static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
62static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
63static void wakeupdispatch(struct tipc_port *tport);
64static void tipc_data_ready(struct sock *sk, int len);
65static void tipc_write_space(struct sock *sk);
66static int release(struct socket *sock);
67static int accept(struct socket *sock, struct socket *new_sock, int flags);
68
69static const struct proto_ops packet_ops;
70static const struct proto_ops stream_ops;
71static const struct proto_ops msg_ops;
72
73static struct proto tipc_proto;
74static struct proto tipc_proto_kern;
75
76static int sockets_enabled;
77
78/*
79 * Revised TIPC socket locking policy:
80 *
81 * Most socket operations take the standard socket lock when they start
82 * and hold it until they finish (or until they need to sleep). Acquiring
83 * this lock grants the owner exclusive access to the fields of the socket
84 * data structures, with the exception of the backlog queue. A few socket
85 * operations can be done without taking the socket lock because they only
86 * read socket information that never changes during the life of the socket.
87 *
88 * Socket operations may acquire the lock for the associated TIPC port if they
89 * need to perform an operation on the port. If any routine needs to acquire
90 * both the socket lock and the port lock it must take the socket lock first
91 * to avoid the risk of deadlock.
92 *
93 * The dispatcher handling incoming messages cannot grab the socket lock in
94 * the standard fashion, since invoked it runs at the BH level and cannot block.
95 * Instead, it checks to see if the socket lock is currently owned by someone,
96 * and either handles the message itself or adds it to the socket's backlog
97 * queue; in the latter case the queued message is processed once the process
98 * owning the socket lock releases it.
99 *
100 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
101 * the problem of a blocked socket operation preventing any other operations
102 * from occurring. However, applications must be careful if they have
103 * multiple threads trying to send (or receive) on the same socket, as these
104 * operations might interfere with each other. For example, doing a connect
105 * and a receive at the same time might allow the receive to consume the
106 * ACK message meant for the connect. While additional work could be done
107 * to try and overcome this, it doesn't seem to be worthwhile at the present.
108 *
109 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
110 * that another operation that must be performed in a non-blocking manner is
111 * not delayed for very long because the lock has already been taken.
112 *
113 * NOTE: This code assumes that certain fields of a port/socket pair are
114 * constant over its lifetime; such fields can be examined without taking
115 * the socket lock and/or port lock, and do not need to be re-read even
116 * after resuming processing after waiting. These fields include:
117 * - socket type
118 * - pointer to socket sk structure (aka tipc_sock structure)
119 * - pointer to port structure
120 * - port reference
121 */
122
123/**
124 * advance_rx_queue - discard first buffer in socket receive queue
125 *
126 * Caller must hold socket lock
127 */
128static void advance_rx_queue(struct sock *sk)
129{
130 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
131}
132
133/**
134 * reject_rx_queue - reject all buffers in socket receive queue
135 *
136 * Caller must hold socket lock
137 */
138static void reject_rx_queue(struct sock *sk)
139{
140 struct sk_buff *buf;
141
142 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
143 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
144}
145
146/**
147 * tipc_sk_create - create a TIPC socket
148 * @net: network namespace (must be default network)
149 * @sock: pre-allocated socket structure
150 * @protocol: protocol indicator (must be 0)
151 * @kern: caused by kernel or by userspace?
152 *
153 * This routine creates additional data structures used by the TIPC socket,
154 * initializes them, and links them together.
155 *
156 * Returns 0 on success, errno otherwise
157 */
158static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
159 int kern)
160{
161 const struct proto_ops *ops;
162 socket_state state;
163 struct sock *sk;
164 struct tipc_port *tp_ptr;
165
166 /* Validate arguments */
167 if (unlikely(protocol != 0))
168 return -EPROTONOSUPPORT;
169
170 switch (sock->type) {
171 case SOCK_STREAM:
172 ops = &stream_ops;
173 state = SS_UNCONNECTED;
174 break;
175 case SOCK_SEQPACKET:
176 ops = &packet_ops;
177 state = SS_UNCONNECTED;
178 break;
179 case SOCK_DGRAM:
180 case SOCK_RDM:
181 ops = &msg_ops;
182 state = SS_READY;
183 break;
184 default:
185 return -EPROTOTYPE;
186 }
187
188 /* Allocate socket's protocol area */
189 if (!kern)
190 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
191 else
192 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
193
194 if (sk == NULL)
195 return -ENOMEM;
196
197 /* Allocate TIPC port for socket to use */
198 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
199 TIPC_LOW_IMPORTANCE);
200 if (unlikely(!tp_ptr)) {
201 sk_free(sk);
202 return -ENOMEM;
203 }
204
205 /* Finish initializing socket data structures */
206 sock->ops = ops;
207 sock->state = state;
208
209 sock_init_data(sock, sk);
210 sk->sk_backlog_rcv = backlog_rcv;
211 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
212 sk->sk_data_ready = tipc_data_ready;
213 sk->sk_write_space = tipc_write_space;
214 tipc_sk(sk)->p = tp_ptr;
215 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
216
217 spin_unlock_bh(tp_ptr->lock);
218
219 if (sock->state == SS_READY) {
220 tipc_set_portunreturnable(tp_ptr->ref, 1);
221 if (sock->type == SOCK_DGRAM)
222 tipc_set_portunreliable(tp_ptr->ref, 1);
223 }
224
225 return 0;
226}
227
228/**
229 * tipc_sock_create_local - create TIPC socket from inside TIPC module
230 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
231 *
232 * We cannot use sock_creat_kern here because it bumps module user count.
233 * Since socket owner and creator is the same module we must make sure
234 * that module count remains zero for module local sockets, otherwise
235 * we cannot do rmmod.
236 *
237 * Returns 0 on success, errno otherwise
238 */
239int tipc_sock_create_local(int type, struct socket **res)
240{
241 int rc;
242 struct sock *sk;
243
244 rc = sock_create_lite(AF_TIPC, type, 0, res);
245 if (rc < 0) {
246 pr_err("Failed to create kernel socket\n");
247 return rc;
248 }
249 tipc_sk_create(&init_net, *res, 0, 1);
250
251 sk = (*res)->sk;
252
253 return 0;
254}
255
256/**
257 * tipc_sock_release_local - release socket created by tipc_sock_create_local
258 * @sock: the socket to be released.
259 *
260 * Module reference count is not incremented when such sockets are created,
261 * so we must keep it from being decremented when they are released.
262 */
263void tipc_sock_release_local(struct socket *sock)
264{
265 release(sock);
266 sock->ops = NULL;
267 sock_release(sock);
268}
269
270/**
271 * tipc_sock_accept_local - accept a connection on a socket created
272 * with tipc_sock_create_local. Use this function to avoid that
273 * module reference count is inadvertently incremented.
274 *
275 * @sock: the accepting socket
276 * @newsock: reference to the new socket to be created
277 * @flags: socket flags
278 */
279
280int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
281 int flags)
282{
283 struct sock *sk = sock->sk;
284 int ret;
285
286 ret = sock_create_lite(sk->sk_family, sk->sk_type,
287 sk->sk_protocol, newsock);
288 if (ret < 0)
289 return ret;
290
291 ret = accept(sock, *newsock, flags);
292 if (ret < 0) {
293 sock_release(*newsock);
294 return ret;
295 }
296 (*newsock)->ops = sock->ops;
297 return ret;
298}
299
300/**
301 * release - destroy a TIPC socket
302 * @sock: socket to destroy
303 *
304 * This routine cleans up any messages that are still queued on the socket.
305 * For DGRAM and RDM socket types, all queued messages are rejected.
306 * For SEQPACKET and STREAM socket types, the first message is rejected
307 * and any others are discarded. (If the first message on a STREAM socket
308 * is partially-read, it is discarded and the next one is rejected instead.)
309 *
310 * NOTE: Rejected messages are not necessarily returned to the sender! They
311 * are returned or discarded according to the "destination droppable" setting
312 * specified for the message by the sender.
313 *
314 * Returns 0 on success, errno otherwise
315 */
316static int release(struct socket *sock)
317{
318 struct sock *sk = sock->sk;
319 struct tipc_port *tport;
320 struct sk_buff *buf;
321 int res;
322
323 /*
324 * Exit if socket isn't fully initialized (occurs when a failed accept()
325 * releases a pre-allocated child socket that was never used)
326 */
327 if (sk == NULL)
328 return 0;
329
330 tport = tipc_sk_port(sk);
331 lock_sock(sk);
332
333 /*
334 * Reject all unreceived messages, except on an active connection
335 * (which disconnects locally & sends a 'FIN+' to peer)
336 */
337 while (sock->state != SS_DISCONNECTING) {
338 buf = __skb_dequeue(&sk->sk_receive_queue);
339 if (buf == NULL)
340 break;
341 if (TIPC_SKB_CB(buf)->handle != 0)
342 kfree_skb(buf);
343 else {
344 if ((sock->state == SS_CONNECTING) ||
345 (sock->state == SS_CONNECTED)) {
346 sock->state = SS_DISCONNECTING;
347 tipc_disconnect(tport->ref);
348 }
349 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
350 }
351 }
352
353 /*
354 * Delete TIPC port; this ensures no more messages are queued
355 * (also disconnects an active connection & sends a 'FIN-' to peer)
356 */
357 res = tipc_deleteport(tport->ref);
358
359 /* Discard any remaining (connection-based) messages in receive queue */
360 __skb_queue_purge(&sk->sk_receive_queue);
361
362 /* Reject any messages that accumulated in backlog queue */
363 sock->state = SS_DISCONNECTING;
364 release_sock(sk);
365
366 sock_put(sk);
367 sock->sk = NULL;
368
369 return res;
370}
371
372/**
373 * bind - associate or disassocate TIPC name(s) with a socket
374 * @sock: socket structure
375 * @uaddr: socket address describing name(s) and desired operation
376 * @uaddr_len: size of socket address data structure
377 *
378 * Name and name sequence binding is indicated using a positive scope value;
379 * a negative scope value unbinds the specified name. Specifying no name
380 * (i.e. a socket address length of 0) unbinds all names from the socket.
381 *
382 * Returns 0 on success, errno otherwise
383 *
384 * NOTE: This routine doesn't need to take the socket lock since it doesn't
385 * access any non-constant socket information.
386 */
387static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
388{
389 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
390 u32 portref = tipc_sk_port(sock->sk)->ref;
391
392 if (unlikely(!uaddr_len))
393 return tipc_withdraw(portref, 0, NULL);
394
395 if (uaddr_len < sizeof(struct sockaddr_tipc))
396 return -EINVAL;
397 if (addr->family != AF_TIPC)
398 return -EAFNOSUPPORT;
399
400 if (addr->addrtype == TIPC_ADDR_NAME)
401 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
402 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
403 return -EAFNOSUPPORT;
404
405 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
406 (addr->addr.nameseq.type != TIPC_TOP_SRV))
407 return -EACCES;
408
409 return (addr->scope > 0) ?
410 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
411 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
412}
413
414/**
415 * get_name - get port ID of socket or peer socket
416 * @sock: socket structure
417 * @uaddr: area for returned socket address
418 * @uaddr_len: area for returned length of socket address
419 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
420 *
421 * Returns 0 on success, errno otherwise
422 *
423 * NOTE: This routine doesn't need to take the socket lock since it only
424 * accesses socket information that is unchanging (or which changes in
425 * a completely predictable manner).
426 */
427static int get_name(struct socket *sock, struct sockaddr *uaddr,
428 int *uaddr_len, int peer)
429{
430 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
431 struct tipc_sock *tsock = tipc_sk(sock->sk);
432
433 memset(addr, 0, sizeof(*addr));
434 if (peer) {
435 if ((sock->state != SS_CONNECTED) &&
436 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
437 return -ENOTCONN;
438 addr->addr.id.ref = tsock->peer_name.ref;
439 addr->addr.id.node = tsock->peer_name.node;
440 } else {
441 addr->addr.id.ref = tsock->p->ref;
442 addr->addr.id.node = tipc_own_addr;
443 }
444
445 *uaddr_len = sizeof(*addr);
446 addr->addrtype = TIPC_ADDR_ID;
447 addr->family = AF_TIPC;
448 addr->scope = 0;
449 addr->addr.name.domain = 0;
450
451 return 0;
452}
453
454/**
455 * poll - read and possibly block on pollmask
456 * @file: file structure associated with the socket
457 * @sock: socket for which to calculate the poll bits
458 * @wait: ???
459 *
460 * Returns pollmask value
461 *
462 * COMMENTARY:
463 * It appears that the usual socket locking mechanisms are not useful here
464 * since the pollmask info is potentially out-of-date the moment this routine
465 * exits. TCP and other protocols seem to rely on higher level poll routines
466 * to handle any preventable race conditions, so TIPC will do the same ...
467 *
468 * TIPC sets the returned events as follows:
469 *
470 * socket state flags set
471 * ------------ ---------
472 * unconnected no read flags
473 * POLLOUT if port is not congested
474 *
475 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
476 * no write flags
477 *
478 * connected POLLIN/POLLRDNORM if data in rx queue
479 * POLLOUT if port is not congested
480 *
481 * disconnecting POLLIN/POLLRDNORM/POLLHUP
482 * no write flags
483 *
484 * listening POLLIN if SYN in rx queue
485 * no write flags
486 *
487 * ready POLLIN/POLLRDNORM if data in rx queue
488 * [connectionless] POLLOUT (since port cannot be congested)
489 *
490 * IMPORTANT: The fact that a read or write operation is indicated does NOT
491 * imply that the operation will succeed, merely that it should be performed
492 * and will not block.
493 */
494static unsigned int poll(struct file *file, struct socket *sock,
495 poll_table *wait)
496{
497 struct sock *sk = sock->sk;
498 u32 mask = 0;
499
500 sock_poll_wait(file, sk_sleep(sk), wait);
501
502 switch ((int)sock->state) {
503 case SS_UNCONNECTED:
504 if (!tipc_sk_port(sk)->congested)
505 mask |= POLLOUT;
506 break;
507 case SS_READY:
508 case SS_CONNECTED:
509 if (!tipc_sk_port(sk)->congested)
510 mask |= POLLOUT;
511 /* fall thru' */
512 case SS_CONNECTING:
513 case SS_LISTENING:
514 if (!skb_queue_empty(&sk->sk_receive_queue))
515 mask |= (POLLIN | POLLRDNORM);
516 break;
517 case SS_DISCONNECTING:
518 mask = (POLLIN | POLLRDNORM | POLLHUP);
519 break;
520 }
521
522 return mask;
523}
524
525/**
526 * dest_name_check - verify user is permitted to send to specified port name
527 * @dest: destination address
528 * @m: descriptor for message to be sent
529 *
530 * Prevents restricted configuration commands from being issued by
531 * unauthorized users.
532 *
533 * Returns 0 if permission is granted, otherwise errno
534 */
535static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
536{
537 struct tipc_cfg_msg_hdr hdr;
538
539 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
540 return 0;
541 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
542 return 0;
543 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
544 return -EACCES;
545
546 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
547 return -EMSGSIZE;
548 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
549 return -EFAULT;
550 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
551 return -EACCES;
552
553 return 0;
554}
555
556/**
557 * send_msg - send message in connectionless manner
558 * @iocb: if NULL, indicates that socket lock is already held
559 * @sock: socket structure
560 * @m: message to send
561 * @total_len: length of message
562 *
563 * Message must have an destination specified explicitly.
564 * Used for SOCK_RDM and SOCK_DGRAM messages,
565 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
566 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
567 *
568 * Returns the number of bytes sent on success, or errno otherwise
569 */
570static int send_msg(struct kiocb *iocb, struct socket *sock,
571 struct msghdr *m, size_t total_len)
572{
573 struct sock *sk = sock->sk;
574 struct tipc_port *tport = tipc_sk_port(sk);
575 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
576 int needs_conn;
577 long timeout_val;
578 int res = -EINVAL;
579
580 if (unlikely(!dest))
581 return -EDESTADDRREQ;
582 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
583 (dest->family != AF_TIPC)))
584 return -EINVAL;
585 if (total_len > TIPC_MAX_USER_MSG_SIZE)
586 return -EMSGSIZE;
587
588 if (iocb)
589 lock_sock(sk);
590
591 needs_conn = (sock->state != SS_READY);
592 if (unlikely(needs_conn)) {
593 if (sock->state == SS_LISTENING) {
594 res = -EPIPE;
595 goto exit;
596 }
597 if (sock->state != SS_UNCONNECTED) {
598 res = -EISCONN;
599 goto exit;
600 }
601 if (tport->published) {
602 res = -EOPNOTSUPP;
603 goto exit;
604 }
605 if (dest->addrtype == TIPC_ADDR_NAME) {
606 tport->conn_type = dest->addr.name.name.type;
607 tport->conn_instance = dest->addr.name.name.instance;
608 }
609
610 /* Abort any pending connection attempts (very unlikely) */
611 reject_rx_queue(sk);
612 }
613
614 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
615
616 do {
617 if (dest->addrtype == TIPC_ADDR_NAME) {
618 res = dest_name_check(dest, m);
619 if (res)
620 break;
621 res = tipc_send2name(tport->ref,
622 &dest->addr.name.name,
623 dest->addr.name.domain,
624 m->msg_iovlen,
625 m->msg_iov,
626 total_len);
627 } else if (dest->addrtype == TIPC_ADDR_ID) {
628 res = tipc_send2port(tport->ref,
629 &dest->addr.id,
630 m->msg_iovlen,
631 m->msg_iov,
632 total_len);
633 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
634 if (needs_conn) {
635 res = -EOPNOTSUPP;
636 break;
637 }
638 res = dest_name_check(dest, m);
639 if (res)
640 break;
641 res = tipc_multicast(tport->ref,
642 &dest->addr.nameseq,
643 m->msg_iovlen,
644 m->msg_iov,
645 total_len);
646 }
647 if (likely(res != -ELINKCONG)) {
648 if (needs_conn && (res >= 0))
649 sock->state = SS_CONNECTING;
650 break;
651 }
652 if (timeout_val <= 0L) {
653 res = timeout_val ? timeout_val : -EWOULDBLOCK;
654 break;
655 }
656 release_sock(sk);
657 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
658 !tport->congested, timeout_val);
659 lock_sock(sk);
660 } while (1);
661
662exit:
663 if (iocb)
664 release_sock(sk);
665 return res;
666}
667
668/**
669 * send_packet - send a connection-oriented message
670 * @iocb: if NULL, indicates that socket lock is already held
671 * @sock: socket structure
672 * @m: message to send
673 * @total_len: length of message
674 *
675 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
676 *
677 * Returns the number of bytes sent on success, or errno otherwise
678 */
679static int send_packet(struct kiocb *iocb, struct socket *sock,
680 struct msghdr *m, size_t total_len)
681{
682 struct sock *sk = sock->sk;
683 struct tipc_port *tport = tipc_sk_port(sk);
684 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
685 long timeout_val;
686 int res;
687
688 /* Handle implied connection establishment */
689 if (unlikely(dest))
690 return send_msg(iocb, sock, m, total_len);
691
692 if (total_len > TIPC_MAX_USER_MSG_SIZE)
693 return -EMSGSIZE;
694
695 if (iocb)
696 lock_sock(sk);
697
698 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
699
700 do {
701 if (unlikely(sock->state != SS_CONNECTED)) {
702 if (sock->state == SS_DISCONNECTING)
703 res = -EPIPE;
704 else
705 res = -ENOTCONN;
706 break;
707 }
708
709 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
710 total_len);
711 if (likely(res != -ELINKCONG))
712 break;
713 if (timeout_val <= 0L) {
714 res = timeout_val ? timeout_val : -EWOULDBLOCK;
715 break;
716 }
717 release_sock(sk);
718 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
719 (!tport->congested || !tport->connected), timeout_val);
720 lock_sock(sk);
721 } while (1);
722
723 if (iocb)
724 release_sock(sk);
725 return res;
726}
727
728/**
729 * send_stream - send stream-oriented data
730 * @iocb: (unused)
731 * @sock: socket structure
732 * @m: data to send
733 * @total_len: total length of data to be sent
734 *
735 * Used for SOCK_STREAM data.
736 *
737 * Returns the number of bytes sent on success (or partial success),
738 * or errno if no data sent
739 */
740static int send_stream(struct kiocb *iocb, struct socket *sock,
741 struct msghdr *m, size_t total_len)
742{
743 struct sock *sk = sock->sk;
744 struct tipc_port *tport = tipc_sk_port(sk);
745 struct msghdr my_msg;
746 struct iovec my_iov;
747 struct iovec *curr_iov;
748 int curr_iovlen;
749 char __user *curr_start;
750 u32 hdr_size;
751 int curr_left;
752 int bytes_to_send;
753 int bytes_sent;
754 int res;
755
756 lock_sock(sk);
757
758 /* Handle special cases where there is no connection */
759 if (unlikely(sock->state != SS_CONNECTED)) {
760 if (sock->state == SS_UNCONNECTED) {
761 res = send_packet(NULL, sock, m, total_len);
762 goto exit;
763 } else if (sock->state == SS_DISCONNECTING) {
764 res = -EPIPE;
765 goto exit;
766 } else {
767 res = -ENOTCONN;
768 goto exit;
769 }
770 }
771
772 if (unlikely(m->msg_name)) {
773 res = -EISCONN;
774 goto exit;
775 }
776
777 if (total_len > (unsigned int)INT_MAX) {
778 res = -EMSGSIZE;
779 goto exit;
780 }
781
782 /*
783 * Send each iovec entry using one or more messages
784 *
785 * Note: This algorithm is good for the most likely case
786 * (i.e. one large iovec entry), but could be improved to pass sets
787 * of small iovec entries into send_packet().
788 */
789 curr_iov = m->msg_iov;
790 curr_iovlen = m->msg_iovlen;
791 my_msg.msg_iov = &my_iov;
792 my_msg.msg_iovlen = 1;
793 my_msg.msg_flags = m->msg_flags;
794 my_msg.msg_name = NULL;
795 bytes_sent = 0;
796
797 hdr_size = msg_hdr_sz(&tport->phdr);
798
799 while (curr_iovlen--) {
800 curr_start = curr_iov->iov_base;
801 curr_left = curr_iov->iov_len;
802
803 while (curr_left) {
804 bytes_to_send = tport->max_pkt - hdr_size;
805 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
806 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
807 if (curr_left < bytes_to_send)
808 bytes_to_send = curr_left;
809 my_iov.iov_base = curr_start;
810 my_iov.iov_len = bytes_to_send;
811 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
812 if (res < 0) {
813 if (bytes_sent)
814 res = bytes_sent;
815 goto exit;
816 }
817 curr_left -= bytes_to_send;
818 curr_start += bytes_to_send;
819 bytes_sent += bytes_to_send;
820 }
821
822 curr_iov++;
823 }
824 res = bytes_sent;
825exit:
826 release_sock(sk);
827 return res;
828}
829
830/**
831 * auto_connect - complete connection setup to a remote port
832 * @sock: socket structure
833 * @msg: peer's response message
834 *
835 * Returns 0 on success, errno otherwise
836 */
837static int auto_connect(struct socket *sock, struct tipc_msg *msg)
838{
839 struct tipc_sock *tsock = tipc_sk(sock->sk);
840 struct tipc_port *p_ptr;
841
842 tsock->peer_name.ref = msg_origport(msg);
843 tsock->peer_name.node = msg_orignode(msg);
844 p_ptr = tipc_port_deref(tsock->p->ref);
845 if (!p_ptr)
846 return -EINVAL;
847
848 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
849
850 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
851 return -EINVAL;
852 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
853 sock->state = SS_CONNECTED;
854 return 0;
855}
856
857/**
858 * set_orig_addr - capture sender's address for received message
859 * @m: descriptor for message info
860 * @msg: received message header
861 *
862 * Note: Address is not captured if not requested by receiver.
863 */
864static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
865{
866 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
867
868 if (addr) {
869 addr->family = AF_TIPC;
870 addr->addrtype = TIPC_ADDR_ID;
871 memset(&addr->addr, 0, sizeof(addr->addr));
872 addr->addr.id.ref = msg_origport(msg);
873 addr->addr.id.node = msg_orignode(msg);
874 addr->addr.name.domain = 0; /* could leave uninitialized */
875 addr->scope = 0; /* could leave uninitialized */
876 m->msg_namelen = sizeof(struct sockaddr_tipc);
877 }
878}
879
880/**
881 * anc_data_recv - optionally capture ancillary data for received message
882 * @m: descriptor for message info
883 * @msg: received message header
884 * @tport: TIPC port associated with message
885 *
886 * Note: Ancillary data is not captured if not requested by receiver.
887 *
888 * Returns 0 if successful, otherwise errno
889 */
890static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
891 struct tipc_port *tport)
892{
893 u32 anc_data[3];
894 u32 err;
895 u32 dest_type;
896 int has_name;
897 int res;
898
899 if (likely(m->msg_controllen == 0))
900 return 0;
901
902 /* Optionally capture errored message object(s) */
903 err = msg ? msg_errcode(msg) : 0;
904 if (unlikely(err)) {
905 anc_data[0] = err;
906 anc_data[1] = msg_data_sz(msg);
907 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
908 if (res)
909 return res;
910 if (anc_data[1]) {
911 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
912 msg_data(msg));
913 if (res)
914 return res;
915 }
916 }
917
918 /* Optionally capture message destination object */
919 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
920 switch (dest_type) {
921 case TIPC_NAMED_MSG:
922 has_name = 1;
923 anc_data[0] = msg_nametype(msg);
924 anc_data[1] = msg_namelower(msg);
925 anc_data[2] = msg_namelower(msg);
926 break;
927 case TIPC_MCAST_MSG:
928 has_name = 1;
929 anc_data[0] = msg_nametype(msg);
930 anc_data[1] = msg_namelower(msg);
931 anc_data[2] = msg_nameupper(msg);
932 break;
933 case TIPC_CONN_MSG:
934 has_name = (tport->conn_type != 0);
935 anc_data[0] = tport->conn_type;
936 anc_data[1] = tport->conn_instance;
937 anc_data[2] = tport->conn_instance;
938 break;
939 default:
940 has_name = 0;
941 }
942 if (has_name) {
943 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
944 if (res)
945 return res;
946 }
947
948 return 0;
949}
950
951/**
952 * recv_msg - receive packet-oriented message
953 * @iocb: (unused)
954 * @m: descriptor for message info
955 * @buf_len: total size of user buffer area
956 * @flags: receive flags
957 *
958 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
959 * If the complete message doesn't fit in user area, truncate it.
960 *
961 * Returns size of returned message data, errno otherwise
962 */
963static int recv_msg(struct kiocb *iocb, struct socket *sock,
964 struct msghdr *m, size_t buf_len, int flags)
965{
966 struct sock *sk = sock->sk;
967 struct tipc_port *tport = tipc_sk_port(sk);
968 struct sk_buff *buf;
969 struct tipc_msg *msg;
970 long timeout;
971 unsigned int sz;
972 u32 err;
973 int res;
974
975 /* Catch invalid receive requests */
976 if (unlikely(!buf_len))
977 return -EINVAL;
978
979 lock_sock(sk);
980
981 if (unlikely(sock->state == SS_UNCONNECTED)) {
982 res = -ENOTCONN;
983 goto exit;
984 }
985
986 /* will be updated in set_orig_addr() if needed */
987 m->msg_namelen = 0;
988
989 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
990restart:
991
992 /* Look for a message in receive queue; wait if necessary */
993 while (skb_queue_empty(&sk->sk_receive_queue)) {
994 if (sock->state == SS_DISCONNECTING) {
995 res = -ENOTCONN;
996 goto exit;
997 }
998 if (timeout <= 0L) {
999 res = timeout ? timeout : -EWOULDBLOCK;
1000 goto exit;
1001 }
1002 release_sock(sk);
1003 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1004 tipc_rx_ready(sock),
1005 timeout);
1006 lock_sock(sk);
1007 }
1008
1009 /* Look at first message in receive queue */
1010 buf = skb_peek(&sk->sk_receive_queue);
1011 msg = buf_msg(buf);
1012 sz = msg_data_sz(msg);
1013 err = msg_errcode(msg);
1014
1015 /* Discard an empty non-errored message & try again */
1016 if ((!sz) && (!err)) {
1017 advance_rx_queue(sk);
1018 goto restart;
1019 }
1020
1021 /* Capture sender's address (optional) */
1022 set_orig_addr(m, msg);
1023
1024 /* Capture ancillary data (optional) */
1025 res = anc_data_recv(m, msg, tport);
1026 if (res)
1027 goto exit;
1028
1029 /* Capture message data (if valid) & compute return value (always) */
1030 if (!err) {
1031 if (unlikely(buf_len < sz)) {
1032 sz = buf_len;
1033 m->msg_flags |= MSG_TRUNC;
1034 }
1035 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1036 m->msg_iov, sz);
1037 if (res)
1038 goto exit;
1039 res = sz;
1040 } else {
1041 if ((sock->state == SS_READY) ||
1042 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1043 res = 0;
1044 else
1045 res = -ECONNRESET;
1046 }
1047
1048 /* Consume received message (optional) */
1049 if (likely(!(flags & MSG_PEEK))) {
1050 if ((sock->state != SS_READY) &&
1051 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1052 tipc_acknowledge(tport->ref, tport->conn_unacked);
1053 advance_rx_queue(sk);
1054 }
1055exit:
1056 release_sock(sk);
1057 return res;
1058}
1059
1060/**
1061 * recv_stream - receive stream-oriented data
1062 * @iocb: (unused)
1063 * @m: descriptor for message info
1064 * @buf_len: total size of user buffer area
1065 * @flags: receive flags
1066 *
1067 * Used for SOCK_STREAM messages only. If not enough data is available
1068 * will optionally wait for more; never truncates data.
1069 *
1070 * Returns size of returned message data, errno otherwise
1071 */
1072static int recv_stream(struct kiocb *iocb, struct socket *sock,
1073 struct msghdr *m, size_t buf_len, int flags)
1074{
1075 struct sock *sk = sock->sk;
1076 struct tipc_port *tport = tipc_sk_port(sk);
1077 struct sk_buff *buf;
1078 struct tipc_msg *msg;
1079 long timeout;
1080 unsigned int sz;
1081 int sz_to_copy, target, needed;
1082 int sz_copied = 0;
1083 u32 err;
1084 int res = 0;
1085
1086 /* Catch invalid receive attempts */
1087 if (unlikely(!buf_len))
1088 return -EINVAL;
1089
1090 lock_sock(sk);
1091
1092 if (unlikely((sock->state == SS_UNCONNECTED))) {
1093 res = -ENOTCONN;
1094 goto exit;
1095 }
1096
1097 /* will be updated in set_orig_addr() if needed */
1098 m->msg_namelen = 0;
1099
1100 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1101 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1102
1103restart:
1104 /* Look for a message in receive queue; wait if necessary */
1105 while (skb_queue_empty(&sk->sk_receive_queue)) {
1106 if (sock->state == SS_DISCONNECTING) {
1107 res = -ENOTCONN;
1108 goto exit;
1109 }
1110 if (timeout <= 0L) {
1111 res = timeout ? timeout : -EWOULDBLOCK;
1112 goto exit;
1113 }
1114 release_sock(sk);
1115 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1116 tipc_rx_ready(sock),
1117 timeout);
1118 lock_sock(sk);
1119 }
1120
1121 /* Look at first message in receive queue */
1122 buf = skb_peek(&sk->sk_receive_queue);
1123 msg = buf_msg(buf);
1124 sz = msg_data_sz(msg);
1125 err = msg_errcode(msg);
1126
1127 /* Discard an empty non-errored message & try again */
1128 if ((!sz) && (!err)) {
1129 advance_rx_queue(sk);
1130 goto restart;
1131 }
1132
1133 /* Optionally capture sender's address & ancillary data of first msg */
1134 if (sz_copied == 0) {
1135 set_orig_addr(m, msg);
1136 res = anc_data_recv(m, msg, tport);
1137 if (res)
1138 goto exit;
1139 }
1140
1141 /* Capture message data (if valid) & compute return value (always) */
1142 if (!err) {
1143 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
1144
1145 sz -= offset;
1146 needed = (buf_len - sz_copied);
1147 sz_to_copy = (sz <= needed) ? sz : needed;
1148
1149 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1150 m->msg_iov, sz_to_copy);
1151 if (res)
1152 goto exit;
1153
1154 sz_copied += sz_to_copy;
1155
1156 if (sz_to_copy < sz) {
1157 if (!(flags & MSG_PEEK))
1158 TIPC_SKB_CB(buf)->handle =
1159 (void *)(unsigned long)(offset + sz_to_copy);
1160 goto exit;
1161 }
1162 } else {
1163 if (sz_copied != 0)
1164 goto exit; /* can't add error msg to valid data */
1165
1166 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1167 res = 0;
1168 else
1169 res = -ECONNRESET;
1170 }
1171
1172 /* Consume received message (optional) */
1173 if (likely(!(flags & MSG_PEEK))) {
1174 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1175 tipc_acknowledge(tport->ref, tport->conn_unacked);
1176 advance_rx_queue(sk);
1177 }
1178
1179 /* Loop around if more data is required */
1180 if ((sz_copied < buf_len) && /* didn't get all requested data */
1181 (!skb_queue_empty(&sk->sk_receive_queue) ||
1182 (sz_copied < target)) && /* and more is ready or required */
1183 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1184 (!err)) /* and haven't reached a FIN */
1185 goto restart;
1186
1187exit:
1188 release_sock(sk);
1189 return sz_copied ? sz_copied : res;
1190}
1191
1192/**
1193 * tipc_write_space - wake up thread if port congestion is released
1194 * @sk: socket
1195 */
1196static void tipc_write_space(struct sock *sk)
1197{
1198 struct socket_wq *wq;
1199
1200 rcu_read_lock();
1201 wq = rcu_dereference(sk->sk_wq);
1202 if (wq_has_sleeper(wq))
1203 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1204 POLLWRNORM | POLLWRBAND);
1205 rcu_read_unlock();
1206}
1207
1208/**
1209 * tipc_data_ready - wake up threads to indicate messages have been received
1210 * @sk: socket
1211 * @len: the length of messages
1212 */
1213static void tipc_data_ready(struct sock *sk, int len)
1214{
1215 struct socket_wq *wq;
1216
1217 rcu_read_lock();
1218 wq = rcu_dereference(sk->sk_wq);
1219 if (wq_has_sleeper(wq))
1220 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1221 POLLRDNORM | POLLRDBAND);
1222 rcu_read_unlock();
1223}
1224
1225/**
1226 * filter_connect - Handle all incoming messages for a connection-based socket
1227 * @tsock: TIPC socket
1228 * @msg: message
1229 *
1230 * Returns TIPC error status code and socket error status code
1231 * once it encounters some errors
1232 */
1233static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1234{
1235 struct socket *sock = tsock->sk.sk_socket;
1236 struct tipc_msg *msg = buf_msg(*buf);
1237 struct sock *sk = &tsock->sk;
1238 u32 retval = TIPC_ERR_NO_PORT;
1239 int res;
1240
1241 if (msg_mcast(msg))
1242 return retval;
1243
1244 switch ((int)sock->state) {
1245 case SS_CONNECTED:
1246 /* Accept only connection-based messages sent by peer */
1247 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1248 if (unlikely(msg_errcode(msg))) {
1249 sock->state = SS_DISCONNECTING;
1250 __tipc_disconnect(tsock->p);
1251 }
1252 retval = TIPC_OK;
1253 }
1254 break;
1255 case SS_CONNECTING:
1256 /* Accept only ACK or NACK message */
1257 if (unlikely(msg_errcode(msg))) {
1258 sock->state = SS_DISCONNECTING;
1259 sk->sk_err = -ECONNREFUSED;
1260 retval = TIPC_OK;
1261 break;
1262 }
1263
1264 if (unlikely(!msg_connected(msg)))
1265 break;
1266
1267 res = auto_connect(sock, msg);
1268 if (res) {
1269 sock->state = SS_DISCONNECTING;
1270 sk->sk_err = res;
1271 retval = TIPC_OK;
1272 break;
1273 }
1274
1275 /* If an incoming message is an 'ACK-', it should be
1276 * discarded here because it doesn't contain useful
1277 * data. In addition, we should try to wake up
1278 * connect() routine if sleeping.
1279 */
1280 if (msg_data_sz(msg) == 0) {
1281 kfree_skb(*buf);
1282 *buf = NULL;
1283 if (waitqueue_active(sk_sleep(sk)))
1284 wake_up_interruptible(sk_sleep(sk));
1285 }
1286 retval = TIPC_OK;
1287 break;
1288 case SS_LISTENING:
1289 case SS_UNCONNECTED:
1290 /* Accept only SYN message */
1291 if (!msg_connected(msg) && !(msg_errcode(msg)))
1292 retval = TIPC_OK;
1293 break;
1294 case SS_DISCONNECTING:
1295 break;
1296 default:
1297 pr_err("Unknown socket state %u\n", sock->state);
1298 }
1299 return retval;
1300}
1301
1302/**
1303 * rcvbuf_limit - get proper overload limit of socket receive queue
1304 * @sk: socket
1305 * @buf: message
1306 *
1307 * For all connection oriented messages, irrespective of importance,
1308 * the default overload value (i.e. 67MB) is set as limit.
1309 *
1310 * For all connectionless messages, by default new queue limits are
1311 * as belows:
1312 *
1313 * TIPC_LOW_IMPORTANCE (4 MB)
1314 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1315 * TIPC_HIGH_IMPORTANCE (16 MB)
1316 * TIPC_CRITICAL_IMPORTANCE (32 MB)
1317 *
1318 * Returns overload limit according to corresponding message importance
1319 */
1320static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1321{
1322 struct tipc_msg *msg = buf_msg(buf);
1323 unsigned int limit;
1324
1325 if (msg_connected(msg))
1326 limit = sysctl_tipc_rmem[2];
1327 else
1328 limit = sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1329 msg_importance(msg);
1330 return limit;
1331}
1332
1333/**
1334 * filter_rcv - validate incoming message
1335 * @sk: socket
1336 * @buf: message
1337 *
1338 * Enqueues message on receive queue if acceptable; optionally handles
1339 * disconnect indication for a connected socket.
1340 *
1341 * Called with socket lock already taken; port lock may also be taken.
1342 *
1343 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1344 */
1345static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
1346{
1347 struct socket *sock = sk->sk_socket;
1348 struct tipc_msg *msg = buf_msg(buf);
1349 unsigned int limit = rcvbuf_limit(sk, buf);
1350 u32 res = TIPC_OK;
1351
1352 /* Reject message if it is wrong sort of message for socket */
1353 if (msg_type(msg) > TIPC_DIRECT_MSG)
1354 return TIPC_ERR_NO_PORT;
1355
1356 if (sock->state == SS_READY) {
1357 if (msg_connected(msg))
1358 return TIPC_ERR_NO_PORT;
1359 } else {
1360 res = filter_connect(tipc_sk(sk), &buf);
1361 if (res != TIPC_OK || buf == NULL)
1362 return res;
1363 }
1364
1365 /* Reject message if there isn't room to queue it */
1366 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1367 return TIPC_ERR_OVERLOAD;
1368
1369 /* Enqueue message */
1370 TIPC_SKB_CB(buf)->handle = 0;
1371 __skb_queue_tail(&sk->sk_receive_queue, buf);
1372 skb_set_owner_r(buf, sk);
1373
1374 sk->sk_data_ready(sk, 0);
1375 return TIPC_OK;
1376}
1377
1378/**
1379 * backlog_rcv - handle incoming message from backlog queue
1380 * @sk: socket
1381 * @buf: message
1382 *
1383 * Caller must hold socket lock, but not port lock.
1384 *
1385 * Returns 0
1386 */
1387static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1388{
1389 u32 res;
1390
1391 res = filter_rcv(sk, buf);
1392 if (res)
1393 tipc_reject_msg(buf, res);
1394 return 0;
1395}
1396
1397/**
1398 * dispatch - handle incoming message
1399 * @tport: TIPC port that received message
1400 * @buf: message
1401 *
1402 * Called with port lock already taken.
1403 *
1404 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1405 */
1406static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1407{
1408 struct sock *sk = (struct sock *)tport->usr_handle;
1409 u32 res;
1410
1411 /*
1412 * Process message if socket is unlocked; otherwise add to backlog queue
1413 *
1414 * This code is based on sk_receive_skb(), but must be distinct from it
1415 * since a TIPC-specific filter/reject mechanism is utilized
1416 */
1417 bh_lock_sock(sk);
1418 if (!sock_owned_by_user(sk)) {
1419 res = filter_rcv(sk, buf);
1420 } else {
1421 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
1422 res = TIPC_ERR_OVERLOAD;
1423 else
1424 res = TIPC_OK;
1425 }
1426 bh_unlock_sock(sk);
1427
1428 return res;
1429}
1430
1431/**
1432 * wakeupdispatch - wake up port after congestion
1433 * @tport: port to wakeup
1434 *
1435 * Called with port lock already taken.
1436 */
1437static void wakeupdispatch(struct tipc_port *tport)
1438{
1439 struct sock *sk = (struct sock *)tport->usr_handle;
1440
1441 sk->sk_write_space(sk);
1442}
1443
1444/**
1445 * connect - establish a connection to another TIPC port
1446 * @sock: socket structure
1447 * @dest: socket address for destination port
1448 * @destlen: size of socket address data structure
1449 * @flags: file-related flags associated with socket
1450 *
1451 * Returns 0 on success, errno otherwise
1452 */
1453static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1454 int flags)
1455{
1456 struct sock *sk = sock->sk;
1457 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1458 struct msghdr m = {NULL,};
1459 unsigned int timeout;
1460 int res;
1461
1462 lock_sock(sk);
1463
1464 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1465 if (sock->state == SS_READY) {
1466 res = -EOPNOTSUPP;
1467 goto exit;
1468 }
1469
1470 /*
1471 * Reject connection attempt using multicast address
1472 *
1473 * Note: send_msg() validates the rest of the address fields,
1474 * so there's no need to do it here
1475 */
1476 if (dst->addrtype == TIPC_ADDR_MCAST) {
1477 res = -EINVAL;
1478 goto exit;
1479 }
1480
1481 timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1482
1483 switch (sock->state) {
1484 case SS_UNCONNECTED:
1485 /* Send a 'SYN-' to destination */
1486 m.msg_name = dest;
1487 m.msg_namelen = destlen;
1488
1489 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1490 * indicate send_msg() is never blocked.
1491 */
1492 if (!timeout)
1493 m.msg_flags = MSG_DONTWAIT;
1494
1495 res = send_msg(NULL, sock, &m, 0);
1496 if ((res < 0) && (res != -EWOULDBLOCK))
1497 goto exit;
1498
1499 /* Just entered SS_CONNECTING state; the only
1500 * difference is that return value in non-blocking
1501 * case is EINPROGRESS, rather than EALREADY.
1502 */
1503 res = -EINPROGRESS;
1504 break;
1505 case SS_CONNECTING:
1506 res = -EALREADY;
1507 break;
1508 case SS_CONNECTED:
1509 res = -EISCONN;
1510 break;
1511 default:
1512 res = -EINVAL;
1513 goto exit;
1514 }
1515
1516 if (sock->state == SS_CONNECTING) {
1517 if (!timeout)
1518 goto exit;
1519
1520 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1521 release_sock(sk);
1522 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1523 sock->state != SS_CONNECTING,
1524 timeout ? (long)msecs_to_jiffies(timeout)
1525 : MAX_SCHEDULE_TIMEOUT);
1526 lock_sock(sk);
1527 if (res <= 0) {
1528 if (res == 0)
1529 res = -ETIMEDOUT;
1530 else
1531 ; /* leave "res" unchanged */
1532 goto exit;
1533 }
1534 }
1535
1536 if (unlikely(sock->state == SS_DISCONNECTING))
1537 res = sock_error(sk);
1538 else
1539 res = 0;
1540
1541exit:
1542 release_sock(sk);
1543 return res;
1544}
1545
1546/**
1547 * listen - allow socket to listen for incoming connections
1548 * @sock: socket structure
1549 * @len: (unused)
1550 *
1551 * Returns 0 on success, errno otherwise
1552 */
1553static int listen(struct socket *sock, int len)
1554{
1555 struct sock *sk = sock->sk;
1556 int res;
1557
1558 lock_sock(sk);
1559
1560 if (sock->state != SS_UNCONNECTED)
1561 res = -EINVAL;
1562 else {
1563 sock->state = SS_LISTENING;
1564 res = 0;
1565 }
1566
1567 release_sock(sk);
1568 return res;
1569}
1570
1571/**
1572 * accept - wait for connection request
1573 * @sock: listening socket
1574 * @newsock: new socket that is to be connected
1575 * @flags: file-related flags associated with socket
1576 *
1577 * Returns 0 on success, errno otherwise
1578 */
1579static int accept(struct socket *sock, struct socket *new_sock, int flags)
1580{
1581 struct sock *new_sk, *sk = sock->sk;
1582 struct sk_buff *buf;
1583 struct tipc_sock *new_tsock;
1584 struct tipc_port *new_tport;
1585 struct tipc_msg *msg;
1586 u32 new_ref;
1587
1588 int res;
1589
1590 lock_sock(sk);
1591
1592 if (sock->state != SS_LISTENING) {
1593 res = -EINVAL;
1594 goto exit;
1595 }
1596
1597 while (skb_queue_empty(&sk->sk_receive_queue)) {
1598 if (flags & O_NONBLOCK) {
1599 res = -EWOULDBLOCK;
1600 goto exit;
1601 }
1602 release_sock(sk);
1603 res = wait_event_interruptible(*sk_sleep(sk),
1604 (!skb_queue_empty(&sk->sk_receive_queue)));
1605 lock_sock(sk);
1606 if (res)
1607 goto exit;
1608 }
1609
1610 buf = skb_peek(&sk->sk_receive_queue);
1611
1612 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
1613 if (res)
1614 goto exit;
1615
1616 new_sk = new_sock->sk;
1617 new_tsock = tipc_sk(new_sk);
1618 new_tport = new_tsock->p;
1619 new_ref = new_tport->ref;
1620 msg = buf_msg(buf);
1621
1622 /* we lock on new_sk; but lockdep sees the lock on sk */
1623 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
1624
1625 /*
1626 * Reject any stray messages received by new socket
1627 * before the socket lock was taken (very, very unlikely)
1628 */
1629 reject_rx_queue(new_sk);
1630
1631 /* Connect new socket to it's peer */
1632 new_tsock->peer_name.ref = msg_origport(msg);
1633 new_tsock->peer_name.node = msg_orignode(msg);
1634 tipc_connect(new_ref, &new_tsock->peer_name);
1635 new_sock->state = SS_CONNECTED;
1636
1637 tipc_set_portimportance(new_ref, msg_importance(msg));
1638 if (msg_named(msg)) {
1639 new_tport->conn_type = msg_nametype(msg);
1640 new_tport->conn_instance = msg_nameinst(msg);
1641 }
1642
1643 /*
1644 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1645 * Respond to 'SYN+' by queuing it on new socket.
1646 */
1647 if (!msg_data_sz(msg)) {
1648 struct msghdr m = {NULL,};
1649
1650 advance_rx_queue(sk);
1651 send_packet(NULL, new_sock, &m, 0);
1652 } else {
1653 __skb_dequeue(&sk->sk_receive_queue);
1654 __skb_queue_head(&new_sk->sk_receive_queue, buf);
1655 skb_set_owner_r(buf, new_sk);
1656 }
1657 release_sock(new_sk);
1658
1659exit:
1660 release_sock(sk);
1661 return res;
1662}
1663
1664/**
1665 * shutdown - shutdown socket connection
1666 * @sock: socket structure
1667 * @how: direction to close (must be SHUT_RDWR)
1668 *
1669 * Terminates connection (if necessary), then purges socket's receive queue.
1670 *
1671 * Returns 0 on success, errno otherwise
1672 */
1673static int shutdown(struct socket *sock, int how)
1674{
1675 struct sock *sk = sock->sk;
1676 struct tipc_port *tport = tipc_sk_port(sk);
1677 struct sk_buff *buf;
1678 int res;
1679
1680 if (how != SHUT_RDWR)
1681 return -EINVAL;
1682
1683 lock_sock(sk);
1684
1685 switch (sock->state) {
1686 case SS_CONNECTING:
1687 case SS_CONNECTED:
1688
1689restart:
1690 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1691 buf = __skb_dequeue(&sk->sk_receive_queue);
1692 if (buf) {
1693 if (TIPC_SKB_CB(buf)->handle != 0) {
1694 kfree_skb(buf);
1695 goto restart;
1696 }
1697 tipc_disconnect(tport->ref);
1698 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1699 } else {
1700 tipc_shutdown(tport->ref);
1701 }
1702
1703 sock->state = SS_DISCONNECTING;
1704
1705 /* fall through */
1706
1707 case SS_DISCONNECTING:
1708
1709 /* Discard any unreceived messages */
1710 __skb_queue_purge(&sk->sk_receive_queue);
1711
1712 /* Wake up anyone sleeping in poll */
1713 sk->sk_state_change(sk);
1714 res = 0;
1715 break;
1716
1717 default:
1718 res = -ENOTCONN;
1719 }
1720
1721 release_sock(sk);
1722 return res;
1723}
1724
1725/**
1726 * setsockopt - set socket option
1727 * @sock: socket structure
1728 * @lvl: option level
1729 * @opt: option identifier
1730 * @ov: pointer to new option value
1731 * @ol: length of option value
1732 *
1733 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1734 * (to ease compatibility).
1735 *
1736 * Returns 0 on success, errno otherwise
1737 */
1738static int setsockopt(struct socket *sock,
1739 int lvl, int opt, char __user *ov, unsigned int ol)
1740{
1741 struct sock *sk = sock->sk;
1742 struct tipc_port *tport = tipc_sk_port(sk);
1743 u32 value;
1744 int res;
1745
1746 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1747 return 0;
1748 if (lvl != SOL_TIPC)
1749 return -ENOPROTOOPT;
1750 if (ol < sizeof(value))
1751 return -EINVAL;
1752 res = get_user(value, (u32 __user *)ov);
1753 if (res)
1754 return res;
1755
1756 lock_sock(sk);
1757
1758 switch (opt) {
1759 case TIPC_IMPORTANCE:
1760 res = tipc_set_portimportance(tport->ref, value);
1761 break;
1762 case TIPC_SRC_DROPPABLE:
1763 if (sock->type != SOCK_STREAM)
1764 res = tipc_set_portunreliable(tport->ref, value);
1765 else
1766 res = -ENOPROTOOPT;
1767 break;
1768 case TIPC_DEST_DROPPABLE:
1769 res = tipc_set_portunreturnable(tport->ref, value);
1770 break;
1771 case TIPC_CONN_TIMEOUT:
1772 tipc_sk(sk)->conn_timeout = value;
1773 /* no need to set "res", since already 0 at this point */
1774 break;
1775 default:
1776 res = -EINVAL;
1777 }
1778
1779 release_sock(sk);
1780
1781 return res;
1782}
1783
1784/**
1785 * getsockopt - get socket option
1786 * @sock: socket structure
1787 * @lvl: option level
1788 * @opt: option identifier
1789 * @ov: receptacle for option value
1790 * @ol: receptacle for length of option value
1791 *
1792 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1793 * (to ease compatibility).
1794 *
1795 * Returns 0 on success, errno otherwise
1796 */
1797static int getsockopt(struct socket *sock,
1798 int lvl, int opt, char __user *ov, int __user *ol)
1799{
1800 struct sock *sk = sock->sk;
1801 struct tipc_port *tport = tipc_sk_port(sk);
1802 int len;
1803 u32 value;
1804 int res;
1805
1806 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1807 return put_user(0, ol);
1808 if (lvl != SOL_TIPC)
1809 return -ENOPROTOOPT;
1810 res = get_user(len, ol);
1811 if (res)
1812 return res;
1813
1814 lock_sock(sk);
1815
1816 switch (opt) {
1817 case TIPC_IMPORTANCE:
1818 res = tipc_portimportance(tport->ref, &value);
1819 break;
1820 case TIPC_SRC_DROPPABLE:
1821 res = tipc_portunreliable(tport->ref, &value);
1822 break;
1823 case TIPC_DEST_DROPPABLE:
1824 res = tipc_portunreturnable(tport->ref, &value);
1825 break;
1826 case TIPC_CONN_TIMEOUT:
1827 value = tipc_sk(sk)->conn_timeout;
1828 /* no need to set "res", since already 0 at this point */
1829 break;
1830 case TIPC_NODE_RECVQ_DEPTH:
1831 value = 0; /* was tipc_queue_size, now obsolete */
1832 break;
1833 case TIPC_SOCK_RECVQ_DEPTH:
1834 value = skb_queue_len(&sk->sk_receive_queue);
1835 break;
1836 default:
1837 res = -EINVAL;
1838 }
1839
1840 release_sock(sk);
1841
1842 if (res)
1843 return res; /* "get" failed */
1844
1845 if (len < sizeof(value))
1846 return -EINVAL;
1847
1848 if (copy_to_user(ov, &value, sizeof(value)))
1849 return -EFAULT;
1850
1851 return put_user(sizeof(value), ol);
1852}
1853
1854/* Protocol switches for the various types of TIPC sockets */
1855
1856static const struct proto_ops msg_ops = {
1857 .owner = THIS_MODULE,
1858 .family = AF_TIPC,
1859 .release = release,
1860 .bind = bind,
1861 .connect = connect,
1862 .socketpair = sock_no_socketpair,
1863 .accept = sock_no_accept,
1864 .getname = get_name,
1865 .poll = poll,
1866 .ioctl = sock_no_ioctl,
1867 .listen = sock_no_listen,
1868 .shutdown = shutdown,
1869 .setsockopt = setsockopt,
1870 .getsockopt = getsockopt,
1871 .sendmsg = send_msg,
1872 .recvmsg = recv_msg,
1873 .mmap = sock_no_mmap,
1874 .sendpage = sock_no_sendpage
1875};
1876
1877static const struct proto_ops packet_ops = {
1878 .owner = THIS_MODULE,
1879 .family = AF_TIPC,
1880 .release = release,
1881 .bind = bind,
1882 .connect = connect,
1883 .socketpair = sock_no_socketpair,
1884 .accept = accept,
1885 .getname = get_name,
1886 .poll = poll,
1887 .ioctl = sock_no_ioctl,
1888 .listen = listen,
1889 .shutdown = shutdown,
1890 .setsockopt = setsockopt,
1891 .getsockopt = getsockopt,
1892 .sendmsg = send_packet,
1893 .recvmsg = recv_msg,
1894 .mmap = sock_no_mmap,
1895 .sendpage = sock_no_sendpage
1896};
1897
1898static const struct proto_ops stream_ops = {
1899 .owner = THIS_MODULE,
1900 .family = AF_TIPC,
1901 .release = release,
1902 .bind = bind,
1903 .connect = connect,
1904 .socketpair = sock_no_socketpair,
1905 .accept = accept,
1906 .getname = get_name,
1907 .poll = poll,
1908 .ioctl = sock_no_ioctl,
1909 .listen = listen,
1910 .shutdown = shutdown,
1911 .setsockopt = setsockopt,
1912 .getsockopt = getsockopt,
1913 .sendmsg = send_stream,
1914 .recvmsg = recv_stream,
1915 .mmap = sock_no_mmap,
1916 .sendpage = sock_no_sendpage
1917};
1918
1919static const struct net_proto_family tipc_family_ops = {
1920 .owner = THIS_MODULE,
1921 .family = AF_TIPC,
1922 .create = tipc_sk_create
1923};
1924
1925static struct proto tipc_proto = {
1926 .name = "TIPC",
1927 .owner = THIS_MODULE,
1928 .obj_size = sizeof(struct tipc_sock),
1929 .sysctl_rmem = sysctl_tipc_rmem
1930};
1931
1932static struct proto tipc_proto_kern = {
1933 .name = "TIPC",
1934 .obj_size = sizeof(struct tipc_sock),
1935 .sysctl_rmem = sysctl_tipc_rmem
1936};
1937
1938/**
1939 * tipc_socket_init - initialize TIPC socket interface
1940 *
1941 * Returns 0 on success, errno otherwise
1942 */
1943int tipc_socket_init(void)
1944{
1945 int res;
1946
1947 res = proto_register(&tipc_proto, 1);
1948 if (res) {
1949 pr_err("Failed to register TIPC protocol type\n");
1950 goto out;
1951 }
1952
1953 res = sock_register(&tipc_family_ops);
1954 if (res) {
1955 pr_err("Failed to register TIPC socket type\n");
1956 proto_unregister(&tipc_proto);
1957 goto out;
1958 }
1959
1960 sockets_enabled = 1;
1961 out:
1962 return res;
1963}
1964
1965/**
1966 * tipc_socket_stop - stop TIPC socket interface
1967 */
1968void tipc_socket_stop(void)
1969{
1970 if (!sockets_enabled)
1971 return;
1972
1973 sockets_enabled = 0;
1974 sock_unregister(tipc_family_ops.family);
1975 proto_unregister(&tipc_proto);
1976}
This page took 0.03866 seconds and 5 git commands to generate.