aboutsummaryrefslogtreecommitdiffstats
path: root/net/dccp/feat.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/dccp/feat.c')
-rw-r--r--net/dccp/feat.c1805
1 files changed, 1344 insertions, 461 deletions
diff --git a/net/dccp/feat.c b/net/dccp/feat.c
index 933a0ecf8d46..f94c7c9d1a7f 100644
--- a/net/dccp/feat.c
+++ b/net/dccp/feat.c
@@ -1,11 +1,19 @@
1/* 1/*
2 * net/dccp/feat.c 2 * net/dccp/feat.c
3 * 3 *
4 * An implementation of the DCCP protocol 4 * Feature negotiation for the DCCP protocol (RFC 4340, section 6)
5 * Andrea Bittau <a.bittau@cs.ucl.ac.uk> 5 *
6 * Copyright (c) 2008 The University of Aberdeen, Scotland, UK
7 * Copyright (c) 2008 Gerrit Renker <gerrit@erg.abdn.ac.uk>
8 * Rewrote from scratch, some bits from earlier code by
9 * Copyright (c) 2005 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
10 *
6 * 11 *
7 * ASSUMPTIONS 12 * ASSUMPTIONS
8 * ----------- 13 * -----------
14 * o Feature negotiation is coordinated with connection setup (as in TCP), wild
15 * changes of parameters of an established connection are not supported.
16 * o Changing NN values (Ack Ratio only) is supported in state OPEN/PARTOPEN.
9 * o All currently known SP features have 1-byte quantities. If in the future 17 * 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 18 * 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. 19 * one byte, a feature-specific extension of the code will be required.
@@ -15,635 +23,1510 @@
15 * as published by the Free Software Foundation; either version 23 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version. 24 * 2 of the License, or (at your option) any later version.
17 */ 25 */
18
19#include <linux/module.h> 26#include <linux/module.h>
20
21#include "ccid.h" 27#include "ccid.h"
22#include "feat.h" 28#include "feat.h"
23 29
24#define DCCP_FEAT_SP_NOAGREE (-123) 30/* feature-specific sysctls - initialised to the defaults from RFC 4340, 6.4 */
25 31unsigned long sysctl_dccp_sequence_window __read_mostly = 100;
26int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature, 32int sysctl_dccp_rx_ccid __read_mostly = 2,
27 u8 *val, u8 len, gfp_t gfp) 33 sysctl_dccp_tx_ccid __read_mostly = 2;
28{
29 struct dccp_opt_pend *opt;
30
31 dccp_feat_debug(type, feature, *val);
32
33 if (len > 3) {
34 DCCP_WARN("invalid length %d\n", len);
35 return -EINVAL;
36 }
37 /* XXX add further sanity checks */
38
39 /* check if that feature is already being negotiated */
40 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
41 /* ok we found a negotiation for this option already */
42 if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
43 dccp_pr_debug("Replacing old\n");
44 /* replace */
45 BUG_ON(opt->dccpop_val == NULL);
46 kfree(opt->dccpop_val);
47 opt->dccpop_val = val;
48 opt->dccpop_len = len;
49 opt->dccpop_conf = 0;
50 return 0;
51 }
52 }
53
54 /* negotiation for a new feature */
55 opt = kmalloc(sizeof(*opt), gfp);
56 if (opt == NULL)
57 return -ENOMEM;
58
59 opt->dccpop_type = type;
60 opt->dccpop_feat = feature;
61 opt->dccpop_len = len;
62 opt->dccpop_val = val;
63 opt->dccpop_conf = 0;
64 opt->dccpop_sc = NULL;
65
66 BUG_ON(opt->dccpop_val == NULL);
67
68 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
69 return 0;
70}
71 34
72EXPORT_SYMBOL_GPL(dccp_feat_change); 35/*
73 36 * Feature activation handlers.
74static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr) 37 *
38 * These all use an u64 argument, to provide enough room for NN/SP features. At
39 * this stage the negotiated values have been checked to be within their range.
40 */
41static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
75{ 42{
76 struct dccp_sock *dp = dccp_sk(sk); 43 struct dccp_sock *dp = dccp_sk(sk);
77 struct dccp_minisock *dmsk = dccp_msk(sk); 44 struct ccid *new_ccid = ccid_new(ccid, sk, rx, gfp_any());
78 /* figure out if we are changing our CCID or the peer's */
79 const int rx = type == DCCPO_CHANGE_R;
80 const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
81 struct ccid *new_ccid;
82
83 /* Check if nothing is being changed. */
84 if (ccid_nr == new_ccid_nr)
85 return 0;
86 45
87 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
88 if (new_ccid == NULL) 46 if (new_ccid == NULL)
89 return -ENOMEM; 47 return -ENOMEM;
90 48
91 if (rx) { 49 if (rx) {
92 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 50 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
93 dp->dccps_hc_rx_ccid = new_ccid; 51 dp->dccps_hc_rx_ccid = new_ccid;
94 dmsk->dccpms_rx_ccid = new_ccid_nr;
95 } else { 52 } else {
96 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 53 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
97 dp->dccps_hc_tx_ccid = new_ccid; 54 dp->dccps_hc_tx_ccid = new_ccid;
98 dmsk->dccpms_tx_ccid = new_ccid_nr;
99 } 55 }
100
101 return 0; 56 return 0;
102} 57}
103 58
104static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val) 59static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
105{ 60{
106 dccp_feat_debug(type, feat, val); 61 struct dccp_sock *dp = dccp_sk(sk);
107 62
108 switch (feat) { 63 if (rx) {
109 case DCCPF_CCID: 64 dp->dccps_r_seq_win = seq_win;
110 return dccp_feat_update_ccid(sk, type, val); 65 /* propagate changes to update SWL/SWH */
111 default: 66 dccp_update_gsr(sk, dp->dccps_gsr);
112 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n", 67 } else {
113 dccp_feat_typename(type), feat); 68 dp->dccps_l_seq_win = seq_win;
114 break; 69 /* propagate changes to update AWL */
70 dccp_update_gss(sk, dp->dccps_gss);
115 } 71 }
116 return 0; 72 return 0;
117} 73}
118 74
119static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt, 75static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
120 u8 *rpref, u8 rlen) 76{
77#ifndef __CCID2_COPES_GRACEFULLY_WITH_DYNAMIC_ACK_RATIO_UPDATES__
78 /*
79 * FIXME: This is required until several problems in the CCID-2 code are
80 * resolved. The CCID-2 code currently does not cope well; using dynamic
81 * Ack Ratios greater than 1 caused instabilities. These were manifest
82 * in hangups and long RTO timeouts (1...3 seconds). Until this has been
83 * stabilised, it is safer not to activate dynamic Ack Ratio changes.
84 */
85 dccp_pr_debug("Not changing %s Ack Ratio from 1 to %u\n",
86 rx ? "RX" : "TX", (u16)ratio);
87 ratio = 1;
88#endif
89 if (rx)
90 dccp_sk(sk)->dccps_r_ack_ratio = ratio;
91 else
92 dccp_sk(sk)->dccps_l_ack_ratio = ratio;
93 return 0;
94}
95
96static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
121{ 97{
122 struct dccp_sock *dp = dccp_sk(sk); 98 struct dccp_sock *dp = dccp_sk(sk);
123 u8 *spref, slen, *res = NULL;
124 int i, j, rc, agree = 1;
125 99
126 BUG_ON(rpref == NULL); 100 if (rx) {
101 if (enable && dp->dccps_hc_rx_ackvec == NULL) {
102 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
103 if (dp->dccps_hc_rx_ackvec == NULL)
104 return -ENOMEM;
105 } else if (!enable) {
106 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
107 dp->dccps_hc_rx_ackvec = NULL;
108 }
109 }
110 return 0;
111}
127 112
128 /* check if we are the black sheep */ 113static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
129 if (dp->dccps_role == DCCP_ROLE_CLIENT) { 114{
130 spref = rpref; 115 if (!rx)
131 slen = rlen; 116 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
132 rpref = opt->dccpop_val; 117 return 0;
133 rlen = opt->dccpop_len; 118}
134 } else { 119
135 spref = opt->dccpop_val; 120/*
136 slen = opt->dccpop_len; 121 * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
122 * `rx' holds when the sending peer informs about his partial coverage via a
123 * ChangeR() option. In the other case, we are the sender and the receiver
124 * announces its coverage via ChangeL() options. The policy here is to honour
125 * such communication by enabling the corresponding partial coverage - but only
126 * if it has not been set manually before; the warning here means that all
127 * packets will be dropped.
128 */
129static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
130{
131 struct dccp_sock *dp = dccp_sk(sk);
132
133 if (rx)
134 dp->dccps_pcrlen = cscov;
135 else {
136 if (dp->dccps_pcslen == 0)
137 dp->dccps_pcslen = cscov;
138 else if (cscov > dp->dccps_pcslen)
139 DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
140 dp->dccps_pcslen, (u8)cscov);
137 } 141 }
142 return 0;
143}
144
145static const struct {
146 u8 feat_num; /* DCCPF_xxx */
147 enum dccp_feat_type rxtx; /* RX or TX */
148 enum dccp_feat_type reconciliation; /* SP or NN */
149 u8 default_value; /* as in 6.4 */
150 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
151/*
152 * Lookup table for location and type of features (from RFC 4340/4342)
153 * +--------------------------+----+-----+----+----+---------+-----------+
154 * | Feature | Location | Reconc. | Initial | Section |
155 * | | RX | TX | SP | NN | Value | Reference |
156 * +--------------------------+----+-----+----+----+---------+-----------+
157 * | DCCPF_CCID | | X | X | | 2 | 10 |
158 * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
159 * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
160 * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
161 * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
162 * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
163 * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
164 * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
165 * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
166 * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
167 * +--------------------------+----+-----+----+----+---------+-----------+
168 */
169} dccp_feat_table[] = {
170 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
171 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
172 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
173 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
174 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
175 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
176 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
177 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
178 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
179 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
180};
181#define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
182
183/**
184 * dccp_feat_index - Hash function to map feature number into array position
185 * Returns consecutive array index or -1 if the feature is not understood.
186 */
187static int dccp_feat_index(u8 feat_num)
188{
189 /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
190 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
191 return feat_num - 1;
192
138 /* 193 /*
139 * Now we have server preference list in spref and client preference in 194 * Other features: add cases for new feature types here after adding
140 * rpref 195 * them to the above table.
141 */ 196 */
142 BUG_ON(spref == NULL); 197 switch (feat_num) {
143 BUG_ON(rpref == NULL); 198 case DCCPF_SEND_LEV_RATE:
199 return DCCP_FEAT_SUPPORTED_MAX - 1;
200 }
201 return -1;
202}
144 203
145 /* FIXME sanity check vals */ 204static u8 dccp_feat_type(u8 feat_num)
205{
206 int idx = dccp_feat_index(feat_num);
146 207
147 /* Are values in any order? XXX Lame "algorithm" here */ 208 if (idx < 0)
148 for (i = 0; i < slen; i++) { 209 return FEAT_UNKNOWN;
149 for (j = 0; j < rlen; j++) { 210 return dccp_feat_table[idx].reconciliation;
150 if (spref[i] == rpref[j]) { 211}
151 res = &spref[i];
152 break;
153 }
154 }
155 if (res)
156 break;
157 }
158 212
159 /* we didn't agree on anything */ 213static int dccp_feat_default_value(u8 feat_num)
160 if (res == NULL) { 214{
161 /* confirm previous value */ 215 int idx = dccp_feat_index(feat_num);
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)
166 res = &dccp_msk(sk)->dccpms_tx_ccid;
167 else
168 res = &dccp_msk(sk)->dccpms_rx_ccid;
169 break;
170 216
171 default: 217 return idx < 0 ? : dccp_feat_table[idx].default_value;
172 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat); 218}
173 /* XXX implement res */
174 return -EFAULT;
175 }
176 219
177 dccp_pr_debug("Don't agree... reconfirming %d\n", *res); 220/*
178 agree = 0; /* this is used for mandatory options... */ 221 * Debugging and verbose-printing section
222 */
223static const char *dccp_feat_fname(const u8 feat)
224{
225 static const char *feature_names[] = {
226 [DCCPF_RESERVED] = "Reserved",
227 [DCCPF_CCID] = "CCID",
228 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
229 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
230 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
231 [DCCPF_ACK_RATIO] = "Ack Ratio",
232 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
233 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
234 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
235 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
236 };
237 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
238 return feature_names[DCCPF_RESERVED];
239
240 if (feat == DCCPF_SEND_LEV_RATE)
241 return "Send Loss Event Rate";
242 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
243 return "CCID-specific";
244
245 return feature_names[feat];
246}
247
248static const char *dccp_feat_sname[] = { "DEFAULT", "INITIALISING", "CHANGING",
249 "UNSTABLE", "STABLE" };
250
251#ifdef CONFIG_IP_DCCP_DEBUG
252static const char *dccp_feat_oname(const u8 opt)
253{
254 switch (opt) {
255 case DCCPO_CHANGE_L: return "Change_L";
256 case DCCPO_CONFIRM_L: return "Confirm_L";
257 case DCCPO_CHANGE_R: return "Change_R";
258 case DCCPO_CONFIRM_R: return "Confirm_R";
179 } 259 }
260 return NULL;
261}
180 262
181 /* need to put result and our preference list */ 263static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val)
182 rlen = 1 + opt->dccpop_len; 264{
183 rpref = kmalloc(rlen, GFP_ATOMIC); 265 u8 i, type = dccp_feat_type(feat_num);
184 if (rpref == NULL) 266
185 return -ENOMEM; 267 if (val == NULL || (type == FEAT_SP && val->sp.vec == NULL))
268 dccp_pr_debug_cat("(NULL)");
269 else if (type == FEAT_SP)
270 for (i = 0; i < val->sp.len; i++)
271 dccp_pr_debug_cat("%s%u", i ? " " : "", val->sp.vec[i]);
272 else if (type == FEAT_NN)
273 dccp_pr_debug_cat("%llu", (unsigned long long)val->nn);
274 else
275 dccp_pr_debug_cat("unknown type %u", type);
276}
277
278static void dccp_feat_printvals(u8 feat_num, u8 *list, u8 len)
279{
280 u8 type = dccp_feat_type(feat_num);
281 dccp_feat_val fval = { .sp.vec = list, .sp.len = len };
282
283 if (type == FEAT_NN)
284 fval.nn = dccp_decode_value_var(list, len);
285 dccp_feat_printval(feat_num, &fval);
286}
287
288static void dccp_feat_print_entry(struct dccp_feat_entry const *entry)
289{
290 dccp_debug(" * %s %s = ", entry->is_local ? "local" : "remote",
291 dccp_feat_fname(entry->feat_num));
292 dccp_feat_printval(entry->feat_num, &entry->val);
293 dccp_pr_debug_cat(", state=%s %s\n", dccp_feat_sname[entry->state],
294 entry->needs_confirm ? "(Confirm pending)" : "");
295}
296
297#define dccp_feat_print_opt(opt, feat, val, len, mandatory) do { \
298 dccp_pr_debug("%s(%s, ", dccp_feat_oname(opt), dccp_feat_fname(feat));\
299 dccp_feat_printvals(feat, val, len); \
300 dccp_pr_debug_cat(") %s\n", mandatory ? "!" : ""); } while (0)
301
302#define dccp_feat_print_fnlist(fn_list) { \
303 const struct dccp_feat_entry *___entry; \
304 \
305 dccp_pr_debug("List Dump:\n"); \
306 list_for_each_entry(___entry, fn_list, node) \
307 dccp_feat_print_entry(___entry); \
308}
309#else /* ! CONFIG_IP_DCCP_DEBUG */
310#define dccp_feat_print_opt(opt, feat, val, len, mandatory)
311#define dccp_feat_print_fnlist(fn_list)
312#endif
186 313
187 *rpref = *res; 314static int __dccp_feat_activate(struct sock *sk, const int idx,
188 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len); 315 const bool is_local, dccp_feat_val const *fval)
316{
317 bool rx;
318 u64 val;
319
320 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
321 return -1;
322 if (dccp_feat_table[idx].activation_hdlr == NULL)
323 return 0;
189 324
190 /* put it in the "confirm queue" */ 325 if (fval == NULL) {
191 if (opt->dccpop_sc == NULL) { 326 val = dccp_feat_table[idx].default_value;
192 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC); 327 } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
193 if (opt->dccpop_sc == NULL) { 328 if (fval->sp.vec == NULL) {
194 kfree(rpref); 329 /*
195 return -ENOMEM; 330 * This can happen when an empty Confirm is sent
331 * for an SP (i.e. known) feature. In this case
332 * we would be using the default anyway.
333 */
334 DCCP_CRIT("Feature #%d undefined: using default", idx);
335 val = dccp_feat_table[idx].default_value;
336 } else {
337 val = fval->sp.vec[0];
196 } 338 }
197 } else { 339 } else {
198 /* recycle the confirm slot */ 340 val = fval->nn;
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);
213 opt->dccpop_sc = NULL;
214 return rc;
215 } 341 }
216 342
217 dccp_pr_debug("Will confirm %d\n", *rpref); 343 /* Location is RX if this is a local-RX or remote-TX feature */
344 rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
218 345
219 /* say we want to change to X but we just got a confirm X, suppress our 346 dccp_debug(" -> activating %s %s, %sval=%llu\n", rx ? "RX" : "TX",
220 * change 347 dccp_feat_fname(dccp_feat_table[idx].feat_num),
221 */ 348 fval ? "" : "default ", (unsigned long long)val);
222 if (!opt->dccpop_conf) { 349
223 if (*opt->dccpop_val == *res) 350 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
224 opt->dccpop_conf = 1; 351}
225 dccp_pr_debug("won't ask for change of same feature\n"); 352
353/**
354 * dccp_feat_activate - Activate feature value on socket
355 * @sk: fully connected DCCP socket (after handshake is complete)
356 * @feat_num: feature to activate, one of %dccp_feature_numbers
357 * @local: whether local (1) or remote (0) @feat_num is meant
358 * @fval: the value (SP or NN) to activate, or NULL to use the default value
359 * For general use this function is preferable over __dccp_feat_activate().
360 */
361static int dccp_feat_activate(struct sock *sk, u8 feat_num, bool local,
362 dccp_feat_val const *fval)
363{
364 return __dccp_feat_activate(sk, dccp_feat_index(feat_num), local, fval);
365}
366
367/* Test for "Req'd" feature (RFC 4340, 6.4) */
368static inline int dccp_feat_must_be_understood(u8 feat_num)
369{
370 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
371 feat_num == DCCPF_SEQUENCE_WINDOW;
372}
373
374/* copy constructor, fval must not already contain allocated memory */
375static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
376{
377 fval->sp.len = len;
378 if (fval->sp.len > 0) {
379 fval->sp.vec = kmemdup(val, len, gfp_any());
380 if (fval->sp.vec == NULL) {
381 fval->sp.len = 0;
382 return -ENOBUFS;
383 }
226 } 384 }
385 return 0;
386}
227 387
228 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */ 388static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
389{
390 if (unlikely(val == NULL))
391 return;
392 if (dccp_feat_type(feat_num) == FEAT_SP)
393 kfree(val->sp.vec);
394 memset(val, 0, sizeof(*val));
229} 395}
230 396
231static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) 397static struct dccp_feat_entry *
398 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
232{ 399{
233 struct dccp_minisock *dmsk = dccp_msk(sk); 400 struct dccp_feat_entry *new;
234 struct dccp_opt_pend *opt; 401 u8 type = dccp_feat_type(original->feat_num);
235 int rc = 1;
236 u8 t;
237 402
238 /* 403 if (type == FEAT_UNKNOWN)
239 * We received a CHANGE. We gotta match it against our own preference 404 return NULL;
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 405
248 /* find our preference list for this feature */ 406 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
249 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { 407 if (new == NULL)
250 if (opt->dccpop_type != t || opt->dccpop_feat != feature) 408 return NULL;
251 continue;
252 409
253 /* find the winner from the two preference lists */ 410 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
254 rc = dccp_feat_reconcile(sk, opt, val, len); 411 original->val.sp.vec,
255 break; 412 original->val.sp.len)) {
413 kfree(new);
414 return NULL;
256 } 415 }
416 return new;
417}
257 418
258 /* We didn't deal with the change. This can happen if we have no 419static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
259 * preference list for the feature. In fact, it just shouldn't 420{
260 * happen---if we understand a feature, we should have a preference list 421 if (entry != NULL) {
261 * with at least the default value. 422 dccp_feat_val_destructor(entry->feat_num, &entry->val);
262 */ 423 kfree(entry);
263 BUG_ON(rc == 1); 424 }
425}
264 426
265 return rc; 427/*
428 * List management functions
429 *
430 * Feature negotiation lists rely on and maintain the following invariants:
431 * - each feat_num in the list is known, i.e. we know its type and default value
432 * - each feat_num/is_local combination is unique (old entries are overwritten)
433 * - SP values are always freshly allocated
434 * - list is sorted in increasing order of feature number (faster lookup)
435 */
436static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
437 u8 feat_num, bool is_local)
438{
439 struct dccp_feat_entry *entry;
440
441 list_for_each_entry(entry, fn_list, node)
442 if (entry->feat_num == feat_num && entry->is_local == is_local)
443 return entry;
444 else if (entry->feat_num > feat_num)
445 break;
446 return NULL;
266} 447}
267 448
268static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) 449/**
450 * dccp_feat_entry_new - Central list update routine (called by all others)
451 * @head: list to add to
452 * @feat: feature number
453 * @local: whether the local (1) or remote feature with number @feat is meant
454 * This is the only constructor and serves to ensure the above invariants.
455 */
456static struct dccp_feat_entry *
457 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
269{ 458{
270 struct dccp_opt_pend *opt; 459 struct dccp_feat_entry *entry;
271 struct dccp_minisock *dmsk = dccp_msk(sk); 460
272 u8 *copy; 461 list_for_each_entry(entry, head, node)
273 int rc; 462 if (entry->feat_num == feat && entry->is_local == local) {
463 dccp_feat_val_destructor(entry->feat_num, &entry->val);
464 return entry;
465 } else if (entry->feat_num > feat) {
466 head = &entry->node;
467 break;
468 }
274 469
275 /* NN features must be Change L (sec. 6.3.2) */ 470 entry = kmalloc(sizeof(*entry), gfp_any());
276 if (type != DCCPO_CHANGE_L) { 471 if (entry != NULL) {
277 dccp_pr_debug("received %s for NN feature %d\n", 472 entry->feat_num = feat;
278 dccp_feat_typename(type), feature); 473 entry->is_local = local;
279 return -EFAULT; 474 list_add_tail(&entry->node, head);
280 } 475 }
476 return entry;
477}
281 478
282 /* XXX sanity check opt val */ 479/**
480 * dccp_feat_push_change - Add/overwrite a Change option in the list
481 * @fn_list: feature-negotiation list to update
482 * @feat: one of %dccp_feature_numbers
483 * @local: whether local (1) or remote (0) @feat_num is meant
484 * @needs_mandatory: whether to use Mandatory feature negotiation options
485 * @fval: pointer to NN/SP value to be inserted (will be copied)
486 */
487static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
488 u8 mandatory, dccp_feat_val *fval)
489{
490 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
283 491
284 /* copy option so we can confirm it */ 492 if (new == NULL)
285 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
286 if (opt == NULL)
287 return -ENOMEM; 493 return -ENOMEM;
288 494
289 copy = kmemdup(val, len, GFP_ATOMIC); 495 new->feat_num = feat;
290 if (copy == NULL) { 496 new->is_local = local;
291 kfree(opt); 497 new->state = FEAT_INITIALISING;
292 return -ENOMEM; 498 new->needs_confirm = 0;
293 } 499 new->empty_confirm = 0;
500 new->val = *fval;
501 new->needs_mandatory = mandatory;
294 502
295 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */ 503 return 0;
296 opt->dccpop_feat = feature; 504}
297 opt->dccpop_val = copy;
298 opt->dccpop_len = len;
299 505
300 /* change feature */ 506/**
301 rc = dccp_feat_update(sk, type, feature, *val); 507 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
302 if (rc) { 508 * @fn_list: feature-negotiation list to add to
303 kfree(opt->dccpop_val); 509 * @feat: one of %dccp_feature_numbers
304 kfree(opt); 510 * @local: whether local (1) or remote (0) @feat_num is being confirmed
305 return rc; 511 * @fval: pointer to NN/SP value to be inserted or NULL
306 } 512 * Returns 0 on success, a Reset code for further processing otherwise.
513 */
514static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
515 dccp_feat_val *fval)
516{
517 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
307 518
308 dccp_feat_debug(type, feature, *copy); 519 if (new == NULL)
520 return DCCP_RESET_CODE_TOO_BUSY;
309 521
310 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf); 522 new->feat_num = feat;
523 new->is_local = local;
524 new->state = FEAT_STABLE; /* transition in 6.6.2 */
525 new->needs_confirm = 1;
526 new->empty_confirm = (fval == NULL);
527 new->val.nn = 0; /* zeroes the whole structure */
528 if (!new->empty_confirm)
529 new->val = *fval;
530 new->needs_mandatory = 0;
311 531
312 return 0; 532 return 0;
313} 533}
314 534
315static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk, 535static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
316 u8 type, u8 feature)
317{ 536{
318 /* XXX check if other confirms for that are queued and recycle slot */ 537 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
319 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC); 538}
320 539
321 if (opt == NULL) { 540static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
322 /* XXX what do we do? Ignoring should be fine. It's a change 541{
323 * after all =P 542 list_del(&entry->node);
324 */ 543 dccp_feat_entry_destructor(entry);
325 return; 544}
326 }
327 545
328 switch (type) { 546void dccp_feat_list_purge(struct list_head *fn_list)
329 case DCCPO_CHANGE_L: 547{
330 opt->dccpop_type = DCCPO_CONFIRM_R; 548 struct dccp_feat_entry *entry, *next;
331 break; 549
332 case DCCPO_CHANGE_R: 550 list_for_each_entry_safe(entry, next, fn_list, node)
333 opt->dccpop_type = DCCPO_CONFIRM_L; 551 dccp_feat_entry_destructor(entry);
334 break; 552 INIT_LIST_HEAD(fn_list);
335 default: 553}
336 DCCP_WARN("invalid type %d\n", type); 554EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
337 kfree(opt); 555
338 return; 556/* generate @to as full clone of @from - @to must not contain any nodes */
557int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
558{
559 struct dccp_feat_entry *entry, *new;
560
561 INIT_LIST_HEAD(to);
562 list_for_each_entry(entry, from, node) {
563 new = dccp_feat_clone_entry(entry);
564 if (new == NULL)
565 goto cloning_failed;
566 list_add_tail(&new->node, to);
339 } 567 }
340 opt->dccpop_feat = feature; 568 return 0;
341 opt->dccpop_val = NULL;
342 opt->dccpop_len = 0;
343 569
344 /* change feature */ 570cloning_failed:
345 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature); 571 dccp_feat_list_purge(to);
572 return -ENOMEM;
573}
346 574
347 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf); 575/**
576 * dccp_feat_valid_nn_length - Enforce length constraints on NN options
577 * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
578 * incoming options are accepted as long as their values are valid.
579 */
580static u8 dccp_feat_valid_nn_length(u8 feat_num)
581{
582 if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
583 return 2;
584 if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
585 return 6;
586 return 0;
348} 587}
349 588
350static void dccp_feat_flush_confirm(struct sock *sk) 589static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
351{ 590{
352 struct dccp_minisock *dmsk = dccp_msk(sk); 591 switch (feat_num) {
353 /* Check if there is anything to confirm in the first place */ 592 case DCCPF_ACK_RATIO:
354 int yes = !list_empty(&dmsk->dccpms_conf); 593 return val <= DCCPF_ACK_RATIO_MAX;
594 case DCCPF_SEQUENCE_WINDOW:
595 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
596 }
597 return 0; /* feature unknown - so we can't tell */
598}
355 599
356 if (!yes) { 600/* check that SP values are within the ranges defined in RFC 4340 */
357 struct dccp_opt_pend *opt; 601static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
602{
603 switch (feat_num) {
604 case DCCPF_CCID:
605 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
606 /* Type-check Boolean feature values: */
607 case DCCPF_SHORT_SEQNOS:
608 case DCCPF_ECN_INCAPABLE:
609 case DCCPF_SEND_ACK_VECTOR:
610 case DCCPF_SEND_NDP_COUNT:
611 case DCCPF_DATA_CHECKSUM:
612 case DCCPF_SEND_LEV_RATE:
613 return val < 2;
614 case DCCPF_MIN_CSUM_COVER:
615 return val < 16;
616 }
617 return 0; /* feature unknown */
618}
619
620static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
621{
622 if (sp_list == NULL || sp_len < 1)
623 return 0;
624 while (sp_len--)
625 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
626 return 0;
627 return 1;
628}
358 629
359 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { 630/**
360 if (opt->dccpop_conf) { 631 * dccp_feat_insert_opts - Generate FN options from current list state
361 yes = 1; 632 * @skb: next sk_buff to be sent to the peer
362 break; 633 * @dp: for client during handshake and general negotiation
634 * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
635 */
636int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
637 struct sk_buff *skb)
638{
639 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
640 struct dccp_feat_entry *pos, *next;
641 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
642 bool rpt;
643
644 /* put entries into @skb in the order they appear in the list */
645 list_for_each_entry_safe_reverse(pos, next, fn, node) {
646 opt = dccp_feat_genopt(pos);
647 type = dccp_feat_type(pos->feat_num);
648 rpt = false;
649
650 if (pos->empty_confirm) {
651 len = 0;
652 ptr = NULL;
653 } else {
654 if (type == FEAT_SP) {
655 len = pos->val.sp.len;
656 ptr = pos->val.sp.vec;
657 rpt = pos->needs_confirm;
658 } else if (type == FEAT_NN) {
659 len = dccp_feat_valid_nn_length(pos->feat_num);
660 ptr = nn_in_nbo;
661 dccp_encode_value_var(pos->val.nn, ptr, len);
662 } else {
663 DCCP_BUG("unknown feature %u", pos->feat_num);
664 return -1;
363 } 665 }
364 } 666 }
667 dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
668
669 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
670 return -1;
671 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
672 return -1;
673 /*
674 * Enter CHANGING after transmitting the Change option (6.6.2).
675 */
676 if (pos->state == FEAT_INITIALISING)
677 pos->state = FEAT_CHANGING;
365 } 678 }
679 return 0;
680}
366 681
367 if (!yes) 682/**
368 return; 683 * __feat_register_nn - Register new NN value on socket
684 * @fn: feature-negotiation list to register with
685 * @feat: an NN feature from %dccp_feature_numbers
686 * @mandatory: use Mandatory option if 1
687 * @nn_val: value to register (restricted to 4 bytes)
688 * Note that NN features are local by definition (RFC 4340, 6.3.2).
689 */
690static int __feat_register_nn(struct list_head *fn, u8 feat,
691 u8 mandatory, u64 nn_val)
692{
693 dccp_feat_val fval = { .nn = nn_val };
369 694
370 /* OK there is something to confirm... */ 695 if (dccp_feat_type(feat) != FEAT_NN ||
371 /* XXX check if packet is in flight? Send delayed ack?? */ 696 !dccp_feat_is_valid_nn_val(feat, nn_val))
372 if (sk->sk_state == DCCP_OPEN) 697 return -EINVAL;
373 dccp_send_ack(sk); 698
699 /* Don't bother with default values, they will be activated anyway. */
700 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
701 return 0;
702
703 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
374} 704}
375 705
376int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) 706/**
707 * __feat_register_sp - Register new SP value/list on socket
708 * @fn: feature-negotiation list to register with
709 * @feat: an SP feature from %dccp_feature_numbers
710 * @is_local: whether the local (1) or the remote (0) @feat is meant
711 * @mandatory: use Mandatory option if 1
712 * @sp_val: SP value followed by optional preference list
713 * @sp_len: length of @sp_val in bytes
714 */
715static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
716 u8 mandatory, u8 const *sp_val, u8 sp_len)
377{ 717{
378 int rc; 718 dccp_feat_val fval;
379 719
380 dccp_feat_debug(type, feature, *val); 720 if (dccp_feat_type(feat) != FEAT_SP ||
721 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
722 return -EINVAL;
381 723
382 /* figure out if it's SP or NN feature */ 724 /* Avoid negotiating alien CCIDs by only advertising supported ones */
383 switch (feature) { 725 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
384 /* deal with SP features */ 726 return -EOPNOTSUPP;
385 case DCCPF_CCID:
386 rc = dccp_feat_sp(sk, type, feature, val, len);
387 break;
388 727
389 /* deal with NN features */ 728 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
390 case DCCPF_ACK_RATIO: 729 return -ENOMEM;
391 rc = dccp_feat_nn(sk, type, feature, val, len);
392 break;
393 730
394 /* XXX implement other features */ 731 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
395 default: 732}
396 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n", 733
397 dccp_feat_typename(type), feature); 734/**
398 rc = -EFAULT; 735 * dccp_feat_register_sp - Register requests to change SP feature values
399 break; 736 * @sk: client or listening socket
737 * @feat: one of %dccp_feature_numbers
738 * @is_local: whether the local (1) or remote (0) @feat is meant
739 * @list: array of preferred values, in descending order of preference
740 * @len: length of @list in bytes
741 */
742int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
743 u8 const *list, u8 len)
744{ /* any changes must be registered before establishing the connection */
745 if (sk->sk_state != DCCP_CLOSED)
746 return -EISCONN;
747 if (dccp_feat_type(feat) != FEAT_SP)
748 return -EINVAL;
749 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
750 0, list, len);
751}
752
753/* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
754int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
755{
756 /* any changes must be registered before establishing the connection */
757 if (sk->sk_state != DCCP_CLOSED)
758 return -EISCONN;
759 if (dccp_feat_type(feat) != FEAT_NN)
760 return -EINVAL;
761 return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
762}
763
764/**
765 * dccp_feat_signal_nn_change - Update NN values for an established connection
766 * @sk: DCCP socket of an established connection
767 * @feat: NN feature number from %dccp_feature_numbers
768 * @nn_val: the new value to use
769 * This function is used to communicate NN updates out-of-band. The difference
770 * to feature negotiation during connection setup is that values are activated
771 * immediately after validation, i.e. we don't wait for the Confirm: either the
772 * value is accepted by the peer (and then the waiting is futile), or it is not
773 * (Reset or empty Confirm). We don't accept empty Confirms - transmitted values
774 * are validated, and the peer "MUST accept any valid value" (RFC 4340, 6.3.2).
775 */
776int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val)
777{
778 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
779 dccp_feat_val fval = { .nn = nn_val };
780 struct dccp_feat_entry *entry;
781
782 if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN)
783 return 0;
784
785 if (dccp_feat_type(feat) != FEAT_NN ||
786 !dccp_feat_is_valid_nn_val(feat, nn_val))
787 return -EINVAL;
788
789 entry = dccp_feat_list_lookup(fn, feat, 1);
790 if (entry != NULL) {
791 dccp_pr_debug("Ignoring %llu, entry %llu exists in state %s\n",
792 (unsigned long long)nn_val,
793 (unsigned long long)entry->val.nn,
794 dccp_feat_sname[entry->state]);
795 return 0;
400 } 796 }
401 797
402 /* check if there were problems changing features */ 798 if (dccp_feat_activate(sk, feat, 1, &fval))
403 if (rc) { 799 return -EADV;
404 /* If we don't agree on SP, we sent a confirm for old value. 800
405 * However we propagate rc to caller in case option was 801 inet_csk_schedule_ack(sk);
406 * mandatory 802 return dccp_feat_push_change(fn, feat, 1, 0, &fval);
803}
804EXPORT_SYMBOL_GPL(dccp_feat_signal_nn_change);
805
806/*
807 * Tracking features whose value depend on the choice of CCID
808 *
809 * This is designed with an extension in mind so that a list walk could be done
810 * before activating any features. However, the existing framework was found to
811 * work satisfactorily up until now, the automatic verification is left open.
812 * When adding new CCIDs, add a corresponding dependency table here.
813 */
814static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
815{
816 static const struct ccid_dependency ccid2_dependencies[2][2] = {
817 /*
818 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
819 * feature and Send Ack Vector is an RX feature, `is_local'
820 * needs to be reversed.
407 */ 821 */
408 if (rc != DCCP_FEAT_SP_NOAGREE) 822 { /* Dependencies of the receiver-side (remote) CCID2 */
409 dccp_feat_empty_confirm(dccp_msk(sk), type, feature); 823 {
824 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
825 .is_local = true,
826 .is_mandatory = true,
827 .val = 1
828 },
829 { 0, 0, 0, 0 }
830 },
831 { /* Dependencies of the sender-side (local) CCID2 */
832 {
833 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
834 .is_local = false,
835 .is_mandatory = true,
836 .val = 1
837 },
838 { 0, 0, 0, 0 }
839 }
840 };
841 static const struct ccid_dependency ccid3_dependencies[2][5] = {
842 { /*
843 * Dependencies of the receiver-side CCID3
844 */
845 { /* locally disable Ack Vectors */
846 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
847 .is_local = true,
848 .is_mandatory = false,
849 .val = 0
850 },
851 { /* see below why Send Loss Event Rate is on */
852 .dependent_feat = DCCPF_SEND_LEV_RATE,
853 .is_local = true,
854 .is_mandatory = true,
855 .val = 1
856 },
857 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
858 .dependent_feat = DCCPF_SEND_NDP_COUNT,
859 .is_local = false,
860 .is_mandatory = true,
861 .val = 1
862 },
863 { 0, 0, 0, 0 },
864 },
865 { /*
866 * CCID3 at the TX side: we request that the HC-receiver
867 * will not send Ack Vectors (they will be ignored, so
868 * Mandatory is not set); we enable Send Loss Event Rate
869 * (Mandatory since the implementation does not support
870 * the Loss Intervals option of RFC 4342, 8.6).
871 * The last two options are for peer's information only.
872 */
873 {
874 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
875 .is_local = false,
876 .is_mandatory = false,
877 .val = 0
878 },
879 {
880 .dependent_feat = DCCPF_SEND_LEV_RATE,
881 .is_local = false,
882 .is_mandatory = true,
883 .val = 1
884 },
885 { /* this CCID does not support Ack Ratio */
886 .dependent_feat = DCCPF_ACK_RATIO,
887 .is_local = true,
888 .is_mandatory = false,
889 .val = 0
890 },
891 { /* tell receiver we are sending NDP counts */
892 .dependent_feat = DCCPF_SEND_NDP_COUNT,
893 .is_local = true,
894 .is_mandatory = false,
895 .val = 1
896 },
897 { 0, 0, 0, 0 }
898 }
899 };
900 switch (ccid) {
901 case DCCPC_CCID2:
902 return ccid2_dependencies[is_local];
903 case DCCPC_CCID3:
904 return ccid3_dependencies[is_local];
905 default:
906 return NULL;
410 } 907 }
908}
411 909
412 /* generate the confirm [if required] */ 910/**
413 dccp_feat_flush_confirm(sk); 911 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
414 912 * @fn: feature-negotiation list to update
913 * @id: CCID number to track
914 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
915 * This function needs to be called after registering all other features.
916 */
917static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
918{
919 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
920 int i, rc = (table == NULL);
921
922 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
923 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
924 rc = __feat_register_sp(fn, table[i].dependent_feat,
925 table[i].is_local,
926 table[i].is_mandatory,
927 &table[i].val, 1);
928 else
929 rc = __feat_register_nn(fn, table[i].dependent_feat,
930 table[i].is_mandatory,
931 table[i].val);
415 return rc; 932 return rc;
416} 933}
417 934
418EXPORT_SYMBOL_GPL(dccp_feat_change_recv); 935/**
936 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
937 * @dp: client or listening socket (settings will be inherited)
938 * This is called after all registrations (socket initialisation, sysctls, and
939 * sockopt calls), and before sending the first packet containing Change options
940 * (ie. client-Request or server-Response), to ensure internal consistency.
941 */
942int dccp_feat_finalise_settings(struct dccp_sock *dp)
943{
944 struct list_head *fn = &dp->dccps_featneg;
945 struct dccp_feat_entry *entry;
946 int i = 2, ccids[2] = { -1, -1 };
947
948 /*
949 * Propagating CCIDs:
950 * 1) not useful to propagate CCID settings if this host advertises more
951 * than one CCID: the choice of CCID may still change - if this is
952 * the client, or if this is the server and the client sends
953 * singleton CCID values.
954 * 2) since is that propagate_ccid changes the list, we defer changing
955 * the sorted list until after the traversal.
956 */
957 list_for_each_entry(entry, fn, node)
958 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
959 ccids[entry->is_local] = entry->val.sp.vec[0];
960 while (i--)
961 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
962 return -1;
963 dccp_feat_print_fnlist(fn);
964 return 0;
965}
419 966
420int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature, 967/**
421 u8 *val, u8 len) 968 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
969 * It is the server which resolves the dependencies once the CCID has been
970 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
971 */
972int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
422{ 973{
423 u8 t; 974 struct list_head *fn = &dreq->dreq_featneg;
424 struct dccp_opt_pend *opt; 975 struct dccp_feat_entry *entry;
425 struct dccp_minisock *dmsk = dccp_msk(sk); 976 u8 is_local, ccid;
426 int found = 0;
427 int all_confirmed = 1;
428 977
429 dccp_feat_debug(type, feature, *val); 978 for (is_local = 0; is_local <= 1; is_local++) {
979 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
430 980
431 /* locate our change request */ 981 if (entry != NULL && !entry->empty_confirm)
432 switch (type) { 982 ccid = entry->val.sp.vec[0];
433 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break; 983 else
434 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break; 984 ccid = dccp_feat_default_value(DCCPF_CCID);
435 default: DCCP_WARN("invalid type %d\n", type);
436 return 1;
437 985
986 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
987 return -1;
438 } 988 }
439 /* XXX sanity check feature value */ 989 return 0;
990}
440 991
441 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { 992/* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
442 if (!opt->dccpop_conf && opt->dccpop_type == t && 993static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
443 opt->dccpop_feat == feature) { 994{
444 found = 1; 995 u8 c, s;
445 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
446 996
447 /* XXX do sanity check */ 997 for (s = 0; s < slen; s++)
998 for (c = 0; c < clen; c++)
999 if (servlist[s] == clilist[c])
1000 return servlist[s];
1001 return -1;
1002}
448 1003
449 opt->dccpop_conf = 1; 1004/**
1005 * dccp_feat_prefer - Move preferred entry to the start of array
1006 * Reorder the @array_len elements in @array so that @preferred_value comes
1007 * first. Returns >0 to indicate that @preferred_value does occur in @array.
1008 */
1009static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
1010{
1011 u8 i, does_occur = 0;
450 1012
451 /* We got a confirmation---change the option */ 1013 if (array != NULL) {
452 dccp_feat_update(sk, opt->dccpop_type, 1014 for (i = 0; i < array_len; i++)
453 opt->dccpop_feat, *val); 1015 if (array[i] == preferred_value) {
1016 array[i] = array[0];
1017 does_occur++;
1018 }
1019 if (does_occur)
1020 array[0] = preferred_value;
1021 }
1022 return does_occur;
1023}
454 1024
455 /* XXX check the return value of dccp_feat_update */ 1025/**
456 break; 1026 * dccp_feat_reconcile - Reconcile SP preference lists
457 } 1027 * @fval: SP list to reconcile into
1028 * @arr: received SP preference list
1029 * @len: length of @arr in bytes
1030 * @is_server: whether this side is the server (and @fv is the server's list)
1031 * @reorder: whether to reorder the list in @fv after reconciling with @arr
1032 * When successful, > 0 is returned and the reconciled list is in @fval.
1033 * A value of 0 means that negotiation failed (no shared entry).
1034 */
1035static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
1036 bool is_server, bool reorder)
1037{
1038 int rc;
458 1039
459 if (!opt->dccpop_conf) 1040 if (!fv->sp.vec || !arr) {
460 all_confirmed = 0; 1041 DCCP_CRIT("NULL feature value or array");
1042 return 0;
461 } 1043 }
462 1044
463 /* fix re-transmit timer */ 1045 if (is_server)
464 /* XXX gotta make sure that no option negotiation occurs during 1046 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
465 * connection shutdown. Consider that the CLOSEREQ is sent and timer is 1047 else
466 * on. if all options are confirmed it might kill timer which should 1048 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
467 * remain alive until close is received.
468 */
469 if (all_confirmed) {
470 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
471 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
472 }
473 1049
474 if (!found) 1050 if (!reorder)
475 dccp_pr_debug("%s(%d, ...) never requested\n", 1051 return rc;
476 dccp_feat_typename(type), feature); 1052 if (rc < 0)
477 return 0; 1053 return 0;
478}
479 1054
480EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv); 1055 /*
1056 * Reorder list: used for activating features and in dccp_insert_fn_opt.
1057 */
1058 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
1059}
481 1060
482void dccp_feat_clean(struct dccp_minisock *dmsk) 1061/**
1062 * dccp_feat_change_recv - Process incoming ChangeL/R options
1063 * @fn: feature-negotiation list to update
1064 * @is_mandatory: whether the Change was preceded by a Mandatory option
1065 * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
1066 * @feat: one of %dccp_feature_numbers
1067 * @val: NN value or SP value/preference list
1068 * @len: length of @val in bytes
1069 * @server: whether this node is the server (1) or the client (0)
1070 */
1071static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1072 u8 feat, u8 *val, u8 len, const bool server)
483{ 1073{
484 struct dccp_opt_pend *opt, *next; 1074 u8 defval, type = dccp_feat_type(feat);
1075 const bool local = (opt == DCCPO_CHANGE_R);
1076 struct dccp_feat_entry *entry;
1077 dccp_feat_val fval;
1078
1079 if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
1080 goto unknown_feature_or_value;
1081
1082 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1083
1084 /*
1085 * Negotiation of NN features: Change R is invalid, so there is no
1086 * simultaneous negotiation; hence we do not look up in the list.
1087 */
1088 if (type == FEAT_NN) {
1089 if (local || len > sizeof(fval.nn))
1090 goto unknown_feature_or_value;
485 1091
486 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending, 1092 /* 6.3.2: "The feature remote MUST accept any valid value..." */
487 dccpop_node) { 1093 fval.nn = dccp_decode_value_var(val, len);
488 BUG_ON(opt->dccpop_val == NULL); 1094 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
489 kfree(opt->dccpop_val); 1095 goto unknown_feature_or_value;
490 1096
491 if (opt->dccpop_sc != NULL) { 1097 return dccp_feat_push_confirm(fn, feat, local, &fval);
492 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL); 1098 }
493 kfree(opt->dccpop_sc->dccpoc_val); 1099
494 kfree(opt->dccpop_sc); 1100 /*
1101 * Unidirectional/simultaneous negotiation of SP features (6.3.1)
1102 */
1103 entry = dccp_feat_list_lookup(fn, feat, local);
1104 if (entry == NULL) {
1105 /*
1106 * No particular preferences have been registered. We deal with
1107 * this situation by assuming that all valid values are equally
1108 * acceptable, and apply the following checks:
1109 * - if the peer's list is a singleton, we accept a valid value;
1110 * - if we are the server, we first try to see if the peer (the
1111 * client) advertises the default value. If yes, we use it,
1112 * otherwise we accept the preferred value;
1113 * - else if we are the client, we use the first list element.
1114 */
1115 if (dccp_feat_clone_sp_val(&fval, val, 1))
1116 return DCCP_RESET_CODE_TOO_BUSY;
1117
1118 if (len > 1 && server) {
1119 defval = dccp_feat_default_value(feat);
1120 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
1121 fval.sp.vec[0] = defval;
1122 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
1123 kfree(fval.sp.vec);
1124 goto unknown_feature_or_value;
1125 }
1126
1127 /* Treat unsupported CCIDs like invalid values */
1128 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
1129 kfree(fval.sp.vec);
1130 goto not_valid_or_not_known;
495 } 1131 }
496 1132
497 kfree(opt); 1133 return dccp_feat_push_confirm(fn, feat, local, &fval);
1134
1135 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
1136 return 0;
498 } 1137 }
499 INIT_LIST_HEAD(&dmsk->dccpms_pending);
500 1138
501 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) { 1139 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
502 BUG_ON(opt == NULL); 1140 entry->empty_confirm = 0;
503 if (opt->dccpop_val != NULL) 1141 } else if (is_mandatory) {
504 kfree(opt->dccpop_val); 1142 return DCCP_RESET_CODE_MANDATORY_ERROR;
505 kfree(opt); 1143 } else if (entry->state == FEAT_INITIALISING) {
1144 /*
1145 * Failed simultaneous negotiation (server only): try to `save'
1146 * the connection by checking whether entry contains the default
1147 * value for @feat. If yes, send an empty Confirm to signal that
1148 * the received Change was not understood - which implies using
1149 * the default value.
1150 * If this also fails, we use Reset as the last resort.
1151 */
1152 WARN_ON(!server);
1153 defval = dccp_feat_default_value(feat);
1154 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
1155 return DCCP_RESET_CODE_OPTION_ERROR;
1156 entry->empty_confirm = 1;
506 } 1157 }
507 INIT_LIST_HEAD(&dmsk->dccpms_conf); 1158 entry->needs_confirm = 1;
508} 1159 entry->needs_mandatory = 0;
1160 entry->state = FEAT_STABLE;
1161 return 0;
509 1162
510EXPORT_SYMBOL_GPL(dccp_feat_clean); 1163unknown_feature_or_value:
1164 if (!is_mandatory)
1165 return dccp_push_empty_confirm(fn, feat, local);
511 1166
512/* this is to be called only when a listening sock creates its child. It is 1167not_valid_or_not_known:
513 * assumed by the function---the confirm is not duplicated, but rather it is 1168 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
514 * "passed on". 1169 : DCCP_RESET_CODE_OPTION_ERROR;
1170}
1171
1172/**
1173 * dccp_feat_confirm_recv - Process received Confirm options
1174 * @fn: feature-negotiation list to update
1175 * @is_mandatory: whether @opt was preceded by a Mandatory option
1176 * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
1177 * @feat: one of %dccp_feature_numbers
1178 * @val: NN value or SP value/preference list
1179 * @len: length of @val in bytes
1180 * @server: whether this node is server (1) or client (0)
515 */ 1181 */
516int dccp_feat_clone(struct sock *oldsk, struct sock *newsk) 1182static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1183 u8 feat, u8 *val, u8 len, const bool server)
517{ 1184{
518 struct dccp_minisock *olddmsk = dccp_msk(oldsk); 1185 u8 *plist, plen, type = dccp_feat_type(feat);
519 struct dccp_minisock *newdmsk = dccp_msk(newsk); 1186 const bool local = (opt == DCCPO_CONFIRM_R);
520 struct dccp_opt_pend *opt; 1187 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
521 int rc = 0;
522 1188
523 INIT_LIST_HEAD(&newdmsk->dccpms_pending); 1189 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
524 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
525 1190
526 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) { 1191 if (entry == NULL) { /* nothing queued: ignore or handle error */
527 struct dccp_opt_pend *newopt; 1192 if (is_mandatory && type == FEAT_UNKNOWN)
528 /* copy the value of the option */ 1193 return DCCP_RESET_CODE_MANDATORY_ERROR;
529 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
530 1194
531 if (val == NULL) 1195 if (!local && type == FEAT_NN) /* 6.3.2 */
532 goto out_clean; 1196 goto confirmation_failed;
533 1197 return 0;
534 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC); 1198 }
535 if (newopt == NULL) {
536 kfree(val);
537 goto out_clean;
538 }
539 1199
540 /* insert the option */ 1200 if (entry->state != FEAT_CHANGING) /* 6.6.2 */
541 newopt->dccpop_val = val; 1201 return 0;
542 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
543 1202
544 /* XXX what happens with backlogs and multiple connections at 1203 if (len == 0) {
545 * once... 1204 if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
1205 goto confirmation_failed;
1206 /*
1207 * Empty Confirm during connection setup: this means reverting
1208 * to the `old' value, which in this case is the default. Since
1209 * we handle default values automatically when no other values
1210 * have been set, we revert to the old value by removing this
1211 * entry from the list.
546 */ 1212 */
547 /* the master socket no longer needs to worry about confirms */ 1213 dccp_feat_list_pop(entry);
548 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */ 1214 return 0;
1215 }
1216
1217 if (type == FEAT_NN) {
1218 if (len > sizeof(entry->val.nn))
1219 goto confirmation_failed;
1220
1221 if (entry->val.nn == dccp_decode_value_var(val, len))
1222 goto confirmation_succeeded;
1223
1224 DCCP_WARN("Bogus Confirm for non-existing value\n");
1225 goto confirmation_failed;
1226 }
549 1227
550 /* reset state for a new socket */ 1228 /*
551 opt->dccpop_conf = 0; 1229 * Parsing SP Confirms: the first element of @val is the preferred
1230 * SP value which the peer confirms, the remainder depends on @len.
1231 * Note that only the confirmed value need to be a valid SP value.
1232 */
1233 if (!dccp_feat_is_valid_sp_val(feat, *val))
1234 goto confirmation_failed;
1235
1236 if (len == 1) { /* peer didn't supply a preference list */
1237 plist = val;
1238 plen = len;
1239 } else { /* preferred value + preference list */
1240 plist = val + 1;
1241 plen = len - 1;
552 } 1242 }
553 1243
554 /* XXX not doing anything about the conf queue */ 1244 /* Check whether the peer got the reconciliation right (6.6.8) */
1245 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1246 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1247 return DCCP_RESET_CODE_OPTION_ERROR;
1248 }
1249 entry->val.sp.vec[0] = *val;
555 1250
556out: 1251confirmation_succeeded:
557 return rc; 1252 entry->state = FEAT_STABLE;
1253 return 0;
558 1254
559out_clean: 1255confirmation_failed:
560 dccp_feat_clean(newdmsk); 1256 DCCP_WARN("Confirmation failed\n");
561 rc = -ENOMEM; 1257 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
562 goto out; 1258 : DCCP_RESET_CODE_OPTION_ERROR;
563} 1259}
564 1260
565EXPORT_SYMBOL_GPL(dccp_feat_clone); 1261/**
1262 * dccp_feat_handle_nn_established - Fast-path reception of NN options
1263 * @sk: socket of an established DCCP connection
1264 * @mandatory: whether @opt was preceded by a Mandatory option
1265 * @opt: %DCCPO_CHANGE_L | %DCCPO_CONFIRM_R (NN only)
1266 * @feat: NN number, one of %dccp_feature_numbers
1267 * @val: NN value
1268 * @len: length of @val in bytes
1269 * This function combines the functionality of change_recv/confirm_recv, with
1270 * the following differences (reset codes are the same):
1271 * - cleanup after receiving the Confirm;
1272 * - values are directly activated after successful parsing;
1273 * - deliberately restricted to NN features.
1274 * The restriction to NN features is essential since SP features can have non-
1275 * predictable outcomes (depending on the remote configuration), and are inter-
1276 * dependent (CCIDs for instance cause further dependencies).
1277 */
1278static u8 dccp_feat_handle_nn_established(struct sock *sk, u8 mandatory, u8 opt,
1279 u8 feat, u8 *val, u8 len)
1280{
1281 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1282 const bool local = (opt == DCCPO_CONFIRM_R);
1283 struct dccp_feat_entry *entry;
1284 u8 type = dccp_feat_type(feat);
1285 dccp_feat_val fval;
1286
1287 dccp_feat_print_opt(opt, feat, val, len, mandatory);
1288
1289 /* Ignore non-mandatory unknown and non-NN features */
1290 if (type == FEAT_UNKNOWN) {
1291 if (local && !mandatory)
1292 return 0;
1293 goto fast_path_unknown;
1294 } else if (type != FEAT_NN) {
1295 return 0;
1296 }
1297
1298 /*
1299 * We don't accept empty Confirms, since in fast-path feature
1300 * negotiation the values are enabled immediately after sending
1301 * the Change option.
1302 * Empty Changes on the other hand are invalid (RFC 4340, 6.1).
1303 */
1304 if (len == 0 || len > sizeof(fval.nn))
1305 goto fast_path_unknown;
1306
1307 if (opt == DCCPO_CHANGE_L) {
1308 fval.nn = dccp_decode_value_var(val, len);
1309 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1310 goto fast_path_unknown;
1311
1312 if (dccp_feat_push_confirm(fn, feat, local, &fval) ||
1313 dccp_feat_activate(sk, feat, local, &fval))
1314 return DCCP_RESET_CODE_TOO_BUSY;
1315
1316 /* set the `Ack Pending' flag to piggyback a Confirm */
1317 inet_csk_schedule_ack(sk);
1318
1319 } else if (opt == DCCPO_CONFIRM_R) {
1320 entry = dccp_feat_list_lookup(fn, feat, local);
1321 if (entry == NULL || entry->state != FEAT_CHANGING)
1322 return 0;
1323
1324 fval.nn = dccp_decode_value_var(val, len);
1325 if (fval.nn != entry->val.nn) {
1326 DCCP_WARN("Bogus Confirm for non-existing value\n");
1327 goto fast_path_failed;
1328 }
1329
1330 /* It has been confirmed - so remove the entry */
1331 dccp_feat_list_pop(entry);
1332
1333 } else {
1334 DCCP_WARN("Received illegal option %u\n", opt);
1335 goto fast_path_failed;
1336 }
1337 return 0;
1338
1339fast_path_unknown:
1340 if (!mandatory)
1341 return dccp_push_empty_confirm(fn, feat, local);
1342
1343fast_path_failed:
1344 return mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1345 : DCCP_RESET_CODE_OPTION_ERROR;
1346}
566 1347
567static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat, 1348/**
568 u8 *val, u8 len) 1349 * dccp_feat_parse_options - Process Feature-Negotiation Options
1350 * @sk: for general use and used by the client during connection setup
1351 * @dreq: used by the server during connection setup
1352 * @mandatory: whether @opt was preceded by a Mandatory option
1353 * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1354 * @feat: one of %dccp_feature_numbers
1355 * @val: value contents of @opt
1356 * @len: length of @val in bytes
1357 * Returns 0 on success, a Reset code for ending the connection otherwise.
1358 */
1359int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1360 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
569{ 1361{
570 int rc = -ENOMEM; 1362 struct dccp_sock *dp = dccp_sk(sk);
571 u8 *copy = kmemdup(val, len, GFP_KERNEL); 1363 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1364 bool server = false;
572 1365
573 if (copy != NULL) { 1366 switch (sk->sk_state) {
574 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL); 1367 /*
575 if (rc) 1368 * Negotiation during connection setup
576 kfree(copy); 1369 */
1370 case DCCP_LISTEN:
1371 server = true; /* fall through */
1372 case DCCP_REQUESTING:
1373 switch (opt) {
1374 case DCCPO_CHANGE_L:
1375 case DCCPO_CHANGE_R:
1376 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1377 val, len, server);
1378 case DCCPO_CONFIRM_R:
1379 case DCCPO_CONFIRM_L:
1380 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1381 val, len, server);
1382 }
1383 break;
1384 /*
1385 * Support for exchanging NN options on an established connection
1386 * This is currently restricted to Ack Ratio (RFC 4341, 6.1.2)
1387 */
1388 case DCCP_OPEN:
1389 case DCCP_PARTOPEN:
1390 return dccp_feat_handle_nn_established(sk, mandatory, opt, feat,
1391 val, len);
577 } 1392 }
578 return rc; 1393 return 0; /* ignore FN options in all other states */
579} 1394}
580 1395
581int dccp_feat_init(struct dccp_minisock *dmsk) 1396/**
1397 * dccp_feat_init - Seed feature negotiation with host-specific defaults
1398 * This initialises global defaults, depending on the value of the sysctls.
1399 * These can later be overridden by registering changes via setsockopt calls.
1400 * The last link in the chain is finalise_settings, to make sure that between
1401 * here and the start of actual feature negotiation no inconsistencies enter.
1402 *
1403 * All features not appearing below use either defaults or are otherwise
1404 * later adjusted through dccp_feat_finalise_settings().
1405 */
1406int dccp_feat_init(struct sock *sk)
582{ 1407{
1408 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1409 u8 on = 1, off = 0;
583 int rc; 1410 int rc;
1411 struct {
1412 u8 *val;
1413 u8 len;
1414 } tx, rx;
1415
1416 /* Non-negotiable (NN) features */
1417 rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
1418 sysctl_dccp_sequence_window);
1419 if (rc)
1420 return rc;
584 1421
585 INIT_LIST_HEAD(&dmsk->dccpms_pending); 1422 /* Server-priority (SP) features */
586 INIT_LIST_HEAD(&dmsk->dccpms_conf); 1423
1424 /* Advertise that short seqnos are not supported (7.6.1) */
1425 rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1426 if (rc)
1427 return rc;
587 1428
588 /* CCID L */ 1429 /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
589 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID, 1430 rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
590 &dmsk->dccpms_tx_ccid, 1);
591 if (rc) 1431 if (rc)
592 goto out; 1432 return rc;
1433
1434 /*
1435 * We advertise the available list of CCIDs and reorder according to
1436 * preferences, to avoid failure resulting from negotiating different
1437 * singleton values (which always leads to failure).
1438 * These settings can still (later) be overridden via sockopts.
1439 */
1440 if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1441 ccid_get_builtin_ccids(&rx.val, &rx.len))
1442 return -ENOBUFS;
1443
1444 /* Pre-load all CCID modules that are going to be advertised */
1445 rc = -EUNATCH;
1446 if (ccid_request_modules(tx.val, tx.len))
1447 goto free_ccid_lists;
1448
1449 if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
1450 !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
1451 goto free_ccid_lists;
593 1452
594 /* CCID R */ 1453 rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
595 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
596 &dmsk->dccpms_rx_ccid, 1);
597 if (rc) 1454 if (rc)
598 goto out; 1455 goto free_ccid_lists;
599 1456
600 /* Ack ratio */ 1457 rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
601 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO, 1458
602 &dmsk->dccpms_ack_ratio, 1); 1459free_ccid_lists:
603out: 1460 kfree(tx.val);
1461 kfree(rx.val);
604 return rc; 1462 return rc;
605} 1463}
606 1464
607EXPORT_SYMBOL_GPL(dccp_feat_init); 1465int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
608
609#ifdef CONFIG_IP_DCCP_DEBUG
610const char *dccp_feat_typename(const u8 type)
611{ 1466{
612 switch(type) { 1467 struct dccp_sock *dp = dccp_sk(sk);
613 case DCCPO_CHANGE_L: return("ChangeL"); 1468 struct dccp_feat_entry *cur, *next;
614 case DCCPO_CONFIRM_L: return("ConfirmL"); 1469 int idx;
615 case DCCPO_CHANGE_R: return("ChangeR"); 1470 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
616 case DCCPO_CONFIRM_R: return("ConfirmR"); 1471 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
617 /* the following case must not appear in feature negotation */ 1472 };
618 default: dccp_pr_debug("unknown type %d [BUG!]\n", type); 1473
1474 list_for_each_entry(cur, fn_list, node) {
1475 /*
1476 * An empty Confirm means that either an unknown feature type
1477 * or an invalid value was present. In the first case there is
1478 * nothing to activate, in the other the default value is used.
1479 */
1480 if (cur->empty_confirm)
1481 continue;
1482
1483 idx = dccp_feat_index(cur->feat_num);
1484 if (idx < 0) {
1485 DCCP_BUG("Unknown feature %u", cur->feat_num);
1486 goto activation_failed;
1487 }
1488 if (cur->state != FEAT_STABLE) {
1489 DCCP_CRIT("Negotiation of %s %s failed in state %s",
1490 cur->is_local ? "local" : "remote",
1491 dccp_feat_fname(cur->feat_num),
1492 dccp_feat_sname[cur->state]);
1493 goto activation_failed;
1494 }
1495 fvals[idx][cur->is_local] = &cur->val;
619 } 1496 }
620 return NULL;
621}
622 1497
623EXPORT_SYMBOL_GPL(dccp_feat_typename); 1498 /*
1499 * Activate in decreasing order of index, so that the CCIDs are always
1500 * activated as the last feature. This avoids the case where a CCID
1501 * relies on the initialisation of one or more features that it depends
1502 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1503 */
1504 for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1505 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1506 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1507 DCCP_CRIT("Could not activate %d", idx);
1508 goto activation_failed;
1509 }
624 1510
625const char *dccp_feat_name(const u8 feat) 1511 /* Clean up Change options which have been confirmed already */
626{ 1512 list_for_each_entry_safe(cur, next, fn_list, node)
627 static const char *feature_names[] = { 1513 if (!cur->needs_confirm)
628 [DCCPF_RESERVED] = "Reserved", 1514 dccp_feat_list_pop(cur);
629 [DCCPF_CCID] = "CCID",
630 [DCCPF_SHORT_SEQNOS] = "Allow Short Seqnos",
631 [DCCPF_SEQUENCE_WINDOW] = "Sequence Window",
632 [DCCPF_ECN_INCAPABLE] = "ECN Incapable",
633 [DCCPF_ACK_RATIO] = "Ack Ratio",
634 [DCCPF_SEND_ACK_VECTOR] = "Send ACK Vector",
635 [DCCPF_SEND_NDP_COUNT] = "Send NDP Count",
636 [DCCPF_MIN_CSUM_COVER] = "Min. Csum Coverage",
637 [DCCPF_DATA_CHECKSUM] = "Send Data Checksum",
638 };
639 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
640 return feature_names[DCCPF_RESERVED];
641 1515
642 if (feat >= DCCPF_MIN_CCID_SPECIFIC) 1516 dccp_pr_debug("Activation OK\n");
643 return "CCID-specific"; 1517 return 0;
644 1518
645 return feature_names[feat]; 1519activation_failed:
1520 /*
1521 * We clean up everything that may have been allocated, since
1522 * it is difficult to track at which stage negotiation failed.
1523 * This is ok, since all allocation functions below are robust
1524 * against NULL arguments.
1525 */
1526 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1527 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1528 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1529 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1530 dp->dccps_hc_rx_ackvec = NULL;
1531 return -1;
646} 1532}
647
648EXPORT_SYMBOL_GPL(dccp_feat_name);
649#endif /* CONFIG_IP_DCCP_DEBUG */