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_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
,
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");
39 BUG_ON(opt
->dccpop_val
== NULL
);
40 kfree(opt
->dccpop_val
);
41 opt
->dccpop_val
= val
;
42 opt
->dccpop_len
= len
;
48 /* negotiation for a new feature */
49 opt
= kmalloc(sizeof(*opt
), gfp
);
53 opt
->dccpop_type
= type
;
54 opt
->dccpop_feat
= feature
;
55 opt
->dccpop_len
= len
;
56 opt
->dccpop_val
= val
;
58 opt
->dccpop_sc
= NULL
;
60 BUG_ON(opt
->dccpop_val
== NULL
);
62 list_add_tail(&opt
->dccpop_node
, &dp
->dccps_options
.dccpo_pending
);
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
)
81 new_ccid
= ccid_new(new_ccid_nr
, sk
, rx
, GFP_ATOMIC
);
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
;
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
;
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
);
105 return dccp_feat_update_ccid(sk
, type
, val
);
107 dccp_pr_debug("IMPLEMENT changing [%d] feat %d to %d\n",
114 static int dccp_feat_reconcile(struct sock
*sk
, struct dccp_opt_pend
*opt
,
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
) {
127 rpref
= opt
->dccpop_val
;
128 rlen
= opt
->dccpop_len
;
130 spref
= opt
->dccpop_val
;
131 slen
= opt
->dccpop_len
;
134 * Now we have server preference list in spref and client preference in
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
]) {
155 /* we didn't agree on anything */
157 /* confirm previous value */
158 switch (opt
->dccpop_feat
) {
160 /* XXX did i get this right? =P */
161 if (opt
->dccpop_type
== DCCPO_CHANGE_L
)
162 res
= &dp
->dccps_options
.dccpo_tx_ccid
;
164 res
= &dp
->dccps_options
.dccpo_rx_ccid
;
168 WARN_ON(1); /* XXX implement res */
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
);
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
) {
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
);
207 kfree(opt
->dccpop_sc
->dccpoc_val
);
208 kfree(opt
->dccpop_sc
);
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
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
;
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
)
244 /* find our preference list for this feature */
245 list_for_each_entry(opt
, &dp
->dccps_options
.dccpo_pending
,
247 if (opt
->dccpop_type
!= t
|| opt
->dccpop_feat
!= feature
)
250 /* find the winner from the two preference lists */
251 rc
= dccp_feat_reconcile(sk
, opt
, val
, len
);
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.
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
);
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",
279 /* XXX sanity check opt val */
281 /* copy option so we can confirm it */
282 opt
= kzalloc(sizeof(*opt
), GFP_ATOMIC
);
286 copy
= kmalloc(len
, GFP_ATOMIC
);
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
;
299 rc
= dccp_feat_update(sk
, type
, feature
, *val
);
301 kfree(opt
->dccpop_val
);
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
);
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
);
319 /* XXX what do we do? Ignoring should be fine. It's a change
325 opt
->dccpop_type
= type
== DCCPO_CHANGE_L
? DCCPO_CONFIRM_R
:
327 opt
->dccpop_feat
= 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
);
343 struct dccp_opt_pend
*opt
;
345 list_for_each_entry(opt
, &dp
->dccps_options
.dccpo_pending
,
347 if (opt
->dccpop_conf
) {
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
)
363 int dccp_feat_change_recv(struct sock
*sk
, u8 type
, u8 feature
, u8
*val
, u8 len
)
367 dccp_pr_debug("got feat change type=%d feat=%d\n", type
, feature
);
369 /* figure out if it's SP or NN feature */
371 /* deal with SP features */
373 rc
= dccp_feat_sp(sk
, type
, feature
, val
, len
);
376 /* deal with NN features */
377 case DCCPF_ACK_RATIO
:
378 rc
= dccp_feat_nn(sk
, type
, feature
, val
, len
);
381 /* XXX implement other features */
387 /* check if there were problems changing features */
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
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
);
403 EXPORT_SYMBOL_GPL(dccp_feat_change_recv
);
405 int dccp_feat_confirm_recv(struct sock
*sk
, u8 type
, u8 feature
,
409 struct dccp_opt_pend
*opt
;
410 struct dccp_sock
*dp
= dccp_sk(sk
);
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
,
423 if (!opt
->dccpop_conf
&& opt
->dccpop_type
== t
&&
424 opt
->dccpop_feat
== feature
) {
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
);
440 if (!opt
->dccpop_conf
)
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.
451 dccp_pr_debug("clear feat negotiation timer %p\n", sk
);
452 inet_csk_clear_xmit_timer(sk
, ICSK_TIME_RETRANS
);
456 dccp_pr_debug("feat %d type %d never requested\n",
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
,
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
);
481 INIT_LIST_HEAD(&dp
->dccps_options
.dccpo_pending
);
483 list_for_each_entry_safe(opt
, next
, &dp
->dccps_options
.dccpo_conf
,
486 if (opt
->dccpop_val
!= NULL
)
487 kfree(opt
->dccpop_val
);
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
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
;
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
,
511 struct dccp_opt_pend
*newopt
;
512 /* copy the value of the option */
513 u8
*val
= kmalloc(opt
->dccpop_len
, GFP_ATOMIC
);
517 memcpy(val
, opt
->dccpop_val
, opt
->dccpop_len
);
519 newopt
= kmalloc(sizeof(*newopt
), GFP_ATOMIC
);
520 if (newopt
== NULL
) {
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
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 */
547 dccp_feat_clean(newsk
);
552 EXPORT_SYMBOL_GPL(dccp_feat_clone
);
554 static int __dccp_feat_init(struct sock
*sk
, u8 type
, u8 feat
, u8
*val
, u8 len
)
557 u8
*copy
= kmalloc(len
, GFP_KERNEL
);
560 memcpy(copy
, val
, len
);
561 rc
= dccp_feat_change(sk
, type
, feat
, copy
, len
, GFP_KERNEL
);
568 int dccp_feat_init(struct sock
*sk
)
570 struct dccp_sock
*dp
= dccp_sk(sk
);
573 INIT_LIST_HEAD(&dp
->dccps_options
.dccpo_pending
);
574 INIT_LIST_HEAD(&dp
->dccps_options
.dccpo_conf
);
577 rc
= __dccp_feat_init(sk
, DCCPO_CHANGE_L
, DCCPF_CCID
,
578 &dp
->dccps_options
.dccpo_tx_ccid
, 1);
583 rc
= __dccp_feat_init(sk
, DCCPO_CHANGE_R
, DCCPF_CCID
,
584 &dp
->dccps_options
.dccpo_rx_ccid
, 1);
589 rc
= __dccp_feat_init(sk
, DCCPO_CHANGE_L
, DCCPF_ACK_RATIO
,
590 &dp
->dccps_options
.dccpo_ack_ratio
, 1);
595 EXPORT_SYMBOL_GPL(dccp_feat_init
);