[CCID3] Avoid unsigned integer overflows in usecs_div
[safe/jmp/linux-2.6] / net / dccp / ccids / ccid3.c
1 /*
2  *  net/dccp/ccids/ccid3.c
3  *
4  *  Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
5  *  Copyright (c) 2005 Ian McDonald <iam4@cs.waikato.ac.nz>
6  *
7  *  An implementation of the DCCP protocol
8  *
9  *  This code has been developed by the University of Waikato WAND
10  *  research group. For further information please see http://www.wand.net.nz/
11  *
12  *  This code also uses code from Lulea University, rereleased as GPL by its
13  *  authors:
14  *  Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
15  *
16  *  Changes to meet Linux coding standards, to make it meet latest ccid3 draft
17  *  and to make it work as a loadable module in the DCCP stack written by
18  *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
19  *
20  *  Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
21  *
22  *  This program is free software; you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License as published by
24  *  the Free Software Foundation; either version 2 of the License, or
25  *  (at your option) any later version.
26  *
27  *  This program is distributed in the hope that it will be useful,
28  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
29  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  *  GNU General Public License for more details.
31  *
32  *  You should have received a copy of the GNU General Public License
33  *  along with this program; if not, write to the Free Software
34  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35  */
36
37 #include <linux/config.h>
38 #include "../ccid.h"
39 #include "../dccp.h"
40 #include "lib/packet_history.h"
41 #include "lib/loss_interval.h"
42 #include "lib/tfrc.h"
43 #include "ccid3.h"
44
45 /*
46  * Reason for maths here is to avoid 32 bit overflow when a is big.
47  * With this we get close to the limit.
48  */
49 static inline u32 usecs_div(const u32 a, const u32 b)
50 {
51         const u32 div = a < (UINT_MAX / (USEC_PER_SEC /    10)) ?    10 :
52                         a < (UINT_MAX / (USEC_PER_SEC /    50)) ?    50 :
53                         a < (UINT_MAX / (USEC_PER_SEC /   100)) ?   100 :
54                         a < (UINT_MAX / (USEC_PER_SEC /   500)) ?   500 :
55                         a < (UINT_MAX / (USEC_PER_SEC /  1000)) ?  1000 :
56                         a < (UINT_MAX / (USEC_PER_SEC /  5000)) ?  5000 :
57                         a < (UINT_MAX / (USEC_PER_SEC / 10000)) ? 10000 :
58                         a < (UINT_MAX / (USEC_PER_SEC / 50000)) ? 50000 :
59                                                                  100000;
60         const u32 tmp = a * (USEC_PER_SEC / div);
61         return (b >= 2 * div) ? tmp / (b / div) : tmp;
62 }
63
64 static int ccid3_debug;
65
66 #ifdef CCID3_DEBUG
67 #define ccid3_pr_debug(format, a...) \
68         do { if (ccid3_debug) \
69                 printk(KERN_DEBUG "%s: " format, __FUNCTION__, ##a); \
70         } while (0)
71 #else
72 #define ccid3_pr_debug(format, a...)
73 #endif
74
75 static struct dccp_tx_hist *ccid3_tx_hist;
76 static struct dccp_rx_hist *ccid3_rx_hist;
77 static struct dccp_li_hist *ccid3_li_hist;
78
79 static int ccid3_init(struct sock *sk)
80 {
81         ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
82         return 0;
83 }
84
85 static void ccid3_exit(struct sock *sk)
86 {
87         ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
88 }
89
90 /* TFRC sender states */
91 enum ccid3_hc_tx_states {
92         TFRC_SSTATE_NO_SENT = 1,
93         TFRC_SSTATE_NO_FBACK,
94         TFRC_SSTATE_FBACK,
95         TFRC_SSTATE_TERM,
96 };
97
98 #ifdef CCID3_DEBUG
99 static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
100 {
101         static char *ccid3_state_names[] = {
102         [TFRC_SSTATE_NO_SENT]  = "NO_SENT",
103         [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
104         [TFRC_SSTATE_FBACK]    = "FBACK",
105         [TFRC_SSTATE_TERM]     = "TERM",
106         };
107
108         return ccid3_state_names[state];
109 }
110 #endif
111
112 static inline void ccid3_hc_tx_set_state(struct sock *sk,
113                                          enum ccid3_hc_tx_states state)
114 {
115         struct dccp_sock *dp = dccp_sk(sk);
116         struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
117         enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
118
119         ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
120                        dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
121                        ccid3_tx_state_name(state));
122         WARN_ON(state == oldstate);
123         hctx->ccid3hctx_state = state;
124 }
125
126 /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */
127 static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx)
128 {
129         /*
130          * If no feedback spec says t_ipi is 1 second (set elsewhere and then
131          * doubles after every no feedback timer (separate function)
132          */
133         if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
134                 hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
135                                                   hctx->ccid3hctx_x);
136 }
137
138 /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
139 static inline void ccid3_calc_new_delta(struct ccid3_hc_tx_sock *hctx)
140 {
141         hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
142                                            TFRC_OPSYS_HALF_TIME_GRAN);
143 }
144
145 /*
146  * Update X by
147  *    If (p > 0)
148  *       x_calc = calcX(s, R, p);
149  *       X = max(min(X_calc, 2 * X_recv), s / t_mbi);
150  *    Else
151  *       If (now - tld >= R)
152  *          X = max(min(2 * X, 2 * X_recv), s / R);
153  *          tld = now;
154  */ 
155 static void ccid3_hc_tx_update_x(struct sock *sk)
156 {
157         struct dccp_sock *dp = dccp_sk(sk);
158         struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
159
160         /* To avoid large error in calcX */
161         if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) {
162                 hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
163                                                      hctx->ccid3hctx_rtt,
164                                                      hctx->ccid3hctx_p);
165                 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_calc,
166                                                           2 * hctx->ccid3hctx_x_recv),
167                                                (hctx->ccid3hctx_s /
168                                                 TFRC_MAX_BACK_OFF_TIME));
169         } else {
170                 struct timeval now;
171
172                 do_gettimeofday(&now);
173                 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >=
174                     hctx->ccid3hctx_rtt) {
175                         hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_recv,
176                                                                   hctx->ccid3hctx_x) * 2,
177                                                        usecs_div(hctx->ccid3hctx_s,
178                                                                  hctx->ccid3hctx_rtt));
179                         hctx->ccid3hctx_t_ld = now;
180                 }
181         }
182 }
183
184 static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
185 {
186         struct sock *sk = (struct sock *)data;
187         struct dccp_sock *dp = dccp_sk(sk);
188         unsigned long next_tmout = 0;
189         struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
190
191         bh_lock_sock(sk);
192         if (sock_owned_by_user(sk)) {
193                 /* Try again later. */
194                 /* XXX: set some sensible MIB */
195                 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
196                                jiffies + HZ / 5);
197                 goto out;
198         }
199
200         ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
201                        ccid3_tx_state_name(hctx->ccid3hctx_state));
202         
203         switch (hctx->ccid3hctx_state) {
204         case TFRC_SSTATE_TERM:
205                 goto out;
206         case TFRC_SSTATE_NO_FBACK:
207                 /* Halve send rate */
208                 hctx->ccid3hctx_x /= 2;
209                 if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s /
210                                          TFRC_MAX_BACK_OFF_TIME))
211                         hctx->ccid3hctx_x = (hctx->ccid3hctx_s /
212                                              TFRC_MAX_BACK_OFF_TIME);
213
214                 ccid3_pr_debug("%s, sk=%p, state=%s, updated tx rate to %d "
215                                "bytes/s\n",
216                                dccp_role(sk), sk,
217                                ccid3_tx_state_name(hctx->ccid3hctx_state),
218                                hctx->ccid3hctx_x);
219                 next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
220                                                       hctx->ccid3hctx_x),
221                                         TFRC_INITIAL_TIMEOUT);
222                 /*
223                  * FIXME - not sure above calculation is correct. See section
224                  * 5 of CCID3 11 should adjust tx_t_ipi and double that to
225                  * achieve it really
226                  */
227                 break;
228         case TFRC_SSTATE_FBACK:
229                 /*
230                  * Check if IDLE since last timeout and recv rate is less than
231                  * 4 packets per RTT
232                  */
233                 if (!hctx->ccid3hctx_idle ||
234                     (hctx->ccid3hctx_x_recv >=
235                      4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) {
236                         ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n",
237                                        dccp_role(sk), sk,
238                                        ccid3_tx_state_name(hctx->ccid3hctx_state));
239                         /* Halve sending rate */
240
241                         /*  If (X_calc > 2 * X_recv)
242                          *    X_recv = max(X_recv / 2, s / (2 * t_mbi));
243                          *  Else
244                          *    X_recv = X_calc / 4;
245                          */
246                         BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
247                                hctx->ccid3hctx_x_calc == 0);
248
249                         /* check also if p is zero -> x_calc is infinity? */
250                         if (hctx->ccid3hctx_p < TFRC_SMALLEST_P ||
251                             hctx->ccid3hctx_x_calc > 2 * hctx->ccid3hctx_x_recv)
252                                 hctx->ccid3hctx_x_recv = max_t(u32, hctx->ccid3hctx_x_recv / 2,
253                                                                     hctx->ccid3hctx_s / (2 * TFRC_MAX_BACK_OFF_TIME));
254                         else
255                                 hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc / 4;
256
257                         /* Update sending rate */
258                         ccid3_hc_tx_update_x(sk);
259                 }
260                 /*
261                  * Schedule no feedback timer to expire in
262                  * max(4 * R, 2 * s / X)
263                  */
264                 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto, 
265                                         2 * usecs_div(hctx->ccid3hctx_s,
266                                                       hctx->ccid3hctx_x));
267                 break;
268         default:
269                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
270                        __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
271                 dump_stack();
272                 goto out;
273         }
274
275         sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer, 
276                       jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
277         hctx->ccid3hctx_idle = 1;
278 out:
279         bh_unlock_sock(sk);
280         sock_put(sk);
281 }
282
283 static int ccid3_hc_tx_send_packet(struct sock *sk,
284                                    struct sk_buff *skb, int len)
285 {
286         struct dccp_sock *dp = dccp_sk(sk);
287         struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
288         struct dccp_tx_hist_entry *new_packet;
289         struct timeval now;
290         long delay;
291         int rc = -ENOTCONN;
292
293         /* Check if pure ACK or Terminating*/
294
295         /*
296          * XXX: We only call this function for DATA and DATAACK, on, these
297          * packets can have zero length, but why the comment about "pure ACK"?
298          */
299         if (hctx == NULL || len == 0 ||
300             hctx->ccid3hctx_state == TFRC_SSTATE_TERM)
301                 goto out;
302
303         /* See if last packet allocated was not sent */
304         new_packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
305         if (new_packet == NULL || new_packet->dccphtx_sent) {
306                 new_packet = dccp_tx_hist_entry_new(ccid3_tx_hist,
307                                                     SLAB_ATOMIC);
308
309                 rc = -ENOBUFS;
310                 if (new_packet == NULL) {
311                         ccid3_pr_debug("%s, sk=%p, not enough mem to add "
312                                        "to history, send refused\n",
313                                        dccp_role(sk), sk);
314                         goto out;
315                 }
316
317                 dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
318         }
319
320         do_gettimeofday(&now);
321
322         switch (hctx->ccid3hctx_state) {
323         case TFRC_SSTATE_NO_SENT:
324                 ccid3_pr_debug("%s, sk=%p, first packet(%llu)\n",
325                                dccp_role(sk), sk, dp->dccps_gss);
326
327                 hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
328                 hctx->ccid3hctx_no_feedback_timer.data     = (unsigned long)sk;
329                 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
330                                jiffies + usecs_to_jiffies(TFRC_INITIAL_TIMEOUT));
331                 hctx->ccid3hctx_last_win_count   = 0;
332                 hctx->ccid3hctx_t_last_win_count = now;
333                 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
334                 hctx->ccid3hctx_t_ipi = TFRC_INITIAL_TIMEOUT;
335
336                 /* Set nominal send time for initial packet */
337                 hctx->ccid3hctx_t_nom = now;
338                 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
339                                   hctx->ccid3hctx_t_ipi);
340                 ccid3_calc_new_delta(hctx);
341                 rc = 0;
342                 break;
343         case TFRC_SSTATE_NO_FBACK:
344         case TFRC_SSTATE_FBACK:
345                 delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
346                          hctx->ccid3hctx_delta);
347                 ccid3_pr_debug("send_packet delay=%ld\n", delay);
348                 delay /= -1000;
349                 /* divide by -1000 is to convert to ms and get sign right */
350                 rc = delay > 0 ? delay : 0;
351                 break;
352         default:
353                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
354                        __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
355                 dump_stack();
356                 rc = -EINVAL;
357                 break;
358         }
359
360         /* Can we send? if so add options and add to packet history */
361         if (rc == 0)
362                 new_packet->dccphtx_ccval =
363                         DCCP_SKB_CB(skb)->dccpd_ccval =
364                                 hctx->ccid3hctx_last_win_count;
365 out:
366         return rc;
367 }
368
369 static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
370 {
371         struct dccp_sock *dp = dccp_sk(sk);
372         struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
373         struct timeval now;
374
375         BUG_ON(hctx == NULL);
376
377         if (hctx->ccid3hctx_state == TFRC_SSTATE_TERM) {
378                 ccid3_pr_debug("%s, sk=%p, while state is TFRC_SSTATE_TERM!\n",
379                                dccp_role(sk), sk);
380                 return;
381         }
382
383         do_gettimeofday(&now);
384
385         /* check if we have sent a data packet */
386         if (len > 0) {
387                 unsigned long quarter_rtt;
388                 struct dccp_tx_hist_entry *packet;
389
390                 packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
391                 if (packet == NULL) {
392                         printk(KERN_CRIT "%s: packet doesn't exists in "
393                                          "history!\n", __FUNCTION__);
394                         return;
395                 }
396                 if (packet->dccphtx_sent) {
397                         printk(KERN_CRIT "%s: no unsent packet in history!\n",
398                                __FUNCTION__);
399                         return;
400                 }
401                 packet->dccphtx_tstamp = now;
402                 packet->dccphtx_seqno  = dp->dccps_gss;
403                 /*
404                  * Check if win_count have changed
405                  * Algorithm in "8.1. Window Counter Valuer" in
406                  * draft-ietf-dccp-ccid3-11.txt
407                  */
408                 quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count);
409                 if (likely(hctx->ccid3hctx_rtt > 8))
410                         quarter_rtt /= hctx->ccid3hctx_rtt / 4;
411
412                 if (quarter_rtt > 0) {
413                         hctx->ccid3hctx_t_last_win_count = now;
414                         hctx->ccid3hctx_last_win_count   = (hctx->ccid3hctx_last_win_count +
415                                                             min_t(unsigned long, quarter_rtt, 5)) % 16;
416                         ccid3_pr_debug("%s, sk=%p, window changed from "
417                                        "%u to %u!\n",
418                                        dccp_role(sk), sk,
419                                        packet->dccphtx_ccval,
420                                        hctx->ccid3hctx_last_win_count);
421                 }
422
423                 hctx->ccid3hctx_idle = 0;
424                 packet->dccphtx_rtt  = hctx->ccid3hctx_rtt;
425                 packet->dccphtx_sent = 1;
426         } else
427                 ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
428                                dccp_role(sk), sk, dp->dccps_gss);
429
430         switch (hctx->ccid3hctx_state) {
431         case TFRC_SSTATE_NO_SENT:
432                 /* if first wasn't pure ack */
433                 if (len != 0)
434                         printk(KERN_CRIT "%s: %s, First packet sent is noted "
435                                          "as a data packet\n",
436                                __FUNCTION__, dccp_role(sk));
437                 return;
438         case TFRC_SSTATE_NO_FBACK:
439         case TFRC_SSTATE_FBACK:
440                 if (len > 0) {
441                         hctx->ccid3hctx_t_nom = now;
442                         ccid3_calc_new_t_ipi(hctx);
443                         ccid3_calc_new_delta(hctx);
444                         timeval_add_usecs(&hctx->ccid3hctx_t_nom,
445                                           hctx->ccid3hctx_t_ipi);
446                 }
447                 break;
448         default:
449                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
450                        __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
451                 dump_stack();
452                 break;
453         }
454 }
455
456 static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
457 {
458         struct dccp_sock *dp = dccp_sk(sk);
459         struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
460         struct ccid3_options_received *opt_recv;
461         struct dccp_tx_hist_entry *packet;
462         unsigned long next_tmout; 
463         u32 t_elapsed;
464         u32 pinv;
465         u32 x_recv;
466         u32 r_sample;
467
468         if (hctx == NULL)
469                 return;
470
471         if (hctx->ccid3hctx_state == TFRC_SSTATE_TERM) {
472                 ccid3_pr_debug("%s, sk=%p, received a packet when "
473                                "terminating!\n", dccp_role(sk), sk);
474                 return;
475         }
476
477         /* we are only interested in ACKs */
478         if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
479               DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
480                 return;
481
482         opt_recv = &hctx->ccid3hctx_options_received;
483
484         t_elapsed = dp->dccps_options_received.dccpor_elapsed_time;
485         x_recv = opt_recv->ccid3or_receive_rate;
486         pinv = opt_recv->ccid3or_loss_event_rate;
487
488         switch (hctx->ccid3hctx_state) {
489         case TFRC_SSTATE_NO_SENT:
490                 /* FIXME: what to do here? */
491                 return;
492         case TFRC_SSTATE_NO_FBACK:
493         case TFRC_SSTATE_FBACK:
494                 /* Calculate new round trip sample by
495                  * R_sample = (now - t_recvdata) - t_delay */
496                 /* get t_recvdata from history */
497                 packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
498                                                  DCCP_SKB_CB(skb)->dccpd_ack_seq);
499                 if (packet == NULL) {
500                         ccid3_pr_debug("%s, sk=%p, seqno %llu(%s) does't "
501                                        "exist in history!\n",
502                                        dccp_role(sk), sk,
503                                        DCCP_SKB_CB(skb)->dccpd_ack_seq,
504                                        dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
505                         return;
506                 }
507
508                 /* Update RTT */
509                 r_sample = timeval_now_delta(&packet->dccphtx_tstamp);
510                 /* FIXME: */
511                 // r_sample -= usecs_to_jiffies(t_elapsed * 10);
512
513                 /* Update RTT estimate by 
514                  * If (No feedback recv)
515                  *    R = R_sample;
516                  * Else
517                  *    R = q * R + (1 - q) * R_sample;
518                  *
519                  * q is a constant, RFC 3448 recomments 0.9
520                  */
521                 if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
522                         ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
523                         hctx->ccid3hctx_rtt = r_sample;
524                 } else
525                         hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
526                                               r_sample / 10;
527
528                 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, "
529                                "r_sample=%us\n", dccp_role(sk), sk,
530                                hctx->ccid3hctx_rtt, r_sample);
531
532                 /* Update timeout interval */
533                 hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
534                                               USEC_PER_SEC);
535
536                 /* Update receive rate */
537                 hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
538
539                 /* Update loss event rate */
540                 if (pinv == ~0 || pinv == 0)
541                         hctx->ccid3hctx_p = 0;
542                 else {
543                         hctx->ccid3hctx_p = 1000000 / pinv;
544
545                         if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) {
546                                 hctx->ccid3hctx_p = TFRC_SMALLEST_P;
547                                 ccid3_pr_debug("%s, sk=%p, Smallest p used!\n",
548                                                dccp_role(sk), sk);
549                         }
550                 }
551
552                 /* unschedule no feedback timer */
553                 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
554
555                 /* Update sending rate */
556                 ccid3_hc_tx_update_x(sk);
557
558                 /* Update next send time */
559                 timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
560                                   hctx->ccid3hctx_t_ipi);
561                 ccid3_calc_new_t_ipi(hctx);
562                 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
563                                   hctx->ccid3hctx_t_ipi);
564                 ccid3_calc_new_delta(hctx);
565
566                 /* remove all packets older than the one acked from history */
567                 dccp_tx_hist_purge_older(ccid3_tx_hist,
568                                          &hctx->ccid3hctx_hist, packet);
569                 /*
570                  * As we have calculated new ipi, delta, t_nom it is possible that
571                  * we now can send a packet, so wake up dccp_wait_for_ccids.
572                  */
573                 sk->sk_write_space(sk);
574
575                 /*
576                  * Schedule no feedback timer to expire in
577                  * max(4 * R, 2 * s / X)
578                  */
579                 next_tmout = max(hctx->ccid3hctx_t_rto,
580                                  2 * usecs_div(hctx->ccid3hctx_s,
581                                                hctx->ccid3hctx_x));
582                         
583                 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
584                                "expire in %lu jiffies (%luus)\n",
585                                dccp_role(sk), sk,
586                                usecs_to_jiffies(next_tmout), next_tmout); 
587
588                 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer, 
589                                jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
590
591                 /* set idle flag */
592                 hctx->ccid3hctx_idle = 1;   
593                 break;
594         default:
595                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
596                        __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
597                 dump_stack();
598                 break;
599         }
600 }
601
602 static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
603 {
604         const struct dccp_sock *dp = dccp_sk(sk);
605         struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
606
607         if (hctx == NULL || !(sk->sk_state == DCCP_OPEN ||
608                               sk->sk_state == DCCP_PARTOPEN))
609                 return;
610
611          DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
612 }
613
614 static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
615                                      unsigned char len, u16 idx,
616                                      unsigned char *value)
617 {
618         int rc = 0;
619         struct dccp_sock *dp = dccp_sk(sk);
620         struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
621         struct ccid3_options_received *opt_recv;
622
623         if (hctx == NULL)
624                 return 0;
625
626         opt_recv = &hctx->ccid3hctx_options_received;
627
628         if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
629                 opt_recv->ccid3or_seqno              = dp->dccps_gsr;
630                 opt_recv->ccid3or_loss_event_rate    = ~0;
631                 opt_recv->ccid3or_loss_intervals_idx = 0;
632                 opt_recv->ccid3or_loss_intervals_len = 0;
633                 opt_recv->ccid3or_receive_rate       = 0;
634         }
635
636         switch (option) {
637         case TFRC_OPT_LOSS_EVENT_RATE:
638                 if (len != 4) {
639                         ccid3_pr_debug("%s, sk=%p, invalid len for "
640                                        "TFRC_OPT_LOSS_EVENT_RATE\n",
641                                        dccp_role(sk), sk);
642                         rc = -EINVAL;
643                 } else {
644                         opt_recv->ccid3or_loss_event_rate = ntohl(*(u32 *)value);
645                         ccid3_pr_debug("%s, sk=%p, LOSS_EVENT_RATE=%u\n",
646                                        dccp_role(sk), sk,
647                                        opt_recv->ccid3or_loss_event_rate);
648                 }
649                 break;
650         case TFRC_OPT_LOSS_INTERVALS:
651                 opt_recv->ccid3or_loss_intervals_idx = idx;
652                 opt_recv->ccid3or_loss_intervals_len = len;
653                 ccid3_pr_debug("%s, sk=%p, LOSS_INTERVALS=(%u, %u)\n",
654                                dccp_role(sk), sk,
655                                opt_recv->ccid3or_loss_intervals_idx,
656                                opt_recv->ccid3or_loss_intervals_len);
657                 break;
658         case TFRC_OPT_RECEIVE_RATE:
659                 if (len != 4) {
660                         ccid3_pr_debug("%s, sk=%p, invalid len for "
661                                        "TFRC_OPT_RECEIVE_RATE\n",
662                                        dccp_role(sk), sk);
663                         rc = -EINVAL;
664                 } else {
665                         opt_recv->ccid3or_receive_rate = ntohl(*(u32 *)value);
666                         ccid3_pr_debug("%s, sk=%p, RECEIVE_RATE=%u\n",
667                                        dccp_role(sk), sk,
668                                        opt_recv->ccid3or_receive_rate);
669                 }
670                 break;
671         }
672
673         return rc;
674 }
675
676 static int ccid3_hc_tx_init(struct sock *sk)
677 {
678         struct dccp_sock *dp = dccp_sk(sk);
679         struct ccid3_hc_tx_sock *hctx;
680
681         ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
682
683         hctx = dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx),
684                                                       gfp_any());
685         if (hctx == NULL)
686                 return -ENOMEM;
687
688         memset(hctx, 0, sizeof(*hctx));
689
690         if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
691             dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
692                 hctx->ccid3hctx_s = dp->dccps_packet_size;
693         else
694                 hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
695
696         /* Set transmission rate to 1 packet per second */
697         hctx->ccid3hctx_x     = hctx->ccid3hctx_s;
698         hctx->ccid3hctx_t_rto = USEC_PER_SEC;
699         hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
700         INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
701         init_timer(&hctx->ccid3hctx_no_feedback_timer);
702
703         return 0;
704 }
705
706 static void ccid3_hc_tx_exit(struct sock *sk)
707 {
708         struct dccp_sock *dp = dccp_sk(sk);
709         struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
710
711         ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
712         BUG_ON(hctx == NULL);
713
714         ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
715         sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
716
717         /* Empty packet history */
718         dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
719
720         kfree(dp->dccps_hc_tx_ccid_private);
721         dp->dccps_hc_tx_ccid_private = NULL;
722 }
723
724 /*
725  * RX Half Connection methods
726  */
727
728 /* TFRC receiver states */
729 enum ccid3_hc_rx_states {
730         TFRC_RSTATE_NO_DATA = 1,
731         TFRC_RSTATE_DATA,
732         TFRC_RSTATE_TERM    = 127,
733 };
734
735 #ifdef CCID3_DEBUG
736 static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
737 {
738         static char *ccid3_rx_state_names[] = {
739         [TFRC_RSTATE_NO_DATA] = "NO_DATA",
740         [TFRC_RSTATE_DATA]    = "DATA",
741         [TFRC_RSTATE_TERM]    = "TERM",
742         };
743
744         return ccid3_rx_state_names[state];
745 }
746 #endif
747
748 static inline void ccid3_hc_rx_set_state(struct sock *sk,
749                                          enum ccid3_hc_rx_states state)
750 {
751         struct dccp_sock *dp = dccp_sk(sk);
752         struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
753         enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
754
755         ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
756                        dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
757                        ccid3_rx_state_name(state));
758         WARN_ON(state == oldstate);
759         hcrx->ccid3hcrx_state = state;
760 }
761
762 static void ccid3_hc_rx_send_feedback(struct sock *sk)
763 {
764         struct dccp_sock *dp = dccp_sk(sk);
765         struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
766         struct dccp_rx_hist_entry *packet;
767         struct timeval now;
768
769         ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
770
771         do_gettimeofday(&now);
772
773         switch (hcrx->ccid3hcrx_state) {
774         case TFRC_RSTATE_NO_DATA:
775                 hcrx->ccid3hcrx_x_recv = 0;
776                 break;
777         case TFRC_RSTATE_DATA: {
778                 const u32 delta = timeval_delta(&now,
779                                         &hcrx->ccid3hcrx_tstamp_last_feedback);
780
781                 hcrx->ccid3hcrx_x_recv = (hcrx->ccid3hcrx_bytes_recv *
782                                           USEC_PER_SEC);
783                 if (likely(delta > 1))
784                         hcrx->ccid3hcrx_x_recv /= delta;
785         }
786                 break;
787         default:
788                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
789                        __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
790                 dump_stack();
791                 return;
792         }
793
794         packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
795         if (packet == NULL) {
796                 printk(KERN_CRIT "%s: %s, sk=%p, no data packet in history!\n",
797                        __FUNCTION__, dccp_role(sk), sk);
798                 dump_stack();
799                 return;
800         }
801
802         hcrx->ccid3hcrx_tstamp_last_feedback = now;
803         hcrx->ccid3hcrx_last_counter         = packet->dccphrx_ccval;
804         hcrx->ccid3hcrx_seqno_last_counter   = packet->dccphrx_seqno;
805         hcrx->ccid3hcrx_bytes_recv           = 0;
806
807         /* Convert to multiples of 10us */
808         hcrx->ccid3hcrx_elapsed_time =
809                         timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
810         if (hcrx->ccid3hcrx_p == 0)
811                 hcrx->ccid3hcrx_pinv = ~0;
812         else
813                 hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
814         dccp_send_ack(sk);
815 }
816
817 static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
818 {
819         const struct dccp_sock *dp = dccp_sk(sk);
820         u32 x_recv, pinv;
821         struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
822
823         if (hcrx == NULL || !(sk->sk_state == DCCP_OPEN ||
824                               sk->sk_state == DCCP_PARTOPEN))
825                 return;
826
827         DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
828
829         if (dccp_packet_without_ack(skb))
830                 return;
831                 
832         if (hcrx->ccid3hcrx_elapsed_time != 0)
833                 dccp_insert_option_elapsed_time(sk, skb,
834                                                 hcrx->ccid3hcrx_elapsed_time);
835         dccp_insert_option_timestamp(sk, skb);
836         x_recv = htonl(hcrx->ccid3hcrx_x_recv);
837         pinv   = htonl(hcrx->ccid3hcrx_pinv);
838         dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
839                            &pinv, sizeof(pinv));
840         dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
841                            &x_recv, sizeof(x_recv));
842 }
843
844 /* calculate first loss interval
845  *
846  * returns estimated loss interval in usecs */
847
848 static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
849 {
850         struct dccp_sock *dp = dccp_sk(sk);
851         struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
852         struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
853         u32 rtt, delta, x_recv, fval, p, tmp2;
854         struct timeval tstamp = { 0, };
855         int interval = 0;
856         int win_count = 0;
857         int step = 0;
858         u64 tmp1;
859
860         list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
861                                  dccphrx_node) {
862                 if (dccp_rx_hist_entry_data_packet(entry)) {
863                         tail = entry;
864
865                         switch (step) {
866                         case 0:
867                                 tstamp    = entry->dccphrx_tstamp;
868                                 win_count = entry->dccphrx_ccval;
869                                 step = 1;
870                                 break;
871                         case 1:
872                                 interval = win_count - entry->dccphrx_ccval;
873                                 if (interval < 0)
874                                         interval += TFRC_WIN_COUNT_LIMIT;
875                                 if (interval > 4)
876                                         goto found;
877                                 break;
878                         }
879                 }
880         }
881
882         if (step == 0) {
883                 printk(KERN_CRIT "%s: %s, sk=%p, packet history contains no "
884                                  "data packets!\n",
885                        __FUNCTION__, dccp_role(sk), sk);
886                 return ~0;
887         }
888
889         if (interval == 0) {
890                 ccid3_pr_debug("%s, sk=%p, Could not find a win_count "
891                                "interval > 0. Defaulting to 1\n",
892                                dccp_role(sk), sk);
893                 interval = 1;
894         }
895 found:
896         rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
897         ccid3_pr_debug("%s, sk=%p, approximated RTT to %uus\n",
898                        dccp_role(sk), sk, rtt);
899         if (rtt == 0)
900                 rtt = 1;
901
902         delta = timeval_now_delta(&hcrx->ccid3hcrx_tstamp_last_feedback);
903         x_recv = hcrx->ccid3hcrx_bytes_recv * USEC_PER_SEC;
904         if (likely(delta > 1))
905                 x_recv /= delta;
906
907         tmp1 = (u64)x_recv * (u64)rtt;
908         do_div(tmp1,10000000);
909         tmp2 = (u32)tmp1;
910         fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
911         /* do not alter order above or you will get overflow on 32 bit */
912         p = tfrc_calc_x_reverse_lookup(fval);
913         ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
914                        "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
915
916         if (p == 0)
917                 return ~0;
918         else
919                 return 1000000 / p; 
920 }
921
922 static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
923 {
924         struct dccp_sock *dp = dccp_sk(sk);
925         struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
926
927         if (seq_loss != DCCP_MAX_SEQNO + 1 &&
928             list_empty(&hcrx->ccid3hcrx_li_hist)) {
929                 struct dccp_li_hist_entry *li_tail;
930
931                 li_tail = dccp_li_hist_interval_new(ccid3_li_hist,
932                                                     &hcrx->ccid3hcrx_li_hist,
933                                                     seq_loss, win_loss);
934                 if (li_tail == NULL)
935                         return;
936                 li_tail->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
937         }
938         /* FIXME: find end of interval */
939 }
940
941 static void ccid3_hc_rx_detect_loss(struct sock *sk)
942 {
943         struct dccp_sock *dp = dccp_sk(sk);
944         struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
945         u8 win_loss;
946         const u64 seq_loss = dccp_rx_hist_detect_loss(&hcrx->ccid3hcrx_hist,
947                                                       &hcrx->ccid3hcrx_li_hist,
948                                                       &win_loss);
949
950         ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
951 }
952
953 static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
954 {
955         struct dccp_sock *dp = dccp_sk(sk);
956         struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
957         const struct dccp_options_received *opt_recv;
958         struct dccp_rx_hist_entry *packet;
959         struct timeval now;
960         u8 win_count;
961         u32 p_prev;
962         int ins;
963
964         if (hcrx == NULL)
965                 return;
966
967         BUG_ON(!(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
968                  hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
969
970         opt_recv = &dp->dccps_options_received;
971
972         switch (DCCP_SKB_CB(skb)->dccpd_type) {
973         case DCCP_PKT_ACK:
974                 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
975                         return;
976         case DCCP_PKT_DATAACK:
977                 if (opt_recv->dccpor_timestamp_echo == 0)
978                         break;
979                 p_prev = hcrx->ccid3hcrx_rtt;
980                 do_gettimeofday(&now);
981                 hcrx->ccid3hcrx_rtt = timeval_usecs(&now) -
982                                      (opt_recv->dccpor_timestamp_echo -
983                                       opt_recv->dccpor_elapsed_time) * 10;
984                 if (p_prev != hcrx->ccid3hcrx_rtt)
985                         ccid3_pr_debug("%s, New RTT=%luus, elapsed time=%u\n",
986                                        dccp_role(sk), hcrx->ccid3hcrx_rtt,
987                                        opt_recv->dccpor_elapsed_time);
988                 break;
989         case DCCP_PKT_DATA:
990                 break;
991         default:
992                 ccid3_pr_debug("%s, sk=%p, not DATA/DATAACK/ACK packet(%s)\n",
993                                dccp_role(sk), sk,
994                                dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
995                 return;
996         }
997
998         packet = dccp_rx_hist_entry_new(ccid3_rx_hist, opt_recv->dccpor_ndp,
999                                         skb, SLAB_ATOMIC);
1000         if (packet == NULL) {
1001                 ccid3_pr_debug("%s, sk=%p, Not enough mem to add rx packet "
1002                                "to history (consider it lost)!",
1003                                dccp_role(sk), sk);
1004                 return;
1005         }
1006
1007         win_count = packet->dccphrx_ccval;
1008
1009         ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
1010                                       &hcrx->ccid3hcrx_li_hist, packet);
1011
1012         if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
1013                 return;
1014
1015         switch (hcrx->ccid3hcrx_state) {
1016         case TFRC_RSTATE_NO_DATA:
1017                 ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
1018                                "feedback\n",
1019                                dccp_role(sk), sk,
1020                                dccp_state_name(sk->sk_state), skb);
1021                 ccid3_hc_rx_send_feedback(sk);
1022                 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
1023                 return;
1024         case TFRC_RSTATE_DATA:
1025                 hcrx->ccid3hcrx_bytes_recv += skb->len -
1026                                               dccp_hdr(skb)->dccph_doff * 4;
1027                 if (ins != 0)
1028                         break;
1029
1030                 do_gettimeofday(&now);
1031                 if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
1032                     hcrx->ccid3hcrx_rtt) {
1033                         hcrx->ccid3hcrx_tstamp_last_ack = now;
1034                         ccid3_hc_rx_send_feedback(sk);
1035                 }
1036                 return;
1037         default:
1038                 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
1039                        __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
1040                 dump_stack();
1041                 return;
1042         }
1043
1044         /* Dealing with packet loss */
1045         ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
1046                        dccp_role(sk), sk, dccp_state_name(sk->sk_state));
1047
1048         ccid3_hc_rx_detect_loss(sk);
1049         p_prev = hcrx->ccid3hcrx_p;
1050         
1051         /* Calculate loss event rate */
1052         if (!list_empty(&hcrx->ccid3hcrx_li_hist))
1053                 /* Scaling up by 1000000 as fixed decimal */
1054                 hcrx->ccid3hcrx_p = 1000000 / dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
1055
1056         if (hcrx->ccid3hcrx_p > p_prev) {
1057                 ccid3_hc_rx_send_feedback(sk);
1058                 return;
1059         }
1060 }
1061
1062 static int ccid3_hc_rx_init(struct sock *sk)
1063 {
1064         struct dccp_sock *dp = dccp_sk(sk);
1065         struct ccid3_hc_rx_sock *hcrx;
1066
1067         ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1068
1069         hcrx = dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx),
1070                                                       gfp_any());
1071         if (hcrx == NULL)
1072                 return -ENOMEM;
1073
1074         memset(hcrx, 0, sizeof(*hcrx));
1075
1076         if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
1077             dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
1078                 hcrx->ccid3hcrx_s = dp->dccps_packet_size;
1079         else
1080                 hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
1081
1082         hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
1083         INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
1084         INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
1085         /*
1086          * XXX this seems to be paranoid, need to think more about this, for
1087          * now start with something different than zero. -acme
1088          */
1089         hcrx->ccid3hcrx_rtt = USEC_PER_SEC / 5;
1090         return 0;
1091 }
1092
1093 static void ccid3_hc_rx_exit(struct sock *sk)
1094 {
1095         struct dccp_sock *dp = dccp_sk(sk);
1096         struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
1097
1098         ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1099
1100         if (hcrx == NULL)
1101                 return;
1102
1103         ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
1104
1105         /* Empty packet history */
1106         dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
1107
1108         /* Empty loss interval history */
1109         dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
1110
1111         kfree(dp->dccps_hc_rx_ccid_private);
1112         dp->dccps_hc_rx_ccid_private = NULL;
1113 }
1114
1115 static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
1116 {
1117         const struct dccp_sock *dp = dccp_sk(sk);
1118         const struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
1119
1120         if (hcrx == NULL)
1121                 return;
1122
1123         info->tcpi_ca_state     = hcrx->ccid3hcrx_state;
1124         info->tcpi_options      |= TCPI_OPT_TIMESTAMPS;
1125         info->tcpi_rcv_rtt      = hcrx->ccid3hcrx_rtt;
1126 }
1127
1128 static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
1129 {
1130         const struct dccp_sock *dp = dccp_sk(sk);
1131         const struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
1132
1133         if (hctx == NULL)
1134                 return;
1135
1136         info->tcpi_rto = hctx->ccid3hctx_t_rto;
1137         info->tcpi_rtt = hctx->ccid3hctx_rtt;
1138 }
1139
1140 static struct ccid ccid3 = {
1141         .ccid_id                   = 3,
1142         .ccid_name                 = "ccid3",
1143         .ccid_owner                = THIS_MODULE,
1144         .ccid_init                 = ccid3_init,
1145         .ccid_exit                 = ccid3_exit,
1146         .ccid_hc_tx_init           = ccid3_hc_tx_init,
1147         .ccid_hc_tx_exit           = ccid3_hc_tx_exit,
1148         .ccid_hc_tx_send_packet    = ccid3_hc_tx_send_packet,
1149         .ccid_hc_tx_packet_sent    = ccid3_hc_tx_packet_sent,
1150         .ccid_hc_tx_packet_recv    = ccid3_hc_tx_packet_recv,
1151         .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
1152         .ccid_hc_tx_parse_options  = ccid3_hc_tx_parse_options,
1153         .ccid_hc_rx_init           = ccid3_hc_rx_init,
1154         .ccid_hc_rx_exit           = ccid3_hc_rx_exit,
1155         .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
1156         .ccid_hc_rx_packet_recv    = ccid3_hc_rx_packet_recv,
1157         .ccid_hc_rx_get_info       = ccid3_hc_rx_get_info,
1158         .ccid_hc_tx_get_info       = ccid3_hc_tx_get_info,
1159 };
1160  
1161 module_param(ccid3_debug, int, 0444);
1162 MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
1163
1164 static __init int ccid3_module_init(void)
1165 {
1166         int rc = -ENOBUFS;
1167
1168         ccid3_rx_hist = dccp_rx_hist_new("ccid3");
1169         if (ccid3_rx_hist == NULL)
1170                 goto out;
1171
1172         ccid3_tx_hist = dccp_tx_hist_new("ccid3");
1173         if (ccid3_tx_hist == NULL)
1174                 goto out_free_rx;
1175
1176         ccid3_li_hist = dccp_li_hist_new("ccid3");
1177         if (ccid3_li_hist == NULL)
1178                 goto out_free_tx;
1179
1180         rc = ccid_register(&ccid3);
1181         if (rc != 0) 
1182                 goto out_free_loss_interval_history;
1183 out:
1184         return rc;
1185
1186 out_free_loss_interval_history:
1187         dccp_li_hist_delete(ccid3_li_hist);
1188         ccid3_li_hist = NULL;
1189 out_free_tx:
1190         dccp_tx_hist_delete(ccid3_tx_hist);
1191         ccid3_tx_hist = NULL;
1192 out_free_rx:
1193         dccp_rx_hist_delete(ccid3_rx_hist);
1194         ccid3_rx_hist = NULL;
1195         goto out;
1196 }
1197 module_init(ccid3_module_init);
1198
1199 static __exit void ccid3_module_exit(void)
1200 {
1201 #ifdef CONFIG_IP_DCCP_UNLOAD_HACK
1202         /*
1203          * Hack to use while developing, so that we get rid of the control
1204          * sock, that is what keeps a refcount on dccp.ko -acme
1205          */
1206         extern void dccp_ctl_sock_exit(void);
1207
1208         dccp_ctl_sock_exit();
1209 #endif
1210         ccid_unregister(&ccid3);
1211
1212         if (ccid3_tx_hist != NULL) {
1213                 dccp_tx_hist_delete(ccid3_tx_hist);
1214                 ccid3_tx_hist = NULL;
1215         }
1216         if (ccid3_rx_hist != NULL) {
1217                 dccp_rx_hist_delete(ccid3_rx_hist);
1218                 ccid3_rx_hist = NULL;
1219         }
1220         if (ccid3_li_hist != NULL) {
1221                 dccp_li_hist_delete(ccid3_li_hist);
1222                 ccid3_li_hist = NULL;
1223         }
1224 }
1225 module_exit(ccid3_module_exit);
1226
1227 MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
1228               "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
1229 MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
1230 MODULE_LICENSE("GPL");
1231 MODULE_ALIAS("net-dccp-ccid-3");