| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  net/dccp/timer.c | 
 | 3 |  *  | 
 | 4 |  *  An implementation of the DCCP protocol | 
 | 5 |  *  Arnaldo Carvalho de Melo <acme@conectiva.com.br> | 
 | 6 |  * | 
 | 7 |  *	This program is free software; you can redistribute it and/or | 
 | 8 |  *	modify it under the terms of the GNU General Public License | 
 | 9 |  *	as published by the Free Software Foundation; either version | 
 | 10 |  *	2 of the License, or (at your option) any later version. | 
 | 11 |  */ | 
 | 12 |  | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 13 | #include <linux/dccp.h> | 
 | 14 | #include <linux/skbuff.h> | 
 | 15 |  | 
 | 16 | #include "dccp.h" | 
 | 17 |  | 
 | 18 | static void dccp_write_timer(unsigned long data); | 
 | 19 | static void dccp_keepalive_timer(unsigned long data); | 
 | 20 | static void dccp_delack_timer(unsigned long data); | 
 | 21 |  | 
 | 22 | void dccp_init_xmit_timers(struct sock *sk) | 
 | 23 | { | 
 | 24 | 	inet_csk_init_xmit_timers(sk, &dccp_write_timer, &dccp_delack_timer, | 
 | 25 | 				  &dccp_keepalive_timer); | 
 | 26 | } | 
 | 27 |  | 
 | 28 | static void dccp_write_err(struct sock *sk) | 
 | 29 | { | 
 | 30 | 	sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT; | 
 | 31 | 	sk->sk_error_report(sk); | 
 | 32 |  | 
| Arnaldo Carvalho de Melo | 017487d | 2006-03-20 19:25:24 -0800 | [diff] [blame] | 33 | 	dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED); | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 34 | 	dccp_done(sk); | 
 | 35 | 	DCCP_INC_STATS_BH(DCCP_MIB_ABORTONTIMEOUT); | 
 | 36 | } | 
 | 37 |  | 
 | 38 | /* A write timeout has occurred. Process the after effects. */ | 
 | 39 | static int dccp_write_timeout(struct sock *sk) | 
 | 40 | { | 
 | 41 | 	const struct inet_connection_sock *icsk = inet_csk(sk); | 
 | 42 | 	int retry_until; | 
 | 43 |  | 
 | 44 | 	if (sk->sk_state == DCCP_REQUESTING || sk->sk_state == DCCP_PARTOPEN) { | 
 | 45 | 		if (icsk->icsk_retransmits != 0) | 
 | 46 | 			dst_negative_advice(&sk->sk_dst_cache); | 
| Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 47 | 		retry_until = icsk->icsk_syn_retries ? : | 
 | 48 | 			    /* FIXME! */ 3 /* FIXME! sysctl_tcp_syn_retries */; | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 49 | 	} else { | 
| Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 50 | 		if (icsk->icsk_retransmits >= | 
 | 51 | 		     /* FIXME! sysctl_tcp_retries1 */ 5 /* FIXME! */) { | 
 | 52 | 			/* NOTE. draft-ietf-tcpimpl-pmtud-01.txt requires pmtu | 
 | 53 | 			   black hole detection. :-( | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 54 |  | 
 | 55 | 			   It is place to make it. It is not made. I do not want | 
 | 56 | 			   to make it. It is disguisting. It does not work in any | 
 | 57 | 			   case. Let me to cite the same draft, which requires for | 
 | 58 | 			   us to implement this: | 
 | 59 |  | 
 | 60 |    "The one security concern raised by this memo is that ICMP black holes | 
 | 61 |    are often caused by over-zealous security administrators who block | 
 | 62 |    all ICMP messages.  It is vitally important that those who design and | 
 | 63 |    deploy security systems understand the impact of strict filtering on | 
 | 64 |    upper-layer protocols.  The safest web site in the world is worthless | 
 | 65 |    if most TCP implementations cannot transfer data from it.  It would | 
 | 66 |    be far nicer to have all of the black holes fixed rather than fixing | 
 | 67 |    all of the TCP implementations." | 
 | 68 |  | 
 | 69 |                            Golden words :-). | 
 | 70 | 		   */ | 
 | 71 |  | 
 | 72 | 			dst_negative_advice(&sk->sk_dst_cache); | 
 | 73 | 		} | 
 | 74 |  | 
 | 75 | 		retry_until = /* FIXME! */ 15 /* FIXME! sysctl_tcp_retries2 */; | 
 | 76 | 		/* | 
 | 77 | 		 * FIXME: see tcp_write_timout and tcp_out_of_resources | 
 | 78 | 		 */ | 
 | 79 | 	} | 
 | 80 |  | 
 | 81 | 	if (icsk->icsk_retransmits >= retry_until) { | 
 | 82 | 		/* Has it gone just too far? */ | 
 | 83 | 		dccp_write_err(sk); | 
 | 84 | 		return 1; | 
 | 85 | 	} | 
 | 86 | 	return 0; | 
 | 87 | } | 
 | 88 |  | 
 | 89 | /* This is the same as tcp_delack_timer, sans prequeue & mem_reclaim stuff */ | 
 | 90 | static void dccp_delack_timer(unsigned long data) | 
 | 91 | { | 
 | 92 | 	struct sock *sk = (struct sock *)data; | 
 | 93 | 	struct inet_connection_sock *icsk = inet_csk(sk); | 
 | 94 |  | 
 | 95 | 	bh_lock_sock(sk); | 
 | 96 | 	if (sock_owned_by_user(sk)) { | 
 | 97 | 		/* Try again later. */ | 
 | 98 | 		icsk->icsk_ack.blocked = 1; | 
 | 99 | 		NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKLOCKED); | 
| Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 100 | 		sk_reset_timer(sk, &icsk->icsk_delack_timer, | 
 | 101 | 			       jiffies + TCP_DELACK_MIN); | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 102 | 		goto out; | 
 | 103 | 	} | 
 | 104 |  | 
| Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 105 | 	if (sk->sk_state == DCCP_CLOSED || | 
 | 106 | 	    !(icsk->icsk_ack.pending & ICSK_ACK_TIMER)) | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 107 | 		goto out; | 
 | 108 | 	if (time_after(icsk->icsk_ack.timeout, jiffies)) { | 
| Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 109 | 		sk_reset_timer(sk, &icsk->icsk_delack_timer, | 
 | 110 | 			       icsk->icsk_ack.timeout); | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 111 | 		goto out; | 
 | 112 | 	} | 
 | 113 |  | 
 | 114 | 	icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER; | 
 | 115 |  | 
 | 116 | 	if (inet_csk_ack_scheduled(sk)) { | 
 | 117 | 		if (!icsk->icsk_ack.pingpong) { | 
 | 118 | 			/* Delayed ACK missed: inflate ATO. */ | 
| Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 119 | 			icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, | 
 | 120 | 						 icsk->icsk_rto); | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 121 | 		} else { | 
 | 122 | 			/* Delayed ACK missed: leave pingpong mode and | 
 | 123 | 			 * deflate ATO. | 
 | 124 | 			 */ | 
 | 125 | 			icsk->icsk_ack.pingpong = 0; | 
 | 126 | 			icsk->icsk_ack.ato = TCP_ATO_MIN; | 
 | 127 | 		} | 
 | 128 | 		dccp_send_ack(sk); | 
 | 129 | 		NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKS); | 
 | 130 | 	} | 
 | 131 | out: | 
 | 132 | 	bh_unlock_sock(sk); | 
 | 133 | 	sock_put(sk); | 
 | 134 | } | 
 | 135 |  | 
 | 136 | /* | 
 | 137 |  *	The DCCP retransmit timer. | 
 | 138 |  */ | 
 | 139 | static void dccp_retransmit_timer(struct sock *sk) | 
 | 140 | { | 
 | 141 | 	struct inet_connection_sock *icsk = inet_csk(sk); | 
 | 142 |  | 
| Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 143 | 	/* retransmit timer is used for feature negotiation throughout | 
 | 144 | 	 * connection.  In this case, no packet is re-transmitted, but rather an | 
 | 145 | 	 * ack is generated and pending changes are splaced into its options. | 
 | 146 | 	 */ | 
 | 147 | 	if (sk->sk_send_head == NULL) { | 
 | 148 | 		dccp_pr_debug("feat negotiation retransmit timeout %p\n", sk); | 
 | 149 | 		if (sk->sk_state == DCCP_OPEN) | 
 | 150 | 			dccp_send_ack(sk); | 
 | 151 | 		goto backoff; | 
 | 152 | 	} | 
 | 153 |  | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 154 | 	/* | 
 | 155 | 	 * sk->sk_send_head has to have one skb with | 
 | 156 | 	 * DCCP_SKB_CB(skb)->dccpd_type set to one of the retransmittable DCCP | 
| Arnaldo Carvalho de Melo | 03ace39 | 2005-08-21 05:36:45 -0300 | [diff] [blame] | 157 | 	 * packet types (REQUEST, RESPONSE, the ACK in the 3way handshake | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 158 | 	 * (PARTOPEN timer), etc). | 
 | 159 | 	 */ | 
 | 160 | 	BUG_TRAP(sk->sk_send_head != NULL); | 
 | 161 |  | 
 | 162 | 	/*  | 
 | 163 | 	 * More than than 4MSL (8 minutes) has passed, a RESET(aborted) was | 
 | 164 | 	 * sent, no need to retransmit, this sock is dead. | 
 | 165 | 	 */ | 
 | 166 | 	if (dccp_write_timeout(sk)) | 
 | 167 | 		goto out; | 
 | 168 |  | 
 | 169 | 	/* | 
 | 170 | 	 * We want to know the number of packets retransmitted, not the | 
 | 171 | 	 * total number of retransmissions of clones of original packets. | 
 | 172 | 	 */ | 
 | 173 | 	if (icsk->icsk_retransmits == 0) | 
 | 174 | 		DCCP_INC_STATS_BH(DCCP_MIB_TIMEOUTS); | 
 | 175 |  | 
 | 176 | 	if (dccp_retransmit_skb(sk, sk->sk_send_head) < 0) { | 
 | 177 | 		/* | 
 | 178 | 		 * Retransmission failed because of local congestion, | 
 | 179 | 		 * do not backoff. | 
 | 180 | 		 */ | 
 | 181 | 		if (icsk->icsk_retransmits == 0) | 
 | 182 | 			icsk->icsk_retransmits = 1; | 
 | 183 | 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, | 
 | 184 | 					  min(icsk->icsk_rto, | 
 | 185 | 					      TCP_RESOURCE_PROBE_INTERVAL), | 
| Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 186 | 					  DCCP_RTO_MAX); | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 187 | 		goto out; | 
 | 188 | 	} | 
 | 189 |  | 
| Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 190 | backoff: | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 191 | 	icsk->icsk_backoff++; | 
 | 192 | 	icsk->icsk_retransmits++; | 
 | 193 |  | 
 | 194 | 	icsk->icsk_rto = min(icsk->icsk_rto << 1, DCCP_RTO_MAX); | 
| Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 195 | 	inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, | 
 | 196 | 				  DCCP_RTO_MAX); | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 197 | 	if (icsk->icsk_retransmits > 3 /* FIXME: sysctl_dccp_retries1 */) | 
 | 198 | 		__sk_dst_reset(sk); | 
 | 199 | out:; | 
 | 200 | } | 
 | 201 |  | 
 | 202 | static void dccp_write_timer(unsigned long data) | 
 | 203 | { | 
 | 204 | 	struct sock *sk = (struct sock *)data; | 
 | 205 | 	struct inet_connection_sock *icsk = inet_csk(sk); | 
 | 206 | 	int event = 0; | 
 | 207 |  | 
 | 208 | 	bh_lock_sock(sk); | 
 | 209 | 	if (sock_owned_by_user(sk)) { | 
 | 210 | 		/* Try again later */ | 
| Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 211 | 		sk_reset_timer(sk, &icsk->icsk_retransmit_timer, | 
 | 212 | 			       jiffies + (HZ / 20)); | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 213 | 		goto out; | 
 | 214 | 	} | 
 | 215 |  | 
 | 216 | 	if (sk->sk_state == DCCP_CLOSED || !icsk->icsk_pending) | 
 | 217 | 		goto out; | 
 | 218 |  | 
 | 219 | 	if (time_after(icsk->icsk_timeout, jiffies)) { | 
| Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 220 | 		sk_reset_timer(sk, &icsk->icsk_retransmit_timer, | 
 | 221 | 			       icsk->icsk_timeout); | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 222 | 		goto out; | 
 | 223 | 	} | 
 | 224 |  | 
 | 225 | 	event = icsk->icsk_pending; | 
 | 226 | 	icsk->icsk_pending = 0; | 
 | 227 |  | 
 | 228 | 	switch (event) { | 
 | 229 | 	case ICSK_TIME_RETRANS: | 
 | 230 | 		dccp_retransmit_timer(sk); | 
 | 231 | 		break; | 
 | 232 | 	} | 
 | 233 | out: | 
 | 234 | 	bh_unlock_sock(sk); | 
 | 235 | 	sock_put(sk); | 
 | 236 | } | 
 | 237 |  | 
 | 238 | /* | 
 | 239 |  *	Timer for listening sockets | 
 | 240 |  */ | 
 | 241 | static void dccp_response_timer(struct sock *sk) | 
 | 242 | { | 
| Arnaldo Carvalho de Melo | 7690af3 | 2005-08-13 20:34:54 -0300 | [diff] [blame] | 243 | 	inet_csk_reqsk_queue_prune(sk, TCP_SYNQ_INTERVAL, DCCP_TIMEOUT_INIT, | 
 | 244 | 				   DCCP_RTO_MAX); | 
| Arnaldo Carvalho de Melo | 7c65787 | 2005-08-09 20:14:34 -0700 | [diff] [blame] | 245 | } | 
 | 246 |  | 
 | 247 | static void dccp_keepalive_timer(unsigned long data) | 
 | 248 | { | 
 | 249 | 	struct sock *sk = (struct sock *)data; | 
 | 250 |  | 
 | 251 | 	/* Only process if socket is not in use. */ | 
 | 252 | 	bh_lock_sock(sk); | 
 | 253 | 	if (sock_owned_by_user(sk)) { | 
 | 254 | 		/* Try again later. */  | 
 | 255 | 		inet_csk_reset_keepalive_timer(sk, HZ / 20); | 
 | 256 | 		goto out; | 
 | 257 | 	} | 
 | 258 |  | 
 | 259 | 	if (sk->sk_state == DCCP_LISTEN) { | 
 | 260 | 		dccp_response_timer(sk); | 
 | 261 | 		goto out; | 
 | 262 | 	} | 
 | 263 | out: | 
 | 264 | 	bh_unlock_sock(sk); | 
 | 265 | 	sock_put(sk); | 
 | 266 | } |