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