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