xref: /openbmc/linux/net/dccp/feat.c (revision 0971d17ca3d80f61863f4750091a64448bf91600)
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  */
1690c116839SGerrit Renker static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
1700c116839SGerrit Renker 						     u8 feat_num, bool is_local)
1710c116839SGerrit Renker {
1720c116839SGerrit Renker 	struct dccp_feat_entry *entry;
1730c116839SGerrit Renker 
1743d3e35aaSGerrit Renker 	list_for_each_entry(entry, fn_list, node) {
1750c116839SGerrit Renker 		if (entry->feat_num == feat_num && entry->is_local == is_local)
1760c116839SGerrit Renker 			return entry;
1770c116839SGerrit Renker 		else if (entry->feat_num > feat_num)
1780c116839SGerrit Renker 			break;
1793d3e35aaSGerrit Renker 	}
1800c116839SGerrit Renker 	return NULL;
1810c116839SGerrit Renker }
18261e6473eSGerrit Renker 
183e8ef967aSGerrit Renker /**
184e8ef967aSGerrit Renker  * dccp_feat_entry_new  -  Central list update routine (called by all others)
185e8ef967aSGerrit Renker  * @head:  list to add to
186e8ef967aSGerrit Renker  * @feat:  feature number
187e8ef967aSGerrit Renker  * @local: whether the local (1) or remote feature with number @feat is meant
188e8ef967aSGerrit Renker  * This is the only constructor and serves to ensure the above invariants.
189e8ef967aSGerrit Renker  */
190e8ef967aSGerrit Renker static struct dccp_feat_entry *
191e8ef967aSGerrit Renker 	      dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
192e8ef967aSGerrit Renker {
193e8ef967aSGerrit Renker 	struct dccp_feat_entry *entry;
194e8ef967aSGerrit Renker 
195e8ef967aSGerrit Renker 	list_for_each_entry(entry, head, node)
196e8ef967aSGerrit Renker 		if (entry->feat_num == feat && entry->is_local == local) {
197e8ef967aSGerrit Renker 			dccp_feat_val_destructor(entry->feat_num, &entry->val);
198e8ef967aSGerrit Renker 			return entry;
199e8ef967aSGerrit Renker 		} else if (entry->feat_num > feat) {
200e8ef967aSGerrit Renker 			head = &entry->node;
201e8ef967aSGerrit Renker 			break;
202e8ef967aSGerrit Renker 		}
203e8ef967aSGerrit Renker 
204e8ef967aSGerrit Renker 	entry = kmalloc(sizeof(*entry), gfp_any());
205e8ef967aSGerrit Renker 	if (entry != NULL) {
206e8ef967aSGerrit Renker 		entry->feat_num = feat;
207e8ef967aSGerrit Renker 		entry->is_local = local;
208e8ef967aSGerrit Renker 		list_add_tail(&entry->node, head);
209e8ef967aSGerrit Renker 	}
210e8ef967aSGerrit Renker 	return entry;
211e8ef967aSGerrit Renker }
212e8ef967aSGerrit Renker 
213e8ef967aSGerrit Renker /**
214e8ef967aSGerrit Renker  * dccp_feat_push_change  -  Add/overwrite a Change option in the list
215e8ef967aSGerrit Renker  * @fn_list: feature-negotiation list to update
216e8ef967aSGerrit Renker  * @feat: one of %dccp_feature_numbers
217e8ef967aSGerrit Renker  * @local: whether local (1) or remote (0) @feat_num is meant
218e8ef967aSGerrit Renker  * @needs_mandatory: whether to use Mandatory feature negotiation options
219e8ef967aSGerrit Renker  * @fval: pointer to NN/SP value to be inserted (will be copied)
220e8ef967aSGerrit Renker  */
221e8ef967aSGerrit Renker static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
222e8ef967aSGerrit Renker 				 u8 mandatory, dccp_feat_val *fval)
223e8ef967aSGerrit Renker {
224e8ef967aSGerrit Renker 	struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
225e8ef967aSGerrit Renker 
226e8ef967aSGerrit Renker 	if (new == NULL)
227e8ef967aSGerrit Renker 		return -ENOMEM;
228e8ef967aSGerrit Renker 
229e8ef967aSGerrit Renker 	new->feat_num	     = feat;
230e8ef967aSGerrit Renker 	new->is_local	     = local;
231e8ef967aSGerrit Renker 	new->state	     = FEAT_INITIALISING;
232e8ef967aSGerrit Renker 	new->needs_confirm   = 0;
233e8ef967aSGerrit Renker 	new->empty_confirm   = 0;
234e8ef967aSGerrit Renker 	new->val	     = *fval;
235e8ef967aSGerrit Renker 	new->needs_mandatory = mandatory;
236e8ef967aSGerrit Renker 
237e8ef967aSGerrit Renker 	return 0;
238e8ef967aSGerrit Renker }
239e8ef967aSGerrit Renker 
24061e6473eSGerrit Renker static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
24161e6473eSGerrit Renker {
24261e6473eSGerrit Renker 	list_del(&entry->node);
24361e6473eSGerrit Renker 	dccp_feat_entry_destructor(entry);
24461e6473eSGerrit Renker }
24561e6473eSGerrit Renker 
24661e6473eSGerrit Renker void dccp_feat_list_purge(struct list_head *fn_list)
24761e6473eSGerrit Renker {
24861e6473eSGerrit Renker 	struct dccp_feat_entry *entry, *next;
24961e6473eSGerrit Renker 
25061e6473eSGerrit Renker 	list_for_each_entry_safe(entry, next, fn_list, node)
25161e6473eSGerrit Renker 		dccp_feat_entry_destructor(entry);
25261e6473eSGerrit Renker 	INIT_LIST_HEAD(fn_list);
25361e6473eSGerrit Renker }
25461e6473eSGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
25561e6473eSGerrit Renker 
256ac75773cSGerrit Renker /* generate @to as full clone of @from - @to must not contain any nodes */
257ac75773cSGerrit Renker int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
258ac75773cSGerrit Renker {
259ac75773cSGerrit Renker 	struct dccp_feat_entry *entry, *new;
260ac75773cSGerrit Renker 
261ac75773cSGerrit Renker 	INIT_LIST_HEAD(to);
262ac75773cSGerrit Renker 	list_for_each_entry(entry, from, node) {
263ac75773cSGerrit Renker 		new = dccp_feat_clone_entry(entry);
264ac75773cSGerrit Renker 		if (new == NULL)
265ac75773cSGerrit Renker 			goto cloning_failed;
266ac75773cSGerrit Renker 		list_add_tail(&new->node, to);
267ac75773cSGerrit Renker 	}
268ac75773cSGerrit Renker 	return 0;
269ac75773cSGerrit Renker 
270ac75773cSGerrit Renker cloning_failed:
271ac75773cSGerrit Renker 	dccp_feat_list_purge(to);
272ac75773cSGerrit Renker 	return -ENOMEM;
273ac75773cSGerrit Renker }
274ac75773cSGerrit Renker 
275*0971d17cSGerrit Renker /**
276*0971d17cSGerrit Renker  * dccp_feat_valid_nn_length  -  Enforce length constraints on NN options
277*0971d17cSGerrit Renker  * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
278*0971d17cSGerrit Renker  * incoming options are accepted as long as their values are valid.
279*0971d17cSGerrit Renker  */
280*0971d17cSGerrit Renker static u8 dccp_feat_valid_nn_length(u8 feat_num)
281*0971d17cSGerrit Renker {
282*0971d17cSGerrit Renker 	if (feat_num == DCCPF_ACK_RATIO)	/* RFC 4340, 11.3 and 6.6.8 */
283*0971d17cSGerrit Renker 		return 2;
284*0971d17cSGerrit Renker 	if (feat_num == DCCPF_SEQUENCE_WINDOW)	/* RFC 4340, 7.5.2 and 6.5  */
285*0971d17cSGerrit Renker 		return 6;
286*0971d17cSGerrit Renker 	return 0;
287*0971d17cSGerrit Renker }
288*0971d17cSGerrit Renker 
289e8ef967aSGerrit Renker static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
290e8ef967aSGerrit Renker {
291e8ef967aSGerrit Renker 	switch (feat_num) {
292e8ef967aSGerrit Renker 	case DCCPF_ACK_RATIO:
293e8ef967aSGerrit Renker 		return val <= DCCPF_ACK_RATIO_MAX;
294e8ef967aSGerrit Renker 	case DCCPF_SEQUENCE_WINDOW:
295e8ef967aSGerrit Renker 		return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
296e8ef967aSGerrit Renker 	}
297e8ef967aSGerrit Renker 	return 0;	/* feature unknown - so we can't tell */
298e8ef967aSGerrit Renker }
299e8ef967aSGerrit Renker 
300e8ef967aSGerrit Renker /* check that SP values are within the ranges defined in RFC 4340 */
301e8ef967aSGerrit Renker static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
302e8ef967aSGerrit Renker {
303e8ef967aSGerrit Renker 	switch (feat_num) {
304e8ef967aSGerrit Renker 	case DCCPF_CCID:
305e8ef967aSGerrit Renker 		return val == DCCPC_CCID2 || val == DCCPC_CCID3;
306e8ef967aSGerrit Renker 	/* Type-check Boolean feature values: */
307e8ef967aSGerrit Renker 	case DCCPF_SHORT_SEQNOS:
308e8ef967aSGerrit Renker 	case DCCPF_ECN_INCAPABLE:
309e8ef967aSGerrit Renker 	case DCCPF_SEND_ACK_VECTOR:
310e8ef967aSGerrit Renker 	case DCCPF_SEND_NDP_COUNT:
311e8ef967aSGerrit Renker 	case DCCPF_DATA_CHECKSUM:
312e8ef967aSGerrit Renker 	case DCCPF_SEND_LEV_RATE:
313e8ef967aSGerrit Renker 		return val < 2;
314e8ef967aSGerrit Renker 	case DCCPF_MIN_CSUM_COVER:
315e8ef967aSGerrit Renker 		return val < 16;
316e8ef967aSGerrit Renker 	}
317e8ef967aSGerrit Renker 	return 0;			/* feature unknown */
318e8ef967aSGerrit Renker }
319e8ef967aSGerrit Renker 
320e8ef967aSGerrit Renker static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
321e8ef967aSGerrit Renker {
322e8ef967aSGerrit Renker 	if (sp_list == NULL || sp_len < 1)
323e8ef967aSGerrit Renker 		return 0;
324e8ef967aSGerrit Renker 	while (sp_len--)
325e8ef967aSGerrit Renker 		if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
326e8ef967aSGerrit Renker 			return 0;
327e8ef967aSGerrit Renker 	return 1;
328e8ef967aSGerrit Renker }
329e8ef967aSGerrit Renker 
330e8ef967aSGerrit Renker /**
331*0971d17cSGerrit Renker  * dccp_feat_insert_opts  -  Generate FN options from current list state
332*0971d17cSGerrit Renker  * @skb: next sk_buff to be sent to the peer
333*0971d17cSGerrit Renker  * @dp: for client during handshake and general negotiation
334*0971d17cSGerrit Renker  * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
335*0971d17cSGerrit Renker  */
336*0971d17cSGerrit Renker int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
337*0971d17cSGerrit Renker 			  struct sk_buff *skb)
338*0971d17cSGerrit Renker {
339*0971d17cSGerrit Renker 	struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
340*0971d17cSGerrit Renker 	struct dccp_feat_entry *pos, *next;
341*0971d17cSGerrit Renker 	u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
342*0971d17cSGerrit Renker 	bool rpt;
343*0971d17cSGerrit Renker 
344*0971d17cSGerrit Renker 	/* put entries into @skb in the order they appear in the list */
345*0971d17cSGerrit Renker 	list_for_each_entry_safe_reverse(pos, next, fn, node) {
346*0971d17cSGerrit Renker 		opt  = dccp_feat_genopt(pos);
347*0971d17cSGerrit Renker 		type = dccp_feat_type(pos->feat_num);
348*0971d17cSGerrit Renker 		rpt  = false;
349*0971d17cSGerrit Renker 
350*0971d17cSGerrit Renker 		if (pos->empty_confirm) {
351*0971d17cSGerrit Renker 			len = 0;
352*0971d17cSGerrit Renker 			ptr = NULL;
353*0971d17cSGerrit Renker 		} else {
354*0971d17cSGerrit Renker 			if (type == FEAT_SP) {
355*0971d17cSGerrit Renker 				len = pos->val.sp.len;
356*0971d17cSGerrit Renker 				ptr = pos->val.sp.vec;
357*0971d17cSGerrit Renker 				rpt = pos->needs_confirm;
358*0971d17cSGerrit Renker 			} else if (type == FEAT_NN) {
359*0971d17cSGerrit Renker 				len = dccp_feat_valid_nn_length(pos->feat_num);
360*0971d17cSGerrit Renker 				ptr = nn_in_nbo;
361*0971d17cSGerrit Renker 				dccp_encode_value_var(pos->val.nn, ptr, len);
362*0971d17cSGerrit Renker 			} else {
363*0971d17cSGerrit Renker 				DCCP_BUG("unknown feature %u", pos->feat_num);
364*0971d17cSGerrit Renker 				return -1;
365*0971d17cSGerrit Renker 			}
366*0971d17cSGerrit Renker 		}
367*0971d17cSGerrit Renker 
368*0971d17cSGerrit Renker 		if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
369*0971d17cSGerrit Renker 			return -1;
370*0971d17cSGerrit Renker 		if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
371*0971d17cSGerrit Renker 			return -1;
372*0971d17cSGerrit Renker 		/*
373*0971d17cSGerrit Renker 		 * Enter CHANGING after transmitting the Change option (6.6.2).
374*0971d17cSGerrit Renker 		 */
375*0971d17cSGerrit Renker 		if (pos->state == FEAT_INITIALISING)
376*0971d17cSGerrit Renker 			pos->state = FEAT_CHANGING;
377*0971d17cSGerrit Renker 	}
378*0971d17cSGerrit Renker 	return 0;
379*0971d17cSGerrit Renker }
380*0971d17cSGerrit Renker 
381*0971d17cSGerrit Renker /**
382e8ef967aSGerrit Renker  * __feat_register_nn  -  Register new NN value on socket
383e8ef967aSGerrit Renker  * @fn: feature-negotiation list to register with
384e8ef967aSGerrit Renker  * @feat: an NN feature from %dccp_feature_numbers
385e8ef967aSGerrit Renker  * @mandatory: use Mandatory option if 1
386e8ef967aSGerrit Renker  * @nn_val: value to register (restricted to 4 bytes)
387e8ef967aSGerrit Renker  * Note that NN features are local by definition (RFC 4340, 6.3.2).
388e8ef967aSGerrit Renker  */
389e8ef967aSGerrit Renker static int __feat_register_nn(struct list_head *fn, u8 feat,
390e8ef967aSGerrit Renker 			      u8 mandatory, u64 nn_val)
391e8ef967aSGerrit Renker {
392e8ef967aSGerrit Renker 	dccp_feat_val fval = { .nn = nn_val };
393e8ef967aSGerrit Renker 
394e8ef967aSGerrit Renker 	if (dccp_feat_type(feat) != FEAT_NN ||
395e8ef967aSGerrit Renker 	    !dccp_feat_is_valid_nn_val(feat, nn_val))
396e8ef967aSGerrit Renker 		return -EINVAL;
397e8ef967aSGerrit Renker 
398e8ef967aSGerrit Renker 	/* Don't bother with default values, they will be activated anyway. */
399e8ef967aSGerrit Renker 	if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
400e8ef967aSGerrit Renker 		return 0;
401e8ef967aSGerrit Renker 
402e8ef967aSGerrit Renker 	return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
403e8ef967aSGerrit Renker }
404e8ef967aSGerrit Renker 
405e8ef967aSGerrit Renker /**
406e8ef967aSGerrit Renker  * __feat_register_sp  -  Register new SP value/list on socket
407e8ef967aSGerrit Renker  * @fn: feature-negotiation list to register with
408e8ef967aSGerrit Renker  * @feat: an SP feature from %dccp_feature_numbers
409e8ef967aSGerrit Renker  * @is_local: whether the local (1) or the remote (0) @feat is meant
410e8ef967aSGerrit Renker  * @mandatory: use Mandatory option if 1
411e8ef967aSGerrit Renker  * @sp_val: SP value followed by optional preference list
412e8ef967aSGerrit Renker  * @sp_len: length of @sp_val in bytes
413e8ef967aSGerrit Renker  */
414e8ef967aSGerrit Renker static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
415e8ef967aSGerrit Renker 			      u8 mandatory, u8 const *sp_val, u8 sp_len)
416e8ef967aSGerrit Renker {
417e8ef967aSGerrit Renker 	dccp_feat_val fval;
418e8ef967aSGerrit Renker 
419e8ef967aSGerrit Renker 	if (dccp_feat_type(feat) != FEAT_SP ||
420e8ef967aSGerrit Renker 	    !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
421e8ef967aSGerrit Renker 		return -EINVAL;
422e8ef967aSGerrit Renker 
423d90ebcbfSGerrit Renker 	/* Avoid negotiating alien CCIDs by only advertising supported ones */
424d90ebcbfSGerrit Renker 	if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
425d90ebcbfSGerrit Renker 		return -EOPNOTSUPP;
426d90ebcbfSGerrit Renker 
427e8ef967aSGerrit Renker 	if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
428e8ef967aSGerrit Renker 		return -ENOMEM;
429e8ef967aSGerrit Renker 
430e8ef967aSGerrit Renker 	return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
431e8ef967aSGerrit Renker }
432e8ef967aSGerrit Renker 
43349aebc66SGerrit Renker /**
43449aebc66SGerrit Renker  * dccp_feat_register_sp  -  Register requests to change SP feature values
43549aebc66SGerrit Renker  * @sk: client or listening socket
43649aebc66SGerrit Renker  * @feat: one of %dccp_feature_numbers
43749aebc66SGerrit Renker  * @is_local: whether the local (1) or remote (0) @feat is meant
43849aebc66SGerrit Renker  * @list: array of preferred values, in descending order of preference
43949aebc66SGerrit Renker  * @len: length of @list in bytes
44049aebc66SGerrit Renker  */
44149aebc66SGerrit Renker int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
44249aebc66SGerrit Renker 			  u8 const *list, u8 len)
44349aebc66SGerrit Renker {	 /* any changes must be registered before establishing the connection */
44449aebc66SGerrit Renker 	if (sk->sk_state != DCCP_CLOSED)
44549aebc66SGerrit Renker 		return -EISCONN;
44649aebc66SGerrit Renker 	if (dccp_feat_type(feat) != FEAT_SP)
44719443178SChris Wright 		return -EINVAL;
44849aebc66SGerrit Renker 	return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
44949aebc66SGerrit Renker 				  0, list, len);
450afe00251SAndrea Bittau }
451afe00251SAndrea Bittau 
45249aebc66SGerrit Renker /* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
45349aebc66SGerrit Renker int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
45449aebc66SGerrit Renker {
45549aebc66SGerrit Renker 	/* any changes must be registered before establishing the connection */
45649aebc66SGerrit Renker 	if (sk->sk_state != DCCP_CLOSED)
45749aebc66SGerrit Renker 		return -EISCONN;
45849aebc66SGerrit Renker 	if (dccp_feat_type(feat) != FEAT_NN)
45949aebc66SGerrit Renker 		return -EINVAL;
46049aebc66SGerrit Renker 	return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
461afe00251SAndrea Bittau }
462afe00251SAndrea Bittau 
4639eca0a47SGerrit Renker /*
4649eca0a47SGerrit Renker  *	Tracking features whose value depend on the choice of CCID
4659eca0a47SGerrit Renker  *
4669eca0a47SGerrit Renker  * This is designed with an extension in mind so that a list walk could be done
4679eca0a47SGerrit Renker  * before activating any features. However, the existing framework was found to
4689eca0a47SGerrit Renker  * work satisfactorily up until now, the automatic verification is left open.
4699eca0a47SGerrit Renker  * When adding new CCIDs, add a corresponding dependency table here.
4709eca0a47SGerrit Renker  */
4719eca0a47SGerrit Renker static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
4729eca0a47SGerrit Renker {
4739eca0a47SGerrit Renker 	static const struct ccid_dependency ccid2_dependencies[2][2] = {
4749eca0a47SGerrit Renker 		/*
4759eca0a47SGerrit Renker 		 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
4769eca0a47SGerrit Renker 		 * feature and Send Ack Vector is an RX feature, `is_local'
4779eca0a47SGerrit Renker 		 * needs to be reversed.
4789eca0a47SGerrit Renker 		 */
4799eca0a47SGerrit Renker 		{	/* Dependencies of the receiver-side (remote) CCID2 */
4809eca0a47SGerrit Renker 			{
4819eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
4829eca0a47SGerrit Renker 				.is_local	= true,
4839eca0a47SGerrit Renker 				.is_mandatory	= true,
4849eca0a47SGerrit Renker 				.val		= 1
4859eca0a47SGerrit Renker 			},
4869eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
4879eca0a47SGerrit Renker 		},
4889eca0a47SGerrit Renker 		{	/* Dependencies of the sender-side (local) CCID2 */
4899eca0a47SGerrit Renker 			{
4909eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
4919eca0a47SGerrit Renker 				.is_local	= false,
4929eca0a47SGerrit Renker 				.is_mandatory	= true,
4939eca0a47SGerrit Renker 				.val		= 1
4949eca0a47SGerrit Renker 			},
4959eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
4969eca0a47SGerrit Renker 		}
4979eca0a47SGerrit Renker 	};
4989eca0a47SGerrit Renker 	static const struct ccid_dependency ccid3_dependencies[2][5] = {
4999eca0a47SGerrit Renker 		{	/*
5009eca0a47SGerrit Renker 			 * Dependencies of the receiver-side CCID3
5019eca0a47SGerrit Renker 			 */
5029eca0a47SGerrit Renker 			{	/* locally disable Ack Vectors */
5039eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
5049eca0a47SGerrit Renker 				.is_local	= true,
5059eca0a47SGerrit Renker 				.is_mandatory	= false,
5069eca0a47SGerrit Renker 				.val		= 0
5079eca0a47SGerrit Renker 			},
5089eca0a47SGerrit Renker 			{	/* see below why Send Loss Event Rate is on */
5099eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_LEV_RATE,
5109eca0a47SGerrit Renker 				.is_local	= true,
5119eca0a47SGerrit Renker 				.is_mandatory	= true,
5129eca0a47SGerrit Renker 				.val		= 1
5139eca0a47SGerrit Renker 			},
5149eca0a47SGerrit Renker 			{	/* NDP Count is needed as per RFC 4342, 6.1.1 */
5159eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_NDP_COUNT,
5169eca0a47SGerrit Renker 				.is_local	= false,
5179eca0a47SGerrit Renker 				.is_mandatory	= true,
5189eca0a47SGerrit Renker 				.val		= 1
5199eca0a47SGerrit Renker 			},
5209eca0a47SGerrit Renker 			{ 0, 0, 0, 0 },
5219eca0a47SGerrit Renker 		},
5229eca0a47SGerrit Renker 		{	/*
5239eca0a47SGerrit Renker 			 * CCID3 at the TX side: we request that the HC-receiver
5249eca0a47SGerrit Renker 			 * will not send Ack Vectors (they will be ignored, so
5259eca0a47SGerrit Renker 			 * Mandatory is not set); we enable Send Loss Event Rate
5269eca0a47SGerrit Renker 			 * (Mandatory since the implementation does not support
5279eca0a47SGerrit Renker 			 * the Loss Intervals option of RFC 4342, 8.6).
5289eca0a47SGerrit Renker 			 * The last two options are for peer's information only.
5299eca0a47SGerrit Renker 			*/
5309eca0a47SGerrit Renker 			{
5319eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
5329eca0a47SGerrit Renker 				.is_local	= false,
5339eca0a47SGerrit Renker 				.is_mandatory	= false,
5349eca0a47SGerrit Renker 				.val		= 0
5359eca0a47SGerrit Renker 			},
5369eca0a47SGerrit Renker 			{
5379eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_LEV_RATE,
5389eca0a47SGerrit Renker 				.is_local	= false,
5399eca0a47SGerrit Renker 				.is_mandatory	= true,
5409eca0a47SGerrit Renker 				.val		= 1
5419eca0a47SGerrit Renker 			},
5429eca0a47SGerrit Renker 			{	/* this CCID does not support Ack Ratio */
5439eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_ACK_RATIO,
5449eca0a47SGerrit Renker 				.is_local	= true,
5459eca0a47SGerrit Renker 				.is_mandatory	= false,
5469eca0a47SGerrit Renker 				.val		= 0
5479eca0a47SGerrit Renker 			},
5489eca0a47SGerrit Renker 			{	/* tell receiver we are sending NDP counts */
5499eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_NDP_COUNT,
5509eca0a47SGerrit Renker 				.is_local	= true,
5519eca0a47SGerrit Renker 				.is_mandatory	= false,
5529eca0a47SGerrit Renker 				.val		= 1
5539eca0a47SGerrit Renker 			},
5549eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
5559eca0a47SGerrit Renker 		}
5569eca0a47SGerrit Renker 	};
5579eca0a47SGerrit Renker 	switch (ccid) {
5589eca0a47SGerrit Renker 	case DCCPC_CCID2:
5599eca0a47SGerrit Renker 		return ccid2_dependencies[is_local];
5609eca0a47SGerrit Renker 	case DCCPC_CCID3:
5619eca0a47SGerrit Renker 		return ccid3_dependencies[is_local];
5629eca0a47SGerrit Renker 	default:
5639eca0a47SGerrit Renker 		return NULL;
5649eca0a47SGerrit Renker 	}
5659eca0a47SGerrit Renker }
5669eca0a47SGerrit Renker 
5679eca0a47SGerrit Renker /**
5689eca0a47SGerrit Renker  * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
5699eca0a47SGerrit Renker  * @fn: feature-negotiation list to update
5709eca0a47SGerrit Renker  * @id: CCID number to track
5719eca0a47SGerrit Renker  * @is_local: whether TX CCID (1) or RX CCID (0) is meant
5729eca0a47SGerrit Renker  * This function needs to be called after registering all other features.
5739eca0a47SGerrit Renker  */
5749eca0a47SGerrit Renker static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
5759eca0a47SGerrit Renker {
5769eca0a47SGerrit Renker 	const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
5779eca0a47SGerrit Renker 	int i, rc = (table == NULL);
5789eca0a47SGerrit Renker 
5799eca0a47SGerrit Renker 	for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
5809eca0a47SGerrit Renker 		if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
5819eca0a47SGerrit Renker 			rc = __feat_register_sp(fn, table[i].dependent_feat,
5829eca0a47SGerrit Renker 						    table[i].is_local,
5839eca0a47SGerrit Renker 						    table[i].is_mandatory,
5849eca0a47SGerrit Renker 						    &table[i].val, 1);
5859eca0a47SGerrit Renker 		else
5869eca0a47SGerrit Renker 			rc = __feat_register_nn(fn, table[i].dependent_feat,
5879eca0a47SGerrit Renker 						    table[i].is_mandatory,
5889eca0a47SGerrit Renker 						    table[i].val);
5899eca0a47SGerrit Renker 	return rc;
5909eca0a47SGerrit Renker }
5919eca0a47SGerrit Renker 
5929eca0a47SGerrit Renker /**
5939eca0a47SGerrit Renker  * dccp_feat_finalise_settings  -  Finalise settings before starting negotiation
5949eca0a47SGerrit Renker  * @dp: client or listening socket (settings will be inherited)
5959eca0a47SGerrit Renker  * This is called after all registrations (socket initialisation, sysctls, and
5969eca0a47SGerrit Renker  * sockopt calls), and before sending the first packet containing Change options
5979eca0a47SGerrit Renker  * (ie. client-Request or server-Response), to ensure internal consistency.
5989eca0a47SGerrit Renker  */
5999eca0a47SGerrit Renker int dccp_feat_finalise_settings(struct dccp_sock *dp)
6009eca0a47SGerrit Renker {
6019eca0a47SGerrit Renker 	struct list_head *fn = &dp->dccps_featneg;
6029eca0a47SGerrit Renker 	struct dccp_feat_entry *entry;
6039eca0a47SGerrit Renker 	int i = 2, ccids[2] = { -1, -1 };
6049eca0a47SGerrit Renker 
6059eca0a47SGerrit Renker 	/*
6069eca0a47SGerrit Renker 	 * Propagating CCIDs:
6079eca0a47SGerrit Renker 	 * 1) not useful to propagate CCID settings if this host advertises more
6089eca0a47SGerrit Renker 	 *    than one CCID: the choice of CCID  may still change - if this is
6099eca0a47SGerrit Renker 	 *    the client, or if this is the server and the client sends
6109eca0a47SGerrit Renker 	 *    singleton CCID values.
6119eca0a47SGerrit Renker 	 * 2) since is that propagate_ccid changes the list, we defer changing
6129eca0a47SGerrit Renker 	 *    the sorted list until after the traversal.
6139eca0a47SGerrit Renker 	 */
6149eca0a47SGerrit Renker 	list_for_each_entry(entry, fn, node)
6159eca0a47SGerrit Renker 		if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
6169eca0a47SGerrit Renker 			ccids[entry->is_local] = entry->val.sp.vec[0];
6179eca0a47SGerrit Renker 	while (i--)
6189eca0a47SGerrit Renker 		if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
6199eca0a47SGerrit Renker 			return -1;
6209eca0a47SGerrit Renker 	return 0;
6219eca0a47SGerrit Renker }
6229eca0a47SGerrit Renker 
6230c116839SGerrit Renker /**
6240c116839SGerrit Renker  * dccp_feat_server_ccid_dependencies  -  Resolve CCID-dependent features
6250c116839SGerrit Renker  * It is the server which resolves the dependencies once the CCID has been
6260c116839SGerrit Renker  * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
6270c116839SGerrit Renker  */
6280c116839SGerrit Renker int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
6290c116839SGerrit Renker {
6300c116839SGerrit Renker 	struct list_head *fn = &dreq->dreq_featneg;
6310c116839SGerrit Renker 	struct dccp_feat_entry *entry;
6320c116839SGerrit Renker 	u8 is_local, ccid;
6330c116839SGerrit Renker 
6340c116839SGerrit Renker 	for (is_local = 0; is_local <= 1; is_local++) {
6350c116839SGerrit Renker 		entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
6360c116839SGerrit Renker 
6370c116839SGerrit Renker 		if (entry != NULL && !entry->empty_confirm)
6380c116839SGerrit Renker 			ccid = entry->val.sp.vec[0];
6390c116839SGerrit Renker 		else
6400c116839SGerrit Renker 			ccid = dccp_feat_default_value(DCCPF_CCID);
6410c116839SGerrit Renker 
6420c116839SGerrit Renker 		if (dccp_feat_propagate_ccid(fn, ccid, is_local))
6430c116839SGerrit Renker 			return -1;
6440c116839SGerrit Renker 	}
6450c116839SGerrit Renker 	return 0;
6460c116839SGerrit Renker }
6470c116839SGerrit Renker 
6486ffd30fbSAndrea Bittau static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
6496ffd30fbSAndrea Bittau {
6506ffd30fbSAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
651a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *dmsk = dccp_msk(sk);
6526ffd30fbSAndrea Bittau 	/* figure out if we are changing our CCID or the peer's */
6536ffd30fbSAndrea Bittau 	const int rx = type == DCCPO_CHANGE_R;
654a4bf3902SArnaldo Carvalho de Melo 	const u8 ccid_nr = rx ? dmsk->dccpms_rx_ccid : dmsk->dccpms_tx_ccid;
6556ffd30fbSAndrea Bittau 	struct ccid *new_ccid;
6566ffd30fbSAndrea Bittau 
6576ffd30fbSAndrea Bittau 	/* Check if nothing is being changed. */
6586ffd30fbSAndrea Bittau 	if (ccid_nr == new_ccid_nr)
6596ffd30fbSAndrea Bittau 		return 0;
6606ffd30fbSAndrea Bittau 
6616ffd30fbSAndrea Bittau 	new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
6626ffd30fbSAndrea Bittau 	if (new_ccid == NULL)
6636ffd30fbSAndrea Bittau 		return -ENOMEM;
6646ffd30fbSAndrea Bittau 
6656ffd30fbSAndrea Bittau 	if (rx) {
6666ffd30fbSAndrea Bittau 		ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
6676ffd30fbSAndrea Bittau 		dp->dccps_hc_rx_ccid = new_ccid;
668a4bf3902SArnaldo Carvalho de Melo 		dmsk->dccpms_rx_ccid = new_ccid_nr;
6696ffd30fbSAndrea Bittau 	} else {
6706ffd30fbSAndrea Bittau 		ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
6716ffd30fbSAndrea Bittau 		dp->dccps_hc_tx_ccid = new_ccid;
672a4bf3902SArnaldo Carvalho de Melo 		dmsk->dccpms_tx_ccid = new_ccid_nr;
6736ffd30fbSAndrea Bittau 	}
6746ffd30fbSAndrea Bittau 
6756ffd30fbSAndrea Bittau 	return 0;
6766ffd30fbSAndrea Bittau }
6776ffd30fbSAndrea Bittau 
678afe00251SAndrea Bittau static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
679afe00251SAndrea Bittau {
680c02fdc0eSGerrit Renker 	dccp_feat_debug(type, feat, val);
6816ffd30fbSAndrea Bittau 
6826ffd30fbSAndrea Bittau 	switch (feat) {
6836ffd30fbSAndrea Bittau 	case DCCPF_CCID:
6846ffd30fbSAndrea Bittau 		return dccp_feat_update_ccid(sk, type, val);
6856ffd30fbSAndrea Bittau 	default:
686c02fdc0eSGerrit Renker 		dccp_pr_debug("UNIMPLEMENTED: %s(%d, ...)\n",
687c02fdc0eSGerrit Renker 			      dccp_feat_typename(type), feat);
6886ffd30fbSAndrea Bittau 		break;
6896ffd30fbSAndrea Bittau 	}
690afe00251SAndrea Bittau 	return 0;
691afe00251SAndrea Bittau }
692afe00251SAndrea Bittau 
693afe00251SAndrea Bittau static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
694afe00251SAndrea Bittau 			       u8 *rpref, u8 rlen)
695afe00251SAndrea Bittau {
696afe00251SAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
697afe00251SAndrea Bittau 	u8 *spref, slen, *res = NULL;
698afe00251SAndrea Bittau 	int i, j, rc, agree = 1;
699afe00251SAndrea Bittau 
700afe00251SAndrea Bittau 	BUG_ON(rpref == NULL);
701afe00251SAndrea Bittau 
702afe00251SAndrea Bittau 	/* check if we are the black sheep */
703afe00251SAndrea Bittau 	if (dp->dccps_role == DCCP_ROLE_CLIENT) {
704afe00251SAndrea Bittau 		spref = rpref;
705afe00251SAndrea Bittau 		slen  = rlen;
706afe00251SAndrea Bittau 		rpref = opt->dccpop_val;
707afe00251SAndrea Bittau 		rlen  = opt->dccpop_len;
708afe00251SAndrea Bittau 	} else {
709afe00251SAndrea Bittau 		spref = opt->dccpop_val;
710afe00251SAndrea Bittau 		slen  = opt->dccpop_len;
711afe00251SAndrea Bittau 	}
712afe00251SAndrea Bittau 	/*
713afe00251SAndrea Bittau 	 * Now we have server preference list in spref and client preference in
714afe00251SAndrea Bittau 	 * rpref
715afe00251SAndrea Bittau 	 */
716afe00251SAndrea Bittau 	BUG_ON(spref == NULL);
717afe00251SAndrea Bittau 	BUG_ON(rpref == NULL);
718afe00251SAndrea Bittau 
719afe00251SAndrea Bittau 	/* FIXME sanity check vals */
720afe00251SAndrea Bittau 
721afe00251SAndrea Bittau 	/* Are values in any order?  XXX Lame "algorithm" here */
722afe00251SAndrea Bittau 	for (i = 0; i < slen; i++) {
723afe00251SAndrea Bittau 		for (j = 0; j < rlen; j++) {
724afe00251SAndrea Bittau 			if (spref[i] == rpref[j]) {
725afe00251SAndrea Bittau 				res = &spref[i];
726afe00251SAndrea Bittau 				break;
727afe00251SAndrea Bittau 			}
728afe00251SAndrea Bittau 		}
729afe00251SAndrea Bittau 		if (res)
730afe00251SAndrea Bittau 			break;
731afe00251SAndrea Bittau 	}
732afe00251SAndrea Bittau 
733afe00251SAndrea Bittau 	/* we didn't agree on anything */
734afe00251SAndrea Bittau 	if (res == NULL) {
735afe00251SAndrea Bittau 		/* confirm previous value */
736afe00251SAndrea Bittau 		switch (opt->dccpop_feat) {
737afe00251SAndrea Bittau 		case DCCPF_CCID:
738afe00251SAndrea Bittau 			/* XXX did i get this right? =P */
739afe00251SAndrea Bittau 			if (opt->dccpop_type == DCCPO_CHANGE_L)
740a4bf3902SArnaldo Carvalho de Melo 				res = &dccp_msk(sk)->dccpms_tx_ccid;
741afe00251SAndrea Bittau 			else
742a4bf3902SArnaldo Carvalho de Melo 				res = &dccp_msk(sk)->dccpms_rx_ccid;
743afe00251SAndrea Bittau 			break;
744afe00251SAndrea Bittau 
745afe00251SAndrea Bittau 		default:
74659348b19SGerrit Renker 			DCCP_BUG("Fell through, feat=%d", opt->dccpop_feat);
74759348b19SGerrit Renker 			/* XXX implement res */
748afe00251SAndrea Bittau 			return -EFAULT;
749afe00251SAndrea Bittau 		}
750afe00251SAndrea Bittau 
751afe00251SAndrea Bittau 		dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
752afe00251SAndrea Bittau 		agree = 0; /* this is used for mandatory options... */
753afe00251SAndrea Bittau 	}
754afe00251SAndrea Bittau 
755afe00251SAndrea Bittau 	/* need to put result and our preference list */
756afe00251SAndrea Bittau 	rlen = 1 + opt->dccpop_len;
757afe00251SAndrea Bittau 	rpref = kmalloc(rlen, GFP_ATOMIC);
758afe00251SAndrea Bittau 	if (rpref == NULL)
759afe00251SAndrea Bittau 		return -ENOMEM;
760afe00251SAndrea Bittau 
761afe00251SAndrea Bittau 	*rpref = *res;
762afe00251SAndrea Bittau 	memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
763afe00251SAndrea Bittau 
764afe00251SAndrea Bittau 	/* put it in the "confirm queue" */
765afe00251SAndrea Bittau 	if (opt->dccpop_sc == NULL) {
766afe00251SAndrea Bittau 		opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
767afe00251SAndrea Bittau 		if (opt->dccpop_sc == NULL) {
768afe00251SAndrea Bittau 			kfree(rpref);
769afe00251SAndrea Bittau 			return -ENOMEM;
770afe00251SAndrea Bittau 		}
771afe00251SAndrea Bittau 	} else {
772afe00251SAndrea Bittau 		/* recycle the confirm slot */
773afe00251SAndrea Bittau 		BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
774afe00251SAndrea Bittau 		kfree(opt->dccpop_sc->dccpoc_val);
775afe00251SAndrea Bittau 		dccp_pr_debug("recycling confirm slot\n");
776afe00251SAndrea Bittau 	}
777afe00251SAndrea Bittau 	memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
778afe00251SAndrea Bittau 
779afe00251SAndrea Bittau 	opt->dccpop_sc->dccpoc_val = rpref;
780afe00251SAndrea Bittau 	opt->dccpop_sc->dccpoc_len = rlen;
781afe00251SAndrea Bittau 
782afe00251SAndrea Bittau 	/* update the option on our side [we are about to send the confirm] */
783afe00251SAndrea Bittau 	rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
784afe00251SAndrea Bittau 	if (rc) {
785afe00251SAndrea Bittau 		kfree(opt->dccpop_sc->dccpoc_val);
786afe00251SAndrea Bittau 		kfree(opt->dccpop_sc);
78768907dadSRandy Dunlap 		opt->dccpop_sc = NULL;
788afe00251SAndrea Bittau 		return rc;
789afe00251SAndrea Bittau 	}
790afe00251SAndrea Bittau 
791afe00251SAndrea Bittau 	dccp_pr_debug("Will confirm %d\n", *rpref);
792afe00251SAndrea Bittau 
793afe00251SAndrea Bittau 	/* say we want to change to X but we just got a confirm X, suppress our
794afe00251SAndrea Bittau 	 * change
795afe00251SAndrea Bittau 	 */
796afe00251SAndrea Bittau 	if (!opt->dccpop_conf) {
797afe00251SAndrea Bittau 		if (*opt->dccpop_val == *res)
798afe00251SAndrea Bittau 			opt->dccpop_conf = 1;
799afe00251SAndrea Bittau 		dccp_pr_debug("won't ask for change of same feature\n");
800afe00251SAndrea Bittau 	}
801afe00251SAndrea Bittau 
802afe00251SAndrea Bittau 	return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
803afe00251SAndrea Bittau }
804afe00251SAndrea Bittau 
805afe00251SAndrea Bittau static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
806afe00251SAndrea Bittau {
807a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *dmsk = dccp_msk(sk);
808afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
809afe00251SAndrea Bittau 	int rc = 1;
810afe00251SAndrea Bittau 	u8 t;
811afe00251SAndrea Bittau 
812afe00251SAndrea Bittau 	/*
813afe00251SAndrea Bittau 	 * We received a CHANGE.  We gotta match it against our own preference
814afe00251SAndrea Bittau 	 * list.  If we got a CHANGE_R it means it's a change for us, so we need
815afe00251SAndrea Bittau 	 * to compare our CHANGE_L list.
816afe00251SAndrea Bittau 	 */
817afe00251SAndrea Bittau 	if (type == DCCPO_CHANGE_L)
818afe00251SAndrea Bittau 		t = DCCPO_CHANGE_R;
819afe00251SAndrea Bittau 	else
820afe00251SAndrea Bittau 		t = DCCPO_CHANGE_L;
821afe00251SAndrea Bittau 
822afe00251SAndrea Bittau 	/* find our preference list for this feature */
823a4bf3902SArnaldo Carvalho de Melo 	list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
824afe00251SAndrea Bittau 		if (opt->dccpop_type != t || opt->dccpop_feat != feature)
825afe00251SAndrea Bittau 			continue;
826afe00251SAndrea Bittau 
827afe00251SAndrea Bittau 		/* find the winner from the two preference lists */
828afe00251SAndrea Bittau 		rc = dccp_feat_reconcile(sk, opt, val, len);
829afe00251SAndrea Bittau 		break;
830afe00251SAndrea Bittau 	}
831afe00251SAndrea Bittau 
832afe00251SAndrea Bittau 	/* We didn't deal with the change.  This can happen if we have no
833afe00251SAndrea Bittau 	 * preference list for the feature.  In fact, it just shouldn't
834afe00251SAndrea Bittau 	 * happen---if we understand a feature, we should have a preference list
835afe00251SAndrea Bittau 	 * with at least the default value.
836afe00251SAndrea Bittau 	 */
837afe00251SAndrea Bittau 	BUG_ON(rc == 1);
838afe00251SAndrea Bittau 
839afe00251SAndrea Bittau 	return rc;
840afe00251SAndrea Bittau }
841afe00251SAndrea Bittau 
842afe00251SAndrea Bittau static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
843afe00251SAndrea Bittau {
844afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
845a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *dmsk = dccp_msk(sk);
846afe00251SAndrea Bittau 	u8 *copy;
847afe00251SAndrea Bittau 	int rc;
848afe00251SAndrea Bittau 
849c02fdc0eSGerrit Renker 	/* NN features must be Change L (sec. 6.3.2) */
850c02fdc0eSGerrit Renker 	if (type != DCCPO_CHANGE_L) {
851c02fdc0eSGerrit Renker 		dccp_pr_debug("received %s for NN feature %d\n",
852c02fdc0eSGerrit Renker 				dccp_feat_typename(type), feature);
853afe00251SAndrea Bittau 		return -EFAULT;
854afe00251SAndrea Bittau 	}
855afe00251SAndrea Bittau 
856afe00251SAndrea Bittau 	/* XXX sanity check opt val */
857afe00251SAndrea Bittau 
858afe00251SAndrea Bittau 	/* copy option so we can confirm it */
859afe00251SAndrea Bittau 	opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
860afe00251SAndrea Bittau 	if (opt == NULL)
861afe00251SAndrea Bittau 		return -ENOMEM;
862afe00251SAndrea Bittau 
863eed73417SArnaldo Carvalho de Melo 	copy = kmemdup(val, len, GFP_ATOMIC);
864afe00251SAndrea Bittau 	if (copy == NULL) {
865afe00251SAndrea Bittau 		kfree(opt);
866afe00251SAndrea Bittau 		return -ENOMEM;
867afe00251SAndrea Bittau 	}
868afe00251SAndrea Bittau 
869afe00251SAndrea Bittau 	opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
870afe00251SAndrea Bittau 	opt->dccpop_feat = feature;
871afe00251SAndrea Bittau 	opt->dccpop_val	 = copy;
872afe00251SAndrea Bittau 	opt->dccpop_len	 = len;
873afe00251SAndrea Bittau 
874afe00251SAndrea Bittau 	/* change feature */
875afe00251SAndrea Bittau 	rc = dccp_feat_update(sk, type, feature, *val);
876afe00251SAndrea Bittau 	if (rc) {
877afe00251SAndrea Bittau 		kfree(opt->dccpop_val);
878afe00251SAndrea Bittau 		kfree(opt);
879afe00251SAndrea Bittau 		return rc;
880afe00251SAndrea Bittau 	}
881afe00251SAndrea Bittau 
882c02fdc0eSGerrit Renker 	dccp_feat_debug(type, feature, *copy);
883c02fdc0eSGerrit Renker 
884a4bf3902SArnaldo Carvalho de Melo 	list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
885afe00251SAndrea Bittau 
886afe00251SAndrea Bittau 	return 0;
887afe00251SAndrea Bittau }
888afe00251SAndrea Bittau 
8898ca0d17bSArnaldo Carvalho de Melo static void dccp_feat_empty_confirm(struct dccp_minisock *dmsk,
8908ca0d17bSArnaldo Carvalho de Melo 				    u8 type, u8 feature)
891afe00251SAndrea Bittau {
892afe00251SAndrea Bittau 	/* XXX check if other confirms for that are queued and recycle slot */
893afe00251SAndrea Bittau 	struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
894afe00251SAndrea Bittau 
895afe00251SAndrea Bittau 	if (opt == NULL) {
896afe00251SAndrea Bittau 		/* XXX what do we do?  Ignoring should be fine.  It's a change
897afe00251SAndrea Bittau 		 * after all =P
898afe00251SAndrea Bittau 		 */
899afe00251SAndrea Bittau 		return;
900afe00251SAndrea Bittau 	}
901afe00251SAndrea Bittau 
902c02fdc0eSGerrit Renker 	switch (type) {
903e576de82SJesper Juhl 	case DCCPO_CHANGE_L:
904e576de82SJesper Juhl 		opt->dccpop_type = DCCPO_CONFIRM_R;
905e576de82SJesper Juhl 		break;
906e576de82SJesper Juhl 	case DCCPO_CHANGE_R:
907e576de82SJesper Juhl 		opt->dccpop_type = DCCPO_CONFIRM_L;
908e576de82SJesper Juhl 		break;
909e576de82SJesper Juhl 	default:
910e576de82SJesper Juhl 		DCCP_WARN("invalid type %d\n", type);
911e576de82SJesper Juhl 		kfree(opt);
912e576de82SJesper Juhl 		return;
913c02fdc0eSGerrit Renker 	}
914afe00251SAndrea Bittau 	opt->dccpop_feat = feature;
91568907dadSRandy Dunlap 	opt->dccpop_val	 = NULL;
916afe00251SAndrea Bittau 	opt->dccpop_len	 = 0;
917afe00251SAndrea Bittau 
918afe00251SAndrea Bittau 	/* change feature */
919c02fdc0eSGerrit Renker 	dccp_pr_debug("Empty %s(%d)\n", dccp_feat_typename(type), feature);
920c02fdc0eSGerrit Renker 
921a4bf3902SArnaldo Carvalho de Melo 	list_add_tail(&opt->dccpop_node, &dmsk->dccpms_conf);
922afe00251SAndrea Bittau }
923afe00251SAndrea Bittau 
924afe00251SAndrea Bittau static void dccp_feat_flush_confirm(struct sock *sk)
925afe00251SAndrea Bittau {
926a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *dmsk = dccp_msk(sk);
927afe00251SAndrea Bittau 	/* Check if there is anything to confirm in the first place */
928a4bf3902SArnaldo Carvalho de Melo 	int yes = !list_empty(&dmsk->dccpms_conf);
929afe00251SAndrea Bittau 
930afe00251SAndrea Bittau 	if (!yes) {
931afe00251SAndrea Bittau 		struct dccp_opt_pend *opt;
932afe00251SAndrea Bittau 
933a4bf3902SArnaldo Carvalho de Melo 		list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
934afe00251SAndrea Bittau 			if (opt->dccpop_conf) {
935afe00251SAndrea Bittau 				yes = 1;
936afe00251SAndrea Bittau 				break;
937afe00251SAndrea Bittau 			}
938afe00251SAndrea Bittau 		}
939afe00251SAndrea Bittau 	}
940afe00251SAndrea Bittau 
941afe00251SAndrea Bittau 	if (!yes)
942afe00251SAndrea Bittau 		return;
943afe00251SAndrea Bittau 
944afe00251SAndrea Bittau 	/* OK there is something to confirm... */
945afe00251SAndrea Bittau 	/* XXX check if packet is in flight?  Send delayed ack?? */
946afe00251SAndrea Bittau 	if (sk->sk_state == DCCP_OPEN)
947afe00251SAndrea Bittau 		dccp_send_ack(sk);
948afe00251SAndrea Bittau }
949afe00251SAndrea Bittau 
950afe00251SAndrea Bittau int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
951afe00251SAndrea Bittau {
952afe00251SAndrea Bittau 	int rc;
953afe00251SAndrea Bittau 
954f74e91b6SGerrit Renker 	/* Ignore Change requests other than during connection setup */
955f74e91b6SGerrit Renker 	if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
956f74e91b6SGerrit Renker 		return 0;
957c02fdc0eSGerrit Renker 	dccp_feat_debug(type, feature, *val);
958afe00251SAndrea Bittau 
959afe00251SAndrea Bittau 	/* figure out if it's SP or NN feature */
960afe00251SAndrea Bittau 	switch (feature) {
961afe00251SAndrea Bittau 	/* deal with SP features */
962afe00251SAndrea Bittau 	case DCCPF_CCID:
963afe00251SAndrea Bittau 		rc = dccp_feat_sp(sk, type, feature, val, len);
964afe00251SAndrea Bittau 		break;
965afe00251SAndrea Bittau 
966afe00251SAndrea Bittau 	/* deal with NN features */
967afe00251SAndrea Bittau 	case DCCPF_ACK_RATIO:
968afe00251SAndrea Bittau 		rc = dccp_feat_nn(sk, type, feature, val, len);
969afe00251SAndrea Bittau 		break;
970afe00251SAndrea Bittau 
971afe00251SAndrea Bittau 	/* XXX implement other features */
972afe00251SAndrea Bittau 	default:
973c02fdc0eSGerrit Renker 		dccp_pr_debug("UNIMPLEMENTED: not handling %s(%d, ...)\n",
974c02fdc0eSGerrit Renker 			      dccp_feat_typename(type), feature);
975afe00251SAndrea Bittau 		rc = -EFAULT;
976afe00251SAndrea Bittau 		break;
977afe00251SAndrea Bittau 	}
978afe00251SAndrea Bittau 
979afe00251SAndrea Bittau 	/* check if there were problems changing features */
980afe00251SAndrea Bittau 	if (rc) {
981afe00251SAndrea Bittau 		/* If we don't agree on SP, we sent a confirm for old value.
982afe00251SAndrea Bittau 		 * However we propagate rc to caller in case option was
983afe00251SAndrea Bittau 		 * mandatory
984afe00251SAndrea Bittau 		 */
985afe00251SAndrea Bittau 		if (rc != DCCP_FEAT_SP_NOAGREE)
9868ca0d17bSArnaldo Carvalho de Melo 			dccp_feat_empty_confirm(dccp_msk(sk), type, feature);
987afe00251SAndrea Bittau 	}
988afe00251SAndrea Bittau 
989afe00251SAndrea Bittau 	/* generate the confirm [if required] */
990afe00251SAndrea Bittau 	dccp_feat_flush_confirm(sk);
991afe00251SAndrea Bittau 
992afe00251SAndrea Bittau 	return rc;
993afe00251SAndrea Bittau }
994afe00251SAndrea Bittau 
995afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
996afe00251SAndrea Bittau 
997afe00251SAndrea Bittau int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
998afe00251SAndrea Bittau 			   u8 *val, u8 len)
999afe00251SAndrea Bittau {
1000afe00251SAndrea Bittau 	u8 t;
1001afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
1002a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *dmsk = dccp_msk(sk);
1003c02fdc0eSGerrit Renker 	int found = 0;
1004afe00251SAndrea Bittau 	int all_confirmed = 1;
1005afe00251SAndrea Bittau 
1006f74e91b6SGerrit Renker 	/* Ignore Confirm options other than during connection setup */
1007f74e91b6SGerrit Renker 	if (sk->sk_state != DCCP_LISTEN && sk->sk_state != DCCP_REQUESTING)
1008f74e91b6SGerrit Renker 		return 0;
1009c02fdc0eSGerrit Renker 	dccp_feat_debug(type, feature, *val);
1010afe00251SAndrea Bittau 
1011afe00251SAndrea Bittau 	/* locate our change request */
1012c02fdc0eSGerrit Renker 	switch (type) {
1013c02fdc0eSGerrit Renker 	case DCCPO_CONFIRM_L: t = DCCPO_CHANGE_R; break;
1014c02fdc0eSGerrit Renker 	case DCCPO_CONFIRM_R: t = DCCPO_CHANGE_L; break;
101559348b19SGerrit Renker 	default:	      DCCP_WARN("invalid type %d\n", type);
1016c02fdc0eSGerrit Renker 			      return 1;
1017c02fdc0eSGerrit Renker 
1018c02fdc0eSGerrit Renker 	}
1019c02fdc0eSGerrit Renker 	/* XXX sanity check feature value */
1020afe00251SAndrea Bittau 
1021a4bf3902SArnaldo Carvalho de Melo 	list_for_each_entry(opt, &dmsk->dccpms_pending, dccpop_node) {
1022afe00251SAndrea Bittau 		if (!opt->dccpop_conf && opt->dccpop_type == t &&
1023afe00251SAndrea Bittau 		    opt->dccpop_feat == feature) {
1024c02fdc0eSGerrit Renker 			found = 1;
1025c02fdc0eSGerrit Renker 			dccp_pr_debug("feature %d found\n", opt->dccpop_feat);
1026c02fdc0eSGerrit Renker 
1027afe00251SAndrea Bittau 			/* XXX do sanity check */
1028afe00251SAndrea Bittau 
1029afe00251SAndrea Bittau 			opt->dccpop_conf = 1;
1030afe00251SAndrea Bittau 
1031afe00251SAndrea Bittau 			/* We got a confirmation---change the option */
1032afe00251SAndrea Bittau 			dccp_feat_update(sk, opt->dccpop_type,
1033afe00251SAndrea Bittau 					 opt->dccpop_feat, *val);
1034afe00251SAndrea Bittau 
1035c02fdc0eSGerrit Renker 			/* XXX check the return value of dccp_feat_update */
1036afe00251SAndrea Bittau 			break;
1037afe00251SAndrea Bittau 		}
1038afe00251SAndrea Bittau 
1039afe00251SAndrea Bittau 		if (!opt->dccpop_conf)
1040afe00251SAndrea Bittau 			all_confirmed = 0;
1041afe00251SAndrea Bittau 	}
1042afe00251SAndrea Bittau 
1043c02fdc0eSGerrit Renker 	if (!found)
1044c02fdc0eSGerrit Renker 		dccp_pr_debug("%s(%d, ...) never requested\n",
1045c02fdc0eSGerrit Renker 			      dccp_feat_typename(type), feature);
1046afe00251SAndrea Bittau 	return 0;
1047afe00251SAndrea Bittau }
1048afe00251SAndrea Bittau 
1049afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
1050afe00251SAndrea Bittau 
10518ca0d17bSArnaldo Carvalho de Melo void dccp_feat_clean(struct dccp_minisock *dmsk)
1052afe00251SAndrea Bittau {
1053afe00251SAndrea Bittau 	struct dccp_opt_pend *opt, *next;
1054afe00251SAndrea Bittau 
1055a4bf3902SArnaldo Carvalho de Melo 	list_for_each_entry_safe(opt, next, &dmsk->dccpms_pending,
1056afe00251SAndrea Bittau 				 dccpop_node) {
1057afe00251SAndrea Bittau 		BUG_ON(opt->dccpop_val == NULL);
1058afe00251SAndrea Bittau 		kfree(opt->dccpop_val);
1059afe00251SAndrea Bittau 
1060afe00251SAndrea Bittau 		if (opt->dccpop_sc != NULL) {
1061afe00251SAndrea Bittau 			BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
1062afe00251SAndrea Bittau 			kfree(opt->dccpop_sc->dccpoc_val);
1063afe00251SAndrea Bittau 			kfree(opt->dccpop_sc);
1064afe00251SAndrea Bittau 		}
1065afe00251SAndrea Bittau 
1066afe00251SAndrea Bittau 		kfree(opt);
1067afe00251SAndrea Bittau 	}
1068a4bf3902SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&dmsk->dccpms_pending);
1069afe00251SAndrea Bittau 
1070a4bf3902SArnaldo Carvalho de Melo 	list_for_each_entry_safe(opt, next, &dmsk->dccpms_conf, dccpop_node) {
1071afe00251SAndrea Bittau 		BUG_ON(opt == NULL);
1072afe00251SAndrea Bittau 		if (opt->dccpop_val != NULL)
1073afe00251SAndrea Bittau 			kfree(opt->dccpop_val);
1074afe00251SAndrea Bittau 		kfree(opt);
1075afe00251SAndrea Bittau 	}
1076a4bf3902SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&dmsk->dccpms_conf);
1077afe00251SAndrea Bittau }
1078afe00251SAndrea Bittau 
1079afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_clean);
1080afe00251SAndrea Bittau 
1081afe00251SAndrea Bittau /* this is to be called only when a listening sock creates its child.  It is
1082afe00251SAndrea Bittau  * assumed by the function---the confirm is not duplicated, but rather it is
1083afe00251SAndrea Bittau  * "passed on".
1084afe00251SAndrea Bittau  */
1085afe00251SAndrea Bittau int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
1086afe00251SAndrea Bittau {
1087a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *olddmsk = dccp_msk(oldsk);
1088a4bf3902SArnaldo Carvalho de Melo 	struct dccp_minisock *newdmsk = dccp_msk(newsk);
1089afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
1090afe00251SAndrea Bittau 	int rc = 0;
1091afe00251SAndrea Bittau 
1092a4bf3902SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&newdmsk->dccpms_pending);
1093a4bf3902SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&newdmsk->dccpms_conf);
1094afe00251SAndrea Bittau 
1095a4bf3902SArnaldo Carvalho de Melo 	list_for_each_entry(opt, &olddmsk->dccpms_pending, dccpop_node) {
1096afe00251SAndrea Bittau 		struct dccp_opt_pend *newopt;
1097afe00251SAndrea Bittau 		/* copy the value of the option */
1098eed73417SArnaldo Carvalho de Melo 		u8 *val = kmemdup(opt->dccpop_val, opt->dccpop_len, GFP_ATOMIC);
1099afe00251SAndrea Bittau 
1100afe00251SAndrea Bittau 		if (val == NULL)
1101afe00251SAndrea Bittau 			goto out_clean;
1102afe00251SAndrea Bittau 
1103eed73417SArnaldo Carvalho de Melo 		newopt = kmemdup(opt, sizeof(*newopt), GFP_ATOMIC);
1104afe00251SAndrea Bittau 		if (newopt == NULL) {
1105afe00251SAndrea Bittau 			kfree(val);
1106afe00251SAndrea Bittau 			goto out_clean;
1107afe00251SAndrea Bittau 		}
1108afe00251SAndrea Bittau 
1109afe00251SAndrea Bittau 		/* insert the option */
1110afe00251SAndrea Bittau 		newopt->dccpop_val = val;
1111a4bf3902SArnaldo Carvalho de Melo 		list_add_tail(&newopt->dccpop_node, &newdmsk->dccpms_pending);
1112afe00251SAndrea Bittau 
1113afe00251SAndrea Bittau 		/* XXX what happens with backlogs and multiple connections at
1114afe00251SAndrea Bittau 		 * once...
1115afe00251SAndrea Bittau 		 */
1116afe00251SAndrea Bittau 		/* the master socket no longer needs to worry about confirms */
111768907dadSRandy Dunlap 		opt->dccpop_sc = NULL; /* it's not a memleak---new socket has it */
1118afe00251SAndrea Bittau 
1119afe00251SAndrea Bittau 		/* reset state for a new socket */
1120afe00251SAndrea Bittau 		opt->dccpop_conf = 0;
1121afe00251SAndrea Bittau 	}
1122afe00251SAndrea Bittau 
1123afe00251SAndrea Bittau 	/* XXX not doing anything about the conf queue */
1124afe00251SAndrea Bittau 
1125afe00251SAndrea Bittau out:
1126afe00251SAndrea Bittau 	return rc;
1127afe00251SAndrea Bittau 
1128afe00251SAndrea Bittau out_clean:
11298ca0d17bSArnaldo Carvalho de Melo 	dccp_feat_clean(newdmsk);
1130afe00251SAndrea Bittau 	rc = -ENOMEM;
1131afe00251SAndrea Bittau 	goto out;
1132afe00251SAndrea Bittau }
1133afe00251SAndrea Bittau 
1134afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_clone);
1135afe00251SAndrea Bittau 
1136e8ef967aSGerrit Renker int dccp_feat_init(struct sock *sk)
1137afe00251SAndrea Bittau {
1138e8ef967aSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
1139e8ef967aSGerrit Renker 	struct dccp_minisock *dmsk = dccp_msk(sk);
1140afe00251SAndrea Bittau 	int rc;
1141afe00251SAndrea Bittau 
1142e8ef967aSGerrit Renker 	INIT_LIST_HEAD(&dmsk->dccpms_pending);	/* XXX no longer used */
1143e8ef967aSGerrit Renker 	INIT_LIST_HEAD(&dmsk->dccpms_conf);	/* XXX no longer used */
1144afe00251SAndrea Bittau 
1145afe00251SAndrea Bittau 	/* CCID L */
1146e8ef967aSGerrit Renker 	rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 1, 0,
1147a4bf3902SArnaldo Carvalho de Melo 				&dmsk->dccpms_tx_ccid, 1);
1148afe00251SAndrea Bittau 	if (rc)
1149afe00251SAndrea Bittau 		goto out;
1150afe00251SAndrea Bittau 
1151afe00251SAndrea Bittau 	/* CCID R */
1152e8ef967aSGerrit Renker 	rc = __feat_register_sp(&dp->dccps_featneg, DCCPF_CCID, 0, 0,
1153a4bf3902SArnaldo Carvalho de Melo 				&dmsk->dccpms_rx_ccid, 1);
1154afe00251SAndrea Bittau 	if (rc)
1155afe00251SAndrea Bittau 		goto out;
1156afe00251SAndrea Bittau 
1157afe00251SAndrea Bittau 	/* Ack ratio */
1158e8ef967aSGerrit Renker 	rc = __feat_register_nn(&dp->dccps_featneg, DCCPF_ACK_RATIO, 0,
115949aebc66SGerrit Renker 				dp->dccps_l_ack_ratio);
1160afe00251SAndrea Bittau out:
1161afe00251SAndrea Bittau 	return rc;
1162afe00251SAndrea Bittau }
1163afe00251SAndrea Bittau 
1164afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_init);
1165c02fdc0eSGerrit Renker 
1166c02fdc0eSGerrit Renker #ifdef CONFIG_IP_DCCP_DEBUG
1167c02fdc0eSGerrit Renker const char *dccp_feat_typename(const u8 type)
1168c02fdc0eSGerrit Renker {
1169c02fdc0eSGerrit Renker 	switch(type) {
1170c02fdc0eSGerrit Renker 	case DCCPO_CHANGE_L:  return("ChangeL");
1171c02fdc0eSGerrit Renker 	case DCCPO_CONFIRM_L: return("ConfirmL");
1172c02fdc0eSGerrit Renker 	case DCCPO_CHANGE_R:  return("ChangeR");
1173c02fdc0eSGerrit Renker 	case DCCPO_CONFIRM_R: return("ConfirmR");
1174c02fdc0eSGerrit Renker 	/* the following case must not appear in feature negotation  */
1175c02fdc0eSGerrit Renker 	default:	      dccp_pr_debug("unknown type %d [BUG!]\n", type);
1176c02fdc0eSGerrit Renker 	}
1177c02fdc0eSGerrit Renker 	return NULL;
1178c02fdc0eSGerrit Renker }
1179c02fdc0eSGerrit Renker 
1180c02fdc0eSGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_typename);
1181c02fdc0eSGerrit Renker 
1182c02fdc0eSGerrit Renker const char *dccp_feat_name(const u8 feat)
1183c02fdc0eSGerrit Renker {
1184c02fdc0eSGerrit Renker 	static const char *feature_names[] = {
1185c02fdc0eSGerrit Renker 		[DCCPF_RESERVED]	= "Reserved",
1186c02fdc0eSGerrit Renker 		[DCCPF_CCID]		= "CCID",
1187c02fdc0eSGerrit Renker 		[DCCPF_SHORT_SEQNOS]	= "Allow Short Seqnos",
1188c02fdc0eSGerrit Renker 		[DCCPF_SEQUENCE_WINDOW]	= "Sequence Window",
1189c02fdc0eSGerrit Renker 		[DCCPF_ECN_INCAPABLE]	= "ECN Incapable",
1190c02fdc0eSGerrit Renker 		[DCCPF_ACK_RATIO]	= "Ack Ratio",
1191c02fdc0eSGerrit Renker 		[DCCPF_SEND_ACK_VECTOR]	= "Send ACK Vector",
1192c02fdc0eSGerrit Renker 		[DCCPF_SEND_NDP_COUNT]	= "Send NDP Count",
1193c02fdc0eSGerrit Renker 		[DCCPF_MIN_CSUM_COVER]	= "Min. Csum Coverage",
1194c02fdc0eSGerrit Renker 		[DCCPF_DATA_CHECKSUM]	= "Send Data Checksum",
1195c02fdc0eSGerrit Renker 	};
1196dd6303dfSGerrit Renker 	if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
1197dd6303dfSGerrit Renker 		return feature_names[DCCPF_RESERVED];
1198dd6303dfSGerrit Renker 
11997d43d1a0SGerrit Renker 	if (feat ==  DCCPF_SEND_LEV_RATE)
12007d43d1a0SGerrit Renker 		return "Send Loss Event Rate";
1201c02fdc0eSGerrit Renker 	if (feat >= DCCPF_MIN_CCID_SPECIFIC)
1202c02fdc0eSGerrit Renker 		return "CCID-specific";
1203c02fdc0eSGerrit Renker 
1204c02fdc0eSGerrit Renker 	return feature_names[feat];
1205c02fdc0eSGerrit Renker }
1206c02fdc0eSGerrit Renker 
1207c02fdc0eSGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_name);
1208c02fdc0eSGerrit Renker #endif /* CONFIG_IP_DCCP_DEBUG */
1209