[IRDA] sem2mutex: drivers/net/irda
[linux-2.6/mini2440.git] / net / dccp / feat.c
blobed0851fa3cb3f4c49e076e8c86cd7c4008a39e06
1 /*
2 * net/dccp/feat.c
4 * An implementation of the DCCP protocol
5 * Andrea Bittau <a.bittau@cs.ucl.ac.uk>
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.
13 #include <linux/config.h>
14 #include <linux/module.h>
16 #include "dccp.h"
17 #include "ccid.h"
18 #include "feat.h"
20 #define DCCP_FEAT_SP_NOAGREE (-123)
22 int dccp_feat_change(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len,
23 gfp_t gfp)
25 struct dccp_sock *dp = dccp_sk(sk);
26 struct dccp_opt_pend *opt;
28 dccp_pr_debug("feat change type=%d feat=%d\n", type, feature);
30 /* XXX sanity check feat change request */
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;
48 /* negotiation for a new feature */
49 opt = kmalloc(sizeof(*opt), gfp);
50 if (opt == NULL)
51 return -ENOMEM;
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;
60 BUG_ON(opt->dccpop_val == NULL);
62 list_add_tail(&opt->dccpop_node, &dp->dccps_options.dccpo_pending);
63 return 0;
66 EXPORT_SYMBOL_GPL(dccp_feat_change);
68 static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
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;
77 /* Check if nothing is being changed. */
78 if (ccid_nr == new_ccid_nr)
79 return 0;
81 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
82 if (new_ccid == NULL)
83 return -ENOMEM;
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;
95 return 0;
98 /* XXX taking only u8 vals */
99 static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
101 dccp_pr_debug("changing [%d] feat %d to %d\n", type, feat, val);
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;
111 return 0;
114 static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
115 u8 *rpref, u8 rlen)
117 struct dccp_sock *dp = dccp_sk(sk);
118 u8 *spref, slen, *res = NULL;
119 int i, j, rc, agree = 1;
121 BUG_ON(rpref == NULL);
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;
134 * Now we have server preference list in spref and client preference in
135 * rpref
137 BUG_ON(spref == NULL);
138 BUG_ON(rpref == NULL);
140 /* FIXME sanity check vals */
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;
151 if (res)
152 break;
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;
167 default:
168 WARN_ON(1); /* XXX implement res */
169 return -EFAULT;
172 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
173 agree = 0; /* this is used for mandatory options... */
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;
183 *rpref = *res;
184 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
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;
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");
199 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
201 opt->dccpop_sc->dccpoc_val = rpref;
202 opt->dccpop_sc->dccpoc_len = rlen;
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;
213 dccp_pr_debug("Will confirm %d\n", *rpref);
215 /* say we want to change to X but we just got a confirm X, suppress our
216 * change
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");
224 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
227 static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
229 struct dccp_sock *dp = dccp_sk(sk);
230 struct dccp_opt_pend *opt;
231 int rc = 1;
232 u8 t;
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.
239 if (type == DCCPO_CHANGE_L)
240 t = DCCPO_CHANGE_R;
241 else
242 t = DCCPO_CHANGE_L;
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;
250 /* find the winner from the two preference lists */
251 rc = dccp_feat_reconcile(sk, opt, val, len);
252 break;
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.
260 BUG_ON(rc == 1);
262 return rc;
265 static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
267 struct dccp_opt_pend *opt;
268 struct dccp_sock *dp = dccp_sk(sk);
269 u8 *copy;
270 int rc;
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;
279 /* XXX sanity check opt val */
281 /* copy option so we can confirm it */
282 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
283 if (opt == NULL)
284 return -ENOMEM;
286 copy = kmalloc(len, GFP_ATOMIC);
287 if (copy == NULL) {
288 kfree(opt);
289 return -ENOMEM;
291 memcpy(copy, val, len);
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;
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;
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);
309 return 0;
312 static void dccp_feat_empty_confirm(struct sock *sk, u8 type, u8 feature)
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);
318 if (opt == NULL) {
319 /* XXX what do we do? Ignoring should be fine. It's a change
320 * after all =P
322 return;
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;
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);
336 static void dccp_feat_flush_confirm(struct sock *sk)
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);
342 if (!yes) {
343 struct dccp_opt_pend *opt;
345 list_for_each_entry(opt, &dp->dccps_options.dccpo_pending,
346 dccpop_node) {
347 if (opt->dccpop_conf) {
348 yes = 1;
349 break;
354 if (!yes)
355 return;
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);
363 int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
365 int rc;
367 dccp_pr_debug("got feat change type=%d feat=%d\n", type, feature);
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;
376 /* deal with NN features */
377 case DCCPF_ACK_RATIO:
378 rc = dccp_feat_nn(sk, type, feature, val, len);
379 break;
381 /* XXX implement other features */
382 default:
383 rc = -EFAULT;
384 break;
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
393 if (rc != DCCP_FEAT_SP_NOAGREE)
394 dccp_feat_empty_confirm(sk, type, feature);
397 /* generate the confirm [if required] */
398 dccp_feat_flush_confirm(sk);
400 return rc;
403 EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
405 int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
406 u8 *val, u8 len)
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;
414 dccp_pr_debug("got feat confirm type=%d feat=%d\n", type, feature);
416 /* XXX sanity check type & feat */
418 /* locate our change request */
419 t = type == DCCPO_CONFIRM_L ? DCCPO_CHANGE_R : DCCPO_CHANGE_L;
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 */
428 opt->dccpop_conf = 1;
430 /* We got a confirmation---change the option */
431 dccp_feat_update(sk, opt->dccpop_type,
432 opt->dccpop_feat, *val);
434 dccp_pr_debug("feat %d type %d confirmed %d\n",
435 feature, type, *val);
436 rc = 0;
437 break;
440 if (!opt->dccpop_conf)
441 all_confirmed = 0;
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.
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);
455 if (rc)
456 dccp_pr_debug("feat %d type %d never requested\n",
457 feature, type);
458 return 0;
461 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
463 void dccp_feat_clean(struct sock *sk)
465 struct dccp_sock *dp = dccp_sk(sk);
466 struct dccp_opt_pend *opt, *next;
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);
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);
479 kfree(opt);
481 INIT_LIST_HEAD(&dp->dccps_options.dccpo_pending);
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);
490 INIT_LIST_HEAD(&dp->dccps_options.dccpo_conf);
493 EXPORT_SYMBOL_GPL(dccp_feat_clean);
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".
499 int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
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;
506 INIT_LIST_HEAD(&newdp->dccps_options.dccpo_pending);
507 INIT_LIST_HEAD(&newdp->dccps_options.dccpo_conf);
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);
515 if (val == NULL)
516 goto out_clean;
517 memcpy(val, opt->dccpop_val, opt->dccpop_len);
519 newopt = kmalloc(sizeof(*newopt), GFP_ATOMIC);
520 if (newopt == NULL) {
521 kfree(val);
522 goto out_clean;
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);
531 /* XXX what happens with backlogs and multiple connections at
532 * once...
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 */
537 /* reset state for a new socket */
538 opt->dccpop_conf = 0;
541 /* XXX not doing anything about the conf queue */
543 out:
544 return rc;
546 out_clean:
547 dccp_feat_clean(newsk);
548 rc = -ENOMEM;
549 goto out;
552 EXPORT_SYMBOL_GPL(dccp_feat_clone);
554 static int __dccp_feat_init(struct sock *sk, u8 type, u8 feat, u8 *val, u8 len)
556 int rc = -ENOMEM;
557 u8 *copy = kmalloc(len, GFP_KERNEL);
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);
565 return rc;
568 int dccp_feat_init(struct sock *sk)
570 struct dccp_sock *dp = dccp_sk(sk);
571 int rc;
573 INIT_LIST_HEAD(&dp->dccps_options.dccpo_pending);
574 INIT_LIST_HEAD(&dp->dccps_options.dccpo_conf);
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;
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;
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;
595 EXPORT_SYMBOL_GPL(dccp_feat_init);