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