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>
19 #define DCCP_FEAT_SP_NOAGREE (-123)
21 int dccp_feat_change(struct sock
*sk
, u8 type
, u8 feature
, u8
*val
, u8 len
,
24 struct dccp_sock
*dp
= dccp_sk(sk
);
25 struct dccp_opt_pend
*opt
;
27 dccp_pr_debug("feat change type=%d feat=%d\n", type
, feature
);
29 /* check if that feature is already being negotiated */
30 list_for_each_entry(opt
, &dp
->dccps_options
.dccpo_pending
,
32 /* ok we found a negotiation for this option already */
33 if (opt
->dccpop_feat
== feature
&& opt
->dccpop_type
== type
) {
34 dccp_pr_debug("Replacing old\n");
36 BUG_ON(opt
->dccpop_val
== NULL
);
37 kfree(opt
->dccpop_val
);
38 opt
->dccpop_val
= val
;
39 opt
->dccpop_len
= len
;
45 /* negotiation for a new feature */
46 opt
= kmalloc(sizeof(*opt
), gfp
);
50 opt
->dccpop_type
= type
;
51 opt
->dccpop_feat
= feature
;
52 opt
->dccpop_len
= len
;
53 opt
->dccpop_val
= val
;
55 opt
->dccpop_sc
= NULL
;
57 BUG_ON(opt
->dccpop_val
== NULL
);
59 list_add_tail(&opt
->dccpop_node
, &dp
->dccps_options
.dccpo_pending
);
63 EXPORT_SYMBOL_GPL(dccp_feat_change
);
65 /* XXX taking only u8 vals */
66 static int dccp_feat_update(struct sock
*sk
, u8 type
, u8 feat
, u8 val
)
69 dccp_pr_debug("changing [%d] feat %d to %d\n", type
, feat
, val
);
73 static int dccp_feat_reconcile(struct sock
*sk
, struct dccp_opt_pend
*opt
,
76 struct dccp_sock
*dp
= dccp_sk(sk
);
77 u8
*spref
, slen
, *res
= NULL
;
78 int i
, j
, rc
, agree
= 1;
80 BUG_ON(rpref
== NULL
);
82 /* check if we are the black sheep */
83 if (dp
->dccps_role
== DCCP_ROLE_CLIENT
) {
86 rpref
= opt
->dccpop_val
;
87 rlen
= opt
->dccpop_len
;
89 spref
= opt
->dccpop_val
;
90 slen
= opt
->dccpop_len
;
93 * Now we have server preference list in spref and client preference in
96 BUG_ON(spref
== NULL
);
97 BUG_ON(rpref
== NULL
);
99 /* FIXME sanity check vals */
101 /* Are values in any order? XXX Lame "algorithm" here */
102 /* XXX assume values are 1 byte */
103 for (i
= 0; i
< slen
; i
++) {
104 for (j
= 0; j
< rlen
; j
++) {
105 if (spref
[i
] == rpref
[j
]) {
114 /* we didn't agree on anything */
116 /* confirm previous value */
117 switch (opt
->dccpop_feat
) {
119 /* XXX did i get this right? =P */
120 if (opt
->dccpop_type
== DCCPO_CHANGE_L
)
121 res
= &dp
->dccps_options
.dccpo_tx_ccid
;
123 res
= &dp
->dccps_options
.dccpo_rx_ccid
;
127 WARN_ON(1); /* XXX implement res */
131 dccp_pr_debug("Don't agree... reconfirming %d\n", *res
);
132 agree
= 0; /* this is used for mandatory options... */
135 /* need to put result and our preference list */
136 /* XXX assume 1 byte vals */
137 rlen
= 1 + opt
->dccpop_len
;
138 rpref
= kmalloc(rlen
, GFP_ATOMIC
);
143 memcpy(&rpref
[1], opt
->dccpop_val
, opt
->dccpop_len
);
145 /* put it in the "confirm queue" */
146 if (opt
->dccpop_sc
== NULL
) {
147 opt
->dccpop_sc
= kmalloc(sizeof(*opt
->dccpop_sc
), GFP_ATOMIC
);
148 if (opt
->dccpop_sc
== NULL
) {
153 /* recycle the confirm slot */
154 BUG_ON(opt
->dccpop_sc
->dccpoc_val
== NULL
);
155 kfree(opt
->dccpop_sc
->dccpoc_val
);
156 dccp_pr_debug("recycling confirm slot\n");
158 memset(opt
->dccpop_sc
, 0, sizeof(*opt
->dccpop_sc
));
160 opt
->dccpop_sc
->dccpoc_val
= rpref
;
161 opt
->dccpop_sc
->dccpoc_len
= rlen
;
163 /* update the option on our side [we are about to send the confirm] */
164 rc
= dccp_feat_update(sk
, opt
->dccpop_type
, opt
->dccpop_feat
, *res
);
166 kfree(opt
->dccpop_sc
->dccpoc_val
);
167 kfree(opt
->dccpop_sc
);
172 dccp_pr_debug("Will confirm %d\n", *rpref
);
174 /* say we want to change to X but we just got a confirm X, suppress our
177 if (!opt
->dccpop_conf
) {
178 if (*opt
->dccpop_val
== *res
)
179 opt
->dccpop_conf
= 1;
180 dccp_pr_debug("won't ask for change of same feature\n");
183 return agree
? 0 : DCCP_FEAT_SP_NOAGREE
; /* used for mandatory opts */
186 static int dccp_feat_sp(struct sock
*sk
, u8 type
, u8 feature
, u8
*val
, u8 len
)
188 struct dccp_sock
*dp
= dccp_sk(sk
);
189 struct dccp_opt_pend
*opt
;
194 * We received a CHANGE. We gotta match it against our own preference
195 * list. If we got a CHANGE_R it means it's a change for us, so we need
196 * to compare our CHANGE_L list.
198 if (type
== DCCPO_CHANGE_L
)
203 /* find our preference list for this feature */
204 list_for_each_entry(opt
, &dp
->dccps_options
.dccpo_pending
,
206 if (opt
->dccpop_type
!= t
|| opt
->dccpop_feat
!= feature
)
209 /* find the winner from the two preference lists */
210 rc
= dccp_feat_reconcile(sk
, opt
, val
, len
);
214 /* We didn't deal with the change. This can happen if we have no
215 * preference list for the feature. In fact, it just shouldn't
216 * happen---if we understand a feature, we should have a preference list
217 * with at least the default value.
224 static int dccp_feat_nn(struct sock
*sk
, u8 type
, u8 feature
, u8
*val
, u8 len
)
226 struct dccp_opt_pend
*opt
;
227 struct dccp_sock
*dp
= dccp_sk(sk
);
231 /* NN features must be change L */
232 if (type
== DCCPO_CHANGE_R
) {
233 dccp_pr_debug("received CHANGE_R %d for NN feat %d\n",
238 /* XXX sanity check opt val */
240 /* copy option so we can confirm it */
241 opt
= kzalloc(sizeof(*opt
), GFP_ATOMIC
);
245 copy
= kmalloc(len
, GFP_ATOMIC
);
250 memcpy(copy
, val
, len
);
252 opt
->dccpop_type
= DCCPO_CONFIRM_R
; /* NN can only confirm R */
253 opt
->dccpop_feat
= feature
;
254 opt
->dccpop_val
= copy
;
255 opt
->dccpop_len
= len
;
258 rc
= dccp_feat_update(sk
, type
, feature
, *val
);
260 kfree(opt
->dccpop_val
);
265 dccp_pr_debug("Confirming NN feature %d (val=%d)\n", feature
, *copy
);
266 list_add_tail(&opt
->dccpop_node
, &dp
->dccps_options
.dccpo_conf
);
271 static void dccp_feat_empty_confirm(struct sock
*sk
, u8 type
, u8 feature
)
273 struct dccp_sock
*dp
= dccp_sk(sk
);
274 /* XXX check if other confirms for that are queued and recycle slot */
275 struct dccp_opt_pend
*opt
= kzalloc(sizeof(*opt
), GFP_ATOMIC
);
278 /* XXX what do we do? Ignoring should be fine. It's a change
284 opt
->dccpop_type
= type
== DCCPO_CHANGE_L
? DCCPO_CONFIRM_R
:
286 opt
->dccpop_feat
= feature
;
291 dccp_pr_debug("Empty confirm feature %d type %d\n", feature
, type
);
292 list_add_tail(&opt
->dccpop_node
, &dp
->dccps_options
.dccpo_conf
);
295 static void dccp_feat_flush_confirm(struct sock
*sk
)
297 struct dccp_sock
*dp
= dccp_sk(sk
);
298 /* Check if there is anything to confirm in the first place */
299 int yes
= !list_empty(&dp
->dccps_options
.dccpo_conf
);
302 struct dccp_opt_pend
*opt
;
304 list_for_each_entry(opt
, &dp
->dccps_options
.dccpo_pending
,
306 if (opt
->dccpop_conf
) {
316 /* OK there is something to confirm... */
317 /* XXX check if packet is in flight? Send delayed ack?? */
318 if (sk
->sk_state
== DCCP_OPEN
)
322 int dccp_feat_change_recv(struct sock
*sk
, u8 type
, u8 feature
, u8
*val
, u8 len
)
326 dccp_pr_debug("got feat change type=%d feat=%d\n", type
, feature
);
328 /* figure out if it's SP or NN feature */
330 /* deal with SP features */
332 rc
= dccp_feat_sp(sk
, type
, feature
, val
, len
);
335 /* deal with NN features */
336 case DCCPF_ACK_RATIO
:
337 rc
= dccp_feat_nn(sk
, type
, feature
, val
, len
);
340 /* XXX implement other features */
346 /* check if there were problems changing features */
348 /* If we don't agree on SP, we sent a confirm for old value.
349 * However we propagate rc to caller in case option was
352 if (rc
!= DCCP_FEAT_SP_NOAGREE
)
353 dccp_feat_empty_confirm(sk
, type
, feature
);
356 /* generate the confirm [if required] */
357 dccp_feat_flush_confirm(sk
);
362 EXPORT_SYMBOL_GPL(dccp_feat_change_recv
);
364 int dccp_feat_confirm_recv(struct sock
*sk
, u8 type
, u8 feature
,
368 struct dccp_opt_pend
*opt
;
369 struct dccp_sock
*dp
= dccp_sk(sk
);
371 int all_confirmed
= 1;
373 dccp_pr_debug("got feat confirm type=%d feat=%d\n", type
, feature
);
375 /* XXX sanity check type & feat */
377 /* locate our change request */
378 t
= type
== DCCPO_CONFIRM_L
? DCCPO_CHANGE_R
: DCCPO_CHANGE_L
;
380 list_for_each_entry(opt
, &dp
->dccps_options
.dccpo_pending
,
382 if (!opt
->dccpop_conf
&& opt
->dccpop_type
== t
&&
383 opt
->dccpop_feat
== feature
) {
385 /* XXX do sanity check */
387 opt
->dccpop_conf
= 1;
389 /* We got a confirmation---change the option */
390 dccp_feat_update(sk
, opt
->dccpop_type
,
391 opt
->dccpop_feat
, *val
);
393 dccp_pr_debug("feat %d type %d confirmed %d\n",
394 feature
, type
, *val
);
399 if (!opt
->dccpop_conf
)
403 /* fix re-transmit timer */
404 /* XXX gotta make sure that no option negotiation occurs during
405 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
406 * on. if all options are confirmed it might kill timer which should
407 * remain alive until close is received.
410 dccp_pr_debug("clear feat negotiation timer %p\n", sk
);
411 inet_csk_clear_xmit_timer(sk
, ICSK_TIME_RETRANS
);
415 dccp_pr_debug("feat %d type %d never requested\n",
420 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv
);
422 void dccp_feat_clean(struct sock
*sk
)
424 struct dccp_sock
*dp
= dccp_sk(sk
);
425 struct dccp_opt_pend
*opt
, *next
;
427 list_for_each_entry_safe(opt
, next
, &dp
->dccps_options
.dccpo_pending
,
429 BUG_ON(opt
->dccpop_val
== NULL
);
430 kfree(opt
->dccpop_val
);
432 if (opt
->dccpop_sc
!= NULL
) {
433 BUG_ON(opt
->dccpop_sc
->dccpoc_val
== NULL
);
434 kfree(opt
->dccpop_sc
->dccpoc_val
);
435 kfree(opt
->dccpop_sc
);
440 INIT_LIST_HEAD(&dp
->dccps_options
.dccpo_pending
);
442 list_for_each_entry_safe(opt
, next
, &dp
->dccps_options
.dccpo_conf
,
445 if (opt
->dccpop_val
!= NULL
)
446 kfree(opt
->dccpop_val
);
449 INIT_LIST_HEAD(&dp
->dccps_options
.dccpo_conf
);
452 EXPORT_SYMBOL_GPL(dccp_feat_clean
);
454 /* this is to be called only when a listening sock creates its child. It is
455 * assumed by the function---the confirm is not duplicated, but rather it is
458 int dccp_feat_clone(struct sock
*oldsk
, struct sock
*newsk
)
460 struct dccp_sock
*olddp
= dccp_sk(oldsk
);
461 struct dccp_sock
*newdp
= dccp_sk(newsk
);
462 struct dccp_opt_pend
*opt
;
465 INIT_LIST_HEAD(&newdp
->dccps_options
.dccpo_pending
);
466 INIT_LIST_HEAD(&newdp
->dccps_options
.dccpo_conf
);
468 list_for_each_entry(opt
, &olddp
->dccps_options
.dccpo_pending
,
470 struct dccp_opt_pend
*newopt
;
471 /* copy the value of the option */
472 u8
*val
= kmalloc(opt
->dccpop_len
, GFP_ATOMIC
);
476 memcpy(val
, opt
->dccpop_val
, opt
->dccpop_len
);
478 newopt
= kmalloc(sizeof(*newopt
), GFP_ATOMIC
);
479 if (newopt
== NULL
) {
484 /* insert the option */
485 memcpy(newopt
, opt
, sizeof(*newopt
));
486 newopt
->dccpop_val
= val
;
487 list_add_tail(&newopt
->dccpop_node
,
488 &newdp
->dccps_options
.dccpo_pending
);
490 /* XXX what happens with backlogs and multiple connections at
493 /* the master socket no longer needs to worry about confirms */
494 opt
->dccpop_sc
= 0; /* it's not a memleak---new socket has it */
496 /* reset state for a new socket */
497 opt
->dccpop_conf
= 0;
500 /* XXX not doing anything about the conf queue */
506 dccp_feat_clean(newsk
);
511 EXPORT_SYMBOL_GPL(dccp_feat_clone
);
513 static int __dccp_feat_init(struct sock
*sk
, u8 type
, u8 feat
, u8
*val
, u8 len
)
516 u8
*copy
= kmalloc(len
, GFP_KERNEL
);
519 memcpy(copy
, val
, len
);
520 rc
= dccp_feat_change(sk
, type
, feat
, copy
, len
, GFP_KERNEL
);
527 int dccp_feat_init(struct sock
*sk
)
529 struct dccp_sock
*dp
= dccp_sk(sk
);
532 INIT_LIST_HEAD(&dp
->dccps_options
.dccpo_pending
);
533 INIT_LIST_HEAD(&dp
->dccps_options
.dccpo_conf
);
536 rc
= __dccp_feat_init(sk
, DCCPO_CHANGE_L
, DCCPF_CCID
,
537 &dp
->dccps_options
.dccpo_tx_ccid
, 1);
542 rc
= __dccp_feat_init(sk
, DCCPO_CHANGE_R
, DCCPF_CCID
,
543 &dp
->dccps_options
.dccpo_rx_ccid
, 1);
548 rc
= __dccp_feat_init(sk
, DCCPO_CHANGE_L
, DCCPF_ACK_RATIO
,
549 &dp
->dccps_options
.dccpo_ack_ratio
, 1);
554 EXPORT_SYMBOL_GPL(dccp_feat_init
);