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