diff options
author | Gerrit Renker <gerrit@erg.abdn.ac.uk> | 2007-11-28 08:59:48 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2008-01-28 17:55:13 -0500 |
commit | 0c869620762fea4b3acf6502d9e80840b27ec642 (patch) | |
tree | 218146397018baf917260f3d0a90dd89fc13cc7f /net/dccp/proto.c | |
parent | f11135a3442996d78dad99933bfdb90d1f6588d3 (diff) |
[DCCP]: Integrate state transitions for passive-close
This adds the necessary state transitions for the two forms of passive-close
* PASSIVE_CLOSE - which is entered when a host receives a Close;
* PASSIVE_CLOSEREQ - which is entered when a client receives a CloseReq.
Here is a detailed account of what the patch does in each state.
1) Receiving CloseReq
The pseudo-code in 8.5 says:
Step 13: Process CloseReq
If P.type == CloseReq and S.state < CLOSEREQ,
Generate Close
S.state := CLOSING
Set CLOSING timer.
This means we need to address what to do in CLOSED, LISTEN, REQUEST, RESPOND, PARTOPEN, and OPEN.
* CLOSED: silently ignore - it may be a late or duplicate CloseReq;
* LISTEN/RESPOND: will not appear, since Step 7 is performed first (we know we are the client);
* REQUEST: perform Step 13 directly (no need to enqueue packet);
* OPEN/PARTOPEN: enter PASSIVE_CLOSEREQ so that the application has a chance to process unread data.
When already in PASSIVE_CLOSEREQ, no second CloseReq is enqueued. In any other state, the CloseReq is ignored.
I think that this offers some robustness against rare and pathological cases: e.g. a simultaneous close where
the client sends a Close and the server a CloseReq. The client will then be retransmitting its Close until it
gets the Reset, so ignoring the CloseReq while in state CLOSING is sane.
2) Receiving Close
The code below from 8.5 is unconditional.
Step 14: Process Close
If P.type == Close,
Generate Reset(Closed)
Tear down connection
Drop packet and return
Thus we need to consider all states:
* CLOSED: silently ignore, since this can happen when a retransmitted or late Close arrives;
* LISTEN: dccp_rcv_state_process() will generate a Reset ("No Connection");
* REQUEST: perform Step 14 directly (no need to enqueue packet);
* RESPOND: dccp_check_req() will generate a Reset ("Packet Error") -- left it at that;
* OPEN/PARTOPEN: enter PASSIVE_CLOSE so that application has a chance to process unread data;
* CLOSEREQ: server performed active-close -- perform Step 14;
* CLOSING: simultaneous-close: use a tie-breaker to avoid message ping-pong (see comment);
* PASSIVE_CLOSEREQ: ignore - the peer has a bug (sending first a CloseReq and now a Close);
* TIMEWAIT: packet is ignored.
Note that the condition of receiving a packet in state CLOSED here is different from the condition "there
is no socket for such a connection": the socket still exists, but its state indicates it is unusable.
Last, dccp_finish_passive_close sets either DCCP_CLOSED or DCCP_CLOSING = TCP_CLOSING, so that
sk_stream_wait_close() will wait for the final Reset (which will trigger CLOSING => CLOSED).
Signed-off-by: Gerrit Renker <gerrit@erg.abdn.ac.uk>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/dccp/proto.c')
-rw-r--r-- | net/dccp/proto.c | 88 |
1 files changed, 58 insertions, 30 deletions
diff --git a/net/dccp/proto.c b/net/dccp/proto.c index 3489d3f21f50..60f40ec72ff3 100644 --- a/net/dccp/proto.c +++ b/net/dccp/proto.c | |||
@@ -71,7 +71,8 @@ void dccp_set_state(struct sock *sk, const int state) | |||
71 | break; | 71 | break; |
72 | 72 | ||
73 | case DCCP_CLOSED: | 73 | case DCCP_CLOSED: |
74 | if (oldstate == DCCP_CLOSING || oldstate == DCCP_OPEN) | 74 | if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ || |
75 | oldstate == DCCP_CLOSING) | ||
75 | DCCP_INC_STATS(DCCP_MIB_ESTABRESETS); | 76 | DCCP_INC_STATS(DCCP_MIB_ESTABRESETS); |
76 | 77 | ||
77 | sk->sk_prot->unhash(sk); | 78 | sk->sk_prot->unhash(sk); |
@@ -92,6 +93,24 @@ void dccp_set_state(struct sock *sk, const int state) | |||
92 | 93 | ||
93 | EXPORT_SYMBOL_GPL(dccp_set_state); | 94 | EXPORT_SYMBOL_GPL(dccp_set_state); |
94 | 95 | ||
96 | static void dccp_finish_passive_close(struct sock *sk) | ||
97 | { | ||
98 | switch (sk->sk_state) { | ||
99 | case DCCP_PASSIVE_CLOSE: | ||
100 | /* Node (client or server) has received Close packet. */ | ||
101 | dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED); | ||
102 | dccp_set_state(sk, DCCP_CLOSED); | ||
103 | break; | ||
104 | case DCCP_PASSIVE_CLOSEREQ: | ||
105 | /* | ||
106 | * Client received CloseReq. We set the `active' flag so that | ||
107 | * dccp_send_close() retransmits the Close as per RFC 4340, 8.3. | ||
108 | */ | ||
109 | dccp_send_close(sk, 1); | ||
110 | dccp_set_state(sk, DCCP_CLOSING); | ||
111 | } | ||
112 | } | ||
113 | |||
95 | void dccp_done(struct sock *sk) | 114 | void dccp_done(struct sock *sk) |
96 | { | 115 | { |
97 | dccp_set_state(sk, DCCP_CLOSED); | 116 | dccp_set_state(sk, DCCP_CLOSED); |
@@ -762,19 +781,26 @@ int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, | |||
762 | 781 | ||
763 | dh = dccp_hdr(skb); | 782 | dh = dccp_hdr(skb); |
764 | 783 | ||
765 | if (dh->dccph_type == DCCP_PKT_DATA || | 784 | switch (dh->dccph_type) { |
766 | dh->dccph_type == DCCP_PKT_DATAACK) | 785 | case DCCP_PKT_DATA: |
786 | case DCCP_PKT_DATAACK: | ||
767 | goto found_ok_skb; | 787 | goto found_ok_skb; |
768 | 788 | ||
769 | if (dh->dccph_type == DCCP_PKT_RESET || | 789 | case DCCP_PKT_CLOSE: |
770 | dh->dccph_type == DCCP_PKT_CLOSE) { | 790 | case DCCP_PKT_CLOSEREQ: |
771 | dccp_pr_debug("found fin ok!\n"); | 791 | if (!(flags & MSG_PEEK)) |
792 | dccp_finish_passive_close(sk); | ||
793 | /* fall through */ | ||
794 | case DCCP_PKT_RESET: | ||
795 | dccp_pr_debug("found fin (%s) ok!\n", | ||
796 | dccp_packet_name(dh->dccph_type)); | ||
772 | len = 0; | 797 | len = 0; |
773 | goto found_fin_ok; | 798 | goto found_fin_ok; |
799 | default: | ||
800 | dccp_pr_debug("packet_type=%s\n", | ||
801 | dccp_packet_name(dh->dccph_type)); | ||
802 | sk_eat_skb(sk, skb, 0); | ||
774 | } | 803 | } |
775 | dccp_pr_debug("packet_type=%s\n", | ||
776 | dccp_packet_name(dh->dccph_type)); | ||
777 | sk_eat_skb(sk, skb, 0); | ||
778 | verify_sock_status: | 804 | verify_sock_status: |
779 | if (sock_flag(sk, SOCK_DONE)) { | 805 | if (sock_flag(sk, SOCK_DONE)) { |
780 | len = 0; | 806 | len = 0; |
@@ -876,28 +902,30 @@ out: | |||
876 | 902 | ||
877 | EXPORT_SYMBOL_GPL(inet_dccp_listen); | 903 | EXPORT_SYMBOL_GPL(inet_dccp_listen); |
878 | 904 | ||
879 | static const unsigned char dccp_new_state[] = { | 905 | static void dccp_terminate_connection(struct sock *sk) |
880 | /* current state: new state: action: */ | ||
881 | [0] = DCCP_CLOSED, | ||
882 | [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, | ||
883 | [DCCP_REQUESTING] = DCCP_CLOSED, | ||
884 | [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, | ||
885 | [DCCP_LISTEN] = DCCP_CLOSED, | ||
886 | [DCCP_RESPOND] = DCCP_CLOSED, | ||
887 | [DCCP_CLOSING] = DCCP_CLOSED, | ||
888 | [DCCP_TIME_WAIT] = DCCP_CLOSED, | ||
889 | [DCCP_CLOSED] = DCCP_CLOSED, | ||
890 | }; | ||
891 | |||
892 | static int dccp_close_state(struct sock *sk) | ||
893 | { | 906 | { |
894 | const int next = dccp_new_state[sk->sk_state]; | 907 | u8 next_state = DCCP_CLOSED; |
895 | const int ns = next & DCCP_STATE_MASK; | ||
896 | 908 | ||
897 | if (ns != sk->sk_state) | 909 | switch (sk->sk_state) { |
898 | dccp_set_state(sk, ns); | 910 | case DCCP_PASSIVE_CLOSE: |
911 | case DCCP_PASSIVE_CLOSEREQ: | ||
912 | dccp_finish_passive_close(sk); | ||
913 | break; | ||
914 | case DCCP_PARTOPEN: | ||
915 | dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk); | ||
916 | inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); | ||
917 | /* fall through */ | ||
918 | case DCCP_OPEN: | ||
919 | dccp_send_close(sk, 1); | ||
899 | 920 | ||
900 | return next & DCCP_ACTION_FIN; | 921 | if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER) |
922 | next_state = DCCP_ACTIVE_CLOSEREQ; | ||
923 | else | ||
924 | next_state = DCCP_CLOSING; | ||
925 | /* fall through */ | ||
926 | default: | ||
927 | dccp_set_state(sk, next_state); | ||
928 | } | ||
901 | } | 929 | } |
902 | 930 | ||
903 | void dccp_close(struct sock *sk, long timeout) | 931 | void dccp_close(struct sock *sk, long timeout) |
@@ -940,8 +968,8 @@ void dccp_close(struct sock *sk, long timeout) | |||
940 | } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { | 968 | } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { |
941 | /* Check zero linger _after_ checking for unread data. */ | 969 | /* Check zero linger _after_ checking for unread data. */ |
942 | sk->sk_prot->disconnect(sk, 0); | 970 | sk->sk_prot->disconnect(sk, 0); |
943 | } else if (dccp_close_state(sk)) { | 971 | } else if (sk->sk_state != DCCP_CLOSED) { |
944 | dccp_send_close(sk, 1); | 972 | dccp_terminate_connection(sk); |
945 | } | 973 | } |
946 | 974 | ||
947 | sk_stream_wait_close(sk, timeout); | 975 | sk_stream_wait_close(sk, timeout); |