aboutsummaryrefslogtreecommitdiffstats
path: root/net/tipc/socket.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/tipc/socket.c')
-rw-r--r--net/tipc/socket.c498
1 files changed, 251 insertions, 247 deletions
diff --git a/net/tipc/socket.c b/net/tipc/socket.c
index 41f013888f07..333c5dae0072 100644
--- a/net/tipc/socket.c
+++ b/net/tipc/socket.c
@@ -44,44 +44,43 @@
44#include "bcast.h" 44#include "bcast.h"
45#include "netlink.h" 45#include "netlink.h"
46 46
47#define SS_LISTENING -1 /* socket is listening */
48#define SS_READY -2 /* socket is connectionless */
49
50#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */ 47#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
51#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */ 48#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
52#define TIPC_FWD_MSG 1 49#define TIPC_FWD_MSG 1
53#define TIPC_CONN_OK 0
54#define TIPC_CONN_PROBING 1
55#define TIPC_MAX_PORT 0xffffffff 50#define TIPC_MAX_PORT 0xffffffff
56#define TIPC_MIN_PORT 1 51#define TIPC_MIN_PORT 1
57 52
53enum {
54 TIPC_LISTEN = TCP_LISTEN,
55 TIPC_ESTABLISHED = TCP_ESTABLISHED,
56 TIPC_OPEN = TCP_CLOSE,
57 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
58 TIPC_CONNECTING = TCP_SYN_SENT,
59};
60
58/** 61/**
59 * struct tipc_sock - TIPC socket structure 62 * struct tipc_sock - TIPC socket structure
60 * @sk: socket - interacts with 'port' and with user via the socket API 63 * @sk: socket - interacts with 'port' and with user via the socket API
61 * @connected: non-zero if port is currently connected to a peer port
62 * @conn_type: TIPC type used when connection was established 64 * @conn_type: TIPC type used when connection was established
63 * @conn_instance: TIPC instance used when connection was established 65 * @conn_instance: TIPC instance used when connection was established
64 * @published: non-zero if port has one or more associated names 66 * @published: non-zero if port has one or more associated names
65 * @max_pkt: maximum packet size "hint" used when building messages sent by port 67 * @max_pkt: maximum packet size "hint" used when building messages sent by port
66 * @portid: unique port identity in TIPC socket hash table 68 * @portid: unique port identity in TIPC socket hash table
67 * @phdr: preformatted message header used when sending messages 69 * @phdr: preformatted message header used when sending messages
68 * @port_list: adjacent ports in TIPC's global list of ports
69 * @publications: list of publications for port 70 * @publications: list of publications for port
70 * @pub_count: total # of publications port has made during its lifetime 71 * @pub_count: total # of publications port has made during its lifetime
71 * @probing_state: 72 * @probing_state:
72 * @probing_intv:
73 * @conn_timeout: the time we can wait for an unresponded setup request 73 * @conn_timeout: the time we can wait for an unresponded setup request
74 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue 74 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
75 * @link_cong: non-zero if owner must sleep because of link congestion 75 * @link_cong: non-zero if owner must sleep because of link congestion
76 * @sent_unacked: # messages sent by socket, and not yet acked by peer 76 * @sent_unacked: # messages sent by socket, and not yet acked by peer
77 * @rcv_unacked: # messages read by user, but not yet acked back to peer 77 * @rcv_unacked: # messages read by user, but not yet acked back to peer
78 * @remote: 'connected' peer for dgram/rdm 78 * @peer: 'connected' peer for dgram/rdm
79 * @node: hash table node 79 * @node: hash table node
80 * @rcu: rcu struct for tipc_sock 80 * @rcu: rcu struct for tipc_sock
81 */ 81 */
82struct tipc_sock { 82struct tipc_sock {
83 struct sock sk; 83 struct sock sk;
84 int connected;
85 u32 conn_type; 84 u32 conn_type;
86 u32 conn_instance; 85 u32 conn_instance;
87 int published; 86 int published;
@@ -91,17 +90,16 @@ struct tipc_sock {
91 struct list_head sock_list; 90 struct list_head sock_list;
92 struct list_head publications; 91 struct list_head publications;
93 u32 pub_count; 92 u32 pub_count;
94 u32 probing_state;
95 unsigned long probing_intv;
96 uint conn_timeout; 93 uint conn_timeout;
97 atomic_t dupl_rcvcnt; 94 atomic_t dupl_rcvcnt;
95 bool probe_unacked;
98 bool link_cong; 96 bool link_cong;
99 u16 snt_unacked; 97 u16 snt_unacked;
100 u16 snd_win; 98 u16 snd_win;
101 u16 peer_caps; 99 u16 peer_caps;
102 u16 rcv_unacked; 100 u16 rcv_unacked;
103 u16 rcv_win; 101 u16 rcv_win;
104 struct sockaddr_tipc remote; 102 struct sockaddr_tipc peer;
105 struct rhash_head node; 103 struct rhash_head node;
106 struct rcu_head rcu; 104 struct rcu_head rcu;
107}; 105};
@@ -248,6 +246,21 @@ static void tsk_rej_rx_queue(struct sock *sk)
248 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT); 246 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
249} 247}
250 248
249static bool tipc_sk_connected(struct sock *sk)
250{
251 return sk->sk_state == TIPC_ESTABLISHED;
252}
253
254/* tipc_sk_type_connectionless - check if the socket is datagram socket
255 * @sk: socket
256 *
257 * Returns true if connection less, false otherwise
258 */
259static bool tipc_sk_type_connectionless(struct sock *sk)
260{
261 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
262}
263
251/* tsk_peer_msg - verify if message was sent by connected port's peer 264/* tsk_peer_msg - verify if message was sent by connected port's peer
252 * 265 *
253 * Handles cases where the node's network address has changed from 266 * Handles cases where the node's network address has changed from
@@ -255,12 +268,13 @@ static void tsk_rej_rx_queue(struct sock *sk)
255 */ 268 */
256static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg) 269static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
257{ 270{
258 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id); 271 struct sock *sk = &tsk->sk;
272 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
259 u32 peer_port = tsk_peer_port(tsk); 273 u32 peer_port = tsk_peer_port(tsk);
260 u32 orig_node; 274 u32 orig_node;
261 u32 peer_node; 275 u32 peer_node;
262 276
263 if (unlikely(!tsk->connected)) 277 if (unlikely(!tipc_sk_connected(sk)))
264 return false; 278 return false;
265 279
266 if (unlikely(msg_origport(msg) != peer_port)) 280 if (unlikely(msg_origport(msg) != peer_port))
@@ -281,6 +295,45 @@ static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
281 return false; 295 return false;
282} 296}
283 297
298/* tipc_set_sk_state - set the sk_state of the socket
299 * @sk: socket
300 *
301 * Caller must hold socket lock
302 *
303 * Returns 0 on success, errno otherwise
304 */
305static int tipc_set_sk_state(struct sock *sk, int state)
306{
307 int oldsk_state = sk->sk_state;
308 int res = -EINVAL;
309
310 switch (state) {
311 case TIPC_OPEN:
312 res = 0;
313 break;
314 case TIPC_LISTEN:
315 case TIPC_CONNECTING:
316 if (oldsk_state == TIPC_OPEN)
317 res = 0;
318 break;
319 case TIPC_ESTABLISHED:
320 if (oldsk_state == TIPC_CONNECTING ||
321 oldsk_state == TIPC_OPEN)
322 res = 0;
323 break;
324 case TIPC_DISCONNECTING:
325 if (oldsk_state == TIPC_CONNECTING ||
326 oldsk_state == TIPC_ESTABLISHED)
327 res = 0;
328 break;
329 }
330
331 if (!res)
332 sk->sk_state = state;
333
334 return res;
335}
336
284/** 337/**
285 * tipc_sk_create - create a TIPC socket 338 * tipc_sk_create - create a TIPC socket
286 * @net: network namespace (must be default network) 339 * @net: network namespace (must be default network)
@@ -298,7 +351,6 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
298{ 351{
299 struct tipc_net *tn; 352 struct tipc_net *tn;
300 const struct proto_ops *ops; 353 const struct proto_ops *ops;
301 socket_state state;
302 struct sock *sk; 354 struct sock *sk;
303 struct tipc_sock *tsk; 355 struct tipc_sock *tsk;
304 struct tipc_msg *msg; 356 struct tipc_msg *msg;
@@ -310,16 +362,13 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
310 switch (sock->type) { 362 switch (sock->type) {
311 case SOCK_STREAM: 363 case SOCK_STREAM:
312 ops = &stream_ops; 364 ops = &stream_ops;
313 state = SS_UNCONNECTED;
314 break; 365 break;
315 case SOCK_SEQPACKET: 366 case SOCK_SEQPACKET:
316 ops = &packet_ops; 367 ops = &packet_ops;
317 state = SS_UNCONNECTED;
318 break; 368 break;
319 case SOCK_DGRAM: 369 case SOCK_DGRAM:
320 case SOCK_RDM: 370 case SOCK_RDM:
321 ops = &msg_ops; 371 ops = &msg_ops;
322 state = SS_READY;
323 break; 372 break;
324 default: 373 default:
325 return -EPROTOTYPE; 374 return -EPROTOTYPE;
@@ -340,14 +389,15 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
340 389
341 /* Finish initializing socket data structures */ 390 /* Finish initializing socket data structures */
342 sock->ops = ops; 391 sock->ops = ops;
343 sock->state = state;
344 sock_init_data(sock, sk); 392 sock_init_data(sock, sk);
393 tipc_set_sk_state(sk, TIPC_OPEN);
345 if (tipc_sk_insert(tsk)) { 394 if (tipc_sk_insert(tsk)) {
346 pr_warn("Socket create failed; port number exhausted\n"); 395 pr_warn("Socket create failed; port number exhausted\n");
347 return -EINVAL; 396 return -EINVAL;
348 } 397 }
349 msg_set_origport(msg, tsk->portid); 398 msg_set_origport(msg, tsk->portid);
350 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk); 399 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
400 sk->sk_shutdown = 0;
351 sk->sk_backlog_rcv = tipc_backlog_rcv; 401 sk->sk_backlog_rcv = tipc_backlog_rcv;
352 sk->sk_rcvbuf = sysctl_tipc_rmem[1]; 402 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
353 sk->sk_data_ready = tipc_data_ready; 403 sk->sk_data_ready = tipc_data_ready;
@@ -360,11 +410,12 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
360 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN); 410 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
361 tsk->rcv_win = tsk->snd_win; 411 tsk->rcv_win = tsk->snd_win;
362 412
363 if (sock->state == SS_READY) { 413 if (tipc_sk_type_connectionless(sk)) {
364 tsk_set_unreturnable(tsk, true); 414 tsk_set_unreturnable(tsk, true);
365 if (sock->type == SOCK_DGRAM) 415 if (sock->type == SOCK_DGRAM)
366 tsk_set_unreliable(tsk, true); 416 tsk_set_unreliable(tsk, true);
367 } 417 }
418
368 return 0; 419 return 0;
369} 420}
370 421
@@ -375,6 +426,44 @@ static void tipc_sk_callback(struct rcu_head *head)
375 sock_put(&tsk->sk); 426 sock_put(&tsk->sk);
376} 427}
377 428
429/* Caller should hold socket lock for the socket. */
430static void __tipc_shutdown(struct socket *sock, int error)
431{
432 struct sock *sk = sock->sk;
433 struct tipc_sock *tsk = tipc_sk(sk);
434 struct net *net = sock_net(sk);
435 u32 dnode = tsk_peer_node(tsk);
436 struct sk_buff *skb;
437
438 /* Reject all unreceived messages, except on an active connection
439 * (which disconnects locally & sends a 'FIN+' to peer).
440 */
441 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
442 if (TIPC_SKB_CB(skb)->bytes_read) {
443 kfree_skb(skb);
444 } else {
445 if (!tipc_sk_type_connectionless(sk) &&
446 sk->sk_state != TIPC_DISCONNECTING) {
447 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
448 tipc_node_remove_conn(net, dnode, tsk->portid);
449 }
450 tipc_sk_respond(sk, skb, error);
451 }
452 }
453 if (sk->sk_state != TIPC_DISCONNECTING) {
454 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
455 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
456 tsk_own_node(tsk), tsk_peer_port(tsk),
457 tsk->portid, error);
458 if (skb)
459 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
460 if (!tipc_sk_type_connectionless(sk)) {
461 tipc_node_remove_conn(net, dnode, tsk->portid);
462 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
463 }
464 }
465}
466
378/** 467/**
379 * tipc_release - destroy a TIPC socket 468 * tipc_release - destroy a TIPC socket
380 * @sock: socket to destroy 469 * @sock: socket to destroy
@@ -394,10 +483,7 @@ static void tipc_sk_callback(struct rcu_head *head)
394static int tipc_release(struct socket *sock) 483static int tipc_release(struct socket *sock)
395{ 484{
396 struct sock *sk = sock->sk; 485 struct sock *sk = sock->sk;
397 struct net *net;
398 struct tipc_sock *tsk; 486 struct tipc_sock *tsk;
399 struct sk_buff *skb;
400 u32 dnode;
401 487
402 /* 488 /*
403 * Exit if socket isn't fully initialized (occurs when a failed accept() 489 * Exit if socket isn't fully initialized (occurs when a failed accept()
@@ -406,47 +492,16 @@ static int tipc_release(struct socket *sock)
406 if (sk == NULL) 492 if (sk == NULL)
407 return 0; 493 return 0;
408 494
409 net = sock_net(sk);
410 tsk = tipc_sk(sk); 495 tsk = tipc_sk(sk);
411 lock_sock(sk); 496 lock_sock(sk);
412 497
413 /* 498 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
414 * Reject all unreceived messages, except on an active connection 499 sk->sk_shutdown = SHUTDOWN_MASK;
415 * (which disconnects locally & sends a 'FIN+' to peer)
416 */
417 dnode = tsk_peer_node(tsk);
418 while (sock->state != SS_DISCONNECTING) {
419 skb = __skb_dequeue(&sk->sk_receive_queue);
420 if (skb == NULL)
421 break;
422 if (TIPC_SKB_CB(skb)->handle != NULL)
423 kfree_skb(skb);
424 else {
425 if ((sock->state == SS_CONNECTING) ||
426 (sock->state == SS_CONNECTED)) {
427 sock->state = SS_DISCONNECTING;
428 tsk->connected = 0;
429 tipc_node_remove_conn(net, dnode, tsk->portid);
430 }
431 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
432 }
433 }
434
435 tipc_sk_withdraw(tsk, 0, NULL); 500 tipc_sk_withdraw(tsk, 0, NULL);
436 sk_stop_timer(sk, &sk->sk_timer); 501 sk_stop_timer(sk, &sk->sk_timer);
437 tipc_sk_remove(tsk); 502 tipc_sk_remove(tsk);
438 if (tsk->connected) {
439 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
440 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
441 tsk_own_node(tsk), tsk_peer_port(tsk),
442 tsk->portid, TIPC_ERR_NO_PORT);
443 if (skb)
444 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
445 tipc_node_remove_conn(net, dnode, tsk->portid);
446 }
447 503
448 /* Reject any messages that accumulated in backlog queue */ 504 /* Reject any messages that accumulated in backlog queue */
449 sock->state = SS_DISCONNECTING;
450 release_sock(sk); 505 release_sock(sk);
451 506
452 call_rcu(&tsk->rcu, tipc_sk_callback); 507 call_rcu(&tsk->rcu, tipc_sk_callback);
@@ -532,13 +587,14 @@ static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
532 int *uaddr_len, int peer) 587 int *uaddr_len, int peer)
533{ 588{
534 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; 589 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
535 struct tipc_sock *tsk = tipc_sk(sock->sk); 590 struct sock *sk = sock->sk;
591 struct tipc_sock *tsk = tipc_sk(sk);
536 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id); 592 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
537 593
538 memset(addr, 0, sizeof(*addr)); 594 memset(addr, 0, sizeof(*addr));
539 if (peer) { 595 if (peer) {
540 if ((sock->state != SS_CONNECTED) && 596 if ((!tipc_sk_connected(sk)) &&
541 ((peer != 2) || (sock->state != SS_DISCONNECTING))) 597 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
542 return -ENOTCONN; 598 return -ENOTCONN;
543 addr->addr.id.ref = tsk_peer_port(tsk); 599 addr->addr.id.ref = tsk_peer_port(tsk);
544 addr->addr.id.node = tsk_peer_node(tsk); 600 addr->addr.id.node = tsk_peer_node(tsk);
@@ -570,28 +626,6 @@ static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
570 * exits. TCP and other protocols seem to rely on higher level poll routines 626 * exits. TCP and other protocols seem to rely on higher level poll routines
571 * to handle any preventable race conditions, so TIPC will do the same ... 627 * to handle any preventable race conditions, so TIPC will do the same ...
572 * 628 *
573 * TIPC sets the returned events as follows:
574 *
575 * socket state flags set
576 * ------------ ---------
577 * unconnected no read flags
578 * POLLOUT if port is not congested
579 *
580 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
581 * no write flags
582 *
583 * connected POLLIN/POLLRDNORM if data in rx queue
584 * POLLOUT if port is not congested
585 *
586 * disconnecting POLLIN/POLLRDNORM/POLLHUP
587 * no write flags
588 *
589 * listening POLLIN if SYN in rx queue
590 * no write flags
591 *
592 * ready POLLIN/POLLRDNORM if data in rx queue
593 * [connectionless] POLLOUT (since port cannot be congested)
594 *
595 * IMPORTANT: The fact that a read or write operation is indicated does NOT 629 * IMPORTANT: The fact that a read or write operation is indicated does NOT
596 * imply that the operation will succeed, merely that it should be performed 630 * imply that the operation will succeed, merely that it should be performed
597 * and will not block. 631 * and will not block.
@@ -605,22 +639,29 @@ static unsigned int tipc_poll(struct file *file, struct socket *sock,
605 639
606 sock_poll_wait(file, sk_sleep(sk), wait); 640 sock_poll_wait(file, sk_sleep(sk), wait);
607 641
608 switch ((int)sock->state) { 642 if (sk->sk_shutdown & RCV_SHUTDOWN)
609 case SS_UNCONNECTED: 643 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
610 if (!tsk->link_cong) 644 if (sk->sk_shutdown == SHUTDOWN_MASK)
611 mask |= POLLOUT; 645 mask |= POLLHUP;
612 break; 646
613 case SS_READY: 647 switch (sk->sk_state) {
614 case SS_CONNECTED: 648 case TIPC_ESTABLISHED:
615 if (!tsk->link_cong && !tsk_conn_cong(tsk)) 649 if (!tsk->link_cong && !tsk_conn_cong(tsk))
616 mask |= POLLOUT; 650 mask |= POLLOUT;
617 /* fall thru' */ 651 /* fall thru' */
618 case SS_CONNECTING: 652 case TIPC_LISTEN:
619 case SS_LISTENING: 653 case TIPC_CONNECTING:
620 if (!skb_queue_empty(&sk->sk_receive_queue)) 654 if (!skb_queue_empty(&sk->sk_receive_queue))
621 mask |= (POLLIN | POLLRDNORM); 655 mask |= (POLLIN | POLLRDNORM);
622 break; 656 break;
623 case SS_DISCONNECTING: 657 case TIPC_OPEN:
658 if (!tsk->link_cong)
659 mask |= POLLOUT;
660 if (tipc_sk_type_connectionless(sk) &&
661 (!skb_queue_empty(&sk->sk_receive_queue)))
662 mask |= (POLLIN | POLLRDNORM);
663 break;
664 case TIPC_DISCONNECTING:
624 mask = (POLLIN | POLLRDNORM | POLLHUP); 665 mask = (POLLIN | POLLRDNORM | POLLHUP);
625 break; 666 break;
626 } 667 }
@@ -651,6 +692,9 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
651 uint mtu; 692 uint mtu;
652 int rc; 693 int rc;
653 694
695 if (!timeo && tsk->link_cong)
696 return -ELINKCONG;
697
654 msg_set_type(mhdr, TIPC_MCAST_MSG); 698 msg_set_type(mhdr, TIPC_MCAST_MSG);
655 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE); 699 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
656 msg_set_destport(mhdr, 0); 700 msg_set_destport(mhdr, 0);
@@ -763,7 +807,7 @@ static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
763 if (!tsk_peer_msg(tsk, hdr)) 807 if (!tsk_peer_msg(tsk, hdr))
764 goto exit; 808 goto exit;
765 809
766 tsk->probing_state = TIPC_CONN_OK; 810 tsk->probe_unacked = false;
767 811
768 if (mtyp == CONN_PROBE) { 812 if (mtyp == CONN_PROBE) {
769 msg_set_type(hdr, CONN_PROBE_REPLY); 813 msg_set_type(hdr, CONN_PROBE_REPLY);
@@ -786,25 +830,25 @@ exit:
786 830
787static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p) 831static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
788{ 832{
833 DEFINE_WAIT_FUNC(wait, woken_wake_function);
789 struct sock *sk = sock->sk; 834 struct sock *sk = sock->sk;
790 struct tipc_sock *tsk = tipc_sk(sk); 835 struct tipc_sock *tsk = tipc_sk(sk);
791 DEFINE_WAIT(wait);
792 int done; 836 int done;
793 837
794 do { 838 do {
795 int err = sock_error(sk); 839 int err = sock_error(sk);
796 if (err) 840 if (err)
797 return err; 841 return err;
798 if (sock->state == SS_DISCONNECTING) 842 if (sk->sk_shutdown & SEND_SHUTDOWN)
799 return -EPIPE; 843 return -EPIPE;
800 if (!*timeo_p) 844 if (!*timeo_p)
801 return -EAGAIN; 845 return -EAGAIN;
802 if (signal_pending(current)) 846 if (signal_pending(current))
803 return sock_intr_errno(*timeo_p); 847 return sock_intr_errno(*timeo_p);
804 848
805 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 849 add_wait_queue(sk_sleep(sk), &wait);
806 done = sk_wait_event(sk, timeo_p, !tsk->link_cong); 850 done = sk_wait_event(sk, timeo_p, !tsk->link_cong, &wait);
807 finish_wait(sk_sleep(sk), &wait); 851 remove_wait_queue(sk_sleep(sk), &wait);
808 } while (!done); 852 } while (!done);
809 return 0; 853 return 0;
810} 854}
@@ -844,6 +888,7 @@ static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
844 struct tipc_msg *mhdr = &tsk->phdr; 888 struct tipc_msg *mhdr = &tsk->phdr;
845 u32 dnode, dport; 889 u32 dnode, dport;
846 struct sk_buff_head pktchain; 890 struct sk_buff_head pktchain;
891 bool is_connectionless = tipc_sk_type_connectionless(sk);
847 struct sk_buff *skb; 892 struct sk_buff *skb;
848 struct tipc_name_seq *seq; 893 struct tipc_name_seq *seq;
849 struct iov_iter save; 894 struct iov_iter save;
@@ -854,18 +899,18 @@ static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
854 if (dsz > TIPC_MAX_USER_MSG_SIZE) 899 if (dsz > TIPC_MAX_USER_MSG_SIZE)
855 return -EMSGSIZE; 900 return -EMSGSIZE;
856 if (unlikely(!dest)) { 901 if (unlikely(!dest)) {
857 if (tsk->connected && sock->state == SS_READY) 902 if (is_connectionless && tsk->peer.family == AF_TIPC)
858 dest = &tsk->remote; 903 dest = &tsk->peer;
859 else 904 else
860 return -EDESTADDRREQ; 905 return -EDESTADDRREQ;
861 } else if (unlikely(m->msg_namelen < sizeof(*dest)) || 906 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
862 dest->family != AF_TIPC) { 907 dest->family != AF_TIPC) {
863 return -EINVAL; 908 return -EINVAL;
864 } 909 }
865 if (unlikely(sock->state != SS_READY)) { 910 if (!is_connectionless) {
866 if (sock->state == SS_LISTENING) 911 if (sk->sk_state == TIPC_LISTEN)
867 return -EPIPE; 912 return -EPIPE;
868 if (sock->state != SS_UNCONNECTED) 913 if (sk->sk_state != TIPC_OPEN)
869 return -EISCONN; 914 return -EISCONN;
870 if (tsk->published) 915 if (tsk->published)
871 return -EOPNOTSUPP; 916 return -EOPNOTSUPP;
@@ -917,8 +962,8 @@ new_mtu:
917 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong; 962 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
918 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid); 963 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
919 if (likely(!rc)) { 964 if (likely(!rc)) {
920 if (sock->state != SS_READY) 965 if (!is_connectionless)
921 sock->state = SS_CONNECTING; 966 tipc_set_sk_state(sk, TIPC_CONNECTING);
922 return dsz; 967 return dsz;
923 } 968 }
924 if (rc == -ELINKCONG) { 969 if (rc == -ELINKCONG) {
@@ -940,30 +985,30 @@ new_mtu:
940 985
941static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p) 986static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
942{ 987{
988 DEFINE_WAIT_FUNC(wait, woken_wake_function);
943 struct sock *sk = sock->sk; 989 struct sock *sk = sock->sk;
944 struct tipc_sock *tsk = tipc_sk(sk); 990 struct tipc_sock *tsk = tipc_sk(sk);
945 DEFINE_WAIT(wait);
946 int done; 991 int done;
947 992
948 do { 993 do {
949 int err = sock_error(sk); 994 int err = sock_error(sk);
950 if (err) 995 if (err)
951 return err; 996 return err;
952 if (sock->state == SS_DISCONNECTING) 997 if (sk->sk_state == TIPC_DISCONNECTING)
953 return -EPIPE; 998 return -EPIPE;
954 else if (sock->state != SS_CONNECTED) 999 else if (!tipc_sk_connected(sk))
955 return -ENOTCONN; 1000 return -ENOTCONN;
956 if (!*timeo_p) 1001 if (!*timeo_p)
957 return -EAGAIN; 1002 return -EAGAIN;
958 if (signal_pending(current)) 1003 if (signal_pending(current))
959 return sock_intr_errno(*timeo_p); 1004 return sock_intr_errno(*timeo_p);
960 1005
961 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 1006 add_wait_queue(sk_sleep(sk), &wait);
962 done = sk_wait_event(sk, timeo_p, 1007 done = sk_wait_event(sk, timeo_p,
963 (!tsk->link_cong && 1008 (!tsk->link_cong &&
964 !tsk_conn_cong(tsk)) || 1009 !tsk_conn_cong(tsk)) ||
965 !tsk->connected); 1010 !tipc_sk_connected(sk), &wait);
966 finish_wait(sk_sleep(sk), &wait); 1011 remove_wait_queue(sk_sleep(sk), &wait);
967 } while (!done); 1012 } while (!done);
968 return 0; 1013 return 0;
969} 1014}
@@ -1018,14 +1063,17 @@ static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1018 if (dsz > (uint)INT_MAX) 1063 if (dsz > (uint)INT_MAX)
1019 return -EMSGSIZE; 1064 return -EMSGSIZE;
1020 1065
1021 if (unlikely(sock->state != SS_CONNECTED)) { 1066 if (unlikely(!tipc_sk_connected(sk))) {
1022 if (sock->state == SS_DISCONNECTING) 1067 if (sk->sk_state == TIPC_DISCONNECTING)
1023 return -EPIPE; 1068 return -EPIPE;
1024 else 1069 else
1025 return -ENOTCONN; 1070 return -ENOTCONN;
1026 } 1071 }
1027 1072
1028 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); 1073 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1074 if (!timeo && tsk->link_cong)
1075 return -ELINKCONG;
1076
1029 dnode = tsk_peer_node(tsk); 1077 dnode = tsk_peer_node(tsk);
1030 skb_queue_head_init(&pktchain); 1078 skb_queue_head_init(&pktchain);
1031 1079
@@ -1099,10 +1147,8 @@ static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1099 msg_set_lookup_scope(msg, 0); 1147 msg_set_lookup_scope(msg, 0);
1100 msg_set_hdr_sz(msg, SHORT_H_SIZE); 1148 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1101 1149
1102 tsk->probing_intv = CONN_PROBING_INTERVAL; 1150 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
1103 tsk->probing_state = TIPC_CONN_OK; 1151 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
1104 tsk->connected = 1;
1105 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
1106 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port); 1152 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1107 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid); 1153 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1108 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node); 1154 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
@@ -1210,13 +1256,14 @@ static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1210 1256
1211static void tipc_sk_send_ack(struct tipc_sock *tsk) 1257static void tipc_sk_send_ack(struct tipc_sock *tsk)
1212{ 1258{
1213 struct net *net = sock_net(&tsk->sk); 1259 struct sock *sk = &tsk->sk;
1260 struct net *net = sock_net(sk);
1214 struct sk_buff *skb = NULL; 1261 struct sk_buff *skb = NULL;
1215 struct tipc_msg *msg; 1262 struct tipc_msg *msg;
1216 u32 peer_port = tsk_peer_port(tsk); 1263 u32 peer_port = tsk_peer_port(tsk);
1217 u32 dnode = tsk_peer_node(tsk); 1264 u32 dnode = tsk_peer_node(tsk);
1218 1265
1219 if (!tsk->connected) 1266 if (!tipc_sk_connected(sk))
1220 return; 1267 return;
1221 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, 1268 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1222 dnode, tsk_own_node(tsk), peer_port, 1269 dnode, tsk_own_node(tsk), peer_port,
@@ -1245,7 +1292,7 @@ static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1245 for (;;) { 1292 for (;;) {
1246 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 1293 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1247 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { 1294 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1248 if (sock->state == SS_DISCONNECTING) { 1295 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1249 err = -ENOTCONN; 1296 err = -ENOTCONN;
1250 break; 1297 break;
1251 } 1298 }
@@ -1286,6 +1333,7 @@ static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1286 struct tipc_sock *tsk = tipc_sk(sk); 1333 struct tipc_sock *tsk = tipc_sk(sk);
1287 struct sk_buff *buf; 1334 struct sk_buff *buf;
1288 struct tipc_msg *msg; 1335 struct tipc_msg *msg;
1336 bool is_connectionless = tipc_sk_type_connectionless(sk);
1289 long timeo; 1337 long timeo;
1290 unsigned int sz; 1338 unsigned int sz;
1291 u32 err; 1339 u32 err;
@@ -1297,7 +1345,7 @@ static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1297 1345
1298 lock_sock(sk); 1346 lock_sock(sk);
1299 1347
1300 if (unlikely(sock->state == SS_UNCONNECTED)) { 1348 if (!is_connectionless && unlikely(sk->sk_state == TIPC_OPEN)) {
1301 res = -ENOTCONN; 1349 res = -ENOTCONN;
1302 goto exit; 1350 goto exit;
1303 } 1351 }
@@ -1342,8 +1390,8 @@ restart:
1342 goto exit; 1390 goto exit;
1343 res = sz; 1391 res = sz;
1344 } else { 1392 } else {
1345 if ((sock->state == SS_READY) || 1393 if (is_connectionless || err == TIPC_CONN_SHUTDOWN ||
1346 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)) 1394 m->msg_control)
1347 res = 0; 1395 res = 0;
1348 else 1396 else
1349 res = -ECONNRESET; 1397 res = -ECONNRESET;
@@ -1352,7 +1400,7 @@ restart:
1352 if (unlikely(flags & MSG_PEEK)) 1400 if (unlikely(flags & MSG_PEEK))
1353 goto exit; 1401 goto exit;
1354 1402
1355 if (likely(sock->state != SS_READY)) { 1403 if (likely(!is_connectionless)) {
1356 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz); 1404 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1357 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4))) 1405 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1358 tipc_sk_send_ack(tsk); 1406 tipc_sk_send_ack(tsk);
@@ -1383,7 +1431,7 @@ static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1383 struct tipc_msg *msg; 1431 struct tipc_msg *msg;
1384 long timeo; 1432 long timeo;
1385 unsigned int sz; 1433 unsigned int sz;
1386 int sz_to_copy, target, needed; 1434 int target;
1387 int sz_copied = 0; 1435 int sz_copied = 0;
1388 u32 err; 1436 u32 err;
1389 int res = 0, hlen; 1437 int res = 0, hlen;
@@ -1394,7 +1442,7 @@ static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1394 1442
1395 lock_sock(sk); 1443 lock_sock(sk);
1396 1444
1397 if (unlikely(sock->state == SS_UNCONNECTED)) { 1445 if (unlikely(sk->sk_state == TIPC_OPEN)) {
1398 res = -ENOTCONN; 1446 res = -ENOTCONN;
1399 goto exit; 1447 goto exit;
1400 } 1448 }
@@ -1431,11 +1479,13 @@ restart:
1431 1479
1432 /* Capture message data (if valid) & compute return value (always) */ 1480 /* Capture message data (if valid) & compute return value (always) */
1433 if (!err) { 1481 if (!err) {
1434 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle); 1482 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1483 u32 needed;
1484 int sz_to_copy;
1435 1485
1436 sz -= offset; 1486 sz -= offset;
1437 needed = (buf_len - sz_copied); 1487 needed = (buf_len - sz_copied);
1438 sz_to_copy = (sz <= needed) ? sz : needed; 1488 sz_to_copy = min(sz, needed);
1439 1489
1440 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy); 1490 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
1441 if (res) 1491 if (res)
@@ -1445,8 +1495,8 @@ restart:
1445 1495
1446 if (sz_to_copy < sz) { 1496 if (sz_to_copy < sz) {
1447 if (!(flags & MSG_PEEK)) 1497 if (!(flags & MSG_PEEK))
1448 TIPC_SKB_CB(buf)->handle = 1498 TIPC_SKB_CB(buf)->bytes_read =
1449 (void *)(unsigned long)(offset + sz_to_copy); 1499 offset + sz_to_copy;
1450 goto exit; 1500 goto exit;
1451 } 1501 }
1452 } else { 1502 } else {
@@ -1528,49 +1578,31 @@ static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
1528{ 1578{
1529 struct sock *sk = &tsk->sk; 1579 struct sock *sk = &tsk->sk;
1530 struct net *net = sock_net(sk); 1580 struct net *net = sock_net(sk);
1531 struct socket *sock = sk->sk_socket;
1532 struct tipc_msg *hdr = buf_msg(skb); 1581 struct tipc_msg *hdr = buf_msg(skb);
1533 1582
1534 if (unlikely(msg_mcast(hdr))) 1583 if (unlikely(msg_mcast(hdr)))
1535 return false; 1584 return false;
1536 1585
1537 switch ((int)sock->state) { 1586 switch (sk->sk_state) {
1538 case SS_CONNECTED: 1587 case TIPC_CONNECTING:
1539
1540 /* Accept only connection-based messages sent by peer */
1541 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1542 return false;
1543
1544 if (unlikely(msg_errcode(hdr))) {
1545 sock->state = SS_DISCONNECTING;
1546 tsk->connected = 0;
1547 /* Let timer expire on it's own */
1548 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1549 tsk->portid);
1550 }
1551 return true;
1552
1553 case SS_CONNECTING:
1554
1555 /* Accept only ACK or NACK message */ 1588 /* Accept only ACK or NACK message */
1556 if (unlikely(!msg_connected(hdr))) 1589 if (unlikely(!msg_connected(hdr)))
1557 return false; 1590 return false;
1558 1591
1559 if (unlikely(msg_errcode(hdr))) { 1592 if (unlikely(msg_errcode(hdr))) {
1560 sock->state = SS_DISCONNECTING; 1593 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1561 sk->sk_err = ECONNREFUSED; 1594 sk->sk_err = ECONNREFUSED;
1562 return true; 1595 return true;
1563 } 1596 }
1564 1597
1565 if (unlikely(!msg_isdata(hdr))) { 1598 if (unlikely(!msg_isdata(hdr))) {
1566 sock->state = SS_DISCONNECTING; 1599 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1567 sk->sk_err = EINVAL; 1600 sk->sk_err = EINVAL;
1568 return true; 1601 return true;
1569 } 1602 }
1570 1603
1571 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr)); 1604 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1572 msg_set_importance(&tsk->phdr, msg_importance(hdr)); 1605 msg_set_importance(&tsk->phdr, msg_importance(hdr));
1573 sock->state = SS_CONNECTED;
1574 1606
1575 /* If 'ACK+' message, add to socket receive queue */ 1607 /* If 'ACK+' message, add to socket receive queue */
1576 if (msg_data_sz(hdr)) 1608 if (msg_data_sz(hdr))
@@ -1584,18 +1616,31 @@ static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
1584 msg_set_dest_droppable(hdr, 1); 1616 msg_set_dest_droppable(hdr, 1);
1585 return false; 1617 return false;
1586 1618
1587 case SS_LISTENING: 1619 case TIPC_OPEN:
1588 case SS_UNCONNECTED: 1620 case TIPC_DISCONNECTING:
1589 1621 break;
1622 case TIPC_LISTEN:
1590 /* Accept only SYN message */ 1623 /* Accept only SYN message */
1591 if (!msg_connected(hdr) && !(msg_errcode(hdr))) 1624 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1592 return true; 1625 return true;
1593 break; 1626 break;
1594 case SS_DISCONNECTING: 1627 case TIPC_ESTABLISHED:
1595 break; 1628 /* Accept only connection-based messages sent by peer */
1629 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1630 return false;
1631
1632 if (unlikely(msg_errcode(hdr))) {
1633 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1634 /* Let timer expire on it's own */
1635 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1636 tsk->portid);
1637 sk->sk_state_change(sk);
1638 }
1639 return true;
1596 default: 1640 default:
1597 pr_err("Unknown socket state %u\n", sock->state); 1641 pr_err("Unknown sk_state %u\n", sk->sk_state);
1598 } 1642 }
1643
1599 return false; 1644 return false;
1600} 1645}
1601 1646
@@ -1646,7 +1691,6 @@ static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
1646static bool filter_rcv(struct sock *sk, struct sk_buff *skb, 1691static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1647 struct sk_buff_head *xmitq) 1692 struct sk_buff_head *xmitq)
1648{ 1693{
1649 struct socket *sock = sk->sk_socket;
1650 struct tipc_sock *tsk = tipc_sk(sk); 1694 struct tipc_sock *tsk = tipc_sk(sk);
1651 struct tipc_msg *hdr = buf_msg(skb); 1695 struct tipc_msg *hdr = buf_msg(skb);
1652 unsigned int limit = rcvbuf_limit(sk, skb); 1696 unsigned int limit = rcvbuf_limit(sk, skb);
@@ -1672,7 +1716,7 @@ static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1672 } 1716 }
1673 1717
1674 /* Reject if wrong message type for current socket state */ 1718 /* Reject if wrong message type for current socket state */
1675 if (unlikely(sock->state == SS_READY)) { 1719 if (tipc_sk_type_connectionless(sk)) {
1676 if (msg_connected(hdr)) { 1720 if (msg_connected(hdr)) {
1677 err = TIPC_ERR_NO_PORT; 1721 err = TIPC_ERR_NO_PORT;
1678 goto reject; 1722 goto reject;
@@ -1689,7 +1733,7 @@ static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1689 } 1733 }
1690 1734
1691 /* Enqueue message */ 1735 /* Enqueue message */
1692 TIPC_SKB_CB(skb)->handle = NULL; 1736 TIPC_SKB_CB(skb)->bytes_read = 0;
1693 __skb_queue_tail(&sk->sk_receive_queue, skb); 1737 __skb_queue_tail(&sk->sk_receive_queue, skb);
1694 skb_set_owner_r(skb, sk); 1738 skb_set_owner_r(skb, sk);
1695 1739
@@ -1839,8 +1883,8 @@ xmit:
1839 1883
1840static int tipc_wait_for_connect(struct socket *sock, long *timeo_p) 1884static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1841{ 1885{
1886 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1842 struct sock *sk = sock->sk; 1887 struct sock *sk = sock->sk;
1843 DEFINE_WAIT(wait);
1844 int done; 1888 int done;
1845 1889
1846 do { 1890 do {
@@ -1852,9 +1896,10 @@ static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1852 if (signal_pending(current)) 1896 if (signal_pending(current))
1853 return sock_intr_errno(*timeo_p); 1897 return sock_intr_errno(*timeo_p);
1854 1898
1855 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 1899 add_wait_queue(sk_sleep(sk), &wait);
1856 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING); 1900 done = sk_wait_event(sk, timeo_p,
1857 finish_wait(sk_sleep(sk), &wait); 1901 sk->sk_state != TIPC_CONNECTING, &wait);
1902 remove_wait_queue(sk_sleep(sk), &wait);
1858 } while (!done); 1903 } while (!done);
1859 return 0; 1904 return 0;
1860} 1905}
@@ -1876,21 +1921,19 @@ static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1876 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; 1921 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1877 struct msghdr m = {NULL,}; 1922 struct msghdr m = {NULL,};
1878 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout; 1923 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
1879 socket_state previous; 1924 int previous;
1880 int res = 0; 1925 int res = 0;
1881 1926
1882 lock_sock(sk); 1927 lock_sock(sk);
1883 1928
1884 /* DGRAM/RDM connect(), just save the destaddr */ 1929 /* DGRAM/RDM connect(), just save the destaddr */
1885 if (sock->state == SS_READY) { 1930 if (tipc_sk_type_connectionless(sk)) {
1886 if (dst->family == AF_UNSPEC) { 1931 if (dst->family == AF_UNSPEC) {
1887 memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc)); 1932 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
1888 tsk->connected = 0;
1889 } else if (destlen != sizeof(struct sockaddr_tipc)) { 1933 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1890 res = -EINVAL; 1934 res = -EINVAL;
1891 } else { 1935 } else {
1892 memcpy(&tsk->remote, dest, destlen); 1936 memcpy(&tsk->peer, dest, destlen);
1893 tsk->connected = 1;
1894 } 1937 }
1895 goto exit; 1938 goto exit;
1896 } 1939 }
@@ -1906,9 +1949,10 @@ static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1906 goto exit; 1949 goto exit;
1907 } 1950 }
1908 1951
1909 previous = sock->state; 1952 previous = sk->sk_state;
1910 switch (sock->state) { 1953
1911 case SS_UNCONNECTED: 1954 switch (sk->sk_state) {
1955 case TIPC_OPEN:
1912 /* Send a 'SYN-' to destination */ 1956 /* Send a 'SYN-' to destination */
1913 m.msg_name = dest; 1957 m.msg_name = dest;
1914 m.msg_namelen = destlen; 1958 m.msg_namelen = destlen;
@@ -1923,27 +1967,29 @@ static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1923 if ((res < 0) && (res != -EWOULDBLOCK)) 1967 if ((res < 0) && (res != -EWOULDBLOCK))
1924 goto exit; 1968 goto exit;
1925 1969
1926 /* Just entered SS_CONNECTING state; the only 1970 /* Just entered TIPC_CONNECTING state; the only
1927 * difference is that return value in non-blocking 1971 * difference is that return value in non-blocking
1928 * case is EINPROGRESS, rather than EALREADY. 1972 * case is EINPROGRESS, rather than EALREADY.
1929 */ 1973 */
1930 res = -EINPROGRESS; 1974 res = -EINPROGRESS;
1931 case SS_CONNECTING: 1975 /* fall thru' */
1932 if (previous == SS_CONNECTING) 1976 case TIPC_CONNECTING:
1933 res = -EALREADY; 1977 if (!timeout) {
1934 if (!timeout) 1978 if (previous == TIPC_CONNECTING)
1979 res = -EALREADY;
1935 goto exit; 1980 goto exit;
1981 }
1936 timeout = msecs_to_jiffies(timeout); 1982 timeout = msecs_to_jiffies(timeout);
1937 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */ 1983 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1938 res = tipc_wait_for_connect(sock, &timeout); 1984 res = tipc_wait_for_connect(sock, &timeout);
1939 break; 1985 break;
1940 case SS_CONNECTED: 1986 case TIPC_ESTABLISHED:
1941 res = -EISCONN; 1987 res = -EISCONN;
1942 break; 1988 break;
1943 default: 1989 default:
1944 res = -EINVAL; 1990 res = -EINVAL;
1945 break;
1946 } 1991 }
1992
1947exit: 1993exit:
1948 release_sock(sk); 1994 release_sock(sk);
1949 return res; 1995 return res;
@@ -1962,15 +2008,9 @@ static int tipc_listen(struct socket *sock, int len)
1962 int res; 2008 int res;
1963 2009
1964 lock_sock(sk); 2010 lock_sock(sk);
1965 2011 res = tipc_set_sk_state(sk, TIPC_LISTEN);
1966 if (sock->state != SS_UNCONNECTED)
1967 res = -EINVAL;
1968 else {
1969 sock->state = SS_LISTENING;
1970 res = 0;
1971 }
1972
1973 release_sock(sk); 2012 release_sock(sk);
2013
1974 return res; 2014 return res;
1975} 2015}
1976 2016
@@ -1996,9 +2036,6 @@ static int tipc_wait_for_accept(struct socket *sock, long timeo)
1996 err = 0; 2036 err = 0;
1997 if (!skb_queue_empty(&sk->sk_receive_queue)) 2037 if (!skb_queue_empty(&sk->sk_receive_queue))
1998 break; 2038 break;
1999 err = -EINVAL;
2000 if (sock->state != SS_LISTENING)
2001 break;
2002 err = -EAGAIN; 2039 err = -EAGAIN;
2003 if (!timeo) 2040 if (!timeo)
2004 break; 2041 break;
@@ -2029,7 +2066,7 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
2029 2066
2030 lock_sock(sk); 2067 lock_sock(sk);
2031 2068
2032 if (sock->state != SS_LISTENING) { 2069 if (sk->sk_state != TIPC_LISTEN) {
2033 res = -EINVAL; 2070 res = -EINVAL;
2034 goto exit; 2071 goto exit;
2035 } 2072 }
@@ -2040,7 +2077,7 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
2040 2077
2041 buf = skb_peek(&sk->sk_receive_queue); 2078 buf = skb_peek(&sk->sk_receive_queue);
2042 2079
2043 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1); 2080 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 0);
2044 if (res) 2081 if (res)
2045 goto exit; 2082 goto exit;
2046 security_sk_clone(sock->sk, new_sock->sk); 2083 security_sk_clone(sock->sk, new_sock->sk);
@@ -2060,7 +2097,6 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
2060 2097
2061 /* Connect new socket to it's peer */ 2098 /* Connect new socket to it's peer */
2062 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg)); 2099 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
2063 new_sock->state = SS_CONNECTED;
2064 2100
2065 tsk_set_importance(new_tsock, msg_importance(msg)); 2101 tsk_set_importance(new_tsock, msg_importance(msg));
2066 if (msg_named(msg)) { 2102 if (msg_named(msg)) {
@@ -2100,13 +2136,6 @@ exit:
2100static int tipc_shutdown(struct socket *sock, int how) 2136static int tipc_shutdown(struct socket *sock, int how)
2101{ 2137{
2102 struct sock *sk = sock->sk; 2138 struct sock *sk = sock->sk;
2103 struct net *net = sock_net(sk);
2104 struct tipc_sock *tsk = tipc_sk(sk);
2105 struct sk_buff *skb;
2106 u32 dnode = tsk_peer_node(tsk);
2107 u32 dport = tsk_peer_port(tsk);
2108 u32 onode = tipc_own_addr(net);
2109 u32 oport = tsk->portid;
2110 int res; 2139 int res;
2111 2140
2112 if (how != SHUT_RDWR) 2141 if (how != SHUT_RDWR)
@@ -2114,45 +2143,17 @@ static int tipc_shutdown(struct socket *sock, int how)
2114 2143
2115 lock_sock(sk); 2144 lock_sock(sk);
2116 2145
2117 switch (sock->state) { 2146 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2118 case SS_CONNECTING: 2147 sk->sk_shutdown = SEND_SHUTDOWN;
2119 case SS_CONNECTED:
2120
2121restart:
2122 dnode = tsk_peer_node(tsk);
2123
2124 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
2125 skb = __skb_dequeue(&sk->sk_receive_queue);
2126 if (skb) {
2127 if (TIPC_SKB_CB(skb)->handle != NULL) {
2128 kfree_skb(skb);
2129 goto restart;
2130 }
2131 tipc_sk_respond(sk, skb, TIPC_CONN_SHUTDOWN);
2132 } else {
2133 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
2134 TIPC_CONN_MSG, SHORT_H_SIZE,
2135 0, dnode, onode, dport, oport,
2136 TIPC_CONN_SHUTDOWN);
2137 if (skb)
2138 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
2139 }
2140 tsk->connected = 0;
2141 sock->state = SS_DISCONNECTING;
2142 tipc_node_remove_conn(net, dnode, tsk->portid);
2143 /* fall through */
2144
2145 case SS_DISCONNECTING:
2146 2148
2149 if (sk->sk_state == TIPC_DISCONNECTING) {
2147 /* Discard any unreceived messages */ 2150 /* Discard any unreceived messages */
2148 __skb_queue_purge(&sk->sk_receive_queue); 2151 __skb_queue_purge(&sk->sk_receive_queue);
2149 2152
2150 /* Wake up anyone sleeping in poll */ 2153 /* Wake up anyone sleeping in poll */
2151 sk->sk_state_change(sk); 2154 sk->sk_state_change(sk);
2152 res = 0; 2155 res = 0;
2153 break; 2156 } else {
2154
2155 default:
2156 res = -ENOTCONN; 2157 res = -ENOTCONN;
2157 } 2158 }
2158 2159
@@ -2169,17 +2170,16 @@ static void tipc_sk_timeout(unsigned long data)
2169 u32 own_node = tsk_own_node(tsk); 2170 u32 own_node = tsk_own_node(tsk);
2170 2171
2171 bh_lock_sock(sk); 2172 bh_lock_sock(sk);
2172 if (!tsk->connected) { 2173 if (!tipc_sk_connected(sk)) {
2173 bh_unlock_sock(sk); 2174 bh_unlock_sock(sk);
2174 goto exit; 2175 goto exit;
2175 } 2176 }
2176 peer_port = tsk_peer_port(tsk); 2177 peer_port = tsk_peer_port(tsk);
2177 peer_node = tsk_peer_node(tsk); 2178 peer_node = tsk_peer_node(tsk);
2178 2179
2179 if (tsk->probing_state == TIPC_CONN_PROBING) { 2180 if (tsk->probe_unacked) {
2180 if (!sock_owned_by_user(sk)) { 2181 if (!sock_owned_by_user(sk)) {
2181 sk->sk_socket->state = SS_DISCONNECTING; 2182 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2182 tsk->connected = 0;
2183 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk), 2183 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2184 tsk_peer_port(tsk)); 2184 tsk_peer_port(tsk));
2185 sk->sk_state_change(sk); 2185 sk->sk_state_change(sk);
@@ -2188,13 +2188,15 @@ static void tipc_sk_timeout(unsigned long data)
2188 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20)); 2188 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2189 } 2189 }
2190 2190
2191 } else { 2191 bh_unlock_sock(sk);
2192 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, 2192 goto exit;
2193 INT_H_SIZE, 0, peer_node, own_node,
2194 peer_port, tsk->portid, TIPC_OK);
2195 tsk->probing_state = TIPC_CONN_PROBING;
2196 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
2197 } 2193 }
2194
2195 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2196 INT_H_SIZE, 0, peer_node, own_node,
2197 peer_port, tsk->portid, TIPC_OK);
2198 tsk->probe_unacked = true;
2199 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
2198 bh_unlock_sock(sk); 2200 bh_unlock_sock(sk);
2199 if (skb) 2201 if (skb)
2200 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid); 2202 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
@@ -2205,11 +2207,12 @@ exit:
2205static int tipc_sk_publish(struct tipc_sock *tsk, uint scope, 2207static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2206 struct tipc_name_seq const *seq) 2208 struct tipc_name_seq const *seq)
2207{ 2209{
2208 struct net *net = sock_net(&tsk->sk); 2210 struct sock *sk = &tsk->sk;
2211 struct net *net = sock_net(sk);
2209 struct publication *publ; 2212 struct publication *publ;
2210 u32 key; 2213 u32 key;
2211 2214
2212 if (tsk->connected) 2215 if (tipc_sk_connected(sk))
2213 return -EINVAL; 2216 return -EINVAL;
2214 key = tsk->portid + tsk->pub_count + 1; 2217 key = tsk->portid + tsk->pub_count + 1;
2215 if (key == tsk->portid) 2218 if (key == tsk->portid)
@@ -2667,6 +2670,7 @@ static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2667 struct nlattr *attrs; 2670 struct nlattr *attrs;
2668 struct net *net = sock_net(skb->sk); 2671 struct net *net = sock_net(skb->sk);
2669 struct tipc_net *tn = net_generic(net, tipc_net_id); 2672 struct tipc_net *tn = net_generic(net, tipc_net_id);
2673 struct sock *sk = &tsk->sk;
2670 2674
2671 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 2675 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2672 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET); 2676 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
@@ -2681,7 +2685,7 @@ static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2681 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr)) 2685 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
2682 goto attr_msg_cancel; 2686 goto attr_msg_cancel;
2683 2687
2684 if (tsk->connected) { 2688 if (tipc_sk_connected(sk)) {
2685 err = __tipc_nl_add_sk_con(skb, tsk); 2689 err = __tipc_nl_add_sk_con(skb, tsk);
2686 if (err) 2690 if (err)
2687 goto attr_msg_cancel; 2691 goto attr_msg_cancel;