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