1/*
2 *  net/dccp/input.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
13#include <linux/dccp.h>
14#include <linux/skbuff.h>
15
16#include <net/sock.h>
17
18#include "ackvec.h"
19#include "ccid.h"
20#include "dccp.h"
21
22static void dccp_fin(struct sock *sk, struct sk_buff *skb)
23{
24	sk->sk_shutdown |= RCV_SHUTDOWN;
25	sock_set_flag(sk, SOCK_DONE);
26	__skb_pull(skb, dccp_hdr(skb)->dccph_doff * 4);
27	__skb_queue_tail(&sk->sk_receive_queue, skb);
28	skb_set_owner_r(skb, sk);
29	sk->sk_data_ready(sk, 0);
30}
31
32static void dccp_rcv_close(struct sock *sk, struct sk_buff *skb)
33{
34	dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
35	dccp_fin(sk, skb);
36	dccp_set_state(sk, DCCP_CLOSED);
37	sk_wake_async(sk, 1, POLL_HUP);
38}
39
40static void dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb)
41{
42	/*
43	 *   Step 7: Check for unexpected packet types
44	 *      If (S.is_server and P.type == CloseReq)
45	 *	  Send Sync packet acknowledging P.seqno
46	 *	  Drop packet and return
47	 */
48	if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) {
49		dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
50		return;
51	}
52
53	if (sk->sk_state != DCCP_CLOSING)
54		dccp_set_state(sk, DCCP_CLOSING);
55	dccp_send_close(sk, 0);
56}
57
58static void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb)
59{
60	struct dccp_sock *dp = dccp_sk(sk);
61
62	if (dccp_msk(sk)->dccpms_send_ack_vector)
63		dccp_ackvec_check_rcv_ackno(dp->dccps_hc_rx_ackvec, sk,
64					    DCCP_SKB_CB(skb)->dccpd_ack_seq);
65}
66
67static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
68{
69	const struct dccp_hdr *dh = dccp_hdr(skb);
70	struct dccp_sock *dp = dccp_sk(sk);
71	u64 lswl, lawl;
72
73	/*
74	 *   Step 5: Prepare sequence numbers for Sync
75	 *     If P.type == Sync or P.type == SyncAck,
76	 *	  If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
77	 *	     / * P is valid, so update sequence number variables
78	 *		 accordingly.  After this update, P will pass the tests
79	 *		 in Step 6.  A SyncAck is generated if necessary in
80	 *		 Step 15 * /
81	 *	     Update S.GSR, S.SWL, S.SWH
82	 *	  Otherwise,
83	 *	     Drop packet and return
84	 */
85	if (dh->dccph_type == DCCP_PKT_SYNC ||
86	    dh->dccph_type == DCCP_PKT_SYNCACK) {
87		if (between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
88			      dp->dccps_awl, dp->dccps_awh) &&
89		    dccp_delta_seqno(dp->dccps_swl,
90				     DCCP_SKB_CB(skb)->dccpd_seq) >= 0)
91			dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
92		else
93			return -1;
94	}
95
96	/*
97	 *   Step 6: Check sequence numbers
98	 *      Let LSWL = S.SWL and LAWL = S.AWL
99	 *      If P.type == CloseReq or P.type == Close or P.type == Reset,
100	 *	  LSWL := S.GSR + 1, LAWL := S.GAR
101	 *      If LSWL <= P.seqno <= S.SWH
102	 *	     and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
103	 *	  Update S.GSR, S.SWL, S.SWH
104	 *	  If P.type != Sync,
105	 *	     Update S.GAR
106	 *      Otherwise,
107	 *	  Send Sync packet acknowledging P.seqno
108	 *	  Drop packet and return
109	 */
110	lswl = dp->dccps_swl;
111	lawl = dp->dccps_awl;
112
113	if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
114	    dh->dccph_type == DCCP_PKT_CLOSE ||
115	    dh->dccph_type == DCCP_PKT_RESET) {
116		lswl = dp->dccps_gsr;
117		dccp_inc_seqno(&lswl);
118		lawl = dp->dccps_gar;
119	}
120
121	if (between48(DCCP_SKB_CB(skb)->dccpd_seq, lswl, dp->dccps_swh) &&
122	    (DCCP_SKB_CB(skb)->dccpd_ack_seq == DCCP_PKT_WITHOUT_ACK_SEQ ||
123	     between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
124		       lawl, dp->dccps_awh))) {
125		dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
126
127		if (dh->dccph_type != DCCP_PKT_SYNC &&
128		    (DCCP_SKB_CB(skb)->dccpd_ack_seq !=
129		     DCCP_PKT_WITHOUT_ACK_SEQ))
130			dp->dccps_gar = DCCP_SKB_CB(skb)->dccpd_ack_seq;
131	} else {
132		DCCP_WARN("DCCP: Step 6 failed for %s packet, "
133			  "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
134			  "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
135			  "sending SYNC...\n",  dccp_packet_name(dh->dccph_type),
136			  (unsigned long long) lswl,
137			  (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq,
138			  (unsigned long long) dp->dccps_swh,
139			  (DCCP_SKB_CB(skb)->dccpd_ack_seq ==
140				DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist" : "exists",
141			  (unsigned long long) lawl,
142			  (unsigned long long) DCCP_SKB_CB(skb)->dccpd_ack_seq,
143			  (unsigned long long) dp->dccps_awh);
144		dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
145		return -1;
146	}
147
148	return 0;
149}
150
151static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
152				  const struct dccp_hdr *dh, const unsigned len)
153{
154	struct dccp_sock *dp = dccp_sk(sk);
155
156	switch (dccp_hdr(skb)->dccph_type) {
157	case DCCP_PKT_DATAACK:
158	case DCCP_PKT_DATA:
159		__skb_pull(skb, dh->dccph_doff * 4);
160		__skb_queue_tail(&sk->sk_receive_queue, skb);
161		skb_set_owner_r(skb, sk);
162		sk->sk_data_ready(sk, 0);
163		return 0;
164	case DCCP_PKT_ACK:
165		goto discard;
166	case DCCP_PKT_RESET:
167		/*
168		 *  Step 9: Process Reset
169		 *	If P.type == Reset,
170		 *		Tear down connection
171		 *		S.state := TIMEWAIT
172		 *		Set TIMEWAIT timer
173		 *		Drop packet and return
174		*/
175		dccp_fin(sk, skb);
176		dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
177		return 0;
178	case DCCP_PKT_CLOSEREQ:
179		dccp_rcv_closereq(sk, skb);
180		goto discard;
181	case DCCP_PKT_CLOSE:
182		dccp_rcv_close(sk, skb);
183		return 0;
184	case DCCP_PKT_REQUEST:
185		/* Step 7
186		 *   or (S.is_server and P.type == Response)
187		 *   or (S.is_client and P.type == Request)
188		 *   or (S.state >= OPEN and P.type == Request
189		 *	and P.seqno >= S.OSR)
190		 *    or (S.state >= OPEN and P.type == Response
191		 *	and P.seqno >= S.OSR)
192		 *    or (S.state == RESPOND and P.type == Data),
193		 *  Send Sync packet acknowledging P.seqno
194		 *  Drop packet and return
195		 */
196		if (dp->dccps_role != DCCP_ROLE_LISTEN)
197			goto send_sync;
198		goto check_seq;
199	case DCCP_PKT_RESPONSE:
200		if (dp->dccps_role != DCCP_ROLE_CLIENT)
201			goto send_sync;
202check_seq:
203		if (dccp_delta_seqno(dp->dccps_osr,
204				     DCCP_SKB_CB(skb)->dccpd_seq) >= 0) {
205send_sync:
206			dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
207				       DCCP_PKT_SYNC);
208		}
209		break;
210	case DCCP_PKT_SYNC:
211		dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
212			       DCCP_PKT_SYNCACK);
213		/*
214		 * From RFC 4340, sec. 5.7
215		 *
216		 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
217		 * MAY have non-zero-length application data areas, whose
218		 * contents receivers MUST ignore.
219		 */
220		goto discard;
221	}
222
223	DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
224discard:
225	__kfree_skb(skb);
226	return 0;
227}
228
229int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
230			 const struct dccp_hdr *dh, const unsigned len)
231{
232	struct dccp_sock *dp = dccp_sk(sk);
233
234	if (dccp_check_seqno(sk, skb))
235		goto discard;
236
237	if (dccp_parse_options(sk, skb))
238		goto discard;
239
240	if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
241		dccp_event_ack_recv(sk, skb);
242
243	if (dccp_msk(sk)->dccpms_send_ack_vector &&
244	    dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
245			    DCCP_SKB_CB(skb)->dccpd_seq,
246			    DCCP_ACKVEC_STATE_RECEIVED))
247		goto discard;
248
249	ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
250	ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
251
252	return __dccp_rcv_established(sk, skb, dh, len);
253discard:
254	__kfree_skb(skb);
255	return 0;
256}
257
258EXPORT_SYMBOL_GPL(dccp_rcv_established);
259
260static int dccp_rcv_request_sent_state_process(struct sock *sk,
261					       struct sk_buff *skb,
262					       const struct dccp_hdr *dh,
263					       const unsigned len)
264{
265	/*
266	 *  Step 4: Prepare sequence numbers in REQUEST
267	 *     If S.state == REQUEST,
268	 *	  If (P.type == Response or P.type == Reset)
269	 *		and S.AWL <= P.ackno <= S.AWH,
270	 *	     / * Set sequence number variables corresponding to the
271	 *		other endpoint, so P will pass the tests in Step 6 * /
272	 *	     Set S.GSR, S.ISR, S.SWL, S.SWH
273	 *	     / * Response processing continues in Step 10; Reset
274	 *		processing continues in Step 9 * /
275	*/
276	if (dh->dccph_type == DCCP_PKT_RESPONSE) {
277		const struct inet_connection_sock *icsk = inet_csk(sk);
278		struct dccp_sock *dp = dccp_sk(sk);
279
280		/* Stop the REQUEST timer */
281		inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
282		BUG_TRAP(sk->sk_send_head != NULL);
283		__kfree_skb(sk->sk_send_head);
284		sk->sk_send_head = NULL;
285
286		if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
287			       dp->dccps_awl, dp->dccps_awh)) {
288			dccp_pr_debug("invalid ackno: S.AWL=%llu, "
289				      "P.ackno=%llu, S.AWH=%llu \n",
290				      (unsigned long long)dp->dccps_awl,
291			   (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
292				      (unsigned long long)dp->dccps_awh);
293			goto out_invalid_packet;
294		}
295
296		if (dccp_parse_options(sk, skb))
297			goto out_invalid_packet;
298
299		/* Obtain RTT sample from SYN exchange (used by CCID 3) */
300		if (dp->dccps_options_received.dccpor_timestamp_echo) {
301			struct timeval now;
302
303			dccp_timestamp(sk, &now);
304			dp->dccps_syn_rtt = dccp_sample_rtt(sk, &now, NULL);
305		}
306
307		if (dccp_msk(sk)->dccpms_send_ack_vector &&
308		    dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
309				    DCCP_SKB_CB(skb)->dccpd_seq,
310				    DCCP_ACKVEC_STATE_RECEIVED))
311			goto out_invalid_packet;
312
313		dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
314		dccp_update_gsr(sk, dp->dccps_isr);
315		/*
316		 * SWL and AWL are initially adjusted so that they are not less than
317		 * the initial Sequence Numbers received and sent, respectively:
318		 *	SWL := max(GSR + 1 - floor(W/4), ISR),
319		 *	AWL := max(GSS - W' + 1, ISS).
320		 * These adjustments MUST be applied only at the beginning of the
321		 * connection.
322		 *
323		 * AWL was adjusted in dccp_v4_connect -acme
324		 */
325		dccp_set_seqno(&dp->dccps_swl,
326			       max48(dp->dccps_swl, dp->dccps_isr));
327
328		dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
329
330		/*
331		 *    Step 10: Process REQUEST state (second part)
332		 *       If S.state == REQUEST,
333		 *	  / * If we get here, P is a valid Response from the
334		 *	      server (see Step 4), and we should move to
335		 *	      PARTOPEN state. PARTOPEN means send an Ack,
336		 *	      don't send Data packets, retransmit Acks
337		 *	      periodically, and always include any Init Cookie
338		 *	      from the Response * /
339		 *	  S.state := PARTOPEN
340		 *	  Set PARTOPEN timer
341		 *	  Continue with S.state == PARTOPEN
342		 *	  / * Step 12 will send the Ack completing the
343		 *	      three-way handshake * /
344		 */
345		dccp_set_state(sk, DCCP_PARTOPEN);
346
347		/* Make sure socket is routed, for correct metrics. */
348		icsk->icsk_af_ops->rebuild_header(sk);
349
350		if (!sock_flag(sk, SOCK_DEAD)) {
351			sk->sk_state_change(sk);
352			sk_wake_async(sk, 0, POLL_OUT);
353		}
354
355		if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
356		    icsk->icsk_accept_queue.rskq_defer_accept) {
357			/* Save one ACK. Data will be ready after
358			 * several ticks, if write_pending is set.
359			 *
360			 * It may be deleted, but with this feature tcpdumps
361			 * look so _wonderfully_ clever, that I was not able
362			 * to stand against the temptation 8)     --ANK
363			 */
364			/*
365			 * OK, in DCCP we can as well do a similar trick, its
366			 * even in the draft, but there is no need for us to
367			 * schedule an ack here, as dccp_sendmsg does this for
368			 * us, also stated in the draft. -acme
369			 */
370			__kfree_skb(skb);
371			return 0;
372		}
373		dccp_send_ack(sk);
374		return -1;
375	}
376
377out_invalid_packet:
378	/* dccp_v4_do_rcv will send a reset */
379	DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
380	return 1;
381}
382
383static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
384						   struct sk_buff *skb,
385						   const struct dccp_hdr *dh,
386						   const unsigned len)
387{
388	int queued = 0;
389
390	switch (dh->dccph_type) {
391	case DCCP_PKT_RESET:
392		inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
393		break;
394	case DCCP_PKT_DATA:
395		if (sk->sk_state == DCCP_RESPOND)
396			break;
397	case DCCP_PKT_DATAACK:
398	case DCCP_PKT_ACK:
399
400		/* Stop the PARTOPEN timer */
401		if (sk->sk_state == DCCP_PARTOPEN)
402			inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
403
404		dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
405		dccp_set_state(sk, DCCP_OPEN);
406
407		if (dh->dccph_type == DCCP_PKT_DATAACK ||
408		    dh->dccph_type == DCCP_PKT_DATA) {
409			__dccp_rcv_established(sk, skb, dh, len);
410			queued = 1; /* packet was queued
411				       (by __dccp_rcv_established) */
412		}
413		break;
414	}
415
416	return queued;
417}
418
419int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
420			   struct dccp_hdr *dh, unsigned len)
421{
422	struct dccp_sock *dp = dccp_sk(sk);
423	struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
424	const int old_state = sk->sk_state;
425	int queued = 0;
426
427	/*
428	 *  Step 3: Process LISTEN state
429	 *
430	 *     If S.state == LISTEN,
431	 *	 If P.type == Request or P contains a valid Init Cookie option,
432	 *	      (* Must scan the packet's options to check for Init
433	 *		 Cookies.  Only Init Cookies are processed here,
434	 *		 however; other options are processed in Step 8.  This
435	 *		 scan need only be performed if the endpoint uses Init
436	 *		 Cookies *)
437	 *	      (* Generate a new socket and switch to that socket *)
438	 *	      Set S := new socket for this port pair
439	 *	      S.state = RESPOND
440	 *	      Choose S.ISS (initial seqno) or set from Init Cookies
441	 *	      Initialize S.GAR := S.ISS
442	 *	      Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
443	 *	      Cookies Continue with S.state == RESPOND
444	 *	      (* A Response packet will be generated in Step 11 *)
445	 *	 Otherwise,
446	 *	      Generate Reset(No Connection) unless P.type == Reset
447	 *	      Drop packet and return
448	 */
449	if (sk->sk_state == DCCP_LISTEN) {
450		if (dh->dccph_type == DCCP_PKT_REQUEST) {
451			if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
452								    skb) < 0)
453				return 1;
454
455			goto discard;
456		}
457		if (dh->dccph_type == DCCP_PKT_RESET)
458			goto discard;
459
460		/* Caller (dccp_v4_do_rcv) will send Reset */
461		dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
462		return 1;
463	}
464
465	if (sk->sk_state != DCCP_REQUESTING) {
466		if (dccp_check_seqno(sk, skb))
467			goto discard;
468
469		/*
470		 * Step 8: Process options and mark acknowledgeable
471		 */
472		if (dccp_parse_options(sk, skb))
473			goto discard;
474
475		if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
476			dccp_event_ack_recv(sk, skb);
477
478		if (dccp_msk(sk)->dccpms_send_ack_vector &&
479		    dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
480				    DCCP_SKB_CB(skb)->dccpd_seq,
481				    DCCP_ACKVEC_STATE_RECEIVED))
482			goto discard;
483
484		ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
485		ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
486	}
487
488	/*
489	 *  Step 9: Process Reset
490	 *	If P.type == Reset,
491	 *		Tear down connection
492	 *		S.state := TIMEWAIT
493	 *		Set TIMEWAIT timer
494	 *		Drop packet and return
495	*/
496	if (dh->dccph_type == DCCP_PKT_RESET) {
497		/*
498		 * Queue the equivalent of TCP fin so that dccp_recvmsg
499		 * exits the loop
500		 */
501		dccp_fin(sk, skb);
502		dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
503		return 0;
504		/*
505		 *   Step 7: Check for unexpected packet types
506		 *      If (S.is_server and P.type == CloseReq)
507		 *	    or (S.is_server and P.type == Response)
508		 *	    or (S.is_client and P.type == Request)
509		 *	    or (S.state == RESPOND and P.type == Data),
510		 *	  Send Sync packet acknowledging P.seqno
511		 *	  Drop packet and return
512		 */
513	} else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
514		    (dh->dccph_type == DCCP_PKT_RESPONSE ||
515		     dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
516		    (dp->dccps_role == DCCP_ROLE_CLIENT &&
517		     dh->dccph_type == DCCP_PKT_REQUEST) ||
518		    (sk->sk_state == DCCP_RESPOND &&
519		     dh->dccph_type == DCCP_PKT_DATA)) {
520		dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
521		goto discard;
522	} else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
523		dccp_rcv_closereq(sk, skb);
524		goto discard;
525	} else if (dh->dccph_type == DCCP_PKT_CLOSE) {
526		dccp_rcv_close(sk, skb);
527		return 0;
528	}
529
530	if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
531		dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
532		goto discard;
533	}
534
535	switch (sk->sk_state) {
536	case DCCP_CLOSED:
537		dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
538		return 1;
539
540	case DCCP_REQUESTING:
541
542		queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
543		if (queued >= 0)
544			return queued;
545
546		__kfree_skb(skb);
547		return 0;
548
549	case DCCP_RESPOND:
550	case DCCP_PARTOPEN:
551		queued = dccp_rcv_respond_partopen_state_process(sk, skb,
552								 dh, len);
553		break;
554	}
555
556	if (dh->dccph_type == DCCP_PKT_ACK ||
557	    dh->dccph_type == DCCP_PKT_DATAACK) {
558		switch (old_state) {
559		case DCCP_PARTOPEN:
560			sk->sk_state_change(sk);
561			sk_wake_async(sk, 0, POLL_OUT);
562			break;
563		}
564	}
565
566	if (!queued) {
567discard:
568		__kfree_skb(skb);
569	}
570	return 0;
571}
572
573EXPORT_SYMBOL_GPL(dccp_rcv_state_process);
574
575/**
576 * dccp_sample_rtt  -  Sample RTT from packet exchange
577 *
578 * @sk:     connected dccp_sock
579 * @t_recv: receive timestamp of packet with timestamp echo
580 * @t_hist: packet history timestamp or NULL
581 */
582u32 dccp_sample_rtt(struct sock *sk, struct timeval *t_recv,
583				     struct timeval *t_hist)
584{
585	struct dccp_sock *dp = dccp_sk(sk);
586	struct dccp_options_received *or = &dp->dccps_options_received;
587	suseconds_t delta;
588
589	if (t_hist == NULL) {
590		if (!or->dccpor_timestamp_echo) {
591			DCCP_WARN("packet without timestamp echo\n");
592			return DCCP_SANE_RTT_MAX;
593		}
594		timeval_sub_usecs(t_recv, or->dccpor_timestamp_echo * 10);
595		delta = timeval_usecs(t_recv);
596	} else
597		delta = timeval_delta(t_recv, t_hist);
598
599	delta -= or->dccpor_elapsed_time * 10;		/* either set or 0 */
600
601	if (unlikely(delta <= 0)) {
602		DCCP_WARN("unusable RTT sample %ld, using min\n", (long)delta);
603		return DCCP_SANE_RTT_MIN;
604	}
605	if (unlikely(delta - (suseconds_t)DCCP_SANE_RTT_MAX > 0)) {
606		DCCP_WARN("RTT sample %ld too large, using max\n", (long)delta);
607		return DCCP_SANE_RTT_MAX;
608	}
609
610	return delta;
611}
612
613EXPORT_SYMBOL_GPL(dccp_sample_rtt);
614