blob: 361dc342f37737de2985ddbbfb59bc8475f0e5d8 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
3 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2001-2006, Ericsson AB
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2004-2005, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * 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/slab.h>
44#include <linux/poll.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010045#include <linux/fcntl.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010046#include <asm/semaphore.h>
47#include <asm/string.h>
48#include <asm/atomic.h>
49#include <net/sock.h>
50
51#include <linux/tipc.h>
Per Lidenea714cc2006-01-11 12:28:47 +010052#include <linux/tipc_config.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010053#include <net/tipc/tipc_msg.h>
54#include <net/tipc/tipc_port.h>
55
56#include "core.h"
57
58#define SS_LISTENING -1 /* socket is listening */
59#define SS_READY -2 /* socket is connectionless */
60
61#define OVERLOAD_LIMIT_BASE 5000
62
63struct tipc_sock {
64 struct sock sk;
65 struct tipc_port *p;
66 struct semaphore sem;
67};
68
69#define tipc_sk(sk) ((struct tipc_sock*)sk)
70
71static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
72static void wakeupdispatch(struct tipc_port *tport);
73
74static struct proto_ops packet_ops;
75static struct proto_ops stream_ops;
76static struct proto_ops msg_ops;
77
78static struct proto tipc_proto;
79
80static int sockets_enabled = 0;
81
82static atomic_t tipc_queue_size = ATOMIC_INIT(0);
83
84
85/*
86 * sock_lock(): Lock a port/socket pair. lock_sock() can
87 * not be used here, since the same lock must protect ports
88 * with non-socket interfaces.
89 * See net.c for description of locking policy.
90 */
Sam Ravnborg05790c62006-03-20 22:37:04 -080091static void sock_lock(struct tipc_sock* tsock)
Per Lidenb97bf3f2006-01-02 19:04:38 +010092{
93 spin_lock_bh(tsock->p->lock);
94}
95
96/*
97 * sock_unlock(): Unlock a port/socket pair
98 */
Sam Ravnborg05790c62006-03-20 22:37:04 -080099static void sock_unlock(struct tipc_sock* tsock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100100{
101 spin_unlock_bh(tsock->p->lock);
102}
103
104/**
105 * pollmask - determine the current set of poll() events for a socket
106 * @sock: socket structure
107 *
108 * TIPC sets the returned events as follows:
109 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
110 * or if a connection-oriented socket is does not have an active connection
111 * (i.e. a read operation will not block).
112 * b) POLLOUT is set except when a socket's connection has been terminated
113 * (i.e. a write operation will not block).
114 * c) POLLHUP is set when a socket's connection has been terminated.
115 *
116 * IMPORTANT: The fact that a read or write operation will not block does NOT
117 * imply that the operation will succeed!
118 *
119 * Returns pollmask value
120 */
121
Sam Ravnborg05790c62006-03-20 22:37:04 -0800122static u32 pollmask(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123{
124 u32 mask;
125
126 if ((skb_queue_len(&sock->sk->sk_receive_queue) != 0) ||
127 (sock->state == SS_UNCONNECTED) ||
128 (sock->state == SS_DISCONNECTING))
129 mask = (POLLRDNORM | POLLIN);
130 else
131 mask = 0;
132
133 if (sock->state == SS_DISCONNECTING)
134 mask |= POLLHUP;
135 else
136 mask |= POLLOUT;
137
138 return mask;
139}
140
141
142/**
143 * advance_queue - discard first buffer in queue
144 * @tsock: TIPC socket
145 */
146
Sam Ravnborg05790c62006-03-20 22:37:04 -0800147static void advance_queue(struct tipc_sock *tsock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148{
149 sock_lock(tsock);
150 buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));
151 sock_unlock(tsock);
152 atomic_dec(&tipc_queue_size);
153}
154
155/**
156 * tipc_create - create a TIPC socket
157 * @sock: pre-allocated socket structure
158 * @protocol: protocol indicator (must be 0)
159 *
160 * This routine creates and attaches a 'struct sock' to the 'struct socket',
161 * then create and attaches a TIPC port to the 'struct sock' part.
162 *
163 * Returns 0 on success, errno otherwise
164 */
165static int tipc_create(struct socket *sock, int protocol)
166{
167 struct tipc_sock *tsock;
168 struct tipc_port *port;
169 struct sock *sk;
170 u32 ref;
171
172 if ((sock->type != SOCK_STREAM) &&
173 (sock->type != SOCK_SEQPACKET) &&
174 (sock->type != SOCK_DGRAM) &&
175 (sock->type != SOCK_RDM))
176 return -EPROTOTYPE;
177
178 if (unlikely(protocol != 0))
179 return -EPROTONOSUPPORT;
180
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800181 ref = tipc_createport_raw(NULL, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 if (unlikely(!ref))
183 return -ENOMEM;
184
185 sock->state = SS_UNCONNECTED;
186
187 switch (sock->type) {
188 case SOCK_STREAM:
189 sock->ops = &stream_ops;
190 break;
191 case SOCK_SEQPACKET:
192 sock->ops = &packet_ops;
193 break;
194 case SOCK_DGRAM:
195 tipc_set_portunreliable(ref, 1);
196 /* fall through */
197 case SOCK_RDM:
198 tipc_set_portunreturnable(ref, 1);
199 sock->ops = &msg_ops;
200 sock->state = SS_READY;
201 break;
202 }
203
204 sk = sk_alloc(AF_TIPC, GFP_KERNEL, &tipc_proto, 1);
205 if (!sk) {
206 tipc_deleteport(ref);
207 return -ENOMEM;
208 }
209
210 sock_init_data(sock, sk);
211 init_waitqueue_head(sk->sk_sleep);
212 sk->sk_rcvtimeo = 8 * HZ; /* default connect timeout = 8s */
213
214 tsock = tipc_sk(sk);
215 port = tipc_get_port(ref);
216
217 tsock->p = port;
218 port->usr_handle = tsock;
219
220 init_MUTEX(&tsock->sem);
221
222 dbg("sock_create: %x\n",tsock);
223
224 atomic_inc(&tipc_user_count);
225
226 return 0;
227}
228
229/**
230 * release - destroy a TIPC socket
231 * @sock: socket to destroy
232 *
233 * This routine cleans up any messages that are still queued on the socket.
234 * For DGRAM and RDM socket types, all queued messages are rejected.
235 * For SEQPACKET and STREAM socket types, the first message is rejected
236 * and any others are discarded. (If the first message on a STREAM socket
237 * is partially-read, it is discarded and the next one is rejected instead.)
238 *
239 * NOTE: Rejected messages are not necessarily returned to the sender! They
240 * are returned or discarded according to the "destination droppable" setting
241 * specified for the message by the sender.
242 *
243 * Returns 0 on success, errno otherwise
244 */
245
246static int release(struct socket *sock)
247{
248 struct tipc_sock *tsock = tipc_sk(sock->sk);
249 struct sock *sk = sock->sk;
250 int res = TIPC_OK;
251 struct sk_buff *buf;
252
253 dbg("sock_delete: %x\n",tsock);
254 if (!tsock)
255 return 0;
256 down_interruptible(&tsock->sem);
257 if (!sock->sk) {
258 up(&tsock->sem);
259 return 0;
260 }
261
262 /* Reject unreceived messages, unless no longer connected */
263
264 while (sock->state != SS_DISCONNECTING) {
265 sock_lock(tsock);
266 buf = skb_dequeue(&sk->sk_receive_queue);
267 if (!buf)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800268 tsock->p->usr_handle = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269 sock_unlock(tsock);
270 if (!buf)
271 break;
272 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
273 buf_discard(buf);
274 else
275 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
276 atomic_dec(&tipc_queue_size);
277 }
278
279 /* Delete TIPC port */
280
281 res = tipc_deleteport(tsock->p->ref);
282 sock->sk = NULL;
283
284 /* Discard any remaining messages */
285
286 while ((buf = skb_dequeue(&sk->sk_receive_queue))) {
287 buf_discard(buf);
288 atomic_dec(&tipc_queue_size);
289 }
290
291 up(&tsock->sem);
292
293 sock_put(sk);
294
295 atomic_dec(&tipc_user_count);
296 return res;
297}
298
299/**
300 * bind - associate or disassocate TIPC name(s) with a socket
301 * @sock: socket structure
302 * @uaddr: socket address describing name(s) and desired operation
303 * @uaddr_len: size of socket address data structure
304 *
305 * Name and name sequence binding is indicated using a positive scope value;
306 * a negative scope value unbinds the specified name. Specifying no name
307 * (i.e. a socket address length of 0) unbinds all names from the socket.
308 *
309 * Returns 0 on success, errno otherwise
310 */
311
312static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
313{
314 struct tipc_sock *tsock = tipc_sk(sock->sk);
315 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
316 int res;
317
318 if (down_interruptible(&tsock->sem))
319 return -ERESTARTSYS;
320
321 if (unlikely(!uaddr_len)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800322 res = tipc_withdraw(tsock->p->ref, 0, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323 goto exit;
324 }
325
326 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
327 res = -EINVAL;
328 goto exit;
329 }
330
331 if (addr->family != AF_TIPC) {
332 res = -EAFNOSUPPORT;
333 goto exit;
334 }
335 if (addr->addrtype == TIPC_ADDR_NAME)
336 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
337 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
338 res = -EAFNOSUPPORT;
339 goto exit;
340 }
341
342 if (addr->scope > 0)
343 res = tipc_publish(tsock->p->ref, addr->scope,
344 &addr->addr.nameseq);
345 else
346 res = tipc_withdraw(tsock->p->ref, -addr->scope,
347 &addr->addr.nameseq);
348exit:
349 up(&tsock->sem);
350 return res;
351}
352
353/**
354 * get_name - get port ID of socket or peer socket
355 * @sock: socket structure
356 * @uaddr: area for returned socket address
357 * @uaddr_len: area for returned length of socket address
358 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
359 *
360 * Returns 0 on success, errno otherwise
361 */
362
363static int get_name(struct socket *sock, struct sockaddr *uaddr,
364 int *uaddr_len, int peer)
365{
366 struct tipc_sock *tsock = tipc_sk(sock->sk);
367 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
368 u32 res;
369
370 if (down_interruptible(&tsock->sem))
371 return -ERESTARTSYS;
372
373 *uaddr_len = sizeof(*addr);
374 addr->addrtype = TIPC_ADDR_ID;
375 addr->family = AF_TIPC;
376 addr->scope = 0;
377 if (peer)
378 res = tipc_peer(tsock->p->ref, &addr->addr.id);
379 else
380 res = tipc_ownidentity(tsock->p->ref, &addr->addr.id);
381 addr->addr.name.domain = 0;
382
383 up(&tsock->sem);
384 return res;
385}
386
387/**
388 * poll - read and possibly block on pollmask
389 * @file: file structure associated with the socket
390 * @sock: socket for which to calculate the poll bits
391 * @wait: ???
392 *
393 * Returns the pollmask
394 */
395
396static unsigned int poll(struct file *file, struct socket *sock,
397 poll_table *wait)
398{
399 poll_wait(file, sock->sk->sk_sleep, wait);
400 /* NEED LOCK HERE? */
401 return pollmask(sock);
402}
403
404/**
405 * dest_name_check - verify user is permitted to send to specified port name
406 * @dest: destination address
407 * @m: descriptor for message to be sent
408 *
409 * Prevents restricted configuration commands from being issued by
410 * unauthorized users.
411 *
412 * Returns 0 if permission is granted, otherwise errno
413 */
414
Sam Ravnborg05790c62006-03-20 22:37:04 -0800415static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416{
417 struct tipc_cfg_msg_hdr hdr;
418
419 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
420 return 0;
421 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
422 return 0;
423
424 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
425 return -EACCES;
426
427 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
428 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700429 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 return -EACCES;
431
432 return 0;
433}
434
435/**
436 * send_msg - send message in connectionless manner
437 * @iocb: (unused)
438 * @sock: socket structure
439 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700440 * @total_len: length of message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 *
442 * Message must have an destination specified explicitly.
443 * Used for SOCK_RDM and SOCK_DGRAM messages,
444 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
445 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
446 *
447 * Returns the number of bytes sent on success, or errno otherwise
448 */
449
450static int send_msg(struct kiocb *iocb, struct socket *sock,
451 struct msghdr *m, size_t total_len)
452{
453 struct tipc_sock *tsock = tipc_sk(sock->sk);
454 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
455 struct sk_buff *buf;
456 int needs_conn;
457 int res = -EINVAL;
458
459 if (unlikely(!dest))
460 return -EDESTADDRREQ;
461 if (unlikely(dest->family != AF_TIPC))
462 return -EINVAL;
463
464 needs_conn = (sock->state != SS_READY);
465 if (unlikely(needs_conn)) {
466 if (sock->state == SS_LISTENING)
467 return -EPIPE;
468 if (sock->state != SS_UNCONNECTED)
469 return -EISCONN;
470 if ((tsock->p->published) ||
471 ((sock->type == SOCK_STREAM) && (total_len != 0)))
472 return -EOPNOTSUPP;
Allan Stephens33880072006-06-25 23:44:57 -0700473 if (dest->addrtype == TIPC_ADDR_NAME) {
474 tsock->p->conn_type = dest->addr.name.name.type;
475 tsock->p->conn_instance = dest->addr.name.name.instance;
476 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 }
478
479 if (down_interruptible(&tsock->sem))
480 return -ERESTARTSYS;
481
482 if (needs_conn) {
483
484 /* Abort any pending connection attempts (very unlikely) */
485
486 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
487 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
488 atomic_dec(&tipc_queue_size);
489 }
490
491 sock->state = SS_CONNECTING;
492 }
493
494 do {
495 if (dest->addrtype == TIPC_ADDR_NAME) {
496 if ((res = dest_name_check(dest, m)))
497 goto exit;
498 res = tipc_send2name(tsock->p->ref,
499 &dest->addr.name.name,
500 dest->addr.name.domain,
501 m->msg_iovlen,
502 m->msg_iov);
503 }
504 else if (dest->addrtype == TIPC_ADDR_ID) {
505 res = tipc_send2port(tsock->p->ref,
506 &dest->addr.id,
507 m->msg_iovlen,
508 m->msg_iov);
509 }
510 else if (dest->addrtype == TIPC_ADDR_MCAST) {
511 if (needs_conn) {
512 res = -EOPNOTSUPP;
513 goto exit;
514 }
515 if ((res = dest_name_check(dest, m)))
516 goto exit;
517 res = tipc_multicast(tsock->p->ref,
518 &dest->addr.nameseq,
519 0,
520 m->msg_iovlen,
521 m->msg_iov);
522 }
523 if (likely(res != -ELINKCONG)) {
524exit:
525 up(&tsock->sem);
526 return res;
527 }
528 if (m->msg_flags & MSG_DONTWAIT) {
529 res = -EWOULDBLOCK;
530 goto exit;
531 }
532 if (wait_event_interruptible(*sock->sk->sk_sleep,
533 !tsock->p->congested)) {
534 res = -ERESTARTSYS;
535 goto exit;
536 }
537 } while (1);
538}
539
540/**
541 * send_packet - send a connection-oriented message
542 * @iocb: (unused)
543 * @sock: socket structure
544 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700545 * @total_len: length of message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 *
547 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
548 *
549 * Returns the number of bytes sent on success, or errno otherwise
550 */
551
552static int send_packet(struct kiocb *iocb, struct socket *sock,
553 struct msghdr *m, size_t total_len)
554{
555 struct tipc_sock *tsock = tipc_sk(sock->sk);
556 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
557 int res;
558
559 /* Handle implied connection establishment */
560
561 if (unlikely(dest))
562 return send_msg(iocb, sock, m, total_len);
563
564 if (down_interruptible(&tsock->sem)) {
565 return -ERESTARTSYS;
566 }
567
568 if (unlikely(sock->state != SS_CONNECTED)) {
569 if (sock->state == SS_DISCONNECTING)
570 res = -EPIPE;
571 else
572 res = -ENOTCONN;
573 goto exit;
574 }
575
576 do {
577 res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov);
578 if (likely(res != -ELINKCONG)) {
579exit:
580 up(&tsock->sem);
581 return res;
582 }
583 if (m->msg_flags & MSG_DONTWAIT) {
584 res = -EWOULDBLOCK;
585 goto exit;
586 }
587 if (wait_event_interruptible(*sock->sk->sk_sleep,
588 !tsock->p->congested)) {
589 res = -ERESTARTSYS;
590 goto exit;
591 }
592 } while (1);
593}
594
595/**
596 * send_stream - send stream-oriented data
597 * @iocb: (unused)
598 * @sock: socket structure
599 * @m: data to send
600 * @total_len: total length of data to be sent
601 *
602 * Used for SOCK_STREAM data.
603 *
604 * Returns the number of bytes sent on success, or errno otherwise
605 */
606
607
608static int send_stream(struct kiocb *iocb, struct socket *sock,
609 struct msghdr *m, size_t total_len)
610{
611 struct msghdr my_msg;
612 struct iovec my_iov;
613 struct iovec *curr_iov;
614 int curr_iovlen;
615 char __user *curr_start;
616 int curr_left;
617 int bytes_to_send;
618 int res;
619
620 if (likely(total_len <= TIPC_MAX_USER_MSG_SIZE))
621 return send_packet(iocb, sock, m, total_len);
622
623 /* Can only send large data streams if already connected */
624
625 if (unlikely(sock->state != SS_CONNECTED)) {
626 if (sock->state == SS_DISCONNECTING)
627 return -EPIPE;
628 else
629 return -ENOTCONN;
630 }
631
632 /*
633 * Send each iovec entry using one or more messages
634 *
635 * Note: This algorithm is good for the most likely case
636 * (i.e. one large iovec entry), but could be improved to pass sets
637 * of small iovec entries into send_packet().
638 */
639
640 my_msg = *m;
641 curr_iov = my_msg.msg_iov;
642 curr_iovlen = my_msg.msg_iovlen;
643 my_msg.msg_iov = &my_iov;
644 my_msg.msg_iovlen = 1;
645
646 while (curr_iovlen--) {
647 curr_start = curr_iov->iov_base;
648 curr_left = curr_iov->iov_len;
649
650 while (curr_left) {
651 bytes_to_send = (curr_left < TIPC_MAX_USER_MSG_SIZE)
652 ? curr_left : TIPC_MAX_USER_MSG_SIZE;
653 my_iov.iov_base = curr_start;
654 my_iov.iov_len = bytes_to_send;
655 if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0)
656 return res;
657 curr_left -= bytes_to_send;
658 curr_start += bytes_to_send;
659 }
660
661 curr_iov++;
662 }
663
664 return total_len;
665}
666
667/**
668 * auto_connect - complete connection setup to a remote port
669 * @sock: socket structure
670 * @tsock: TIPC-specific socket structure
671 * @msg: peer's response message
672 *
673 * Returns 0 on success, errno otherwise
674 */
675
676static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
677 struct tipc_msg *msg)
678{
679 struct tipc_portid peer;
680
681 if (msg_errcode(msg)) {
682 sock->state = SS_DISCONNECTING;
683 return -ECONNREFUSED;
684 }
685
686 peer.ref = msg_origport(msg);
687 peer.node = msg_orignode(msg);
688 tipc_connect2port(tsock->p->ref, &peer);
689 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
690 sock->state = SS_CONNECTED;
691 return 0;
692}
693
694/**
695 * set_orig_addr - capture sender's address for received message
696 * @m: descriptor for message info
697 * @msg: received message header
698 *
699 * Note: Address is not captured if not requested by receiver.
700 */
701
Sam Ravnborg05790c62006-03-20 22:37:04 -0800702static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703{
704 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
705
706 if (addr) {
707 addr->family = AF_TIPC;
708 addr->addrtype = TIPC_ADDR_ID;
709 addr->addr.id.ref = msg_origport(msg);
710 addr->addr.id.node = msg_orignode(msg);
711 addr->addr.name.domain = 0; /* could leave uninitialized */
712 addr->scope = 0; /* could leave uninitialized */
713 m->msg_namelen = sizeof(struct sockaddr_tipc);
714 }
715}
716
717/**
718 * anc_data_recv - optionally capture ancillary data for received message
719 * @m: descriptor for message info
720 * @msg: received message header
721 * @tport: TIPC port associated with message
722 *
723 * Note: Ancillary data is not captured if not requested by receiver.
724 *
725 * Returns 0 if successful, otherwise errno
726 */
727
Sam Ravnborg05790c62006-03-20 22:37:04 -0800728static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 struct tipc_port *tport)
730{
731 u32 anc_data[3];
732 u32 err;
733 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700734 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 int res;
736
737 if (likely(m->msg_controllen == 0))
738 return 0;
739
740 /* Optionally capture errored message object(s) */
741
742 err = msg ? msg_errcode(msg) : 0;
743 if (unlikely(err)) {
744 anc_data[0] = err;
745 anc_data[1] = msg_data_sz(msg);
746 if ((res = put_cmsg(m, SOL_SOCKET, TIPC_ERRINFO, 8, anc_data)))
747 return res;
748 if (anc_data[1] &&
749 (res = put_cmsg(m, SOL_SOCKET, TIPC_RETDATA, anc_data[1],
750 msg_data(msg))))
751 return res;
752 }
753
754 /* Optionally capture message destination object */
755
756 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
757 switch (dest_type) {
758 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700759 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 anc_data[0] = msg_nametype(msg);
761 anc_data[1] = msg_namelower(msg);
762 anc_data[2] = msg_namelower(msg);
763 break;
764 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700765 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 anc_data[0] = msg_nametype(msg);
767 anc_data[1] = msg_namelower(msg);
768 anc_data[2] = msg_nameupper(msg);
769 break;
770 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700771 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 anc_data[0] = tport->conn_type;
773 anc_data[1] = tport->conn_instance;
774 anc_data[2] = tport->conn_instance;
775 break;
776 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700777 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 }
Allan Stephens3546c752006-06-25 23:45:24 -0700779 if (has_name &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 (res = put_cmsg(m, SOL_SOCKET, TIPC_DESTNAME, 12, anc_data)))
781 return res;
782
783 return 0;
784}
785
786/**
787 * recv_msg - receive packet-oriented message
788 * @iocb: (unused)
789 * @m: descriptor for message info
790 * @buf_len: total size of user buffer area
791 * @flags: receive flags
792 *
793 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
794 * If the complete message doesn't fit in user area, truncate it.
795 *
796 * Returns size of returned message data, errno otherwise
797 */
798
799static int recv_msg(struct kiocb *iocb, struct socket *sock,
800 struct msghdr *m, size_t buf_len, int flags)
801{
802 struct tipc_sock *tsock = tipc_sk(sock->sk);
803 struct sk_buff *buf;
804 struct tipc_msg *msg;
805 unsigned int q_len;
806 unsigned int sz;
807 u32 err;
808 int res;
809
810 /* Currently doesn't support receiving into multiple iovec entries */
811
812 if (m->msg_iovlen != 1)
813 return -EOPNOTSUPP;
814
815 /* Catch invalid receive attempts */
816
817 if (unlikely(!buf_len))
818 return -EINVAL;
819
820 if (sock->type == SOCK_SEQPACKET) {
821 if (unlikely(sock->state == SS_UNCONNECTED))
822 return -ENOTCONN;
823 if (unlikely((sock->state == SS_DISCONNECTING) &&
824 (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
825 return -ENOTCONN;
826 }
827
828 /* Look for a message in receive queue; wait if necessary */
829
830 if (unlikely(down_interruptible(&tsock->sem)))
831 return -ERESTARTSYS;
832
833restart:
834 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
835 (flags & MSG_DONTWAIT))) {
836 res = -EWOULDBLOCK;
837 goto exit;
838 }
839
840 if ((res = wait_event_interruptible(
841 *sock->sk->sk_sleep,
842 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
843 (sock->state == SS_DISCONNECTING))) )) {
844 goto exit;
845 }
846
847 /* Catch attempt to receive on an already terminated connection */
848 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
849
850 if (!q_len) {
851 res = -ENOTCONN;
852 goto exit;
853 }
854
855 /* Get access to first message in receive queue */
856
857 buf = skb_peek(&sock->sk->sk_receive_queue);
858 msg = buf_msg(buf);
859 sz = msg_data_sz(msg);
860 err = msg_errcode(msg);
861
862 /* Complete connection setup for an implied connect */
863
864 if (unlikely(sock->state == SS_CONNECTING)) {
865 if ((res = auto_connect(sock, tsock, msg)))
866 goto exit;
867 }
868
869 /* Discard an empty non-errored message & try again */
870
871 if ((!sz) && (!err)) {
872 advance_queue(tsock);
873 goto restart;
874 }
875
876 /* Capture sender's address (optional) */
877
878 set_orig_addr(m, msg);
879
880 /* Capture ancillary data (optional) */
881
882 if ((res = anc_data_recv(m, msg, tsock->p)))
883 goto exit;
884
885 /* Capture message data (if valid) & compute return value (always) */
886
887 if (!err) {
888 if (unlikely(buf_len < sz)) {
889 sz = buf_len;
890 m->msg_flags |= MSG_TRUNC;
891 }
892 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
893 sz))) {
894 res = -EFAULT;
895 goto exit;
896 }
897 res = sz;
898 } else {
899 if ((sock->state == SS_READY) ||
900 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
901 res = 0;
902 else
903 res = -ECONNRESET;
904 }
905
906 /* Consume received message (optional) */
907
908 if (likely(!(flags & MSG_PEEK))) {
909 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
910 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
911 advance_queue(tsock);
912 }
913exit:
914 up(&tsock->sem);
915 return res;
916}
917
918/**
919 * recv_stream - receive stream-oriented data
920 * @iocb: (unused)
921 * @m: descriptor for message info
922 * @buf_len: total size of user buffer area
923 * @flags: receive flags
924 *
925 * Used for SOCK_STREAM messages only. If not enough data is available
926 * will optionally wait for more; never truncates data.
927 *
928 * Returns size of returned message data, errno otherwise
929 */
930
931static int recv_stream(struct kiocb *iocb, struct socket *sock,
932 struct msghdr *m, size_t buf_len, int flags)
933{
934 struct tipc_sock *tsock = tipc_sk(sock->sk);
935 struct sk_buff *buf;
936 struct tipc_msg *msg;
937 unsigned int q_len;
938 unsigned int sz;
939 int sz_to_copy;
940 int sz_copied = 0;
941 int needed;
942 char *crs = m->msg_iov->iov_base;
943 unsigned char *buf_crs;
944 u32 err;
945 int res;
946
947 /* Currently doesn't support receiving into multiple iovec entries */
948
949 if (m->msg_iovlen != 1)
950 return -EOPNOTSUPP;
951
952 /* Catch invalid receive attempts */
953
954 if (unlikely(!buf_len))
955 return -EINVAL;
956
957 if (unlikely(sock->state == SS_DISCONNECTING)) {
958 if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
959 return -ENOTCONN;
960 } else if (unlikely(sock->state != SS_CONNECTED))
961 return -ENOTCONN;
962
963 /* Look for a message in receive queue; wait if necessary */
964
965 if (unlikely(down_interruptible(&tsock->sem)))
966 return -ERESTARTSYS;
967
968restart:
969 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
970 (flags & MSG_DONTWAIT))) {
971 res = (sz_copied == 0) ? -EWOULDBLOCK : 0;
972 goto exit;
973 }
974
975 if ((res = wait_event_interruptible(
976 *sock->sk->sk_sleep,
977 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
978 (sock->state == SS_DISCONNECTING))) )) {
979 goto exit;
980 }
981
982 /* Catch attempt to receive on an already terminated connection */
983 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
984
985 if (!q_len) {
986 res = -ENOTCONN;
987 goto exit;
988 }
989
990 /* Get access to first message in receive queue */
991
992 buf = skb_peek(&sock->sk->sk_receive_queue);
993 msg = buf_msg(buf);
994 sz = msg_data_sz(msg);
995 err = msg_errcode(msg);
996
997 /* Discard an empty non-errored message & try again */
998
999 if ((!sz) && (!err)) {
1000 advance_queue(tsock);
1001 goto restart;
1002 }
1003
1004 /* Optionally capture sender's address & ancillary data of first msg */
1005
1006 if (sz_copied == 0) {
1007 set_orig_addr(m, msg);
1008 if ((res = anc_data_recv(m, msg, tsock->p)))
1009 goto exit;
1010 }
1011
1012 /* Capture message data (if valid) & compute return value (always) */
1013
1014 if (!err) {
1015 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
1016 sz = buf->tail - buf_crs;
1017
1018 needed = (buf_len - sz_copied);
1019 sz_to_copy = (sz <= needed) ? sz : needed;
1020 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1021 res = -EFAULT;
1022 goto exit;
1023 }
1024 sz_copied += sz_to_copy;
1025
1026 if (sz_to_copy < sz) {
1027 if (!(flags & MSG_PEEK))
1028 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1029 goto exit;
1030 }
1031
1032 crs += sz_to_copy;
1033 } else {
1034 if (sz_copied != 0)
1035 goto exit; /* can't add error msg to valid data */
1036
1037 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1038 res = 0;
1039 else
1040 res = -ECONNRESET;
1041 }
1042
1043 /* Consume received message (optional) */
1044
1045 if (likely(!(flags & MSG_PEEK))) {
1046 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1047 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
1048 advance_queue(tsock);
1049 }
1050
1051 /* Loop around if more data is required */
1052
1053 if ((sz_copied < buf_len) /* didn't get all requested data */
1054 && (flags & MSG_WAITALL) /* ... and need to wait for more */
1055 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1056 && (!err) /* ... and haven't reached a FIN */
1057 )
1058 goto restart;
1059
1060exit:
1061 up(&tsock->sem);
1062 return res ? res : sz_copied;
1063}
1064
1065/**
1066 * queue_overloaded - test if queue overload condition exists
1067 * @queue_size: current size of queue
1068 * @base: nominal maximum size of queue
1069 * @msg: message to be added to queue
1070 *
1071 * Returns 1 if queue is currently overloaded, 0 otherwise
1072 */
1073
1074static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg)
1075{
1076 u32 threshold;
1077 u32 imp = msg_importance(msg);
1078
1079 if (imp == TIPC_LOW_IMPORTANCE)
1080 threshold = base;
1081 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1082 threshold = base * 2;
1083 else if (imp == TIPC_HIGH_IMPORTANCE)
1084 threshold = base * 100;
1085 else
1086 return 0;
1087
1088 if (msg_connected(msg))
1089 threshold *= 4;
1090
1091 return (queue_size > threshold);
1092}
1093
1094/**
1095 * async_disconnect - wrapper function used to disconnect port
1096 * @portref: TIPC port reference (passed as pointer-sized value)
1097 */
1098
1099static void async_disconnect(unsigned long portref)
1100{
1101 tipc_disconnect((u32)portref);
1102}
1103
1104/**
1105 * dispatch - handle arriving message
1106 * @tport: TIPC port that received message
1107 * @buf: message
1108 *
1109 * Called with port locked. Must not take socket lock to avoid deadlock risk.
1110 *
1111 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1112 */
1113
1114static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1115{
1116 struct tipc_msg *msg = buf_msg(buf);
1117 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1118 struct socket *sock;
1119 u32 recv_q_len;
1120
1121 /* Reject message if socket is closing */
1122
1123 if (!tsock)
1124 return TIPC_ERR_NO_PORT;
1125
1126 /* Reject message if it is wrong sort of message for socket */
1127
1128 /*
1129 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1130 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1131 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1132 */
1133 sock = tsock->sk.sk_socket;
1134 if (sock->state == SS_READY) {
1135 if (msg_connected(msg)) {
1136 msg_dbg(msg, "dispatch filter 1\n");
1137 return TIPC_ERR_NO_PORT;
1138 }
1139 } else {
1140 if (msg_mcast(msg)) {
1141 msg_dbg(msg, "dispatch filter 2\n");
1142 return TIPC_ERR_NO_PORT;
1143 }
1144 if (sock->state == SS_CONNECTED) {
1145 if (!msg_connected(msg)) {
1146 msg_dbg(msg, "dispatch filter 3\n");
1147 return TIPC_ERR_NO_PORT;
1148 }
1149 }
1150 else if (sock->state == SS_CONNECTING) {
1151 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1152 msg_dbg(msg, "dispatch filter 4\n");
1153 return TIPC_ERR_NO_PORT;
1154 }
1155 }
1156 else if (sock->state == SS_LISTENING) {
1157 if (msg_connected(msg) || msg_errcode(msg)) {
1158 msg_dbg(msg, "dispatch filter 5\n");
1159 return TIPC_ERR_NO_PORT;
1160 }
1161 }
1162 else if (sock->state == SS_DISCONNECTING) {
1163 msg_dbg(msg, "dispatch filter 6\n");
1164 return TIPC_ERR_NO_PORT;
1165 }
1166 else /* (sock->state == SS_UNCONNECTED) */ {
1167 if (msg_connected(msg) || msg_errcode(msg)) {
1168 msg_dbg(msg, "dispatch filter 7\n");
1169 return TIPC_ERR_NO_PORT;
1170 }
1171 }
1172 }
1173
1174 /* Reject message if there isn't room to queue it */
1175
1176 if (unlikely((u32)atomic_read(&tipc_queue_size) >
1177 OVERLOAD_LIMIT_BASE)) {
1178 if (queue_overloaded(atomic_read(&tipc_queue_size),
1179 OVERLOAD_LIMIT_BASE, msg))
1180 return TIPC_ERR_OVERLOAD;
1181 }
1182 recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
1183 if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) {
1184 if (queue_overloaded(recv_q_len,
1185 OVERLOAD_LIMIT_BASE / 2, msg))
1186 return TIPC_ERR_OVERLOAD;
1187 }
1188
1189 /* Initiate connection termination for an incoming 'FIN' */
1190
1191 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1192 sock->state = SS_DISCONNECTING;
1193 /* Note: Use signal since port lock is already taken! */
Per Liden4323add2006-01-18 00:38:21 +01001194 tipc_k_signal((Handler)async_disconnect, tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 }
1196
1197 /* Enqueue message (finally!) */
1198
1199 msg_dbg(msg,"<DISP<: ");
1200 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1201 atomic_inc(&tipc_queue_size);
1202 skb_queue_tail(&sock->sk->sk_receive_queue, buf);
1203
1204 wake_up_interruptible(sock->sk->sk_sleep);
1205 return TIPC_OK;
1206}
1207
1208/**
1209 * wakeupdispatch - wake up port after congestion
1210 * @tport: port to wakeup
1211 *
1212 * Called with port lock on.
1213 */
1214
1215static void wakeupdispatch(struct tipc_port *tport)
1216{
1217 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1218
1219 wake_up_interruptible(tsock->sk.sk_sleep);
1220}
1221
1222/**
1223 * connect - establish a connection to another TIPC port
1224 * @sock: socket structure
1225 * @dest: socket address for destination port
1226 * @destlen: size of socket address data structure
1227 * @flags: (unused)
1228 *
1229 * Returns 0 on success, errno otherwise
1230 */
1231
1232static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1233 int flags)
1234{
1235 struct tipc_sock *tsock = tipc_sk(sock->sk);
1236 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001237 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 struct sk_buff *buf;
1239 struct tipc_msg *msg;
1240 int res;
1241
1242 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1243
1244 if (sock->state == SS_READY)
1245 return -EOPNOTSUPP;
1246
1247 /* MOVE THE REST OF THIS ERROR CHECKING TO send_msg()? */
1248 if (sock->state == SS_LISTENING)
1249 return -EOPNOTSUPP;
1250 if (sock->state == SS_CONNECTING)
1251 return -EALREADY;
1252 if (sock->state != SS_UNCONNECTED)
1253 return -EISCONN;
1254
Allan Stephens6b384de2006-06-25 23:44:27 -07001255 if ((destlen < sizeof(*dst)) || (dst->family != AF_TIPC) ||
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 ((dst->addrtype != TIPC_ADDR_NAME) && (dst->addrtype != TIPC_ADDR_ID)))
1257 return -EINVAL;
1258
1259 /* Send a 'SYN-' to destination */
1260
1261 m.msg_name = dest;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001262 if ((res = send_msg(NULL, sock, &m, 0)) < 0) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001263 sock->state = SS_DISCONNECTING;
1264 return res;
1265 }
1266
1267 if (down_interruptible(&tsock->sem))
1268 return -ERESTARTSYS;
1269
1270 /* Wait for destination's 'ACK' response */
1271
1272 res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
1273 skb_queue_len(&sock->sk->sk_receive_queue),
1274 sock->sk->sk_rcvtimeo);
1275 buf = skb_peek(&sock->sk->sk_receive_queue);
1276 if (res > 0) {
1277 msg = buf_msg(buf);
1278 res = auto_connect(sock, tsock, msg);
1279 if (!res) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280 if (!msg_data_sz(msg))
1281 advance_queue(tsock);
1282 }
1283 } else {
1284 if (res == 0) {
1285 res = -ETIMEDOUT;
1286 } else
1287 { /* leave "res" unchanged */ }
1288 sock->state = SS_DISCONNECTING;
1289 }
1290
1291 up(&tsock->sem);
1292 return res;
1293}
1294
1295/**
1296 * listen - allow socket to listen for incoming connections
1297 * @sock: socket structure
1298 * @len: (unused)
1299 *
1300 * Returns 0 on success, errno otherwise
1301 */
1302
1303static int listen(struct socket *sock, int len)
1304{
1305 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1306
1307 if (sock->state == SS_READY)
1308 return -EOPNOTSUPP;
1309 if (sock->state != SS_UNCONNECTED)
1310 return -EINVAL;
1311 sock->state = SS_LISTENING;
1312 return 0;
1313}
1314
1315/**
1316 * accept - wait for connection request
1317 * @sock: listening socket
1318 * @newsock: new socket that is to be connected
1319 * @flags: file-related flags associated with socket
1320 *
1321 * Returns 0 on success, errno otherwise
1322 */
1323
1324static int accept(struct socket *sock, struct socket *newsock, int flags)
1325{
1326 struct tipc_sock *tsock = tipc_sk(sock->sk);
1327 struct sk_buff *buf;
1328 int res = -EFAULT;
1329
1330 if (sock->state == SS_READY)
1331 return -EOPNOTSUPP;
1332 if (sock->state != SS_LISTENING)
1333 return -EINVAL;
1334
1335 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
1336 (flags & O_NONBLOCK)))
1337 return -EWOULDBLOCK;
1338
1339 if (down_interruptible(&tsock->sem))
1340 return -ERESTARTSYS;
1341
1342 if (wait_event_interruptible(*sock->sk->sk_sleep,
1343 skb_queue_len(&sock->sk->sk_receive_queue))) {
1344 res = -ERESTARTSYS;
1345 goto exit;
1346 }
1347 buf = skb_peek(&sock->sk->sk_receive_queue);
1348
1349 res = tipc_create(newsock, 0);
1350 if (!res) {
1351 struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
1352 struct tipc_portid id;
1353 struct tipc_msg *msg = buf_msg(buf);
1354 u32 new_ref = new_tsock->p->ref;
1355
1356 id.ref = msg_origport(msg);
1357 id.node = msg_orignode(msg);
1358 tipc_connect2port(new_ref, &id);
1359 newsock->state = SS_CONNECTED;
1360
1361 tipc_set_portimportance(new_ref, msg_importance(msg));
1362 if (msg_named(msg)) {
1363 new_tsock->p->conn_type = msg_nametype(msg);
1364 new_tsock->p->conn_instance = msg_nameinst(msg);
1365 }
1366
1367 /*
1368 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1369 * Respond to 'SYN+' by queuing it on new socket.
1370 */
1371
1372 msg_dbg(msg,"<ACC<: ");
1373 if (!msg_data_sz(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001374 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001376 send_packet(NULL, newsock, &m, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 advance_queue(tsock);
1378 } else {
1379 sock_lock(tsock);
1380 skb_dequeue(&sock->sk->sk_receive_queue);
1381 sock_unlock(tsock);
1382 skb_queue_head(&newsock->sk->sk_receive_queue, buf);
1383 }
1384 }
1385exit:
1386 up(&tsock->sem);
1387 return res;
1388}
1389
1390/**
1391 * shutdown - shutdown socket connection
1392 * @sock: socket structure
Allan Stephense9024f0f2006-06-25 23:43:57 -07001393 * @how: direction to close (unused; always treated as read + write)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394 *
1395 * Terminates connection (if necessary), then purges socket's receive queue.
1396 *
1397 * Returns 0 on success, errno otherwise
1398 */
1399
1400static int shutdown(struct socket *sock, int how)
1401{
1402 struct tipc_sock* tsock = tipc_sk(sock->sk);
1403 struct sk_buff *buf;
1404 int res;
1405
1406 /* Could return -EINVAL for an invalid "how", but why bother? */
1407
1408 if (down_interruptible(&tsock->sem))
1409 return -ERESTARTSYS;
1410
1411 sock_lock(tsock);
1412
1413 switch (sock->state) {
1414 case SS_CONNECTED:
1415
1416 /* Send 'FIN+' or 'FIN-' message to peer */
1417
1418 sock_unlock(tsock);
1419restart:
1420 if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1421 atomic_dec(&tipc_queue_size);
1422 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1423 buf_discard(buf);
1424 goto restart;
1425 }
1426 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1427 }
1428 else {
1429 tipc_shutdown(tsock->p->ref);
1430 }
1431 sock_lock(tsock);
1432
1433 /* fall through */
1434
1435 case SS_DISCONNECTING:
1436
1437 /* Discard any unreceived messages */
1438
1439 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1440 atomic_dec(&tipc_queue_size);
1441 buf_discard(buf);
1442 }
1443 tsock->p->conn_unacked = 0;
1444
1445 /* fall through */
1446
1447 case SS_CONNECTING:
1448 sock->state = SS_DISCONNECTING;
1449 res = 0;
1450 break;
1451
1452 default:
1453 res = -ENOTCONN;
1454 }
1455
1456 sock_unlock(tsock);
1457
1458 up(&tsock->sem);
1459 return res;
1460}
1461
1462/**
1463 * setsockopt - set socket option
1464 * @sock: socket structure
1465 * @lvl: option level
1466 * @opt: option identifier
1467 * @ov: pointer to new option value
1468 * @ol: length of option value
1469 *
1470 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1471 * (to ease compatibility).
1472 *
1473 * Returns 0 on success, errno otherwise
1474 */
1475
Allan Stephense9024f0f2006-06-25 23:43:57 -07001476static int setsockopt(struct socket *sock,
1477 int lvl, int opt, char __user *ov, int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478{
1479 struct tipc_sock *tsock = tipc_sk(sock->sk);
1480 u32 value;
1481 int res;
1482
1483 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1484 return 0;
1485 if (lvl != SOL_TIPC)
1486 return -ENOPROTOOPT;
1487 if (ol < sizeof(value))
1488 return -EINVAL;
1489 if ((res = get_user(value, (u32 *)ov)))
1490 return res;
1491
1492 if (down_interruptible(&tsock->sem))
1493 return -ERESTARTSYS;
1494
1495 switch (opt) {
1496 case TIPC_IMPORTANCE:
1497 res = tipc_set_portimportance(tsock->p->ref, value);
1498 break;
1499 case TIPC_SRC_DROPPABLE:
1500 if (sock->type != SOCK_STREAM)
1501 res = tipc_set_portunreliable(tsock->p->ref, value);
1502 else
1503 res = -ENOPROTOOPT;
1504 break;
1505 case TIPC_DEST_DROPPABLE:
1506 res = tipc_set_portunreturnable(tsock->p->ref, value);
1507 break;
1508 case TIPC_CONN_TIMEOUT:
1509 sock->sk->sk_rcvtimeo = (value * HZ / 1000);
1510 break;
1511 default:
1512 res = -EINVAL;
1513 }
1514
1515 up(&tsock->sem);
1516 return res;
1517}
1518
1519/**
1520 * getsockopt - get socket option
1521 * @sock: socket structure
1522 * @lvl: option level
1523 * @opt: option identifier
1524 * @ov: receptacle for option value
1525 * @ol: receptacle for length of option value
1526 *
1527 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1528 * (to ease compatibility).
1529 *
1530 * Returns 0 on success, errno otherwise
1531 */
1532
Allan Stephense9024f0f2006-06-25 23:43:57 -07001533static int getsockopt(struct socket *sock,
1534 int lvl, int opt, char __user *ov, int *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001535{
1536 struct tipc_sock *tsock = tipc_sk(sock->sk);
1537 int len;
1538 u32 value;
1539 int res;
1540
1541 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1542 return put_user(0, ol);
1543 if (lvl != SOL_TIPC)
1544 return -ENOPROTOOPT;
1545 if ((res = get_user(len, ol)))
1546 return res;
1547
1548 if (down_interruptible(&tsock->sem))
1549 return -ERESTARTSYS;
1550
1551 switch (opt) {
1552 case TIPC_IMPORTANCE:
1553 res = tipc_portimportance(tsock->p->ref, &value);
1554 break;
1555 case TIPC_SRC_DROPPABLE:
1556 res = tipc_portunreliable(tsock->p->ref, &value);
1557 break;
1558 case TIPC_DEST_DROPPABLE:
1559 res = tipc_portunreturnable(tsock->p->ref, &value);
1560 break;
1561 case TIPC_CONN_TIMEOUT:
1562 value = (sock->sk->sk_rcvtimeo * 1000) / HZ;
1563 break;
1564 default:
1565 res = -EINVAL;
1566 }
1567
1568 if (res) {
1569 /* "get" failed */
1570 }
1571 else if (len < sizeof(value)) {
1572 res = -EINVAL;
1573 }
1574 else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
1575 /* couldn't return value */
1576 }
1577 else {
1578 res = put_user(sizeof(value), ol);
1579 }
1580
1581 up(&tsock->sem);
1582 return res;
1583}
1584
1585/**
1586 * Placeholders for non-implemented functionality
1587 *
1588 * Returns error code (POSIX-compliant where defined)
1589 */
1590
1591static int ioctl(struct socket *s, u32 cmd, unsigned long arg)
1592{
1593 return -EINVAL;
1594}
1595
1596static int no_mmap(struct file *file, struct socket *sock,
1597 struct vm_area_struct *vma)
1598{
1599 return -EINVAL;
1600}
1601static ssize_t no_sendpage(struct socket *sock, struct page *page,
1602 int offset, size_t size, int flags)
1603{
1604 return -EINVAL;
1605}
1606
1607static int no_skpair(struct socket *s1, struct socket *s2)
1608{
1609 return -EOPNOTSUPP;
1610}
1611
1612/**
1613 * Protocol switches for the various types of TIPC sockets
1614 */
1615
1616static struct proto_ops msg_ops = {
1617 .owner = THIS_MODULE,
1618 .family = AF_TIPC,
1619 .release = release,
1620 .bind = bind,
1621 .connect = connect,
1622 .socketpair = no_skpair,
1623 .accept = accept,
1624 .getname = get_name,
1625 .poll = poll,
1626 .ioctl = ioctl,
1627 .listen = listen,
1628 .shutdown = shutdown,
1629 .setsockopt = setsockopt,
1630 .getsockopt = getsockopt,
1631 .sendmsg = send_msg,
1632 .recvmsg = recv_msg,
1633 .mmap = no_mmap,
1634 .sendpage = no_sendpage
1635};
1636
1637static struct proto_ops packet_ops = {
1638 .owner = THIS_MODULE,
1639 .family = AF_TIPC,
1640 .release = release,
1641 .bind = bind,
1642 .connect = connect,
1643 .socketpair = no_skpair,
1644 .accept = accept,
1645 .getname = get_name,
1646 .poll = poll,
1647 .ioctl = ioctl,
1648 .listen = listen,
1649 .shutdown = shutdown,
1650 .setsockopt = setsockopt,
1651 .getsockopt = getsockopt,
1652 .sendmsg = send_packet,
1653 .recvmsg = recv_msg,
1654 .mmap = no_mmap,
1655 .sendpage = no_sendpage
1656};
1657
1658static struct proto_ops stream_ops = {
1659 .owner = THIS_MODULE,
1660 .family = AF_TIPC,
1661 .release = release,
1662 .bind = bind,
1663 .connect = connect,
1664 .socketpair = no_skpair,
1665 .accept = accept,
1666 .getname = get_name,
1667 .poll = poll,
1668 .ioctl = ioctl,
1669 .listen = listen,
1670 .shutdown = shutdown,
1671 .setsockopt = setsockopt,
1672 .getsockopt = getsockopt,
1673 .sendmsg = send_stream,
1674 .recvmsg = recv_stream,
1675 .mmap = no_mmap,
1676 .sendpage = no_sendpage
1677};
1678
1679static struct net_proto_family tipc_family_ops = {
1680 .owner = THIS_MODULE,
1681 .family = AF_TIPC,
1682 .create = tipc_create
1683};
1684
1685static struct proto tipc_proto = {
1686 .name = "TIPC",
1687 .owner = THIS_MODULE,
1688 .obj_size = sizeof(struct tipc_sock)
1689};
1690
1691/**
Per Liden4323add2006-01-18 00:38:21 +01001692 * tipc_socket_init - initialize TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693 *
1694 * Returns 0 on success, errno otherwise
1695 */
Per Liden4323add2006-01-18 00:38:21 +01001696int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697{
1698 int res;
1699
1700 res = proto_register(&tipc_proto, 1);
1701 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001702 err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703 goto out;
1704 }
1705
1706 res = sock_register(&tipc_family_ops);
1707 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001708 err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001709 proto_unregister(&tipc_proto);
1710 goto out;
1711 }
1712
1713 sockets_enabled = 1;
1714 out:
1715 return res;
1716}
1717
1718/**
Per Liden4323add2006-01-18 00:38:21 +01001719 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 */
Per Liden4323add2006-01-18 00:38:21 +01001721void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722{
1723 if (!sockets_enabled)
1724 return;
1725
1726 sockets_enabled = 0;
1727 sock_unregister(tipc_family_ops.family);
1728 proto_unregister(&tipc_proto);
1729}
1730