Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * net/dccp/feat.c |
| 3 | * |
| 4 | * An implementation of the DCCP protocol |
| 5 | * Andrea Bittau <a.bittau@cs.ucl.ac.uk> |
| 6 | * |
Gerrit Renker | 5cdae19 | 2007-12-13 12:41:46 -0200 | [diff] [blame] | 7 | * ASSUMPTIONS |
| 8 | * ----------- |
| 9 | * o All currently known SP features have 1-byte quantities. If in the future |
| 10 | * extensions of RFCs 4340..42 define features with item lengths larger than |
| 11 | * one byte, a feature-specific extension of the code will be required. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License |
| 15 | * as published by the Free Software Foundation; either version |
| 16 | * 2 of the License, or (at your option) any later version. |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 17 | */ |
| 18 | |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | |
Andrea Bittau | 6ffd30f | 2006-03-20 19:22:37 -0800 | [diff] [blame] | 21 | #include "ccid.h" |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 22 | #include "feat.h" |
| 23 | |
| 24 | #define DCCP_FEAT_SP_NOAGREE (-123) |
| 25 | |
Gerrit Renker | 5c7c945 | 2008-09-04 07:30:19 +0200 | [diff] [blame^] | 26 | /* copy constructor, fval must not already contain allocated memory */ |
| 27 | static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len) |
| 28 | { |
| 29 | fval->sp.len = len; |
| 30 | if (fval->sp.len > 0) { |
| 31 | fval->sp.vec = kmemdup(val, len, gfp_any()); |
| 32 | if (fval->sp.vec == NULL) { |
| 33 | fval->sp.len = 0; |
| 34 | return -ENOBUFS; |
| 35 | } |
| 36 | } |
| 37 | return 0; |
| 38 | } |
| 39 | |
Arnaldo Carvalho de Melo | 8ca0d17 | 2006-03-20 22:51:53 -0800 | [diff] [blame] | 40 | int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature, |
| 41 | u8 *val, u8 len, gfp_t gfp) |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 42 | { |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 43 | struct dccp_opt_pend *opt; |
| 44 | |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 45 | dccp_feat_debug(type, feature, *val); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 46 | |
Gerrit Renker | dd6303d | 2007-12-13 12:40:40 -0200 | [diff] [blame] | 47 | if (len > 3) { |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 48 | DCCP_WARN("invalid length %d\n", len); |
Chris Wright | 1944317 | 2008-05-05 13:50:24 -0700 | [diff] [blame] | 49 | return -EINVAL; |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 50 | } |
| 51 | /* XXX add further sanity checks */ |
Andrea Bittau | 6ffd30f | 2006-03-20 19:22:37 -0800 | [diff] [blame] | 52 | |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 53 | /* check if that feature is already being negotiated */ |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 54 | list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 55 | /* ok we found a negotiation for this option already */ |
| 56 | if (opt->dccpop_feat == feature && opt->dccpop_type == type) { |
| 57 | dccp_pr_debug("Replacing old\n"); |
| 58 | /* replace */ |
| 59 | BUG_ON(opt->dccpop_val == NULL); |
| 60 | kfree(opt->dccpop_val); |
| 61 | opt->dccpop_val = val; |
| 62 | opt->dccpop_len = len; |
| 63 | opt->dccpop_conf = 0; |
| 64 | return 0; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /* negotiation for a new feature */ |
| 69 | opt = kmalloc(sizeof(*opt), gfp); |
| 70 | if (opt == NULL) |
| 71 | return -ENOMEM; |
| 72 | |
| 73 | opt->dccpop_type = type; |
| 74 | opt->dccpop_feat = feature; |
| 75 | opt->dccpop_len = len; |
| 76 | opt->dccpop_val = val; |
| 77 | opt->dccpop_conf = 0; |
| 78 | opt->dccpop_sc = NULL; |
| 79 | |
| 80 | BUG_ON(opt->dccpop_val == NULL); |
| 81 | |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 82 | list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | EXPORT_SYMBOL_GPL(dccp_feat_change); |
| 87 | |
Andrea Bittau | 6ffd30f | 2006-03-20 19:22:37 -0800 | [diff] [blame] | 88 | static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr) |
| 89 | { |
| 90 | struct dccp_sock *dp = dccp_sk(sk); |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 91 | struct dccp_minisock *dmsk = dccp_msk(sk); |
Andrea Bittau | 6ffd30f | 2006-03-20 19:22:37 -0800 | [diff] [blame] | 92 | /* figure out if we are changing our CCID or the peer's */ |
| 93 | const int rx = type == DCCPO_CHANGE_R; |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 94 | const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid; |
Andrea Bittau | 6ffd30f | 2006-03-20 19:22:37 -0800 | [diff] [blame] | 95 | struct ccid *new_ccid; |
| 96 | |
| 97 | /* Check if nothing is being changed. */ |
| 98 | if (ccid_nr == new_ccid_nr) |
| 99 | return 0; |
| 100 | |
| 101 | new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC); |
| 102 | if (new_ccid == NULL) |
| 103 | return -ENOMEM; |
| 104 | |
| 105 | if (rx) { |
| 106 | ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); |
| 107 | dp->dccps_hc_rx_ccid = new_ccid; |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 108 | dmsk->dccpms_rx_ccid = new_ccid_nr; |
Andrea Bittau | 6ffd30f | 2006-03-20 19:22:37 -0800 | [diff] [blame] | 109 | } else { |
| 110 | ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); |
| 111 | dp->dccps_hc_tx_ccid = new_ccid; |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 112 | dmsk->dccpms_tx_ccid = new_ccid_nr; |
Andrea Bittau | 6ffd30f | 2006-03-20 19:22:37 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 118 | static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val) |
| 119 | { |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 120 | dccp_feat_debug(type, feat, val); |
Andrea Bittau | 6ffd30f | 2006-03-20 19:22:37 -0800 | [diff] [blame] | 121 | |
| 122 | switch (feat) { |
| 123 | case DCCPF_CCID: |
| 124 | return dccp_feat_update_ccid(sk, type, val); |
| 125 | default: |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 126 | dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n", |
| 127 | dccp_feat_typename(type), feat); |
Andrea Bittau | 6ffd30f | 2006-03-20 19:22:37 -0800 | [diff] [blame] | 128 | break; |
| 129 | } |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt, |
| 134 | u8 *rpref, u8 rlen) |
| 135 | { |
| 136 | struct dccp_sock *dp = dccp_sk(sk); |
| 137 | u8 *spref, slen, *res = NULL; |
| 138 | int i, j, rc, agree = 1; |
| 139 | |
| 140 | BUG_ON(rpref == NULL); |
| 141 | |
| 142 | /* check if we are the black sheep */ |
| 143 | if (dp->dccps_role == DCCP_ROLE_CLIENT) { |
| 144 | spref = rpref; |
| 145 | slen = rlen; |
| 146 | rpref = opt->dccpop_val; |
| 147 | rlen = opt->dccpop_len; |
| 148 | } else { |
| 149 | spref = opt->dccpop_val; |
| 150 | slen = opt->dccpop_len; |
| 151 | } |
| 152 | /* |
| 153 | * Now we have server preference list in spref and client preference in |
| 154 | * rpref |
| 155 | */ |
| 156 | BUG_ON(spref == NULL); |
| 157 | BUG_ON(rpref == NULL); |
| 158 | |
| 159 | /* FIXME sanity check vals */ |
| 160 | |
| 161 | /* Are values in any order? XXX Lame "algorithm" here */ |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 162 | for (i = 0; i < slen; i++) { |
| 163 | for (j = 0; j < rlen; j++) { |
| 164 | if (spref[i] == rpref[j]) { |
| 165 | res = &spref[i]; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | if (res) |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | /* we didn't agree on anything */ |
| 174 | if (res == NULL) { |
| 175 | /* confirm previous value */ |
| 176 | switch (opt->dccpop_feat) { |
| 177 | case DCCPF_CCID: |
| 178 | /* XXX did i get this right? =P */ |
| 179 | if (opt->dccpop_type == DCCPO_CHANGE_L) |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 180 | res = &dccp_msk(sk)->dccpms_tx_ccid; |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 181 | else |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 182 | res = &dccp_msk(sk)->dccpms_rx_ccid; |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 183 | break; |
| 184 | |
| 185 | default: |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 186 | DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat); |
| 187 | /* XXX implement res */ |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 188 | return -EFAULT; |
| 189 | } |
| 190 | |
| 191 | dccp_pr_debug("Don't agree... reconfirming %d\n", *res); |
| 192 | agree = 0; /* this is used for mandatory options... */ |
| 193 | } |
| 194 | |
| 195 | /* need to put result and our preference list */ |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 196 | rlen = 1 + opt->dccpop_len; |
| 197 | rpref = kmalloc(rlen, GFP_ATOMIC); |
| 198 | if (rpref == NULL) |
| 199 | return -ENOMEM; |
| 200 | |
| 201 | *rpref = *res; |
| 202 | memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len); |
| 203 | |
| 204 | /* put it in the "confirm queue" */ |
| 205 | if (opt->dccpop_sc == NULL) { |
| 206 | opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC); |
| 207 | if (opt->dccpop_sc == NULL) { |
| 208 | kfree(rpref); |
| 209 | return -ENOMEM; |
| 210 | } |
| 211 | } else { |
| 212 | /* recycle the confirm slot */ |
| 213 | BUG_ON(opt->dccpop_sc->dccpoc_val == NULL); |
| 214 | kfree(opt->dccpop_sc->dccpoc_val); |
| 215 | dccp_pr_debug("recycling confirm slot\n"); |
| 216 | } |
| 217 | memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc)); |
| 218 | |
| 219 | opt->dccpop_sc->dccpoc_val = rpref; |
| 220 | opt->dccpop_sc->dccpoc_len = rlen; |
| 221 | |
| 222 | /* update the option on our side [we are about to send the confirm] */ |
| 223 | rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res); |
| 224 | if (rc) { |
| 225 | kfree(opt->dccpop_sc->dccpoc_val); |
| 226 | kfree(opt->dccpop_sc); |
Randy Dunlap | 68907da | 2006-03-29 13:58:25 -0800 | [diff] [blame] | 227 | opt->dccpop_sc = NULL; |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 228 | return rc; |
| 229 | } |
| 230 | |
| 231 | dccp_pr_debug("Will confirm %d\n", *rpref); |
| 232 | |
| 233 | /* say we want to change to X but we just got a confirm X, suppress our |
| 234 | * change |
| 235 | */ |
| 236 | if (!opt->dccpop_conf) { |
| 237 | if (*opt->dccpop_val == *res) |
| 238 | opt->dccpop_conf = 1; |
| 239 | dccp_pr_debug("won't ask for change of same feature\n"); |
| 240 | } |
| 241 | |
| 242 | return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */ |
| 243 | } |
| 244 | |
| 245 | static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) |
| 246 | { |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 247 | struct dccp_minisock *dmsk = dccp_msk(sk); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 248 | struct dccp_opt_pend *opt; |
| 249 | int rc = 1; |
| 250 | u8 t; |
| 251 | |
| 252 | /* |
| 253 | * We received a CHANGE. We gotta match it against our own preference |
| 254 | * list. If we got a CHANGE_R it means it's a change for us, so we need |
| 255 | * to compare our CHANGE_L list. |
| 256 | */ |
| 257 | if (type == DCCPO_CHANGE_L) |
| 258 | t = DCCPO_CHANGE_R; |
| 259 | else |
| 260 | t = DCCPO_CHANGE_L; |
| 261 | |
| 262 | /* find our preference list for this feature */ |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 263 | list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 264 | if (opt->dccpop_type != t || opt->dccpop_feat != feature) |
| 265 | continue; |
| 266 | |
| 267 | /* find the winner from the two preference lists */ |
| 268 | rc = dccp_feat_reconcile(sk, opt, val, len); |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | /* We didn't deal with the change. This can happen if we have no |
| 273 | * preference list for the feature. In fact, it just shouldn't |
| 274 | * happen---if we understand a feature, we should have a preference list |
| 275 | * with at least the default value. |
| 276 | */ |
| 277 | BUG_ON(rc == 1); |
| 278 | |
| 279 | return rc; |
| 280 | } |
| 281 | |
| 282 | static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) |
| 283 | { |
| 284 | struct dccp_opt_pend *opt; |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 285 | struct dccp_minisock *dmsk = dccp_msk(sk); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 286 | u8 *copy; |
| 287 | int rc; |
| 288 | |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 289 | /* NN features must be Change L (sec. 6.3.2) */ |
| 290 | if (type != DCCPO_CHANGE_L) { |
| 291 | dccp_pr_debug("received %s for NN feature %d\n", |
| 292 | dccp_feat_typename(type), feature); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 293 | return -EFAULT; |
| 294 | } |
| 295 | |
| 296 | /* XXX sanity check opt val */ |
| 297 | |
| 298 | /* copy option so we can confirm it */ |
| 299 | opt = kzalloc(sizeof(*opt), GFP_ATOMIC); |
| 300 | if (opt == NULL) |
| 301 | return -ENOMEM; |
| 302 | |
Arnaldo Carvalho de Melo | eed7341 | 2006-11-17 12:21:43 -0200 | [diff] [blame] | 303 | copy = kmemdup(val, len, GFP_ATOMIC); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 304 | if (copy == NULL) { |
| 305 | kfree(opt); |
| 306 | return -ENOMEM; |
| 307 | } |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 308 | |
| 309 | opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */ |
| 310 | opt->dccpop_feat = feature; |
| 311 | opt->dccpop_val = copy; |
| 312 | opt->dccpop_len = len; |
| 313 | |
| 314 | /* change feature */ |
| 315 | rc = dccp_feat_update(sk, type, feature, *val); |
| 316 | if (rc) { |
| 317 | kfree(opt->dccpop_val); |
| 318 | kfree(opt); |
| 319 | return rc; |
| 320 | } |
| 321 | |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 322 | dccp_feat_debug(type, feature, *copy); |
| 323 | |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 324 | list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
Arnaldo Carvalho de Melo | 8ca0d17 | 2006-03-20 22:51:53 -0800 | [diff] [blame] | 329 | static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk, |
| 330 | u8 type, u8 feature) |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 331 | { |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 332 | /* XXX check if other confirms for that are queued and recycle slot */ |
| 333 | struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC); |
| 334 | |
| 335 | if (opt == NULL) { |
| 336 | /* XXX what do we do? Ignoring should be fine. It's a change |
| 337 | * after all =P |
| 338 | */ |
| 339 | return; |
| 340 | } |
| 341 | |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 342 | switch (type) { |
Jesper Juhl | e576de8 | 2007-08-10 15:23:54 -0700 | [diff] [blame] | 343 | case DCCPO_CHANGE_L: |
| 344 | opt->dccpop_type = DCCPO_CONFIRM_R; |
| 345 | break; |
| 346 | case DCCPO_CHANGE_R: |
| 347 | opt->dccpop_type = DCCPO_CONFIRM_L; |
| 348 | break; |
| 349 | default: |
| 350 | DCCP_WARN("invalid type %d\n", type); |
| 351 | kfree(opt); |
| 352 | return; |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 353 | } |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 354 | opt->dccpop_feat = feature; |
Randy Dunlap | 68907da | 2006-03-29 13:58:25 -0800 | [diff] [blame] | 355 | opt->dccpop_val = NULL; |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 356 | opt->dccpop_len = 0; |
| 357 | |
| 358 | /* change feature */ |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 359 | dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature); |
| 360 | |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 361 | list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static void dccp_feat_flush_confirm(struct sock *sk) |
| 365 | { |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 366 | struct dccp_minisock *dmsk = dccp_msk(sk); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 367 | /* Check if there is anything to confirm in the first place */ |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 368 | int yes = !list_empty(&dmsk->dccpms_conf); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 369 | |
| 370 | if (!yes) { |
| 371 | struct dccp_opt_pend *opt; |
| 372 | |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 373 | list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 374 | if (opt->dccpop_conf) { |
| 375 | yes = 1; |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if (!yes) |
| 382 | return; |
| 383 | |
| 384 | /* OK there is something to confirm... */ |
| 385 | /* XXX check if packet is in flight? Send delayed ack?? */ |
| 386 | if (sk->sk_state == DCCP_OPEN) |
| 387 | dccp_send_ack(sk); |
| 388 | } |
| 389 | |
| 390 | int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) |
| 391 | { |
| 392 | int rc; |
| 393 | |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 394 | dccp_feat_debug(type, feature, *val); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 395 | |
| 396 | /* figure out if it's SP or NN feature */ |
| 397 | switch (feature) { |
| 398 | /* deal with SP features */ |
| 399 | case DCCPF_CCID: |
| 400 | rc = dccp_feat_sp(sk, type, feature, val, len); |
| 401 | break; |
| 402 | |
| 403 | /* deal with NN features */ |
| 404 | case DCCPF_ACK_RATIO: |
| 405 | rc = dccp_feat_nn(sk, type, feature, val, len); |
| 406 | break; |
| 407 | |
| 408 | /* XXX implement other features */ |
| 409 | default: |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 410 | dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n", |
| 411 | dccp_feat_typename(type), feature); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 412 | rc = -EFAULT; |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | /* check if there were problems changing features */ |
| 417 | if (rc) { |
| 418 | /* If we don't agree on SP, we sent a confirm for old value. |
| 419 | * However we propagate rc to caller in case option was |
| 420 | * mandatory |
| 421 | */ |
| 422 | if (rc != DCCP_FEAT_SP_NOAGREE) |
Arnaldo Carvalho de Melo | 8ca0d17 | 2006-03-20 22:51:53 -0800 | [diff] [blame] | 423 | dccp_feat_empty_confirm(dccp_msk(sk), type, feature); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* generate the confirm [if required] */ |
| 427 | dccp_feat_flush_confirm(sk); |
| 428 | |
| 429 | return rc; |
| 430 | } |
| 431 | |
| 432 | EXPORT_SYMBOL_GPL(dccp_feat_change_recv); |
| 433 | |
| 434 | int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature, |
| 435 | u8 *val, u8 len) |
| 436 | { |
| 437 | u8 t; |
| 438 | struct dccp_opt_pend *opt; |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 439 | struct dccp_minisock *dmsk = dccp_msk(sk); |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 440 | int found = 0; |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 441 | int all_confirmed = 1; |
| 442 | |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 443 | dccp_feat_debug(type, feature, *val); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 444 | |
| 445 | /* locate our change request */ |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 446 | switch (type) { |
| 447 | case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break; |
| 448 | case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break; |
Arnaldo Carvalho de Melo | 8109b02 | 2006-12-10 16:01:18 -0200 | [diff] [blame] | 449 | default: DCCP_WARN("invalid type %d\n", type); |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 450 | return 1; |
| 451 | |
| 452 | } |
| 453 | /* XXX sanity check feature value */ |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 454 | |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 455 | list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 456 | if (!opt->dccpop_conf && opt->dccpop_type == t && |
| 457 | opt->dccpop_feat == feature) { |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 458 | found = 1; |
| 459 | dccp_pr_debug("feature %d found\n", opt->dccpop_feat); |
| 460 | |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 461 | /* XXX do sanity check */ |
| 462 | |
| 463 | opt->dccpop_conf = 1; |
| 464 | |
| 465 | /* We got a confirmation---change the option */ |
| 466 | dccp_feat_update(sk, opt->dccpop_type, |
| 467 | opt->dccpop_feat, *val); |
| 468 | |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 469 | /* XXX check the return value of dccp_feat_update */ |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 470 | break; |
| 471 | } |
| 472 | |
| 473 | if (!opt->dccpop_conf) |
| 474 | all_confirmed = 0; |
| 475 | } |
| 476 | |
| 477 | /* fix re-transmit timer */ |
| 478 | /* XXX gotta make sure that no option negotiation occurs during |
| 479 | * connection shutdown. Consider that the CLOSEREQ is sent and timer is |
| 480 | * on. if all options are confirmed it might kill timer which should |
| 481 | * remain alive until close is received. |
| 482 | */ |
| 483 | if (all_confirmed) { |
| 484 | dccp_pr_debug("clear feat negotiation timer %p\n", sk); |
| 485 | inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS); |
| 486 | } |
| 487 | |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 488 | if (!found) |
| 489 | dccp_pr_debug("%s(%d, ...) never requested\n", |
| 490 | dccp_feat_typename(type), feature); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv); |
| 495 | |
Arnaldo Carvalho de Melo | 8ca0d17 | 2006-03-20 22:51:53 -0800 | [diff] [blame] | 496 | void dccp_feat_clean(struct dccp_minisock *dmsk) |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 497 | { |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 498 | struct dccp_opt_pend *opt, *next; |
| 499 | |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 500 | list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending, |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 501 | dccpop_node) { |
YOSHIFUJI Hideaki | c9eaf17 | 2007-02-09 23:24:38 +0900 | [diff] [blame] | 502 | BUG_ON(opt->dccpop_val == NULL); |
| 503 | kfree(opt->dccpop_val); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 504 | |
| 505 | if (opt->dccpop_sc != NULL) { |
| 506 | BUG_ON(opt->dccpop_sc->dccpoc_val == NULL); |
| 507 | kfree(opt->dccpop_sc->dccpoc_val); |
| 508 | kfree(opt->dccpop_sc); |
| 509 | } |
| 510 | |
YOSHIFUJI Hideaki | c9eaf17 | 2007-02-09 23:24:38 +0900 | [diff] [blame] | 511 | kfree(opt); |
| 512 | } |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 513 | INIT_LIST_HEAD(&dmsk->dccpms_pending); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 514 | |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 515 | list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) { |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 516 | BUG_ON(opt == NULL); |
| 517 | if (opt->dccpop_val != NULL) |
| 518 | kfree(opt->dccpop_val); |
| 519 | kfree(opt); |
| 520 | } |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 521 | INIT_LIST_HEAD(&dmsk->dccpms_conf); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | EXPORT_SYMBOL_GPL(dccp_feat_clean); |
| 525 | |
| 526 | /* this is to be called only when a listening sock creates its child. It is |
| 527 | * assumed by the function---the confirm is not duplicated, but rather it is |
| 528 | * "passed on". |
| 529 | */ |
| 530 | int dccp_feat_clone(struct sock *oldsk, struct sock *newsk) |
| 531 | { |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 532 | struct dccp_minisock *olddmsk = dccp_msk(oldsk); |
| 533 | struct dccp_minisock *newdmsk = dccp_msk(newsk); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 534 | struct dccp_opt_pend *opt; |
| 535 | int rc = 0; |
| 536 | |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 537 | INIT_LIST_HEAD(&newdmsk->dccpms_pending); |
| 538 | INIT_LIST_HEAD(&newdmsk->dccpms_conf); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 539 | |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 540 | list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) { |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 541 | struct dccp_opt_pend *newopt; |
| 542 | /* copy the value of the option */ |
Arnaldo Carvalho de Melo | eed7341 | 2006-11-17 12:21:43 -0200 | [diff] [blame] | 543 | u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 544 | |
| 545 | if (val == NULL) |
| 546 | goto out_clean; |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 547 | |
Arnaldo Carvalho de Melo | eed7341 | 2006-11-17 12:21:43 -0200 | [diff] [blame] | 548 | newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 549 | if (newopt == NULL) { |
| 550 | kfree(val); |
| 551 | goto out_clean; |
| 552 | } |
| 553 | |
| 554 | /* insert the option */ |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 555 | newopt->dccpop_val = val; |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 556 | list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 557 | |
| 558 | /* XXX what happens with backlogs and multiple connections at |
| 559 | * once... |
| 560 | */ |
| 561 | /* the master socket no longer needs to worry about confirms */ |
Randy Dunlap | 68907da | 2006-03-29 13:58:25 -0800 | [diff] [blame] | 562 | opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */ |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 563 | |
| 564 | /* reset state for a new socket */ |
| 565 | opt->dccpop_conf = 0; |
| 566 | } |
| 567 | |
| 568 | /* XXX not doing anything about the conf queue */ |
| 569 | |
| 570 | out: |
| 571 | return rc; |
| 572 | |
| 573 | out_clean: |
Arnaldo Carvalho de Melo | 8ca0d17 | 2006-03-20 22:51:53 -0800 | [diff] [blame] | 574 | dccp_feat_clean(newdmsk); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 575 | rc = -ENOMEM; |
| 576 | goto out; |
| 577 | } |
| 578 | |
| 579 | EXPORT_SYMBOL_GPL(dccp_feat_clone); |
| 580 | |
Arnaldo Carvalho de Melo | 8ca0d17 | 2006-03-20 22:51:53 -0800 | [diff] [blame] | 581 | static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat, |
| 582 | u8 *val, u8 len) |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 583 | { |
| 584 | int rc = -ENOMEM; |
Arnaldo Carvalho de Melo | eed7341 | 2006-11-17 12:21:43 -0200 | [diff] [blame] | 585 | u8 *copy = kmemdup(val, len, GFP_KERNEL); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 586 | |
| 587 | if (copy != NULL) { |
Arnaldo Carvalho de Melo | 8ca0d17 | 2006-03-20 22:51:53 -0800 | [diff] [blame] | 588 | rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 589 | if (rc) |
| 590 | kfree(copy); |
| 591 | } |
| 592 | return rc; |
| 593 | } |
| 594 | |
Arnaldo Carvalho de Melo | 8ca0d17 | 2006-03-20 22:51:53 -0800 | [diff] [blame] | 595 | int dccp_feat_init(struct dccp_minisock *dmsk) |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 596 | { |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 597 | int rc; |
| 598 | |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 599 | INIT_LIST_HEAD(&dmsk->dccpms_pending); |
| 600 | INIT_LIST_HEAD(&dmsk->dccpms_conf); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 601 | |
| 602 | /* CCID L */ |
Arnaldo Carvalho de Melo | 8ca0d17 | 2006-03-20 22:51:53 -0800 | [diff] [blame] | 603 | rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID, |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 604 | &dmsk->dccpms_tx_ccid, 1); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 605 | if (rc) |
| 606 | goto out; |
| 607 | |
| 608 | /* CCID R */ |
Arnaldo Carvalho de Melo | 8ca0d17 | 2006-03-20 22:51:53 -0800 | [diff] [blame] | 609 | rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID, |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 610 | &dmsk->dccpms_rx_ccid, 1); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 611 | if (rc) |
| 612 | goto out; |
| 613 | |
| 614 | /* Ack ratio */ |
Arnaldo Carvalho de Melo | 8ca0d17 | 2006-03-20 22:51:53 -0800 | [diff] [blame] | 615 | rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO, |
Arnaldo Carvalho de Melo | a4bf390 | 2006-03-20 22:50:58 -0800 | [diff] [blame] | 616 | &dmsk->dccpms_ack_ratio, 1); |
Andrea Bittau | afe0025 | 2006-03-20 17:43:56 -0800 | [diff] [blame] | 617 | out: |
| 618 | return rc; |
| 619 | } |
| 620 | |
| 621 | EXPORT_SYMBOL_GPL(dccp_feat_init); |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 622 | |
| 623 | #ifdef CONFIG_IP_DCCP_DEBUG |
| 624 | const char *dccp_feat_typename(const u8 type) |
| 625 | { |
| 626 | switch(type) { |
| 627 | case DCCPO_CHANGE_L: return("ChangeL"); |
| 628 | case DCCPO_CONFIRM_L: return("ConfirmL"); |
| 629 | case DCCPO_CHANGE_R: return("ChangeR"); |
| 630 | case DCCPO_CONFIRM_R: return("ConfirmR"); |
| 631 | /* the following case must not appear in feature negotation */ |
Arnaldo Carvalho de Melo | 8109b02 | 2006-12-10 16:01:18 -0200 | [diff] [blame] | 632 | default: dccp_pr_debug("unknown type %d [BUG!]\n", type); |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 633 | } |
| 634 | return NULL; |
| 635 | } |
| 636 | |
| 637 | EXPORT_SYMBOL_GPL(dccp_feat_typename); |
| 638 | |
| 639 | const char *dccp_feat_name(const u8 feat) |
| 640 | { |
| 641 | static const char *feature_names[] = { |
| 642 | [DCCPF_RESERVED] = "Reserved", |
| 643 | [DCCPF_CCID] = "CCID", |
| 644 | [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos", |
| 645 | [DCCPF_SEQUENCE_WINDOW] = "Sequence Window", |
| 646 | [DCCPF_ECN_INCAPABLE] = "ECN Incapable", |
| 647 | [DCCPF_ACK_RATIO] = "Ack Ratio", |
| 648 | [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector", |
| 649 | [DCCPF_SEND_NDP_COUNT] = "Send NDP Count", |
| 650 | [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage", |
| 651 | [DCCPF_DATA_CHECKSUM] = "Send Data Checksum", |
| 652 | }; |
Gerrit Renker | dd6303d | 2007-12-13 12:40:40 -0200 | [diff] [blame] | 653 | if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC) |
| 654 | return feature_names[DCCPF_RESERVED]; |
| 655 | |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 656 | if (feat >= DCCPF_MIN_CCID_SPECIFIC) |
| 657 | return "CCID-specific"; |
| 658 | |
Gerrit Renker | c02fdc0 | 2006-11-14 12:48:10 -0200 | [diff] [blame] | 659 | return feature_names[feat]; |
| 660 | } |
| 661 | |
| 662 | EXPORT_SYMBOL_GPL(dccp_feat_name); |
| 663 | #endif /* CONFIG_IP_DCCP_DEBUG */ |