xref: /openbmc/linux/net/dccp/feat.c (revision 3db1cd5c05f35fb43eb134df6f321de4e63141f2)
1afe00251SAndrea Bittau /*
2afe00251SAndrea Bittau  *  net/dccp/feat.c
3afe00251SAndrea Bittau  *
463b8e286SGerrit Renker  *  Feature negotiation for the DCCP protocol (RFC 4340, section 6)
563b8e286SGerrit Renker  *
663b8e286SGerrit Renker  *  Copyright (c) 2008 Gerrit Renker <gerrit@erg.abdn.ac.uk>
763b8e286SGerrit Renker  *  Rewrote from scratch, some bits from earlier code by
863b8e286SGerrit Renker  *  Copyright (c) 2005 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
963b8e286SGerrit Renker  *
10afe00251SAndrea Bittau  *
115cdae198SGerrit Renker  *  ASSUMPTIONS
125cdae198SGerrit Renker  *  -----------
13f74e91b6SGerrit Renker  *  o Feature negotiation is coordinated with connection setup (as in TCP), wild
14f74e91b6SGerrit Renker  *    changes of parameters of an established connection are not supported.
15d6916f87SGerrit Renker  *  o Changing non-negotiable (NN) values is supported in state OPEN/PARTOPEN.
165cdae198SGerrit Renker  *  o All currently known SP features have 1-byte quantities. If in the future
175cdae198SGerrit Renker  *    extensions of RFCs 4340..42 define features with item lengths larger than
185cdae198SGerrit Renker  *    one byte, a feature-specific extension of the code will be required.
195cdae198SGerrit Renker  *
20afe00251SAndrea Bittau  *  This program is free software; you can redistribute it and/or
21afe00251SAndrea Bittau  *  modify it under the terms of the GNU General Public License
22afe00251SAndrea Bittau  *  as published by the Free Software Foundation; either version
23afe00251SAndrea Bittau  *  2 of the License, or (at your option) any later version.
24afe00251SAndrea Bittau  */
25afe00251SAndrea Bittau #include <linux/module.h>
265a0e3ad6STejun Heo #include <linux/slab.h>
276ffd30fbSAndrea Bittau #include "ccid.h"
28afe00251SAndrea Bittau #include "feat.h"
29afe00251SAndrea Bittau 
30883ca833SGerrit Renker /* feature-specific sysctls - initialised to the defaults from RFC 4340, 6.4 */
31883ca833SGerrit Renker unsigned long	sysctl_dccp_sequence_window __read_mostly = 100;
32883ca833SGerrit Renker int		sysctl_dccp_rx_ccid	    __read_mostly = 2,
33883ca833SGerrit Renker 		sysctl_dccp_tx_ccid	    __read_mostly = 2;
34883ca833SGerrit Renker 
35422d9cdcSGerrit Renker /*
36422d9cdcSGerrit Renker  * Feature activation handlers.
37422d9cdcSGerrit Renker  *
38422d9cdcSGerrit Renker  * These all use an u64 argument, to provide enough room for NN/SP features. At
39422d9cdcSGerrit Renker  * this stage the negotiated values have been checked to be within their range.
40422d9cdcSGerrit Renker  */
41422d9cdcSGerrit Renker static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
42422d9cdcSGerrit Renker {
43422d9cdcSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
44e5fd56caSGerrit Renker 	struct ccid *new_ccid = ccid_new(ccid, sk, rx);
45422d9cdcSGerrit Renker 
46422d9cdcSGerrit Renker 	if (new_ccid == NULL)
47422d9cdcSGerrit Renker 		return -ENOMEM;
48422d9cdcSGerrit Renker 
49422d9cdcSGerrit Renker 	if (rx) {
50422d9cdcSGerrit Renker 		ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
51422d9cdcSGerrit Renker 		dp->dccps_hc_rx_ccid = new_ccid;
52422d9cdcSGerrit Renker 	} else {
53422d9cdcSGerrit Renker 		ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
54422d9cdcSGerrit Renker 		dp->dccps_hc_tx_ccid = new_ccid;
55422d9cdcSGerrit Renker 	}
56422d9cdcSGerrit Renker 	return 0;
57422d9cdcSGerrit Renker }
58422d9cdcSGerrit Renker 
59422d9cdcSGerrit Renker static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
60422d9cdcSGerrit Renker {
61792b4878SGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
62792b4878SGerrit Renker 
63792b4878SGerrit Renker 	if (rx) {
64792b4878SGerrit Renker 		dp->dccps_r_seq_win = seq_win;
65792b4878SGerrit Renker 		/* propagate changes to update SWL/SWH */
66792b4878SGerrit Renker 		dccp_update_gsr(sk, dp->dccps_gsr);
67792b4878SGerrit Renker 	} else {
68792b4878SGerrit Renker 		dp->dccps_l_seq_win = seq_win;
69792b4878SGerrit Renker 		/* propagate changes to update AWL */
70792b4878SGerrit Renker 		dccp_update_gss(sk, dp->dccps_gss);
71792b4878SGerrit Renker 	}
72422d9cdcSGerrit Renker 	return 0;
73422d9cdcSGerrit Renker }
74422d9cdcSGerrit Renker 
75422d9cdcSGerrit Renker static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
76422d9cdcSGerrit Renker {
77422d9cdcSGerrit Renker 	if (rx)
78422d9cdcSGerrit Renker 		dccp_sk(sk)->dccps_r_ack_ratio = ratio;
79422d9cdcSGerrit Renker 	else
80422d9cdcSGerrit Renker 		dccp_sk(sk)->dccps_l_ack_ratio = ratio;
81422d9cdcSGerrit Renker 	return 0;
82422d9cdcSGerrit Renker }
83422d9cdcSGerrit Renker 
84422d9cdcSGerrit Renker static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
85422d9cdcSGerrit Renker {
86422d9cdcSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
87422d9cdcSGerrit Renker 
88422d9cdcSGerrit Renker 	if (rx) {
89422d9cdcSGerrit Renker 		if (enable && dp->dccps_hc_rx_ackvec == NULL) {
90422d9cdcSGerrit Renker 			dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
91422d9cdcSGerrit Renker 			if (dp->dccps_hc_rx_ackvec == NULL)
92422d9cdcSGerrit Renker 				return -ENOMEM;
93422d9cdcSGerrit Renker 		} else if (!enable) {
94422d9cdcSGerrit Renker 			dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
95422d9cdcSGerrit Renker 			dp->dccps_hc_rx_ackvec = NULL;
96422d9cdcSGerrit Renker 		}
97422d9cdcSGerrit Renker 	}
98422d9cdcSGerrit Renker 	return 0;
99422d9cdcSGerrit Renker }
100422d9cdcSGerrit Renker 
101422d9cdcSGerrit Renker static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
102422d9cdcSGerrit Renker {
103422d9cdcSGerrit Renker 	if (!rx)
1044098dce5SGerrit Renker 		dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
105422d9cdcSGerrit Renker 	return 0;
106422d9cdcSGerrit Renker }
107422d9cdcSGerrit Renker 
108422d9cdcSGerrit Renker /*
109422d9cdcSGerrit Renker  * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
110422d9cdcSGerrit Renker  * `rx' holds when the sending peer informs about his partial coverage via a
111422d9cdcSGerrit Renker  * ChangeR() option. In the other case, we are the sender and the receiver
112422d9cdcSGerrit Renker  * announces its coverage via ChangeL() options. The policy here is to honour
113422d9cdcSGerrit Renker  * such communication by enabling the corresponding partial coverage - but only
114422d9cdcSGerrit Renker  * if it has not been set manually before; the warning here means that all
115422d9cdcSGerrit Renker  * packets will be dropped.
116422d9cdcSGerrit Renker  */
117422d9cdcSGerrit Renker static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
118422d9cdcSGerrit Renker {
119422d9cdcSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
120422d9cdcSGerrit Renker 
121422d9cdcSGerrit Renker 	if (rx)
122422d9cdcSGerrit Renker 		dp->dccps_pcrlen = cscov;
123422d9cdcSGerrit Renker 	else {
124422d9cdcSGerrit Renker 		if (dp->dccps_pcslen == 0)
125422d9cdcSGerrit Renker 			dp->dccps_pcslen = cscov;
126422d9cdcSGerrit Renker 		else if (cscov > dp->dccps_pcslen)
127422d9cdcSGerrit Renker 			DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
128422d9cdcSGerrit Renker 				  dp->dccps_pcslen, (u8)cscov);
129422d9cdcSGerrit Renker 	}
130422d9cdcSGerrit Renker 	return 0;
131422d9cdcSGerrit Renker }
132422d9cdcSGerrit Renker 
1337d43d1a0SGerrit Renker static const struct {
1347d43d1a0SGerrit Renker 	u8			feat_num;		/* DCCPF_xxx */
1357d43d1a0SGerrit Renker 	enum dccp_feat_type	rxtx;			/* RX or TX  */
1367d43d1a0SGerrit Renker 	enum dccp_feat_type	reconciliation;		/* SP or NN  */
1377d43d1a0SGerrit Renker 	u8			default_value;		/* as in 6.4 */
138422d9cdcSGerrit Renker 	int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
1397d43d1a0SGerrit Renker /*
1407d43d1a0SGerrit Renker  *    Lookup table for location and type of features (from RFC 4340/4342)
1417d43d1a0SGerrit Renker  *  +--------------------------+----+-----+----+----+---------+-----------+
1427d43d1a0SGerrit Renker  *  | Feature                  | Location | Reconc. | Initial |  Section  |
1437d43d1a0SGerrit Renker  *  |                          | RX | TX  | SP | NN |  Value  | Reference |
1447d43d1a0SGerrit Renker  *  +--------------------------+----+-----+----+----+---------+-----------+
1457d43d1a0SGerrit Renker  *  | DCCPF_CCID               |    |  X  | X  |    |   2     | 10        |
1467d43d1a0SGerrit Renker  *  | DCCPF_SHORT_SEQNOS       |    |  X  | X  |    |   0     |  7.6.1    |
1477d43d1a0SGerrit Renker  *  | DCCPF_SEQUENCE_WINDOW    |    |  X  |    | X  | 100     |  7.5.2    |
1487d43d1a0SGerrit Renker  *  | DCCPF_ECN_INCAPABLE      | X  |     | X  |    |   0     | 12.1      |
1497d43d1a0SGerrit Renker  *  | DCCPF_ACK_RATIO          |    |  X  |    | X  |   2     | 11.3      |
1507d43d1a0SGerrit Renker  *  | DCCPF_SEND_ACK_VECTOR    | X  |     | X  |    |   0     | 11.5      |
1517d43d1a0SGerrit Renker  *  | DCCPF_SEND_NDP_COUNT     |    |  X  | X  |    |   0     |  7.7.2    |
1527d43d1a0SGerrit Renker  *  | DCCPF_MIN_CSUM_COVER     | X  |     | X  |    |   0     |  9.2.1    |
1537d43d1a0SGerrit Renker  *  | DCCPF_DATA_CHECKSUM      | X  |     | X  |    |   0     |  9.3.1    |
1547d43d1a0SGerrit Renker  *  | DCCPF_SEND_LEV_RATE      | X  |     | X  |    |   0     | 4342/8.4  |
1557d43d1a0SGerrit Renker  *  +--------------------------+----+-----+----+----+---------+-----------+
1567d43d1a0SGerrit Renker  */
1577d43d1a0SGerrit Renker } dccp_feat_table[] = {
158422d9cdcSGerrit Renker 	{ DCCPF_CCID,		 FEAT_AT_TX, FEAT_SP, 2,   dccp_hdlr_ccid     },
159422d9cdcSGerrit Renker 	{ DCCPF_SHORT_SEQNOS,	 FEAT_AT_TX, FEAT_SP, 0,   NULL },
160422d9cdcSGerrit Renker 	{ DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win  },
161422d9cdcSGerrit Renker 	{ DCCPF_ECN_INCAPABLE,	 FEAT_AT_RX, FEAT_SP, 0,   NULL },
162422d9cdcSGerrit Renker 	{ DCCPF_ACK_RATIO,	 FEAT_AT_TX, FEAT_NN, 2,   dccp_hdlr_ack_ratio},
163422d9cdcSGerrit Renker 	{ DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0,   dccp_hdlr_ackvec   },
164422d9cdcSGerrit Renker 	{ DCCPF_SEND_NDP_COUNT,  FEAT_AT_TX, FEAT_SP, 0,   dccp_hdlr_ndp      },
165422d9cdcSGerrit Renker 	{ DCCPF_MIN_CSUM_COVER,  FEAT_AT_RX, FEAT_SP, 0,   dccp_hdlr_min_cscov},
166422d9cdcSGerrit Renker 	{ DCCPF_DATA_CHECKSUM,	 FEAT_AT_RX, FEAT_SP, 0,   NULL },
167422d9cdcSGerrit Renker 	{ DCCPF_SEND_LEV_RATE,	 FEAT_AT_RX, FEAT_SP, 0,   NULL },
1687d43d1a0SGerrit Renker };
1697d43d1a0SGerrit Renker #define DCCP_FEAT_SUPPORTED_MAX		ARRAY_SIZE(dccp_feat_table)
1707d43d1a0SGerrit Renker 
17161e6473eSGerrit Renker /**
17261e6473eSGerrit Renker  * dccp_feat_index  -  Hash function to map feature number into array position
17361e6473eSGerrit Renker  * Returns consecutive array index or -1 if the feature is not understood.
17461e6473eSGerrit Renker  */
17561e6473eSGerrit Renker static int dccp_feat_index(u8 feat_num)
17661e6473eSGerrit Renker {
17761e6473eSGerrit Renker 	/* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
17861e6473eSGerrit Renker 	if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
17961e6473eSGerrit Renker 		return feat_num - 1;
18061e6473eSGerrit Renker 
18161e6473eSGerrit Renker 	/*
18261e6473eSGerrit Renker 	 * Other features: add cases for new feature types here after adding
18361e6473eSGerrit Renker 	 * them to the above table.
18461e6473eSGerrit Renker 	 */
18561e6473eSGerrit Renker 	switch (feat_num) {
18661e6473eSGerrit Renker 	case DCCPF_SEND_LEV_RATE:
18761e6473eSGerrit Renker 			return DCCP_FEAT_SUPPORTED_MAX - 1;
18861e6473eSGerrit Renker 	}
18961e6473eSGerrit Renker 	return -1;
19061e6473eSGerrit Renker }
19161e6473eSGerrit Renker 
19261e6473eSGerrit Renker static u8 dccp_feat_type(u8 feat_num)
19361e6473eSGerrit Renker {
19461e6473eSGerrit Renker 	int idx = dccp_feat_index(feat_num);
19561e6473eSGerrit Renker 
19661e6473eSGerrit Renker 	if (idx < 0)
19761e6473eSGerrit Renker 		return FEAT_UNKNOWN;
19861e6473eSGerrit Renker 	return dccp_feat_table[idx].reconciliation;
19961e6473eSGerrit Renker }
20061e6473eSGerrit Renker 
201e8ef967aSGerrit Renker static int dccp_feat_default_value(u8 feat_num)
202e8ef967aSGerrit Renker {
203e8ef967aSGerrit Renker 	int idx = dccp_feat_index(feat_num);
204e8ef967aSGerrit Renker 	/*
205e8ef967aSGerrit Renker 	 * There are no default values for unknown features, so encountering a
206e8ef967aSGerrit Renker 	 * negative index here indicates a serious problem somewhere else.
207e8ef967aSGerrit Renker 	 */
208e8ef967aSGerrit Renker 	DCCP_BUG_ON(idx < 0);
209e8ef967aSGerrit Renker 
210e8ef967aSGerrit Renker 	return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
211e8ef967aSGerrit Renker }
212e8ef967aSGerrit Renker 
213f3f3abb6SGerrit Renker /*
214f3f3abb6SGerrit Renker  *	Debugging and verbose-printing section
215f3f3abb6SGerrit Renker  */
216f3f3abb6SGerrit Renker static const char *dccp_feat_fname(const u8 feat)
217f3f3abb6SGerrit Renker {
21836cbd3dcSJan Engelhardt 	static const char *const feature_names[] = {
219f3f3abb6SGerrit Renker 		[DCCPF_RESERVED]	= "Reserved",
220f3f3abb6SGerrit Renker 		[DCCPF_CCID]		= "CCID",
221f3f3abb6SGerrit Renker 		[DCCPF_SHORT_SEQNOS]	= "Allow Short Seqnos",
222f3f3abb6SGerrit Renker 		[DCCPF_SEQUENCE_WINDOW]	= "Sequence Window",
223f3f3abb6SGerrit Renker 		[DCCPF_ECN_INCAPABLE]	= "ECN Incapable",
224f3f3abb6SGerrit Renker 		[DCCPF_ACK_RATIO]	= "Ack Ratio",
225f3f3abb6SGerrit Renker 		[DCCPF_SEND_ACK_VECTOR]	= "Send ACK Vector",
226f3f3abb6SGerrit Renker 		[DCCPF_SEND_NDP_COUNT]	= "Send NDP Count",
227f3f3abb6SGerrit Renker 		[DCCPF_MIN_CSUM_COVER]	= "Min. Csum Coverage",
228f3f3abb6SGerrit Renker 		[DCCPF_DATA_CHECKSUM]	= "Send Data Checksum",
229f3f3abb6SGerrit Renker 	};
230f3f3abb6SGerrit Renker 	if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
231f3f3abb6SGerrit Renker 		return feature_names[DCCPF_RESERVED];
232f3f3abb6SGerrit Renker 
233f3f3abb6SGerrit Renker 	if (feat ==  DCCPF_SEND_LEV_RATE)
234f3f3abb6SGerrit Renker 		return "Send Loss Event Rate";
235f3f3abb6SGerrit Renker 	if (feat >= DCCPF_MIN_CCID_SPECIFIC)
236f3f3abb6SGerrit Renker 		return "CCID-specific";
237f3f3abb6SGerrit Renker 
238f3f3abb6SGerrit Renker 	return feature_names[feat];
239f3f3abb6SGerrit Renker }
240f3f3abb6SGerrit Renker 
24136cbd3dcSJan Engelhardt static const char *const dccp_feat_sname[] = {
24236cbd3dcSJan Engelhardt 	"DEFAULT", "INITIALISING", "CHANGING", "UNSTABLE", "STABLE",
24336cbd3dcSJan Engelhardt };
244f3f3abb6SGerrit Renker 
245f3f3abb6SGerrit Renker #ifdef CONFIG_IP_DCCP_DEBUG
246f3f3abb6SGerrit Renker static const char *dccp_feat_oname(const u8 opt)
247f3f3abb6SGerrit Renker {
248f3f3abb6SGerrit Renker 	switch (opt) {
249f3f3abb6SGerrit Renker 	case DCCPO_CHANGE_L:  return "Change_L";
250f3f3abb6SGerrit Renker 	case DCCPO_CONFIRM_L: return "Confirm_L";
251f3f3abb6SGerrit Renker 	case DCCPO_CHANGE_R:  return "Change_R";
252f3f3abb6SGerrit Renker 	case DCCPO_CONFIRM_R: return "Confirm_R";
253f3f3abb6SGerrit Renker 	}
254f3f3abb6SGerrit Renker 	return NULL;
255f3f3abb6SGerrit Renker }
256f3f3abb6SGerrit Renker 
257f3f3abb6SGerrit Renker static void dccp_feat_printval(u8 feat_num, dccp_feat_val const *val)
258f3f3abb6SGerrit Renker {
259f3f3abb6SGerrit Renker 	u8 i, type = dccp_feat_type(feat_num);
260f3f3abb6SGerrit Renker 
261f3f3abb6SGerrit Renker 	if (val == NULL || (type == FEAT_SP && val->sp.vec == NULL))
262f3f3abb6SGerrit Renker 		dccp_pr_debug_cat("(NULL)");
263f3f3abb6SGerrit Renker 	else if (type == FEAT_SP)
264f3f3abb6SGerrit Renker 		for (i = 0; i < val->sp.len; i++)
265f3f3abb6SGerrit Renker 			dccp_pr_debug_cat("%s%u", i ? " " : "", val->sp.vec[i]);
266f3f3abb6SGerrit Renker 	else if (type == FEAT_NN)
267f3f3abb6SGerrit Renker 		dccp_pr_debug_cat("%llu", (unsigned long long)val->nn);
268f3f3abb6SGerrit Renker 	else
269f3f3abb6SGerrit Renker 		dccp_pr_debug_cat("unknown type %u", type);
270f3f3abb6SGerrit Renker }
271f3f3abb6SGerrit Renker 
272f3f3abb6SGerrit Renker static void dccp_feat_printvals(u8 feat_num, u8 *list, u8 len)
273f3f3abb6SGerrit Renker {
274f3f3abb6SGerrit Renker 	u8 type = dccp_feat_type(feat_num);
275f3f3abb6SGerrit Renker 	dccp_feat_val fval = { .sp.vec = list, .sp.len = len };
276f3f3abb6SGerrit Renker 
277f3f3abb6SGerrit Renker 	if (type == FEAT_NN)
278f3f3abb6SGerrit Renker 		fval.nn = dccp_decode_value_var(list, len);
279f3f3abb6SGerrit Renker 	dccp_feat_printval(feat_num, &fval);
280f3f3abb6SGerrit Renker }
281f3f3abb6SGerrit Renker 
282f3f3abb6SGerrit Renker static void dccp_feat_print_entry(struct dccp_feat_entry const *entry)
283f3f3abb6SGerrit Renker {
284f3f3abb6SGerrit Renker 	dccp_debug("   * %s %s = ", entry->is_local ? "local" : "remote",
285f3f3abb6SGerrit Renker 				    dccp_feat_fname(entry->feat_num));
286f3f3abb6SGerrit Renker 	dccp_feat_printval(entry->feat_num, &entry->val);
287f3f3abb6SGerrit Renker 	dccp_pr_debug_cat(", state=%s %s\n", dccp_feat_sname[entry->state],
288f3f3abb6SGerrit Renker 			  entry->needs_confirm ? "(Confirm pending)" : "");
289f3f3abb6SGerrit Renker }
290f3f3abb6SGerrit Renker 
291f3f3abb6SGerrit Renker #define dccp_feat_print_opt(opt, feat, val, len, mandatory)	do {	      \
292f3f3abb6SGerrit Renker 	dccp_pr_debug("%s(%s, ", dccp_feat_oname(opt), dccp_feat_fname(feat));\
293f3f3abb6SGerrit Renker 	dccp_feat_printvals(feat, val, len);				      \
294f3f3abb6SGerrit Renker 	dccp_pr_debug_cat(") %s\n", mandatory ? "!" : "");	} while (0)
295f3f3abb6SGerrit Renker 
296f3f3abb6SGerrit Renker #define dccp_feat_print_fnlist(fn_list)  {		\
297f3f3abb6SGerrit Renker 	const struct dccp_feat_entry *___entry;		\
298f3f3abb6SGerrit Renker 							\
299f3f3abb6SGerrit Renker 	dccp_pr_debug("List Dump:\n");			\
300f3f3abb6SGerrit Renker 	list_for_each_entry(___entry, fn_list, node)	\
301f3f3abb6SGerrit Renker 		dccp_feat_print_entry(___entry);	\
302f3f3abb6SGerrit Renker }
303f3f3abb6SGerrit Renker #else	/* ! CONFIG_IP_DCCP_DEBUG */
304f3f3abb6SGerrit Renker #define dccp_feat_print_opt(opt, feat, val, len, mandatory)
305f3f3abb6SGerrit Renker #define dccp_feat_print_fnlist(fn_list)
306f3f3abb6SGerrit Renker #endif
307f3f3abb6SGerrit Renker 
308422d9cdcSGerrit Renker static int __dccp_feat_activate(struct sock *sk, const int idx,
309422d9cdcSGerrit Renker 				const bool is_local, dccp_feat_val const *fval)
310422d9cdcSGerrit Renker {
311422d9cdcSGerrit Renker 	bool rx;
312422d9cdcSGerrit Renker 	u64 val;
313422d9cdcSGerrit Renker 
314422d9cdcSGerrit Renker 	if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
315422d9cdcSGerrit Renker 		return -1;
316422d9cdcSGerrit Renker 	if (dccp_feat_table[idx].activation_hdlr == NULL)
317422d9cdcSGerrit Renker 		return 0;
318422d9cdcSGerrit Renker 
319422d9cdcSGerrit Renker 	if (fval == NULL) {
320422d9cdcSGerrit Renker 		val = dccp_feat_table[idx].default_value;
321422d9cdcSGerrit Renker 	} else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
322422d9cdcSGerrit Renker 		if (fval->sp.vec == NULL) {
323422d9cdcSGerrit Renker 			/*
324422d9cdcSGerrit Renker 			 * This can happen when an empty Confirm is sent
325422d9cdcSGerrit Renker 			 * for an SP (i.e. known) feature. In this case
326422d9cdcSGerrit Renker 			 * we would be using the default anyway.
327422d9cdcSGerrit Renker 			 */
328422d9cdcSGerrit Renker 			DCCP_CRIT("Feature #%d undefined: using default", idx);
329422d9cdcSGerrit Renker 			val = dccp_feat_table[idx].default_value;
330422d9cdcSGerrit Renker 		} else {
331422d9cdcSGerrit Renker 			val = fval->sp.vec[0];
332422d9cdcSGerrit Renker 		}
333422d9cdcSGerrit Renker 	} else {
334422d9cdcSGerrit Renker 		val = fval->nn;
335422d9cdcSGerrit Renker 	}
336422d9cdcSGerrit Renker 
337422d9cdcSGerrit Renker 	/* Location is RX if this is a local-RX or remote-TX feature */
338422d9cdcSGerrit Renker 	rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
339422d9cdcSGerrit Renker 
340f3f3abb6SGerrit Renker 	dccp_debug("   -> activating %s %s, %sval=%llu\n", rx ? "RX" : "TX",
341f3f3abb6SGerrit Renker 		   dccp_feat_fname(dccp_feat_table[idx].feat_num),
342f3f3abb6SGerrit Renker 		   fval ? "" : "default ",  (unsigned long long)val);
343f3f3abb6SGerrit Renker 
344422d9cdcSGerrit Renker 	return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
345422d9cdcSGerrit Renker }
346422d9cdcSGerrit Renker 
34744e6fd9eSGerrit Renker /**
34844e6fd9eSGerrit Renker  * dccp_feat_activate  -  Activate feature value on socket
34944e6fd9eSGerrit Renker  * @sk: fully connected DCCP socket (after handshake is complete)
35044e6fd9eSGerrit Renker  * @feat_num: feature to activate, one of %dccp_feature_numbers
35144e6fd9eSGerrit Renker  * @local: whether local (1) or remote (0) @feat_num is meant
35244e6fd9eSGerrit Renker  * @fval: the value (SP or NN) to activate, or NULL to use the default value
35344e6fd9eSGerrit Renker  * For general use this function is preferable over __dccp_feat_activate().
35444e6fd9eSGerrit Renker  */
35544e6fd9eSGerrit Renker static int dccp_feat_activate(struct sock *sk, u8 feat_num, bool local,
35644e6fd9eSGerrit Renker 			      dccp_feat_val const *fval)
35744e6fd9eSGerrit Renker {
35844e6fd9eSGerrit Renker 	return __dccp_feat_activate(sk, dccp_feat_index(feat_num), local, fval);
35944e6fd9eSGerrit Renker }
36044e6fd9eSGerrit Renker 
361b1ad0042SGerrit Renker /* Test for "Req'd" feature (RFC 4340, 6.4) */
362b1ad0042SGerrit Renker static inline int dccp_feat_must_be_understood(u8 feat_num)
363b1ad0042SGerrit Renker {
364b1ad0042SGerrit Renker 	return	feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
365b1ad0042SGerrit Renker 		feat_num == DCCPF_SEQUENCE_WINDOW;
366b1ad0042SGerrit Renker }
367b1ad0042SGerrit Renker 
368ac75773cSGerrit Renker /* copy constructor, fval must not already contain allocated memory */
369ac75773cSGerrit Renker static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
370ac75773cSGerrit Renker {
371ac75773cSGerrit Renker 	fval->sp.len = len;
372ac75773cSGerrit Renker 	if (fval->sp.len > 0) {
373ac75773cSGerrit Renker 		fval->sp.vec = kmemdup(val, len, gfp_any());
374ac75773cSGerrit Renker 		if (fval->sp.vec == NULL) {
375ac75773cSGerrit Renker 			fval->sp.len = 0;
376ac75773cSGerrit Renker 			return -ENOBUFS;
377ac75773cSGerrit Renker 		}
378ac75773cSGerrit Renker 	}
379ac75773cSGerrit Renker 	return 0;
380ac75773cSGerrit Renker }
381ac75773cSGerrit Renker 
38261e6473eSGerrit Renker static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
38361e6473eSGerrit Renker {
38461e6473eSGerrit Renker 	if (unlikely(val == NULL))
38561e6473eSGerrit Renker 		return;
38661e6473eSGerrit Renker 	if (dccp_feat_type(feat_num) == FEAT_SP)
38761e6473eSGerrit Renker 		kfree(val->sp.vec);
38861e6473eSGerrit Renker 	memset(val, 0, sizeof(*val));
38961e6473eSGerrit Renker }
39061e6473eSGerrit Renker 
391ac75773cSGerrit Renker static struct dccp_feat_entry *
392ac75773cSGerrit Renker 	      dccp_feat_clone_entry(struct dccp_feat_entry const *original)
393ac75773cSGerrit Renker {
394ac75773cSGerrit Renker 	struct dccp_feat_entry *new;
395ac75773cSGerrit Renker 	u8 type = dccp_feat_type(original->feat_num);
396ac75773cSGerrit Renker 
397ac75773cSGerrit Renker 	if (type == FEAT_UNKNOWN)
398ac75773cSGerrit Renker 		return NULL;
399ac75773cSGerrit Renker 
400ac75773cSGerrit Renker 	new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
401ac75773cSGerrit Renker 	if (new == NULL)
402ac75773cSGerrit Renker 		return NULL;
403ac75773cSGerrit Renker 
404ac75773cSGerrit Renker 	if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
405ac75773cSGerrit Renker 						      original->val.sp.vec,
406ac75773cSGerrit Renker 						      original->val.sp.len)) {
407ac75773cSGerrit Renker 		kfree(new);
408ac75773cSGerrit Renker 		return NULL;
409ac75773cSGerrit Renker 	}
410ac75773cSGerrit Renker 	return new;
411ac75773cSGerrit Renker }
412ac75773cSGerrit Renker 
41361e6473eSGerrit Renker static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
41461e6473eSGerrit Renker {
41561e6473eSGerrit Renker 	if (entry != NULL) {
41661e6473eSGerrit Renker 		dccp_feat_val_destructor(entry->feat_num, &entry->val);
41761e6473eSGerrit Renker 		kfree(entry);
41861e6473eSGerrit Renker 	}
41961e6473eSGerrit Renker }
42061e6473eSGerrit Renker 
42161e6473eSGerrit Renker /*
42261e6473eSGerrit Renker  * List management functions
42361e6473eSGerrit Renker  *
42461e6473eSGerrit Renker  * Feature negotiation lists rely on and maintain the following invariants:
42561e6473eSGerrit Renker  * - each feat_num in the list is known, i.e. we know its type and default value
42661e6473eSGerrit Renker  * - each feat_num/is_local combination is unique (old entries are overwritten)
42761e6473eSGerrit Renker  * - SP values are always freshly allocated
42861e6473eSGerrit Renker  * - list is sorted in increasing order of feature number (faster lookup)
42961e6473eSGerrit Renker  */
4300c116839SGerrit Renker static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
4310c116839SGerrit Renker 						     u8 feat_num, bool is_local)
4320c116839SGerrit Renker {
4330c116839SGerrit Renker 	struct dccp_feat_entry *entry;
4340c116839SGerrit Renker 
4353d3e35aaSGerrit Renker 	list_for_each_entry(entry, fn_list, node) {
4360c116839SGerrit Renker 		if (entry->feat_num == feat_num && entry->is_local == is_local)
4370c116839SGerrit Renker 			return entry;
4380c116839SGerrit Renker 		else if (entry->feat_num > feat_num)
4390c116839SGerrit Renker 			break;
4403d3e35aaSGerrit Renker 	}
4410c116839SGerrit Renker 	return NULL;
4420c116839SGerrit Renker }
44361e6473eSGerrit Renker 
444e8ef967aSGerrit Renker /**
445e8ef967aSGerrit Renker  * dccp_feat_entry_new  -  Central list update routine (called by all others)
446e8ef967aSGerrit Renker  * @head:  list to add to
447e8ef967aSGerrit Renker  * @feat:  feature number
448e8ef967aSGerrit Renker  * @local: whether the local (1) or remote feature with number @feat is meant
449e8ef967aSGerrit Renker  * This is the only constructor and serves to ensure the above invariants.
450e8ef967aSGerrit Renker  */
451e8ef967aSGerrit Renker static struct dccp_feat_entry *
452e8ef967aSGerrit Renker 	      dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
453e8ef967aSGerrit Renker {
454e8ef967aSGerrit Renker 	struct dccp_feat_entry *entry;
455e8ef967aSGerrit Renker 
456e8ef967aSGerrit Renker 	list_for_each_entry(entry, head, node)
457e8ef967aSGerrit Renker 		if (entry->feat_num == feat && entry->is_local == local) {
458e8ef967aSGerrit Renker 			dccp_feat_val_destructor(entry->feat_num, &entry->val);
459e8ef967aSGerrit Renker 			return entry;
460e8ef967aSGerrit Renker 		} else if (entry->feat_num > feat) {
461e8ef967aSGerrit Renker 			head = &entry->node;
462e8ef967aSGerrit Renker 			break;
463e8ef967aSGerrit Renker 		}
464e8ef967aSGerrit Renker 
465e8ef967aSGerrit Renker 	entry = kmalloc(sizeof(*entry), gfp_any());
466e8ef967aSGerrit Renker 	if (entry != NULL) {
467e8ef967aSGerrit Renker 		entry->feat_num = feat;
468e8ef967aSGerrit Renker 		entry->is_local = local;
469e8ef967aSGerrit Renker 		list_add_tail(&entry->node, head);
470e8ef967aSGerrit Renker 	}
471e8ef967aSGerrit Renker 	return entry;
472e8ef967aSGerrit Renker }
473e8ef967aSGerrit Renker 
474e8ef967aSGerrit Renker /**
475e8ef967aSGerrit Renker  * dccp_feat_push_change  -  Add/overwrite a Change option in the list
476e8ef967aSGerrit Renker  * @fn_list: feature-negotiation list to update
477e8ef967aSGerrit Renker  * @feat: one of %dccp_feature_numbers
478e8ef967aSGerrit Renker  * @local: whether local (1) or remote (0) @feat_num is meant
479e8ef967aSGerrit Renker  * @needs_mandatory: whether to use Mandatory feature negotiation options
480e8ef967aSGerrit Renker  * @fval: pointer to NN/SP value to be inserted (will be copied)
481e8ef967aSGerrit Renker  */
482e8ef967aSGerrit Renker static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
483e8ef967aSGerrit Renker 				 u8 mandatory, dccp_feat_val *fval)
484e8ef967aSGerrit Renker {
485e8ef967aSGerrit Renker 	struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
486e8ef967aSGerrit Renker 
487e8ef967aSGerrit Renker 	if (new == NULL)
488e8ef967aSGerrit Renker 		return -ENOMEM;
489e8ef967aSGerrit Renker 
490e8ef967aSGerrit Renker 	new->feat_num	     = feat;
491e8ef967aSGerrit Renker 	new->is_local	     = local;
492e8ef967aSGerrit Renker 	new->state	     = FEAT_INITIALISING;
493*3db1cd5cSRusty Russell 	new->needs_confirm   = false;
494*3db1cd5cSRusty Russell 	new->empty_confirm   = false;
495e8ef967aSGerrit Renker 	new->val	     = *fval;
496e8ef967aSGerrit Renker 	new->needs_mandatory = mandatory;
497e8ef967aSGerrit Renker 
498e8ef967aSGerrit Renker 	return 0;
499e8ef967aSGerrit Renker }
500e8ef967aSGerrit Renker 
501e77b8363SGerrit Renker /**
502e77b8363SGerrit Renker  * dccp_feat_push_confirm  -  Add a Confirm entry to the FN list
503e77b8363SGerrit Renker  * @fn_list: feature-negotiation list to add to
504e77b8363SGerrit Renker  * @feat: one of %dccp_feature_numbers
505e77b8363SGerrit Renker  * @local: whether local (1) or remote (0) @feat_num is being confirmed
506e77b8363SGerrit Renker  * @fval: pointer to NN/SP value to be inserted or NULL
507e77b8363SGerrit Renker  * Returns 0 on success, a Reset code for further processing otherwise.
508e77b8363SGerrit Renker  */
509e77b8363SGerrit Renker static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
510e77b8363SGerrit Renker 				  dccp_feat_val *fval)
511e77b8363SGerrit Renker {
512e77b8363SGerrit Renker 	struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
513e77b8363SGerrit Renker 
514e77b8363SGerrit Renker 	if (new == NULL)
515e77b8363SGerrit Renker 		return DCCP_RESET_CODE_TOO_BUSY;
516e77b8363SGerrit Renker 
517e77b8363SGerrit Renker 	new->feat_num	     = feat;
518e77b8363SGerrit Renker 	new->is_local	     = local;
519e77b8363SGerrit Renker 	new->state	     = FEAT_STABLE;	/* transition in 6.6.2 */
520*3db1cd5cSRusty Russell 	new->needs_confirm   = true;
521e77b8363SGerrit Renker 	new->empty_confirm   = (fval == NULL);
522e77b8363SGerrit Renker 	new->val.nn	     = 0;		/* zeroes the whole structure */
523e77b8363SGerrit Renker 	if (!new->empty_confirm)
524e77b8363SGerrit Renker 		new->val     = *fval;
525*3db1cd5cSRusty Russell 	new->needs_mandatory = false;
526e77b8363SGerrit Renker 
527e77b8363SGerrit Renker 	return 0;
528e77b8363SGerrit Renker }
529e77b8363SGerrit Renker 
530e77b8363SGerrit Renker static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
531e77b8363SGerrit Renker {
532e77b8363SGerrit Renker 	return dccp_feat_push_confirm(fn_list, feat, local, NULL);
533e77b8363SGerrit Renker }
534e77b8363SGerrit Renker 
53561e6473eSGerrit Renker static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
53661e6473eSGerrit Renker {
53761e6473eSGerrit Renker 	list_del(&entry->node);
53861e6473eSGerrit Renker 	dccp_feat_entry_destructor(entry);
53961e6473eSGerrit Renker }
54061e6473eSGerrit Renker 
54161e6473eSGerrit Renker void dccp_feat_list_purge(struct list_head *fn_list)
54261e6473eSGerrit Renker {
54361e6473eSGerrit Renker 	struct dccp_feat_entry *entry, *next;
54461e6473eSGerrit Renker 
54561e6473eSGerrit Renker 	list_for_each_entry_safe(entry, next, fn_list, node)
54661e6473eSGerrit Renker 		dccp_feat_entry_destructor(entry);
54761e6473eSGerrit Renker 	INIT_LIST_HEAD(fn_list);
54861e6473eSGerrit Renker }
54961e6473eSGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
55061e6473eSGerrit Renker 
551ac75773cSGerrit Renker /* generate @to as full clone of @from - @to must not contain any nodes */
552ac75773cSGerrit Renker int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
553ac75773cSGerrit Renker {
554ac75773cSGerrit Renker 	struct dccp_feat_entry *entry, *new;
555ac75773cSGerrit Renker 
556ac75773cSGerrit Renker 	INIT_LIST_HEAD(to);
557ac75773cSGerrit Renker 	list_for_each_entry(entry, from, node) {
558ac75773cSGerrit Renker 		new = dccp_feat_clone_entry(entry);
559ac75773cSGerrit Renker 		if (new == NULL)
560ac75773cSGerrit Renker 			goto cloning_failed;
561ac75773cSGerrit Renker 		list_add_tail(&new->node, to);
562ac75773cSGerrit Renker 	}
563ac75773cSGerrit Renker 	return 0;
564ac75773cSGerrit Renker 
565ac75773cSGerrit Renker cloning_failed:
566ac75773cSGerrit Renker 	dccp_feat_list_purge(to);
567ac75773cSGerrit Renker 	return -ENOMEM;
568ac75773cSGerrit Renker }
569ac75773cSGerrit Renker 
5700971d17cSGerrit Renker /**
5710971d17cSGerrit Renker  * dccp_feat_valid_nn_length  -  Enforce length constraints on NN options
5720971d17cSGerrit Renker  * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
5730971d17cSGerrit Renker  * incoming options are accepted as long as their values are valid.
5740971d17cSGerrit Renker  */
5750971d17cSGerrit Renker static u8 dccp_feat_valid_nn_length(u8 feat_num)
5760971d17cSGerrit Renker {
5770971d17cSGerrit Renker 	if (feat_num == DCCPF_ACK_RATIO)	/* RFC 4340, 11.3 and 6.6.8 */
5780971d17cSGerrit Renker 		return 2;
5790971d17cSGerrit Renker 	if (feat_num == DCCPF_SEQUENCE_WINDOW)	/* RFC 4340, 7.5.2 and 6.5  */
5800971d17cSGerrit Renker 		return 6;
5810971d17cSGerrit Renker 	return 0;
5820971d17cSGerrit Renker }
5830971d17cSGerrit Renker 
584e8ef967aSGerrit Renker static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
585e8ef967aSGerrit Renker {
586e8ef967aSGerrit Renker 	switch (feat_num) {
587e8ef967aSGerrit Renker 	case DCCPF_ACK_RATIO:
588e8ef967aSGerrit Renker 		return val <= DCCPF_ACK_RATIO_MAX;
589e8ef967aSGerrit Renker 	case DCCPF_SEQUENCE_WINDOW:
590e8ef967aSGerrit Renker 		return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
591e8ef967aSGerrit Renker 	}
592e8ef967aSGerrit Renker 	return 0;	/* feature unknown - so we can't tell */
593e8ef967aSGerrit Renker }
594e8ef967aSGerrit Renker 
595e8ef967aSGerrit Renker /* check that SP values are within the ranges defined in RFC 4340 */
596e8ef967aSGerrit Renker static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
597e8ef967aSGerrit Renker {
598e8ef967aSGerrit Renker 	switch (feat_num) {
599e8ef967aSGerrit Renker 	case DCCPF_CCID:
600e8ef967aSGerrit Renker 		return val == DCCPC_CCID2 || val == DCCPC_CCID3;
601e8ef967aSGerrit Renker 	/* Type-check Boolean feature values: */
602e8ef967aSGerrit Renker 	case DCCPF_SHORT_SEQNOS:
603e8ef967aSGerrit Renker 	case DCCPF_ECN_INCAPABLE:
604e8ef967aSGerrit Renker 	case DCCPF_SEND_ACK_VECTOR:
605e8ef967aSGerrit Renker 	case DCCPF_SEND_NDP_COUNT:
606e8ef967aSGerrit Renker 	case DCCPF_DATA_CHECKSUM:
607e8ef967aSGerrit Renker 	case DCCPF_SEND_LEV_RATE:
608e8ef967aSGerrit Renker 		return val < 2;
609e8ef967aSGerrit Renker 	case DCCPF_MIN_CSUM_COVER:
610e8ef967aSGerrit Renker 		return val < 16;
611e8ef967aSGerrit Renker 	}
612e8ef967aSGerrit Renker 	return 0;			/* feature unknown */
613e8ef967aSGerrit Renker }
614e8ef967aSGerrit Renker 
615e8ef967aSGerrit Renker static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
616e8ef967aSGerrit Renker {
617e8ef967aSGerrit Renker 	if (sp_list == NULL || sp_len < 1)
618e8ef967aSGerrit Renker 		return 0;
619e8ef967aSGerrit Renker 	while (sp_len--)
620e8ef967aSGerrit Renker 		if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
621e8ef967aSGerrit Renker 			return 0;
622e8ef967aSGerrit Renker 	return 1;
623e8ef967aSGerrit Renker }
624e8ef967aSGerrit Renker 
625e8ef967aSGerrit Renker /**
6260971d17cSGerrit Renker  * dccp_feat_insert_opts  -  Generate FN options from current list state
6270971d17cSGerrit Renker  * @skb: next sk_buff to be sent to the peer
6280971d17cSGerrit Renker  * @dp: for client during handshake and general negotiation
6290971d17cSGerrit Renker  * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
6300971d17cSGerrit Renker  */
6310971d17cSGerrit Renker int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
6320971d17cSGerrit Renker 			  struct sk_buff *skb)
6330971d17cSGerrit Renker {
6340971d17cSGerrit Renker 	struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
6350971d17cSGerrit Renker 	struct dccp_feat_entry *pos, *next;
6360971d17cSGerrit Renker 	u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
6370971d17cSGerrit Renker 	bool rpt;
6380971d17cSGerrit Renker 
6390971d17cSGerrit Renker 	/* put entries into @skb in the order they appear in the list */
6400971d17cSGerrit Renker 	list_for_each_entry_safe_reverse(pos, next, fn, node) {
6410971d17cSGerrit Renker 		opt  = dccp_feat_genopt(pos);
6420971d17cSGerrit Renker 		type = dccp_feat_type(pos->feat_num);
6430971d17cSGerrit Renker 		rpt  = false;
6440971d17cSGerrit Renker 
6450971d17cSGerrit Renker 		if (pos->empty_confirm) {
6460971d17cSGerrit Renker 			len = 0;
6470971d17cSGerrit Renker 			ptr = NULL;
6480971d17cSGerrit Renker 		} else {
6490971d17cSGerrit Renker 			if (type == FEAT_SP) {
6500971d17cSGerrit Renker 				len = pos->val.sp.len;
6510971d17cSGerrit Renker 				ptr = pos->val.sp.vec;
6520971d17cSGerrit Renker 				rpt = pos->needs_confirm;
6530971d17cSGerrit Renker 			} else if (type == FEAT_NN) {
6540971d17cSGerrit Renker 				len = dccp_feat_valid_nn_length(pos->feat_num);
6550971d17cSGerrit Renker 				ptr = nn_in_nbo;
6560971d17cSGerrit Renker 				dccp_encode_value_var(pos->val.nn, ptr, len);
6570971d17cSGerrit Renker 			} else {
6580971d17cSGerrit Renker 				DCCP_BUG("unknown feature %u", pos->feat_num);
6590971d17cSGerrit Renker 				return -1;
6600971d17cSGerrit Renker 			}
6610971d17cSGerrit Renker 		}
662f3f3abb6SGerrit Renker 		dccp_feat_print_opt(opt, pos->feat_num, ptr, len, 0);
6630971d17cSGerrit Renker 
6640971d17cSGerrit Renker 		if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
6650971d17cSGerrit Renker 			return -1;
6660971d17cSGerrit Renker 		if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
6670971d17cSGerrit Renker 			return -1;
668a6444f42SSamuel Jero 
669a6444f42SSamuel Jero 		if (skb->sk->sk_state == DCCP_OPEN &&
670a6444f42SSamuel Jero 		    (opt == DCCPO_CONFIRM_R || opt == DCCPO_CONFIRM_L)) {
6710971d17cSGerrit Renker 			/*
672a6444f42SSamuel Jero 			 * Confirms don't get retransmitted (6.6.3) once the
673a6444f42SSamuel Jero 			 * connection is in state OPEN
674a6444f42SSamuel Jero 			 */
675a6444f42SSamuel Jero 			dccp_feat_list_pop(pos);
676a6444f42SSamuel Jero 		} else {
677a6444f42SSamuel Jero 			/*
678a6444f42SSamuel Jero 			 * Enter CHANGING after transmitting the Change
679a6444f42SSamuel Jero 			 * option (6.6.2).
6800971d17cSGerrit Renker 			 */
6810971d17cSGerrit Renker 			if (pos->state == FEAT_INITIALISING)
6820971d17cSGerrit Renker 				pos->state = FEAT_CHANGING;
6830971d17cSGerrit Renker 		}
684a6444f42SSamuel Jero 	}
6850971d17cSGerrit Renker 	return 0;
6860971d17cSGerrit Renker }
6870971d17cSGerrit Renker 
6880971d17cSGerrit Renker /**
689e8ef967aSGerrit Renker  * __feat_register_nn  -  Register new NN value on socket
690e8ef967aSGerrit Renker  * @fn: feature-negotiation list to register with
691e8ef967aSGerrit Renker  * @feat: an NN feature from %dccp_feature_numbers
692e8ef967aSGerrit Renker  * @mandatory: use Mandatory option if 1
693e8ef967aSGerrit Renker  * @nn_val: value to register (restricted to 4 bytes)
694e8ef967aSGerrit Renker  * Note that NN features are local by definition (RFC 4340, 6.3.2).
695e8ef967aSGerrit Renker  */
696e8ef967aSGerrit Renker static int __feat_register_nn(struct list_head *fn, u8 feat,
697e8ef967aSGerrit Renker 			      u8 mandatory, u64 nn_val)
698e8ef967aSGerrit Renker {
699e8ef967aSGerrit Renker 	dccp_feat_val fval = { .nn = nn_val };
700e8ef967aSGerrit Renker 
701e8ef967aSGerrit Renker 	if (dccp_feat_type(feat) != FEAT_NN ||
702e8ef967aSGerrit Renker 	    !dccp_feat_is_valid_nn_val(feat, nn_val))
703e8ef967aSGerrit Renker 		return -EINVAL;
704e8ef967aSGerrit Renker 
705e8ef967aSGerrit Renker 	/* Don't bother with default values, they will be activated anyway. */
706e8ef967aSGerrit Renker 	if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
707e8ef967aSGerrit Renker 		return 0;
708e8ef967aSGerrit Renker 
709e8ef967aSGerrit Renker 	return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
710e8ef967aSGerrit Renker }
711e8ef967aSGerrit Renker 
712e8ef967aSGerrit Renker /**
713e8ef967aSGerrit Renker  * __feat_register_sp  -  Register new SP value/list on socket
714e8ef967aSGerrit Renker  * @fn: feature-negotiation list to register with
715e8ef967aSGerrit Renker  * @feat: an SP feature from %dccp_feature_numbers
716e8ef967aSGerrit Renker  * @is_local: whether the local (1) or the remote (0) @feat is meant
717e8ef967aSGerrit Renker  * @mandatory: use Mandatory option if 1
718e8ef967aSGerrit Renker  * @sp_val: SP value followed by optional preference list
719e8ef967aSGerrit Renker  * @sp_len: length of @sp_val in bytes
720e8ef967aSGerrit Renker  */
721e8ef967aSGerrit Renker static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
722e8ef967aSGerrit Renker 			      u8 mandatory, u8 const *sp_val, u8 sp_len)
723e8ef967aSGerrit Renker {
724e8ef967aSGerrit Renker 	dccp_feat_val fval;
725e8ef967aSGerrit Renker 
726e8ef967aSGerrit Renker 	if (dccp_feat_type(feat) != FEAT_SP ||
727e8ef967aSGerrit Renker 	    !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
728e8ef967aSGerrit Renker 		return -EINVAL;
729e8ef967aSGerrit Renker 
730d90ebcbfSGerrit Renker 	/* Avoid negotiating alien CCIDs by only advertising supported ones */
731d90ebcbfSGerrit Renker 	if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
732d90ebcbfSGerrit Renker 		return -EOPNOTSUPP;
733d90ebcbfSGerrit Renker 
734e8ef967aSGerrit Renker 	if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
735e8ef967aSGerrit Renker 		return -ENOMEM;
736e8ef967aSGerrit Renker 
737e8ef967aSGerrit Renker 	return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
738e8ef967aSGerrit Renker }
739e8ef967aSGerrit Renker 
74049aebc66SGerrit Renker /**
74149aebc66SGerrit Renker  * dccp_feat_register_sp  -  Register requests to change SP feature values
74249aebc66SGerrit Renker  * @sk: client or listening socket
74349aebc66SGerrit Renker  * @feat: one of %dccp_feature_numbers
74449aebc66SGerrit Renker  * @is_local: whether the local (1) or remote (0) @feat is meant
74549aebc66SGerrit Renker  * @list: array of preferred values, in descending order of preference
74649aebc66SGerrit Renker  * @len: length of @list in bytes
74749aebc66SGerrit Renker  */
74849aebc66SGerrit Renker int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
74949aebc66SGerrit Renker 			  u8 const *list, u8 len)
75049aebc66SGerrit Renker {	 /* any changes must be registered before establishing the connection */
75149aebc66SGerrit Renker 	if (sk->sk_state != DCCP_CLOSED)
75249aebc66SGerrit Renker 		return -EISCONN;
75349aebc66SGerrit Renker 	if (dccp_feat_type(feat) != FEAT_SP)
75419443178SChris Wright 		return -EINVAL;
75549aebc66SGerrit Renker 	return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
75649aebc66SGerrit Renker 				  0, list, len);
757afe00251SAndrea Bittau }
758afe00251SAndrea Bittau 
759d6916f87SGerrit Renker /**
760d6916f87SGerrit Renker  * dccp_feat_nn_get  -  Query current/pending value of NN feature
761d6916f87SGerrit Renker  * @sk: DCCP socket of an established connection
762d6916f87SGerrit Renker  * @feat: NN feature number from %dccp_feature_numbers
763d6916f87SGerrit Renker  * For a known NN feature, returns value currently being negotiated, or
764d6916f87SGerrit Renker  * current (confirmed) value if no negotiation is going on.
765d6916f87SGerrit Renker  */
766d6916f87SGerrit Renker u64 dccp_feat_nn_get(struct sock *sk, u8 feat)
767d6916f87SGerrit Renker {
768d6916f87SGerrit Renker 	if (dccp_feat_type(feat) == FEAT_NN) {
769d6916f87SGerrit Renker 		struct dccp_sock *dp = dccp_sk(sk);
770d6916f87SGerrit Renker 		struct dccp_feat_entry *entry;
771d6916f87SGerrit Renker 
772d6916f87SGerrit Renker 		entry = dccp_feat_list_lookup(&dp->dccps_featneg, feat, 1);
773d6916f87SGerrit Renker 		if (entry != NULL)
774d6916f87SGerrit Renker 			return entry->val.nn;
775d6916f87SGerrit Renker 
776d6916f87SGerrit Renker 		switch (feat) {
777d6916f87SGerrit Renker 		case DCCPF_ACK_RATIO:
778d6916f87SGerrit Renker 			return dp->dccps_l_ack_ratio;
779d6916f87SGerrit Renker 		case DCCPF_SEQUENCE_WINDOW:
780d6916f87SGerrit Renker 			return dp->dccps_l_seq_win;
781d6916f87SGerrit Renker 		}
782d6916f87SGerrit Renker 	}
783d6916f87SGerrit Renker 	DCCP_BUG("attempt to look up unsupported feature %u", feat);
784d6916f87SGerrit Renker 	return 0;
785d6916f87SGerrit Renker }
786d6916f87SGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_nn_get);
787d6916f87SGerrit Renker 
788d6916f87SGerrit Renker /**
789d6916f87SGerrit Renker  * dccp_feat_signal_nn_change  -  Update NN values for an established connection
790d6916f87SGerrit Renker  * @sk: DCCP socket of an established connection
791d6916f87SGerrit Renker  * @feat: NN feature number from %dccp_feature_numbers
792d6916f87SGerrit Renker  * @nn_val: the new value to use
793d6916f87SGerrit Renker  * This function is used to communicate NN updates out-of-band.
794d6916f87SGerrit Renker  */
795d6916f87SGerrit Renker int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val)
796d6916f87SGerrit Renker {
797d6916f87SGerrit Renker 	struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
798d6916f87SGerrit Renker 	dccp_feat_val fval = { .nn = nn_val };
799d6916f87SGerrit Renker 	struct dccp_feat_entry *entry;
800d6916f87SGerrit Renker 
801d6916f87SGerrit Renker 	if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN)
802d6916f87SGerrit Renker 		return 0;
803d6916f87SGerrit Renker 
804d6916f87SGerrit Renker 	if (dccp_feat_type(feat) != FEAT_NN ||
805d6916f87SGerrit Renker 	    !dccp_feat_is_valid_nn_val(feat, nn_val))
806d6916f87SGerrit Renker 		return -EINVAL;
807d6916f87SGerrit Renker 
808d6916f87SGerrit Renker 	if (nn_val == dccp_feat_nn_get(sk, feat))
809d6916f87SGerrit Renker 		return 0;	/* already set or negotiation under way */
810d6916f87SGerrit Renker 
811d6916f87SGerrit Renker 	entry = dccp_feat_list_lookup(fn, feat, 1);
812d6916f87SGerrit Renker 	if (entry != NULL) {
813d6916f87SGerrit Renker 		dccp_pr_debug("Clobbering existing NN entry %llu -> %llu\n",
814d6916f87SGerrit Renker 			      (unsigned long long)entry->val.nn,
815d6916f87SGerrit Renker 			      (unsigned long long)nn_val);
816d6916f87SGerrit Renker 		dccp_feat_list_pop(entry);
817d6916f87SGerrit Renker 	}
818d6916f87SGerrit Renker 
819d6916f87SGerrit Renker 	inet_csk_schedule_ack(sk);
820d6916f87SGerrit Renker 	return dccp_feat_push_change(fn, feat, 1, 0, &fval);
821d6916f87SGerrit Renker }
822d6916f87SGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_signal_nn_change);
823afe00251SAndrea Bittau 
8249eca0a47SGerrit Renker /*
8259eca0a47SGerrit Renker  *	Tracking features whose value depend on the choice of CCID
8269eca0a47SGerrit Renker  *
8279eca0a47SGerrit Renker  * This is designed with an extension in mind so that a list walk could be done
8289eca0a47SGerrit Renker  * before activating any features. However, the existing framework was found to
8299eca0a47SGerrit Renker  * work satisfactorily up until now, the automatic verification is left open.
8309eca0a47SGerrit Renker  * When adding new CCIDs, add a corresponding dependency table here.
8319eca0a47SGerrit Renker  */
8329eca0a47SGerrit Renker static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
8339eca0a47SGerrit Renker {
8349eca0a47SGerrit Renker 	static const struct ccid_dependency ccid2_dependencies[2][2] = {
8359eca0a47SGerrit Renker 		/*
8369eca0a47SGerrit Renker 		 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
8379eca0a47SGerrit Renker 		 * feature and Send Ack Vector is an RX feature, `is_local'
8389eca0a47SGerrit Renker 		 * needs to be reversed.
8399eca0a47SGerrit Renker 		 */
8409eca0a47SGerrit Renker 		{	/* Dependencies of the receiver-side (remote) CCID2 */
8419eca0a47SGerrit Renker 			{
8429eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
8439eca0a47SGerrit Renker 				.is_local	= true,
8449eca0a47SGerrit Renker 				.is_mandatory	= true,
8459eca0a47SGerrit Renker 				.val		= 1
8469eca0a47SGerrit Renker 			},
8479eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
8489eca0a47SGerrit Renker 		},
8499eca0a47SGerrit Renker 		{	/* Dependencies of the sender-side (local) CCID2 */
8509eca0a47SGerrit Renker 			{
8519eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
8529eca0a47SGerrit Renker 				.is_local	= false,
8539eca0a47SGerrit Renker 				.is_mandatory	= true,
8549eca0a47SGerrit Renker 				.val		= 1
8559eca0a47SGerrit Renker 			},
8569eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
8579eca0a47SGerrit Renker 		}
8589eca0a47SGerrit Renker 	};
8599eca0a47SGerrit Renker 	static const struct ccid_dependency ccid3_dependencies[2][5] = {
8609eca0a47SGerrit Renker 		{	/*
8619eca0a47SGerrit Renker 			 * Dependencies of the receiver-side CCID3
8629eca0a47SGerrit Renker 			 */
8639eca0a47SGerrit Renker 			{	/* locally disable Ack Vectors */
8649eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
8659eca0a47SGerrit Renker 				.is_local	= true,
8669eca0a47SGerrit Renker 				.is_mandatory	= false,
8679eca0a47SGerrit Renker 				.val		= 0
8689eca0a47SGerrit Renker 			},
8699eca0a47SGerrit Renker 			{	/* see below why Send Loss Event Rate is on */
8709eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_LEV_RATE,
8719eca0a47SGerrit Renker 				.is_local	= true,
8729eca0a47SGerrit Renker 				.is_mandatory	= true,
8739eca0a47SGerrit Renker 				.val		= 1
8749eca0a47SGerrit Renker 			},
8759eca0a47SGerrit Renker 			{	/* NDP Count is needed as per RFC 4342, 6.1.1 */
8769eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_NDP_COUNT,
8779eca0a47SGerrit Renker 				.is_local	= false,
8789eca0a47SGerrit Renker 				.is_mandatory	= true,
8799eca0a47SGerrit Renker 				.val		= 1
8809eca0a47SGerrit Renker 			},
8819eca0a47SGerrit Renker 			{ 0, 0, 0, 0 },
8829eca0a47SGerrit Renker 		},
8839eca0a47SGerrit Renker 		{	/*
8849eca0a47SGerrit Renker 			 * CCID3 at the TX side: we request that the HC-receiver
8859eca0a47SGerrit Renker 			 * will not send Ack Vectors (they will be ignored, so
8869eca0a47SGerrit Renker 			 * Mandatory is not set); we enable Send Loss Event Rate
8879eca0a47SGerrit Renker 			 * (Mandatory since the implementation does not support
8889eca0a47SGerrit Renker 			 * the Loss Intervals option of RFC 4342, 8.6).
8899eca0a47SGerrit Renker 			 * The last two options are for peer's information only.
8909eca0a47SGerrit Renker 			*/
8919eca0a47SGerrit Renker 			{
8929eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
8939eca0a47SGerrit Renker 				.is_local	= false,
8949eca0a47SGerrit Renker 				.is_mandatory	= false,
8959eca0a47SGerrit Renker 				.val		= 0
8969eca0a47SGerrit Renker 			},
8979eca0a47SGerrit Renker 			{
8989eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_LEV_RATE,
8999eca0a47SGerrit Renker 				.is_local	= false,
9009eca0a47SGerrit Renker 				.is_mandatory	= true,
9019eca0a47SGerrit Renker 				.val		= 1
9029eca0a47SGerrit Renker 			},
9039eca0a47SGerrit Renker 			{	/* this CCID does not support Ack Ratio */
9049eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_ACK_RATIO,
9059eca0a47SGerrit Renker 				.is_local	= true,
9069eca0a47SGerrit Renker 				.is_mandatory	= false,
9079eca0a47SGerrit Renker 				.val		= 0
9089eca0a47SGerrit Renker 			},
9099eca0a47SGerrit Renker 			{	/* tell receiver we are sending NDP counts */
9109eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_NDP_COUNT,
9119eca0a47SGerrit Renker 				.is_local	= true,
9129eca0a47SGerrit Renker 				.is_mandatory	= false,
9139eca0a47SGerrit Renker 				.val		= 1
9149eca0a47SGerrit Renker 			},
9159eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
9169eca0a47SGerrit Renker 		}
9179eca0a47SGerrit Renker 	};
9189eca0a47SGerrit Renker 	switch (ccid) {
9199eca0a47SGerrit Renker 	case DCCPC_CCID2:
9209eca0a47SGerrit Renker 		return ccid2_dependencies[is_local];
9219eca0a47SGerrit Renker 	case DCCPC_CCID3:
9229eca0a47SGerrit Renker 		return ccid3_dependencies[is_local];
9239eca0a47SGerrit Renker 	default:
9249eca0a47SGerrit Renker 		return NULL;
9259eca0a47SGerrit Renker 	}
9269eca0a47SGerrit Renker }
9279eca0a47SGerrit Renker 
9289eca0a47SGerrit Renker /**
9299eca0a47SGerrit Renker  * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
9309eca0a47SGerrit Renker  * @fn: feature-negotiation list to update
9319eca0a47SGerrit Renker  * @id: CCID number to track
9329eca0a47SGerrit Renker  * @is_local: whether TX CCID (1) or RX CCID (0) is meant
9339eca0a47SGerrit Renker  * This function needs to be called after registering all other features.
9349eca0a47SGerrit Renker  */
9359eca0a47SGerrit Renker static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
9369eca0a47SGerrit Renker {
9379eca0a47SGerrit Renker 	const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
9389eca0a47SGerrit Renker 	int i, rc = (table == NULL);
9399eca0a47SGerrit Renker 
9409eca0a47SGerrit Renker 	for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
9419eca0a47SGerrit Renker 		if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
9429eca0a47SGerrit Renker 			rc = __feat_register_sp(fn, table[i].dependent_feat,
9439eca0a47SGerrit Renker 						    table[i].is_local,
9449eca0a47SGerrit Renker 						    table[i].is_mandatory,
9459eca0a47SGerrit Renker 						    &table[i].val, 1);
9469eca0a47SGerrit Renker 		else
9479eca0a47SGerrit Renker 			rc = __feat_register_nn(fn, table[i].dependent_feat,
9489eca0a47SGerrit Renker 						    table[i].is_mandatory,
9499eca0a47SGerrit Renker 						    table[i].val);
9509eca0a47SGerrit Renker 	return rc;
9519eca0a47SGerrit Renker }
9529eca0a47SGerrit Renker 
9539eca0a47SGerrit Renker /**
9549eca0a47SGerrit Renker  * dccp_feat_finalise_settings  -  Finalise settings before starting negotiation
9559eca0a47SGerrit Renker  * @dp: client or listening socket (settings will be inherited)
9569eca0a47SGerrit Renker  * This is called after all registrations (socket initialisation, sysctls, and
9579eca0a47SGerrit Renker  * sockopt calls), and before sending the first packet containing Change options
9589eca0a47SGerrit Renker  * (ie. client-Request or server-Response), to ensure internal consistency.
9599eca0a47SGerrit Renker  */
9609eca0a47SGerrit Renker int dccp_feat_finalise_settings(struct dccp_sock *dp)
9619eca0a47SGerrit Renker {
9629eca0a47SGerrit Renker 	struct list_head *fn = &dp->dccps_featneg;
9639eca0a47SGerrit Renker 	struct dccp_feat_entry *entry;
9649eca0a47SGerrit Renker 	int i = 2, ccids[2] = { -1, -1 };
9659eca0a47SGerrit Renker 
9669eca0a47SGerrit Renker 	/*
9679eca0a47SGerrit Renker 	 * Propagating CCIDs:
9689eca0a47SGerrit Renker 	 * 1) not useful to propagate CCID settings if this host advertises more
9699eca0a47SGerrit Renker 	 *    than one CCID: the choice of CCID  may still change - if this is
9709eca0a47SGerrit Renker 	 *    the client, or if this is the server and the client sends
9719eca0a47SGerrit Renker 	 *    singleton CCID values.
9729eca0a47SGerrit Renker 	 * 2) since is that propagate_ccid changes the list, we defer changing
9739eca0a47SGerrit Renker 	 *    the sorted list until after the traversal.
9749eca0a47SGerrit Renker 	 */
9759eca0a47SGerrit Renker 	list_for_each_entry(entry, fn, node)
9769eca0a47SGerrit Renker 		if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
9779eca0a47SGerrit Renker 			ccids[entry->is_local] = entry->val.sp.vec[0];
9789eca0a47SGerrit Renker 	while (i--)
9799eca0a47SGerrit Renker 		if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
9809eca0a47SGerrit Renker 			return -1;
981f3f3abb6SGerrit Renker 	dccp_feat_print_fnlist(fn);
9829eca0a47SGerrit Renker 	return 0;
9839eca0a47SGerrit Renker }
9849eca0a47SGerrit Renker 
9850c116839SGerrit Renker /**
9860c116839SGerrit Renker  * dccp_feat_server_ccid_dependencies  -  Resolve CCID-dependent features
9870c116839SGerrit Renker  * It is the server which resolves the dependencies once the CCID has been
9880c116839SGerrit Renker  * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
9890c116839SGerrit Renker  */
9900c116839SGerrit Renker int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
9910c116839SGerrit Renker {
9920c116839SGerrit Renker 	struct list_head *fn = &dreq->dreq_featneg;
9930c116839SGerrit Renker 	struct dccp_feat_entry *entry;
9940c116839SGerrit Renker 	u8 is_local, ccid;
9950c116839SGerrit Renker 
9960c116839SGerrit Renker 	for (is_local = 0; is_local <= 1; is_local++) {
9970c116839SGerrit Renker 		entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
9980c116839SGerrit Renker 
9990c116839SGerrit Renker 		if (entry != NULL && !entry->empty_confirm)
10000c116839SGerrit Renker 			ccid = entry->val.sp.vec[0];
10010c116839SGerrit Renker 		else
10020c116839SGerrit Renker 			ccid = dccp_feat_default_value(DCCPF_CCID);
10030c116839SGerrit Renker 
10040c116839SGerrit Renker 		if (dccp_feat_propagate_ccid(fn, ccid, is_local))
10050c116839SGerrit Renker 			return -1;
10060c116839SGerrit Renker 	}
10070c116839SGerrit Renker 	return 0;
10080c116839SGerrit Renker }
10090c116839SGerrit Renker 
101075757a7dSGerrit Renker /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
101175757a7dSGerrit Renker static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
101275757a7dSGerrit Renker {
101375757a7dSGerrit Renker 	u8 c, s;
101475757a7dSGerrit Renker 
101575757a7dSGerrit Renker 	for (s = 0; s < slen; s++)
101675757a7dSGerrit Renker 		for (c = 0; c < clen; c++)
101775757a7dSGerrit Renker 			if (servlist[s] == clilist[c])
101875757a7dSGerrit Renker 				return servlist[s];
101975757a7dSGerrit Renker 	return -1;
102075757a7dSGerrit Renker }
102175757a7dSGerrit Renker 
102275757a7dSGerrit Renker /**
102375757a7dSGerrit Renker  * dccp_feat_prefer  -  Move preferred entry to the start of array
102475757a7dSGerrit Renker  * Reorder the @array_len elements in @array so that @preferred_value comes
102575757a7dSGerrit Renker  * first. Returns >0 to indicate that @preferred_value does occur in @array.
102675757a7dSGerrit Renker  */
102775757a7dSGerrit Renker static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
102875757a7dSGerrit Renker {
102975757a7dSGerrit Renker 	u8 i, does_occur = 0;
103075757a7dSGerrit Renker 
103175757a7dSGerrit Renker 	if (array != NULL) {
103275757a7dSGerrit Renker 		for (i = 0; i < array_len; i++)
103375757a7dSGerrit Renker 			if (array[i] == preferred_value) {
103475757a7dSGerrit Renker 				array[i] = array[0];
103575757a7dSGerrit Renker 				does_occur++;
103675757a7dSGerrit Renker 			}
103775757a7dSGerrit Renker 		if (does_occur)
103875757a7dSGerrit Renker 			array[0] = preferred_value;
103975757a7dSGerrit Renker 	}
104075757a7dSGerrit Renker 	return does_occur;
104175757a7dSGerrit Renker }
104275757a7dSGerrit Renker 
104375757a7dSGerrit Renker /**
104475757a7dSGerrit Renker  * dccp_feat_reconcile  -  Reconcile SP preference lists
104575757a7dSGerrit Renker  *  @fval: SP list to reconcile into
104675757a7dSGerrit Renker  *  @arr: received SP preference list
104775757a7dSGerrit Renker  *  @len: length of @arr in bytes
104875757a7dSGerrit Renker  *  @is_server: whether this side is the server (and @fv is the server's list)
104975757a7dSGerrit Renker  *  @reorder: whether to reorder the list in @fv after reconciling with @arr
105075757a7dSGerrit Renker  * When successful, > 0 is returned and the reconciled list is in @fval.
105175757a7dSGerrit Renker  * A value of 0 means that negotiation failed (no shared entry).
105275757a7dSGerrit Renker  */
105375757a7dSGerrit Renker static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
105475757a7dSGerrit Renker 			       bool is_server, bool reorder)
105575757a7dSGerrit Renker {
105675757a7dSGerrit Renker 	int rc;
105775757a7dSGerrit Renker 
105875757a7dSGerrit Renker 	if (!fv->sp.vec || !arr) {
105975757a7dSGerrit Renker 		DCCP_CRIT("NULL feature value or array");
106075757a7dSGerrit Renker 		return 0;
106175757a7dSGerrit Renker 	}
106275757a7dSGerrit Renker 
106375757a7dSGerrit Renker 	if (is_server)
106475757a7dSGerrit Renker 		rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
106575757a7dSGerrit Renker 	else
106675757a7dSGerrit Renker 		rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
106775757a7dSGerrit Renker 
106875757a7dSGerrit Renker 	if (!reorder)
106975757a7dSGerrit Renker 		return rc;
107075757a7dSGerrit Renker 	if (rc < 0)
107175757a7dSGerrit Renker 		return 0;
107275757a7dSGerrit Renker 
107375757a7dSGerrit Renker 	/*
107475757a7dSGerrit Renker 	 * Reorder list: used for activating features and in dccp_insert_fn_opt.
107575757a7dSGerrit Renker 	 */
107675757a7dSGerrit Renker 	return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
107775757a7dSGerrit Renker }
107875757a7dSGerrit Renker 
1079e77b8363SGerrit Renker /**
1080e77b8363SGerrit Renker  * dccp_feat_change_recv  -  Process incoming ChangeL/R options
1081e77b8363SGerrit Renker  * @fn: feature-negotiation list to update
1082e77b8363SGerrit Renker  * @is_mandatory: whether the Change was preceded by a Mandatory option
1083e77b8363SGerrit Renker  * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
1084e77b8363SGerrit Renker  * @feat: one of %dccp_feature_numbers
1085e77b8363SGerrit Renker  * @val: NN value or SP value/preference list
1086e77b8363SGerrit Renker  * @len: length of @val in bytes
1087e77b8363SGerrit Renker  * @server: whether this node is the server (1) or the client (0)
1088e77b8363SGerrit Renker  */
1089e77b8363SGerrit Renker static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1090e77b8363SGerrit Renker 				u8 feat, u8 *val, u8 len, const bool server)
1091e77b8363SGerrit Renker {
1092e77b8363SGerrit Renker 	u8 defval, type = dccp_feat_type(feat);
1093e77b8363SGerrit Renker 	const bool local = (opt == DCCPO_CHANGE_R);
1094e77b8363SGerrit Renker 	struct dccp_feat_entry *entry;
1095e77b8363SGerrit Renker 	dccp_feat_val fval;
1096e77b8363SGerrit Renker 
1097e77b8363SGerrit Renker 	if (len == 0 || type == FEAT_UNKNOWN)		/* 6.1 and 6.6.8 */
1098e77b8363SGerrit Renker 		goto unknown_feature_or_value;
1099e77b8363SGerrit Renker 
1100f3f3abb6SGerrit Renker 	dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1101f3f3abb6SGerrit Renker 
1102e77b8363SGerrit Renker 	/*
1103e77b8363SGerrit Renker 	 *	Negotiation of NN features: Change R is invalid, so there is no
1104e77b8363SGerrit Renker 	 *	simultaneous negotiation; hence we do not look up in the list.
1105e77b8363SGerrit Renker 	 */
1106e77b8363SGerrit Renker 	if (type == FEAT_NN) {
1107e77b8363SGerrit Renker 		if (local || len > sizeof(fval.nn))
1108e77b8363SGerrit Renker 			goto unknown_feature_or_value;
1109e77b8363SGerrit Renker 
1110e77b8363SGerrit Renker 		/* 6.3.2: "The feature remote MUST accept any valid value..." */
1111e77b8363SGerrit Renker 		fval.nn = dccp_decode_value_var(val, len);
1112e77b8363SGerrit Renker 		if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1113e77b8363SGerrit Renker 			goto unknown_feature_or_value;
1114e77b8363SGerrit Renker 
1115e77b8363SGerrit Renker 		return dccp_feat_push_confirm(fn, feat, local, &fval);
1116e77b8363SGerrit Renker 	}
1117e77b8363SGerrit Renker 
1118e77b8363SGerrit Renker 	/*
1119e77b8363SGerrit Renker 	 *	Unidirectional/simultaneous negotiation of SP features (6.3.1)
1120e77b8363SGerrit Renker 	 */
1121e77b8363SGerrit Renker 	entry = dccp_feat_list_lookup(fn, feat, local);
1122e77b8363SGerrit Renker 	if (entry == NULL) {
1123e77b8363SGerrit Renker 		/*
1124e77b8363SGerrit Renker 		 * No particular preferences have been registered. We deal with
1125e77b8363SGerrit Renker 		 * this situation by assuming that all valid values are equally
1126e77b8363SGerrit Renker 		 * acceptable, and apply the following checks:
1127e77b8363SGerrit Renker 		 * - if the peer's list is a singleton, we accept a valid value;
1128e77b8363SGerrit Renker 		 * - if we are the server, we first try to see if the peer (the
1129e77b8363SGerrit Renker 		 *   client) advertises the default value. If yes, we use it,
1130e77b8363SGerrit Renker 		 *   otherwise we accept the preferred value;
1131e77b8363SGerrit Renker 		 * - else if we are the client, we use the first list element.
1132e77b8363SGerrit Renker 		 */
1133e77b8363SGerrit Renker 		if (dccp_feat_clone_sp_val(&fval, val, 1))
1134e77b8363SGerrit Renker 			return DCCP_RESET_CODE_TOO_BUSY;
1135e77b8363SGerrit Renker 
1136e77b8363SGerrit Renker 		if (len > 1 && server) {
1137e77b8363SGerrit Renker 			defval = dccp_feat_default_value(feat);
1138e77b8363SGerrit Renker 			if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
1139e77b8363SGerrit Renker 				fval.sp.vec[0] = defval;
1140e77b8363SGerrit Renker 		} else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
1141e77b8363SGerrit Renker 			kfree(fval.sp.vec);
1142e77b8363SGerrit Renker 			goto unknown_feature_or_value;
1143e77b8363SGerrit Renker 		}
1144e77b8363SGerrit Renker 
1145e77b8363SGerrit Renker 		/* Treat unsupported CCIDs like invalid values */
1146e77b8363SGerrit Renker 		if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
1147e77b8363SGerrit Renker 			kfree(fval.sp.vec);
1148e77b8363SGerrit Renker 			goto not_valid_or_not_known;
1149e77b8363SGerrit Renker 		}
1150e77b8363SGerrit Renker 
1151e77b8363SGerrit Renker 		return dccp_feat_push_confirm(fn, feat, local, &fval);
1152e77b8363SGerrit Renker 
1153e77b8363SGerrit Renker 	} else if (entry->state == FEAT_UNSTABLE) {	/* 6.6.2 */
1154e77b8363SGerrit Renker 		return 0;
1155e77b8363SGerrit Renker 	}
1156e77b8363SGerrit Renker 
1157e77b8363SGerrit Renker 	if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
1158*3db1cd5cSRusty Russell 		entry->empty_confirm = false;
1159e77b8363SGerrit Renker 	} else if (is_mandatory) {
1160e77b8363SGerrit Renker 		return DCCP_RESET_CODE_MANDATORY_ERROR;
1161e77b8363SGerrit Renker 	} else if (entry->state == FEAT_INITIALISING) {
1162e77b8363SGerrit Renker 		/*
1163e77b8363SGerrit Renker 		 * Failed simultaneous negotiation (server only): try to `save'
1164e77b8363SGerrit Renker 		 * the connection by checking whether entry contains the default
1165e77b8363SGerrit Renker 		 * value for @feat. If yes, send an empty Confirm to signal that
1166e77b8363SGerrit Renker 		 * the received Change was not understood - which implies using
1167e77b8363SGerrit Renker 		 * the default value.
1168e77b8363SGerrit Renker 		 * If this also fails, we use Reset as the last resort.
1169e77b8363SGerrit Renker 		 */
1170e77b8363SGerrit Renker 		WARN_ON(!server);
1171e77b8363SGerrit Renker 		defval = dccp_feat_default_value(feat);
1172e77b8363SGerrit Renker 		if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
1173e77b8363SGerrit Renker 			return DCCP_RESET_CODE_OPTION_ERROR;
1174*3db1cd5cSRusty Russell 		entry->empty_confirm = true;
1175e77b8363SGerrit Renker 	}
1176*3db1cd5cSRusty Russell 	entry->needs_confirm   = true;
1177*3db1cd5cSRusty Russell 	entry->needs_mandatory = false;
1178e77b8363SGerrit Renker 	entry->state	       = FEAT_STABLE;
1179e77b8363SGerrit Renker 	return 0;
1180e77b8363SGerrit Renker 
1181e77b8363SGerrit Renker unknown_feature_or_value:
1182e77b8363SGerrit Renker 	if (!is_mandatory)
1183e77b8363SGerrit Renker 		return dccp_push_empty_confirm(fn, feat, local);
1184e77b8363SGerrit Renker 
1185e77b8363SGerrit Renker not_valid_or_not_known:
1186e77b8363SGerrit Renker 	return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1187e77b8363SGerrit Renker 			    : DCCP_RESET_CODE_OPTION_ERROR;
1188e77b8363SGerrit Renker }
1189e77b8363SGerrit Renker 
1190e77b8363SGerrit Renker /**
1191b1ad0042SGerrit Renker  * dccp_feat_confirm_recv  -  Process received Confirm options
1192b1ad0042SGerrit Renker  * @fn: feature-negotiation list to update
1193b1ad0042SGerrit Renker  * @is_mandatory: whether @opt was preceded by a Mandatory option
1194b1ad0042SGerrit Renker  * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
1195b1ad0042SGerrit Renker  * @feat: one of %dccp_feature_numbers
1196b1ad0042SGerrit Renker  * @val: NN value or SP value/preference list
1197b1ad0042SGerrit Renker  * @len: length of @val in bytes
1198b1ad0042SGerrit Renker  * @server: whether this node is server (1) or client (0)
1199b1ad0042SGerrit Renker  */
1200b1ad0042SGerrit Renker static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1201b1ad0042SGerrit Renker 				 u8 feat, u8 *val, u8 len, const bool server)
1202b1ad0042SGerrit Renker {
1203b1ad0042SGerrit Renker 	u8 *plist, plen, type = dccp_feat_type(feat);
1204b1ad0042SGerrit Renker 	const bool local = (opt == DCCPO_CONFIRM_R);
1205b1ad0042SGerrit Renker 	struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1206b1ad0042SGerrit Renker 
1207f3f3abb6SGerrit Renker 	dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1208f3f3abb6SGerrit Renker 
1209b1ad0042SGerrit Renker 	if (entry == NULL) {	/* nothing queued: ignore or handle error */
1210b1ad0042SGerrit Renker 		if (is_mandatory && type == FEAT_UNKNOWN)
1211b1ad0042SGerrit Renker 			return DCCP_RESET_CODE_MANDATORY_ERROR;
1212b1ad0042SGerrit Renker 
1213b1ad0042SGerrit Renker 		if (!local && type == FEAT_NN)		/* 6.3.2 */
1214b1ad0042SGerrit Renker 			goto confirmation_failed;
1215b1ad0042SGerrit Renker 		return 0;
1216b1ad0042SGerrit Renker 	}
1217b1ad0042SGerrit Renker 
1218b1ad0042SGerrit Renker 	if (entry->state != FEAT_CHANGING)		/* 6.6.2 */
1219b1ad0042SGerrit Renker 		return 0;
1220b1ad0042SGerrit Renker 
1221b1ad0042SGerrit Renker 	if (len == 0) {
1222b1ad0042SGerrit Renker 		if (dccp_feat_must_be_understood(feat))	/* 6.6.7 */
1223b1ad0042SGerrit Renker 			goto confirmation_failed;
1224b1ad0042SGerrit Renker 		/*
1225b1ad0042SGerrit Renker 		 * Empty Confirm during connection setup: this means reverting
1226b1ad0042SGerrit Renker 		 * to the `old' value, which in this case is the default. Since
1227b1ad0042SGerrit Renker 		 * we handle default values automatically when no other values
1228b1ad0042SGerrit Renker 		 * have been set, we revert to the old value by removing this
1229b1ad0042SGerrit Renker 		 * entry from the list.
1230b1ad0042SGerrit Renker 		 */
1231b1ad0042SGerrit Renker 		dccp_feat_list_pop(entry);
1232b1ad0042SGerrit Renker 		return 0;
1233b1ad0042SGerrit Renker 	}
1234b1ad0042SGerrit Renker 
1235b1ad0042SGerrit Renker 	if (type == FEAT_NN) {
1236b1ad0042SGerrit Renker 		if (len > sizeof(entry->val.nn))
1237b1ad0042SGerrit Renker 			goto confirmation_failed;
1238b1ad0042SGerrit Renker 
1239b1ad0042SGerrit Renker 		if (entry->val.nn == dccp_decode_value_var(val, len))
1240b1ad0042SGerrit Renker 			goto confirmation_succeeded;
1241b1ad0042SGerrit Renker 
1242b1ad0042SGerrit Renker 		DCCP_WARN("Bogus Confirm for non-existing value\n");
1243b1ad0042SGerrit Renker 		goto confirmation_failed;
1244b1ad0042SGerrit Renker 	}
1245b1ad0042SGerrit Renker 
1246b1ad0042SGerrit Renker 	/*
1247b1ad0042SGerrit Renker 	 * Parsing SP Confirms: the first element of @val is the preferred
1248b1ad0042SGerrit Renker 	 * SP value which the peer confirms, the remainder depends on @len.
1249b1ad0042SGerrit Renker 	 * Note that only the confirmed value need to be a valid SP value.
1250b1ad0042SGerrit Renker 	 */
1251b1ad0042SGerrit Renker 	if (!dccp_feat_is_valid_sp_val(feat, *val))
1252b1ad0042SGerrit Renker 		goto confirmation_failed;
1253b1ad0042SGerrit Renker 
1254b1ad0042SGerrit Renker 	if (len == 1) {		/* peer didn't supply a preference list */
1255b1ad0042SGerrit Renker 		plist = val;
1256b1ad0042SGerrit Renker 		plen  = len;
1257b1ad0042SGerrit Renker 	} else {		/* preferred value + preference list */
1258b1ad0042SGerrit Renker 		plist = val + 1;
1259b1ad0042SGerrit Renker 		plen  = len - 1;
1260b1ad0042SGerrit Renker 	}
1261b1ad0042SGerrit Renker 
1262b1ad0042SGerrit Renker 	/* Check whether the peer got the reconciliation right (6.6.8) */
1263b1ad0042SGerrit Renker 	if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1264b1ad0042SGerrit Renker 		DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1265b1ad0042SGerrit Renker 		return DCCP_RESET_CODE_OPTION_ERROR;
1266b1ad0042SGerrit Renker 	}
1267b1ad0042SGerrit Renker 	entry->val.sp.vec[0] = *val;
1268b1ad0042SGerrit Renker 
1269b1ad0042SGerrit Renker confirmation_succeeded:
1270b1ad0042SGerrit Renker 	entry->state = FEAT_STABLE;
1271b1ad0042SGerrit Renker 	return 0;
1272b1ad0042SGerrit Renker 
1273b1ad0042SGerrit Renker confirmation_failed:
1274b1ad0042SGerrit Renker 	DCCP_WARN("Confirmation failed\n");
1275b1ad0042SGerrit Renker 	return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1276b1ad0042SGerrit Renker 			    : DCCP_RESET_CODE_OPTION_ERROR;
1277b1ad0042SGerrit Renker }
1278b1ad0042SGerrit Renker 
1279b1ad0042SGerrit Renker /**
128044e6fd9eSGerrit Renker  * dccp_feat_handle_nn_established  -  Fast-path reception of NN options
128144e6fd9eSGerrit Renker  * @sk:		socket of an established DCCP connection
128244e6fd9eSGerrit Renker  * @mandatory:	whether @opt was preceded by a Mandatory option
128344e6fd9eSGerrit Renker  * @opt:	%DCCPO_CHANGE_L | %DCCPO_CONFIRM_R (NN only)
128444e6fd9eSGerrit Renker  * @feat:	NN number, one of %dccp_feature_numbers
128544e6fd9eSGerrit Renker  * @val:	NN value
128644e6fd9eSGerrit Renker  * @len:	length of @val in bytes
128744e6fd9eSGerrit Renker  * This function combines the functionality of change_recv/confirm_recv, with
128844e6fd9eSGerrit Renker  * the following differences (reset codes are the same):
128944e6fd9eSGerrit Renker  *    - cleanup after receiving the Confirm;
129044e6fd9eSGerrit Renker  *    - values are directly activated after successful parsing;
129144e6fd9eSGerrit Renker  *    - deliberately restricted to NN features.
129244e6fd9eSGerrit Renker  * The restriction to NN features is essential since SP features can have non-
129344e6fd9eSGerrit Renker  * predictable outcomes (depending on the remote configuration), and are inter-
129444e6fd9eSGerrit Renker  * dependent (CCIDs for instance cause further dependencies).
129544e6fd9eSGerrit Renker  */
129644e6fd9eSGerrit Renker static u8 dccp_feat_handle_nn_established(struct sock *sk, u8 mandatory, u8 opt,
129744e6fd9eSGerrit Renker 					  u8 feat, u8 *val, u8 len)
129844e6fd9eSGerrit Renker {
129944e6fd9eSGerrit Renker 	struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
130044e6fd9eSGerrit Renker 	const bool local = (opt == DCCPO_CONFIRM_R);
130144e6fd9eSGerrit Renker 	struct dccp_feat_entry *entry;
130244e6fd9eSGerrit Renker 	u8 type = dccp_feat_type(feat);
130344e6fd9eSGerrit Renker 	dccp_feat_val fval;
130444e6fd9eSGerrit Renker 
130544e6fd9eSGerrit Renker 	dccp_feat_print_opt(opt, feat, val, len, mandatory);
130644e6fd9eSGerrit Renker 
130744e6fd9eSGerrit Renker 	/* Ignore non-mandatory unknown and non-NN features */
130844e6fd9eSGerrit Renker 	if (type == FEAT_UNKNOWN) {
130944e6fd9eSGerrit Renker 		if (local && !mandatory)
131044e6fd9eSGerrit Renker 			return 0;
131144e6fd9eSGerrit Renker 		goto fast_path_unknown;
131244e6fd9eSGerrit Renker 	} else if (type != FEAT_NN) {
131344e6fd9eSGerrit Renker 		return 0;
131444e6fd9eSGerrit Renker 	}
131544e6fd9eSGerrit Renker 
131644e6fd9eSGerrit Renker 	/*
131744e6fd9eSGerrit Renker 	 * We don't accept empty Confirms, since in fast-path feature
131844e6fd9eSGerrit Renker 	 * negotiation the values are enabled immediately after sending
131944e6fd9eSGerrit Renker 	 * the Change option.
132044e6fd9eSGerrit Renker 	 * Empty Changes on the other hand are invalid (RFC 4340, 6.1).
132144e6fd9eSGerrit Renker 	 */
132244e6fd9eSGerrit Renker 	if (len == 0 || len > sizeof(fval.nn))
132344e6fd9eSGerrit Renker 		goto fast_path_unknown;
132444e6fd9eSGerrit Renker 
132544e6fd9eSGerrit Renker 	if (opt == DCCPO_CHANGE_L) {
132644e6fd9eSGerrit Renker 		fval.nn = dccp_decode_value_var(val, len);
132744e6fd9eSGerrit Renker 		if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
132844e6fd9eSGerrit Renker 			goto fast_path_unknown;
132944e6fd9eSGerrit Renker 
133044e6fd9eSGerrit Renker 		if (dccp_feat_push_confirm(fn, feat, local, &fval) ||
133144e6fd9eSGerrit Renker 		    dccp_feat_activate(sk, feat, local, &fval))
133244e6fd9eSGerrit Renker 			return DCCP_RESET_CODE_TOO_BUSY;
133344e6fd9eSGerrit Renker 
133444e6fd9eSGerrit Renker 		/* set the `Ack Pending' flag to piggyback a Confirm */
133544e6fd9eSGerrit Renker 		inet_csk_schedule_ack(sk);
133644e6fd9eSGerrit Renker 
133744e6fd9eSGerrit Renker 	} else if (opt == DCCPO_CONFIRM_R) {
133844e6fd9eSGerrit Renker 		entry = dccp_feat_list_lookup(fn, feat, local);
133944e6fd9eSGerrit Renker 		if (entry == NULL || entry->state != FEAT_CHANGING)
134044e6fd9eSGerrit Renker 			return 0;
134144e6fd9eSGerrit Renker 
134244e6fd9eSGerrit Renker 		fval.nn = dccp_decode_value_var(val, len);
134344e6fd9eSGerrit Renker 		/*
134444e6fd9eSGerrit Renker 		 * Just ignore a value that doesn't match our current value.
134544e6fd9eSGerrit Renker 		 * If the option changes twice within two RTTs, then at least
134644e6fd9eSGerrit Renker 		 * one CONFIRM will be received for the old value after a
134744e6fd9eSGerrit Renker 		 * new CHANGE was sent.
134844e6fd9eSGerrit Renker 		 */
134944e6fd9eSGerrit Renker 		if (fval.nn != entry->val.nn)
135044e6fd9eSGerrit Renker 			return 0;
135144e6fd9eSGerrit Renker 
135244e6fd9eSGerrit Renker 		/* Only activate after receiving the Confirm option (6.6.1). */
135344e6fd9eSGerrit Renker 		dccp_feat_activate(sk, feat, local, &fval);
135444e6fd9eSGerrit Renker 
135544e6fd9eSGerrit Renker 		/* It has been confirmed - so remove the entry */
135644e6fd9eSGerrit Renker 		dccp_feat_list_pop(entry);
135744e6fd9eSGerrit Renker 
135844e6fd9eSGerrit Renker 	} else {
135944e6fd9eSGerrit Renker 		DCCP_WARN("Received illegal option %u\n", opt);
136044e6fd9eSGerrit Renker 		goto fast_path_failed;
136144e6fd9eSGerrit Renker 	}
136244e6fd9eSGerrit Renker 	return 0;
136344e6fd9eSGerrit Renker 
136444e6fd9eSGerrit Renker fast_path_unknown:
136544e6fd9eSGerrit Renker 	if (!mandatory)
136644e6fd9eSGerrit Renker 		return dccp_push_empty_confirm(fn, feat, local);
136744e6fd9eSGerrit Renker 
136844e6fd9eSGerrit Renker fast_path_failed:
136944e6fd9eSGerrit Renker 	return mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
137044e6fd9eSGerrit Renker 			 : DCCP_RESET_CODE_OPTION_ERROR;
137144e6fd9eSGerrit Renker }
137244e6fd9eSGerrit Renker 
137344e6fd9eSGerrit Renker /**
1374e77b8363SGerrit Renker  * dccp_feat_parse_options  -  Process Feature-Negotiation Options
1375e77b8363SGerrit Renker  * @sk: for general use and used by the client during connection setup
1376e77b8363SGerrit Renker  * @dreq: used by the server during connection setup
1377e77b8363SGerrit Renker  * @mandatory: whether @opt was preceded by a Mandatory option
1378e77b8363SGerrit Renker  * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1379e77b8363SGerrit Renker  * @feat: one of %dccp_feature_numbers
1380e77b8363SGerrit Renker  * @val: value contents of @opt
1381e77b8363SGerrit Renker  * @len: length of @val in bytes
1382e77b8363SGerrit Renker  * Returns 0 on success, a Reset code for ending the connection otherwise.
1383e77b8363SGerrit Renker  */
1384e77b8363SGerrit Renker int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1385e77b8363SGerrit Renker 			    u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1386e77b8363SGerrit Renker {
1387e77b8363SGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
1388e77b8363SGerrit Renker 	struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1389e77b8363SGerrit Renker 	bool server = false;
1390e77b8363SGerrit Renker 
1391e77b8363SGerrit Renker 	switch (sk->sk_state) {
1392e77b8363SGerrit Renker 	/*
1393e77b8363SGerrit Renker 	 *	Negotiation during connection setup
1394e77b8363SGerrit Renker 	 */
1395e77b8363SGerrit Renker 	case DCCP_LISTEN:
1396e77b8363SGerrit Renker 		server = true;			/* fall through */
1397e77b8363SGerrit Renker 	case DCCP_REQUESTING:
1398e77b8363SGerrit Renker 		switch (opt) {
1399e77b8363SGerrit Renker 		case DCCPO_CHANGE_L:
1400e77b8363SGerrit Renker 		case DCCPO_CHANGE_R:
1401e77b8363SGerrit Renker 			return dccp_feat_change_recv(fn, mandatory, opt, feat,
1402e77b8363SGerrit Renker 						     val, len, server);
1403b1ad0042SGerrit Renker 		case DCCPO_CONFIRM_R:
1404b1ad0042SGerrit Renker 		case DCCPO_CONFIRM_L:
1405b1ad0042SGerrit Renker 			return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1406b1ad0042SGerrit Renker 						      val, len, server);
1407e77b8363SGerrit Renker 		}
140844e6fd9eSGerrit Renker 		break;
140944e6fd9eSGerrit Renker 	/*
141044e6fd9eSGerrit Renker 	 *	Support for exchanging NN options on an established connection.
141144e6fd9eSGerrit Renker 	 */
141244e6fd9eSGerrit Renker 	case DCCP_OPEN:
141344e6fd9eSGerrit Renker 	case DCCP_PARTOPEN:
141444e6fd9eSGerrit Renker 		return dccp_feat_handle_nn_established(sk, mandatory, opt, feat,
141544e6fd9eSGerrit Renker 						       val, len);
1416e77b8363SGerrit Renker 	}
1417e77b8363SGerrit Renker 	return 0;	/* ignore FN options in all other states */
1418e77b8363SGerrit Renker }
1419e77b8363SGerrit Renker 
1420f90f92eeSGerrit Renker /**
1421f90f92eeSGerrit Renker  * dccp_feat_init  -  Seed feature negotiation with host-specific defaults
1422f90f92eeSGerrit Renker  * This initialises global defaults, depending on the value of the sysctls.
1423f90f92eeSGerrit Renker  * These can later be overridden by registering changes via setsockopt calls.
1424f90f92eeSGerrit Renker  * The last link in the chain is finalise_settings, to make sure that between
1425f90f92eeSGerrit Renker  * here and the start of actual feature negotiation no inconsistencies enter.
1426f90f92eeSGerrit Renker  *
1427f90f92eeSGerrit Renker  * All features not appearing below use either defaults or are otherwise
1428f90f92eeSGerrit Renker  * later adjusted through dccp_feat_finalise_settings().
1429f90f92eeSGerrit Renker  */
1430e8ef967aSGerrit Renker int dccp_feat_init(struct sock *sk)
1431afe00251SAndrea Bittau {
1432f90f92eeSGerrit Renker 	struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1433f90f92eeSGerrit Renker 	u8 on = 1, off = 0;
1434afe00251SAndrea Bittau 	int rc;
1435f90f92eeSGerrit Renker 	struct {
1436f90f92eeSGerrit Renker 		u8 *val;
1437f90f92eeSGerrit Renker 		u8 len;
1438f90f92eeSGerrit Renker 	} tx, rx;
1439afe00251SAndrea Bittau 
1440f90f92eeSGerrit Renker 	/* Non-negotiable (NN) features */
1441f90f92eeSGerrit Renker 	rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
1442883ca833SGerrit Renker 				    sysctl_dccp_sequence_window);
1443f90f92eeSGerrit Renker 	if (rc)
1444f90f92eeSGerrit Renker 		return rc;
1445afe00251SAndrea Bittau 
1446f90f92eeSGerrit Renker 	/* Server-priority (SP) features */
1447f90f92eeSGerrit Renker 
1448f90f92eeSGerrit Renker 	/* Advertise that short seqnos are not supported (7.6.1) */
1449f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1450f90f92eeSGerrit Renker 	if (rc)
1451f90f92eeSGerrit Renker 		return rc;
1452f90f92eeSGerrit Renker 
1453f90f92eeSGerrit Renker 	/* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
1454f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
1455f90f92eeSGerrit Renker 	if (rc)
1456f90f92eeSGerrit Renker 		return rc;
1457f90f92eeSGerrit Renker 
1458f90f92eeSGerrit Renker 	/*
1459f90f92eeSGerrit Renker 	 * We advertise the available list of CCIDs and reorder according to
1460f90f92eeSGerrit Renker 	 * preferences, to avoid failure resulting from negotiating different
1461f90f92eeSGerrit Renker 	 * singleton values (which always leads to failure).
1462f90f92eeSGerrit Renker 	 * These settings can still (later) be overridden via sockopts.
1463f90f92eeSGerrit Renker 	 */
1464f90f92eeSGerrit Renker 	if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1465f90f92eeSGerrit Renker 	    ccid_get_builtin_ccids(&rx.val, &rx.len))
1466f90f92eeSGerrit Renker 		return -ENOBUFS;
1467f90f92eeSGerrit Renker 
1468883ca833SGerrit Renker 	if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
1469883ca833SGerrit Renker 	    !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
1470f90f92eeSGerrit Renker 		goto free_ccid_lists;
1471f90f92eeSGerrit Renker 
1472f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
1473f90f92eeSGerrit Renker 	if (rc)
1474f90f92eeSGerrit Renker 		goto free_ccid_lists;
1475f90f92eeSGerrit Renker 
1476f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
1477f90f92eeSGerrit Renker 
1478f90f92eeSGerrit Renker free_ccid_lists:
1479f90f92eeSGerrit Renker 	kfree(tx.val);
1480f90f92eeSGerrit Renker 	kfree(rx.val);
1481afe00251SAndrea Bittau 	return rc;
1482afe00251SAndrea Bittau }
1483afe00251SAndrea Bittau 
1484422d9cdcSGerrit Renker int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1485422d9cdcSGerrit Renker {
1486422d9cdcSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
1487422d9cdcSGerrit Renker 	struct dccp_feat_entry *cur, *next;
1488422d9cdcSGerrit Renker 	int idx;
1489422d9cdcSGerrit Renker 	dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1490422d9cdcSGerrit Renker 		 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1491422d9cdcSGerrit Renker 	};
1492422d9cdcSGerrit Renker 
1493422d9cdcSGerrit Renker 	list_for_each_entry(cur, fn_list, node) {
1494422d9cdcSGerrit Renker 		/*
1495422d9cdcSGerrit Renker 		 * An empty Confirm means that either an unknown feature type
1496422d9cdcSGerrit Renker 		 * or an invalid value was present. In the first case there is
1497422d9cdcSGerrit Renker 		 * nothing to activate, in the other the default value is used.
1498422d9cdcSGerrit Renker 		 */
1499422d9cdcSGerrit Renker 		if (cur->empty_confirm)
1500422d9cdcSGerrit Renker 			continue;
1501422d9cdcSGerrit Renker 
1502422d9cdcSGerrit Renker 		idx = dccp_feat_index(cur->feat_num);
1503422d9cdcSGerrit Renker 		if (idx < 0) {
1504422d9cdcSGerrit Renker 			DCCP_BUG("Unknown feature %u", cur->feat_num);
1505422d9cdcSGerrit Renker 			goto activation_failed;
1506422d9cdcSGerrit Renker 		}
1507422d9cdcSGerrit Renker 		if (cur->state != FEAT_STABLE) {
1508f3f3abb6SGerrit Renker 			DCCP_CRIT("Negotiation of %s %s failed in state %s",
1509422d9cdcSGerrit Renker 				  cur->is_local ? "local" : "remote",
1510f3f3abb6SGerrit Renker 				  dccp_feat_fname(cur->feat_num),
1511f3f3abb6SGerrit Renker 				  dccp_feat_sname[cur->state]);
1512422d9cdcSGerrit Renker 			goto activation_failed;
1513422d9cdcSGerrit Renker 		}
1514422d9cdcSGerrit Renker 		fvals[idx][cur->is_local] = &cur->val;
1515422d9cdcSGerrit Renker 	}
1516422d9cdcSGerrit Renker 
1517422d9cdcSGerrit Renker 	/*
1518422d9cdcSGerrit Renker 	 * Activate in decreasing order of index, so that the CCIDs are always
1519422d9cdcSGerrit Renker 	 * activated as the last feature. This avoids the case where a CCID
1520422d9cdcSGerrit Renker 	 * relies on the initialisation of one or more features that it depends
1521422d9cdcSGerrit Renker 	 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1522422d9cdcSGerrit Renker 	 */
1523422d9cdcSGerrit Renker 	for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1524422d9cdcSGerrit Renker 		if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1525422d9cdcSGerrit Renker 		    __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1526422d9cdcSGerrit Renker 			DCCP_CRIT("Could not activate %d", idx);
1527422d9cdcSGerrit Renker 			goto activation_failed;
1528422d9cdcSGerrit Renker 		}
1529422d9cdcSGerrit Renker 
1530422d9cdcSGerrit Renker 	/* Clean up Change options which have been confirmed already */
1531422d9cdcSGerrit Renker 	list_for_each_entry_safe(cur, next, fn_list, node)
1532422d9cdcSGerrit Renker 		if (!cur->needs_confirm)
1533422d9cdcSGerrit Renker 			dccp_feat_list_pop(cur);
1534422d9cdcSGerrit Renker 
1535422d9cdcSGerrit Renker 	dccp_pr_debug("Activation OK\n");
1536422d9cdcSGerrit Renker 	return 0;
1537422d9cdcSGerrit Renker 
1538422d9cdcSGerrit Renker activation_failed:
1539422d9cdcSGerrit Renker 	/*
1540422d9cdcSGerrit Renker 	 * We clean up everything that may have been allocated, since
1541422d9cdcSGerrit Renker 	 * it is difficult to track at which stage negotiation failed.
1542422d9cdcSGerrit Renker 	 * This is ok, since all allocation functions below are robust
1543422d9cdcSGerrit Renker 	 * against NULL arguments.
1544422d9cdcSGerrit Renker 	 */
1545422d9cdcSGerrit Renker 	ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1546422d9cdcSGerrit Renker 	ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1547422d9cdcSGerrit Renker 	dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1548422d9cdcSGerrit Renker 	dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1549422d9cdcSGerrit Renker 	dp->dccps_hc_rx_ackvec = NULL;
1550422d9cdcSGerrit Renker 	return -1;
1551422d9cdcSGerrit Renker }
1552