xref: /openbmc/linux/net/dccp/feat.c (revision 44e6fd9e67c1043aaeed381c10b74e73807b7f26)
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 
347*44e6fd9eSGerrit Renker /**
348*44e6fd9eSGerrit Renker  * dccp_feat_activate  -  Activate feature value on socket
349*44e6fd9eSGerrit Renker  * @sk: fully connected DCCP socket (after handshake is complete)
350*44e6fd9eSGerrit Renker  * @feat_num: feature to activate, one of %dccp_feature_numbers
351*44e6fd9eSGerrit Renker  * @local: whether local (1) or remote (0) @feat_num is meant
352*44e6fd9eSGerrit Renker  * @fval: the value (SP or NN) to activate, or NULL to use the default value
353*44e6fd9eSGerrit Renker  * For general use this function is preferable over __dccp_feat_activate().
354*44e6fd9eSGerrit Renker  */
355*44e6fd9eSGerrit Renker static int dccp_feat_activate(struct sock *sk, u8 feat_num, bool local,
356*44e6fd9eSGerrit Renker 			      dccp_feat_val const *fval)
357*44e6fd9eSGerrit Renker {
358*44e6fd9eSGerrit Renker 	return __dccp_feat_activate(sk, dccp_feat_index(feat_num), local, fval);
359*44e6fd9eSGerrit Renker }
360*44e6fd9eSGerrit 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;
493e8ef967aSGerrit Renker 	new->needs_confirm   = 0;
494e8ef967aSGerrit Renker 	new->empty_confirm   = 0;
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 */
520e77b8363SGerrit Renker 	new->needs_confirm   = 1;
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;
525e77b8363SGerrit Renker 	new->needs_mandatory = 0;
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;
6680971d17cSGerrit Renker 		/*
6690971d17cSGerrit Renker 		 * Enter CHANGING after transmitting the Change option (6.6.2).
6700971d17cSGerrit Renker 		 */
6710971d17cSGerrit Renker 		if (pos->state == FEAT_INITIALISING)
6720971d17cSGerrit Renker 			pos->state = FEAT_CHANGING;
6730971d17cSGerrit Renker 	}
6740971d17cSGerrit Renker 	return 0;
6750971d17cSGerrit Renker }
6760971d17cSGerrit Renker 
6770971d17cSGerrit Renker /**
678e8ef967aSGerrit Renker  * __feat_register_nn  -  Register new NN value on socket
679e8ef967aSGerrit Renker  * @fn: feature-negotiation list to register with
680e8ef967aSGerrit Renker  * @feat: an NN feature from %dccp_feature_numbers
681e8ef967aSGerrit Renker  * @mandatory: use Mandatory option if 1
682e8ef967aSGerrit Renker  * @nn_val: value to register (restricted to 4 bytes)
683e8ef967aSGerrit Renker  * Note that NN features are local by definition (RFC 4340, 6.3.2).
684e8ef967aSGerrit Renker  */
685e8ef967aSGerrit Renker static int __feat_register_nn(struct list_head *fn, u8 feat,
686e8ef967aSGerrit Renker 			      u8 mandatory, u64 nn_val)
687e8ef967aSGerrit Renker {
688e8ef967aSGerrit Renker 	dccp_feat_val fval = { .nn = nn_val };
689e8ef967aSGerrit Renker 
690e8ef967aSGerrit Renker 	if (dccp_feat_type(feat) != FEAT_NN ||
691e8ef967aSGerrit Renker 	    !dccp_feat_is_valid_nn_val(feat, nn_val))
692e8ef967aSGerrit Renker 		return -EINVAL;
693e8ef967aSGerrit Renker 
694e8ef967aSGerrit Renker 	/* Don't bother with default values, they will be activated anyway. */
695e8ef967aSGerrit Renker 	if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
696e8ef967aSGerrit Renker 		return 0;
697e8ef967aSGerrit Renker 
698e8ef967aSGerrit Renker 	return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
699e8ef967aSGerrit Renker }
700e8ef967aSGerrit Renker 
701e8ef967aSGerrit Renker /**
702e8ef967aSGerrit Renker  * __feat_register_sp  -  Register new SP value/list on socket
703e8ef967aSGerrit Renker  * @fn: feature-negotiation list to register with
704e8ef967aSGerrit Renker  * @feat: an SP feature from %dccp_feature_numbers
705e8ef967aSGerrit Renker  * @is_local: whether the local (1) or the remote (0) @feat is meant
706e8ef967aSGerrit Renker  * @mandatory: use Mandatory option if 1
707e8ef967aSGerrit Renker  * @sp_val: SP value followed by optional preference list
708e8ef967aSGerrit Renker  * @sp_len: length of @sp_val in bytes
709e8ef967aSGerrit Renker  */
710e8ef967aSGerrit Renker static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
711e8ef967aSGerrit Renker 			      u8 mandatory, u8 const *sp_val, u8 sp_len)
712e8ef967aSGerrit Renker {
713e8ef967aSGerrit Renker 	dccp_feat_val fval;
714e8ef967aSGerrit Renker 
715e8ef967aSGerrit Renker 	if (dccp_feat_type(feat) != FEAT_SP ||
716e8ef967aSGerrit Renker 	    !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
717e8ef967aSGerrit Renker 		return -EINVAL;
718e8ef967aSGerrit Renker 
719d90ebcbfSGerrit Renker 	/* Avoid negotiating alien CCIDs by only advertising supported ones */
720d90ebcbfSGerrit Renker 	if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
721d90ebcbfSGerrit Renker 		return -EOPNOTSUPP;
722d90ebcbfSGerrit Renker 
723e8ef967aSGerrit Renker 	if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
724e8ef967aSGerrit Renker 		return -ENOMEM;
725e8ef967aSGerrit Renker 
726e8ef967aSGerrit Renker 	return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
727e8ef967aSGerrit Renker }
728e8ef967aSGerrit Renker 
72949aebc66SGerrit Renker /**
73049aebc66SGerrit Renker  * dccp_feat_register_sp  -  Register requests to change SP feature values
73149aebc66SGerrit Renker  * @sk: client or listening socket
73249aebc66SGerrit Renker  * @feat: one of %dccp_feature_numbers
73349aebc66SGerrit Renker  * @is_local: whether the local (1) or remote (0) @feat is meant
73449aebc66SGerrit Renker  * @list: array of preferred values, in descending order of preference
73549aebc66SGerrit Renker  * @len: length of @list in bytes
73649aebc66SGerrit Renker  */
73749aebc66SGerrit Renker int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
73849aebc66SGerrit Renker 			  u8 const *list, u8 len)
73949aebc66SGerrit Renker {	 /* any changes must be registered before establishing the connection */
74049aebc66SGerrit Renker 	if (sk->sk_state != DCCP_CLOSED)
74149aebc66SGerrit Renker 		return -EISCONN;
74249aebc66SGerrit Renker 	if (dccp_feat_type(feat) != FEAT_SP)
74319443178SChris Wright 		return -EINVAL;
74449aebc66SGerrit Renker 	return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
74549aebc66SGerrit Renker 				  0, list, len);
746afe00251SAndrea Bittau }
747afe00251SAndrea Bittau 
748d6916f87SGerrit Renker /**
749d6916f87SGerrit Renker  * dccp_feat_nn_get  -  Query current/pending value of NN feature
750d6916f87SGerrit Renker  * @sk: DCCP socket of an established connection
751d6916f87SGerrit Renker  * @feat: NN feature number from %dccp_feature_numbers
752d6916f87SGerrit Renker  * For a known NN feature, returns value currently being negotiated, or
753d6916f87SGerrit Renker  * current (confirmed) value if no negotiation is going on.
754d6916f87SGerrit Renker  */
755d6916f87SGerrit Renker u64 dccp_feat_nn_get(struct sock *sk, u8 feat)
756d6916f87SGerrit Renker {
757d6916f87SGerrit Renker 	if (dccp_feat_type(feat) == FEAT_NN) {
758d6916f87SGerrit Renker 		struct dccp_sock *dp = dccp_sk(sk);
759d6916f87SGerrit Renker 		struct dccp_feat_entry *entry;
760d6916f87SGerrit Renker 
761d6916f87SGerrit Renker 		entry = dccp_feat_list_lookup(&dp->dccps_featneg, feat, 1);
762d6916f87SGerrit Renker 		if (entry != NULL)
763d6916f87SGerrit Renker 			return entry->val.nn;
764d6916f87SGerrit Renker 
765d6916f87SGerrit Renker 		switch (feat) {
766d6916f87SGerrit Renker 		case DCCPF_ACK_RATIO:
767d6916f87SGerrit Renker 			return dp->dccps_l_ack_ratio;
768d6916f87SGerrit Renker 		case DCCPF_SEQUENCE_WINDOW:
769d6916f87SGerrit Renker 			return dp->dccps_l_seq_win;
770d6916f87SGerrit Renker 		}
771d6916f87SGerrit Renker 	}
772d6916f87SGerrit Renker 	DCCP_BUG("attempt to look up unsupported feature %u", feat);
773d6916f87SGerrit Renker 	return 0;
774d6916f87SGerrit Renker }
775d6916f87SGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_nn_get);
776d6916f87SGerrit Renker 
777d6916f87SGerrit Renker /**
778d6916f87SGerrit Renker  * dccp_feat_signal_nn_change  -  Update NN values for an established connection
779d6916f87SGerrit Renker  * @sk: DCCP socket of an established connection
780d6916f87SGerrit Renker  * @feat: NN feature number from %dccp_feature_numbers
781d6916f87SGerrit Renker  * @nn_val: the new value to use
782d6916f87SGerrit Renker  * This function is used to communicate NN updates out-of-band.
783d6916f87SGerrit Renker  */
784d6916f87SGerrit Renker int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val)
785d6916f87SGerrit Renker {
786d6916f87SGerrit Renker 	struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
787d6916f87SGerrit Renker 	dccp_feat_val fval = { .nn = nn_val };
788d6916f87SGerrit Renker 	struct dccp_feat_entry *entry;
789d6916f87SGerrit Renker 
790d6916f87SGerrit Renker 	if (sk->sk_state != DCCP_OPEN && sk->sk_state != DCCP_PARTOPEN)
791d6916f87SGerrit Renker 		return 0;
792d6916f87SGerrit Renker 
793d6916f87SGerrit Renker 	if (dccp_feat_type(feat) != FEAT_NN ||
794d6916f87SGerrit Renker 	    !dccp_feat_is_valid_nn_val(feat, nn_val))
795d6916f87SGerrit Renker 		return -EINVAL;
796d6916f87SGerrit Renker 
797d6916f87SGerrit Renker 	if (nn_val == dccp_feat_nn_get(sk, feat))
798d6916f87SGerrit Renker 		return 0;	/* already set or negotiation under way */
799d6916f87SGerrit Renker 
800d6916f87SGerrit Renker 	entry = dccp_feat_list_lookup(fn, feat, 1);
801d6916f87SGerrit Renker 	if (entry != NULL) {
802d6916f87SGerrit Renker 		dccp_pr_debug("Clobbering existing NN entry %llu -> %llu\n",
803d6916f87SGerrit Renker 			      (unsigned long long)entry->val.nn,
804d6916f87SGerrit Renker 			      (unsigned long long)nn_val);
805d6916f87SGerrit Renker 		dccp_feat_list_pop(entry);
806d6916f87SGerrit Renker 	}
807d6916f87SGerrit Renker 
808d6916f87SGerrit Renker 	inet_csk_schedule_ack(sk);
809d6916f87SGerrit Renker 	return dccp_feat_push_change(fn, feat, 1, 0, &fval);
810d6916f87SGerrit Renker }
811d6916f87SGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_signal_nn_change);
812afe00251SAndrea Bittau 
8139eca0a47SGerrit Renker /*
8149eca0a47SGerrit Renker  *	Tracking features whose value depend on the choice of CCID
8159eca0a47SGerrit Renker  *
8169eca0a47SGerrit Renker  * This is designed with an extension in mind so that a list walk could be done
8179eca0a47SGerrit Renker  * before activating any features. However, the existing framework was found to
8189eca0a47SGerrit Renker  * work satisfactorily up until now, the automatic verification is left open.
8199eca0a47SGerrit Renker  * When adding new CCIDs, add a corresponding dependency table here.
8209eca0a47SGerrit Renker  */
8219eca0a47SGerrit Renker static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
8229eca0a47SGerrit Renker {
8239eca0a47SGerrit Renker 	static const struct ccid_dependency ccid2_dependencies[2][2] = {
8249eca0a47SGerrit Renker 		/*
8259eca0a47SGerrit Renker 		 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
8269eca0a47SGerrit Renker 		 * feature and Send Ack Vector is an RX feature, `is_local'
8279eca0a47SGerrit Renker 		 * needs to be reversed.
8289eca0a47SGerrit Renker 		 */
8299eca0a47SGerrit Renker 		{	/* Dependencies of the receiver-side (remote) CCID2 */
8309eca0a47SGerrit Renker 			{
8319eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
8329eca0a47SGerrit Renker 				.is_local	= true,
8339eca0a47SGerrit Renker 				.is_mandatory	= true,
8349eca0a47SGerrit Renker 				.val		= 1
8359eca0a47SGerrit Renker 			},
8369eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
8379eca0a47SGerrit Renker 		},
8389eca0a47SGerrit Renker 		{	/* Dependencies of the sender-side (local) CCID2 */
8399eca0a47SGerrit Renker 			{
8409eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
8419eca0a47SGerrit Renker 				.is_local	= false,
8429eca0a47SGerrit Renker 				.is_mandatory	= true,
8439eca0a47SGerrit Renker 				.val		= 1
8449eca0a47SGerrit Renker 			},
8459eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
8469eca0a47SGerrit Renker 		}
8479eca0a47SGerrit Renker 	};
8489eca0a47SGerrit Renker 	static const struct ccid_dependency ccid3_dependencies[2][5] = {
8499eca0a47SGerrit Renker 		{	/*
8509eca0a47SGerrit Renker 			 * Dependencies of the receiver-side CCID3
8519eca0a47SGerrit Renker 			 */
8529eca0a47SGerrit Renker 			{	/* locally disable Ack Vectors */
8539eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
8549eca0a47SGerrit Renker 				.is_local	= true,
8559eca0a47SGerrit Renker 				.is_mandatory	= false,
8569eca0a47SGerrit Renker 				.val		= 0
8579eca0a47SGerrit Renker 			},
8589eca0a47SGerrit Renker 			{	/* see below why Send Loss Event Rate is on */
8599eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_LEV_RATE,
8609eca0a47SGerrit Renker 				.is_local	= true,
8619eca0a47SGerrit Renker 				.is_mandatory	= true,
8629eca0a47SGerrit Renker 				.val		= 1
8639eca0a47SGerrit Renker 			},
8649eca0a47SGerrit Renker 			{	/* NDP Count is needed as per RFC 4342, 6.1.1 */
8659eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_NDP_COUNT,
8669eca0a47SGerrit Renker 				.is_local	= false,
8679eca0a47SGerrit Renker 				.is_mandatory	= true,
8689eca0a47SGerrit Renker 				.val		= 1
8699eca0a47SGerrit Renker 			},
8709eca0a47SGerrit Renker 			{ 0, 0, 0, 0 },
8719eca0a47SGerrit Renker 		},
8729eca0a47SGerrit Renker 		{	/*
8739eca0a47SGerrit Renker 			 * CCID3 at the TX side: we request that the HC-receiver
8749eca0a47SGerrit Renker 			 * will not send Ack Vectors (they will be ignored, so
8759eca0a47SGerrit Renker 			 * Mandatory is not set); we enable Send Loss Event Rate
8769eca0a47SGerrit Renker 			 * (Mandatory since the implementation does not support
8779eca0a47SGerrit Renker 			 * the Loss Intervals option of RFC 4342, 8.6).
8789eca0a47SGerrit Renker 			 * The last two options are for peer's information only.
8799eca0a47SGerrit Renker 			*/
8809eca0a47SGerrit Renker 			{
8819eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
8829eca0a47SGerrit Renker 				.is_local	= false,
8839eca0a47SGerrit Renker 				.is_mandatory	= false,
8849eca0a47SGerrit Renker 				.val		= 0
8859eca0a47SGerrit Renker 			},
8869eca0a47SGerrit Renker 			{
8879eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_LEV_RATE,
8889eca0a47SGerrit Renker 				.is_local	= false,
8899eca0a47SGerrit Renker 				.is_mandatory	= true,
8909eca0a47SGerrit Renker 				.val		= 1
8919eca0a47SGerrit Renker 			},
8929eca0a47SGerrit Renker 			{	/* this CCID does not support Ack Ratio */
8939eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_ACK_RATIO,
8949eca0a47SGerrit Renker 				.is_local	= true,
8959eca0a47SGerrit Renker 				.is_mandatory	= false,
8969eca0a47SGerrit Renker 				.val		= 0
8979eca0a47SGerrit Renker 			},
8989eca0a47SGerrit Renker 			{	/* tell receiver we are sending NDP counts */
8999eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_NDP_COUNT,
9009eca0a47SGerrit Renker 				.is_local	= true,
9019eca0a47SGerrit Renker 				.is_mandatory	= false,
9029eca0a47SGerrit Renker 				.val		= 1
9039eca0a47SGerrit Renker 			},
9049eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
9059eca0a47SGerrit Renker 		}
9069eca0a47SGerrit Renker 	};
9079eca0a47SGerrit Renker 	switch (ccid) {
9089eca0a47SGerrit Renker 	case DCCPC_CCID2:
9099eca0a47SGerrit Renker 		return ccid2_dependencies[is_local];
9109eca0a47SGerrit Renker 	case DCCPC_CCID3:
9119eca0a47SGerrit Renker 		return ccid3_dependencies[is_local];
9129eca0a47SGerrit Renker 	default:
9139eca0a47SGerrit Renker 		return NULL;
9149eca0a47SGerrit Renker 	}
9159eca0a47SGerrit Renker }
9169eca0a47SGerrit Renker 
9179eca0a47SGerrit Renker /**
9189eca0a47SGerrit Renker  * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
9199eca0a47SGerrit Renker  * @fn: feature-negotiation list to update
9209eca0a47SGerrit Renker  * @id: CCID number to track
9219eca0a47SGerrit Renker  * @is_local: whether TX CCID (1) or RX CCID (0) is meant
9229eca0a47SGerrit Renker  * This function needs to be called after registering all other features.
9239eca0a47SGerrit Renker  */
9249eca0a47SGerrit Renker static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
9259eca0a47SGerrit Renker {
9269eca0a47SGerrit Renker 	const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
9279eca0a47SGerrit Renker 	int i, rc = (table == NULL);
9289eca0a47SGerrit Renker 
9299eca0a47SGerrit Renker 	for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
9309eca0a47SGerrit Renker 		if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
9319eca0a47SGerrit Renker 			rc = __feat_register_sp(fn, table[i].dependent_feat,
9329eca0a47SGerrit Renker 						    table[i].is_local,
9339eca0a47SGerrit Renker 						    table[i].is_mandatory,
9349eca0a47SGerrit Renker 						    &table[i].val, 1);
9359eca0a47SGerrit Renker 		else
9369eca0a47SGerrit Renker 			rc = __feat_register_nn(fn, table[i].dependent_feat,
9379eca0a47SGerrit Renker 						    table[i].is_mandatory,
9389eca0a47SGerrit Renker 						    table[i].val);
9399eca0a47SGerrit Renker 	return rc;
9409eca0a47SGerrit Renker }
9419eca0a47SGerrit Renker 
9429eca0a47SGerrit Renker /**
9439eca0a47SGerrit Renker  * dccp_feat_finalise_settings  -  Finalise settings before starting negotiation
9449eca0a47SGerrit Renker  * @dp: client or listening socket (settings will be inherited)
9459eca0a47SGerrit Renker  * This is called after all registrations (socket initialisation, sysctls, and
9469eca0a47SGerrit Renker  * sockopt calls), and before sending the first packet containing Change options
9479eca0a47SGerrit Renker  * (ie. client-Request or server-Response), to ensure internal consistency.
9489eca0a47SGerrit Renker  */
9499eca0a47SGerrit Renker int dccp_feat_finalise_settings(struct dccp_sock *dp)
9509eca0a47SGerrit Renker {
9519eca0a47SGerrit Renker 	struct list_head *fn = &dp->dccps_featneg;
9529eca0a47SGerrit Renker 	struct dccp_feat_entry *entry;
9539eca0a47SGerrit Renker 	int i = 2, ccids[2] = { -1, -1 };
9549eca0a47SGerrit Renker 
9559eca0a47SGerrit Renker 	/*
9569eca0a47SGerrit Renker 	 * Propagating CCIDs:
9579eca0a47SGerrit Renker 	 * 1) not useful to propagate CCID settings if this host advertises more
9589eca0a47SGerrit Renker 	 *    than one CCID: the choice of CCID  may still change - if this is
9599eca0a47SGerrit Renker 	 *    the client, or if this is the server and the client sends
9609eca0a47SGerrit Renker 	 *    singleton CCID values.
9619eca0a47SGerrit Renker 	 * 2) since is that propagate_ccid changes the list, we defer changing
9629eca0a47SGerrit Renker 	 *    the sorted list until after the traversal.
9639eca0a47SGerrit Renker 	 */
9649eca0a47SGerrit Renker 	list_for_each_entry(entry, fn, node)
9659eca0a47SGerrit Renker 		if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
9669eca0a47SGerrit Renker 			ccids[entry->is_local] = entry->val.sp.vec[0];
9679eca0a47SGerrit Renker 	while (i--)
9689eca0a47SGerrit Renker 		if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
9699eca0a47SGerrit Renker 			return -1;
970f3f3abb6SGerrit Renker 	dccp_feat_print_fnlist(fn);
9719eca0a47SGerrit Renker 	return 0;
9729eca0a47SGerrit Renker }
9739eca0a47SGerrit Renker 
9740c116839SGerrit Renker /**
9750c116839SGerrit Renker  * dccp_feat_server_ccid_dependencies  -  Resolve CCID-dependent features
9760c116839SGerrit Renker  * It is the server which resolves the dependencies once the CCID has been
9770c116839SGerrit Renker  * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
9780c116839SGerrit Renker  */
9790c116839SGerrit Renker int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
9800c116839SGerrit Renker {
9810c116839SGerrit Renker 	struct list_head *fn = &dreq->dreq_featneg;
9820c116839SGerrit Renker 	struct dccp_feat_entry *entry;
9830c116839SGerrit Renker 	u8 is_local, ccid;
9840c116839SGerrit Renker 
9850c116839SGerrit Renker 	for (is_local = 0; is_local <= 1; is_local++) {
9860c116839SGerrit Renker 		entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
9870c116839SGerrit Renker 
9880c116839SGerrit Renker 		if (entry != NULL && !entry->empty_confirm)
9890c116839SGerrit Renker 			ccid = entry->val.sp.vec[0];
9900c116839SGerrit Renker 		else
9910c116839SGerrit Renker 			ccid = dccp_feat_default_value(DCCPF_CCID);
9920c116839SGerrit Renker 
9930c116839SGerrit Renker 		if (dccp_feat_propagate_ccid(fn, ccid, is_local))
9940c116839SGerrit Renker 			return -1;
9950c116839SGerrit Renker 	}
9960c116839SGerrit Renker 	return 0;
9970c116839SGerrit Renker }
9980c116839SGerrit Renker 
99975757a7dSGerrit Renker /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
100075757a7dSGerrit Renker static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
100175757a7dSGerrit Renker {
100275757a7dSGerrit Renker 	u8 c, s;
100375757a7dSGerrit Renker 
100475757a7dSGerrit Renker 	for (s = 0; s < slen; s++)
100575757a7dSGerrit Renker 		for (c = 0; c < clen; c++)
100675757a7dSGerrit Renker 			if (servlist[s] == clilist[c])
100775757a7dSGerrit Renker 				return servlist[s];
100875757a7dSGerrit Renker 	return -1;
100975757a7dSGerrit Renker }
101075757a7dSGerrit Renker 
101175757a7dSGerrit Renker /**
101275757a7dSGerrit Renker  * dccp_feat_prefer  -  Move preferred entry to the start of array
101375757a7dSGerrit Renker  * Reorder the @array_len elements in @array so that @preferred_value comes
101475757a7dSGerrit Renker  * first. Returns >0 to indicate that @preferred_value does occur in @array.
101575757a7dSGerrit Renker  */
101675757a7dSGerrit Renker static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
101775757a7dSGerrit Renker {
101875757a7dSGerrit Renker 	u8 i, does_occur = 0;
101975757a7dSGerrit Renker 
102075757a7dSGerrit Renker 	if (array != NULL) {
102175757a7dSGerrit Renker 		for (i = 0; i < array_len; i++)
102275757a7dSGerrit Renker 			if (array[i] == preferred_value) {
102375757a7dSGerrit Renker 				array[i] = array[0];
102475757a7dSGerrit Renker 				does_occur++;
102575757a7dSGerrit Renker 			}
102675757a7dSGerrit Renker 		if (does_occur)
102775757a7dSGerrit Renker 			array[0] = preferred_value;
102875757a7dSGerrit Renker 	}
102975757a7dSGerrit Renker 	return does_occur;
103075757a7dSGerrit Renker }
103175757a7dSGerrit Renker 
103275757a7dSGerrit Renker /**
103375757a7dSGerrit Renker  * dccp_feat_reconcile  -  Reconcile SP preference lists
103475757a7dSGerrit Renker  *  @fval: SP list to reconcile into
103575757a7dSGerrit Renker  *  @arr: received SP preference list
103675757a7dSGerrit Renker  *  @len: length of @arr in bytes
103775757a7dSGerrit Renker  *  @is_server: whether this side is the server (and @fv is the server's list)
103875757a7dSGerrit Renker  *  @reorder: whether to reorder the list in @fv after reconciling with @arr
103975757a7dSGerrit Renker  * When successful, > 0 is returned and the reconciled list is in @fval.
104075757a7dSGerrit Renker  * A value of 0 means that negotiation failed (no shared entry).
104175757a7dSGerrit Renker  */
104275757a7dSGerrit Renker static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
104375757a7dSGerrit Renker 			       bool is_server, bool reorder)
104475757a7dSGerrit Renker {
104575757a7dSGerrit Renker 	int rc;
104675757a7dSGerrit Renker 
104775757a7dSGerrit Renker 	if (!fv->sp.vec || !arr) {
104875757a7dSGerrit Renker 		DCCP_CRIT("NULL feature value or array");
104975757a7dSGerrit Renker 		return 0;
105075757a7dSGerrit Renker 	}
105175757a7dSGerrit Renker 
105275757a7dSGerrit Renker 	if (is_server)
105375757a7dSGerrit Renker 		rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
105475757a7dSGerrit Renker 	else
105575757a7dSGerrit Renker 		rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
105675757a7dSGerrit Renker 
105775757a7dSGerrit Renker 	if (!reorder)
105875757a7dSGerrit Renker 		return rc;
105975757a7dSGerrit Renker 	if (rc < 0)
106075757a7dSGerrit Renker 		return 0;
106175757a7dSGerrit Renker 
106275757a7dSGerrit Renker 	/*
106375757a7dSGerrit Renker 	 * Reorder list: used for activating features and in dccp_insert_fn_opt.
106475757a7dSGerrit Renker 	 */
106575757a7dSGerrit Renker 	return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
106675757a7dSGerrit Renker }
106775757a7dSGerrit Renker 
1068e77b8363SGerrit Renker /**
1069e77b8363SGerrit Renker  * dccp_feat_change_recv  -  Process incoming ChangeL/R options
1070e77b8363SGerrit Renker  * @fn: feature-negotiation list to update
1071e77b8363SGerrit Renker  * @is_mandatory: whether the Change was preceded by a Mandatory option
1072e77b8363SGerrit Renker  * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
1073e77b8363SGerrit Renker  * @feat: one of %dccp_feature_numbers
1074e77b8363SGerrit Renker  * @val: NN value or SP value/preference list
1075e77b8363SGerrit Renker  * @len: length of @val in bytes
1076e77b8363SGerrit Renker  * @server: whether this node is the server (1) or the client (0)
1077e77b8363SGerrit Renker  */
1078e77b8363SGerrit Renker static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1079e77b8363SGerrit Renker 				u8 feat, u8 *val, u8 len, const bool server)
1080e77b8363SGerrit Renker {
1081e77b8363SGerrit Renker 	u8 defval, type = dccp_feat_type(feat);
1082e77b8363SGerrit Renker 	const bool local = (opt == DCCPO_CHANGE_R);
1083e77b8363SGerrit Renker 	struct dccp_feat_entry *entry;
1084e77b8363SGerrit Renker 	dccp_feat_val fval;
1085e77b8363SGerrit Renker 
1086e77b8363SGerrit Renker 	if (len == 0 || type == FEAT_UNKNOWN)		/* 6.1 and 6.6.8 */
1087e77b8363SGerrit Renker 		goto unknown_feature_or_value;
1088e77b8363SGerrit Renker 
1089f3f3abb6SGerrit Renker 	dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1090f3f3abb6SGerrit Renker 
1091e77b8363SGerrit Renker 	/*
1092e77b8363SGerrit Renker 	 *	Negotiation of NN features: Change R is invalid, so there is no
1093e77b8363SGerrit Renker 	 *	simultaneous negotiation; hence we do not look up in the list.
1094e77b8363SGerrit Renker 	 */
1095e77b8363SGerrit Renker 	if (type == FEAT_NN) {
1096e77b8363SGerrit Renker 		if (local || len > sizeof(fval.nn))
1097e77b8363SGerrit Renker 			goto unknown_feature_or_value;
1098e77b8363SGerrit Renker 
1099e77b8363SGerrit Renker 		/* 6.3.2: "The feature remote MUST accept any valid value..." */
1100e77b8363SGerrit Renker 		fval.nn = dccp_decode_value_var(val, len);
1101e77b8363SGerrit Renker 		if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1102e77b8363SGerrit Renker 			goto unknown_feature_or_value;
1103e77b8363SGerrit Renker 
1104e77b8363SGerrit Renker 		return dccp_feat_push_confirm(fn, feat, local, &fval);
1105e77b8363SGerrit Renker 	}
1106e77b8363SGerrit Renker 
1107e77b8363SGerrit Renker 	/*
1108e77b8363SGerrit Renker 	 *	Unidirectional/simultaneous negotiation of SP features (6.3.1)
1109e77b8363SGerrit Renker 	 */
1110e77b8363SGerrit Renker 	entry = dccp_feat_list_lookup(fn, feat, local);
1111e77b8363SGerrit Renker 	if (entry == NULL) {
1112e77b8363SGerrit Renker 		/*
1113e77b8363SGerrit Renker 		 * No particular preferences have been registered. We deal with
1114e77b8363SGerrit Renker 		 * this situation by assuming that all valid values are equally
1115e77b8363SGerrit Renker 		 * acceptable, and apply the following checks:
1116e77b8363SGerrit Renker 		 * - if the peer's list is a singleton, we accept a valid value;
1117e77b8363SGerrit Renker 		 * - if we are the server, we first try to see if the peer (the
1118e77b8363SGerrit Renker 		 *   client) advertises the default value. If yes, we use it,
1119e77b8363SGerrit Renker 		 *   otherwise we accept the preferred value;
1120e77b8363SGerrit Renker 		 * - else if we are the client, we use the first list element.
1121e77b8363SGerrit Renker 		 */
1122e77b8363SGerrit Renker 		if (dccp_feat_clone_sp_val(&fval, val, 1))
1123e77b8363SGerrit Renker 			return DCCP_RESET_CODE_TOO_BUSY;
1124e77b8363SGerrit Renker 
1125e77b8363SGerrit Renker 		if (len > 1 && server) {
1126e77b8363SGerrit Renker 			defval = dccp_feat_default_value(feat);
1127e77b8363SGerrit Renker 			if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
1128e77b8363SGerrit Renker 				fval.sp.vec[0] = defval;
1129e77b8363SGerrit Renker 		} else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
1130e77b8363SGerrit Renker 			kfree(fval.sp.vec);
1131e77b8363SGerrit Renker 			goto unknown_feature_or_value;
1132e77b8363SGerrit Renker 		}
1133e77b8363SGerrit Renker 
1134e77b8363SGerrit Renker 		/* Treat unsupported CCIDs like invalid values */
1135e77b8363SGerrit Renker 		if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
1136e77b8363SGerrit Renker 			kfree(fval.sp.vec);
1137e77b8363SGerrit Renker 			goto not_valid_or_not_known;
1138e77b8363SGerrit Renker 		}
1139e77b8363SGerrit Renker 
1140e77b8363SGerrit Renker 		return dccp_feat_push_confirm(fn, feat, local, &fval);
1141e77b8363SGerrit Renker 
1142e77b8363SGerrit Renker 	} else if (entry->state == FEAT_UNSTABLE) {	/* 6.6.2 */
1143e77b8363SGerrit Renker 		return 0;
1144e77b8363SGerrit Renker 	}
1145e77b8363SGerrit Renker 
1146e77b8363SGerrit Renker 	if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
1147e77b8363SGerrit Renker 		entry->empty_confirm = 0;
1148e77b8363SGerrit Renker 	} else if (is_mandatory) {
1149e77b8363SGerrit Renker 		return DCCP_RESET_CODE_MANDATORY_ERROR;
1150e77b8363SGerrit Renker 	} else if (entry->state == FEAT_INITIALISING) {
1151e77b8363SGerrit Renker 		/*
1152e77b8363SGerrit Renker 		 * Failed simultaneous negotiation (server only): try to `save'
1153e77b8363SGerrit Renker 		 * the connection by checking whether entry contains the default
1154e77b8363SGerrit Renker 		 * value for @feat. If yes, send an empty Confirm to signal that
1155e77b8363SGerrit Renker 		 * the received Change was not understood - which implies using
1156e77b8363SGerrit Renker 		 * the default value.
1157e77b8363SGerrit Renker 		 * If this also fails, we use Reset as the last resort.
1158e77b8363SGerrit Renker 		 */
1159e77b8363SGerrit Renker 		WARN_ON(!server);
1160e77b8363SGerrit Renker 		defval = dccp_feat_default_value(feat);
1161e77b8363SGerrit Renker 		if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
1162e77b8363SGerrit Renker 			return DCCP_RESET_CODE_OPTION_ERROR;
1163e77b8363SGerrit Renker 		entry->empty_confirm = 1;
1164e77b8363SGerrit Renker 	}
1165e77b8363SGerrit Renker 	entry->needs_confirm   = 1;
1166e77b8363SGerrit Renker 	entry->needs_mandatory = 0;
1167e77b8363SGerrit Renker 	entry->state	       = FEAT_STABLE;
1168e77b8363SGerrit Renker 	return 0;
1169e77b8363SGerrit Renker 
1170e77b8363SGerrit Renker unknown_feature_or_value:
1171e77b8363SGerrit Renker 	if (!is_mandatory)
1172e77b8363SGerrit Renker 		return dccp_push_empty_confirm(fn, feat, local);
1173e77b8363SGerrit Renker 
1174e77b8363SGerrit Renker not_valid_or_not_known:
1175e77b8363SGerrit Renker 	return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1176e77b8363SGerrit Renker 			    : DCCP_RESET_CODE_OPTION_ERROR;
1177e77b8363SGerrit Renker }
1178e77b8363SGerrit Renker 
1179e77b8363SGerrit Renker /**
1180b1ad0042SGerrit Renker  * dccp_feat_confirm_recv  -  Process received Confirm options
1181b1ad0042SGerrit Renker  * @fn: feature-negotiation list to update
1182b1ad0042SGerrit Renker  * @is_mandatory: whether @opt was preceded by a Mandatory option
1183b1ad0042SGerrit Renker  * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
1184b1ad0042SGerrit Renker  * @feat: one of %dccp_feature_numbers
1185b1ad0042SGerrit Renker  * @val: NN value or SP value/preference list
1186b1ad0042SGerrit Renker  * @len: length of @val in bytes
1187b1ad0042SGerrit Renker  * @server: whether this node is server (1) or client (0)
1188b1ad0042SGerrit Renker  */
1189b1ad0042SGerrit Renker static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1190b1ad0042SGerrit Renker 				 u8 feat, u8 *val, u8 len, const bool server)
1191b1ad0042SGerrit Renker {
1192b1ad0042SGerrit Renker 	u8 *plist, plen, type = dccp_feat_type(feat);
1193b1ad0042SGerrit Renker 	const bool local = (opt == DCCPO_CONFIRM_R);
1194b1ad0042SGerrit Renker 	struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1195b1ad0042SGerrit Renker 
1196f3f3abb6SGerrit Renker 	dccp_feat_print_opt(opt, feat, val, len, is_mandatory);
1197f3f3abb6SGerrit Renker 
1198b1ad0042SGerrit Renker 	if (entry == NULL) {	/* nothing queued: ignore or handle error */
1199b1ad0042SGerrit Renker 		if (is_mandatory && type == FEAT_UNKNOWN)
1200b1ad0042SGerrit Renker 			return DCCP_RESET_CODE_MANDATORY_ERROR;
1201b1ad0042SGerrit Renker 
1202b1ad0042SGerrit Renker 		if (!local && type == FEAT_NN)		/* 6.3.2 */
1203b1ad0042SGerrit Renker 			goto confirmation_failed;
1204b1ad0042SGerrit Renker 		return 0;
1205b1ad0042SGerrit Renker 	}
1206b1ad0042SGerrit Renker 
1207b1ad0042SGerrit Renker 	if (entry->state != FEAT_CHANGING)		/* 6.6.2 */
1208b1ad0042SGerrit Renker 		return 0;
1209b1ad0042SGerrit Renker 
1210b1ad0042SGerrit Renker 	if (len == 0) {
1211b1ad0042SGerrit Renker 		if (dccp_feat_must_be_understood(feat))	/* 6.6.7 */
1212b1ad0042SGerrit Renker 			goto confirmation_failed;
1213b1ad0042SGerrit Renker 		/*
1214b1ad0042SGerrit Renker 		 * Empty Confirm during connection setup: this means reverting
1215b1ad0042SGerrit Renker 		 * to the `old' value, which in this case is the default. Since
1216b1ad0042SGerrit Renker 		 * we handle default values automatically when no other values
1217b1ad0042SGerrit Renker 		 * have been set, we revert to the old value by removing this
1218b1ad0042SGerrit Renker 		 * entry from the list.
1219b1ad0042SGerrit Renker 		 */
1220b1ad0042SGerrit Renker 		dccp_feat_list_pop(entry);
1221b1ad0042SGerrit Renker 		return 0;
1222b1ad0042SGerrit Renker 	}
1223b1ad0042SGerrit Renker 
1224b1ad0042SGerrit Renker 	if (type == FEAT_NN) {
1225b1ad0042SGerrit Renker 		if (len > sizeof(entry->val.nn))
1226b1ad0042SGerrit Renker 			goto confirmation_failed;
1227b1ad0042SGerrit Renker 
1228b1ad0042SGerrit Renker 		if (entry->val.nn == dccp_decode_value_var(val, len))
1229b1ad0042SGerrit Renker 			goto confirmation_succeeded;
1230b1ad0042SGerrit Renker 
1231b1ad0042SGerrit Renker 		DCCP_WARN("Bogus Confirm for non-existing value\n");
1232b1ad0042SGerrit Renker 		goto confirmation_failed;
1233b1ad0042SGerrit Renker 	}
1234b1ad0042SGerrit Renker 
1235b1ad0042SGerrit Renker 	/*
1236b1ad0042SGerrit Renker 	 * Parsing SP Confirms: the first element of @val is the preferred
1237b1ad0042SGerrit Renker 	 * SP value which the peer confirms, the remainder depends on @len.
1238b1ad0042SGerrit Renker 	 * Note that only the confirmed value need to be a valid SP value.
1239b1ad0042SGerrit Renker 	 */
1240b1ad0042SGerrit Renker 	if (!dccp_feat_is_valid_sp_val(feat, *val))
1241b1ad0042SGerrit Renker 		goto confirmation_failed;
1242b1ad0042SGerrit Renker 
1243b1ad0042SGerrit Renker 	if (len == 1) {		/* peer didn't supply a preference list */
1244b1ad0042SGerrit Renker 		plist = val;
1245b1ad0042SGerrit Renker 		plen  = len;
1246b1ad0042SGerrit Renker 	} else {		/* preferred value + preference list */
1247b1ad0042SGerrit Renker 		plist = val + 1;
1248b1ad0042SGerrit Renker 		plen  = len - 1;
1249b1ad0042SGerrit Renker 	}
1250b1ad0042SGerrit Renker 
1251b1ad0042SGerrit Renker 	/* Check whether the peer got the reconciliation right (6.6.8) */
1252b1ad0042SGerrit Renker 	if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1253b1ad0042SGerrit Renker 		DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1254b1ad0042SGerrit Renker 		return DCCP_RESET_CODE_OPTION_ERROR;
1255b1ad0042SGerrit Renker 	}
1256b1ad0042SGerrit Renker 	entry->val.sp.vec[0] = *val;
1257b1ad0042SGerrit Renker 
1258b1ad0042SGerrit Renker confirmation_succeeded:
1259b1ad0042SGerrit Renker 	entry->state = FEAT_STABLE;
1260b1ad0042SGerrit Renker 	return 0;
1261b1ad0042SGerrit Renker 
1262b1ad0042SGerrit Renker confirmation_failed:
1263b1ad0042SGerrit Renker 	DCCP_WARN("Confirmation failed\n");
1264b1ad0042SGerrit Renker 	return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1265b1ad0042SGerrit Renker 			    : DCCP_RESET_CODE_OPTION_ERROR;
1266b1ad0042SGerrit Renker }
1267b1ad0042SGerrit Renker 
1268b1ad0042SGerrit Renker /**
1269*44e6fd9eSGerrit Renker  * dccp_feat_handle_nn_established  -  Fast-path reception of NN options
1270*44e6fd9eSGerrit Renker  * @sk:		socket of an established DCCP connection
1271*44e6fd9eSGerrit Renker  * @mandatory:	whether @opt was preceded by a Mandatory option
1272*44e6fd9eSGerrit Renker  * @opt:	%DCCPO_CHANGE_L | %DCCPO_CONFIRM_R (NN only)
1273*44e6fd9eSGerrit Renker  * @feat:	NN number, one of %dccp_feature_numbers
1274*44e6fd9eSGerrit Renker  * @val:	NN value
1275*44e6fd9eSGerrit Renker  * @len:	length of @val in bytes
1276*44e6fd9eSGerrit Renker  * This function combines the functionality of change_recv/confirm_recv, with
1277*44e6fd9eSGerrit Renker  * the following differences (reset codes are the same):
1278*44e6fd9eSGerrit Renker  *    - cleanup after receiving the Confirm;
1279*44e6fd9eSGerrit Renker  *    - values are directly activated after successful parsing;
1280*44e6fd9eSGerrit Renker  *    - deliberately restricted to NN features.
1281*44e6fd9eSGerrit Renker  * The restriction to NN features is essential since SP features can have non-
1282*44e6fd9eSGerrit Renker  * predictable outcomes (depending on the remote configuration), and are inter-
1283*44e6fd9eSGerrit Renker  * dependent (CCIDs for instance cause further dependencies).
1284*44e6fd9eSGerrit Renker  */
1285*44e6fd9eSGerrit Renker static u8 dccp_feat_handle_nn_established(struct sock *sk, u8 mandatory, u8 opt,
1286*44e6fd9eSGerrit Renker 					  u8 feat, u8 *val, u8 len)
1287*44e6fd9eSGerrit Renker {
1288*44e6fd9eSGerrit Renker 	struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1289*44e6fd9eSGerrit Renker 	const bool local = (opt == DCCPO_CONFIRM_R);
1290*44e6fd9eSGerrit Renker 	struct dccp_feat_entry *entry;
1291*44e6fd9eSGerrit Renker 	u8 type = dccp_feat_type(feat);
1292*44e6fd9eSGerrit Renker 	dccp_feat_val fval;
1293*44e6fd9eSGerrit Renker 
1294*44e6fd9eSGerrit Renker 	dccp_feat_print_opt(opt, feat, val, len, mandatory);
1295*44e6fd9eSGerrit Renker 
1296*44e6fd9eSGerrit Renker 	/* Ignore non-mandatory unknown and non-NN features */
1297*44e6fd9eSGerrit Renker 	if (type == FEAT_UNKNOWN) {
1298*44e6fd9eSGerrit Renker 		if (local && !mandatory)
1299*44e6fd9eSGerrit Renker 			return 0;
1300*44e6fd9eSGerrit Renker 		goto fast_path_unknown;
1301*44e6fd9eSGerrit Renker 	} else if (type != FEAT_NN) {
1302*44e6fd9eSGerrit Renker 		return 0;
1303*44e6fd9eSGerrit Renker 	}
1304*44e6fd9eSGerrit Renker 
1305*44e6fd9eSGerrit Renker 	/*
1306*44e6fd9eSGerrit Renker 	 * We don't accept empty Confirms, since in fast-path feature
1307*44e6fd9eSGerrit Renker 	 * negotiation the values are enabled immediately after sending
1308*44e6fd9eSGerrit Renker 	 * the Change option.
1309*44e6fd9eSGerrit Renker 	 * Empty Changes on the other hand are invalid (RFC 4340, 6.1).
1310*44e6fd9eSGerrit Renker 	 */
1311*44e6fd9eSGerrit Renker 	if (len == 0 || len > sizeof(fval.nn))
1312*44e6fd9eSGerrit Renker 		goto fast_path_unknown;
1313*44e6fd9eSGerrit Renker 
1314*44e6fd9eSGerrit Renker 	if (opt == DCCPO_CHANGE_L) {
1315*44e6fd9eSGerrit Renker 		fval.nn = dccp_decode_value_var(val, len);
1316*44e6fd9eSGerrit Renker 		if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
1317*44e6fd9eSGerrit Renker 			goto fast_path_unknown;
1318*44e6fd9eSGerrit Renker 
1319*44e6fd9eSGerrit Renker 		if (dccp_feat_push_confirm(fn, feat, local, &fval) ||
1320*44e6fd9eSGerrit Renker 		    dccp_feat_activate(sk, feat, local, &fval))
1321*44e6fd9eSGerrit Renker 			return DCCP_RESET_CODE_TOO_BUSY;
1322*44e6fd9eSGerrit Renker 
1323*44e6fd9eSGerrit Renker 		/* set the `Ack Pending' flag to piggyback a Confirm */
1324*44e6fd9eSGerrit Renker 		inet_csk_schedule_ack(sk);
1325*44e6fd9eSGerrit Renker 
1326*44e6fd9eSGerrit Renker 	} else if (opt == DCCPO_CONFIRM_R) {
1327*44e6fd9eSGerrit Renker 		entry = dccp_feat_list_lookup(fn, feat, local);
1328*44e6fd9eSGerrit Renker 		if (entry == NULL || entry->state != FEAT_CHANGING)
1329*44e6fd9eSGerrit Renker 			return 0;
1330*44e6fd9eSGerrit Renker 
1331*44e6fd9eSGerrit Renker 		fval.nn = dccp_decode_value_var(val, len);
1332*44e6fd9eSGerrit Renker 		/*
1333*44e6fd9eSGerrit Renker 		 * Just ignore a value that doesn't match our current value.
1334*44e6fd9eSGerrit Renker 		 * If the option changes twice within two RTTs, then at least
1335*44e6fd9eSGerrit Renker 		 * one CONFIRM will be received for the old value after a
1336*44e6fd9eSGerrit Renker 		 * new CHANGE was sent.
1337*44e6fd9eSGerrit Renker 		 */
1338*44e6fd9eSGerrit Renker 		if (fval.nn != entry->val.nn)
1339*44e6fd9eSGerrit Renker 			return 0;
1340*44e6fd9eSGerrit Renker 
1341*44e6fd9eSGerrit Renker 		/* Only activate after receiving the Confirm option (6.6.1). */
1342*44e6fd9eSGerrit Renker 		dccp_feat_activate(sk, feat, local, &fval);
1343*44e6fd9eSGerrit Renker 
1344*44e6fd9eSGerrit Renker 		/* It has been confirmed - so remove the entry */
1345*44e6fd9eSGerrit Renker 		dccp_feat_list_pop(entry);
1346*44e6fd9eSGerrit Renker 
1347*44e6fd9eSGerrit Renker 	} else {
1348*44e6fd9eSGerrit Renker 		DCCP_WARN("Received illegal option %u\n", opt);
1349*44e6fd9eSGerrit Renker 		goto fast_path_failed;
1350*44e6fd9eSGerrit Renker 	}
1351*44e6fd9eSGerrit Renker 	return 0;
1352*44e6fd9eSGerrit Renker 
1353*44e6fd9eSGerrit Renker fast_path_unknown:
1354*44e6fd9eSGerrit Renker 	if (!mandatory)
1355*44e6fd9eSGerrit Renker 		return dccp_push_empty_confirm(fn, feat, local);
1356*44e6fd9eSGerrit Renker 
1357*44e6fd9eSGerrit Renker fast_path_failed:
1358*44e6fd9eSGerrit Renker 	return mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1359*44e6fd9eSGerrit Renker 			 : DCCP_RESET_CODE_OPTION_ERROR;
1360*44e6fd9eSGerrit Renker }
1361*44e6fd9eSGerrit Renker 
1362*44e6fd9eSGerrit Renker /**
1363e77b8363SGerrit Renker  * dccp_feat_parse_options  -  Process Feature-Negotiation Options
1364e77b8363SGerrit Renker  * @sk: for general use and used by the client during connection setup
1365e77b8363SGerrit Renker  * @dreq: used by the server during connection setup
1366e77b8363SGerrit Renker  * @mandatory: whether @opt was preceded by a Mandatory option
1367e77b8363SGerrit Renker  * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1368e77b8363SGerrit Renker  * @feat: one of %dccp_feature_numbers
1369e77b8363SGerrit Renker  * @val: value contents of @opt
1370e77b8363SGerrit Renker  * @len: length of @val in bytes
1371e77b8363SGerrit Renker  * Returns 0 on success, a Reset code for ending the connection otherwise.
1372e77b8363SGerrit Renker  */
1373e77b8363SGerrit Renker int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1374e77b8363SGerrit Renker 			    u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1375e77b8363SGerrit Renker {
1376e77b8363SGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
1377e77b8363SGerrit Renker 	struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1378e77b8363SGerrit Renker 	bool server = false;
1379e77b8363SGerrit Renker 
1380e77b8363SGerrit Renker 	switch (sk->sk_state) {
1381e77b8363SGerrit Renker 	/*
1382e77b8363SGerrit Renker 	 *	Negotiation during connection setup
1383e77b8363SGerrit Renker 	 */
1384e77b8363SGerrit Renker 	case DCCP_LISTEN:
1385e77b8363SGerrit Renker 		server = true;			/* fall through */
1386e77b8363SGerrit Renker 	case DCCP_REQUESTING:
1387e77b8363SGerrit Renker 		switch (opt) {
1388e77b8363SGerrit Renker 		case DCCPO_CHANGE_L:
1389e77b8363SGerrit Renker 		case DCCPO_CHANGE_R:
1390e77b8363SGerrit Renker 			return dccp_feat_change_recv(fn, mandatory, opt, feat,
1391e77b8363SGerrit Renker 						     val, len, server);
1392b1ad0042SGerrit Renker 		case DCCPO_CONFIRM_R:
1393b1ad0042SGerrit Renker 		case DCCPO_CONFIRM_L:
1394b1ad0042SGerrit Renker 			return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1395b1ad0042SGerrit Renker 						      val, len, server);
1396e77b8363SGerrit Renker 		}
1397*44e6fd9eSGerrit Renker 		break;
1398*44e6fd9eSGerrit Renker 	/*
1399*44e6fd9eSGerrit Renker 	 *	Support for exchanging NN options on an established connection.
1400*44e6fd9eSGerrit Renker 	 */
1401*44e6fd9eSGerrit Renker 	case DCCP_OPEN:
1402*44e6fd9eSGerrit Renker 	case DCCP_PARTOPEN:
1403*44e6fd9eSGerrit Renker 		return dccp_feat_handle_nn_established(sk, mandatory, opt, feat,
1404*44e6fd9eSGerrit Renker 						       val, len);
1405e77b8363SGerrit Renker 	}
1406e77b8363SGerrit Renker 	return 0;	/* ignore FN options in all other states */
1407e77b8363SGerrit Renker }
1408e77b8363SGerrit Renker 
1409f90f92eeSGerrit Renker /**
1410f90f92eeSGerrit Renker  * dccp_feat_init  -  Seed feature negotiation with host-specific defaults
1411f90f92eeSGerrit Renker  * This initialises global defaults, depending on the value of the sysctls.
1412f90f92eeSGerrit Renker  * These can later be overridden by registering changes via setsockopt calls.
1413f90f92eeSGerrit Renker  * The last link in the chain is finalise_settings, to make sure that between
1414f90f92eeSGerrit Renker  * here and the start of actual feature negotiation no inconsistencies enter.
1415f90f92eeSGerrit Renker  *
1416f90f92eeSGerrit Renker  * All features not appearing below use either defaults or are otherwise
1417f90f92eeSGerrit Renker  * later adjusted through dccp_feat_finalise_settings().
1418f90f92eeSGerrit Renker  */
1419e8ef967aSGerrit Renker int dccp_feat_init(struct sock *sk)
1420afe00251SAndrea Bittau {
1421f90f92eeSGerrit Renker 	struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1422f90f92eeSGerrit Renker 	u8 on = 1, off = 0;
1423afe00251SAndrea Bittau 	int rc;
1424f90f92eeSGerrit Renker 	struct {
1425f90f92eeSGerrit Renker 		u8 *val;
1426f90f92eeSGerrit Renker 		u8 len;
1427f90f92eeSGerrit Renker 	} tx, rx;
1428afe00251SAndrea Bittau 
1429f90f92eeSGerrit Renker 	/* Non-negotiable (NN) features */
1430f90f92eeSGerrit Renker 	rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
1431883ca833SGerrit Renker 				    sysctl_dccp_sequence_window);
1432f90f92eeSGerrit Renker 	if (rc)
1433f90f92eeSGerrit Renker 		return rc;
1434afe00251SAndrea Bittau 
1435f90f92eeSGerrit Renker 	/* Server-priority (SP) features */
1436f90f92eeSGerrit Renker 
1437f90f92eeSGerrit Renker 	/* Advertise that short seqnos are not supported (7.6.1) */
1438f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1439f90f92eeSGerrit Renker 	if (rc)
1440f90f92eeSGerrit Renker 		return rc;
1441f90f92eeSGerrit Renker 
1442f90f92eeSGerrit Renker 	/* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
1443f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
1444f90f92eeSGerrit Renker 	if (rc)
1445f90f92eeSGerrit Renker 		return rc;
1446f90f92eeSGerrit Renker 
1447f90f92eeSGerrit Renker 	/*
1448f90f92eeSGerrit Renker 	 * We advertise the available list of CCIDs and reorder according to
1449f90f92eeSGerrit Renker 	 * preferences, to avoid failure resulting from negotiating different
1450f90f92eeSGerrit Renker 	 * singleton values (which always leads to failure).
1451f90f92eeSGerrit Renker 	 * These settings can still (later) be overridden via sockopts.
1452f90f92eeSGerrit Renker 	 */
1453f90f92eeSGerrit Renker 	if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1454f90f92eeSGerrit Renker 	    ccid_get_builtin_ccids(&rx.val, &rx.len))
1455f90f92eeSGerrit Renker 		return -ENOBUFS;
1456f90f92eeSGerrit Renker 
1457883ca833SGerrit Renker 	if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
1458883ca833SGerrit Renker 	    !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
1459f90f92eeSGerrit Renker 		goto free_ccid_lists;
1460f90f92eeSGerrit Renker 
1461f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
1462f90f92eeSGerrit Renker 	if (rc)
1463f90f92eeSGerrit Renker 		goto free_ccid_lists;
1464f90f92eeSGerrit Renker 
1465f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
1466f90f92eeSGerrit Renker 
1467f90f92eeSGerrit Renker free_ccid_lists:
1468f90f92eeSGerrit Renker 	kfree(tx.val);
1469f90f92eeSGerrit Renker 	kfree(rx.val);
1470afe00251SAndrea Bittau 	return rc;
1471afe00251SAndrea Bittau }
1472afe00251SAndrea Bittau 
1473422d9cdcSGerrit Renker int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1474422d9cdcSGerrit Renker {
1475422d9cdcSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
1476422d9cdcSGerrit Renker 	struct dccp_feat_entry *cur, *next;
1477422d9cdcSGerrit Renker 	int idx;
1478422d9cdcSGerrit Renker 	dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1479422d9cdcSGerrit Renker 		 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1480422d9cdcSGerrit Renker 	};
1481422d9cdcSGerrit Renker 
1482422d9cdcSGerrit Renker 	list_for_each_entry(cur, fn_list, node) {
1483422d9cdcSGerrit Renker 		/*
1484422d9cdcSGerrit Renker 		 * An empty Confirm means that either an unknown feature type
1485422d9cdcSGerrit Renker 		 * or an invalid value was present. In the first case there is
1486422d9cdcSGerrit Renker 		 * nothing to activate, in the other the default value is used.
1487422d9cdcSGerrit Renker 		 */
1488422d9cdcSGerrit Renker 		if (cur->empty_confirm)
1489422d9cdcSGerrit Renker 			continue;
1490422d9cdcSGerrit Renker 
1491422d9cdcSGerrit Renker 		idx = dccp_feat_index(cur->feat_num);
1492422d9cdcSGerrit Renker 		if (idx < 0) {
1493422d9cdcSGerrit Renker 			DCCP_BUG("Unknown feature %u", cur->feat_num);
1494422d9cdcSGerrit Renker 			goto activation_failed;
1495422d9cdcSGerrit Renker 		}
1496422d9cdcSGerrit Renker 		if (cur->state != FEAT_STABLE) {
1497f3f3abb6SGerrit Renker 			DCCP_CRIT("Negotiation of %s %s failed in state %s",
1498422d9cdcSGerrit Renker 				  cur->is_local ? "local" : "remote",
1499f3f3abb6SGerrit Renker 				  dccp_feat_fname(cur->feat_num),
1500f3f3abb6SGerrit Renker 				  dccp_feat_sname[cur->state]);
1501422d9cdcSGerrit Renker 			goto activation_failed;
1502422d9cdcSGerrit Renker 		}
1503422d9cdcSGerrit Renker 		fvals[idx][cur->is_local] = &cur->val;
1504422d9cdcSGerrit Renker 	}
1505422d9cdcSGerrit Renker 
1506422d9cdcSGerrit Renker 	/*
1507422d9cdcSGerrit Renker 	 * Activate in decreasing order of index, so that the CCIDs are always
1508422d9cdcSGerrit Renker 	 * activated as the last feature. This avoids the case where a CCID
1509422d9cdcSGerrit Renker 	 * relies on the initialisation of one or more features that it depends
1510422d9cdcSGerrit Renker 	 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1511422d9cdcSGerrit Renker 	 */
1512422d9cdcSGerrit Renker 	for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1513422d9cdcSGerrit Renker 		if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1514422d9cdcSGerrit Renker 		    __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1515422d9cdcSGerrit Renker 			DCCP_CRIT("Could not activate %d", idx);
1516422d9cdcSGerrit Renker 			goto activation_failed;
1517422d9cdcSGerrit Renker 		}
1518422d9cdcSGerrit Renker 
1519422d9cdcSGerrit Renker 	/* Clean up Change options which have been confirmed already */
1520422d9cdcSGerrit Renker 	list_for_each_entry_safe(cur, next, fn_list, node)
1521422d9cdcSGerrit Renker 		if (!cur->needs_confirm)
1522422d9cdcSGerrit Renker 			dccp_feat_list_pop(cur);
1523422d9cdcSGerrit Renker 
1524422d9cdcSGerrit Renker 	dccp_pr_debug("Activation OK\n");
1525422d9cdcSGerrit Renker 	return 0;
1526422d9cdcSGerrit Renker 
1527422d9cdcSGerrit Renker activation_failed:
1528422d9cdcSGerrit Renker 	/*
1529422d9cdcSGerrit Renker 	 * We clean up everything that may have been allocated, since
1530422d9cdcSGerrit Renker 	 * it is difficult to track at which stage negotiation failed.
1531422d9cdcSGerrit Renker 	 * This is ok, since all allocation functions below are robust
1532422d9cdcSGerrit Renker 	 * against NULL arguments.
1533422d9cdcSGerrit Renker 	 */
1534422d9cdcSGerrit Renker 	ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1535422d9cdcSGerrit Renker 	ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1536422d9cdcSGerrit Renker 	dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1537422d9cdcSGerrit Renker 	dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1538422d9cdcSGerrit Renker 	dp->dccps_hc_rx_ackvec = NULL;
1539422d9cdcSGerrit Renker 	return -1;
1540422d9cdcSGerrit Renker }
1541