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>
20 #define DCCP_FEAT_SP_NOAGREE (-123)
22 int dccp_feat_change(struct sock
*sk
, u8 type
, u8 feature
, u8
*val
, u8 len
,
25 struct dccp_minisock
*dmsk
= dccp_msk(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
, &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");
38 BUG_ON(opt
->dccpop_val
== NULL
);
39 kfree(opt
->dccpop_val
);
40 opt
->dccpop_val
= val
;
41 opt
->dccpop_len
= len
;
47 /* negotiation for a new feature */
48 opt
= kmalloc(sizeof(*opt
), gfp
);
52 opt
->dccpop_type
= type
;
53 opt
->dccpop_feat
= feature
;
54 opt
->dccpop_len
= len
;
55 opt
->dccpop_val
= val
;
57 opt
->dccpop_sc
= NULL
;
59 BUG_ON(opt
->dccpop_val
== NULL
);
61 list_add_tail(&opt
->dccpop_node
, &dmsk
->dccpms_pending
);
65 EXPORT_SYMBOL_GPL(dccp_feat_change
);
67 static int dccp_feat_update_ccid(struct sock
*sk
, u8 type
, u8 new_ccid_nr
)
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
;
76 /* Check if nothing is being changed. */
77 if (ccid_nr
== new_ccid_nr
)
80 new_ccid
= ccid_new(new_ccid_nr
, sk
, rx
, GFP_ATOMIC
);
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
;
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
;
97 /* XXX taking only u8 vals */
98 static int dccp_feat_update(struct sock
*sk
, u8 type
, u8 feat
, u8 val
)
100 dccp_pr_debug("changing [%d] feat %d to %d\n", type
, feat
, val
);
104 return dccp_feat_update_ccid(sk
, type
, val
);
106 dccp_pr_debug("IMPLEMENT changing [%d] feat %d to %d\n",
113 static int dccp_feat_reconcile(struct sock
*sk
, struct dccp_opt_pend
*opt
,
116 struct dccp_sock
*dp
= dccp_sk(sk
);
117 u8
*spref
, slen
, *res
= NULL
;
118 int i
, j
, rc
, agree
= 1;
120 BUG_ON(rpref
== NULL
);
122 /* check if we are the black sheep */
123 if (dp
->dccps_role
== DCCP_ROLE_CLIENT
) {
126 rpref
= opt
->dccpop_val
;
127 rlen
= opt
->dccpop_len
;
129 spref
= opt
->dccpop_val
;
130 slen
= opt
->dccpop_len
;
133 * Now we have server preference list in spref and client preference in
136 BUG_ON(spref
== NULL
);
137 BUG_ON(rpref
== NULL
);
139 /* FIXME sanity check vals */
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
]) {
154 /* we didn't agree on anything */
156 /* confirm previous value */
157 switch (opt
->dccpop_feat
) {
159 /* XXX did i get this right? =P */
160 if (opt
->dccpop_type
== DCCPO_CHANGE_L
)
161 res
= &dccp_msk(sk
)->dccpms_tx_ccid
;
163 res
= &dccp_msk(sk
)->dccpms_rx_ccid
;
167 WARN_ON(1); /* XXX implement res */
171 dccp_pr_debug("Don't agree... reconfirming %d\n", *res
);
172 agree
= 0; /* this is used for mandatory options... */
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
);
183 memcpy(&rpref
[1], opt
->dccpop_val
, opt
->dccpop_len
);
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
) {
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");
198 memset(opt
->dccpop_sc
, 0, sizeof(*opt
->dccpop_sc
));
200 opt
->dccpop_sc
->dccpoc_val
= rpref
;
201 opt
->dccpop_sc
->dccpoc_len
= rlen
;
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
);
206 kfree(opt
->dccpop_sc
->dccpoc_val
);
207 kfree(opt
->dccpop_sc
);
212 dccp_pr_debug("Will confirm %d\n", *rpref
);
214 /* say we want to change to X but we just got a confirm X, suppress our
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");
223 return agree
? 0 : DCCP_FEAT_SP_NOAGREE
; /* used for mandatory opts */
226 static int dccp_feat_sp(struct sock
*sk
, u8 type
, u8 feature
, u8
*val
, u8 len
)
228 struct dccp_minisock
*dmsk
= dccp_msk(sk
);
229 struct dccp_opt_pend
*opt
;
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.
238 if (type
== DCCPO_CHANGE_L
)
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
)
248 /* find the winner from the two preference lists */
249 rc
= dccp_feat_reconcile(sk
, opt
, val
, len
);
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.
263 static int dccp_feat_nn(struct sock
*sk
, u8 type
, u8 feature
, u8
*val
, u8 len
)
265 struct dccp_opt_pend
*opt
;
266 struct dccp_minisock
*dmsk
= dccp_msk(sk
);
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",
277 /* XXX sanity check opt val */
279 /* copy option so we can confirm it */
280 opt
= kzalloc(sizeof(*opt
), GFP_ATOMIC
);
284 copy
= kmalloc(len
, GFP_ATOMIC
);
289 memcpy(copy
, val
, len
);
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
;
297 rc
= dccp_feat_update(sk
, type
, feature
, *val
);
299 kfree(opt
->dccpop_val
);
304 dccp_pr_debug("Confirming NN feature %d (val=%d)\n", feature
, *copy
);
305 list_add_tail(&opt
->dccpop_node
, &dmsk
->dccpms_conf
);
310 static void dccp_feat_empty_confirm(struct sock
*sk
, u8 type
, u8 feature
)
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
);
317 /* XXX what do we do? Ignoring should be fine. It's a change
323 opt
->dccpop_type
= type
== DCCPO_CHANGE_L
? DCCPO_CONFIRM_R
:
325 opt
->dccpop_feat
= feature
;
330 dccp_pr_debug("Empty confirm feature %d type %d\n", feature
, type
);
331 list_add_tail(&opt
->dccpop_node
, &dmsk
->dccpms_conf
);
334 static void dccp_feat_flush_confirm(struct sock
*sk
)
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
);
341 struct dccp_opt_pend
*opt
;
343 list_for_each_entry(opt
, &dmsk
->dccpms_pending
, dccpop_node
) {
344 if (opt
->dccpop_conf
) {
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
)
360 int dccp_feat_change_recv(struct sock
*sk
, u8 type
, u8 feature
, u8
*val
, u8 len
)
364 dccp_pr_debug("got feat change type=%d feat=%d\n", type
, feature
);
366 /* figure out if it's SP or NN feature */
368 /* deal with SP features */
370 rc
= dccp_feat_sp(sk
, type
, feature
, val
, len
);
373 /* deal with NN features */
374 case DCCPF_ACK_RATIO
:
375 rc
= dccp_feat_nn(sk
, type
, feature
, val
, len
);
378 /* XXX implement other features */
384 /* check if there were problems changing features */
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
390 if (rc
!= DCCP_FEAT_SP_NOAGREE
)
391 dccp_feat_empty_confirm(sk
, type
, feature
);
394 /* generate the confirm [if required] */
395 dccp_feat_flush_confirm(sk
);
400 EXPORT_SYMBOL_GPL(dccp_feat_change_recv
);
402 int dccp_feat_confirm_recv(struct sock
*sk
, u8 type
, u8 feature
,
406 struct dccp_opt_pend
*opt
;
407 struct dccp_minisock
*dmsk
= dccp_msk(sk
);
409 int all_confirmed
= 1;
411 dccp_pr_debug("got feat confirm type=%d feat=%d\n", type
, feature
);
413 /* XXX sanity check type & feat */
415 /* locate our change request */
416 t
= type
== DCCPO_CONFIRM_L
? DCCPO_CHANGE_R
: DCCPO_CHANGE_L
;
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
) {
422 /* XXX do sanity check */
424 opt
->dccpop_conf
= 1;
426 /* We got a confirmation---change the option */
427 dccp_feat_update(sk
, opt
->dccpop_type
,
428 opt
->dccpop_feat
, *val
);
430 dccp_pr_debug("feat %d type %d confirmed %d\n",
431 feature
, type
, *val
);
436 if (!opt
->dccpop_conf
)
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.
447 dccp_pr_debug("clear feat negotiation timer %p\n", sk
);
448 inet_csk_clear_xmit_timer(sk
, ICSK_TIME_RETRANS
);
452 dccp_pr_debug("feat %d type %d never requested\n",
457 EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv
);
459 void dccp_feat_clean(struct sock
*sk
)
461 struct dccp_minisock
*dmsk
= dccp_msk(sk
);
462 struct dccp_opt_pend
*opt
, *next
;
464 list_for_each_entry_safe(opt
, next
, &dmsk
->dccpms_pending
,
466 BUG_ON(opt
->dccpop_val
== NULL
);
467 kfree(opt
->dccpop_val
);
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
);
477 INIT_LIST_HEAD(&dmsk
->dccpms_pending
);
479 list_for_each_entry_safe(opt
, next
, &dmsk
->dccpms_conf
, dccpop_node
) {
481 if (opt
->dccpop_val
!= NULL
)
482 kfree(opt
->dccpop_val
);
485 INIT_LIST_HEAD(&dmsk
->dccpms_conf
);
488 EXPORT_SYMBOL_GPL(dccp_feat_clean
);
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
494 int dccp_feat_clone(struct sock
*oldsk
, struct sock
*newsk
)
496 struct dccp_minisock
*olddmsk
= dccp_msk(oldsk
);
497 struct dccp_minisock
*newdmsk
= dccp_msk(newsk
);
498 struct dccp_opt_pend
*opt
;
501 INIT_LIST_HEAD(&newdmsk
->dccpms_pending
);
502 INIT_LIST_HEAD(&newdmsk
->dccpms_conf
);
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
);
511 memcpy(val
, opt
->dccpop_val
, opt
->dccpop_len
);
513 newopt
= kmalloc(sizeof(*newopt
), GFP_ATOMIC
);
514 if (newopt
== NULL
) {
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
);
524 /* XXX what happens with backlogs and multiple connections at
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 */
530 /* reset state for a new socket */
531 opt
->dccpop_conf
= 0;
534 /* XXX not doing anything about the conf queue */
540 dccp_feat_clean(newsk
);
545 EXPORT_SYMBOL_GPL(dccp_feat_clone
);
547 static int __dccp_feat_init(struct sock
*sk
, u8 type
, u8 feat
, u8
*val
, u8 len
)
550 u8
*copy
= kmalloc(len
, GFP_KERNEL
);
553 memcpy(copy
, val
, len
);
554 rc
= dccp_feat_change(sk
, type
, feat
, copy
, len
, GFP_KERNEL
);
561 int dccp_feat_init(struct sock
*sk
)
563 struct dccp_minisock
*dmsk
= dccp_msk(sk
);
566 INIT_LIST_HEAD(&dmsk
->dccpms_pending
);
567 INIT_LIST_HEAD(&dmsk
->dccpms_conf
);
570 rc
= __dccp_feat_init(sk
, DCCPO_CHANGE_L
, DCCPF_CCID
,
571 &dmsk
->dccpms_tx_ccid
, 1);
576 rc
= __dccp_feat_init(sk
, DCCPO_CHANGE_R
, DCCPF_CCID
,
577 &dmsk
->dccpms_rx_ccid
, 1);
582 rc
= __dccp_feat_init(sk
, DCCPO_CHANGE_L
, DCCPF_ACK_RATIO
,
583 &dmsk
->dccpms_ack_ratio
, 1);
588 EXPORT_SYMBOL_GPL(dccp_feat_init
);