aboutsummaryrefslogtreecommitdiffstats
path: root/net/dccp/feat.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2008-12-28 15:49:40 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2008-12-28 15:49:40 -0500
commit0191b625ca5a46206d2fb862bb08f36f2fcb3b31 (patch)
tree454d1842b1833d976da62abcbd5c47521ebe9bd7 /net/dccp/feat.c
parent54a696bd07c14d3b1192d03ce7269bc59b45209a (diff)
parenteb56092fc168bf5af199d47af50c0d84a96db898 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next-2.6
* git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next-2.6: (1429 commits) net: Allow dependancies of FDDI & Tokenring to be modular. igb: Fix build warning when DCA is disabled. net: Fix warning fallout from recent NAPI interface changes. gro: Fix potential use after free sfc: If AN is enabled, always read speed/duplex from the AN advertising bits sfc: When disabling the NIC, close the device rather than unregistering it sfc: SFT9001: Add cable diagnostics sfc: Add support for multiple PHY self-tests sfc: Merge top-level functions for self-tests sfc: Clean up PHY mode management in loopback self-test sfc: Fix unreliable link detection in some loopback modes sfc: Generate unique names for per-NIC workqueues 802.3ad: use standard ethhdr instead of ad_header 802.3ad: generalize out mac address initializer 802.3ad: initialize ports LACPDU from const initializer 802.3ad: remove typedef around ad_system 802.3ad: turn ports is_individual into a bool 802.3ad: turn ports is_enabled into a bool 802.3ad: make ntt bool ixgbe: Fix set_ringparam in ixgbe to use the same memory pools. ... Fixed trivial IPv4/6 address printing conflicts in fs/cifs/connect.c due to the conversion to %pI (in this networking merge) and the addition of doing IPv6 addresses (from the earlier merge of CIFS).
Diffstat (limited to 'net/dccp/feat.c')
-rw-r--r--net/dccp/feat.c1458
1 files changed, 1027 insertions, 431 deletions
diff --git a/net/dccp/feat.c b/net/dccp/feat.c
index 933a0ecf8d46..30f9fb76b921 100644
--- a/net/dccp/feat.c
+++ b/net/dccp/feat.c
@@ -1,11 +1,17 @@
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 Gerrit Renker <gerrit@erg.abdn.ac.uk>
7 * Rewrote from scratch, some bits from earlier code by
8 * Copyright (c) 2005 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
9 *
6 * 10 *
7 * ASSUMPTIONS 11 * ASSUMPTIONS
8 * ----------- 12 * -----------
13 * o Feature negotiation is coordinated with connection setup (as in TCP), wild
14 * changes of parameters of an established connection are not supported.
9 * o All currently known SP features have 1-byte quantities. If in the future 15 * 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 16 * 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. 17 * one byte, a feature-specific extension of the code will be required.
@@ -15,597 +21,1185 @@
15 * as published by the Free Software Foundation; either version 21 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version. 22 * 2 of the License, or (at your option) any later version.
17 */ 23 */
18
19#include <linux/module.h> 24#include <linux/module.h>
20
21#include "ccid.h" 25#include "ccid.h"
22#include "feat.h" 26#include "feat.h"
23 27
24#define DCCP_FEAT_SP_NOAGREE (-123) 28/*
25 29 * Feature activation handlers.
26int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature, 30 *
27 u8 *val, u8 len, gfp_t gfp) 31 * These all use an u64 argument, to provide enough room for NN/SP features. At
28{ 32 * this stage the negotiated values have been checked to be within their range.
29 struct dccp_opt_pend *opt; 33 */
30 34static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
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
72EXPORT_SYMBOL_GPL(dccp_feat_change);
73
74static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
75{ 35{
76 struct dccp_sock *dp = dccp_sk(sk); 36 struct dccp_sock *dp = dccp_sk(sk);
77 struct dccp_minisock *dmsk = dccp_msk(sk); 37 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 38
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) 39 if (new_ccid == NULL)
89 return -ENOMEM; 40 return -ENOMEM;
90 41
91 if (rx) { 42 if (rx) {
92 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk); 43 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
93 dp->dccps_hc_rx_ccid = new_ccid; 44 dp->dccps_hc_rx_ccid = new_ccid;
94 dmsk->dccpms_rx_ccid = new_ccid_nr;
95 } else { 45 } else {
96 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk); 46 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
97 dp->dccps_hc_tx_ccid = new_ccid; 47 dp->dccps_hc_tx_ccid = new_ccid;
98 dmsk->dccpms_tx_ccid = new_ccid_nr;
99 } 48 }
49 return 0;
50}
100 51
52static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
53{
54 if (!rx)
55 dccp_msk(sk)->dccpms_sequence_window = seq_win;
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_ack_ratio(struct sock *sk, u64 ratio, bool rx)
105{ 60{
106 dccp_feat_debug(type, feat, val); 61 if (rx)
62 dccp_sk(sk)->dccps_r_ack_ratio = ratio;
63 else
64 dccp_sk(sk)->dccps_l_ack_ratio = ratio;
65 return 0;
66}
107 67
108 switch (feat) { 68static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
109 case DCCPF_CCID: 69{
110 return dccp_feat_update_ccid(sk, type, val); 70 struct dccp_sock *dp = dccp_sk(sk);
111 default: 71
112 dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n", 72 if (rx) {
113 dccp_feat_typename(type), feat); 73 if (enable && dp->dccps_hc_rx_ackvec == NULL) {
114 break; 74 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
75 if (dp->dccps_hc_rx_ackvec == NULL)
76 return -ENOMEM;
77 } else if (!enable) {
78 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
79 dp->dccps_hc_rx_ackvec = NULL;
80 }
115 } 81 }
116 return 0; 82 return 0;
117} 83}
118 84
119static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt, 85static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
120 u8 *rpref, u8 rlen)
121{ 86{
122 struct dccp_sock *dp = dccp_sk(sk); 87 if (!rx)
123 u8 *spref, slen, *res = NULL; 88 dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
124 int i, j, rc, agree = 1; 89 return 0;
90}
125 91
126 BUG_ON(rpref == NULL); 92/*
93 * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
94 * `rx' holds when the sending peer informs about his partial coverage via a
95 * ChangeR() option. In the other case, we are the sender and the receiver
96 * announces its coverage via ChangeL() options. The policy here is to honour
97 * such communication by enabling the corresponding partial coverage - but only
98 * if it has not been set manually before; the warning here means that all
99 * packets will be dropped.
100 */
101static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
102{
103 struct dccp_sock *dp = dccp_sk(sk);
127 104
128 /* check if we are the black sheep */ 105 if (rx)
129 if (dp->dccps_role == DCCP_ROLE_CLIENT) { 106 dp->dccps_pcrlen = cscov;
130 spref = rpref; 107 else {
131 slen = rlen; 108 if (dp->dccps_pcslen == 0)
132 rpref = opt->dccpop_val; 109 dp->dccps_pcslen = cscov;
133 rlen = opt->dccpop_len; 110 else if (cscov > dp->dccps_pcslen)
134 } else { 111 DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
135 spref = opt->dccpop_val; 112 dp->dccps_pcslen, (u8)cscov);
136 slen = opt->dccpop_len;
137 } 113 }
138 /* 114 return 0;
139 * Now we have server preference list in spref and client preference in 115}
140 * rpref
141 */
142 BUG_ON(spref == NULL);
143 BUG_ON(rpref == NULL);
144 116
145 /* FIXME sanity check vals */ 117static const struct {
118 u8 feat_num; /* DCCPF_xxx */
119 enum dccp_feat_type rxtx; /* RX or TX */
120 enum dccp_feat_type reconciliation; /* SP or NN */
121 u8 default_value; /* as in 6.4 */
122 int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
123/*
124 * Lookup table for location and type of features (from RFC 4340/4342)
125 * +--------------------------+----+-----+----+----+---------+-----------+
126 * | Feature | Location | Reconc. | Initial | Section |
127 * | | RX | TX | SP | NN | Value | Reference |
128 * +--------------------------+----+-----+----+----+---------+-----------+
129 * | DCCPF_CCID | | X | X | | 2 | 10 |
130 * | DCCPF_SHORT_SEQNOS | | X | X | | 0 | 7.6.1 |
131 * | DCCPF_SEQUENCE_WINDOW | | X | | X | 100 | 7.5.2 |
132 * | DCCPF_ECN_INCAPABLE | X | | X | | 0 | 12.1 |
133 * | DCCPF_ACK_RATIO | | X | | X | 2 | 11.3 |
134 * | DCCPF_SEND_ACK_VECTOR | X | | X | | 0 | 11.5 |
135 * | DCCPF_SEND_NDP_COUNT | | X | X | | 0 | 7.7.2 |
136 * | DCCPF_MIN_CSUM_COVER | X | | X | | 0 | 9.2.1 |
137 * | DCCPF_DATA_CHECKSUM | X | | X | | 0 | 9.3.1 |
138 * | DCCPF_SEND_LEV_RATE | X | | X | | 0 | 4342/8.4 |
139 * +--------------------------+----+-----+----+----+---------+-----------+
140 */
141} dccp_feat_table[] = {
142 { DCCPF_CCID, FEAT_AT_TX, FEAT_SP, 2, dccp_hdlr_ccid },
143 { DCCPF_SHORT_SEQNOS, FEAT_AT_TX, FEAT_SP, 0, NULL },
144 { DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win },
145 { DCCPF_ECN_INCAPABLE, FEAT_AT_RX, FEAT_SP, 0, NULL },
146 { DCCPF_ACK_RATIO, FEAT_AT_TX, FEAT_NN, 2, dccp_hdlr_ack_ratio},
147 { DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_ackvec },
148 { DCCPF_SEND_NDP_COUNT, FEAT_AT_TX, FEAT_SP, 0, dccp_hdlr_ndp },
149 { DCCPF_MIN_CSUM_COVER, FEAT_AT_RX, FEAT_SP, 0, dccp_hdlr_min_cscov},
150 { DCCPF_DATA_CHECKSUM, FEAT_AT_RX, FEAT_SP, 0, NULL },
151 { DCCPF_SEND_LEV_RATE, FEAT_AT_RX, FEAT_SP, 0, NULL },
152};
153#define DCCP_FEAT_SUPPORTED_MAX ARRAY_SIZE(dccp_feat_table)
154
155/**
156 * dccp_feat_index - Hash function to map feature number into array position
157 * Returns consecutive array index or -1 if the feature is not understood.
158 */
159static int dccp_feat_index(u8 feat_num)
160{
161 /* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
162 if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
163 return feat_num - 1;
146 164
147 /* Are values in any order? XXX Lame "algorithm" here */ 165 /*
148 for (i = 0; i < slen; i++) { 166 * Other features: add cases for new feature types here after adding
149 for (j = 0; j < rlen; j++) { 167 * them to the above table.
150 if (spref[i] == rpref[j]) { 168 */
151 res = &spref[i]; 169 switch (feat_num) {
152 break; 170 case DCCPF_SEND_LEV_RATE:
153 } 171 return DCCP_FEAT_SUPPORTED_MAX - 1;
154 }
155 if (res)
156 break;
157 } 172 }
173 return -1;
174}
158 175
159 /* we didn't agree on anything */ 176static u8 dccp_feat_type(u8 feat_num)
160 if (res == NULL) { 177{
161 /* confirm previous value */ 178 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 179
171 default: 180 if (idx < 0)
172 DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat); 181 return FEAT_UNKNOWN;
173 /* XXX implement res */ 182 return dccp_feat_table[idx].reconciliation;
174 return -EFAULT; 183}
175 }
176 184
177 dccp_pr_debug("Don't agree... reconfirming %d\n", *res); 185static int dccp_feat_default_value(u8 feat_num)
178 agree = 0; /* this is used for mandatory options... */ 186{
179 } 187 int idx = dccp_feat_index(feat_num);
188 /*
189 * There are no default values for unknown features, so encountering a
190 * negative index here indicates a serious problem somewhere else.
191 */
192 DCCP_BUG_ON(idx < 0);
180 193
181 /* need to put result and our preference list */ 194 return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
182 rlen = 1 + opt->dccpop_len; 195}
183 rpref = kmalloc(rlen, GFP_ATOMIC);
184 if (rpref == NULL)
185 return -ENOMEM;
186 196
187 *rpref = *res; 197static int __dccp_feat_activate(struct sock *sk, const int idx,
188 memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len); 198 const bool is_local, dccp_feat_val const *fval)
199{
200 bool rx;
201 u64 val;
189 202
190 /* put it in the "confirm queue" */ 203 if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
191 if (opt->dccpop_sc == NULL) { 204 return -1;
192 opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC); 205 if (dccp_feat_table[idx].activation_hdlr == NULL)
193 if (opt->dccpop_sc == NULL) { 206 return 0;
194 kfree(rpref); 207
195 return -ENOMEM; 208 if (fval == NULL) {
209 val = dccp_feat_table[idx].default_value;
210 } else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
211 if (fval->sp.vec == NULL) {
212 /*
213 * This can happen when an empty Confirm is sent
214 * for an SP (i.e. known) feature. In this case
215 * we would be using the default anyway.
216 */
217 DCCP_CRIT("Feature #%d undefined: using default", idx);
218 val = dccp_feat_table[idx].default_value;
219 } else {
220 val = fval->sp.vec[0];
196 } 221 }
197 } else { 222 } else {
198 /* recycle the confirm slot */ 223 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 } 224 }
203 memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
204 225
205 opt->dccpop_sc->dccpoc_val = rpref; 226 /* Location is RX if this is a local-RX or remote-TX feature */
206 opt->dccpop_sc->dccpoc_len = rlen; 227 rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
207 228
208 /* update the option on our side [we are about to send the confirm] */ 229 return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
209 rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res); 230}
210 if (rc) {
211 kfree(opt->dccpop_sc->dccpoc_val);
212 kfree(opt->dccpop_sc);
213 opt->dccpop_sc = NULL;
214 return rc;
215 }
216 231
217 dccp_pr_debug("Will confirm %d\n", *rpref); 232/* Test for "Req'd" feature (RFC 4340, 6.4) */
233static inline int dccp_feat_must_be_understood(u8 feat_num)
234{
235 return feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
236 feat_num == DCCPF_SEQUENCE_WINDOW;
237}
218 238
219 /* say we want to change to X but we just got a confirm X, suppress our 239/* copy constructor, fval must not already contain allocated memory */
220 * change 240static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
221 */ 241{
222 if (!opt->dccpop_conf) { 242 fval->sp.len = len;
223 if (*opt->dccpop_val == *res) 243 if (fval->sp.len > 0) {
224 opt->dccpop_conf = 1; 244 fval->sp.vec = kmemdup(val, len, gfp_any());
225 dccp_pr_debug("won't ask for change of same feature\n"); 245 if (fval->sp.vec == NULL) {
246 fval->sp.len = 0;
247 return -ENOBUFS;
248 }
226 } 249 }
250 return 0;
251}
227 252
228 return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */ 253static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
254{
255 if (unlikely(val == NULL))
256 return;
257 if (dccp_feat_type(feat_num) == FEAT_SP)
258 kfree(val->sp.vec);
259 memset(val, 0, sizeof(*val));
229} 260}
230 261
231static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) 262static struct dccp_feat_entry *
263 dccp_feat_clone_entry(struct dccp_feat_entry const *original)
232{ 264{
233 struct dccp_minisock *dmsk = dccp_msk(sk); 265 struct dccp_feat_entry *new;
234 struct dccp_opt_pend *opt; 266 u8 type = dccp_feat_type(original->feat_num);
235 int rc = 1;
236 u8 t;
237 267
238 /* 268 if (type == FEAT_UNKNOWN)
239 * We received a CHANGE. We gotta match it against our own preference 269 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 270
248 /* find our preference list for this feature */ 271 new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
249 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { 272 if (new == NULL)
250 if (opt->dccpop_type != t || opt->dccpop_feat != feature) 273 return NULL;
251 continue;
252 274
253 /* find the winner from the two preference lists */ 275 if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
254 rc = dccp_feat_reconcile(sk, opt, val, len); 276 original->val.sp.vec,
255 break; 277 original->val.sp.len)) {
278 kfree(new);
279 return NULL;
256 } 280 }
281 return new;
282}
257 283
258 /* We didn't deal with the change. This can happen if we have no 284static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
259 * preference list for the feature. In fact, it just shouldn't 285{
260 * happen---if we understand a feature, we should have a preference list 286 if (entry != NULL) {
261 * with at least the default value. 287 dccp_feat_val_destructor(entry->feat_num, &entry->val);
262 */ 288 kfree(entry);
263 BUG_ON(rc == 1); 289 }
290}
264 291
265 return rc; 292/*
293 * List management functions
294 *
295 * Feature negotiation lists rely on and maintain the following invariants:
296 * - each feat_num in the list is known, i.e. we know its type and default value
297 * - each feat_num/is_local combination is unique (old entries are overwritten)
298 * - SP values are always freshly allocated
299 * - list is sorted in increasing order of feature number (faster lookup)
300 */
301static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
302 u8 feat_num, bool is_local)
303{
304 struct dccp_feat_entry *entry;
305
306 list_for_each_entry(entry, fn_list, node) {
307 if (entry->feat_num == feat_num && entry->is_local == is_local)
308 return entry;
309 else if (entry->feat_num > feat_num)
310 break;
311 }
312 return NULL;
266} 313}
267 314
268static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) 315/**
316 * dccp_feat_entry_new - Central list update routine (called by all others)
317 * @head: list to add to
318 * @feat: feature number
319 * @local: whether the local (1) or remote feature with number @feat is meant
320 * This is the only constructor and serves to ensure the above invariants.
321 */
322static struct dccp_feat_entry *
323 dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
269{ 324{
270 struct dccp_opt_pend *opt; 325 struct dccp_feat_entry *entry;
271 struct dccp_minisock *dmsk = dccp_msk(sk); 326
272 u8 *copy; 327 list_for_each_entry(entry, head, node)
273 int rc; 328 if (entry->feat_num == feat && entry->is_local == local) {
329 dccp_feat_val_destructor(entry->feat_num, &entry->val);
330 return entry;
331 } else if (entry->feat_num > feat) {
332 head = &entry->node;
333 break;
334 }
274 335
275 /* NN features must be Change L (sec. 6.3.2) */ 336 entry = kmalloc(sizeof(*entry), gfp_any());
276 if (type != DCCPO_CHANGE_L) { 337 if (entry != NULL) {
277 dccp_pr_debug("received %s for NN feature %d\n", 338 entry->feat_num = feat;
278 dccp_feat_typename(type), feature); 339 entry->is_local = local;
279 return -EFAULT; 340 list_add_tail(&entry->node, head);
280 } 341 }
342 return entry;
343}
281 344
282 /* XXX sanity check opt val */ 345/**
346 * dccp_feat_push_change - Add/overwrite a Change option in the list
347 * @fn_list: feature-negotiation list to update
348 * @feat: one of %dccp_feature_numbers
349 * @local: whether local (1) or remote (0) @feat_num is meant
350 * @needs_mandatory: whether to use Mandatory feature negotiation options
351 * @fval: pointer to NN/SP value to be inserted (will be copied)
352 */
353static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
354 u8 mandatory, dccp_feat_val *fval)
355{
356 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
283 357
284 /* copy option so we can confirm it */ 358 if (new == NULL)
285 opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
286 if (opt == NULL)
287 return -ENOMEM; 359 return -ENOMEM;
288 360
289 copy = kmemdup(val, len, GFP_ATOMIC); 361 new->feat_num = feat;
290 if (copy == NULL) { 362 new->is_local = local;
291 kfree(opt); 363 new->state = FEAT_INITIALISING;
292 return -ENOMEM; 364 new->needs_confirm = 0;
293 } 365 new->empty_confirm = 0;
366 new->val = *fval;
367 new->needs_mandatory = mandatory;
294 368
295 opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */ 369 return 0;
296 opt->dccpop_feat = feature; 370}
297 opt->dccpop_val = copy;
298 opt->dccpop_len = len;
299 371
300 /* change feature */ 372/**
301 rc = dccp_feat_update(sk, type, feature, *val); 373 * dccp_feat_push_confirm - Add a Confirm entry to the FN list
302 if (rc) { 374 * @fn_list: feature-negotiation list to add to
303 kfree(opt->dccpop_val); 375 * @feat: one of %dccp_feature_numbers
304 kfree(opt); 376 * @local: whether local (1) or remote (0) @feat_num is being confirmed
305 return rc; 377 * @fval: pointer to NN/SP value to be inserted or NULL
306 } 378 * Returns 0 on success, a Reset code for further processing otherwise.
379 */
380static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
381 dccp_feat_val *fval)
382{
383 struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
307 384
308 dccp_feat_debug(type, feature, *copy); 385 if (new == NULL)
386 return DCCP_RESET_CODE_TOO_BUSY;
309 387
310 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf); 388 new->feat_num = feat;
389 new->is_local = local;
390 new->state = FEAT_STABLE; /* transition in 6.6.2 */
391 new->needs_confirm = 1;
392 new->empty_confirm = (fval == NULL);
393 new->val.nn = 0; /* zeroes the whole structure */
394 if (!new->empty_confirm)
395 new->val = *fval;
396 new->needs_mandatory = 0;
311 397
312 return 0; 398 return 0;
313} 399}
314 400
315static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk, 401static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
316 u8 type, u8 feature)
317{ 402{
318 /* XXX check if other confirms for that are queued and recycle slot */ 403 return dccp_feat_push_confirm(fn_list, feat, local, NULL);
319 struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC); 404}
320 405
321 if (opt == NULL) { 406static 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 407{
323 * after all =P 408 list_del(&entry->node);
324 */ 409 dccp_feat_entry_destructor(entry);
325 return; 410}
326 }
327 411
328 switch (type) { 412void dccp_feat_list_purge(struct list_head *fn_list)
329 case DCCPO_CHANGE_L: 413{
330 opt->dccpop_type = DCCPO_CONFIRM_R; 414 struct dccp_feat_entry *entry, *next;
331 break; 415
332 case DCCPO_CHANGE_R: 416 list_for_each_entry_safe(entry, next, fn_list, node)
333 opt->dccpop_type = DCCPO_CONFIRM_L; 417 dccp_feat_entry_destructor(entry);
334 break; 418 INIT_LIST_HEAD(fn_list);
335 default: 419}
336 DCCP_WARN("invalid type %d\n", type); 420EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
337 kfree(opt); 421
338 return; 422/* generate @to as full clone of @from - @to must not contain any nodes */
423int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
424{
425 struct dccp_feat_entry *entry, *new;
426
427 INIT_LIST_HEAD(to);
428 list_for_each_entry(entry, from, node) {
429 new = dccp_feat_clone_entry(entry);
430 if (new == NULL)
431 goto cloning_failed;
432 list_add_tail(&new->node, to);
339 } 433 }
340 opt->dccpop_feat = feature; 434 return 0;
341 opt->dccpop_val = NULL; 435
342 opt->dccpop_len = 0; 436cloning_failed:
437 dccp_feat_list_purge(to);
438 return -ENOMEM;
439}
343 440
344 /* change feature */ 441/**
345 dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature); 442 * dccp_feat_valid_nn_length - Enforce length constraints on NN options
443 * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
444 * incoming options are accepted as long as their values are valid.
445 */
446static u8 dccp_feat_valid_nn_length(u8 feat_num)
447{
448 if (feat_num == DCCPF_ACK_RATIO) /* RFC 4340, 11.3 and 6.6.8 */
449 return 2;
450 if (feat_num == DCCPF_SEQUENCE_WINDOW) /* RFC 4340, 7.5.2 and 6.5 */
451 return 6;
452 return 0;
453}
346 454
347 list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf); 455static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
456{
457 switch (feat_num) {
458 case DCCPF_ACK_RATIO:
459 return val <= DCCPF_ACK_RATIO_MAX;
460 case DCCPF_SEQUENCE_WINDOW:
461 return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
462 }
463 return 0; /* feature unknown - so we can't tell */
348} 464}
349 465
350static void dccp_feat_flush_confirm(struct sock *sk) 466/* check that SP values are within the ranges defined in RFC 4340 */
467static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
351{ 468{
352 struct dccp_minisock *dmsk = dccp_msk(sk); 469 switch (feat_num) {
353 /* Check if there is anything to confirm in the first place */ 470 case DCCPF_CCID:
354 int yes = !list_empty(&dmsk->dccpms_conf); 471 return val == DCCPC_CCID2 || val == DCCPC_CCID3;
472 /* Type-check Boolean feature values: */
473 case DCCPF_SHORT_SEQNOS:
474 case DCCPF_ECN_INCAPABLE:
475 case DCCPF_SEND_ACK_VECTOR:
476 case DCCPF_SEND_NDP_COUNT:
477 case DCCPF_DATA_CHECKSUM:
478 case DCCPF_SEND_LEV_RATE:
479 return val < 2;
480 case DCCPF_MIN_CSUM_COVER:
481 return val < 16;
482 }
483 return 0; /* feature unknown */
484}
355 485
356 if (!yes) { 486static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
357 struct dccp_opt_pend *opt; 487{
488 if (sp_list == NULL || sp_len < 1)
489 return 0;
490 while (sp_len--)
491 if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
492 return 0;
493 return 1;
494}
358 495
359 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { 496/**
360 if (opt->dccpop_conf) { 497 * dccp_feat_insert_opts - Generate FN options from current list state
361 yes = 1; 498 * @skb: next sk_buff to be sent to the peer
362 break; 499 * @dp: for client during handshake and general negotiation
500 * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
501 */
502int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
503 struct sk_buff *skb)
504{
505 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
506 struct dccp_feat_entry *pos, *next;
507 u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
508 bool rpt;
509
510 /* put entries into @skb in the order they appear in the list */
511 list_for_each_entry_safe_reverse(pos, next, fn, node) {
512 opt = dccp_feat_genopt(pos);
513 type = dccp_feat_type(pos->feat_num);
514 rpt = false;
515
516 if (pos->empty_confirm) {
517 len = 0;
518 ptr = NULL;
519 } else {
520 if (type == FEAT_SP) {
521 len = pos->val.sp.len;
522 ptr = pos->val.sp.vec;
523 rpt = pos->needs_confirm;
524 } else if (type == FEAT_NN) {
525 len = dccp_feat_valid_nn_length(pos->feat_num);
526 ptr = nn_in_nbo;
527 dccp_encode_value_var(pos->val.nn, ptr, len);
528 } else {
529 DCCP_BUG("unknown feature %u", pos->feat_num);
530 return -1;
363 } 531 }
364 } 532 }
533
534 if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
535 return -1;
536 if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
537 return -1;
538 /*
539 * Enter CHANGING after transmitting the Change option (6.6.2).
540 */
541 if (pos->state == FEAT_INITIALISING)
542 pos->state = FEAT_CHANGING;
365 } 543 }
544 return 0;
545}
366 546
367 if (!yes) 547/**
368 return; 548 * __feat_register_nn - Register new NN value on socket
549 * @fn: feature-negotiation list to register with
550 * @feat: an NN feature from %dccp_feature_numbers
551 * @mandatory: use Mandatory option if 1
552 * @nn_val: value to register (restricted to 4 bytes)
553 * Note that NN features are local by definition (RFC 4340, 6.3.2).
554 */
555static int __feat_register_nn(struct list_head *fn, u8 feat,
556 u8 mandatory, u64 nn_val)
557{
558 dccp_feat_val fval = { .nn = nn_val };
369 559
370 /* OK there is something to confirm... */ 560 if (dccp_feat_type(feat) != FEAT_NN ||
371 /* XXX check if packet is in flight? Send delayed ack?? */ 561 !dccp_feat_is_valid_nn_val(feat, nn_val))
372 if (sk->sk_state == DCCP_OPEN) 562 return -EINVAL;
373 dccp_send_ack(sk); 563
564 /* Don't bother with default values, they will be activated anyway. */
565 if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
566 return 0;
567
568 return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
374} 569}
375 570
376int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len) 571/**
572 * __feat_register_sp - Register new SP value/list on socket
573 * @fn: feature-negotiation list to register with
574 * @feat: an SP feature from %dccp_feature_numbers
575 * @is_local: whether the local (1) or the remote (0) @feat is meant
576 * @mandatory: use Mandatory option if 1
577 * @sp_val: SP value followed by optional preference list
578 * @sp_len: length of @sp_val in bytes
579 */
580static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
581 u8 mandatory, u8 const *sp_val, u8 sp_len)
377{ 582{
378 int rc; 583 dccp_feat_val fval;
379 584
380 dccp_feat_debug(type, feature, *val); 585 if (dccp_feat_type(feat) != FEAT_SP ||
586 !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
587 return -EINVAL;
381 588
382 /* figure out if it's SP or NN feature */ 589 /* Avoid negotiating alien CCIDs by only advertising supported ones */
383 switch (feature) { 590 if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
384 /* deal with SP features */ 591 return -EOPNOTSUPP;
385 case DCCPF_CCID:
386 rc = dccp_feat_sp(sk, type, feature, val, len);
387 break;
388 592
389 /* deal with NN features */ 593 if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
390 case DCCPF_ACK_RATIO: 594 return -ENOMEM;
391 rc = dccp_feat_nn(sk, type, feature, val, len);
392 break;
393 595
394 /* XXX implement other features */ 596 return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
395 default: 597}
396 dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
397 dccp_feat_typename(type), feature);
398 rc = -EFAULT;
399 break;
400 }
401 598
402 /* check if there were problems changing features */ 599/**
403 if (rc) { 600 * dccp_feat_register_sp - Register requests to change SP feature values
404 /* If we don't agree on SP, we sent a confirm for old value. 601 * @sk: client or listening socket
405 * However we propagate rc to caller in case option was 602 * @feat: one of %dccp_feature_numbers
406 * mandatory 603 * @is_local: whether the local (1) or remote (0) @feat is meant
604 * @list: array of preferred values, in descending order of preference
605 * @len: length of @list in bytes
606 */
607int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
608 u8 const *list, u8 len)
609{ /* any changes must be registered before establishing the connection */
610 if (sk->sk_state != DCCP_CLOSED)
611 return -EISCONN;
612 if (dccp_feat_type(feat) != FEAT_SP)
613 return -EINVAL;
614 return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
615 0, list, len);
616}
617
618/* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
619int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
620{
621 /* any changes must be registered before establishing the connection */
622 if (sk->sk_state != DCCP_CLOSED)
623 return -EISCONN;
624 if (dccp_feat_type(feat) != FEAT_NN)
625 return -EINVAL;
626 return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
627}
628
629/*
630 * Tracking features whose value depend on the choice of CCID
631 *
632 * This is designed with an extension in mind so that a list walk could be done
633 * before activating any features. However, the existing framework was found to
634 * work satisfactorily up until now, the automatic verification is left open.
635 * When adding new CCIDs, add a corresponding dependency table here.
636 */
637static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
638{
639 static const struct ccid_dependency ccid2_dependencies[2][2] = {
640 /*
641 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
642 * feature and Send Ack Vector is an RX feature, `is_local'
643 * needs to be reversed.
407 */ 644 */
408 if (rc != DCCP_FEAT_SP_NOAGREE) 645 { /* Dependencies of the receiver-side (remote) CCID2 */
409 dccp_feat_empty_confirm(dccp_msk(sk), type, feature); 646 {
647 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
648 .is_local = true,
649 .is_mandatory = true,
650 .val = 1
651 },
652 { 0, 0, 0, 0 }
653 },
654 { /* Dependencies of the sender-side (local) CCID2 */
655 {
656 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
657 .is_local = false,
658 .is_mandatory = true,
659 .val = 1
660 },
661 { 0, 0, 0, 0 }
662 }
663 };
664 static const struct ccid_dependency ccid3_dependencies[2][5] = {
665 { /*
666 * Dependencies of the receiver-side CCID3
667 */
668 { /* locally disable Ack Vectors */
669 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
670 .is_local = true,
671 .is_mandatory = false,
672 .val = 0
673 },
674 { /* see below why Send Loss Event Rate is on */
675 .dependent_feat = DCCPF_SEND_LEV_RATE,
676 .is_local = true,
677 .is_mandatory = true,
678 .val = 1
679 },
680 { /* NDP Count is needed as per RFC 4342, 6.1.1 */
681 .dependent_feat = DCCPF_SEND_NDP_COUNT,
682 .is_local = false,
683 .is_mandatory = true,
684 .val = 1
685 },
686 { 0, 0, 0, 0 },
687 },
688 { /*
689 * CCID3 at the TX side: we request that the HC-receiver
690 * will not send Ack Vectors (they will be ignored, so
691 * Mandatory is not set); we enable Send Loss Event Rate
692 * (Mandatory since the implementation does not support
693 * the Loss Intervals option of RFC 4342, 8.6).
694 * The last two options are for peer's information only.
695 */
696 {
697 .dependent_feat = DCCPF_SEND_ACK_VECTOR,
698 .is_local = false,
699 .is_mandatory = false,
700 .val = 0
701 },
702 {
703 .dependent_feat = DCCPF_SEND_LEV_RATE,
704 .is_local = false,
705 .is_mandatory = true,
706 .val = 1
707 },
708 { /* this CCID does not support Ack Ratio */
709 .dependent_feat = DCCPF_ACK_RATIO,
710 .is_local = true,
711 .is_mandatory = false,
712 .val = 0
713 },
714 { /* tell receiver we are sending NDP counts */
715 .dependent_feat = DCCPF_SEND_NDP_COUNT,
716 .is_local = true,
717 .is_mandatory = false,
718 .val = 1
719 },
720 { 0, 0, 0, 0 }
721 }
722 };
723 switch (ccid) {
724 case DCCPC_CCID2:
725 return ccid2_dependencies[is_local];
726 case DCCPC_CCID3:
727 return ccid3_dependencies[is_local];
728 default:
729 return NULL;
410 } 730 }
731}
411 732
412 /* generate the confirm [if required] */ 733/**
413 dccp_feat_flush_confirm(sk); 734 * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
414 735 * @fn: feature-negotiation list to update
736 * @id: CCID number to track
737 * @is_local: whether TX CCID (1) or RX CCID (0) is meant
738 * This function needs to be called after registering all other features.
739 */
740static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
741{
742 const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
743 int i, rc = (table == NULL);
744
745 for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
746 if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
747 rc = __feat_register_sp(fn, table[i].dependent_feat,
748 table[i].is_local,
749 table[i].is_mandatory,
750 &table[i].val, 1);
751 else
752 rc = __feat_register_nn(fn, table[i].dependent_feat,
753 table[i].is_mandatory,
754 table[i].val);
415 return rc; 755 return rc;
416} 756}
417 757
418EXPORT_SYMBOL_GPL(dccp_feat_change_recv); 758/**
759 * dccp_feat_finalise_settings - Finalise settings before starting negotiation
760 * @dp: client or listening socket (settings will be inherited)
761 * This is called after all registrations (socket initialisation, sysctls, and
762 * sockopt calls), and before sending the first packet containing Change options
763 * (ie. client-Request or server-Response), to ensure internal consistency.
764 */
765int dccp_feat_finalise_settings(struct dccp_sock *dp)
766{
767 struct list_head *fn = &dp->dccps_featneg;
768 struct dccp_feat_entry *entry;
769 int i = 2, ccids[2] = { -1, -1 };
419 770
420int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature, 771 /*
421 u8 *val, u8 len) 772 * Propagating CCIDs:
773 * 1) not useful to propagate CCID settings if this host advertises more
774 * than one CCID: the choice of CCID may still change - if this is
775 * the client, or if this is the server and the client sends
776 * singleton CCID values.
777 * 2) since is that propagate_ccid changes the list, we defer changing
778 * the sorted list until after the traversal.
779 */
780 list_for_each_entry(entry, fn, node)
781 if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
782 ccids[entry->is_local] = entry->val.sp.vec[0];
783 while (i--)
784 if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
785 return -1;
786 return 0;
787}
788
789/**
790 * dccp_feat_server_ccid_dependencies - Resolve CCID-dependent features
791 * It is the server which resolves the dependencies once the CCID has been
792 * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
793 */
794int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
422{ 795{
423 u8 t; 796 struct list_head *fn = &dreq->dreq_featneg;
424 struct dccp_opt_pend *opt; 797 struct dccp_feat_entry *entry;
425 struct dccp_minisock *dmsk = dccp_msk(sk); 798 u8 is_local, ccid;
426 int found = 0;
427 int all_confirmed = 1;
428 799
429 dccp_feat_debug(type, feature, *val); 800 for (is_local = 0; is_local <= 1; is_local++) {
801 entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
430 802
431 /* locate our change request */ 803 if (entry != NULL && !entry->empty_confirm)
432 switch (type) { 804 ccid = entry->val.sp.vec[0];
433 case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break; 805 else
434 case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break; 806 ccid = dccp_feat_default_value(DCCPF_CCID);
435 default: DCCP_WARN("invalid type %d\n", type);
436 return 1;
437 807
808 if (dccp_feat_propagate_ccid(fn, ccid, is_local))
809 return -1;
438 } 810 }
439 /* XXX sanity check feature value */ 811 return 0;
812}
440 813
441 list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) { 814/* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
442 if (!opt->dccpop_conf && opt->dccpop_type == t && 815static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
443 opt->dccpop_feat == feature) { 816{
444 found = 1; 817 u8 c, s;
445 dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
446 818
447 /* XXX do sanity check */ 819 for (s = 0; s < slen; s++)
820 for (c = 0; c < clen; c++)
821 if (servlist[s] == clilist[c])
822 return servlist[s];
823 return -1;
824}
448 825
449 opt->dccpop_conf = 1; 826/**
827 * dccp_feat_prefer - Move preferred entry to the start of array
828 * Reorder the @array_len elements in @array so that @preferred_value comes
829 * first. Returns >0 to indicate that @preferred_value does occur in @array.
830 */
831static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
832{
833 u8 i, does_occur = 0;
450 834
451 /* We got a confirmation---change the option */ 835 if (array != NULL) {
452 dccp_feat_update(sk, opt->dccpop_type, 836 for (i = 0; i < array_len; i++)
453 opt->dccpop_feat, *val); 837 if (array[i] == preferred_value) {
838 array[i] = array[0];
839 does_occur++;
840 }
841 if (does_occur)
842 array[0] = preferred_value;
843 }
844 return does_occur;
845}
454 846
455 /* XXX check the return value of dccp_feat_update */ 847/**
456 break; 848 * dccp_feat_reconcile - Reconcile SP preference lists
457 } 849 * @fval: SP list to reconcile into
850 * @arr: received SP preference list
851 * @len: length of @arr in bytes
852 * @is_server: whether this side is the server (and @fv is the server's list)
853 * @reorder: whether to reorder the list in @fv after reconciling with @arr
854 * When successful, > 0 is returned and the reconciled list is in @fval.
855 * A value of 0 means that negotiation failed (no shared entry).
856 */
857static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
858 bool is_server, bool reorder)
859{
860 int rc;
458 861
459 if (!opt->dccpop_conf) 862 if (!fv->sp.vec || !arr) {
460 all_confirmed = 0; 863 DCCP_CRIT("NULL feature value or array");
864 return 0;
461 } 865 }
462 866
463 /* fix re-transmit timer */ 867 if (is_server)
464 /* XXX gotta make sure that no option negotiation occurs during 868 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 869 else
466 * on. if all options are confirmed it might kill timer which should 870 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 871
474 if (!found) 872 if (!reorder)
475 dccp_pr_debug("%s(%d, ...) never requested\n", 873 return rc;
476 dccp_feat_typename(type), feature); 874 if (rc < 0)
477 return 0; 875 return 0;
478}
479 876
480EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv); 877 /*
878 * Reorder list: used for activating features and in dccp_insert_fn_opt.
879 */
880 return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
881}
481 882
482void dccp_feat_clean(struct dccp_minisock *dmsk) 883/**
884 * dccp_feat_change_recv - Process incoming ChangeL/R options
885 * @fn: feature-negotiation list to update
886 * @is_mandatory: whether the Change was preceded by a Mandatory option
887 * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
888 * @feat: one of %dccp_feature_numbers
889 * @val: NN value or SP value/preference list
890 * @len: length of @val in bytes
891 * @server: whether this node is the server (1) or the client (0)
892 */
893static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
894 u8 feat, u8 *val, u8 len, const bool server)
483{ 895{
484 struct dccp_opt_pend *opt, *next; 896 u8 defval, type = dccp_feat_type(feat);
897 const bool local = (opt == DCCPO_CHANGE_R);
898 struct dccp_feat_entry *entry;
899 dccp_feat_val fval;
485 900
486 list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending, 901 if (len == 0 || type == FEAT_UNKNOWN) /* 6.1 and 6.6.8 */
487 dccpop_node) { 902 goto unknown_feature_or_value;
488 BUG_ON(opt->dccpop_val == NULL); 903
489 kfree(opt->dccpop_val); 904 /*
905 * Negotiation of NN features: Change R is invalid, so there is no
906 * simultaneous negotiation; hence we do not look up in the list.
907 */
908 if (type == FEAT_NN) {
909 if (local || len > sizeof(fval.nn))
910 goto unknown_feature_or_value;
911
912 /* 6.3.2: "The feature remote MUST accept any valid value..." */
913 fval.nn = dccp_decode_value_var(val, len);
914 if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
915 goto unknown_feature_or_value;
916
917 return dccp_feat_push_confirm(fn, feat, local, &fval);
918 }
919
920 /*
921 * Unidirectional/simultaneous negotiation of SP features (6.3.1)
922 */
923 entry = dccp_feat_list_lookup(fn, feat, local);
924 if (entry == NULL) {
925 /*
926 * No particular preferences have been registered. We deal with
927 * this situation by assuming that all valid values are equally
928 * acceptable, and apply the following checks:
929 * - if the peer's list is a singleton, we accept a valid value;
930 * - if we are the server, we first try to see if the peer (the
931 * client) advertises the default value. If yes, we use it,
932 * otherwise we accept the preferred value;
933 * - else if we are the client, we use the first list element.
934 */
935 if (dccp_feat_clone_sp_val(&fval, val, 1))
936 return DCCP_RESET_CODE_TOO_BUSY;
937
938 if (len > 1 && server) {
939 defval = dccp_feat_default_value(feat);
940 if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
941 fval.sp.vec[0] = defval;
942 } else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
943 kfree(fval.sp.vec);
944 goto unknown_feature_or_value;
945 }
490 946
491 if (opt->dccpop_sc != NULL) { 947 /* Treat unsupported CCIDs like invalid values */
492 BUG_ON(opt->dccpop_sc->dccpoc_val == NULL); 948 if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
493 kfree(opt->dccpop_sc->dccpoc_val); 949 kfree(fval.sp.vec);
494 kfree(opt->dccpop_sc); 950 goto not_valid_or_not_known;
495 } 951 }
496 952
497 kfree(opt); 953 return dccp_feat_push_confirm(fn, feat, local, &fval);
954
955 } else if (entry->state == FEAT_UNSTABLE) { /* 6.6.2 */
956 return 0;
498 } 957 }
499 INIT_LIST_HEAD(&dmsk->dccpms_pending);
500 958
501 list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) { 959 if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
502 BUG_ON(opt == NULL); 960 entry->empty_confirm = 0;
503 if (opt->dccpop_val != NULL) 961 } else if (is_mandatory) {
504 kfree(opt->dccpop_val); 962 return DCCP_RESET_CODE_MANDATORY_ERROR;
505 kfree(opt); 963 } else if (entry->state == FEAT_INITIALISING) {
964 /*
965 * Failed simultaneous negotiation (server only): try to `save'
966 * the connection by checking whether entry contains the default
967 * value for @feat. If yes, send an empty Confirm to signal that
968 * the received Change was not understood - which implies using
969 * the default value.
970 * If this also fails, we use Reset as the last resort.
971 */
972 WARN_ON(!server);
973 defval = dccp_feat_default_value(feat);
974 if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
975 return DCCP_RESET_CODE_OPTION_ERROR;
976 entry->empty_confirm = 1;
506 } 977 }
507 INIT_LIST_HEAD(&dmsk->dccpms_conf); 978 entry->needs_confirm = 1;
508} 979 entry->needs_mandatory = 0;
980 entry->state = FEAT_STABLE;
981 return 0;
509 982
510EXPORT_SYMBOL_GPL(dccp_feat_clean); 983unknown_feature_or_value:
984 if (!is_mandatory)
985 return dccp_push_empty_confirm(fn, feat, local);
511 986
512/* this is to be called only when a listening sock creates its child. It is 987not_valid_or_not_known:
513 * assumed by the function---the confirm is not duplicated, but rather it is 988 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
514 * "passed on". 989 : DCCP_RESET_CODE_OPTION_ERROR;
990}
991
992/**
993 * dccp_feat_confirm_recv - Process received Confirm options
994 * @fn: feature-negotiation list to update
995 * @is_mandatory: whether @opt was preceded by a Mandatory option
996 * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
997 * @feat: one of %dccp_feature_numbers
998 * @val: NN value or SP value/preference list
999 * @len: length of @val in bytes
1000 * @server: whether this node is server (1) or client (0)
515 */ 1001 */
516int dccp_feat_clone(struct sock *oldsk, struct sock *newsk) 1002static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1003 u8 feat, u8 *val, u8 len, const bool server)
517{ 1004{
518 struct dccp_minisock *olddmsk = dccp_msk(oldsk); 1005 u8 *plist, plen, type = dccp_feat_type(feat);
519 struct dccp_minisock *newdmsk = dccp_msk(newsk); 1006 const bool local = (opt == DCCPO_CONFIRM_R);
520 struct dccp_opt_pend *opt; 1007 struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
521 int rc = 0;
522 1008
523 INIT_LIST_HEAD(&newdmsk->dccpms_pending); 1009 if (entry == NULL) { /* nothing queued: ignore or handle error */
524 INIT_LIST_HEAD(&newdmsk->dccpms_conf); 1010 if (is_mandatory && type == FEAT_UNKNOWN)
1011 return DCCP_RESET_CODE_MANDATORY_ERROR;
1012
1013 if (!local && type == FEAT_NN) /* 6.3.2 */
1014 goto confirmation_failed;
1015 return 0;
1016 }
525 1017
526 list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) { 1018 if (entry->state != FEAT_CHANGING) /* 6.6.2 */
527 struct dccp_opt_pend *newopt; 1019 return 0;
528 /* copy the value of the option */
529 u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
530 1020
531 if (val == NULL) 1021 if (len == 0) {
532 goto out_clean; 1022 if (dccp_feat_must_be_understood(feat)) /* 6.6.7 */
1023 goto confirmation_failed;
1024 /*
1025 * Empty Confirm during connection setup: this means reverting
1026 * to the `old' value, which in this case is the default. Since
1027 * we handle default values automatically when no other values
1028 * have been set, we revert to the old value by removing this
1029 * entry from the list.
1030 */
1031 dccp_feat_list_pop(entry);
1032 return 0;
1033 }
533 1034
534 newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC); 1035 if (type == FEAT_NN) {
535 if (newopt == NULL) { 1036 if (len > sizeof(entry->val.nn))
536 kfree(val); 1037 goto confirmation_failed;
537 goto out_clean;
538 }
539 1038
540 /* insert the option */ 1039 if (entry->val.nn == dccp_decode_value_var(val, len))
541 newopt->dccpop_val = val; 1040 goto confirmation_succeeded;
542 list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
543 1041
544 /* XXX what happens with backlogs and multiple connections at 1042 DCCP_WARN("Bogus Confirm for non-existing value\n");
545 * once... 1043 goto confirmation_failed;
546 */ 1044 }
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 1045
550 /* reset state for a new socket */ 1046 /*
551 opt->dccpop_conf = 0; 1047 * Parsing SP Confirms: the first element of @val is the preferred
1048 * SP value which the peer confirms, the remainder depends on @len.
1049 * Note that only the confirmed value need to be a valid SP value.
1050 */
1051 if (!dccp_feat_is_valid_sp_val(feat, *val))
1052 goto confirmation_failed;
1053
1054 if (len == 1) { /* peer didn't supply a preference list */
1055 plist = val;
1056 plen = len;
1057 } else { /* preferred value + preference list */
1058 plist = val + 1;
1059 plen = len - 1;
552 } 1060 }
553 1061
554 /* XXX not doing anything about the conf queue */ 1062 /* Check whether the peer got the reconciliation right (6.6.8) */
1063 if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1064 DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1065 return DCCP_RESET_CODE_OPTION_ERROR;
1066 }
1067 entry->val.sp.vec[0] = *val;
555 1068
556out: 1069confirmation_succeeded:
557 return rc; 1070 entry->state = FEAT_STABLE;
1071 return 0;
558 1072
559out_clean: 1073confirmation_failed:
560 dccp_feat_clean(newdmsk); 1074 DCCP_WARN("Confirmation failed\n");
561 rc = -ENOMEM; 1075 return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
562 goto out; 1076 : DCCP_RESET_CODE_OPTION_ERROR;
563} 1077}
564 1078
565EXPORT_SYMBOL_GPL(dccp_feat_clone); 1079/**
566 1080 * dccp_feat_parse_options - Process Feature-Negotiation Options
567static int __dccp_feat_init(struct dccp_minisock *dmsk, u8 type, u8 feat, 1081 * @sk: for general use and used by the client during connection setup
568 u8 *val, u8 len) 1082 * @dreq: used by the server during connection setup
1083 * @mandatory: whether @opt was preceded by a Mandatory option
1084 * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1085 * @feat: one of %dccp_feature_numbers
1086 * @val: value contents of @opt
1087 * @len: length of @val in bytes
1088 * Returns 0 on success, a Reset code for ending the connection otherwise.
1089 */
1090int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1091 u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
569{ 1092{
570 int rc = -ENOMEM; 1093 struct dccp_sock *dp = dccp_sk(sk);
571 u8 *copy = kmemdup(val, len, GFP_KERNEL); 1094 struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1095 bool server = false;
572 1096
573 if (copy != NULL) { 1097 switch (sk->sk_state) {
574 rc = dccp_feat_change(dmsk, type, feat, copy, len, GFP_KERNEL); 1098 /*
575 if (rc) 1099 * Negotiation during connection setup
576 kfree(copy); 1100 */
1101 case DCCP_LISTEN:
1102 server = true; /* fall through */
1103 case DCCP_REQUESTING:
1104 switch (opt) {
1105 case DCCPO_CHANGE_L:
1106 case DCCPO_CHANGE_R:
1107 return dccp_feat_change_recv(fn, mandatory, opt, feat,
1108 val, len, server);
1109 case DCCPO_CONFIRM_R:
1110 case DCCPO_CONFIRM_L:
1111 return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1112 val, len, server);
1113 }
577 } 1114 }
578 return rc; 1115 return 0; /* ignore FN options in all other states */
579} 1116}
580 1117
581int dccp_feat_init(struct dccp_minisock *dmsk) 1118int dccp_feat_init(struct sock *sk)
582{ 1119{
1120 struct dccp_sock *dp = dccp_sk(sk);
1121 struct dccp_minisock *dmsk = dccp_msk(sk);
583 int rc; 1122 int rc;
584 1123
585 INIT_LIST_HEAD(&dmsk->dccpms_pending); 1124 INIT_LIST_HEAD(&dmsk->dccpms_pending); /* XXX no longer used */
586 INIT_LIST_HEAD(&dmsk->dccpms_conf); 1125 INIT_LIST_HEAD(&dmsk->dccpms_conf); /* XXX no longer used */
587
588 /* CCID L */
589 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_CCID,
590 &dmsk->dccpms_tx_ccid, 1);
591 if (rc)
592 goto out;
593
594 /* CCID R */
595 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_R, DCCPF_CCID,
596 &dmsk->dccpms_rx_ccid, 1);
597 if (rc)
598 goto out;
599 1126
600 /* Ack ratio */ 1127 /* Ack ratio */
601 rc = __dccp_feat_init(dmsk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO, 1128 rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
602 &dmsk->dccpms_ack_ratio, 1); 1129 dp->dccps_l_ack_ratio);
603out:
604 return rc; 1130 return rc;
605} 1131}
606 1132
607EXPORT_SYMBOL_GPL(dccp_feat_init); 1133EXPORT_SYMBOL_GPL(dccp_feat_init);
608 1134
1135int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1136{
1137 struct dccp_sock *dp = dccp_sk(sk);
1138 struct dccp_feat_entry *cur, *next;
1139 int idx;
1140 dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1141 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1142 };
1143
1144 list_for_each_entry(cur, fn_list, node) {
1145 /*
1146 * An empty Confirm means that either an unknown feature type
1147 * or an invalid value was present. In the first case there is
1148 * nothing to activate, in the other the default value is used.
1149 */
1150 if (cur->empty_confirm)
1151 continue;
1152
1153 idx = dccp_feat_index(cur->feat_num);
1154 if (idx < 0) {
1155 DCCP_BUG("Unknown feature %u", cur->feat_num);
1156 goto activation_failed;
1157 }
1158 if (cur->state != FEAT_STABLE) {
1159 DCCP_CRIT("Negotiation of %s %u failed in state %u",
1160 cur->is_local ? "local" : "remote",
1161 cur->feat_num, cur->state);
1162 goto activation_failed;
1163 }
1164 fvals[idx][cur->is_local] = &cur->val;
1165 }
1166
1167 /*
1168 * Activate in decreasing order of index, so that the CCIDs are always
1169 * activated as the last feature. This avoids the case where a CCID
1170 * relies on the initialisation of one or more features that it depends
1171 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1172 */
1173 for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1174 if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1175 __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1176 DCCP_CRIT("Could not activate %d", idx);
1177 goto activation_failed;
1178 }
1179
1180 /* Clean up Change options which have been confirmed already */
1181 list_for_each_entry_safe(cur, next, fn_list, node)
1182 if (!cur->needs_confirm)
1183 dccp_feat_list_pop(cur);
1184
1185 dccp_pr_debug("Activation OK\n");
1186 return 0;
1187
1188activation_failed:
1189 /*
1190 * We clean up everything that may have been allocated, since
1191 * it is difficult to track at which stage negotiation failed.
1192 * This is ok, since all allocation functions below are robust
1193 * against NULL arguments.
1194 */
1195 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1196 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1197 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1198 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1199 dp->dccps_hc_rx_ackvec = NULL;
1200 return -1;
1201}
1202
609#ifdef CONFIG_IP_DCCP_DEBUG 1203#ifdef CONFIG_IP_DCCP_DEBUG
610const char *dccp_feat_typename(const u8 type) 1204const char *dccp_feat_typename(const u8 type)
611{ 1205{
@@ -639,6 +1233,8 @@ const char *dccp_feat_name(const u8 feat)
639 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC) 1233 if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
640 return feature_names[DCCPF_RESERVED]; 1234 return feature_names[DCCPF_RESERVED];
641 1235
1236 if (feat == DCCPF_SEND_LEV_RATE)
1237 return "Send Loss Event Rate";
642 if (feat >= DCCPF_MIN_CCID_SPECIFIC) 1238 if (feat >= DCCPF_MIN_CCID_SPECIFIC)
643 return "CCID-specific"; 1239 return "CCID-specific";
644 1240