[DCCP]: ditch dccp_v[46]_ctl_send_ack
[linux-2.6] / net / dccp / feat.c
1 /*
2  *  net/dccp/feat.c
3  *
4  *  An implementation of the DCCP protocol
5  *  Andrea Bittau <a.bittau@cs.ucl.ac.uk>
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/config.h>
14 #include <linux/module.h>
15
16 #include "dccp.h"
17 #include "ccid.h"
18 #include "feat.h"
19
20 #define DCCP_FEAT_SP_NOAGREE (-123)
21
22 int dccp_feat_change(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len,
23                      gfp_t gfp)
24 {
25         struct dccp_sock *dp = dccp_sk(sk);
26         struct dccp_opt_pend *opt;
27
28         dccp_pr_debug("feat change type=%d feat=%d\n", type, feature);
29
30         /* XXX sanity check feat change request */
31
32         /* check if that feature is already being negotiated */
33         list_for_each_entry(opt, &dp->dccps_options.dccpo_pending,
34                             dccpop_node) {
35                 /* ok we found a negotiation for this option already */
36                 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
37                         dccp_pr_debug("Replacing old\n");
38                         /* replace */
39                         BUG_ON(opt->dccpop_val == NULL);
40                         kfree(opt->dccpop_val);
41                         opt->dccpop_val  = val;
42                         opt->dccpop_len  = len;
43                         opt->dccpop_conf = 0;
44                         return 0;
45                 }
46         }
47
48         /* negotiation for a new feature */
49         opt = kmalloc(sizeof(*opt), gfp);
50         if (opt == NULL)
51                 return -ENOMEM;
52
53         opt->dccpop_type = type;
54         opt->dccpop_feat = feature;
55         opt->dccpop_len  = len;
56         opt->dccpop_val  = val;
57         opt->dccpop_conf = 0;
58         opt->dccpop_sc   = NULL;
59
60         BUG_ON(opt->dccpop_val == NULL);
61
62         list_add_tail(&opt->dccpop_node, &dp->dccps_options.dccpo_pending);
63         return 0;
64 }
65
66 EXPORT_SYMBOL_GPL(dccp_feat_change);
67
68 static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
69 {
70         struct dccp_sock *dp = dccp_sk(sk);
71         /* figure out if we are changing our CCID or the peer's */
72         const int rx = type == DCCPO_CHANGE_R;
73         const u8 ccid_nr = rx ? dp->dccps_options.dccpo_rx_ccid :
74                                 dp->dccps_options.dccpo_tx_ccid;
75         struct ccid *new_ccid;
76
77         /* Check if nothing is being changed. */
78         if (ccid_nr == new_ccid_nr)
79                 return 0;
80
81         new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
82         if (new_ccid == NULL)
83                 return -ENOMEM;
84
85         if (rx) {
86                 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
87                 dp->dccps_hc_rx_ccid = new_ccid;
88                 dp->dccps_options.dccpo_rx_ccid = new_ccid_nr;
89         } else {
90                 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
91                 dp->dccps_hc_tx_ccid = new_ccid;
92                 dp->dccps_options.dccpo_tx_ccid = new_ccid_nr;
93         }
94
95         return 0;
96 }
97
98 /* XXX taking only u8 vals */
99 static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
100 {
101         dccp_pr_debug("changing [%d] feat %d to %d\n", type, feat, val);
102
103         switch (feat) {
104         case DCCPF_CCID:
105                 return dccp_feat_update_ccid(sk, type, val);
106         default:
107                 dccp_pr_debug("IMPLEMENT changing [%d] feat %d to %d\n",
108                               type, feat, val);
109                 break;
110         }
111         return 0;
112 }
113
114 static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
115                                u8 *rpref, u8 rlen)
116 {
117         struct dccp_sock *dp = dccp_sk(sk);
118         u8 *spref, slen, *res = NULL;
119         int i, j, rc, agree = 1;
120
121         BUG_ON(rpref == NULL);
122
123         /* check if we are the black sheep */
124         if (dp->dccps_role == DCCP_ROLE_CLIENT) {
125                 spref = rpref;
126                 slen  = rlen;
127                 rpref = opt->dccpop_val;
128                 rlen  = opt->dccpop_len;
129         } else {
130                 spref = opt->dccpop_val;
131                 slen  = opt->dccpop_len;
132         }
133         /*
134          * Now we have server preference list in spref and client preference in
135          * rpref
136          */
137         BUG_ON(spref == NULL);
138         BUG_ON(rpref == NULL);
139
140         /* FIXME sanity check vals */
141
142         /* Are values in any order?  XXX Lame "algorithm" here */
143         /* XXX assume values are 1 byte */
144         for (i = 0; i < slen; i++) {
145                 for (j = 0; j < rlen; j++) {
146                         if (spref[i] == rpref[j]) {
147                                 res = &spref[i];
148                                 break;
149                         }
150                 }
151                 if (res)
152                         break;
153         }
154
155         /* we didn't agree on anything */
156         if (res == NULL) {
157                 /* confirm previous value */
158                 switch (opt->dccpop_feat) {
159                 case DCCPF_CCID:
160                         /* XXX did i get this right? =P */
161                         if (opt->dccpop_type == DCCPO_CHANGE_L)
162                                 res = &dp->dccps_options.dccpo_tx_ccid;
163                         else
164                                 res = &dp->dccps_options.dccpo_rx_ccid;
165                         break;
166
167                 default:
168                         WARN_ON(1); /* XXX implement res */
169                         return -EFAULT;
170                 }
171
172                 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
173                 agree = 0; /* this is used for mandatory options... */
174         }
175
176         /* need to put result and our preference list */
177         /* XXX assume 1 byte vals */
178         rlen = 1 + opt->dccpop_len;
179         rpref = kmalloc(rlen, GFP_ATOMIC);
180         if (rpref == NULL)
181                 return -ENOMEM;
182
183         *rpref = *res;
184         memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
185
186         /* put it in the "confirm queue" */
187         if (opt->dccpop_sc == NULL) {
188                 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
189                 if (opt->dccpop_sc == NULL) {
190                         kfree(rpref);
191                         return -ENOMEM;
192                 }
193         } else {
194                 /* recycle the confirm slot */
195                 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
196                 kfree(opt->dccpop_sc->dccpoc_val);
197                 dccp_pr_debug("recycling confirm slot\n");
198         }
199         memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
200
201         opt->dccpop_sc->dccpoc_val = rpref;
202         opt->dccpop_sc->dccpoc_len = rlen;
203
204         /* update the option on our side [we are about to send the confirm] */
205         rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
206         if (rc) {
207                 kfree(opt->dccpop_sc->dccpoc_val);
208                 kfree(opt->dccpop_sc);
209                 opt->dccpop_sc = 0;
210                 return rc;
211         }
212
213         dccp_pr_debug("Will confirm %d\n", *rpref);
214
215         /* say we want to change to X but we just got a confirm X, suppress our
216          * change
217          */
218         if (!opt->dccpop_conf) {
219                 if (*opt->dccpop_val == *res)
220                         opt->dccpop_conf = 1;
221                 dccp_pr_debug("won't ask for change of same feature\n");
222         }
223
224         return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
225 }
226
227 static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
228 {
229         struct dccp_sock *dp = dccp_sk(sk);
230         struct dccp_opt_pend *opt;
231         int rc = 1;
232         u8 t;
233
234         /*
235          * We received a CHANGE.  We gotta match it against our own preference
236          * list.  If we got a CHANGE_R it means it's a change for us, so we need
237          * to compare our CHANGE_L list.
238          */
239         if (type == DCCPO_CHANGE_L)
240                 t = DCCPO_CHANGE_R;
241         else
242                 t = DCCPO_CHANGE_L;
243
244         /* find our preference list for this feature */
245         list_for_each_entry(opt, &dp->dccps_options.dccpo_pending,
246                             dccpop_node) {
247                 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
248                         continue;
249
250                 /* find the winner from the two preference lists */
251                 rc = dccp_feat_reconcile(sk, opt, val, len);
252                 break;
253         }
254
255         /* We didn't deal with the change.  This can happen if we have no
256          * preference list for the feature.  In fact, it just shouldn't
257          * happen---if we understand a feature, we should have a preference list
258          * with at least the default value.
259          */
260         BUG_ON(rc == 1);
261
262         return rc;
263 }
264
265 static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
266 {
267         struct dccp_opt_pend *opt;
268         struct dccp_sock *dp = dccp_sk(sk);
269         u8 *copy;
270         int rc;
271
272         /* NN features must be change L */
273         if (type == DCCPO_CHANGE_R) {
274                 dccp_pr_debug("received CHANGE_R %d for NN feat %d\n",
275                               type, feature);
276                 return -EFAULT;
277         }
278
279         /* XXX sanity check opt val */
280
281         /* copy option so we can confirm it */
282         opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
283         if (opt == NULL)
284                 return -ENOMEM;
285
286         copy = kmalloc(len, GFP_ATOMIC);
287         if (copy == NULL) {
288                 kfree(opt);
289                 return -ENOMEM;
290         }
291         memcpy(copy, val, len);
292
293         opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
294         opt->dccpop_feat = feature;
295         opt->dccpop_val  = copy;
296         opt->dccpop_len  = len;
297
298         /* change feature */
299         rc = dccp_feat_update(sk, type, feature, *val);
300         if (rc) {
301                 kfree(opt->dccpop_val);
302                 kfree(opt);
303                 return rc;
304         }
305
306         dccp_pr_debug("Confirming NN feature %d (val=%d)\n", feature, *copy);
307         list_add_tail(&opt->dccpop_node, &dp->dccps_options.dccpo_conf);
308
309         return 0;
310 }
311
312 static void dccp_feat_empty_confirm(struct sock *sk, u8 type, u8 feature)
313 {
314         struct dccp_sock *dp = dccp_sk(sk);
315         /* XXX check if other confirms for that are queued and recycle slot */
316         struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
317
318         if (opt == NULL) {
319                 /* XXX what do we do?  Ignoring should be fine.  It's a change
320                  * after all =P
321                  */
322                 return;
323         }
324
325         opt->dccpop_type = type == DCCPO_CHANGE_L ? DCCPO_CONFIRM_R :
326                                                     DCCPO_CONFIRM_L;
327         opt->dccpop_feat = feature;
328         opt->dccpop_val  = 0;
329         opt->dccpop_len  = 0;
330
331         /* change feature */
332         dccp_pr_debug("Empty confirm feature %d type %d\n", feature, type);
333         list_add_tail(&opt->dccpop_node, &dp->dccps_options.dccpo_conf);
334 }
335
336 static void dccp_feat_flush_confirm(struct sock *sk)
337 {
338         struct dccp_sock *dp = dccp_sk(sk);
339         /* Check if there is anything to confirm in the first place */
340         int yes = !list_empty(&dp->dccps_options.dccpo_conf);
341
342         if (!yes) {
343                 struct dccp_opt_pend *opt;
344
345                 list_for_each_entry(opt, &dp->dccps_options.dccpo_pending,
346                                     dccpop_node) {
347                         if (opt->dccpop_conf) {
348                                 yes = 1;
349                                 break;
350                         }
351                 }
352         }
353
354         if (!yes)
355                 return;
356
357         /* OK there is something to confirm... */
358         /* XXX check if packet is in flight?  Send delayed ack?? */
359         if (sk->sk_state == DCCP_OPEN)
360                 dccp_send_ack(sk);
361 }
362
363 int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
364 {
365         int rc;
366
367         dccp_pr_debug("got feat change type=%d feat=%d\n", type, feature);
368
369         /* figure out if it's SP or NN feature */
370         switch (feature) {
371         /* deal with SP features */
372         case DCCPF_CCID:
373                 rc = dccp_feat_sp(sk, type, feature, val, len);
374                 break;
375
376         /* deal with NN features */
377         case DCCPF_ACK_RATIO:
378                 rc = dccp_feat_nn(sk, type, feature, val, len);
379                 break;
380
381         /* XXX implement other features */
382         default:
383                 rc = -EFAULT;
384                 break;
385         }
386
387         /* check if there were problems changing features */
388         if (rc) {
389                 /* If we don't agree on SP, we sent a confirm for old value.
390                  * However we propagate rc to caller in case option was
391                  * mandatory
392                  */
393                 if (rc != DCCP_FEAT_SP_NOAGREE)
394                         dccp_feat_empty_confirm(sk, type, feature);
395         }
396
397         /* generate the confirm [if required] */
398         dccp_feat_flush_confirm(sk);
399
400         return rc;
401 }
402
403 EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
404
405 int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
406                            u8 *val, u8 len)
407 {
408         u8 t;
409         struct dccp_opt_pend *opt;
410         struct dccp_sock *dp = dccp_sk(sk);
411         int rc = 1;
412         int all_confirmed = 1;
413
414         dccp_pr_debug("got feat confirm type=%d feat=%d\n", type, feature);
415
416         /* XXX sanity check type & feat */
417
418         /* locate our change request */
419         t = type == DCCPO_CONFIRM_L ? DCCPO_CHANGE_R : DCCPO_CHANGE_L;
420
421         list_for_each_entry(opt, &dp->dccps_options.dccpo_pending,
422                             dccpop_node) {
423                 if (!opt->dccpop_conf && opt->dccpop_type == t &&
424                     opt->dccpop_feat == feature) {
425                         /* we found it */
426                         /* XXX do sanity check */
427
428                         opt->dccpop_conf = 1;
429
430                         /* We got a confirmation---change the option */
431                         dccp_feat_update(sk, opt->dccpop_type,
432                                          opt->dccpop_feat, *val);
433
434                         dccp_pr_debug("feat %d type %d confirmed %d\n",
435                                       feature, type, *val);
436                         rc = 0;
437                         break;
438                 }
439
440                 if (!opt->dccpop_conf)
441                         all_confirmed = 0;
442         }
443
444         /* fix re-transmit timer */
445         /* XXX gotta make sure that no option negotiation occurs during
446          * connection shutdown.  Consider that the CLOSEREQ is sent and timer is
447          * on.  if all options are confirmed it might kill timer which should
448          * remain alive until close is received.
449          */
450         if (all_confirmed) {
451                 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
452                 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
453         }
454
455         if (rc)
456                 dccp_pr_debug("feat %d type %d never requested\n",
457                               feature, type);
458         return 0;
459 }
460
461 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
462
463 void dccp_feat_clean(struct sock *sk)
464 {
465         struct dccp_sock *dp = dccp_sk(sk);
466         struct dccp_opt_pend *opt, *next;
467
468         list_for_each_entry_safe(opt, next, &dp->dccps_options.dccpo_pending,
469                                  dccpop_node) {
470                 BUG_ON(opt->dccpop_val == NULL);
471                 kfree(opt->dccpop_val);
472
473                 if (opt->dccpop_sc != NULL) {
474                         BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
475                         kfree(opt->dccpop_sc->dccpoc_val);
476                         kfree(opt->dccpop_sc);
477                 }
478
479                 kfree(opt);
480         }
481         INIT_LIST_HEAD(&dp->dccps_options.dccpo_pending);
482
483         list_for_each_entry_safe(opt, next, &dp->dccps_options.dccpo_conf,
484                                  dccpop_node) {
485                 BUG_ON(opt == NULL);
486                 if (opt->dccpop_val != NULL)
487                         kfree(opt->dccpop_val);
488                 kfree(opt);
489         }
490         INIT_LIST_HEAD(&dp->dccps_options.dccpo_conf);
491 }
492
493 EXPORT_SYMBOL_GPL(dccp_feat_clean);
494
495 /* this is to be called only when a listening sock creates its child.  It is
496  * assumed by the function---the confirm is not duplicated, but rather it is
497  * "passed on".
498  */
499 int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
500 {
501         struct dccp_sock *olddp = dccp_sk(oldsk);
502         struct dccp_sock *newdp = dccp_sk(newsk);
503         struct dccp_opt_pend *opt;
504         int rc = 0;
505
506         INIT_LIST_HEAD(&newdp->dccps_options.dccpo_pending);
507         INIT_LIST_HEAD(&newdp->dccps_options.dccpo_conf);
508
509         list_for_each_entry(opt, &olddp->dccps_options.dccpo_pending,
510                             dccpop_node) {
511                 struct dccp_opt_pend *newopt;
512                 /* copy the value of the option */
513                 u8 *val = kmalloc(opt->dccpop_len, GFP_ATOMIC);
514
515                 if (val == NULL)
516                         goto out_clean;
517                 memcpy(val, opt->dccpop_val, opt->dccpop_len);
518
519                 newopt = kmalloc(sizeof(*newopt), GFP_ATOMIC);
520                 if (newopt == NULL) {
521                         kfree(val);
522                         goto out_clean;
523                 }
524
525                 /* insert the option */
526                 memcpy(newopt, opt, sizeof(*newopt));
527                 newopt->dccpop_val = val;
528                 list_add_tail(&newopt->dccpop_node,
529                               &newdp->dccps_options.dccpo_pending);
530
531                 /* XXX what happens with backlogs and multiple connections at
532                  * once...
533                  */
534                 /* the master socket no longer needs to worry about confirms */
535                 opt->dccpop_sc = 0; /* it's not a memleak---new socket has it */
536
537                 /* reset state for a new socket */
538                 opt->dccpop_conf = 0;
539         }
540
541         /* XXX not doing anything about the conf queue */
542
543 out:
544         return rc;
545
546 out_clean:
547         dccp_feat_clean(newsk);
548         rc = -ENOMEM;
549         goto out;
550 }
551
552 EXPORT_SYMBOL_GPL(dccp_feat_clone);
553
554 static int __dccp_feat_init(struct sock *sk, u8 type, u8 feat, u8 *val, u8 len)
555 {
556         int rc = -ENOMEM;
557         u8 *copy = kmalloc(len, GFP_KERNEL);
558
559         if (copy != NULL) {
560                 memcpy(copy, val, len);
561                 rc = dccp_feat_change(sk, type, feat, copy, len, GFP_KERNEL);
562                 if (rc)
563                         kfree(copy);
564         }
565         return rc;
566 }
567
568 int dccp_feat_init(struct sock *sk)
569 {
570         struct dccp_sock *dp = dccp_sk(sk);
571         int rc;
572
573         INIT_LIST_HEAD(&dp->dccps_options.dccpo_pending);
574         INIT_LIST_HEAD(&dp->dccps_options.dccpo_conf);
575
576         /* CCID L */
577         rc = __dccp_feat_init(sk, DCCPO_CHANGE_L, DCCPF_CCID,
578                               &dp->dccps_options.dccpo_tx_ccid, 1);
579         if (rc)
580                 goto out;
581
582         /* CCID R */
583         rc = __dccp_feat_init(sk, DCCPO_CHANGE_R, DCCPF_CCID,
584                               &dp->dccps_options.dccpo_rx_ccid, 1);
585         if (rc)
586                 goto out;
587
588         /* Ack ratio */
589         rc = __dccp_feat_init(sk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
590                               &dp->dccps_options.dccpo_ack_ratio, 1);
591 out:
592         return rc;
593 }
594
595 EXPORT_SYMBOL_GPL(dccp_feat_init);