xref: /openbmc/linux/net/dccp/feat.c (revision 9eca0a47dee201a73967026985b5f0a79a46bd36)
1afe00251SAndrea Bittau /*
2afe00251SAndrea Bittau  *  net/dccp/feat.c
3afe00251SAndrea Bittau  *
4afe00251SAndrea Bittau  *  An implementation of the DCCP protocol
5afe00251SAndrea Bittau  *  Andrea Bittau <a.bittau@cs.ucl.ac.uk>
6afe00251SAndrea Bittau  *
75cdae198SGerrit Renker  *  ASSUMPTIONS
85cdae198SGerrit Renker  *  -----------
9f74e91b6SGerrit Renker  *  o Feature negotiation is coordinated with connection setup (as in TCP), wild
10f74e91b6SGerrit Renker  *    changes of parameters of an established connection are not supported.
115cdae198SGerrit Renker  *  o All currently known SP features have 1-byte quantities. If in the future
125cdae198SGerrit Renker  *    extensions of RFCs 4340..42 define features with item lengths larger than
135cdae198SGerrit Renker  *    one byte, a feature-specific extension of the code will be required.
145cdae198SGerrit Renker  *
15afe00251SAndrea Bittau  *  This program is free software; you can redistribute it and/or
16afe00251SAndrea Bittau  *  modify it under the terms of the GNU General Public License
17afe00251SAndrea Bittau  *  as published by the Free Software Foundation; either version
18afe00251SAndrea Bittau  *  2 of the License, or (at your option) any later version.
19afe00251SAndrea Bittau  */
20afe00251SAndrea Bittau 
21afe00251SAndrea Bittau #include <linux/module.h>
22afe00251SAndrea Bittau 
236ffd30fbSAndrea Bittau #include "ccid.h"
24afe00251SAndrea Bittau #include "feat.h"
25afe00251SAndrea Bittau 
26afe00251SAndrea Bittau #define DCCP_FEAT_SP_NOAGREE (-123)
27afe00251SAndrea Bittau 
287d43d1a0SGerrit Renker static const struct {
297d43d1a0SGerrit Renker 	u8			feat_num;		/* DCCPF_xxx */
307d43d1a0SGerrit Renker 	enum dccp_feat_type	rxtx;			/* RX or TX  */
317d43d1a0SGerrit Renker 	enum dccp_feat_type	reconciliation;		/* SP or NN  */
327d43d1a0SGerrit Renker 	u8			default_value;		/* as in 6.4 */
337d43d1a0SGerrit Renker /*
347d43d1a0SGerrit Renker  *    Lookup table for location and type of features (from RFC 4340/4342)
357d43d1a0SGerrit Renker  *  +--------------------------+----+-----+----+----+---------+-----------+
367d43d1a0SGerrit Renker  *  | Feature                  | Location | Reconc. | Initial |  Section  |
377d43d1a0SGerrit Renker  *  |                          | RX | TX  | SP | NN |  Value  | Reference |
387d43d1a0SGerrit Renker  *  +--------------------------+----+-----+----+----+---------+-----------+
397d43d1a0SGerrit Renker  *  | DCCPF_CCID               |    |  X  | X  |    |   2     | 10        |
407d43d1a0SGerrit Renker  *  | DCCPF_SHORT_SEQNOS       |    |  X  | X  |    |   0     |  7.6.1    |
417d43d1a0SGerrit Renker  *  | DCCPF_SEQUENCE_WINDOW    |    |  X  |    | X  | 100     |  7.5.2    |
427d43d1a0SGerrit Renker  *  | DCCPF_ECN_INCAPABLE      | X  |     | X  |    |   0     | 12.1      |
437d43d1a0SGerrit Renker  *  | DCCPF_ACK_RATIO          |    |  X  |    | X  |   2     | 11.3      |
447d43d1a0SGerrit Renker  *  | DCCPF_SEND_ACK_VECTOR    | X  |     | X  |    |   0     | 11.5      |
457d43d1a0SGerrit Renker  *  | DCCPF_SEND_NDP_COUNT     |    |  X  | X  |    |   0     |  7.7.2    |
467d43d1a0SGerrit Renker  *  | DCCPF_MIN_CSUM_COVER     | X  |     | X  |    |   0     |  9.2.1    |
477d43d1a0SGerrit Renker  *  | DCCPF_DATA_CHECKSUM      | X  |     | X  |    |   0     |  9.3.1    |
487d43d1a0SGerrit Renker  *  | DCCPF_SEND_LEV_RATE      | X  |     | X  |    |   0     | 4342/8.4  |
497d43d1a0SGerrit Renker  *  +--------------------------+----+-----+----+----+---------+-----------+
507d43d1a0SGerrit Renker  */
517d43d1a0SGerrit Renker } dccp_feat_table[] = {
527d43d1a0SGerrit Renker 	{ DCCPF_CCID,		 FEAT_AT_TX, FEAT_SP, 2 },
537d43d1a0SGerrit Renker 	{ DCCPF_SHORT_SEQNOS,	 FEAT_AT_TX, FEAT_SP, 0 },
547d43d1a0SGerrit Renker 	{ DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100 },
557d43d1a0SGerrit Renker 	{ DCCPF_ECN_INCAPABLE,	 FEAT_AT_RX, FEAT_SP, 0 },
567d43d1a0SGerrit Renker 	{ DCCPF_ACK_RATIO,	 FEAT_AT_TX, FEAT_NN, 2 },
577d43d1a0SGerrit Renker 	{ DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0 },
587d43d1a0SGerrit Renker 	{ DCCPF_SEND_NDP_COUNT,  FEAT_AT_TX, FEAT_SP, 0 },
597d43d1a0SGerrit Renker 	{ DCCPF_MIN_CSUM_COVER,  FEAT_AT_RX, FEAT_SP, 0 },
607d43d1a0SGerrit Renker 	{ DCCPF_DATA_CHECKSUM,	 FEAT_AT_RX, FEAT_SP, 0 },
617d43d1a0SGerrit Renker 	{ DCCPF_SEND_LEV_RATE,	 FEAT_AT_RX, FEAT_SP, 0 },
627d43d1a0SGerrit Renker };
637d43d1a0SGerrit Renker #define DCCP_FEAT_SUPPORTED_MAX		ARRAY_SIZE(dccp_feat_table)
647d43d1a0SGerrit Renker 
6561e6473eSGerrit Renker /**
6661e6473eSGerrit Renker  * dccp_feat_index  -  Hash function to map feature number into array position
6761e6473eSGerrit Renker  * Returns consecutive array index or -1 if the feature is not understood.
6861e6473eSGerrit Renker  */
6961e6473eSGerrit Renker static int dccp_feat_index(u8 feat_num)
7061e6473eSGerrit Renker {
7161e6473eSGerrit Renker 	/* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
7261e6473eSGerrit Renker 	if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
7361e6473eSGerrit Renker 		return feat_num - 1;
7461e6473eSGerrit Renker 
7561e6473eSGerrit Renker 	/*
7661e6473eSGerrit Renker 	 * Other features: add cases for new feature types here after adding
7761e6473eSGerrit Renker 	 * them to the above table.
7861e6473eSGerrit Renker 	 */
7961e6473eSGerrit Renker 	switch (feat_num) {
8061e6473eSGerrit Renker 	case DCCPF_SEND_LEV_RATE:
8161e6473eSGerrit Renker 			return DCCP_FEAT_SUPPORTED_MAX - 1;
8261e6473eSGerrit Renker 	}
8361e6473eSGerrit Renker 	return -1;
8461e6473eSGerrit Renker }
8561e6473eSGerrit Renker 
8661e6473eSGerrit Renker static u8 dccp_feat_type(u8 feat_num)
8761e6473eSGerrit Renker {
8861e6473eSGerrit Renker 	int idx = dccp_feat_index(feat_num);
8961e6473eSGerrit Renker 
9061e6473eSGerrit Renker 	if (idx < 0)
9161e6473eSGerrit Renker 		return FEAT_UNKNOWN;
9261e6473eSGerrit Renker 	return dccp_feat_table[idx].reconciliation;
9361e6473eSGerrit Renker }
9461e6473eSGerrit Renker 
95e8ef967aSGerrit Renker static int dccp_feat_default_value(u8 feat_num)
96e8ef967aSGerrit Renker {
97e8ef967aSGerrit Renker 	int idx = dccp_feat_index(feat_num);
98e8ef967aSGerrit Renker 	/*
99e8ef967aSGerrit Renker 	 * There are no default values for unknown features, so encountering a
100e8ef967aSGerrit Renker 	 * negative index here indicates a serious problem somewhere else.
101e8ef967aSGerrit Renker 	 */
102e8ef967aSGerrit Renker 	DCCP_BUG_ON(idx < 0);
103e8ef967aSGerrit Renker 
104e8ef967aSGerrit Renker 	return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
105e8ef967aSGerrit Renker }
106e8ef967aSGerrit Renker 
107ac75773cSGerrit Renker /* copy constructor, fval must not already contain allocated memory */
108ac75773cSGerrit Renker static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
109ac75773cSGerrit Renker {
110ac75773cSGerrit Renker 	fval->sp.len = len;
111ac75773cSGerrit Renker 	if (fval->sp.len > 0) {
112ac75773cSGerrit Renker 		fval->sp.vec = kmemdup(val, len, gfp_any());
113ac75773cSGerrit Renker 		if (fval->sp.vec == NULL) {
114ac75773cSGerrit Renker 			fval->sp.len = 0;
115ac75773cSGerrit Renker 			return -ENOBUFS;
116ac75773cSGerrit Renker 		}
117ac75773cSGerrit Renker 	}
118ac75773cSGerrit Renker 	return 0;
119ac75773cSGerrit Renker }
120ac75773cSGerrit Renker 
12161e6473eSGerrit Renker static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
12261e6473eSGerrit Renker {
12361e6473eSGerrit Renker 	if (unlikely(val == NULL))
12461e6473eSGerrit Renker 		return;
12561e6473eSGerrit Renker 	if (dccp_feat_type(feat_num) == FEAT_SP)
12661e6473eSGerrit Renker 		kfree(val->sp.vec);
12761e6473eSGerrit Renker 	memset(val, 0, sizeof(*val));
12861e6473eSGerrit Renker }
12961e6473eSGerrit Renker 
130ac75773cSGerrit Renker static struct dccp_feat_entry *
131ac75773cSGerrit Renker 	      dccp_feat_clone_entry(struct dccp_feat_entry const *original)
132ac75773cSGerrit Renker {
133ac75773cSGerrit Renker 	struct dccp_feat_entry *new;
134ac75773cSGerrit Renker 	u8 type = dccp_feat_type(original->feat_num);
135ac75773cSGerrit Renker 
136ac75773cSGerrit Renker 	if (type == FEAT_UNKNOWN)
137ac75773cSGerrit Renker 		return NULL;
138ac75773cSGerrit Renker 
139ac75773cSGerrit Renker 	new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
140ac75773cSGerrit Renker 	if (new == NULL)
141ac75773cSGerrit Renker 		return NULL;
142ac75773cSGerrit Renker 
143ac75773cSGerrit Renker 	if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
144ac75773cSGerrit Renker 						      original->val.sp.vec,
145ac75773cSGerrit Renker 						      original->val.sp.len)) {
146ac75773cSGerrit Renker 		kfree(new);
147ac75773cSGerrit Renker 		return NULL;
148ac75773cSGerrit Renker 	}
149ac75773cSGerrit Renker 	return new;
150ac75773cSGerrit Renker }
151ac75773cSGerrit Renker 
15261e6473eSGerrit Renker static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
15361e6473eSGerrit Renker {
15461e6473eSGerrit Renker 	if (entry != NULL) {
15561e6473eSGerrit Renker 		dccp_feat_val_destructor(entry->feat_num, &entry->val);
15661e6473eSGerrit Renker 		kfree(entry);
15761e6473eSGerrit Renker 	}
15861e6473eSGerrit Renker }
15961e6473eSGerrit Renker 
16061e6473eSGerrit Renker /*
16161e6473eSGerrit Renker  * List management functions
16261e6473eSGerrit Renker  *
16361e6473eSGerrit Renker  * Feature negotiation lists rely on and maintain the following invariants:
16461e6473eSGerrit Renker  * - each feat_num in the list is known, i.e. we know its type and default value
16561e6473eSGerrit Renker  * - each feat_num/is_local combination is unique (old entries are overwritten)
16661e6473eSGerrit Renker  * - SP values are always freshly allocated
16761e6473eSGerrit Renker  * - list is sorted in increasing order of feature number (faster lookup)
16861e6473eSGerrit Renker  */
16961e6473eSGerrit Renker 
170e8ef967aSGerrit Renker /**
171e8ef967aSGerrit Renker  * dccp_feat_entry_new  -  Central list update routine (called by all others)
172e8ef967aSGerrit Renker  * @head:  list to add to
173e8ef967aSGerrit Renker  * @feat:  feature number
174e8ef967aSGerrit Renker  * @local: whether the local (1) or remote feature with number @feat is meant
175e8ef967aSGerrit Renker  * This is the only constructor and serves to ensure the above invariants.
176e8ef967aSGerrit Renker  */
177e8ef967aSGerrit Renker static struct dccp_feat_entry *
178e8ef967aSGerrit Renker 	      dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
179e8ef967aSGerrit Renker {
180e8ef967aSGerrit Renker 	struct dccp_feat_entry *entry;
181e8ef967aSGerrit Renker 
182e8ef967aSGerrit Renker 	list_for_each_entry(entry, head, node)
183e8ef967aSGerrit Renker 		if (entry->feat_num == feat && entry->is_local == local) {
184e8ef967aSGerrit Renker 			dccp_feat_val_destructor(entry->feat_num, &entry->val);
185e8ef967aSGerrit Renker 			return entry;
186e8ef967aSGerrit Renker 		} else if (entry->feat_num > feat) {
187e8ef967aSGerrit Renker 			head = &entry->node;
188e8ef967aSGerrit Renker 			break;
189e8ef967aSGerrit Renker 		}
190e8ef967aSGerrit Renker 
191e8ef967aSGerrit Renker 	entry = kmalloc(sizeof(*entry), gfp_any());
192e8ef967aSGerrit Renker 	if (entry != NULL) {
193e8ef967aSGerrit Renker 		entry->feat_num = feat;
194e8ef967aSGerrit Renker 		entry->is_local = local;
195e8ef967aSGerrit Renker 		list_add_tail(&entry->node, head);
196e8ef967aSGerrit Renker 	}
197e8ef967aSGerrit Renker 	return entry;
198e8ef967aSGerrit Renker }
199e8ef967aSGerrit Renker 
200e8ef967aSGerrit Renker /**
201e8ef967aSGerrit Renker  * dccp_feat_push_change  -  Add/overwrite a Change option in the list
202e8ef967aSGerrit Renker  * @fn_list: feature-negotiation list to update
203e8ef967aSGerrit Renker  * @feat: one of %dccp_feature_numbers
204e8ef967aSGerrit Renker  * @local: whether local (1) or remote (0) @feat_num is meant
205e8ef967aSGerrit Renker  * @needs_mandatory: whether to use Mandatory feature negotiation options
206e8ef967aSGerrit Renker  * @fval: pointer to NN/SP value to be inserted (will be copied)
207e8ef967aSGerrit Renker  */
208e8ef967aSGerrit Renker static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
209e8ef967aSGerrit Renker 				 u8 mandatory, dccp_feat_val *fval)
210e8ef967aSGerrit Renker {
211e8ef967aSGerrit Renker 	struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
212e8ef967aSGerrit Renker 
213e8ef967aSGerrit Renker 	if (new == NULL)
214e8ef967aSGerrit Renker 		return -ENOMEM;
215e8ef967aSGerrit Renker 
216e8ef967aSGerrit Renker 	new->feat_num	     = feat;
217e8ef967aSGerrit Renker 	new->is_local	     = local;
218e8ef967aSGerrit Renker 	new->state	     = FEAT_INITIALISING;
219e8ef967aSGerrit Renker 	new->needs_confirm   = 0;
220e8ef967aSGerrit Renker 	new->empty_confirm   = 0;
221e8ef967aSGerrit Renker 	new->val	     = *fval;
222e8ef967aSGerrit Renker 	new->needs_mandatory = mandatory;
223e8ef967aSGerrit Renker 
224e8ef967aSGerrit Renker 	return 0;
225e8ef967aSGerrit Renker }
226e8ef967aSGerrit Renker 
22761e6473eSGerrit Renker static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
22861e6473eSGerrit Renker {
22961e6473eSGerrit Renker 	list_del(&entry->node);
23061e6473eSGerrit Renker 	dccp_feat_entry_destructor(entry);
23161e6473eSGerrit Renker }
23261e6473eSGerrit Renker 
23361e6473eSGerrit Renker void dccp_feat_list_purge(struct list_head *fn_list)
23461e6473eSGerrit Renker {
23561e6473eSGerrit Renker 	struct dccp_feat_entry *entry, *next;
23661e6473eSGerrit Renker 
23761e6473eSGerrit Renker 	list_for_each_entry_safe(entry, next, fn_list, node)
23861e6473eSGerrit Renker 		dccp_feat_entry_destructor(entry);
23961e6473eSGerrit Renker 	INIT_LIST_HEAD(fn_list);
24061e6473eSGerrit Renker }
24161e6473eSGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
24261e6473eSGerrit Renker 
243ac75773cSGerrit Renker /* generate @to as full clone of @from - @to must not contain any nodes */
244ac75773cSGerrit Renker int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
245ac75773cSGerrit Renker {
246ac75773cSGerrit Renker 	struct dccp_feat_entry *entry, *new;
247ac75773cSGerrit Renker 
248ac75773cSGerrit Renker 	INIT_LIST_HEAD(to);
249ac75773cSGerrit Renker 	list_for_each_entry(entry, from, node) {
250ac75773cSGerrit Renker 		new = dccp_feat_clone_entry(entry);
251ac75773cSGerrit Renker 		if (new == NULL)
252ac75773cSGerrit Renker 			goto cloning_failed;
253ac75773cSGerrit Renker 		list_add_tail(&new->node, to);
254ac75773cSGerrit Renker 	}
255ac75773cSGerrit Renker 	return 0;
256ac75773cSGerrit Renker 
257ac75773cSGerrit Renker cloning_failed:
258ac75773cSGerrit Renker 	dccp_feat_list_purge(to);
259ac75773cSGerrit Renker 	return -ENOMEM;
260ac75773cSGerrit Renker }
261ac75773cSGerrit Renker 
262e8ef967aSGerrit Renker static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
263e8ef967aSGerrit Renker {
264e8ef967aSGerrit Renker 	switch (feat_num) {
265e8ef967aSGerrit Renker 	case DCCPF_ACK_RATIO:
266e8ef967aSGerrit Renker 		return val <= DCCPF_ACK_RATIO_MAX;
267e8ef967aSGerrit Renker 	case DCCPF_SEQUENCE_WINDOW:
268e8ef967aSGerrit Renker 		return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
269e8ef967aSGerrit Renker 	}
270e8ef967aSGerrit Renker 	return 0;	/* feature unknown - so we can't tell */
271e8ef967aSGerrit Renker }
272e8ef967aSGerrit Renker 
273e8ef967aSGerrit Renker /* check that SP values are within the ranges defined in RFC 4340 */
274e8ef967aSGerrit Renker static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
275e8ef967aSGerrit Renker {
276e8ef967aSGerrit Renker 	switch (feat_num) {
277e8ef967aSGerrit Renker 	case DCCPF_CCID:
278e8ef967aSGerrit Renker 		return val == DCCPC_CCID2 || val == DCCPC_CCID3;
279e8ef967aSGerrit Renker 	/* Type-check Boolean feature values: */
280e8ef967aSGerrit Renker 	case DCCPF_SHORT_SEQNOS:
281e8ef967aSGerrit Renker 	case DCCPF_ECN_INCAPABLE:
282e8ef967aSGerrit Renker 	case DCCPF_SEND_ACK_VECTOR:
283e8ef967aSGerrit Renker 	case DCCPF_SEND_NDP_COUNT:
284e8ef967aSGerrit Renker 	case DCCPF_DATA_CHECKSUM:
285e8ef967aSGerrit Renker 	case DCCPF_SEND_LEV_RATE:
286e8ef967aSGerrit Renker 		return val < 2;
287e8ef967aSGerrit Renker 	case DCCPF_MIN_CSUM_COVER:
288e8ef967aSGerrit Renker 		return val < 16;
289e8ef967aSGerrit Renker 	}
290e8ef967aSGerrit Renker 	return 0;			/* feature unknown */
291e8ef967aSGerrit Renker }
292e8ef967aSGerrit Renker 
293e8ef967aSGerrit Renker static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
294e8ef967aSGerrit Renker {
295e8ef967aSGerrit Renker 	if (sp_list == NULL || sp_len < 1)
296e8ef967aSGerrit Renker 		return 0;
297e8ef967aSGerrit Renker 	while (sp_len--)
298e8ef967aSGerrit Renker 		if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
299e8ef967aSGerrit Renker 			return 0;
300e8ef967aSGerrit Renker 	return 1;
301e8ef967aSGerrit Renker }
302e8ef967aSGerrit Renker 
303e8ef967aSGerrit Renker /**
304e8ef967aSGerrit Renker  * __feat_register_nn  -  Register new NN value on socket
305e8ef967aSGerrit Renker  * @fn: feature-negotiation list to register with
306e8ef967aSGerrit Renker  * @feat: an NN feature from %dccp_feature_numbers
307e8ef967aSGerrit Renker  * @mandatory: use Mandatory option if 1
308e8ef967aSGerrit Renker  * @nn_val: value to register (restricted to 4 bytes)
309e8ef967aSGerrit Renker  * Note that NN features are local by definition (RFC 4340, 6.3.2).
310e8ef967aSGerrit Renker  */
311e8ef967aSGerrit Renker static int __feat_register_nn(struct list_head *fn, u8 feat,
312e8ef967aSGerrit Renker 			      u8 mandatory, u64 nn_val)
313e8ef967aSGerrit Renker {
314e8ef967aSGerrit Renker 	dccp_feat_val fval = { .nn = nn_val };
315e8ef967aSGerrit Renker 
316e8ef967aSGerrit Renker 	if (dccp_feat_type(feat) != FEAT_NN ||
317e8ef967aSGerrit Renker 	    !dccp_feat_is_valid_nn_val(feat, nn_val))
318e8ef967aSGerrit Renker 		return -EINVAL;
319e8ef967aSGerrit Renker 
320e8ef967aSGerrit Renker 	/* Don't bother with default values, they will be activated anyway. */
321e8ef967aSGerrit Renker 	if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
322e8ef967aSGerrit Renker 		return 0;
323e8ef967aSGerrit Renker 
324e8ef967aSGerrit Renker 	return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
325e8ef967aSGerrit Renker }
326e8ef967aSGerrit Renker 
327e8ef967aSGerrit Renker /**
328e8ef967aSGerrit Renker  * __feat_register_sp  -  Register new SP value/list on socket
329e8ef967aSGerrit Renker  * @fn: feature-negotiation list to register with
330e8ef967aSGerrit Renker  * @feat: an SP feature from %dccp_feature_numbers
331e8ef967aSGerrit Renker  * @is_local: whether the local (1) or the remote (0) @feat is meant
332e8ef967aSGerrit Renker  * @mandatory: use Mandatory option if 1
333e8ef967aSGerrit Renker  * @sp_val: SP value followed by optional preference list
334e8ef967aSGerrit Renker  * @sp_len: length of @sp_val in bytes
335e8ef967aSGerrit Renker  */
336e8ef967aSGerrit Renker static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
337e8ef967aSGerrit Renker 			      u8 mandatory, u8 const *sp_val, u8 sp_len)
338e8ef967aSGerrit Renker {
339e8ef967aSGerrit Renker 	dccp_feat_val fval;
340e8ef967aSGerrit Renker 
341e8ef967aSGerrit Renker 	if (dccp_feat_type(feat) != FEAT_SP ||
342e8ef967aSGerrit Renker 	    !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
343e8ef967aSGerrit Renker 		return -EINVAL;
344e8ef967aSGerrit Renker 
345d90ebcbfSGerrit Renker 	/* Avoid negotiating alien CCIDs by only advertising supported ones */
346d90ebcbfSGerrit Renker 	if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
347d90ebcbfSGerrit Renker 		return -EOPNOTSUPP;
348d90ebcbfSGerrit Renker 
349e8ef967aSGerrit Renker 	if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
350e8ef967aSGerrit Renker 		return -ENOMEM;
351e8ef967aSGerrit Renker 
352e8ef967aSGerrit Renker 	return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
353e8ef967aSGerrit Renker }
354e8ef967aSGerrit Renker 
3558ca0d17bSArnaldo Carvalho de Melo int dccp_feat_change(struct dccp_minisock *dmsk, u8 type, u8 feature,
3568ca0d17bSArnaldo Carvalho de Melo 		     u8 *val, u8 len, gfp_t gfp)
357afe00251SAndrea Bittau {
358afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
359afe00251SAndrea Bittau 
360c02fdc0eSGerrit Renker 	dccp_feat_debug(type, feature, *val);
361afe00251SAndrea Bittau 
362dd6303dfSGerrit Renker 	if (len > 3) {
36359348b19SGerrit Renker 		DCCP_WARN("invalid length %d\n", len);
36419443178SChris Wright 		return -EINVAL;
365c02fdc0eSGerrit Renker 	}
366c02fdc0eSGerrit Renker 	/* XXX add further sanity checks */
3676ffd30fbSAndrea Bittau 
368afe00251SAndrea Bittau 	/* check if that feature is already being negotiated */
369a4bf3902SArnaldo Carvalho de Melo 	list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
370afe00251SAndrea Bittau 		/* ok we found a negotiation for this option already */
371afe00251SAndrea Bittau 		if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
372afe00251SAndrea Bittau 			dccp_pr_debug("Replacing old\n");
373afe00251SAndrea Bittau 			/* replace */
374afe00251SAndrea Bittau 			BUG_ON(opt->dccpop_val == NULL);
375afe00251SAndrea Bittau 			kfree(opt->dccpop_val);
376afe00251SAndrea Bittau 			opt->dccpop_val	 = val;
377afe00251SAndrea Bittau 			opt->dccpop_len	 = len;
378afe00251SAndrea Bittau 			opt->dccpop_conf = 0;
379afe00251SAndrea Bittau 			return 0;
380afe00251SAndrea Bittau 		}
381afe00251SAndrea Bittau 	}
382afe00251SAndrea Bittau 
383afe00251SAndrea Bittau 	/* negotiation for a new feature */
384afe00251SAndrea Bittau 	opt = kmalloc(sizeof(*opt), gfp);
385afe00251SAndrea Bittau 	if (opt == NULL)
386afe00251SAndrea Bittau 		return -ENOMEM;
387afe00251SAndrea Bittau 
388afe00251SAndrea Bittau 	opt->dccpop_type = type;
389afe00251SAndrea Bittau 	opt->dccpop_feat = feature;
390afe00251SAndrea Bittau 	opt->dccpop_len	 = len;
391afe00251SAndrea Bittau 	opt->dccpop_val	 = val;
392afe00251SAndrea Bittau 	opt->dccpop_conf = 0;
393afe00251SAndrea Bittau 	opt->dccpop_sc	 = NULL;
394afe00251SAndrea Bittau 
395afe00251SAndrea Bittau 	BUG_ON(opt->dccpop_val == NULL);
396afe00251SAndrea Bittau 
397a4bf3902SArnaldo Carvalho de Melo 	list_add_tail(&opt->dccpop_node, &dmsk->dccpms_pending);
398afe00251SAndrea Bittau 	return 0;
399afe00251SAndrea Bittau }
400afe00251SAndrea Bittau 
401afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_change);
402afe00251SAndrea Bittau 
403*9eca0a47SGerrit Renker /*
404*9eca0a47SGerrit Renker  *	Tracking features whose value depend on the choice of CCID
405*9eca0a47SGerrit Renker  *
406*9eca0a47SGerrit Renker  * This is designed with an extension in mind so that a list walk could be done
407*9eca0a47SGerrit Renker  * before activating any features. However, the existing framework was found to
408*9eca0a47SGerrit Renker  * work satisfactorily up until now, the automatic verification is left open.
409*9eca0a47SGerrit Renker  * When adding new CCIDs, add a corresponding dependency table here.
410*9eca0a47SGerrit Renker  */
411*9eca0a47SGerrit Renker static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
412*9eca0a47SGerrit Renker {
413*9eca0a47SGerrit Renker 	static const struct ccid_dependency ccid2_dependencies[2][2] = {
414*9eca0a47SGerrit Renker 		/*
415*9eca0a47SGerrit Renker 		 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
416*9eca0a47SGerrit Renker 		 * feature and Send Ack Vector is an RX feature, `is_local'
417*9eca0a47SGerrit Renker 		 * needs to be reversed.
418*9eca0a47SGerrit Renker 		 */
419*9eca0a47SGerrit Renker 		{	/* Dependencies of the receiver-side (remote) CCID2 */
420*9eca0a47SGerrit Renker 			{
421*9eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
422*9eca0a47SGerrit Renker 				.is_local	= true,
423*9eca0a47SGerrit Renker 				.is_mandatory	= true,
424*9eca0a47SGerrit Renker 				.val		= 1
425*9eca0a47SGerrit Renker 			},
426*9eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
427*9eca0a47SGerrit Renker 		},
428*9eca0a47SGerrit Renker 		{	/* Dependencies of the sender-side (local) CCID2 */
429*9eca0a47SGerrit Renker 			{
430*9eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
431*9eca0a47SGerrit Renker 				.is_local	= false,
432*9eca0a47SGerrit Renker 				.is_mandatory	= true,
433*9eca0a47SGerrit Renker 				.val		= 1
434*9eca0a47SGerrit Renker 			},
435*9eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
436*9eca0a47SGerrit Renker 		}
437*9eca0a47SGerrit Renker 	};
438*9eca0a47SGerrit Renker 	static const struct ccid_dependency ccid3_dependencies[2][5] = {
439*9eca0a47SGerrit Renker 		{	/*
440*9eca0a47SGerrit Renker 			 * Dependencies of the receiver-side CCID3
441*9eca0a47SGerrit Renker 			 */
442*9eca0a47SGerrit Renker 			{	/* locally disable Ack Vectors */
443*9eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
444*9eca0a47SGerrit Renker 				.is_local	= true,
445*9eca0a47SGerrit Renker 				.is_mandatory	= false,
446*9eca0a47SGerrit Renker 				.val		= 0
447*9eca0a47SGerrit Renker 			},
448*9eca0a47SGerrit Renker 			{	/* see below why Send Loss Event Rate is on */
449*9eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_LEV_RATE,
450*9eca0a47SGerrit Renker 				.is_local	= true,
451*9eca0a47SGerrit Renker 				.is_mandatory	= true,
452*9eca0a47SGerrit Renker 				.val		= 1
453*9eca0a47SGerrit Renker 			},
454*9eca0a47SGerrit Renker 			{	/* NDP Count is needed as per RFC 4342, 6.1.1 */
455*9eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_NDP_COUNT,
456*9eca0a47SGerrit Renker 				.is_local	= false,
457*9eca0a47SGerrit Renker 				.is_mandatory	= true,
458*9eca0a47SGerrit Renker 				.val		= 1
459*9eca0a47SGerrit Renker 			},
460*9eca0a47SGerrit Renker 			{ 0, 0, 0, 0 },
461*9eca0a47SGerrit Renker 		},
462*9eca0a47SGerrit Renker 		{	/*
463*9eca0a47SGerrit Renker 			 * CCID3 at the TX side: we request that the HC-receiver
464*9eca0a47SGerrit Renker 			 * will not send Ack Vectors (they will be ignored, so
465*9eca0a47SGerrit Renker 			 * Mandatory is not set); we enable Send Loss Event Rate
466*9eca0a47SGerrit Renker 			 * (Mandatory since the implementation does not support
467*9eca0a47SGerrit Renker 			 * the Loss Intervals option of RFC 4342, 8.6).
468*9eca0a47SGerrit Renker 			 * The last two options are for peer's information only.
469*9eca0a47SGerrit Renker 			*/
470*9eca0a47SGerrit Renker 			{
471*9eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
472*9eca0a47SGerrit Renker 				.is_local	= false,
473*9eca0a47SGerrit Renker 				.is_mandatory	= false,
474*9eca0a47SGerrit Renker 				.val		= 0
475*9eca0a47SGerrit Renker 			},
476*9eca0a47SGerrit Renker 			{
477*9eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_LEV_RATE,
478*9eca0a47SGerrit Renker 				.is_local	= false,
479*9eca0a47SGerrit Renker 				.is_mandatory	= true,
480*9eca0a47SGerrit Renker 				.val		= 1
481*9eca0a47SGerrit Renker 			},
482*9eca0a47SGerrit Renker 			{	/* this CCID does not support Ack Ratio */
483*9eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_ACK_RATIO,
484*9eca0a47SGerrit Renker 				.is_local	= true,
485*9eca0a47SGerrit Renker 				.is_mandatory	= false,
486*9eca0a47SGerrit Renker 				.val		= 0
487*9eca0a47SGerrit Renker 			},
488*9eca0a47SGerrit Renker 			{	/* tell receiver we are sending NDP counts */
489*9eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_NDP_COUNT,
490*9eca0a47SGerrit Renker 				.is_local	= true,
491*9eca0a47SGerrit Renker 				.is_mandatory	= false,
492*9eca0a47SGerrit Renker 				.val		= 1
493*9eca0a47SGerrit Renker 			},
494*9eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
495*9eca0a47SGerrit Renker 		}
496*9eca0a47SGerrit Renker 	};
497*9eca0a47SGerrit Renker 	switch (ccid) {
498*9eca0a47SGerrit Renker 	case DCCPC_CCID2:
499*9eca0a47SGerrit Renker 		return ccid2_dependencies[is_local];
500*9eca0a47SGerrit Renker 	case DCCPC_CCID3:
501*9eca0a47SGerrit Renker 		return ccid3_dependencies[is_local];
502*9eca0a47SGerrit Renker 	default:
503*9eca0a47SGerrit Renker 		return NULL;
504*9eca0a47SGerrit Renker 	}
505*9eca0a47SGerrit Renker }
506*9eca0a47SGerrit Renker 
507*9eca0a47SGerrit Renker /**
508*9eca0a47SGerrit Renker  * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
509*9eca0a47SGerrit Renker  * @fn: feature-negotiation list to update
510*9eca0a47SGerrit Renker  * @id: CCID number to track
511*9eca0a47SGerrit Renker  * @is_local: whether TX CCID (1) or RX CCID (0) is meant
512*9eca0a47SGerrit Renker  * This function needs to be called after registering all other features.
513*9eca0a47SGerrit Renker  */
514*9eca0a47SGerrit Renker static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
515*9eca0a47SGerrit Renker {
516*9eca0a47SGerrit Renker 	const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
517*9eca0a47SGerrit Renker 	int i, rc = (table == NULL);
518*9eca0a47SGerrit Renker 
519*9eca0a47SGerrit Renker 	for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
520*9eca0a47SGerrit Renker 		if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
521*9eca0a47SGerrit Renker 			rc = __feat_register_sp(fn, table[i].dependent_feat,
522*9eca0a47SGerrit Renker 						    table[i].is_local,
523*9eca0a47SGerrit Renker 						    table[i].is_mandatory,
524*9eca0a47SGerrit Renker 						    &table[i].val, 1);
525*9eca0a47SGerrit Renker 		else
526*9eca0a47SGerrit Renker 			rc = __feat_register_nn(fn, table[i].dependent_feat,
527*9eca0a47SGerrit Renker 						    table[i].is_mandatory,
528*9eca0a47SGerrit Renker 						    table[i].val);
529*9eca0a47SGerrit Renker 	return rc;
530*9eca0a47SGerrit Renker }
531*9eca0a47SGerrit Renker 
532*9eca0a47SGerrit Renker /**
533*9eca0a47SGerrit Renker  * dccp_feat_finalise_settings  -  Finalise settings before starting negotiation
534*9eca0a47SGerrit Renker  * @dp: client or listening socket (settings will be inherited)
535*9eca0a47SGerrit Renker  * This is called after all registrations (socket initialisation, sysctls, and
536*9eca0a47SGerrit Renker  * sockopt calls), and before sending the first packet containing Change options
537*9eca0a47SGerrit Renker  * (ie. client-Request or server-Response), to ensure internal consistency.
538*9eca0a47SGerrit Renker  */
539*9eca0a47SGerrit Renker int dccp_feat_finalise_settings(struct dccp_sock *dp)
540*9eca0a47SGerrit Renker {
541*9eca0a47SGerrit Renker 	struct list_head *fn = &dp->dccps_featneg;
542*9eca0a47SGerrit Renker 	struct dccp_feat_entry *entry;
543*9eca0a47SGerrit Renker 	int i = 2, ccids[2] = { -1, -1 };
544*9eca0a47SGerrit Renker 
545*9eca0a47SGerrit Renker 	/*
546*9eca0a47SGerrit Renker 	 * Propagating CCIDs:
547*9eca0a47SGerrit Renker 	 * 1) not useful to propagate CCID settings if this host advertises more
548*9eca0a47SGerrit Renker 	 *    than one CCID: the choice of CCID  may still change - if this is
549*9eca0a47SGerrit Renker 	 *    the client, or if this is the server and the client sends
550*9eca0a47SGerrit Renker 	 *    singleton CCID values.
551*9eca0a47SGerrit Renker 	 * 2) since is that propagate_ccid changes the list, we defer changing
552*9eca0a47SGerrit Renker 	 *    the sorted list until after the traversal.
553*9eca0a47SGerrit Renker 	 */
554*9eca0a47SGerrit Renker 	list_for_each_entry(entry, fn, node)
555*9eca0a47SGerrit Renker 		if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
556*9eca0a47SGerrit Renker 			ccids[entry->is_local] = entry->val.sp.vec[0];
557*9eca0a47SGerrit Renker 	while (i--)
558*9eca0a47SGerrit Renker 		if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
559*9eca0a47SGerrit Renker 			return -1;
560*9eca0a47SGerrit Renker 	return 0;
561*9eca0a47SGerrit Renker }
562*9eca0a47SGerrit Renker 
5636ffd30fbSAndrea Bittau static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
5646ffd30fbSAndrea Bittau {
5656ffd30fbSAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
566a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *dmsk = dccp_msk(sk);
5676ffd30fbSAndrea Bittau 	/* figure out if we are changing our CCID or the peer's */
5686ffd30fbSAndrea Bittau 	const int rx = type == DCCPO_CHANGE_R;
569a4bf3902SArnaldo Carvalho de Melo 	const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
5706ffd30fbSAndrea Bittau 	struct ccid *new_ccid;
5716ffd30fbSAndrea Bittau 
5726ffd30fbSAndrea Bittau 	/* Check if nothing is being changed. */
5736ffd30fbSAndrea Bittau 	if (ccid_nr == new_ccid_nr)
5746ffd30fbSAndrea Bittau 		return 0;
5756ffd30fbSAndrea Bittau 
5766ffd30fbSAndrea Bittau 	new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
5776ffd30fbSAndrea Bittau 	if (new_ccid == NULL)
5786ffd30fbSAndrea Bittau 		return -ENOMEM;
5796ffd30fbSAndrea Bittau 
5806ffd30fbSAndrea Bittau 	if (rx) {
5816ffd30fbSAndrea Bittau 		ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
5826ffd30fbSAndrea Bittau 		dp->dccps_hc_rx_ccid = new_ccid;
583a4bf3902SArnaldo Carvalho de Melo 		dmsk->dccpms_rx_ccid = new_ccid_nr;
5846ffd30fbSAndrea Bittau 	} else {
5856ffd30fbSAndrea Bittau 		ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
5866ffd30fbSAndrea Bittau 		dp->dccps_hc_tx_ccid = new_ccid;
587a4bf3902SArnaldo Carvalho de Melo 		dmsk->dccpms_tx_ccid = new_ccid_nr;
5886ffd30fbSAndrea Bittau 	}
5896ffd30fbSAndrea Bittau 
5906ffd30fbSAndrea Bittau 	return 0;
5916ffd30fbSAndrea Bittau }
5926ffd30fbSAndrea Bittau 
593afe00251SAndrea Bittau static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
594afe00251SAndrea Bittau {
595c02fdc0eSGerrit Renker 	dccp_feat_debug(type, feat, val);
5966ffd30fbSAndrea Bittau 
5976ffd30fbSAndrea Bittau 	switch (feat) {
5986ffd30fbSAndrea Bittau 	case DCCPF_CCID:
5996ffd30fbSAndrea Bittau 		return dccp_feat_update_ccid(sk, type, val);
6006ffd30fbSAndrea Bittau 	default:
601c02fdc0eSGerrit Renker 		dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
602c02fdc0eSGerrit Renker 			      dccp_feat_typename(type), feat);
6036ffd30fbSAndrea Bittau 		break;
6046ffd30fbSAndrea Bittau 	}
605afe00251SAndrea Bittau 	return 0;
606afe00251SAndrea Bittau }
607afe00251SAndrea Bittau 
608afe00251SAndrea Bittau static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
609afe00251SAndrea Bittau 			       u8 *rpref, u8 rlen)
610afe00251SAndrea Bittau {
611afe00251SAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
612afe00251SAndrea Bittau 	u8 *spref, slen, *res = NULL;
613afe00251SAndrea Bittau 	int i, j, rc, agree = 1;
614afe00251SAndrea Bittau 
615afe00251SAndrea Bittau 	BUG_ON(rpref == NULL);
616afe00251SAndrea Bittau 
617afe00251SAndrea Bittau 	/* check if we are the black sheep */
618afe00251SAndrea Bittau 	if (dp->dccps_role == DCCP_ROLE_CLIENT) {
619afe00251SAndrea Bittau 		spref = rpref;
620afe00251SAndrea Bittau 		slen  = rlen;
621afe00251SAndrea Bittau 		rpref = opt->dccpop_val;
622afe00251SAndrea Bittau 		rlen  = opt->dccpop_len;
623afe00251SAndrea Bittau 	} else {
624afe00251SAndrea Bittau 		spref = opt->dccpop_val;
625afe00251SAndrea Bittau 		slen  = opt->dccpop_len;
626afe00251SAndrea Bittau 	}
627afe00251SAndrea Bittau 	/*
628afe00251SAndrea Bittau 	 * Now we have server preference list in spref and client preference in
629afe00251SAndrea Bittau 	 * rpref
630afe00251SAndrea Bittau 	 */
631afe00251SAndrea Bittau 	BUG_ON(spref == NULL);
632afe00251SAndrea Bittau 	BUG_ON(rpref == NULL);
633afe00251SAndrea Bittau 
634afe00251SAndrea Bittau 	/* FIXME sanity check vals */
635afe00251SAndrea Bittau 
636afe00251SAndrea Bittau 	/* Are values in any order?  XXX Lame "algorithm" here */
637afe00251SAndrea Bittau 	for (i = 0; i < slen; i++) {
638afe00251SAndrea Bittau 		for (j = 0; j < rlen; j++) {
639afe00251SAndrea Bittau 			if (spref[i] == rpref[j]) {
640afe00251SAndrea Bittau 				res = &spref[i];
641afe00251SAndrea Bittau 				break;
642afe00251SAndrea Bittau 			}
643afe00251SAndrea Bittau 		}
644afe00251SAndrea Bittau 		if (res)
645afe00251SAndrea Bittau 			break;
646afe00251SAndrea Bittau 	}
647afe00251SAndrea Bittau 
648afe00251SAndrea Bittau 	/* we didn't agree on anything */
649afe00251SAndrea Bittau 	if (res == NULL) {
650afe00251SAndrea Bittau 		/* confirm previous value */
651afe00251SAndrea Bittau 		switch (opt->dccpop_feat) {
652afe00251SAndrea Bittau 		case DCCPF_CCID:
653afe00251SAndrea Bittau 			/* XXX did i get this right? =P */
654afe00251SAndrea Bittau 			if (opt->dccpop_type == DCCPO_CHANGE_L)
655a4bf3902SArnaldo Carvalho de Melo 				res = &dccp_msk(sk)->dccpms_tx_ccid;
656afe00251SAndrea Bittau 			else
657a4bf3902SArnaldo Carvalho de Melo 				res = &dccp_msk(sk)->dccpms_rx_ccid;
658afe00251SAndrea Bittau 			break;
659afe00251SAndrea Bittau 
660afe00251SAndrea Bittau 		default:
66159348b19SGerrit Renker 			DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
66259348b19SGerrit Renker 			/* XXX implement res */
663afe00251SAndrea Bittau 			return -EFAULT;
664afe00251SAndrea Bittau 		}
665afe00251SAndrea Bittau 
666afe00251SAndrea Bittau 		dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
667afe00251SAndrea Bittau 		agree = 0; /* this is used for mandatory options... */
668afe00251SAndrea Bittau 	}
669afe00251SAndrea Bittau 
670afe00251SAndrea Bittau 	/* need to put result and our preference list */
671afe00251SAndrea Bittau 	rlen = 1 + opt->dccpop_len;
672afe00251SAndrea Bittau 	rpref = kmalloc(rlen, GFP_ATOMIC);
673afe00251SAndrea Bittau 	if (rpref == NULL)
674afe00251SAndrea Bittau 		return -ENOMEM;
675afe00251SAndrea Bittau 
676afe00251SAndrea Bittau 	*rpref = *res;
677afe00251SAndrea Bittau 	memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
678afe00251SAndrea Bittau 
679afe00251SAndrea Bittau 	/* put it in the "confirm queue" */
680afe00251SAndrea Bittau 	if (opt->dccpop_sc == NULL) {
681afe00251SAndrea Bittau 		opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
682afe00251SAndrea Bittau 		if (opt->dccpop_sc == NULL) {
683afe00251SAndrea Bittau 			kfree(rpref);
684afe00251SAndrea Bittau 			return -ENOMEM;
685afe00251SAndrea Bittau 		}
686afe00251SAndrea Bittau 	} else {
687afe00251SAndrea Bittau 		/* recycle the confirm slot */
688afe00251SAndrea Bittau 		BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
689afe00251SAndrea Bittau 		kfree(opt->dccpop_sc->dccpoc_val);
690afe00251SAndrea Bittau 		dccp_pr_debug("recycling confirm slot\n");
691afe00251SAndrea Bittau 	}
692afe00251SAndrea Bittau 	memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
693afe00251SAndrea Bittau 
694afe00251SAndrea Bittau 	opt->dccpop_sc->dccpoc_val = rpref;
695afe00251SAndrea Bittau 	opt->dccpop_sc->dccpoc_len = rlen;
696afe00251SAndrea Bittau 
697afe00251SAndrea Bittau 	/* update the option on our side [we are about to send the confirm] */
698afe00251SAndrea Bittau 	rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
699afe00251SAndrea Bittau 	if (rc) {
700afe00251SAndrea Bittau 		kfree(opt->dccpop_sc->dccpoc_val);
701afe00251SAndrea Bittau 		kfree(opt->dccpop_sc);
70268907dadSRandy Dunlap 		opt->dccpop_sc = NULL;
703afe00251SAndrea Bittau 		return rc;
704afe00251SAndrea Bittau 	}
705afe00251SAndrea Bittau 
706afe00251SAndrea Bittau 	dccp_pr_debug("Will confirm %d\n", *rpref);
707afe00251SAndrea Bittau 
708afe00251SAndrea Bittau 	/* say we want to change to X but we just got a confirm X, suppress our
709afe00251SAndrea Bittau 	 * change
710afe00251SAndrea Bittau 	 */
711afe00251SAndrea Bittau 	if (!opt->dccpop_conf) {
712afe00251SAndrea Bittau 		if (*opt->dccpop_val == *res)
713afe00251SAndrea Bittau 			opt->dccpop_conf = 1;
714afe00251SAndrea Bittau 		dccp_pr_debug("won't ask for change of same feature\n");
715afe00251SAndrea Bittau 	}
716afe00251SAndrea Bittau 
717afe00251SAndrea Bittau 	return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
718afe00251SAndrea Bittau }
719afe00251SAndrea Bittau 
720afe00251SAndrea Bittau static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
721afe00251SAndrea Bittau {
722a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *dmsk = dccp_msk(sk);
723afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
724afe00251SAndrea Bittau 	int rc = 1;
725afe00251SAndrea Bittau 	u8 t;
726afe00251SAndrea Bittau 
727afe00251SAndrea Bittau 	/*
728afe00251SAndrea Bittau 	 * We received a CHANGE.  We gotta match it against our own preference
729afe00251SAndrea Bittau 	 * list.  If we got a CHANGE_R it means it's a change for us, so we need
730afe00251SAndrea Bittau 	 * to compare our CHANGE_L list.
731afe00251SAndrea Bittau 	 */
732afe00251SAndrea Bittau 	if (type == DCCPO_CHANGE_L)
733afe00251SAndrea Bittau 		t = DCCPO_CHANGE_R;
734afe00251SAndrea Bittau 	else
735afe00251SAndrea Bittau 		t = DCCPO_CHANGE_L;
736afe00251SAndrea Bittau 
737afe00251SAndrea Bittau 	/* find our preference list for this feature */
738a4bf3902SArnaldo Carvalho de Melo 	list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
739afe00251SAndrea Bittau 		if (opt->dccpop_type != t || opt->dccpop_feat != feature)
740afe00251SAndrea Bittau 			continue;
741afe00251SAndrea Bittau 
742afe00251SAndrea Bittau 		/* find the winner from the two preference lists */
743afe00251SAndrea Bittau 		rc = dccp_feat_reconcile(sk, opt, val, len);
744afe00251SAndrea Bittau 		break;
745afe00251SAndrea Bittau 	}
746afe00251SAndrea Bittau 
747afe00251SAndrea Bittau 	/* We didn't deal with the change.  This can happen if we have no
748afe00251SAndrea Bittau 	 * preference list for the feature.  In fact, it just shouldn't
749afe00251SAndrea Bittau 	 * happen---if we understand a feature, we should have a preference list
750afe00251SAndrea Bittau 	 * with at least the default value.
751afe00251SAndrea Bittau 	 */
752afe00251SAndrea Bittau 	BUG_ON(rc == 1);
753afe00251SAndrea Bittau 
754afe00251SAndrea Bittau 	return rc;
755afe00251SAndrea Bittau }
756afe00251SAndrea Bittau 
757afe00251SAndrea Bittau static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
758afe00251SAndrea Bittau {
759afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
760a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *dmsk = dccp_msk(sk);
761afe00251SAndrea Bittau 	u8 *copy;
762afe00251SAndrea Bittau 	int rc;
763afe00251SAndrea Bittau 
764c02fdc0eSGerrit Renker 	/* NN features must be Change L (sec. 6.3.2) */
765c02fdc0eSGerrit Renker 	if (type != DCCPO_CHANGE_L) {
766c02fdc0eSGerrit Renker 		dccp_pr_debug("received %s for NN feature %d\n",
767c02fdc0eSGerrit Renker 				dccp_feat_typename(type), feature);
768afe00251SAndrea Bittau 		return -EFAULT;
769afe00251SAndrea Bittau 	}
770afe00251SAndrea Bittau 
771afe00251SAndrea Bittau 	/* XXX sanity check opt val */
772afe00251SAndrea Bittau 
773afe00251SAndrea Bittau 	/* copy option so we can confirm it */
774afe00251SAndrea Bittau 	opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
775afe00251SAndrea Bittau 	if (opt == NULL)
776afe00251SAndrea Bittau 		return -ENOMEM;
777afe00251SAndrea Bittau 
778eed73417SArnaldo Carvalho de Melo 	copy = kmemdup(val, len, GFP_ATOMIC);
779afe00251SAndrea Bittau 	if (copy == NULL) {
780afe00251SAndrea Bittau 		kfree(opt);
781afe00251SAndrea Bittau 		return -ENOMEM;
782afe00251SAndrea Bittau 	}
783afe00251SAndrea Bittau 
784afe00251SAndrea Bittau 	opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
785afe00251SAndrea Bittau 	opt->dccpop_feat = feature;
786afe00251SAndrea Bittau 	opt->dccpop_val	 = copy;
787afe00251SAndrea Bittau 	opt->dccpop_len	 = len;
788afe00251SAndrea Bittau 
789afe00251SAndrea Bittau 	/* change feature */
790afe00251SAndrea Bittau 	rc = dccp_feat_update(sk, type, feature, *val);
791afe00251SAndrea Bittau 	if (rc) {
792afe00251SAndrea Bittau 		kfree(opt->dccpop_val);
793afe00251SAndrea Bittau 		kfree(opt);
794afe00251SAndrea Bittau 		return rc;
795afe00251SAndrea Bittau 	}
796afe00251SAndrea Bittau 
797c02fdc0eSGerrit Renker 	dccp_feat_debug(type, feature, *copy);
798c02fdc0eSGerrit Renker 
799a4bf3902SArnaldo Carvalho de Melo 	list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
800afe00251SAndrea Bittau 
801afe00251SAndrea Bittau 	return 0;
802afe00251SAndrea Bittau }
803afe00251SAndrea Bittau 
8048ca0d17bSArnaldo Carvalho de Melo static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
8058ca0d17bSArnaldo Carvalho de Melo 				    u8 type, u8 feature)
806afe00251SAndrea Bittau {
807afe00251SAndrea Bittau 	/* XXX check if other confirms for that are queued and recycle slot */
808afe00251SAndrea Bittau 	struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
809afe00251SAndrea Bittau 
810afe00251SAndrea Bittau 	if (opt == NULL) {
811afe00251SAndrea Bittau 		/* XXX what do we do?  Ignoring should be fine.  It's a change
812afe00251SAndrea Bittau 		 * after all =P
813afe00251SAndrea Bittau 		 */
814afe00251SAndrea Bittau 		return;
815afe00251SAndrea Bittau 	}
816afe00251SAndrea Bittau 
817c02fdc0eSGerrit Renker 	switch (type) {
818e576de82SJesper Juhl 	case DCCPO_CHANGE_L:
819e576de82SJesper Juhl 		opt->dccpop_type = DCCPO_CONFIRM_R;
820e576de82SJesper Juhl 		break;
821e576de82SJesper Juhl 	case DCCPO_CHANGE_R:
822e576de82SJesper Juhl 		opt->dccpop_type = DCCPO_CONFIRM_L;
823e576de82SJesper Juhl 		break;
824e576de82SJesper Juhl 	default:
825e576de82SJesper Juhl 		DCCP_WARN("invalid type %d\n", type);
826e576de82SJesper Juhl 		kfree(opt);
827e576de82SJesper Juhl 		return;
828c02fdc0eSGerrit Renker 	}
829afe00251SAndrea Bittau 	opt->dccpop_feat = feature;
83068907dadSRandy Dunlap 	opt->dccpop_val	 = NULL;
831afe00251SAndrea Bittau 	opt->dccpop_len	 = 0;
832afe00251SAndrea Bittau 
833afe00251SAndrea Bittau 	/* change feature */
834c02fdc0eSGerrit Renker 	dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
835c02fdc0eSGerrit Renker 
836a4bf3902SArnaldo Carvalho de Melo 	list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
837afe00251SAndrea Bittau }
838afe00251SAndrea Bittau 
839afe00251SAndrea Bittau static void dccp_feat_flush_confirm(struct sock *sk)
840afe00251SAndrea Bittau {
841a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *dmsk = dccp_msk(sk);
842afe00251SAndrea Bittau 	/* Check if there is anything to confirm in the first place */
843a4bf3902SArnaldo Carvalho de Melo 	int yes = !list_empty(&dmsk->dccpms_conf);
844afe00251SAndrea Bittau 
845afe00251SAndrea Bittau 	if (!yes) {
846afe00251SAndrea Bittau 		struct dccp_opt_pend *opt;
847afe00251SAndrea Bittau 
848a4bf3902SArnaldo Carvalho de Melo 		list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
849afe00251SAndrea Bittau 			if (opt->dccpop_conf) {
850afe00251SAndrea Bittau 				yes = 1;
851afe00251SAndrea Bittau 				break;
852afe00251SAndrea Bittau 			}
853afe00251SAndrea Bittau 		}
854afe00251SAndrea Bittau 	}
855afe00251SAndrea Bittau 
856afe00251SAndrea Bittau 	if (!yes)
857afe00251SAndrea Bittau 		return;
858afe00251SAndrea Bittau 
859afe00251SAndrea Bittau 	/* OK there is something to confirm... */
860afe00251SAndrea Bittau 	/* XXX check if packet is in flight?  Send delayed ack?? */
861afe00251SAndrea Bittau 	if (sk->sk_state == DCCP_OPEN)
862afe00251SAndrea Bittau 		dccp_send_ack(sk);
863afe00251SAndrea Bittau }
864afe00251SAndrea Bittau 
865afe00251SAndrea Bittau int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
866afe00251SAndrea Bittau {
867afe00251SAndrea Bittau 	int rc;
868afe00251SAndrea Bittau 
869f74e91b6SGerrit Renker 	/* Ignore Change requests other than during connection setup */
870f74e91b6SGerrit Renker 	if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
871f74e91b6SGerrit Renker 		return 0;
872c02fdc0eSGerrit Renker 	dccp_feat_debug(type, feature, *val);
873afe00251SAndrea Bittau 
874afe00251SAndrea Bittau 	/* figure out if it's SP or NN feature */
875afe00251SAndrea Bittau 	switch (feature) {
876afe00251SAndrea Bittau 	/* deal with SP features */
877afe00251SAndrea Bittau 	case DCCPF_CCID:
878afe00251SAndrea Bittau 		rc = dccp_feat_sp(sk, type, feature, val, len);
879afe00251SAndrea Bittau 		break;
880afe00251SAndrea Bittau 
881afe00251SAndrea Bittau 	/* deal with NN features */
882afe00251SAndrea Bittau 	case DCCPF_ACK_RATIO:
883afe00251SAndrea Bittau 		rc = dccp_feat_nn(sk, type, feature, val, len);
884afe00251SAndrea Bittau 		break;
885afe00251SAndrea Bittau 
886afe00251SAndrea Bittau 	/* XXX implement other features */
887afe00251SAndrea Bittau 	default:
888c02fdc0eSGerrit Renker 		dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
889c02fdc0eSGerrit Renker 			      dccp_feat_typename(type), feature);
890afe00251SAndrea Bittau 		rc = -EFAULT;
891afe00251SAndrea Bittau 		break;
892afe00251SAndrea Bittau 	}
893afe00251SAndrea Bittau 
894afe00251SAndrea Bittau 	/* check if there were problems changing features */
895afe00251SAndrea Bittau 	if (rc) {
896afe00251SAndrea Bittau 		/* If we don't agree on SP, we sent a confirm for old value.
897afe00251SAndrea Bittau 		 * However we propagate rc to caller in case option was
898afe00251SAndrea Bittau 		 * mandatory
899afe00251SAndrea Bittau 		 */
900afe00251SAndrea Bittau 		if (rc != DCCP_FEAT_SP_NOAGREE)
9018ca0d17bSArnaldo Carvalho de Melo 			dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
902afe00251SAndrea Bittau 	}
903afe00251SAndrea Bittau 
904afe00251SAndrea Bittau 	/* generate the confirm [if required] */
905afe00251SAndrea Bittau 	dccp_feat_flush_confirm(sk);
906afe00251SAndrea Bittau 
907afe00251SAndrea Bittau 	return rc;
908afe00251SAndrea Bittau }
909afe00251SAndrea Bittau 
910afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
911afe00251SAndrea Bittau 
912afe00251SAndrea Bittau int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
913afe00251SAndrea Bittau 			   u8 *val, u8 len)
914afe00251SAndrea Bittau {
915afe00251SAndrea Bittau 	u8 t;
916afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
917a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *dmsk = dccp_msk(sk);
918c02fdc0eSGerrit Renker 	int found = 0;
919afe00251SAndrea Bittau 	int all_confirmed = 1;
920afe00251SAndrea Bittau 
921f74e91b6SGerrit Renker 	/* Ignore Confirm options other than during connection setup */
922f74e91b6SGerrit Renker 	if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
923f74e91b6SGerrit Renker 		return 0;
924c02fdc0eSGerrit Renker 	dccp_feat_debug(type, feature, *val);
925afe00251SAndrea Bittau 
926afe00251SAndrea Bittau 	/* locate our change request */
927c02fdc0eSGerrit Renker 	switch (type) {
928c02fdc0eSGerrit Renker 	case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
929c02fdc0eSGerrit Renker 	case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
93059348b19SGerrit Renker 	default:	      DCCP_WARN("invalid type %d\n", type);
931c02fdc0eSGerrit Renker 			      return 1;
932c02fdc0eSGerrit Renker 
933c02fdc0eSGerrit Renker 	}
934c02fdc0eSGerrit Renker 	/* XXX sanity check feature value */
935afe00251SAndrea Bittau 
936a4bf3902SArnaldo Carvalho de Melo 	list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
937afe00251SAndrea Bittau 		if (!opt->dccpop_conf && opt->dccpop_type == t &&
938afe00251SAndrea Bittau 		    opt->dccpop_feat == feature) {
939c02fdc0eSGerrit Renker 			found = 1;
940c02fdc0eSGerrit Renker 			dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
941c02fdc0eSGerrit Renker 
942afe00251SAndrea Bittau 			/* XXX do sanity check */
943afe00251SAndrea Bittau 
944afe00251SAndrea Bittau 			opt->dccpop_conf = 1;
945afe00251SAndrea Bittau 
946afe00251SAndrea Bittau 			/* We got a confirmation---change the option */
947afe00251SAndrea Bittau 			dccp_feat_update(sk, opt->dccpop_type,
948afe00251SAndrea Bittau 					 opt->dccpop_feat, *val);
949afe00251SAndrea Bittau 
950c02fdc0eSGerrit Renker 			/* XXX check the return value of dccp_feat_update */
951afe00251SAndrea Bittau 			break;
952afe00251SAndrea Bittau 		}
953afe00251SAndrea Bittau 
954afe00251SAndrea Bittau 		if (!opt->dccpop_conf)
955afe00251SAndrea Bittau 			all_confirmed = 0;
956afe00251SAndrea Bittau 	}
957afe00251SAndrea Bittau 
958c02fdc0eSGerrit Renker 	if (!found)
959c02fdc0eSGerrit Renker 		dccp_pr_debug("%s(%d, ...) never requested\n",
960c02fdc0eSGerrit Renker 			      dccp_feat_typename(type), feature);
961afe00251SAndrea Bittau 	return 0;
962afe00251SAndrea Bittau }
963afe00251SAndrea Bittau 
964afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
965afe00251SAndrea Bittau 
9668ca0d17bSArnaldo Carvalho de Melo void dccp_feat_clean(struct dccp_minisock *dmsk)
967afe00251SAndrea Bittau {
968afe00251SAndrea Bittau 	struct dccp_opt_pend *opt, *next;
969afe00251SAndrea Bittau 
970a4bf3902SArnaldo Carvalho de Melo 	list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
971afe00251SAndrea Bittau 				 dccpop_node) {
972afe00251SAndrea Bittau 		BUG_ON(opt->dccpop_val == NULL);
973afe00251SAndrea Bittau 		kfree(opt->dccpop_val);
974afe00251SAndrea Bittau 
975afe00251SAndrea Bittau 		if (opt->dccpop_sc != NULL) {
976afe00251SAndrea Bittau 			BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
977afe00251SAndrea Bittau 			kfree(opt->dccpop_sc->dccpoc_val);
978afe00251SAndrea Bittau 			kfree(opt->dccpop_sc);
979afe00251SAndrea Bittau 		}
980afe00251SAndrea Bittau 
981afe00251SAndrea Bittau 		kfree(opt);
982afe00251SAndrea Bittau 	}
983a4bf3902SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&dmsk->dccpms_pending);
984afe00251SAndrea Bittau 
985a4bf3902SArnaldo Carvalho de Melo 	list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
986afe00251SAndrea Bittau 		BUG_ON(opt == NULL);
987afe00251SAndrea Bittau 		if (opt->dccpop_val != NULL)
988afe00251SAndrea Bittau 			kfree(opt->dccpop_val);
989afe00251SAndrea Bittau 		kfree(opt);
990afe00251SAndrea Bittau 	}
991a4bf3902SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&dmsk->dccpms_conf);
992afe00251SAndrea Bittau }
993afe00251SAndrea Bittau 
994afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_clean);
995afe00251SAndrea Bittau 
996afe00251SAndrea Bittau /* this is to be called only when a listening sock creates its child.  It is
997afe00251SAndrea Bittau  * assumed by the function---the confirm is not duplicated, but rather it is
998afe00251SAndrea Bittau  * "passed on".
999afe00251SAndrea Bittau  */
1000afe00251SAndrea Bittau int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
1001afe00251SAndrea Bittau {
1002a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *olddmsk = dccp_msk(oldsk);
1003a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *newdmsk = dccp_msk(newsk);
1004afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
1005afe00251SAndrea Bittau 	int rc = 0;
1006afe00251SAndrea Bittau 
1007a4bf3902SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&newdmsk->dccpms_pending);
1008a4bf3902SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&newdmsk->dccpms_conf);
1009afe00251SAndrea Bittau 
1010a4bf3902SArnaldo Carvalho de Melo 	list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
1011afe00251SAndrea Bittau 		struct dccp_opt_pend *newopt;
1012afe00251SAndrea Bittau 		/* copy the value of the option */
1013eed73417SArnaldo Carvalho de Melo 		u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
1014afe00251SAndrea Bittau 
1015afe00251SAndrea Bittau 		if (val == NULL)
1016afe00251SAndrea Bittau 			goto out_clean;
1017afe00251SAndrea Bittau 
1018eed73417SArnaldo Carvalho de Melo 		newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
1019afe00251SAndrea Bittau 		if (newopt == NULL) {
1020afe00251SAndrea Bittau 			kfree(val);
1021afe00251SAndrea Bittau 			goto out_clean;
1022afe00251SAndrea Bittau 		}
1023afe00251SAndrea Bittau 
1024afe00251SAndrea Bittau 		/* insert the option */
1025afe00251SAndrea Bittau 		newopt->dccpop_val = val;
1026a4bf3902SArnaldo Carvalho de Melo 		list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
1027afe00251SAndrea Bittau 
1028afe00251SAndrea Bittau 		/* XXX what happens with backlogs and multiple connections at
1029afe00251SAndrea Bittau 		 * once...
1030afe00251SAndrea Bittau 		 */
1031afe00251SAndrea Bittau 		/* the master socket no longer needs to worry about confirms */
103268907dadSRandy Dunlap 		opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
1033afe00251SAndrea Bittau 
1034afe00251SAndrea Bittau 		/* reset state for a new socket */
1035afe00251SAndrea Bittau 		opt->dccpop_conf = 0;
1036afe00251SAndrea Bittau 	}
1037afe00251SAndrea Bittau 
1038afe00251SAndrea Bittau 	/* XXX not doing anything about the conf queue */
1039afe00251SAndrea Bittau 
1040afe00251SAndrea Bittau out:
1041afe00251SAndrea Bittau 	return rc;
1042afe00251SAndrea Bittau 
1043afe00251SAndrea Bittau out_clean:
10448ca0d17bSArnaldo Carvalho de Melo 	dccp_feat_clean(newdmsk);
1045afe00251SAndrea Bittau 	rc = -ENOMEM;
1046afe00251SAndrea Bittau 	goto out;
1047afe00251SAndrea Bittau }
1048afe00251SAndrea Bittau 
1049afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_clone);
1050afe00251SAndrea Bittau 
1051e8ef967aSGerrit Renker int dccp_feat_init(struct sock *sk)
1052afe00251SAndrea Bittau {
1053e8ef967aSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
1054e8ef967aSGerrit Renker 	struct dccp_minisock *dmsk = dccp_msk(sk);
1055afe00251SAndrea Bittau 	int rc;
1056afe00251SAndrea Bittau 
1057e8ef967aSGerrit Renker 	INIT_LIST_HEAD(&dmsk->dccpms_pending);	/* XXX no longer used */
1058e8ef967aSGerrit Renker 	INIT_LIST_HEAD(&dmsk->dccpms_conf);	/* XXX no longer used */
1059afe00251SAndrea Bittau 
1060afe00251SAndrea Bittau 	/* CCID L */
1061e8ef967aSGerrit Renker 	rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
1062a4bf3902SArnaldo Carvalho de Melo 				&dmsk->dccpms_tx_ccid, 1);
1063afe00251SAndrea Bittau 	if (rc)
1064afe00251SAndrea Bittau 		goto out;
1065afe00251SAndrea Bittau 
1066afe00251SAndrea Bittau 	/* CCID R */
1067e8ef967aSGerrit Renker 	rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
1068a4bf3902SArnaldo Carvalho de Melo 				&dmsk->dccpms_rx_ccid, 1);
1069afe00251SAndrea Bittau 	if (rc)
1070afe00251SAndrea Bittau 		goto out;
1071afe00251SAndrea Bittau 
1072afe00251SAndrea Bittau 	/* Ack ratio */
1073e8ef967aSGerrit Renker 	rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
1074e8ef967aSGerrit Renker 				dmsk->dccpms_ack_ratio);
1075afe00251SAndrea Bittau out:
1076afe00251SAndrea Bittau 	return rc;
1077afe00251SAndrea Bittau }
1078afe00251SAndrea Bittau 
1079afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_init);
1080c02fdc0eSGerrit Renker 
1081c02fdc0eSGerrit Renker #ifdef CONFIG_IP_DCCP_DEBUG
1082c02fdc0eSGerrit Renker const char *dccp_feat_typename(const u8 type)
1083c02fdc0eSGerrit Renker {
1084c02fdc0eSGerrit Renker 	switch(type) {
1085c02fdc0eSGerrit Renker 	case DCCPO_CHANGE_L:  return("ChangeL");
1086c02fdc0eSGerrit Renker 	case DCCPO_CONFIRM_L: return("ConfirmL");
1087c02fdc0eSGerrit Renker 	case DCCPO_CHANGE_R:  return("ChangeR");
1088c02fdc0eSGerrit Renker 	case DCCPO_CONFIRM_R: return("ConfirmR");
1089c02fdc0eSGerrit Renker 	/* the following case must not appear in feature negotation  */
1090c02fdc0eSGerrit Renker 	default:	      dccp_pr_debug("unknown type %d [BUG!]\n", type);
1091c02fdc0eSGerrit Renker 	}
1092c02fdc0eSGerrit Renker 	return NULL;
1093c02fdc0eSGerrit Renker }
1094c02fdc0eSGerrit Renker 
1095c02fdc0eSGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_typename);
1096c02fdc0eSGerrit Renker 
1097c02fdc0eSGerrit Renker const char *dccp_feat_name(const u8 feat)
1098c02fdc0eSGerrit Renker {
1099c02fdc0eSGerrit Renker 	static const char *feature_names[] = {
1100c02fdc0eSGerrit Renker 		[DCCPF_RESERVED]	= "Reserved",
1101c02fdc0eSGerrit Renker 		[DCCPF_CCID]		= "CCID",
1102c02fdc0eSGerrit Renker 		[DCCPF_SHORT_SEQNOS]	= "Allow Short Seqnos",
1103c02fdc0eSGerrit Renker 		[DCCPF_SEQUENCE_WINDOW]	= "Sequence Window",
1104c02fdc0eSGerrit Renker 		[DCCPF_ECN_INCAPABLE]	= "ECN Incapable",
1105c02fdc0eSGerrit Renker 		[DCCPF_ACK_RATIO]	= "Ack Ratio",
1106c02fdc0eSGerrit Renker 		[DCCPF_SEND_ACK_VECTOR]	= "Send ACK Vector",
1107c02fdc0eSGerrit Renker 		[DCCPF_SEND_NDP_COUNT]	= "Send NDP Count",
1108c02fdc0eSGerrit Renker 		[DCCPF_MIN_CSUM_COVER]	= "Min. Csum Coverage",
1109c02fdc0eSGerrit Renker 		[DCCPF_DATA_CHECKSUM]	= "Send Data Checksum",
1110c02fdc0eSGerrit Renker 	};
1111dd6303dfSGerrit Renker 	if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
1112dd6303dfSGerrit Renker 		return feature_names[DCCPF_RESERVED];
1113dd6303dfSGerrit Renker 
11147d43d1a0SGerrit Renker 	if (feat ==  DCCPF_SEND_LEV_RATE)
11157d43d1a0SGerrit Renker 		return "Send Loss Event Rate";
1116c02fdc0eSGerrit Renker 	if (feat >= DCCPF_MIN_CCID_SPECIFIC)
1117c02fdc0eSGerrit Renker 		return "CCID-specific";
1118c02fdc0eSGerrit Renker 
1119c02fdc0eSGerrit Renker 	return feature_names[feat];
1120c02fdc0eSGerrit Renker }
1121c02fdc0eSGerrit Renker 
1122c02fdc0eSGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_name);
1123c02fdc0eSGerrit Renker #endif /* CONFIG_IP_DCCP_DEBUG */
1124