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, 461 insertions, 1344 deletions
diff --git a/net/dccp/feat.c b/net/dccp/feat.c
index f94c7c9d1a7f..933a0ecf8d46 100644
--- a/net/dccp/feat.c
+++ b/net/dccp/feat.c
@@ -1,19 +1,11 @@
1/* 1/*
2 * net/dccp/feat.c 2 * net/dccp/feat.c
3 * 3 *
4 * Feature negotiation for the DCCP protocol (RFC 4340, section 6) 4 * An implementation of the DCCP protocol
5 * 5 * Andrea Bittau <a.bittau@cs.ucl.ac.uk>
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 *
11 * 6 *
12 * ASSUMPTIONS 7 * ASSUMPTIONS
13 * ----------- 8 * -----------
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.
17 * o All currently known SP features have 1-byte quantities. If in the future 9 * o All currently known SP features have 1-byte quantities. If in the future
18 * extensions of RFCs 4340..42 define features with item lengths larger than 10 * extensions of RFCs 4340..42 define features with item lengths larger than
19 * one byte, a feature-specific extension of the code will be required. 11 * one byte, a feature-specific extension of the code will be required.
@@ -23,1510 +15,635 @@
23 * as published by the Free Software Foundation; either version 15 * as published by the Free Software Foundation; either version
24 * 2 of the License, or (at your option) any later version. 16 * 2 of the License, or (at your option) any later version.
25 */ 17 */
18
26#include <linux/module.h> 19#include <linux/module.h>
20
27#include "ccid.h" 21#include "ccid.h"
28#include "feat.h" 22#include "feat.h"
29 23
30/* feature-specific sysctls - initialised to the defaults from RFC 4340, 6.4 */ 24#define DCCP_FEAT_SP_NOAGREE (-123)
31unsigned long sysctl_dccp_sequence_window __read_mostly = 100;
32int sysctl_dccp_rx_ccid __read_mostly = 2,
33 sysctl_dccp_tx_ccid __read_mostly = 2;
34 25
35/* 26int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
36 * Feature activation handlers. 27 u8 *val, u8 len, gfp_t gfp)
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)
42{ 28{
43 struct dccp_sock *dp = dccp_sk(sk); 29 struct dccp_opt_pend *opt;
44 struct ccid *new_ccid = ccid_new(ccid, sk, rx, gfp_any());
45 30
46 if (new_ccid == NULL) 31 dccp_feat_debug(type, feature, *val);
47 return -ENOMEM;
48 32
49 if (rx) { 33 if (len > 3) {
50 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 34 DCCP_WARN("invalid length %d\n", len);
51 dp->dccps_hc_rx_ccid = new_ccid; 35 return -EINVAL;
52 } else { 36 }
53 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 37 /* XXX add further sanity checks */
54 dp->dccps_hc_tx_ccid = new_ccid; 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 }
55 } 52 }
56 return 0;
57}
58 53
59static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx) 54 /* negotiation for a new feature */
60{ 55 opt = kmalloc(sizeof(*opt), gfp);
61 struct dccp_sock *dp = dccp_sk(sk); 56 if (opt == NULL)
57 return -ENOMEM;
62 58
63 if (rx) { 59 opt->dccpop_type = type;
64 dp->dccps_r_seq_win = seq_win; 60 opt->dccpop_feat = feature;
65 /* propagate changes to update SWL/SWH */ 61 opt->dccpop_len = len;
66 dccp_update_gsr(sk, dp->dccps_gsr); 62 opt->dccpop_val = val;
67 } else { 63 opt->dccpop_conf = 0;
68 dp->dccps_l_seq_win = seq_win; 64 opt->dccpop_sc = NULL;
69 /* propagate changes to update AWL */
70 dccp_update_gss(sk, dp->dccps_gss);
71 }
72 return 0;
73}
74 65
75static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx) 66 BUG_ON(opt->dccpop_val == NULL);
76{ 67
77#ifndef __CCID2_COPES_GRACEFULLY_WITH_DYNAMIC_ACK_RATIO_UPDATES__ 68 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
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; 69 return 0;
94} 70}
95 71
96static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx) 72EXPORT_SYMBOL_GPL(dccp_feat_change);
73
74static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
97{ 75{
98 struct dccp_sock *dp = dccp_sk(sk); 76 struct dccp_sock *dp = dccp_sk(sk);
77 struct dccp_minisock *dmsk = dccp_msk(sk);
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
87 new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
88 if (new_ccid == NULL)
89 return -ENOMEM;
99 90
100 if (rx) { 91 if (rx) {
101 if (enable && dp->dccps_hc_rx_ackvec == NULL) { 92 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
102 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any()); 93 dp->dccps_hc_rx_ccid = new_ccid;
103 if (dp->dccps_hc_rx_ackvec == NULL) 94 dmsk->dccpms_rx_ccid = new_ccid_nr;
104 return -ENOMEM; 95 } else {
105 } else if (!enable) { 96 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
106 dccp_ackvec_free(dp->dccps_hc_rx_ackvec); 97 dp->dccps_hc_tx_ccid = new_ccid;
107 dp->dccps_hc_rx_ackvec = NULL; 98 dmsk->dccpms_tx_ccid = new_ccid_nr;
108 }
109 } 99 }
110 return 0;
111}
112 100
113static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
114{
115 if (!rx)
116 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
117 return 0; 101 return 0;
118} 102}
119 103
120/* 104static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
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{ 105{
131 struct dccp_sock *dp = dccp_sk(sk); 106 dccp_feat_debug(type, feat, val);
132 107
133 if (rx) 108 switch (feat) {
134 dp->dccps_pcrlen = cscov; 109 case DCCPF_CCID:
135 else { 110 return dccp_feat_update_ccid(sk, type, val);
136 if (dp->dccps_pcslen == 0) 111 default:
137 dp->dccps_pcslen = cscov; 112 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
138 else if (cscov > dp->dccps_pcslen) 113 dccp_feat_typename(type), feat);
139 DCCP_WARN("CsCov %u too small, peer requires >= %u\n", 114 break;
140 dp->dccps_pcslen, (u8)cscov);
141 } 115 }
142 return 0; 116 return 0;
143} 117}
144 118
145static const struct { 119static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
146 u8 feat_num; /* DCCPF_xxx */ 120 u8 *rpref, u8 rlen)
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{ 121{
189 /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */ 122 struct dccp_sock *dp = dccp_sk(sk);
190 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM) 123 u8 *spref, slen, *res = NULL;
191 return feat_num - 1; 124 int i, j, rc, agree = 1;
192 125
126 BUG_ON(rpref == NULL);
127
128 /* check if we are the black sheep */
129 if (dp->dccps_role == DCCP_ROLE_CLIENT) {
130 spref = rpref;
131 slen = rlen;
132 rpref = opt->dccpop_val;
133 rlen = opt->dccpop_len;
134 } else {
135 spref = opt->dccpop_val;
136 slen = opt->dccpop_len;
137 }
193 /* 138 /*
194 * Other features: add cases for new feature types here after adding 139 * Now we have server preference list in spref and client preference in
195 * them to the above table. 140 * rpref
196 */ 141 */
197 switch (feat_num) { 142 BUG_ON(spref == NULL);
198 case DCCPF_SEND_LEV_RATE: 143 BUG_ON(rpref == NULL);
199 return DCCP_FEAT_SUPPORTED_MAX - 1;
200 }
201 return -1;
202}
203
204static u8 dccp_feat_type(u8 feat_num)
205{
206 int idx = dccp_feat_index(feat_num);
207
208 if (idx < 0)
209 return FEAT_UNKNOWN;
210 return dccp_feat_table[idx].reconciliation;
211}
212 144
213static int dccp_feat_default_value(u8 feat_num) 145 /* FIXME sanity check vals */
214{
215 int idx = dccp_feat_index(feat_num);
216 146
217 return idx < 0 ? : dccp_feat_table[idx].default_value; 147 /* Are values in any order? XXX Lame "algorithm" here */
218} 148 for (i = 0; i < slen; i++) {
219 149 for (j = 0; j < rlen; j++) {
220/* 150 if (spref[i] == rpref[j]) {
221 * Debugging and verbose-printing section 151 res = &spref[i];
222 */ 152 break;
223static const char *dccp_feat_fname(const u8 feat) 153 }
224{ 154 }
225 static const char *feature_names[] = { 155 if (res)
226 [DCCPF_RESERVED] = "Reserved", 156 break;
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";
259 } 157 }
260 return NULL;
261}
262 158
263static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val) 159 /* we didn't agree on anything */
264{ 160 if (res == NULL) {
265 u8 i, type = dccp_feat_type(feat_num); 161 /* confirm previous value */
266 162 switch (opt->dccpop_feat) {
267 if (val == NULL || (type == FEAT_SP && val->sp.vec == NULL)) 163 case DCCPF_CCID:
268 dccp_pr_debug_cat("(NULL)"); 164 /* XXX did i get this right? =P */
269 else if (type == FEAT_SP) 165 if (opt->dccpop_type == DCCPO_CHANGE_L)
270 for (i = 0; i < val->sp.len; i++) 166 res = &dccp_msk(sk)->dccpms_tx_ccid;
271 dccp_pr_debug_cat("%s%u", i ? " " : "", val->sp.vec[i]); 167 else
272 else if (type == FEAT_NN) 168 res = &dccp_msk(sk)->dccpms_rx_ccid;
273 dccp_pr_debug_cat("%llu", (unsigned long long)val->nn); 169 break;
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 170
288static void dccp_feat_print_entry(struct dccp_feat_entry const *entry) 171 default:
289{ 172 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
290 dccp_debug(" * %s %s = ", entry->is_local ? "local" : "remote", 173 /* XXX implement res */
291 dccp_feat_fname(entry->feat_num)); 174 return -EFAULT;
292 dccp_feat_printval(entry->feat_num, &entry->val); 175 }
293 dccp_pr_debug_cat(", state=%s %s\n", dccp_feat_sname[entry->state],
294 entry->needs_confirm ? "(Confirm pending)" : "");
295}
296 176
297#define dccp_feat_print_opt(opt, feat, val, len, mandatory) do { \ 177 dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
298 dccp_pr_debug("%s(%s, ", dccp_feat_oname(opt), dccp_feat_fname(feat));\ 178 agree = 0; /* this is used for mandatory options... */
299 dccp_feat_printvals(feat, val, len); \ 179 }
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
313 180
314static int __dccp_feat_activate(struct sock *sk, const int idx, 181 /* need to put result and our preference list */
315 const bool is_local, dccp_feat_val const *fval) 182 rlen = 1 + opt->dccpop_len;
316{ 183 rpref = kmalloc(rlen, GFP_ATOMIC);
317 bool rx; 184 if (rpref == NULL)
318 u64 val; 185 return -ENOMEM;
319 186
320 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX) 187 *rpref = *res;
321 return -1; 188 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
322 if (dccp_feat_table[idx].activation_hdlr == NULL)
323 return 0;
324 189
325 if (fval == NULL) { 190 /* put it in the "confirm queue" */
326 val = dccp_feat_table[idx].default_value; 191 if (opt->dccpop_sc == NULL) {
327 } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) { 192 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
328 if (fval->sp.vec == NULL) { 193 if (opt->dccpop_sc == NULL) {
329 /* 194 kfree(rpref);
330 * This can happen when an empty Confirm is sent 195 return -ENOMEM;
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];
338 } 196 }
339 } else { 197 } else {
340 val = fval->nn; 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);
213 opt->dccpop_sc = NULL;
214 return rc;
341 } 215 }
342 216
343 /* Location is RX if this is a local-RX or remote-TX feature */ 217 dccp_pr_debug("Will confirm %d\n", *rpref);
344 rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
345
346 dccp_debug(" -> activating %s %s, %sval=%llu\n", rx ? "RX" : "TX",
347 dccp_feat_fname(dccp_feat_table[idx].feat_num),
348 fval ? "" : "default ", (unsigned long long)val);
349
350 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
351}
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 218
374/* copy constructor, fval must not already contain allocated memory */ 219 /* say we want to change to X but we just got a confirm X, suppress our
375static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len) 220 * change
376{ 221 */
377 fval->sp.len = len; 222 if (!opt->dccpop_conf) {
378 if (fval->sp.len > 0) { 223 if (*opt->dccpop_val == *res)
379 fval->sp.vec = kmemdup(val, len, gfp_any()); 224 opt->dccpop_conf = 1;
380 if (fval->sp.vec == NULL) { 225 dccp_pr_debug("won't ask for change of same feature\n");
381 fval->sp.len = 0;
382 return -ENOBUFS;
383 }
384 } 226 }
385 return 0;
386}
387 227
388static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val) 228 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
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));
395} 229}
396 230
397static struct dccp_feat_entry * 231static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
398 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
399{ 232{
400 struct dccp_feat_entry *new; 233 struct dccp_minisock *dmsk = dccp_msk(sk);
401 u8 type = dccp_feat_type(original->feat_num); 234 struct dccp_opt_pend *opt;
402 235 int rc = 1;
403 if (type == FEAT_UNKNOWN) 236 u8 t;
404 return NULL;
405 237
406 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any()); 238 /*
407 if (new == NULL) 239 * We received a CHANGE. We gotta match it against our own preference
408 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;
409 247
410 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val, 248 /* find our preference list for this feature */
411 original->val.sp.vec, 249 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
412 original->val.sp.len)) { 250 if (opt->dccpop_type != t || opt->dccpop_feat != feature)
413 kfree(new); 251 continue;
414 return NULL;
415 }
416 return new;
417}
418 252
419static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry) 253 /* find the winner from the two preference lists */
420{ 254 rc = dccp_feat_reconcile(sk, opt, val, len);
421 if (entry != NULL) { 255 break;
422 dccp_feat_val_destructor(entry->feat_num, &entry->val);
423 kfree(entry);
424 } 256 }
425}
426 257
427/* 258 /* We didn't deal with the change. This can happen if we have no
428 * List management functions 259 * preference list for the feature. In fact, it just shouldn't
429 * 260 * happen---if we understand a feature, we should have a preference list
430 * Feature negotiation lists rely on and maintain the following invariants: 261 * with at least the default value.
431 * - each feat_num in the list is known, i.e. we know its type and default value 262 */
432 * - each feat_num/is_local combination is unique (old entries are overwritten) 263 BUG_ON(rc == 1);
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 264
441 list_for_each_entry(entry, fn_list, node) 265 return rc;
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;
447} 266}
448 267
449/** 268static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
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)
458{ 269{
459 struct dccp_feat_entry *entry; 270 struct dccp_opt_pend *opt;
460 271 struct dccp_minisock *dmsk = dccp_msk(sk);
461 list_for_each_entry(entry, head, node) 272 u8 *copy;
462 if (entry->feat_num == feat && entry->is_local == local) { 273 int rc;
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 }
469 274
470 entry = kmalloc(sizeof(*entry), gfp_any()); 275 /* NN features must be Change L (sec. 6.3.2) */
471 if (entry != NULL) { 276 if (type != DCCPO_CHANGE_L) {
472 entry->feat_num = feat; 277 dccp_pr_debug("received %s for NN feature %d\n",
473 entry->is_local = local; 278 dccp_feat_typename(type), feature);
474 list_add_tail(&entry->node, head); 279 return -EFAULT;
475 } 280 }
476 return entry;
477}
478 281
479/** 282 /* XXX sanity check opt val */
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);
491 283
492 if (new == NULL) 284 /* copy option so we can confirm it */
285 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
286 if (opt == NULL)
493 return -ENOMEM; 287 return -ENOMEM;
494 288
495 new->feat_num = feat; 289 copy = kmemdup(val, len, GFP_ATOMIC);
496 new->is_local = local; 290 if (copy == NULL) {
497 new->state = FEAT_INITIALISING; 291 kfree(opt);
498 new->needs_confirm = 0; 292 return -ENOMEM;
499 new->empty_confirm = 0; 293 }
500 new->val = *fval;
501 new->needs_mandatory = mandatory;
502 294
503 return 0; 295 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
504} 296 opt->dccpop_feat = feature;
297 opt->dccpop_val = copy;
298 opt->dccpop_len = len;
505 299
506/** 300 /* change feature */
507 * dccp_feat_push_confirm - Add a Confirm entry to the FN list 301 rc = dccp_feat_update(sk, type, feature, *val);
508 * @fn_list: feature-negotiation list to add to 302 if (rc) {
509 * @feat: one of %dccp_feature_numbers 303 kfree(opt->dccpop_val);
510 * @local: whether local (1) or remote (0) @feat_num is being confirmed 304 kfree(opt);
511 * @fval: pointer to NN/SP value to be inserted or NULL 305 return rc;
512 * Returns 0 on success, a Reset code for further processing otherwise. 306 }
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);
518 307
519 if (new == NULL) 308 dccp_feat_debug(type, feature, *copy);
520 return DCCP_RESET_CODE_TOO_BUSY;
521 309
522 new->feat_num = feat; 310 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
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;
531 311
532 return 0; 312 return 0;
533} 313}
534 314
535static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local) 315static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
316 u8 type, u8 feature)
536{ 317{
537 return dccp_feat_push_confirm(fn_list, feat, local, NULL); 318 /* XXX check if other confirms for that are queued and recycle slot */
538} 319 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
539 320
540static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry) 321 if (opt == NULL) {
541{ 322 /* XXX what do we do? Ignoring should be fine. It's a change
542 list_del(&entry->node); 323 * after all =P
543 dccp_feat_entry_destructor(entry); 324 */
544} 325 return;
545
546void dccp_feat_list_purge(struct list_head *fn_list)
547{
548 struct dccp_feat_entry *entry, *next;
549
550 list_for_each_entry_safe(entry, next, fn_list, node)
551 dccp_feat_entry_destructor(entry);
552 INIT_LIST_HEAD(fn_list);
553}
554EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
555
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);
567 } 326 }
568 return 0;
569 327
570cloning_failed: 328 switch (type) {
571 dccp_feat_list_purge(to); 329 case DCCPO_CHANGE_L:
572 return -ENOMEM; 330 opt->dccpop_type = DCCPO_CONFIRM_R;
573} 331 break;
332 case DCCPO_CHANGE_R:
333 opt->dccpop_type = DCCPO_CONFIRM_L;
334 break;
335 default:
336 DCCP_WARN("invalid type %d\n", type);
337 kfree(opt);
338 return;
339 }
340 opt->dccpop_feat = feature;
341 opt->dccpop_val = NULL;
342 opt->dccpop_len = 0;
574 343
575/** 344 /* change feature */
576 * dccp_feat_valid_nn_length - Enforce length constraints on NN options 345 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
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;
587}
588 346
589static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val) 347 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
590{
591 switch (feat_num) {
592 case DCCPF_ACK_RATIO:
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} 348}
599 349
600/* check that SP values are within the ranges defined in RFC 4340 */ 350static void dccp_feat_flush_confirm(struct sock *sk)
601static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
602{ 351{
603 switch (feat_num) { 352 struct dccp_minisock *dmsk = dccp_msk(sk);
604 case DCCPF_CCID: 353 /* Check if there is anything to confirm in the first place */
605 return val == DCCPC_CCID2 || val == DCCPC_CCID3; 354 int yes = !list_empty(&dmsk->dccpms_conf);
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 355
620static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len) 356 if (!yes) {
621{ 357 struct dccp_opt_pend *opt;
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}
629 358
630/** 359 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
631 * dccp_feat_insert_opts - Generate FN options from current list state 360 if (opt->dccpop_conf) {
632 * @skb: next sk_buff to be sent to the peer 361 yes = 1;
633 * @dp: for client during handshake and general negotiation 362 break;
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;
665 } 363 }
666 } 364 }
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;
678 } 365 }
679 return 0;
680}
681
682/**
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 };
694
695 if (dccp_feat_type(feat) != FEAT_NN ||
696 !dccp_feat_is_valid_nn_val(feat, nn_val))
697 return -EINVAL;
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);
704}
705
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)
717{
718 dccp_feat_val fval;
719 366
720 if (dccp_feat_type(feat) != FEAT_SP || 367 if (!yes)
721 !dccp_feat_sp_list_ok(feat, sp_val, sp_len)) 368 return;
722 return -EINVAL;
723
724 /* Avoid negotiating alien CCIDs by only advertising supported ones */
725 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
726 return -EOPNOTSUPP;
727
728 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
729 return -ENOMEM;
730 369
731 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval); 370 /* OK there is something to confirm... */
371 /* XXX check if packet is in flight? Send delayed ack?? */
372 if (sk->sk_state == DCCP_OPEN)
373 dccp_send_ack(sk);
732} 374}
733 375
734/** 376int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
735 * dccp_feat_register_sp - Register requests to change SP feature values
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{ 377{
756 /* any changes must be registered before establishing the connection */ 378 int rc;
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 379
764/** 380 dccp_feat_debug(type, feature, *val);
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 381
782 if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN) 382 /* figure out if it's SP or NN feature */
783 return 0; 383 switch (feature) {
384 /* deal with SP features */
385 case DCCPF_CCID:
386 rc = dccp_feat_sp(sk, type, feature, val, len);
387 break;
784 388
785 if (dccp_feat_type(feat) != FEAT_NN || 389 /* deal with NN features */
786 !dccp_feat_is_valid_nn_val(feat, nn_val)) 390 case DCCPF_ACK_RATIO:
787 return -EINVAL; 391 rc = dccp_feat_nn(sk, type, feature, val, len);
392 break;
788 393
789 entry = dccp_feat_list_lookup(fn, feat, 1); 394 /* XXX implement other features */
790 if (entry != NULL) { 395 default:
791 dccp_pr_debug("Ignoring %llu, entry %llu exists in state %s\n", 396 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
792 (unsigned long long)nn_val, 397 dccp_feat_typename(type), feature);
793 (unsigned long long)entry->val.nn, 398 rc = -EFAULT;
794 dccp_feat_sname[entry->state]); 399 break;
795 return 0;
796 } 400 }
797 401
798 if (dccp_feat_activate(sk, feat, 1, &fval)) 402 /* check if there were problems changing features */
799 return -EADV; 403 if (rc) {
800 404 /* If we don't agree on SP, we sent a confirm for old value.
801 inet_csk_schedule_ack(sk); 405 * However we propagate rc to caller in case option was
802 return dccp_feat_push_change(fn, feat, 1, 0, &fval); 406 * mandatory
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.
821 */ 407 */
822 { /* Dependencies of the receiver-side (remote) CCID2 */ 408 if (rc != DCCP_FEAT_SP_NOAGREE)
823 { 409 dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
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;
907 } 410 }
908}
909 411
910/** 412 /* generate the confirm [if required] */
911 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID 413 dccp_feat_flush_confirm(sk);
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);
932 return rc;
933}
934
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 414
948 /* 415 return rc;
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} 416}
966 417
967/** 418EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
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)
973{
974 struct list_head *fn = &dreq->dreq_featneg;
975 struct dccp_feat_entry *entry;
976 u8 is_local, ccid;
977
978 for (is_local = 0; is_local <= 1; is_local++) {
979 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
980
981 if (entry != NULL && !entry->empty_confirm)
982 ccid = entry->val.sp.vec[0];
983 else
984 ccid = dccp_feat_default_value(DCCPF_CCID);
985
986 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
987 return -1;
988 }
989 return 0;
990}
991 419
992/* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */ 420int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
993static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen) 421 u8 *val, u8 len)
994{ 422{
995 u8 c, s; 423 u8 t;
424 struct dccp_opt_pend *opt;
425 struct dccp_minisock *dmsk = dccp_msk(sk);
426 int found = 0;
427 int all_confirmed = 1;
996 428
997 for (s = 0; s < slen; s++) 429 dccp_feat_debug(type, feature, *val);
998 for (c = 0; c < clen; c++)
999 if (servlist[s] == clilist[c])
1000 return servlist[s];
1001 return -1;
1002}
1003 430
1004/** 431 /* locate our change request */
1005 * dccp_feat_prefer - Move preferred entry to the start of array 432 switch (type) {
1006 * Reorder the @array_len elements in @array so that @preferred_value comes 433 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
1007 * first. Returns >0 to indicate that @preferred_value does occur in @array. 434 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
1008 */ 435 default: DCCP_WARN("invalid type %d\n", type);
1009static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len) 436 return 1;
1010{
1011 u8 i, does_occur = 0;
1012 437
1013 if (array != NULL) {
1014 for (i = 0; i < array_len; i++)
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 } 438 }
1022 return does_occur; 439 /* XXX sanity check feature value */
1023}
1024 440
1025/** 441 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
1026 * dccp_feat_reconcile - Reconcile SP preference lists 442 if (!opt->dccpop_conf && opt->dccpop_type == t &&
1027 * @fval: SP list to reconcile into 443 opt->dccpop_feat == feature) {
1028 * @arr: received SP preference list 444 found = 1;
1029 * @len: length of @arr in bytes 445 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
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;
1039 446
1040 if (!fv->sp.vec || !arr) { 447 /* XXX do sanity check */
1041 DCCP_CRIT("NULL feature value or array");
1042 return 0;
1043 }
1044 448
1045 if (is_server) 449 opt->dccpop_conf = 1;
1046 rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
1047 else
1048 rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
1049
1050 if (!reorder)
1051 return rc;
1052 if (rc < 0)
1053 return 0;
1054 450
1055 /* 451 /* We got a confirmation---change the option */
1056 * Reorder list: used for activating features and in dccp_insert_fn_opt. 452 dccp_feat_update(sk, opt->dccpop_type,
1057 */ 453 opt->dccpop_feat, *val);
1058 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
1059}
1060 454
1061/** 455 /* XXX check the return value of dccp_feat_update */
1062 * dccp_feat_change_recv - Process incoming ChangeL/R options 456 break;
1063 * @fn: feature-negotiation list to update 457 }
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)
1073{
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;
1091
1092 /* 6.3.2: "The feature remote MUST accept any valid value..." */
1093 fval.nn = dccp_decode_value_var(val, len);
1094 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1095 goto unknown_feature_or_value;
1096 458
1097 return dccp_feat_push_confirm(fn, feat, local, &fval); 459 if (!opt->dccpop_conf)
460 all_confirmed = 0;
1098 } 461 }
1099 462
1100 /* 463 /* fix re-transmit timer */
1101 * Unidirectional/simultaneous negotiation of SP features (6.3.1) 464 /* XXX gotta make sure that no option negotiation occurs during
465 * connection shutdown. Consider that the CLOSEREQ is sent and timer is
466 * on. if all options are confirmed it might kill timer which should
467 * remain alive until close is received.
1102 */ 468 */
1103 entry = dccp_feat_list_lookup(fn, feat, local); 469 if (all_confirmed) {
1104 if (entry == NULL) { 470 dccp_pr_debug("clear feat negotiation timer %p\n", sk);
1105 /* 471 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
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;
1131 }
1132
1133 return dccp_feat_push_confirm(fn, feat, local, &fval);
1134
1135 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
1136 return 0;
1137 } 472 }
1138 473
1139 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) { 474 if (!found)
1140 entry->empty_confirm = 0; 475 dccp_pr_debug("%s(%d, ...) never requested\n",
1141 } else if (is_mandatory) { 476 dccp_feat_typename(type), feature);
1142 return DCCP_RESET_CODE_MANDATORY_ERROR;
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;
1157 }
1158 entry->needs_confirm = 1;
1159 entry->needs_mandatory = 0;
1160 entry->state = FEAT_STABLE;
1161 return 0; 477 return 0;
1162
1163unknown_feature_or_value:
1164 if (!is_mandatory)
1165 return dccp_push_empty_confirm(fn, feat, local);
1166
1167not_valid_or_not_known:
1168 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1169 : DCCP_RESET_CODE_OPTION_ERROR;
1170} 478}
1171 479
1172/** 480EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
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)
1181 */
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)
1184{
1185 u8 *plist, plen, type = dccp_feat_type(feat);
1186 const bool local = (opt == DCCPO_CONFIRM_R);
1187 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1188
1189 dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1190
1191 if (entry == NULL) { /* nothing queued: ignore or handle error */
1192 if (is_mandatory && type == FEAT_UNKNOWN)
1193 return DCCP_RESET_CODE_MANDATORY_ERROR;
1194
1195 if (!local && type == FEAT_NN) /* 6.3.2 */
1196 goto confirmation_failed;
1197 return 0;
1198 }
1199
1200 if (entry->state != FEAT_CHANGING) /* 6.6.2 */
1201 return 0;
1202
1203 if (len == 0) {
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.
1212 */
1213 dccp_feat_list_pop(entry);
1214 return 0;
1215 }
1216 481
1217 if (type == FEAT_NN) { 482void dccp_feat_clean(struct dccp_minisock *dmsk)
1218 if (len > sizeof(entry->val.nn)) 483{
1219 goto confirmation_failed; 484 struct dccp_opt_pend *opt, *next;
1220 485
1221 if (entry->val.nn == dccp_decode_value_var(val, len)) 486 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
1222 goto confirmation_succeeded; 487 dccpop_node) {
488 BUG_ON(opt->dccpop_val == NULL);
489 kfree(opt->dccpop_val);
1223 490
1224 DCCP_WARN("Bogus Confirm for non-existing value\n"); 491 if (opt->dccpop_sc != NULL) {
1225 goto confirmation_failed; 492 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
1226 } 493 kfree(opt->dccpop_sc->dccpoc_val);
494 kfree(opt->dccpop_sc);
495 }
1227 496
1228 /* 497 kfree(opt);
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;
1242 } 498 }
499 INIT_LIST_HEAD(&dmsk->dccpms_pending);
1243 500
1244 /* Check whether the peer got the reconciliation right (6.6.8) */ 501 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
1245 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) { 502 BUG_ON(opt == NULL);
1246 DCCP_WARN("Confirm selected the wrong value %u\n", *val); 503 if (opt->dccpop_val != NULL)
1247 return DCCP_RESET_CODE_OPTION_ERROR; 504 kfree(opt->dccpop_val);
505 kfree(opt);
1248 } 506 }
1249 entry->val.sp.vec[0] = *val; 507 INIT_LIST_HEAD(&dmsk->dccpms_conf);
1250
1251confirmation_succeeded:
1252 entry->state = FEAT_STABLE;
1253 return 0;
1254
1255confirmation_failed:
1256 DCCP_WARN("Confirmation failed\n");
1257 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1258 : DCCP_RESET_CODE_OPTION_ERROR;
1259} 508}
1260 509
1261/** 510EXPORT_SYMBOL_GPL(dccp_feat_clean);
1262 * dccp_feat_handle_nn_established - Fast-path reception of NN options 511
1263 * @sk: socket of an established DCCP connection 512/* this is to be called only when a listening sock creates its child. It is
1264 * @mandatory: whether @opt was preceded by a Mandatory option 513 * assumed by the function---the confirm is not duplicated, but rather it is
1265 * @opt: %DCCPO_CHANGE_L | %DCCPO_CONFIRM_R (NN only) 514 * "passed on".
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 */ 515 */
1278static u8 dccp_feat_handle_nn_established(struct sock *sk, u8 mandatory, u8 opt, 516int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
1279 u8 feat, u8 *val, u8 len)
1280{ 517{
1281 struct list_head *fn = &dccp_sk(sk)->dccps_featneg; 518 struct dccp_minisock *olddmsk = dccp_msk(oldsk);
1282 const bool local = (opt == DCCPO_CONFIRM_R); 519 struct dccp_minisock *newdmsk = dccp_msk(newsk);
1283 struct dccp_feat_entry *entry; 520 struct dccp_opt_pend *opt;
1284 u8 type = dccp_feat_type(feat); 521 int rc = 0;
1285 dccp_feat_val fval;
1286 522
1287 dccp_feat_print_opt(opt, feat, val, len, mandatory); 523 INIT_LIST_HEAD(&newdmsk->dccpms_pending);
524 INIT_LIST_HEAD(&newdmsk->dccpms_conf);
1288 525
1289 /* Ignore non-mandatory unknown and non-NN features */ 526 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
1290 if (type == FEAT_UNKNOWN) { 527 struct dccp_opt_pend *newopt;
1291 if (local && !mandatory) 528 /* copy the value of the option */
1292 return 0; 529 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
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 530
1312 if (dccp_feat_push_confirm(fn, feat, local, &fval) || 531 if (val == NULL)
1313 dccp_feat_activate(sk, feat, local, &fval)) 532 goto out_clean;
1314 return DCCP_RESET_CODE_TOO_BUSY;
1315 533
1316 /* set the `Ack Pending' flag to piggyback a Confirm */ 534 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
1317 inet_csk_schedule_ack(sk); 535 if (newopt == NULL) {
1318 536 kfree(val);
1319 } else if (opt == DCCPO_CONFIRM_R) { 537 goto out_clean;
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 } 538 }
1329 539
1330 /* It has been confirmed - so remove the entry */ 540 /* insert the option */
1331 dccp_feat_list_pop(entry); 541 newopt->dccpop_val = val;
542 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
1332 543
1333 } else { 544 /* XXX what happens with backlogs and multiple connections at
1334 DCCP_WARN("Received illegal option %u\n", opt); 545 * once...
1335 goto fast_path_failed; 546 */
547 /* the master socket no longer needs to worry about confirms */
548 opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
549
550 /* reset state for a new socket */
551 opt->dccpop_conf = 0;
1336 } 552 }
1337 return 0;
1338 553
1339fast_path_unknown: 554 /* XXX not doing anything about the conf queue */
1340 if (!mandatory) 555
1341 return dccp_push_empty_confirm(fn, feat, local); 556out:
557 return rc;
1342 558
1343fast_path_failed: 559out_clean:
1344 return mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR 560 dccp_feat_clean(newdmsk);
1345 : DCCP_RESET_CODE_OPTION_ERROR; 561 rc = -ENOMEM;
562 goto out;
1346} 563}
1347 564
1348/** 565EXPORT_SYMBOL_GPL(dccp_feat_clone);
1349 * dccp_feat_parse_options - Process Feature-Negotiation Options 566
1350 * @sk: for general use and used by the client during connection setup 567static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat,
1351 * @dreq: used by the server during connection setup 568 u8 *val, u8 len)
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)
1361{ 569{
1362 struct dccp_sock *dp = dccp_sk(sk); 570 int rc = -ENOMEM;
1363 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg; 571 u8 *copy = kmemdup(val, len, GFP_KERNEL);
1364 bool server = false;
1365 572
1366 switch (sk->sk_state) { 573 if (copy != NULL) {
1367 /* 574 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL);
1368 * Negotiation during connection setup 575 if (rc)
1369 */ 576 kfree(copy);
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);
1392 } 577 }
1393 return 0; /* ignore FN options in all other states */ 578 return rc;
1394} 579}
1395 580
1396/** 581int dccp_feat_init(struct dccp_minisock *dmsk)
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)
1407{ 582{
1408 struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1409 u8 on = 1, off = 0;
1410 int rc; 583 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;
1421 584
1422 /* Server-priority (SP) features */ 585 INIT_LIST_HEAD(&dmsk->dccpms_pending);
1423 586 INIT_LIST_HEAD(&dmsk->dccpms_conf);
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;
1428 587
1429 /* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */ 588 /* CCID L */
1430 rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1); 589 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
590 &dmsk->dccpms_tx_ccid, 1);
1431 if (rc) 591 if (rc)
1432 return rc; 592 goto out;
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;
1452 593
1453 rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len); 594 /* CCID R */
595 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
596 &dmsk->dccpms_rx_ccid, 1);
1454 if (rc) 597 if (rc)
1455 goto free_ccid_lists; 598 goto out;
1456 599
1457 rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len); 600 /* Ack ratio */
1458 601 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
1459free_ccid_lists: 602 &dmsk->dccpms_ack_ratio, 1);
1460 kfree(tx.val); 603out:
1461 kfree(rx.val);
1462 return rc; 604 return rc;
1463} 605}
1464 606
1465int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list) 607EXPORT_SYMBOL_GPL(dccp_feat_init);
1466{
1467 struct dccp_sock *dp = dccp_sk(sk);
1468 struct dccp_feat_entry *cur, *next;
1469 int idx;
1470 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1471 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1472 };
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 608
1483 idx = dccp_feat_index(cur->feat_num); 609#ifdef CONFIG_IP_DCCP_DEBUG
1484 if (idx < 0) { 610const char *dccp_feat_typename(const u8 type)
1485 DCCP_BUG("Unknown feature %u", cur->feat_num); 611{
1486 goto activation_failed; 612 switch(type) {
1487 } 613 case DCCPO_CHANGE_L: return("ChangeL");
1488 if (cur->state != FEAT_STABLE) { 614 case DCCPO_CONFIRM_L: return("ConfirmL");
1489 DCCP_CRIT("Negotiation of %s %s failed in state %s", 615 case DCCPO_CHANGE_R: return("ChangeR");
1490 cur->is_local ? "local" : "remote", 616 case DCCPO_CONFIRM_R: return("ConfirmR");
1491 dccp_feat_fname(cur->feat_num), 617 /* the following case must not appear in feature negotation */
1492 dccp_feat_sname[cur->state]); 618 default: dccp_pr_debug("unknown type %d [BUG!]\n", type);
1493 goto activation_failed;
1494 }
1495 fvals[idx][cur->is_local] = &cur->val;
1496 } 619 }
620 return NULL;
621}
1497 622
1498 /* 623EXPORT_SYMBOL_GPL(dccp_feat_typename);
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 }
1510 624
1511 /* Clean up Change options which have been confirmed already */ 625const char *dccp_feat_name(const u8 feat)
1512 list_for_each_entry_safe(cur, next, fn_list, node) 626{
1513 if (!cur->needs_confirm) 627 static const char *feature_names[] = {
1514 dccp_feat_list_pop(cur); 628 [DCCPF_RESERVED] = "Reserved",
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];
1515 641
1516 dccp_pr_debug("Activation OK\n"); 642 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
1517 return 0; 643 return "CCID-specific";
1518 644
1519activation_failed: 645 return feature_names[feat];
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;
1532} 646}
647
648EXPORT_SYMBOL_GPL(dccp_feat_name);
649#endif /* CONFIG_IP_DCCP_DEBUG */