aboutsummaryrefslogtreecommitdiffstats
path: root/net/tipc/socket.c
diff options
context:
space:
mode:
authorGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
committerGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
commitc71c03bda1e86c9d5198c5d83f712e695c4f2a1e (patch)
treeecb166cb3e2b7e2adb3b5e292245fefd23381ac8 /net/tipc/socket.c
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'net/tipc/socket.c')
-rw-r--r--net/tipc/socket.c338
1 files changed, 171 insertions, 167 deletions
diff --git a/net/tipc/socket.c b/net/tipc/socket.c
index 66e889ba48fd..338837396642 100644
--- a/net/tipc/socket.c
+++ b/net/tipc/socket.c
@@ -2,7 +2,7 @@
2 * net/tipc/socket.c: TIPC socket API 2 * net/tipc/socket.c: TIPC socket API
3 * 3 *
4 * Copyright (c) 2001-2007, Ericsson AB 4 * Copyright (c) 2001-2007, Ericsson AB
5 * Copyright (c) 2004-2008, Wind River Systems 5 * Copyright (c) 2004-2008, 2010-2011, Wind River Systems
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
@@ -34,25 +34,13 @@
34 * POSSIBILITY OF SUCH DAMAGE. 34 * POSSIBILITY OF SUCH DAMAGE.
35 */ 35 */
36 36
37#include <linux/module.h>
38#include <linux/types.h>
39#include <linux/net.h>
40#include <linux/socket.h>
41#include <linux/errno.h>
42#include <linux/mm.h>
43#include <linux/poll.h>
44#include <linux/fcntl.h>
45#include <linux/gfp.h>
46#include <asm/string.h>
47#include <asm/atomic.h>
48#include <net/sock.h> 37#include <net/sock.h>
49 38
50#include <linux/tipc.h> 39#include <linux/tipc.h>
51#include <linux/tipc_config.h> 40#include <linux/tipc_config.h>
52#include <net/tipc/tipc_msg.h>
53#include <net/tipc/tipc_port.h>
54 41
55#include "core.h" 42#include "core.h"
43#include "port.h"
56 44
57#define SS_LISTENING -1 /* socket is listening */ 45#define SS_LISTENING -1 /* socket is listening */
58#define SS_READY -2 /* socket is connectionless */ 46#define SS_READY -2 /* socket is connectionless */
@@ -64,11 +52,15 @@ struct tipc_sock {
64 struct sock sk; 52 struct sock sk;
65 struct tipc_port *p; 53 struct tipc_port *p;
66 struct tipc_portid peer_name; 54 struct tipc_portid peer_name;
55 long conn_timeout;
67}; 56};
68 57
69#define tipc_sk(sk) ((struct tipc_sock *)(sk)) 58#define tipc_sk(sk) ((struct tipc_sock *)(sk))
70#define tipc_sk_port(sk) ((struct tipc_port *)(tipc_sk(sk)->p)) 59#define tipc_sk_port(sk) ((struct tipc_port *)(tipc_sk(sk)->p))
71 60
61#define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
62 (sock->state == SS_DISCONNECTING))
63
72static int backlog_rcv(struct sock *sk, struct sk_buff *skb); 64static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
73static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf); 65static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
74static void wakeupdispatch(struct tipc_port *tport); 66static void wakeupdispatch(struct tipc_port *tport);
@@ -79,7 +71,7 @@ static const struct proto_ops msg_ops;
79 71
80static struct proto tipc_proto; 72static struct proto tipc_proto;
81 73
82static int sockets_enabled = 0; 74static int sockets_enabled;
83 75
84static atomic_t tipc_queue_size = ATOMIC_INIT(0); 76static atomic_t tipc_queue_size = ATOMIC_INIT(0);
85 77
@@ -240,9 +232,9 @@ static int tipc_create(struct net *net, struct socket *sock, int protocol,
240 sock->state = state; 232 sock->state = state;
241 233
242 sock_init_data(sock, sk); 234 sock_init_data(sock, sk);
243 sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
244 sk->sk_backlog_rcv = backlog_rcv; 235 sk->sk_backlog_rcv = backlog_rcv;
245 tipc_sk(sk)->p = tp_ptr; 236 tipc_sk(sk)->p = tp_ptr;
237 tipc_sk(sk)->conn_timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
246 238
247 spin_unlock_bh(tp_ptr->lock); 239 spin_unlock_bh(tp_ptr->lock);
248 240
@@ -252,7 +244,6 @@ static int tipc_create(struct net *net, struct socket *sock, int protocol,
252 tipc_set_portunreliable(tp_ptr->ref, 1); 244 tipc_set_portunreliable(tp_ptr->ref, 1);
253 } 245 }
254 246
255 atomic_inc(&tipc_user_count);
256 return 0; 247 return 0;
257} 248}
258 249
@@ -301,7 +292,7 @@ static int release(struct socket *sock)
301 if (buf == NULL) 292 if (buf == NULL)
302 break; 293 break;
303 atomic_dec(&tipc_queue_size); 294 atomic_dec(&tipc_queue_size);
304 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) 295 if (TIPC_SKB_CB(buf)->handle != 0)
305 buf_discard(buf); 296 buf_discard(buf);
306 else { 297 else {
307 if ((sock->state == SS_CONNECTING) || 298 if ((sock->state == SS_CONNECTING) ||
@@ -332,7 +323,6 @@ static int release(struct socket *sock)
332 sock_put(sk); 323 sock_put(sk);
333 sock->sk = NULL; 324 sock->sk = NULL;
334 325
335 atomic_dec(&tipc_user_count);
336 return res; 326 return res;
337} 327}
338 328
@@ -386,7 +376,7 @@ static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
386 * 376 *
387 * NOTE: This routine doesn't need to take the socket lock since it only 377 * NOTE: This routine doesn't need to take the socket lock since it only
388 * accesses socket information that is unchanging (or which changes in 378 * accesses socket information that is unchanging (or which changes in
389 * a completely predictable manner). 379 * a completely predictable manner).
390 */ 380 */
391 381
392static int get_name(struct socket *sock, struct sockaddr *uaddr, 382static int get_name(struct socket *sock, struct sockaddr *uaddr,
@@ -395,6 +385,7 @@ static int get_name(struct socket *sock, struct sockaddr *uaddr,
395 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; 385 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
396 struct tipc_sock *tsock = tipc_sk(sock->sk); 386 struct tipc_sock *tsock = tipc_sk(sock->sk);
397 387
388 memset(addr, 0, sizeof(*addr));
398 if (peer) { 389 if (peer) {
399 if ((sock->state != SS_CONNECTED) && 390 if ((sock->state != SS_CONNECTED) &&
400 ((peer != 2) || (sock->state != SS_DISCONNECTING))) 391 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
@@ -402,7 +393,8 @@ static int get_name(struct socket *sock, struct sockaddr *uaddr,
402 addr->addr.id.ref = tsock->peer_name.ref; 393 addr->addr.id.ref = tsock->peer_name.ref;
403 addr->addr.id.node = tsock->peer_name.node; 394 addr->addr.id.node = tsock->peer_name.node;
404 } else { 395 } else {
405 tipc_ownidentity(tsock->p->ref, &addr->addr.id); 396 addr->addr.id.ref = tsock->p->ref;
397 addr->addr.id.node = tipc_own_addr;
406 } 398 }
407 399
408 *uaddr_len = sizeof(*addr); 400 *uaddr_len = sizeof(*addr);
@@ -429,36 +421,55 @@ static int get_name(struct socket *sock, struct sockaddr *uaddr,
429 * to handle any preventable race conditions, so TIPC will do the same ... 421 * to handle any preventable race conditions, so TIPC will do the same ...
430 * 422 *
431 * TIPC sets the returned events as follows: 423 * TIPC sets the returned events as follows:
432 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty 424 *
433 * or if a connection-oriented socket is does not have an active connection 425 * socket state flags set
434 * (i.e. a read operation will not block). 426 * ------------ ---------
435 * b) POLLOUT is set except when a socket's connection has been terminated 427 * unconnected no read flags
436 * (i.e. a write operation will not block). 428 * no write flags
437 * c) POLLHUP is set when a socket's connection has been terminated. 429 *
438 * 430 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
439 * IMPORTANT: The fact that a read or write operation will not block does NOT 431 * no write flags
440 * imply that the operation will succeed! 432 *
433 * connected POLLIN/POLLRDNORM if data in rx queue
434 * POLLOUT if port is not congested
435 *
436 * disconnecting POLLIN/POLLRDNORM/POLLHUP
437 * no write flags
438 *
439 * listening POLLIN if SYN in rx queue
440 * no write flags
441 *
442 * ready POLLIN/POLLRDNORM if data in rx queue
443 * [connectionless] POLLOUT (since port cannot be congested)
444 *
445 * IMPORTANT: The fact that a read or write operation is indicated does NOT
446 * imply that the operation will succeed, merely that it should be performed
447 * and will not block.
441 */ 448 */
442 449
443static unsigned int poll(struct file *file, struct socket *sock, 450static unsigned int poll(struct file *file, struct socket *sock,
444 poll_table *wait) 451 poll_table *wait)
445{ 452{
446 struct sock *sk = sock->sk; 453 struct sock *sk = sock->sk;
447 u32 mask; 454 u32 mask = 0;
448 455
449 poll_wait(file, sk_sleep(sk), wait); 456 poll_wait(file, sk_sleep(sk), wait);
450 457
451 if (!skb_queue_empty(&sk->sk_receive_queue) || 458 switch ((int)sock->state) {
452 (sock->state == SS_UNCONNECTED) || 459 case SS_READY:
453 (sock->state == SS_DISCONNECTING)) 460 case SS_CONNECTED:
454 mask = (POLLRDNORM | POLLIN); 461 if (!tipc_sk_port(sk)->congested)
455 else 462 mask |= POLLOUT;
456 mask = 0; 463 /* fall thru' */
457 464 case SS_CONNECTING:
458 if (sock->state == SS_DISCONNECTING) 465 case SS_LISTENING:
459 mask |= POLLHUP; 466 if (!skb_queue_empty(&sk->sk_receive_queue))
460 else 467 mask |= (POLLIN | POLLRDNORM);
461 mask |= POLLOUT; 468 break;
469 case SS_DISCONNECTING:
470 mask = (POLLIN | POLLRDNORM | POLLHUP);
471 break;
472 }
462 473
463 return mask; 474 return mask;
464} 475}
@@ -485,6 +496,8 @@ static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
485 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV)) 496 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
486 return -EACCES; 497 return -EACCES;
487 498
499 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
500 return -EMSGSIZE;
488 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr))) 501 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
489 return -EFAULT; 502 return -EFAULT;
490 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN))) 503 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
@@ -522,6 +535,9 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
522 if (unlikely((m->msg_namelen < sizeof(*dest)) || 535 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
523 (dest->family != AF_TIPC))) 536 (dest->family != AF_TIPC)))
524 return -EINVAL; 537 return -EINVAL;
538 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
539 (m->msg_iovlen > (unsigned)INT_MAX))
540 return -EMSGSIZE;
525 541
526 if (iocb) 542 if (iocb)
527 lock_sock(sk); 543 lock_sock(sk);
@@ -553,37 +569,38 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
553 569
554 do { 570 do {
555 if (dest->addrtype == TIPC_ADDR_NAME) { 571 if (dest->addrtype == TIPC_ADDR_NAME) {
556 if ((res = dest_name_check(dest, m))) 572 res = dest_name_check(dest, m);
573 if (res)
557 break; 574 break;
558 res = tipc_send2name(tport->ref, 575 res = tipc_send2name(tport->ref,
559 &dest->addr.name.name, 576 &dest->addr.name.name,
560 dest->addr.name.domain, 577 dest->addr.name.domain,
561 m->msg_iovlen, 578 m->msg_iovlen,
562 m->msg_iov); 579 m->msg_iov,
563 } 580 total_len);
564 else if (dest->addrtype == TIPC_ADDR_ID) { 581 } else if (dest->addrtype == TIPC_ADDR_ID) {
565 res = tipc_send2port(tport->ref, 582 res = tipc_send2port(tport->ref,
566 &dest->addr.id, 583 &dest->addr.id,
567 m->msg_iovlen, 584 m->msg_iovlen,
568 m->msg_iov); 585 m->msg_iov,
569 } 586 total_len);
570 else if (dest->addrtype == TIPC_ADDR_MCAST) { 587 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
571 if (needs_conn) { 588 if (needs_conn) {
572 res = -EOPNOTSUPP; 589 res = -EOPNOTSUPP;
573 break; 590 break;
574 } 591 }
575 if ((res = dest_name_check(dest, m))) 592 res = dest_name_check(dest, m);
593 if (res)
576 break; 594 break;
577 res = tipc_multicast(tport->ref, 595 res = tipc_multicast(tport->ref,
578 &dest->addr.nameseq, 596 &dest->addr.nameseq,
579 0,
580 m->msg_iovlen, 597 m->msg_iovlen,
581 m->msg_iov); 598 m->msg_iov,
599 total_len);
582 } 600 }
583 if (likely(res != -ELINKCONG)) { 601 if (likely(res != -ELINKCONG)) {
584 if (needs_conn && (res >= 0)) { 602 if (needs_conn && (res >= 0))
585 sock->state = SS_CONNECTING; 603 sock->state = SS_CONNECTING;
586 }
587 break; 604 break;
588 } 605 }
589 if (m->msg_flags & MSG_DONTWAIT) { 606 if (m->msg_flags & MSG_DONTWAIT) {
@@ -629,6 +646,10 @@ static int send_packet(struct kiocb *iocb, struct socket *sock,
629 if (unlikely(dest)) 646 if (unlikely(dest))
630 return send_msg(iocb, sock, m, total_len); 647 return send_msg(iocb, sock, m, total_len);
631 648
649 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
650 (m->msg_iovlen > (unsigned)INT_MAX))
651 return -EMSGSIZE;
652
632 if (iocb) 653 if (iocb)
633 lock_sock(sk); 654 lock_sock(sk);
634 655
@@ -641,10 +662,10 @@ static int send_packet(struct kiocb *iocb, struct socket *sock,
641 break; 662 break;
642 } 663 }
643 664
644 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov); 665 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
645 if (likely(res != -ELINKCONG)) { 666 total_len);
667 if (likely(res != -ELINKCONG))
646 break; 668 break;
647 }
648 if (m->msg_flags & MSG_DONTWAIT) { 669 if (m->msg_flags & MSG_DONTWAIT) {
649 res = -EWOULDBLOCK; 670 res = -EWOULDBLOCK;
650 break; 671 break;
@@ -713,6 +734,12 @@ static int send_stream(struct kiocb *iocb, struct socket *sock,
713 goto exit; 734 goto exit;
714 } 735 }
715 736
737 if ((total_len > (unsigned)INT_MAX) ||
738 (m->msg_iovlen > (unsigned)INT_MAX)) {
739 res = -EMSGSIZE;
740 goto exit;
741 }
742
716 /* 743 /*
717 * Send each iovec entry using one or more messages 744 * Send each iovec entry using one or more messages
718 * 745 *
@@ -743,7 +770,8 @@ static int send_stream(struct kiocb *iocb, struct socket *sock,
743 bytes_to_send = curr_left; 770 bytes_to_send = curr_left;
744 my_iov.iov_base = curr_start; 771 my_iov.iov_base = curr_start;
745 my_iov.iov_len = bytes_to_send; 772 my_iov.iov_len = bytes_to_send;
746 if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) { 773 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
774 if (res < 0) {
747 if (bytes_sent) 775 if (bytes_sent)
748 res = bytes_sent; 776 res = bytes_sent;
749 goto exit; 777 goto exit;
@@ -803,8 +831,8 @@ static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
803 addr->addrtype = TIPC_ADDR_ID; 831 addr->addrtype = TIPC_ADDR_ID;
804 addr->addr.id.ref = msg_origport(msg); 832 addr->addr.id.ref = msg_origport(msg);
805 addr->addr.id.node = msg_orignode(msg); 833 addr->addr.id.node = msg_orignode(msg);
806 addr->addr.name.domain = 0; /* could leave uninitialized */ 834 addr->addr.name.domain = 0; /* could leave uninitialized */
807 addr->scope = 0; /* could leave uninitialized */ 835 addr->scope = 0; /* could leave uninitialized */
808 m->msg_namelen = sizeof(struct sockaddr_tipc); 836 m->msg_namelen = sizeof(struct sockaddr_tipc);
809 } 837 }
810} 838}
@@ -838,12 +866,15 @@ static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
838 if (unlikely(err)) { 866 if (unlikely(err)) {
839 anc_data[0] = err; 867 anc_data[0] = err;
840 anc_data[1] = msg_data_sz(msg); 868 anc_data[1] = msg_data_sz(msg);
841 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data))) 869 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
842 return res; 870 if (res)
843 if (anc_data[1] &&
844 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
845 msg_data(msg))))
846 return res; 871 return res;
872 if (anc_data[1]) {
873 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
874 msg_data(msg));
875 if (res)
876 return res;
877 }
847 } 878 }
848 879
849 /* Optionally capture message destination object */ 880 /* Optionally capture message destination object */
@@ -871,9 +902,11 @@ static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
871 default: 902 default:
872 has_name = 0; 903 has_name = 0;
873 } 904 }
874 if (has_name && 905 if (has_name) {
875 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data))) 906 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
876 return res; 907 if (res)
908 return res;
909 }
877 910
878 return 0; 911 return 0;
879} 912}
@@ -898,15 +931,13 @@ static int recv_msg(struct kiocb *iocb, struct socket *sock,
898 struct tipc_port *tport = tipc_sk_port(sk); 931 struct tipc_port *tport = tipc_sk_port(sk);
899 struct sk_buff *buf; 932 struct sk_buff *buf;
900 struct tipc_msg *msg; 933 struct tipc_msg *msg;
934 long timeout;
901 unsigned int sz; 935 unsigned int sz;
902 u32 err; 936 u32 err;
903 int res; 937 int res;
904 938
905 /* Catch invalid receive requests */ 939 /* Catch invalid receive requests */
906 940
907 if (m->msg_iovlen != 1)
908 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
909
910 if (unlikely(!buf_len)) 941 if (unlikely(!buf_len))
911 return -EINVAL; 942 return -EINVAL;
912 943
@@ -917,6 +948,7 @@ static int recv_msg(struct kiocb *iocb, struct socket *sock,
917 goto exit; 948 goto exit;
918 } 949 }
919 950
951 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
920restart: 952restart:
921 953
922 /* Look for a message in receive queue; wait if necessary */ 954 /* Look for a message in receive queue; wait if necessary */
@@ -926,17 +958,15 @@ restart:
926 res = -ENOTCONN; 958 res = -ENOTCONN;
927 goto exit; 959 goto exit;
928 } 960 }
929 if (flags & MSG_DONTWAIT) { 961 if (timeout <= 0L) {
930 res = -EWOULDBLOCK; 962 res = timeout ? timeout : -EWOULDBLOCK;
931 goto exit; 963 goto exit;
932 } 964 }
933 release_sock(sk); 965 release_sock(sk);
934 res = wait_event_interruptible(*sk_sleep(sk), 966 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
935 (!skb_queue_empty(&sk->sk_receive_queue) || 967 tipc_rx_ready(sock),
936 (sock->state == SS_DISCONNECTING))); 968 timeout);
937 lock_sock(sk); 969 lock_sock(sk);
938 if (res)
939 goto exit;
940 } 970 }
941 971
942 /* Look at first message in receive queue */ 972 /* Look at first message in receive queue */
@@ -978,11 +1008,10 @@ restart:
978 sz = buf_len; 1008 sz = buf_len;
979 m->msg_flags |= MSG_TRUNC; 1009 m->msg_flags |= MSG_TRUNC;
980 } 1010 }
981 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg), 1011 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
982 sz))) { 1012 m->msg_iov, sz);
983 res = -EFAULT; 1013 if (res)
984 goto exit; 1014 goto exit;
985 }
986 res = sz; 1015 res = sz;
987 } else { 1016 } else {
988 if ((sock->state == SS_READY) || 1017 if ((sock->state == SS_READY) ||
@@ -1025,20 +1054,15 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock,
1025 struct tipc_port *tport = tipc_sk_port(sk); 1054 struct tipc_port *tport = tipc_sk_port(sk);
1026 struct sk_buff *buf; 1055 struct sk_buff *buf;
1027 struct tipc_msg *msg; 1056 struct tipc_msg *msg;
1057 long timeout;
1028 unsigned int sz; 1058 unsigned int sz;
1029 int sz_to_copy; 1059 int sz_to_copy, target, needed;
1030 int sz_copied = 0; 1060 int sz_copied = 0;
1031 int needed;
1032 char __user *crs = m->msg_iov->iov_base;
1033 unsigned char *buf_crs;
1034 u32 err; 1061 u32 err;
1035 int res = 0; 1062 int res = 0;
1036 1063
1037 /* Catch invalid receive attempts */ 1064 /* Catch invalid receive attempts */
1038 1065
1039 if (m->msg_iovlen != 1)
1040 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
1041
1042 if (unlikely(!buf_len)) 1066 if (unlikely(!buf_len))
1043 return -EINVAL; 1067 return -EINVAL;
1044 1068
@@ -1050,6 +1074,8 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock,
1050 goto exit; 1074 goto exit;
1051 } 1075 }
1052 1076
1077 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1078 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1053restart: 1079restart:
1054 1080
1055 /* Look for a message in receive queue; wait if necessary */ 1081 /* Look for a message in receive queue; wait if necessary */
@@ -1059,17 +1085,15 @@ restart:
1059 res = -ENOTCONN; 1085 res = -ENOTCONN;
1060 goto exit; 1086 goto exit;
1061 } 1087 }
1062 if (flags & MSG_DONTWAIT) { 1088 if (timeout <= 0L) {
1063 res = -EWOULDBLOCK; 1089 res = timeout ? timeout : -EWOULDBLOCK;
1064 goto exit; 1090 goto exit;
1065 } 1091 }
1066 release_sock(sk); 1092 release_sock(sk);
1067 res = wait_event_interruptible(*sk_sleep(sk), 1093 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1068 (!skb_queue_empty(&sk->sk_receive_queue) || 1094 tipc_rx_ready(sock),
1069 (sock->state == SS_DISCONNECTING))); 1095 timeout);
1070 lock_sock(sk); 1096 lock_sock(sk);
1071 if (res)
1072 goto exit;
1073 } 1097 }
1074 1098
1075 /* Look at first message in receive queue */ 1099 /* Look at first message in receive queue */
@@ -1098,24 +1122,25 @@ restart:
1098 /* Capture message data (if valid) & compute return value (always) */ 1122 /* Capture message data (if valid) & compute return value (always) */
1099 1123
1100 if (!err) { 1124 if (!err) {
1101 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle); 1125 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
1102 sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
1103 1126
1127 sz -= offset;
1104 needed = (buf_len - sz_copied); 1128 needed = (buf_len - sz_copied);
1105 sz_to_copy = (sz <= needed) ? sz : needed; 1129 sz_to_copy = (sz <= needed) ? sz : needed;
1106 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) { 1130
1107 res = -EFAULT; 1131 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1132 m->msg_iov, sz_to_copy);
1133 if (res)
1108 goto exit; 1134 goto exit;
1109 } 1135
1110 sz_copied += sz_to_copy; 1136 sz_copied += sz_to_copy;
1111 1137
1112 if (sz_to_copy < sz) { 1138 if (sz_to_copy < sz) {
1113 if (!(flags & MSG_PEEK)) 1139 if (!(flags & MSG_PEEK))
1114 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy; 1140 TIPC_SKB_CB(buf)->handle =
1141 (void *)(unsigned long)(offset + sz_to_copy);
1115 goto exit; 1142 goto exit;
1116 } 1143 }
1117
1118 crs += sz_to_copy;
1119 } else { 1144 } else {
1120 if (sz_copied != 0) 1145 if (sz_copied != 0)
1121 goto exit; /* can't add error msg to valid data */ 1146 goto exit; /* can't add error msg to valid data */
@@ -1138,7 +1163,7 @@ restart:
1138 1163
1139 if ((sz_copied < buf_len) && /* didn't get all requested data */ 1164 if ((sz_copied < buf_len) && /* didn't get all requested data */
1140 (!skb_queue_empty(&sk->sk_receive_queue) || 1165 (!skb_queue_empty(&sk->sk_receive_queue) ||
1141 (flags & MSG_WAITALL)) && /* and more is ready or required */ 1166 (sz_copied < target)) && /* and more is ready or required */
1142 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */ 1167 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1143 (!err)) /* and haven't reached a FIN */ 1168 (!err)) /* and haven't reached a FIN */
1144 goto restart; 1169 goto restart;
@@ -1174,7 +1199,7 @@ static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
1174 if (msg_connected(msg)) 1199 if (msg_connected(msg))
1175 threshold *= 4; 1200 threshold *= 4;
1176 1201
1177 return (queue_size >= threshold); 1202 return queue_size >= threshold;
1178} 1203}
1179 1204
1180/** 1205/**
@@ -1205,42 +1230,25 @@ static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
1205 */ 1230 */
1206 1231
1207 if (sock->state == SS_READY) { 1232 if (sock->state == SS_READY) {
1208 if (msg_connected(msg)) { 1233 if (msg_connected(msg))
1209 msg_dbg(msg, "dispatch filter 1\n");
1210 return TIPC_ERR_NO_PORT; 1234 return TIPC_ERR_NO_PORT;
1211 }
1212 } else { 1235 } else {
1213 if (msg_mcast(msg)) { 1236 if (msg_mcast(msg))
1214 msg_dbg(msg, "dispatch filter 2\n");
1215 return TIPC_ERR_NO_PORT; 1237 return TIPC_ERR_NO_PORT;
1216 }
1217 if (sock->state == SS_CONNECTED) { 1238 if (sock->state == SS_CONNECTED) {
1218 if (!msg_connected(msg)) { 1239 if (!msg_connected(msg))
1219 msg_dbg(msg, "dispatch filter 3\n");
1220 return TIPC_ERR_NO_PORT; 1240 return TIPC_ERR_NO_PORT;
1221 } 1241 } else if (sock->state == SS_CONNECTING) {
1222 } 1242 if (!msg_connected(msg) && (msg_errcode(msg) == 0))
1223 else if (sock->state == SS_CONNECTING) {
1224 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1225 msg_dbg(msg, "dispatch filter 4\n");
1226 return TIPC_ERR_NO_PORT; 1243 return TIPC_ERR_NO_PORT;
1227 } 1244 } else if (sock->state == SS_LISTENING) {
1228 } 1245 if (msg_connected(msg) || msg_errcode(msg))
1229 else if (sock->state == SS_LISTENING) {
1230 if (msg_connected(msg) || msg_errcode(msg)) {
1231 msg_dbg(msg, "dispatch filter 5\n");
1232 return TIPC_ERR_NO_PORT; 1246 return TIPC_ERR_NO_PORT;
1233 } 1247 } else if (sock->state == SS_DISCONNECTING) {
1234 }
1235 else if (sock->state == SS_DISCONNECTING) {
1236 msg_dbg(msg, "dispatch filter 6\n");
1237 return TIPC_ERR_NO_PORT; 1248 return TIPC_ERR_NO_PORT;
1238 } 1249 } else /* (sock->state == SS_UNCONNECTED) */ {
1239 else /* (sock->state == SS_UNCONNECTED) */ { 1250 if (msg_connected(msg) || msg_errcode(msg))
1240 if (msg_connected(msg) || msg_errcode(msg)) {
1241 msg_dbg(msg, "dispatch filter 7\n");
1242 return TIPC_ERR_NO_PORT; 1251 return TIPC_ERR_NO_PORT;
1243 }
1244 } 1252 }
1245 } 1253 }
1246 1254
@@ -1259,8 +1267,7 @@ static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
1259 1267
1260 /* Enqueue message (finally!) */ 1268 /* Enqueue message (finally!) */
1261 1269
1262 msg_dbg(msg, "<DISP<: "); 1270 TIPC_SKB_CB(buf)->handle = 0;
1263 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1264 atomic_inc(&tipc_queue_size); 1271 atomic_inc(&tipc_queue_size);
1265 __skb_queue_tail(&sk->sk_receive_queue, buf); 1272 __skb_queue_tail(&sk->sk_receive_queue, buf);
1266 1273
@@ -1365,6 +1372,7 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1365 struct msghdr m = {NULL,}; 1372 struct msghdr m = {NULL,};
1366 struct sk_buff *buf; 1373 struct sk_buff *buf;
1367 struct tipc_msg *msg; 1374 struct tipc_msg *msg;
1375 long timeout;
1368 int res; 1376 int res;
1369 1377
1370 lock_sock(sk); 1378 lock_sock(sk);
@@ -1379,7 +1387,7 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1379 /* For now, TIPC does not support the non-blocking form of connect() */ 1387 /* For now, TIPC does not support the non-blocking form of connect() */
1380 1388
1381 if (flags & O_NONBLOCK) { 1389 if (flags & O_NONBLOCK) {
1382 res = -EWOULDBLOCK; 1390 res = -EOPNOTSUPP;
1383 goto exit; 1391 goto exit;
1384 } 1392 }
1385 1393
@@ -1419,17 +1427,17 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1419 m.msg_name = dest; 1427 m.msg_name = dest;
1420 m.msg_namelen = destlen; 1428 m.msg_namelen = destlen;
1421 res = send_msg(NULL, sock, &m, 0); 1429 res = send_msg(NULL, sock, &m, 0);
1422 if (res < 0) { 1430 if (res < 0)
1423 goto exit; 1431 goto exit;
1424 }
1425 1432
1426 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */ 1433 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1427 1434
1435 timeout = tipc_sk(sk)->conn_timeout;
1428 release_sock(sk); 1436 release_sock(sk);
1429 res = wait_event_interruptible_timeout(*sk_sleep(sk), 1437 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1430 (!skb_queue_empty(&sk->sk_receive_queue) || 1438 (!skb_queue_empty(&sk->sk_receive_queue) ||
1431 (sock->state != SS_CONNECTING)), 1439 (sock->state != SS_CONNECTING)),
1432 sk->sk_rcvtimeo); 1440 timeout ? timeout : MAX_SCHEDULE_TIMEOUT);
1433 lock_sock(sk); 1441 lock_sock(sk);
1434 1442
1435 if (res > 0) { 1443 if (res > 0) {
@@ -1442,11 +1450,10 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1442 advance_rx_queue(sk); 1450 advance_rx_queue(sk);
1443 } 1451 }
1444 } else { 1452 } else {
1445 if (sock->state == SS_CONNECTED) { 1453 if (sock->state == SS_CONNECTED)
1446 res = -EISCONN; 1454 res = -EISCONN;
1447 } else { 1455 else
1448 res = -ECONNREFUSED; 1456 res = -ECONNREFUSED;
1449 }
1450 } 1457 }
1451 } else { 1458 } else {
1452 if (res == 0) 1459 if (res == 0)
@@ -1565,7 +1572,6 @@ static int accept(struct socket *sock, struct socket *new_sock, int flags)
1565 * Respond to 'SYN+' by queuing it on new socket. 1572 * Respond to 'SYN+' by queuing it on new socket.
1566 */ 1573 */
1567 1574
1568 msg_dbg(msg,"<ACC<: ");
1569 if (!msg_data_sz(msg)) { 1575 if (!msg_data_sz(msg)) {
1570 struct msghdr m = {NULL,}; 1576 struct msghdr m = {NULL,};
1571 1577
@@ -1613,7 +1619,7 @@ restart:
1613 buf = __skb_dequeue(&sk->sk_receive_queue); 1619 buf = __skb_dequeue(&sk->sk_receive_queue);
1614 if (buf) { 1620 if (buf) {
1615 atomic_dec(&tipc_queue_size); 1621 atomic_dec(&tipc_queue_size);
1616 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) { 1622 if (TIPC_SKB_CB(buf)->handle != 0) {
1617 buf_discard(buf); 1623 buf_discard(buf);
1618 goto restart; 1624 goto restart;
1619 } 1625 }
@@ -1673,7 +1679,8 @@ static int setsockopt(struct socket *sock,
1673 return -ENOPROTOOPT; 1679 return -ENOPROTOOPT;
1674 if (ol < sizeof(value)) 1680 if (ol < sizeof(value))
1675 return -EINVAL; 1681 return -EINVAL;
1676 if ((res = get_user(value, (u32 __user *)ov))) 1682 res = get_user(value, (u32 __user *)ov);
1683 if (res)
1677 return res; 1684 return res;
1678 1685
1679 lock_sock(sk); 1686 lock_sock(sk);
@@ -1692,7 +1699,7 @@ static int setsockopt(struct socket *sock,
1692 res = tipc_set_portunreturnable(tport->ref, value); 1699 res = tipc_set_portunreturnable(tport->ref, value);
1693 break; 1700 break;
1694 case TIPC_CONN_TIMEOUT: 1701 case TIPC_CONN_TIMEOUT:
1695 sk->sk_rcvtimeo = msecs_to_jiffies(value); 1702 tipc_sk(sk)->conn_timeout = msecs_to_jiffies(value);
1696 /* no need to set "res", since already 0 at this point */ 1703 /* no need to set "res", since already 0 at this point */
1697 break; 1704 break;
1698 default: 1705 default:
@@ -1731,7 +1738,8 @@ static int getsockopt(struct socket *sock,
1731 return put_user(0, ol); 1738 return put_user(0, ol);
1732 if (lvl != SOL_TIPC) 1739 if (lvl != SOL_TIPC)
1733 return -ENOPROTOOPT; 1740 return -ENOPROTOOPT;
1734 if ((res = get_user(len, ol))) 1741 res = get_user(len, ol);
1742 if (res)
1735 return res; 1743 return res;
1736 1744
1737 lock_sock(sk); 1745 lock_sock(sk);
@@ -1747,13 +1755,13 @@ static int getsockopt(struct socket *sock,
1747 res = tipc_portunreturnable(tport->ref, &value); 1755 res = tipc_portunreturnable(tport->ref, &value);
1748 break; 1756 break;
1749 case TIPC_CONN_TIMEOUT: 1757 case TIPC_CONN_TIMEOUT:
1750 value = jiffies_to_msecs(sk->sk_rcvtimeo); 1758 value = jiffies_to_msecs(tipc_sk(sk)->conn_timeout);
1751 /* no need to set "res", since already 0 at this point */ 1759 /* no need to set "res", since already 0 at this point */
1752 break; 1760 break;
1753 case TIPC_NODE_RECVQ_DEPTH: 1761 case TIPC_NODE_RECVQ_DEPTH:
1754 value = (u32)atomic_read(&tipc_queue_size); 1762 value = (u32)atomic_read(&tipc_queue_size);
1755 break; 1763 break;
1756 case TIPC_SOCK_RECVQ_DEPTH: 1764 case TIPC_SOCK_RECVQ_DEPTH:
1757 value = skb_queue_len(&sk->sk_receive_queue); 1765 value = skb_queue_len(&sk->sk_receive_queue);
1758 break; 1766 break;
1759 default: 1767 default:
@@ -1762,20 +1770,16 @@ static int getsockopt(struct socket *sock,
1762 1770
1763 release_sock(sk); 1771 release_sock(sk);
1764 1772
1765 if (res) { 1773 if (res)
1766 /* "get" failed */ 1774 return res; /* "get" failed */
1767 }
1768 else if (len < sizeof(value)) {
1769 res = -EINVAL;
1770 }
1771 else if (copy_to_user(ov, &value, sizeof(value))) {
1772 res = -EFAULT;
1773 }
1774 else {
1775 res = put_user(sizeof(value), ol);
1776 }
1777 1775
1778 return res; 1776 if (len < sizeof(value))
1777 return -EINVAL;
1778
1779 if (copy_to_user(ov, &value, sizeof(value)))
1780 return -EFAULT;
1781
1782 return put_user(sizeof(value), ol);
1779} 1783}
1780 1784
1781/** 1785/**
@@ -1783,7 +1787,7 @@ static int getsockopt(struct socket *sock,
1783 */ 1787 */
1784 1788
1785static const struct proto_ops msg_ops = { 1789static const struct proto_ops msg_ops = {
1786 .owner = THIS_MODULE, 1790 .owner = THIS_MODULE,
1787 .family = AF_TIPC, 1791 .family = AF_TIPC,
1788 .release = release, 1792 .release = release,
1789 .bind = bind, 1793 .bind = bind,
@@ -1804,7 +1808,7 @@ static const struct proto_ops msg_ops = {
1804}; 1808};
1805 1809
1806static const struct proto_ops packet_ops = { 1810static const struct proto_ops packet_ops = {
1807 .owner = THIS_MODULE, 1811 .owner = THIS_MODULE,
1808 .family = AF_TIPC, 1812 .family = AF_TIPC,
1809 .release = release, 1813 .release = release,
1810 .bind = bind, 1814 .bind = bind,
@@ -1825,7 +1829,7 @@ static const struct proto_ops packet_ops = {
1825}; 1829};
1826 1830
1827static const struct proto_ops stream_ops = { 1831static const struct proto_ops stream_ops = {
1828 .owner = THIS_MODULE, 1832 .owner = THIS_MODULE,
1829 .family = AF_TIPC, 1833 .family = AF_TIPC,
1830 .release = release, 1834 .release = release,
1831 .bind = bind, 1835 .bind = bind,
@@ -1846,7 +1850,7 @@ static const struct proto_ops stream_ops = {
1846}; 1850};
1847 1851
1848static const struct net_proto_family tipc_family_ops = { 1852static const struct net_proto_family tipc_family_ops = {
1849 .owner = THIS_MODULE, 1853 .owner = THIS_MODULE,
1850 .family = AF_TIPC, 1854 .family = AF_TIPC,
1851 .create = tipc_create 1855 .create = tipc_create
1852}; 1856};