xref: /openbmc/linux/net/dccp/feat.c (revision 883ca833e5fb814fb03426c9d35e5489ce43e8da)
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.
155cdae198SGerrit Renker  *  o All currently known SP features have 1-byte quantities. If in the future
165cdae198SGerrit Renker  *    extensions of RFCs 4340..42 define features with item lengths larger than
175cdae198SGerrit Renker  *    one byte, a feature-specific extension of the code will be required.
185cdae198SGerrit Renker  *
19afe00251SAndrea Bittau  *  This program is free software; you can redistribute it and/or
20afe00251SAndrea Bittau  *  modify it under the terms of the GNU General Public License
21afe00251SAndrea Bittau  *  as published by the Free Software Foundation; either version
22afe00251SAndrea Bittau  *  2 of the License, or (at your option) any later version.
23afe00251SAndrea Bittau  */
24afe00251SAndrea Bittau #include <linux/module.h>
256ffd30fbSAndrea Bittau #include "ccid.h"
26afe00251SAndrea Bittau #include "feat.h"
27afe00251SAndrea Bittau 
28*883ca833SGerrit Renker /* feature-specific sysctls - initialised to the defaults from RFC 4340, 6.4 */
29*883ca833SGerrit Renker unsigned long	sysctl_dccp_sequence_window __read_mostly = 100;
30*883ca833SGerrit Renker int		sysctl_dccp_rx_ccid	    __read_mostly = 2,
31*883ca833SGerrit Renker 		sysctl_dccp_tx_ccid	    __read_mostly = 2;
32*883ca833SGerrit Renker 
33422d9cdcSGerrit Renker /*
34422d9cdcSGerrit Renker  * Feature activation handlers.
35422d9cdcSGerrit Renker  *
36422d9cdcSGerrit Renker  * These all use an u64 argument, to provide enough room for NN/SP features. At
37422d9cdcSGerrit Renker  * this stage the negotiated values have been checked to be within their range.
38422d9cdcSGerrit Renker  */
39422d9cdcSGerrit Renker static int dccp_hdlr_ccid(struct sock *sk, u64 ccid, bool rx)
40422d9cdcSGerrit Renker {
41422d9cdcSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
42e5fd56caSGerrit Renker 	struct ccid *new_ccid = ccid_new(ccid, sk, rx);
43422d9cdcSGerrit Renker 
44422d9cdcSGerrit Renker 	if (new_ccid == NULL)
45422d9cdcSGerrit Renker 		return -ENOMEM;
46422d9cdcSGerrit Renker 
47422d9cdcSGerrit Renker 	if (rx) {
48422d9cdcSGerrit Renker 		ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
49422d9cdcSGerrit Renker 		dp->dccps_hc_rx_ccid = new_ccid;
50422d9cdcSGerrit Renker 	} else {
51422d9cdcSGerrit Renker 		ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
52422d9cdcSGerrit Renker 		dp->dccps_hc_tx_ccid = new_ccid;
53422d9cdcSGerrit Renker 	}
54422d9cdcSGerrit Renker 	return 0;
55422d9cdcSGerrit Renker }
56422d9cdcSGerrit Renker 
57422d9cdcSGerrit Renker static int dccp_hdlr_seq_win(struct sock *sk, u64 seq_win, bool rx)
58422d9cdcSGerrit Renker {
59792b4878SGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
60792b4878SGerrit Renker 
61792b4878SGerrit Renker 	if (rx) {
62792b4878SGerrit Renker 		dp->dccps_r_seq_win = seq_win;
63792b4878SGerrit Renker 		/* propagate changes to update SWL/SWH */
64792b4878SGerrit Renker 		dccp_update_gsr(sk, dp->dccps_gsr);
65792b4878SGerrit Renker 	} else {
66792b4878SGerrit Renker 		dp->dccps_l_seq_win = seq_win;
67792b4878SGerrit Renker 		/* propagate changes to update AWL */
68792b4878SGerrit Renker 		dccp_update_gss(sk, dp->dccps_gss);
69792b4878SGerrit Renker 	}
70422d9cdcSGerrit Renker 	return 0;
71422d9cdcSGerrit Renker }
72422d9cdcSGerrit Renker 
73422d9cdcSGerrit Renker static int dccp_hdlr_ack_ratio(struct sock *sk, u64 ratio, bool rx)
74422d9cdcSGerrit Renker {
75422d9cdcSGerrit Renker 	if (rx)
76422d9cdcSGerrit Renker 		dccp_sk(sk)->dccps_r_ack_ratio = ratio;
77422d9cdcSGerrit Renker 	else
78422d9cdcSGerrit Renker 		dccp_sk(sk)->dccps_l_ack_ratio = ratio;
79422d9cdcSGerrit Renker 	return 0;
80422d9cdcSGerrit Renker }
81422d9cdcSGerrit Renker 
82422d9cdcSGerrit Renker static int dccp_hdlr_ackvec(struct sock *sk, u64 enable, bool rx)
83422d9cdcSGerrit Renker {
84422d9cdcSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
85422d9cdcSGerrit Renker 
86422d9cdcSGerrit Renker 	if (rx) {
87422d9cdcSGerrit Renker 		if (enable && dp->dccps_hc_rx_ackvec == NULL) {
88422d9cdcSGerrit Renker 			dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(gfp_any());
89422d9cdcSGerrit Renker 			if (dp->dccps_hc_rx_ackvec == NULL)
90422d9cdcSGerrit Renker 				return -ENOMEM;
91422d9cdcSGerrit Renker 		} else if (!enable) {
92422d9cdcSGerrit Renker 			dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
93422d9cdcSGerrit Renker 			dp->dccps_hc_rx_ackvec = NULL;
94422d9cdcSGerrit Renker 		}
95422d9cdcSGerrit Renker 	}
96422d9cdcSGerrit Renker 	return 0;
97422d9cdcSGerrit Renker }
98422d9cdcSGerrit Renker 
99422d9cdcSGerrit Renker static int dccp_hdlr_ndp(struct sock *sk, u64 enable, bool rx)
100422d9cdcSGerrit Renker {
101422d9cdcSGerrit Renker 	if (!rx)
1024098dce5SGerrit Renker 		dccp_sk(sk)->dccps_send_ndp_count = (enable > 0);
103422d9cdcSGerrit Renker 	return 0;
104422d9cdcSGerrit Renker }
105422d9cdcSGerrit Renker 
106422d9cdcSGerrit Renker /*
107422d9cdcSGerrit Renker  * Minimum Checksum Coverage is located at the RX side (9.2.1). This means that
108422d9cdcSGerrit Renker  * `rx' holds when the sending peer informs about his partial coverage via a
109422d9cdcSGerrit Renker  * ChangeR() option. In the other case, we are the sender and the receiver
110422d9cdcSGerrit Renker  * announces its coverage via ChangeL() options. The policy here is to honour
111422d9cdcSGerrit Renker  * such communication by enabling the corresponding partial coverage - but only
112422d9cdcSGerrit Renker  * if it has not been set manually before; the warning here means that all
113422d9cdcSGerrit Renker  * packets will be dropped.
114422d9cdcSGerrit Renker  */
115422d9cdcSGerrit Renker static int dccp_hdlr_min_cscov(struct sock *sk, u64 cscov, bool rx)
116422d9cdcSGerrit Renker {
117422d9cdcSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
118422d9cdcSGerrit Renker 
119422d9cdcSGerrit Renker 	if (rx)
120422d9cdcSGerrit Renker 		dp->dccps_pcrlen = cscov;
121422d9cdcSGerrit Renker 	else {
122422d9cdcSGerrit Renker 		if (dp->dccps_pcslen == 0)
123422d9cdcSGerrit Renker 			dp->dccps_pcslen = cscov;
124422d9cdcSGerrit Renker 		else if (cscov > dp->dccps_pcslen)
125422d9cdcSGerrit Renker 			DCCP_WARN("CsCov %u too small, peer requires >= %u\n",
126422d9cdcSGerrit Renker 				  dp->dccps_pcslen, (u8)cscov);
127422d9cdcSGerrit Renker 	}
128422d9cdcSGerrit Renker 	return 0;
129422d9cdcSGerrit Renker }
130422d9cdcSGerrit Renker 
1317d43d1a0SGerrit Renker static const struct {
1327d43d1a0SGerrit Renker 	u8			feat_num;		/* DCCPF_xxx */
1337d43d1a0SGerrit Renker 	enum dccp_feat_type	rxtx;			/* RX or TX  */
1347d43d1a0SGerrit Renker 	enum dccp_feat_type	reconciliation;		/* SP or NN  */
1357d43d1a0SGerrit Renker 	u8			default_value;		/* as in 6.4 */
136422d9cdcSGerrit Renker 	int (*activation_hdlr)(struct sock *sk, u64 val, bool rx);
1377d43d1a0SGerrit Renker /*
1387d43d1a0SGerrit Renker  *    Lookup table for location and type of features (from RFC 4340/4342)
1397d43d1a0SGerrit Renker  *  +--------------------------+----+-----+----+----+---------+-----------+
1407d43d1a0SGerrit Renker  *  | Feature                  | Location | Reconc. | Initial |  Section  |
1417d43d1a0SGerrit Renker  *  |                          | RX | TX  | SP | NN |  Value  | Reference |
1427d43d1a0SGerrit Renker  *  +--------------------------+----+-----+----+----+---------+-----------+
1437d43d1a0SGerrit Renker  *  | DCCPF_CCID               |    |  X  | X  |    |   2     | 10        |
1447d43d1a0SGerrit Renker  *  | DCCPF_SHORT_SEQNOS       |    |  X  | X  |    |   0     |  7.6.1    |
1457d43d1a0SGerrit Renker  *  | DCCPF_SEQUENCE_WINDOW    |    |  X  |    | X  | 100     |  7.5.2    |
1467d43d1a0SGerrit Renker  *  | DCCPF_ECN_INCAPABLE      | X  |     | X  |    |   0     | 12.1      |
1477d43d1a0SGerrit Renker  *  | DCCPF_ACK_RATIO          |    |  X  |    | X  |   2     | 11.3      |
1487d43d1a0SGerrit Renker  *  | DCCPF_SEND_ACK_VECTOR    | X  |     | X  |    |   0     | 11.5      |
1497d43d1a0SGerrit Renker  *  | DCCPF_SEND_NDP_COUNT     |    |  X  | X  |    |   0     |  7.7.2    |
1507d43d1a0SGerrit Renker  *  | DCCPF_MIN_CSUM_COVER     | X  |     | X  |    |   0     |  9.2.1    |
1517d43d1a0SGerrit Renker  *  | DCCPF_DATA_CHECKSUM      | X  |     | X  |    |   0     |  9.3.1    |
1527d43d1a0SGerrit Renker  *  | DCCPF_SEND_LEV_RATE      | X  |     | X  |    |   0     | 4342/8.4  |
1537d43d1a0SGerrit Renker  *  +--------------------------+----+-----+----+----+---------+-----------+
1547d43d1a0SGerrit Renker  */
1557d43d1a0SGerrit Renker } dccp_feat_table[] = {
156422d9cdcSGerrit Renker 	{ DCCPF_CCID,		 FEAT_AT_TX, FEAT_SP, 2,   dccp_hdlr_ccid     },
157422d9cdcSGerrit Renker 	{ DCCPF_SHORT_SEQNOS,	 FEAT_AT_TX, FEAT_SP, 0,   NULL },
158422d9cdcSGerrit Renker 	{ DCCPF_SEQUENCE_WINDOW, FEAT_AT_TX, FEAT_NN, 100, dccp_hdlr_seq_win  },
159422d9cdcSGerrit Renker 	{ DCCPF_ECN_INCAPABLE,	 FEAT_AT_RX, FEAT_SP, 0,   NULL },
160422d9cdcSGerrit Renker 	{ DCCPF_ACK_RATIO,	 FEAT_AT_TX, FEAT_NN, 2,   dccp_hdlr_ack_ratio},
161422d9cdcSGerrit Renker 	{ DCCPF_SEND_ACK_VECTOR, FEAT_AT_RX, FEAT_SP, 0,   dccp_hdlr_ackvec   },
162422d9cdcSGerrit Renker 	{ DCCPF_SEND_NDP_COUNT,  FEAT_AT_TX, FEAT_SP, 0,   dccp_hdlr_ndp      },
163422d9cdcSGerrit Renker 	{ DCCPF_MIN_CSUM_COVER,  FEAT_AT_RX, FEAT_SP, 0,   dccp_hdlr_min_cscov},
164422d9cdcSGerrit Renker 	{ DCCPF_DATA_CHECKSUM,	 FEAT_AT_RX, FEAT_SP, 0,   NULL },
165422d9cdcSGerrit Renker 	{ DCCPF_SEND_LEV_RATE,	 FEAT_AT_RX, FEAT_SP, 0,   NULL },
1667d43d1a0SGerrit Renker };
1677d43d1a0SGerrit Renker #define DCCP_FEAT_SUPPORTED_MAX		ARRAY_SIZE(dccp_feat_table)
1687d43d1a0SGerrit Renker 
16961e6473eSGerrit Renker /**
17061e6473eSGerrit Renker  * dccp_feat_index  -  Hash function to map feature number into array position
17161e6473eSGerrit Renker  * Returns consecutive array index or -1 if the feature is not understood.
17261e6473eSGerrit Renker  */
17361e6473eSGerrit Renker static int dccp_feat_index(u8 feat_num)
17461e6473eSGerrit Renker {
17561e6473eSGerrit Renker 	/* The first 9 entries are occupied by the types from RFC 4340, 6.4 */
17661e6473eSGerrit Renker 	if (feat_num > DCCPF_RESERVED && feat_num <= DCCPF_DATA_CHECKSUM)
17761e6473eSGerrit Renker 		return feat_num - 1;
17861e6473eSGerrit Renker 
17961e6473eSGerrit Renker 	/*
18061e6473eSGerrit Renker 	 * Other features: add cases for new feature types here after adding
18161e6473eSGerrit Renker 	 * them to the above table.
18261e6473eSGerrit Renker 	 */
18361e6473eSGerrit Renker 	switch (feat_num) {
18461e6473eSGerrit Renker 	case DCCPF_SEND_LEV_RATE:
18561e6473eSGerrit Renker 			return DCCP_FEAT_SUPPORTED_MAX - 1;
18661e6473eSGerrit Renker 	}
18761e6473eSGerrit Renker 	return -1;
18861e6473eSGerrit Renker }
18961e6473eSGerrit Renker 
19061e6473eSGerrit Renker static u8 dccp_feat_type(u8 feat_num)
19161e6473eSGerrit Renker {
19261e6473eSGerrit Renker 	int idx = dccp_feat_index(feat_num);
19361e6473eSGerrit Renker 
19461e6473eSGerrit Renker 	if (idx < 0)
19561e6473eSGerrit Renker 		return FEAT_UNKNOWN;
19661e6473eSGerrit Renker 	return dccp_feat_table[idx].reconciliation;
19761e6473eSGerrit Renker }
19861e6473eSGerrit Renker 
199e8ef967aSGerrit Renker static int dccp_feat_default_value(u8 feat_num)
200e8ef967aSGerrit Renker {
201e8ef967aSGerrit Renker 	int idx = dccp_feat_index(feat_num);
202e8ef967aSGerrit Renker 	/*
203e8ef967aSGerrit Renker 	 * There are no default values for unknown features, so encountering a
204e8ef967aSGerrit Renker 	 * negative index here indicates a serious problem somewhere else.
205e8ef967aSGerrit Renker 	 */
206e8ef967aSGerrit Renker 	DCCP_BUG_ON(idx < 0);
207e8ef967aSGerrit Renker 
208e8ef967aSGerrit Renker 	return idx < 0 ? 0 : dccp_feat_table[idx].default_value;
209e8ef967aSGerrit Renker }
210e8ef967aSGerrit Renker 
211422d9cdcSGerrit Renker static int __dccp_feat_activate(struct sock *sk, const int idx,
212422d9cdcSGerrit Renker 				const bool is_local, dccp_feat_val const *fval)
213422d9cdcSGerrit Renker {
214422d9cdcSGerrit Renker 	bool rx;
215422d9cdcSGerrit Renker 	u64 val;
216422d9cdcSGerrit Renker 
217422d9cdcSGerrit Renker 	if (idx < 0 || idx >= DCCP_FEAT_SUPPORTED_MAX)
218422d9cdcSGerrit Renker 		return -1;
219422d9cdcSGerrit Renker 	if (dccp_feat_table[idx].activation_hdlr == NULL)
220422d9cdcSGerrit Renker 		return 0;
221422d9cdcSGerrit Renker 
222422d9cdcSGerrit Renker 	if (fval == NULL) {
223422d9cdcSGerrit Renker 		val = dccp_feat_table[idx].default_value;
224422d9cdcSGerrit Renker 	} else if (dccp_feat_table[idx].reconciliation == FEAT_SP) {
225422d9cdcSGerrit Renker 		if (fval->sp.vec == NULL) {
226422d9cdcSGerrit Renker 			/*
227422d9cdcSGerrit Renker 			 * This can happen when an empty Confirm is sent
228422d9cdcSGerrit Renker 			 * for an SP (i.e. known) feature. In this case
229422d9cdcSGerrit Renker 			 * we would be using the default anyway.
230422d9cdcSGerrit Renker 			 */
231422d9cdcSGerrit Renker 			DCCP_CRIT("Feature #%d undefined: using default", idx);
232422d9cdcSGerrit Renker 			val = dccp_feat_table[idx].default_value;
233422d9cdcSGerrit Renker 		} else {
234422d9cdcSGerrit Renker 			val = fval->sp.vec[0];
235422d9cdcSGerrit Renker 		}
236422d9cdcSGerrit Renker 	} else {
237422d9cdcSGerrit Renker 		val = fval->nn;
238422d9cdcSGerrit Renker 	}
239422d9cdcSGerrit Renker 
240422d9cdcSGerrit Renker 	/* Location is RX if this is a local-RX or remote-TX feature */
241422d9cdcSGerrit Renker 	rx = (is_local == (dccp_feat_table[idx].rxtx == FEAT_AT_RX));
242422d9cdcSGerrit Renker 
243422d9cdcSGerrit Renker 	return dccp_feat_table[idx].activation_hdlr(sk, val, rx);
244422d9cdcSGerrit Renker }
245422d9cdcSGerrit Renker 
246b1ad0042SGerrit Renker /* Test for "Req'd" feature (RFC 4340, 6.4) */
247b1ad0042SGerrit Renker static inline int dccp_feat_must_be_understood(u8 feat_num)
248b1ad0042SGerrit Renker {
249b1ad0042SGerrit Renker 	return	feat_num == DCCPF_CCID || feat_num == DCCPF_SHORT_SEQNOS ||
250b1ad0042SGerrit Renker 		feat_num == DCCPF_SEQUENCE_WINDOW;
251b1ad0042SGerrit Renker }
252b1ad0042SGerrit Renker 
253ac75773cSGerrit Renker /* copy constructor, fval must not already contain allocated memory */
254ac75773cSGerrit Renker static int dccp_feat_clone_sp_val(dccp_feat_val *fval, u8 const *val, u8 len)
255ac75773cSGerrit Renker {
256ac75773cSGerrit Renker 	fval->sp.len = len;
257ac75773cSGerrit Renker 	if (fval->sp.len > 0) {
258ac75773cSGerrit Renker 		fval->sp.vec = kmemdup(val, len, gfp_any());
259ac75773cSGerrit Renker 		if (fval->sp.vec == NULL) {
260ac75773cSGerrit Renker 			fval->sp.len = 0;
261ac75773cSGerrit Renker 			return -ENOBUFS;
262ac75773cSGerrit Renker 		}
263ac75773cSGerrit Renker 	}
264ac75773cSGerrit Renker 	return 0;
265ac75773cSGerrit Renker }
266ac75773cSGerrit Renker 
26761e6473eSGerrit Renker static void dccp_feat_val_destructor(u8 feat_num, dccp_feat_val *val)
26861e6473eSGerrit Renker {
26961e6473eSGerrit Renker 	if (unlikely(val == NULL))
27061e6473eSGerrit Renker 		return;
27161e6473eSGerrit Renker 	if (dccp_feat_type(feat_num) == FEAT_SP)
27261e6473eSGerrit Renker 		kfree(val->sp.vec);
27361e6473eSGerrit Renker 	memset(val, 0, sizeof(*val));
27461e6473eSGerrit Renker }
27561e6473eSGerrit Renker 
276ac75773cSGerrit Renker static struct dccp_feat_entry *
277ac75773cSGerrit Renker 	      dccp_feat_clone_entry(struct dccp_feat_entry const *original)
278ac75773cSGerrit Renker {
279ac75773cSGerrit Renker 	struct dccp_feat_entry *new;
280ac75773cSGerrit Renker 	u8 type = dccp_feat_type(original->feat_num);
281ac75773cSGerrit Renker 
282ac75773cSGerrit Renker 	if (type == FEAT_UNKNOWN)
283ac75773cSGerrit Renker 		return NULL;
284ac75773cSGerrit Renker 
285ac75773cSGerrit Renker 	new = kmemdup(original, sizeof(struct dccp_feat_entry), gfp_any());
286ac75773cSGerrit Renker 	if (new == NULL)
287ac75773cSGerrit Renker 		return NULL;
288ac75773cSGerrit Renker 
289ac75773cSGerrit Renker 	if (type == FEAT_SP && dccp_feat_clone_sp_val(&new->val,
290ac75773cSGerrit Renker 						      original->val.sp.vec,
291ac75773cSGerrit Renker 						      original->val.sp.len)) {
292ac75773cSGerrit Renker 		kfree(new);
293ac75773cSGerrit Renker 		return NULL;
294ac75773cSGerrit Renker 	}
295ac75773cSGerrit Renker 	return new;
296ac75773cSGerrit Renker }
297ac75773cSGerrit Renker 
29861e6473eSGerrit Renker static void dccp_feat_entry_destructor(struct dccp_feat_entry *entry)
29961e6473eSGerrit Renker {
30061e6473eSGerrit Renker 	if (entry != NULL) {
30161e6473eSGerrit Renker 		dccp_feat_val_destructor(entry->feat_num, &entry->val);
30261e6473eSGerrit Renker 		kfree(entry);
30361e6473eSGerrit Renker 	}
30461e6473eSGerrit Renker }
30561e6473eSGerrit Renker 
30661e6473eSGerrit Renker /*
30761e6473eSGerrit Renker  * List management functions
30861e6473eSGerrit Renker  *
30961e6473eSGerrit Renker  * Feature negotiation lists rely on and maintain the following invariants:
31061e6473eSGerrit Renker  * - each feat_num in the list is known, i.e. we know its type and default value
31161e6473eSGerrit Renker  * - each feat_num/is_local combination is unique (old entries are overwritten)
31261e6473eSGerrit Renker  * - SP values are always freshly allocated
31361e6473eSGerrit Renker  * - list is sorted in increasing order of feature number (faster lookup)
31461e6473eSGerrit Renker  */
3150c116839SGerrit Renker static struct dccp_feat_entry *dccp_feat_list_lookup(struct list_head *fn_list,
3160c116839SGerrit Renker 						     u8 feat_num, bool is_local)
3170c116839SGerrit Renker {
3180c116839SGerrit Renker 	struct dccp_feat_entry *entry;
3190c116839SGerrit Renker 
3203d3e35aaSGerrit Renker 	list_for_each_entry(entry, fn_list, node) {
3210c116839SGerrit Renker 		if (entry->feat_num == feat_num && entry->is_local == is_local)
3220c116839SGerrit Renker 			return entry;
3230c116839SGerrit Renker 		else if (entry->feat_num > feat_num)
3240c116839SGerrit Renker 			break;
3253d3e35aaSGerrit Renker 	}
3260c116839SGerrit Renker 	return NULL;
3270c116839SGerrit Renker }
32861e6473eSGerrit Renker 
329e8ef967aSGerrit Renker /**
330e8ef967aSGerrit Renker  * dccp_feat_entry_new  -  Central list update routine (called by all others)
331e8ef967aSGerrit Renker  * @head:  list to add to
332e8ef967aSGerrit Renker  * @feat:  feature number
333e8ef967aSGerrit Renker  * @local: whether the local (1) or remote feature with number @feat is meant
334e8ef967aSGerrit Renker  * This is the only constructor and serves to ensure the above invariants.
335e8ef967aSGerrit Renker  */
336e8ef967aSGerrit Renker static struct dccp_feat_entry *
337e8ef967aSGerrit Renker 	      dccp_feat_entry_new(struct list_head *head, u8 feat, bool local)
338e8ef967aSGerrit Renker {
339e8ef967aSGerrit Renker 	struct dccp_feat_entry *entry;
340e8ef967aSGerrit Renker 
341e8ef967aSGerrit Renker 	list_for_each_entry(entry, head, node)
342e8ef967aSGerrit Renker 		if (entry->feat_num == feat && entry->is_local == local) {
343e8ef967aSGerrit Renker 			dccp_feat_val_destructor(entry->feat_num, &entry->val);
344e8ef967aSGerrit Renker 			return entry;
345e8ef967aSGerrit Renker 		} else if (entry->feat_num > feat) {
346e8ef967aSGerrit Renker 			head = &entry->node;
347e8ef967aSGerrit Renker 			break;
348e8ef967aSGerrit Renker 		}
349e8ef967aSGerrit Renker 
350e8ef967aSGerrit Renker 	entry = kmalloc(sizeof(*entry), gfp_any());
351e8ef967aSGerrit Renker 	if (entry != NULL) {
352e8ef967aSGerrit Renker 		entry->feat_num = feat;
353e8ef967aSGerrit Renker 		entry->is_local = local;
354e8ef967aSGerrit Renker 		list_add_tail(&entry->node, head);
355e8ef967aSGerrit Renker 	}
356e8ef967aSGerrit Renker 	return entry;
357e8ef967aSGerrit Renker }
358e8ef967aSGerrit Renker 
359e8ef967aSGerrit Renker /**
360e8ef967aSGerrit Renker  * dccp_feat_push_change  -  Add/overwrite a Change option in the list
361e8ef967aSGerrit Renker  * @fn_list: feature-negotiation list to update
362e8ef967aSGerrit Renker  * @feat: one of %dccp_feature_numbers
363e8ef967aSGerrit Renker  * @local: whether local (1) or remote (0) @feat_num is meant
364e8ef967aSGerrit Renker  * @needs_mandatory: whether to use Mandatory feature negotiation options
365e8ef967aSGerrit Renker  * @fval: pointer to NN/SP value to be inserted (will be copied)
366e8ef967aSGerrit Renker  */
367e8ef967aSGerrit Renker static int dccp_feat_push_change(struct list_head *fn_list, u8 feat, u8 local,
368e8ef967aSGerrit Renker 				 u8 mandatory, dccp_feat_val *fval)
369e8ef967aSGerrit Renker {
370e8ef967aSGerrit Renker 	struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
371e8ef967aSGerrit Renker 
372e8ef967aSGerrit Renker 	if (new == NULL)
373e8ef967aSGerrit Renker 		return -ENOMEM;
374e8ef967aSGerrit Renker 
375e8ef967aSGerrit Renker 	new->feat_num	     = feat;
376e8ef967aSGerrit Renker 	new->is_local	     = local;
377e8ef967aSGerrit Renker 	new->state	     = FEAT_INITIALISING;
378e8ef967aSGerrit Renker 	new->needs_confirm   = 0;
379e8ef967aSGerrit Renker 	new->empty_confirm   = 0;
380e8ef967aSGerrit Renker 	new->val	     = *fval;
381e8ef967aSGerrit Renker 	new->needs_mandatory = mandatory;
382e8ef967aSGerrit Renker 
383e8ef967aSGerrit Renker 	return 0;
384e8ef967aSGerrit Renker }
385e8ef967aSGerrit Renker 
386e77b8363SGerrit Renker /**
387e77b8363SGerrit Renker  * dccp_feat_push_confirm  -  Add a Confirm entry to the FN list
388e77b8363SGerrit Renker  * @fn_list: feature-negotiation list to add to
389e77b8363SGerrit Renker  * @feat: one of %dccp_feature_numbers
390e77b8363SGerrit Renker  * @local: whether local (1) or remote (0) @feat_num is being confirmed
391e77b8363SGerrit Renker  * @fval: pointer to NN/SP value to be inserted or NULL
392e77b8363SGerrit Renker  * Returns 0 on success, a Reset code for further processing otherwise.
393e77b8363SGerrit Renker  */
394e77b8363SGerrit Renker static int dccp_feat_push_confirm(struct list_head *fn_list, u8 feat, u8 local,
395e77b8363SGerrit Renker 				  dccp_feat_val *fval)
396e77b8363SGerrit Renker {
397e77b8363SGerrit Renker 	struct dccp_feat_entry *new = dccp_feat_entry_new(fn_list, feat, local);
398e77b8363SGerrit Renker 
399e77b8363SGerrit Renker 	if (new == NULL)
400e77b8363SGerrit Renker 		return DCCP_RESET_CODE_TOO_BUSY;
401e77b8363SGerrit Renker 
402e77b8363SGerrit Renker 	new->feat_num	     = feat;
403e77b8363SGerrit Renker 	new->is_local	     = local;
404e77b8363SGerrit Renker 	new->state	     = FEAT_STABLE;	/* transition in 6.6.2 */
405e77b8363SGerrit Renker 	new->needs_confirm   = 1;
406e77b8363SGerrit Renker 	new->empty_confirm   = (fval == NULL);
407e77b8363SGerrit Renker 	new->val.nn	     = 0;		/* zeroes the whole structure */
408e77b8363SGerrit Renker 	if (!new->empty_confirm)
409e77b8363SGerrit Renker 		new->val     = *fval;
410e77b8363SGerrit Renker 	new->needs_mandatory = 0;
411e77b8363SGerrit Renker 
412e77b8363SGerrit Renker 	return 0;
413e77b8363SGerrit Renker }
414e77b8363SGerrit Renker 
415e77b8363SGerrit Renker static int dccp_push_empty_confirm(struct list_head *fn_list, u8 feat, u8 local)
416e77b8363SGerrit Renker {
417e77b8363SGerrit Renker 	return dccp_feat_push_confirm(fn_list, feat, local, NULL);
418e77b8363SGerrit Renker }
419e77b8363SGerrit Renker 
42061e6473eSGerrit Renker static inline void dccp_feat_list_pop(struct dccp_feat_entry *entry)
42161e6473eSGerrit Renker {
42261e6473eSGerrit Renker 	list_del(&entry->node);
42361e6473eSGerrit Renker 	dccp_feat_entry_destructor(entry);
42461e6473eSGerrit Renker }
42561e6473eSGerrit Renker 
42661e6473eSGerrit Renker void dccp_feat_list_purge(struct list_head *fn_list)
42761e6473eSGerrit Renker {
42861e6473eSGerrit Renker 	struct dccp_feat_entry *entry, *next;
42961e6473eSGerrit Renker 
43061e6473eSGerrit Renker 	list_for_each_entry_safe(entry, next, fn_list, node)
43161e6473eSGerrit Renker 		dccp_feat_entry_destructor(entry);
43261e6473eSGerrit Renker 	INIT_LIST_HEAD(fn_list);
43361e6473eSGerrit Renker }
43461e6473eSGerrit Renker EXPORT_SYMBOL_GPL(dccp_feat_list_purge);
43561e6473eSGerrit Renker 
436ac75773cSGerrit Renker /* generate @to as full clone of @from - @to must not contain any nodes */
437ac75773cSGerrit Renker int dccp_feat_clone_list(struct list_head const *from, struct list_head *to)
438ac75773cSGerrit Renker {
439ac75773cSGerrit Renker 	struct dccp_feat_entry *entry, *new;
440ac75773cSGerrit Renker 
441ac75773cSGerrit Renker 	INIT_LIST_HEAD(to);
442ac75773cSGerrit Renker 	list_for_each_entry(entry, from, node) {
443ac75773cSGerrit Renker 		new = dccp_feat_clone_entry(entry);
444ac75773cSGerrit Renker 		if (new == NULL)
445ac75773cSGerrit Renker 			goto cloning_failed;
446ac75773cSGerrit Renker 		list_add_tail(&new->node, to);
447ac75773cSGerrit Renker 	}
448ac75773cSGerrit Renker 	return 0;
449ac75773cSGerrit Renker 
450ac75773cSGerrit Renker cloning_failed:
451ac75773cSGerrit Renker 	dccp_feat_list_purge(to);
452ac75773cSGerrit Renker 	return -ENOMEM;
453ac75773cSGerrit Renker }
454ac75773cSGerrit Renker 
4550971d17cSGerrit Renker /**
4560971d17cSGerrit Renker  * dccp_feat_valid_nn_length  -  Enforce length constraints on NN options
4570971d17cSGerrit Renker  * Length is between 0 and %DCCP_OPTVAL_MAXLEN. Used for outgoing packets only,
4580971d17cSGerrit Renker  * incoming options are accepted as long as their values are valid.
4590971d17cSGerrit Renker  */
4600971d17cSGerrit Renker static u8 dccp_feat_valid_nn_length(u8 feat_num)
4610971d17cSGerrit Renker {
4620971d17cSGerrit Renker 	if (feat_num == DCCPF_ACK_RATIO)	/* RFC 4340, 11.3 and 6.6.8 */
4630971d17cSGerrit Renker 		return 2;
4640971d17cSGerrit Renker 	if (feat_num == DCCPF_SEQUENCE_WINDOW)	/* RFC 4340, 7.5.2 and 6.5  */
4650971d17cSGerrit Renker 		return 6;
4660971d17cSGerrit Renker 	return 0;
4670971d17cSGerrit Renker }
4680971d17cSGerrit Renker 
469e8ef967aSGerrit Renker static u8 dccp_feat_is_valid_nn_val(u8 feat_num, u64 val)
470e8ef967aSGerrit Renker {
471e8ef967aSGerrit Renker 	switch (feat_num) {
472e8ef967aSGerrit Renker 	case DCCPF_ACK_RATIO:
473e8ef967aSGerrit Renker 		return val <= DCCPF_ACK_RATIO_MAX;
474e8ef967aSGerrit Renker 	case DCCPF_SEQUENCE_WINDOW:
475e8ef967aSGerrit Renker 		return val >= DCCPF_SEQ_WMIN && val <= DCCPF_SEQ_WMAX;
476e8ef967aSGerrit Renker 	}
477e8ef967aSGerrit Renker 	return 0;	/* feature unknown - so we can't tell */
478e8ef967aSGerrit Renker }
479e8ef967aSGerrit Renker 
480e8ef967aSGerrit Renker /* check that SP values are within the ranges defined in RFC 4340 */
481e8ef967aSGerrit Renker static u8 dccp_feat_is_valid_sp_val(u8 feat_num, u8 val)
482e8ef967aSGerrit Renker {
483e8ef967aSGerrit Renker 	switch (feat_num) {
484e8ef967aSGerrit Renker 	case DCCPF_CCID:
485e8ef967aSGerrit Renker 		return val == DCCPC_CCID2 || val == DCCPC_CCID3;
486e8ef967aSGerrit Renker 	/* Type-check Boolean feature values: */
487e8ef967aSGerrit Renker 	case DCCPF_SHORT_SEQNOS:
488e8ef967aSGerrit Renker 	case DCCPF_ECN_INCAPABLE:
489e8ef967aSGerrit Renker 	case DCCPF_SEND_ACK_VECTOR:
490e8ef967aSGerrit Renker 	case DCCPF_SEND_NDP_COUNT:
491e8ef967aSGerrit Renker 	case DCCPF_DATA_CHECKSUM:
492e8ef967aSGerrit Renker 	case DCCPF_SEND_LEV_RATE:
493e8ef967aSGerrit Renker 		return val < 2;
494e8ef967aSGerrit Renker 	case DCCPF_MIN_CSUM_COVER:
495e8ef967aSGerrit Renker 		return val < 16;
496e8ef967aSGerrit Renker 	}
497e8ef967aSGerrit Renker 	return 0;			/* feature unknown */
498e8ef967aSGerrit Renker }
499e8ef967aSGerrit Renker 
500e8ef967aSGerrit Renker static u8 dccp_feat_sp_list_ok(u8 feat_num, u8 const *sp_list, u8 sp_len)
501e8ef967aSGerrit Renker {
502e8ef967aSGerrit Renker 	if (sp_list == NULL || sp_len < 1)
503e8ef967aSGerrit Renker 		return 0;
504e8ef967aSGerrit Renker 	while (sp_len--)
505e8ef967aSGerrit Renker 		if (!dccp_feat_is_valid_sp_val(feat_num, *sp_list++))
506e8ef967aSGerrit Renker 			return 0;
507e8ef967aSGerrit Renker 	return 1;
508e8ef967aSGerrit Renker }
509e8ef967aSGerrit Renker 
510e8ef967aSGerrit Renker /**
5110971d17cSGerrit Renker  * dccp_feat_insert_opts  -  Generate FN options from current list state
5120971d17cSGerrit Renker  * @skb: next sk_buff to be sent to the peer
5130971d17cSGerrit Renker  * @dp: for client during handshake and general negotiation
5140971d17cSGerrit Renker  * @dreq: used by the server only (all Changes/Confirms in LISTEN/RESPOND)
5150971d17cSGerrit Renker  */
5160971d17cSGerrit Renker int dccp_feat_insert_opts(struct dccp_sock *dp, struct dccp_request_sock *dreq,
5170971d17cSGerrit Renker 			  struct sk_buff *skb)
5180971d17cSGerrit Renker {
5190971d17cSGerrit Renker 	struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
5200971d17cSGerrit Renker 	struct dccp_feat_entry *pos, *next;
5210971d17cSGerrit Renker 	u8 opt, type, len, *ptr, nn_in_nbo[DCCP_OPTVAL_MAXLEN];
5220971d17cSGerrit Renker 	bool rpt;
5230971d17cSGerrit Renker 
5240971d17cSGerrit Renker 	/* put entries into @skb in the order they appear in the list */
5250971d17cSGerrit Renker 	list_for_each_entry_safe_reverse(pos, next, fn, node) {
5260971d17cSGerrit Renker 		opt  = dccp_feat_genopt(pos);
5270971d17cSGerrit Renker 		type = dccp_feat_type(pos->feat_num);
5280971d17cSGerrit Renker 		rpt  = false;
5290971d17cSGerrit Renker 
5300971d17cSGerrit Renker 		if (pos->empty_confirm) {
5310971d17cSGerrit Renker 			len = 0;
5320971d17cSGerrit Renker 			ptr = NULL;
5330971d17cSGerrit Renker 		} else {
5340971d17cSGerrit Renker 			if (type == FEAT_SP) {
5350971d17cSGerrit Renker 				len = pos->val.sp.len;
5360971d17cSGerrit Renker 				ptr = pos->val.sp.vec;
5370971d17cSGerrit Renker 				rpt = pos->needs_confirm;
5380971d17cSGerrit Renker 			} else if (type == FEAT_NN) {
5390971d17cSGerrit Renker 				len = dccp_feat_valid_nn_length(pos->feat_num);
5400971d17cSGerrit Renker 				ptr = nn_in_nbo;
5410971d17cSGerrit Renker 				dccp_encode_value_var(pos->val.nn, ptr, len);
5420971d17cSGerrit Renker 			} else {
5430971d17cSGerrit Renker 				DCCP_BUG("unknown feature %u", pos->feat_num);
5440971d17cSGerrit Renker 				return -1;
5450971d17cSGerrit Renker 			}
5460971d17cSGerrit Renker 		}
5470971d17cSGerrit Renker 
5480971d17cSGerrit Renker 		if (dccp_insert_fn_opt(skb, opt, pos->feat_num, ptr, len, rpt))
5490971d17cSGerrit Renker 			return -1;
5500971d17cSGerrit Renker 		if (pos->needs_mandatory && dccp_insert_option_mandatory(skb))
5510971d17cSGerrit Renker 			return -1;
5520971d17cSGerrit Renker 		/*
5530971d17cSGerrit Renker 		 * Enter CHANGING after transmitting the Change option (6.6.2).
5540971d17cSGerrit Renker 		 */
5550971d17cSGerrit Renker 		if (pos->state == FEAT_INITIALISING)
5560971d17cSGerrit Renker 			pos->state = FEAT_CHANGING;
5570971d17cSGerrit Renker 	}
5580971d17cSGerrit Renker 	return 0;
5590971d17cSGerrit Renker }
5600971d17cSGerrit Renker 
5610971d17cSGerrit Renker /**
562e8ef967aSGerrit Renker  * __feat_register_nn  -  Register new NN value on socket
563e8ef967aSGerrit Renker  * @fn: feature-negotiation list to register with
564e8ef967aSGerrit Renker  * @feat: an NN feature from %dccp_feature_numbers
565e8ef967aSGerrit Renker  * @mandatory: use Mandatory option if 1
566e8ef967aSGerrit Renker  * @nn_val: value to register (restricted to 4 bytes)
567e8ef967aSGerrit Renker  * Note that NN features are local by definition (RFC 4340, 6.3.2).
568e8ef967aSGerrit Renker  */
569e8ef967aSGerrit Renker static int __feat_register_nn(struct list_head *fn, u8 feat,
570e8ef967aSGerrit Renker 			      u8 mandatory, u64 nn_val)
571e8ef967aSGerrit Renker {
572e8ef967aSGerrit Renker 	dccp_feat_val fval = { .nn = nn_val };
573e8ef967aSGerrit Renker 
574e8ef967aSGerrit Renker 	if (dccp_feat_type(feat) != FEAT_NN ||
575e8ef967aSGerrit Renker 	    !dccp_feat_is_valid_nn_val(feat, nn_val))
576e8ef967aSGerrit Renker 		return -EINVAL;
577e8ef967aSGerrit Renker 
578e8ef967aSGerrit Renker 	/* Don't bother with default values, they will be activated anyway. */
579e8ef967aSGerrit Renker 	if (nn_val - (u64)dccp_feat_default_value(feat) == 0)
580e8ef967aSGerrit Renker 		return 0;
581e8ef967aSGerrit Renker 
582e8ef967aSGerrit Renker 	return dccp_feat_push_change(fn, feat, 1, mandatory, &fval);
583e8ef967aSGerrit Renker }
584e8ef967aSGerrit Renker 
585e8ef967aSGerrit Renker /**
586e8ef967aSGerrit Renker  * __feat_register_sp  -  Register new SP value/list on socket
587e8ef967aSGerrit Renker  * @fn: feature-negotiation list to register with
588e8ef967aSGerrit Renker  * @feat: an SP feature from %dccp_feature_numbers
589e8ef967aSGerrit Renker  * @is_local: whether the local (1) or the remote (0) @feat is meant
590e8ef967aSGerrit Renker  * @mandatory: use Mandatory option if 1
591e8ef967aSGerrit Renker  * @sp_val: SP value followed by optional preference list
592e8ef967aSGerrit Renker  * @sp_len: length of @sp_val in bytes
593e8ef967aSGerrit Renker  */
594e8ef967aSGerrit Renker static int __feat_register_sp(struct list_head *fn, u8 feat, u8 is_local,
595e8ef967aSGerrit Renker 			      u8 mandatory, u8 const *sp_val, u8 sp_len)
596e8ef967aSGerrit Renker {
597e8ef967aSGerrit Renker 	dccp_feat_val fval;
598e8ef967aSGerrit Renker 
599e8ef967aSGerrit Renker 	if (dccp_feat_type(feat) != FEAT_SP ||
600e8ef967aSGerrit Renker 	    !dccp_feat_sp_list_ok(feat, sp_val, sp_len))
601e8ef967aSGerrit Renker 		return -EINVAL;
602e8ef967aSGerrit Renker 
603d90ebcbfSGerrit Renker 	/* Avoid negotiating alien CCIDs by only advertising supported ones */
604d90ebcbfSGerrit Renker 	if (feat == DCCPF_CCID && !ccid_support_check(sp_val, sp_len))
605d90ebcbfSGerrit Renker 		return -EOPNOTSUPP;
606d90ebcbfSGerrit Renker 
607e8ef967aSGerrit Renker 	if (dccp_feat_clone_sp_val(&fval, sp_val, sp_len))
608e8ef967aSGerrit Renker 		return -ENOMEM;
609e8ef967aSGerrit Renker 
610e8ef967aSGerrit Renker 	return dccp_feat_push_change(fn, feat, is_local, mandatory, &fval);
611e8ef967aSGerrit Renker }
612e8ef967aSGerrit Renker 
61349aebc66SGerrit Renker /**
61449aebc66SGerrit Renker  * dccp_feat_register_sp  -  Register requests to change SP feature values
61549aebc66SGerrit Renker  * @sk: client or listening socket
61649aebc66SGerrit Renker  * @feat: one of %dccp_feature_numbers
61749aebc66SGerrit Renker  * @is_local: whether the local (1) or remote (0) @feat is meant
61849aebc66SGerrit Renker  * @list: array of preferred values, in descending order of preference
61949aebc66SGerrit Renker  * @len: length of @list in bytes
62049aebc66SGerrit Renker  */
62149aebc66SGerrit Renker int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
62249aebc66SGerrit Renker 			  u8 const *list, u8 len)
62349aebc66SGerrit Renker {	 /* any changes must be registered before establishing the connection */
62449aebc66SGerrit Renker 	if (sk->sk_state != DCCP_CLOSED)
62549aebc66SGerrit Renker 		return -EISCONN;
62649aebc66SGerrit Renker 	if (dccp_feat_type(feat) != FEAT_SP)
62719443178SChris Wright 		return -EINVAL;
62849aebc66SGerrit Renker 	return __feat_register_sp(&dccp_sk(sk)->dccps_featneg, feat, is_local,
62949aebc66SGerrit Renker 				  0, list, len);
630afe00251SAndrea Bittau }
631afe00251SAndrea Bittau 
63249aebc66SGerrit Renker /* Analogous to dccp_feat_register_sp(), but for non-negotiable values */
63349aebc66SGerrit Renker int dccp_feat_register_nn(struct sock *sk, u8 feat, u64 val)
63449aebc66SGerrit Renker {
63549aebc66SGerrit Renker 	/* any changes must be registered before establishing the connection */
63649aebc66SGerrit Renker 	if (sk->sk_state != DCCP_CLOSED)
63749aebc66SGerrit Renker 		return -EISCONN;
63849aebc66SGerrit Renker 	if (dccp_feat_type(feat) != FEAT_NN)
63949aebc66SGerrit Renker 		return -EINVAL;
64049aebc66SGerrit Renker 	return __feat_register_nn(&dccp_sk(sk)->dccps_featneg, feat, 0, val);
641afe00251SAndrea Bittau }
642afe00251SAndrea Bittau 
6439eca0a47SGerrit Renker /*
6449eca0a47SGerrit Renker  *	Tracking features whose value depend on the choice of CCID
6459eca0a47SGerrit Renker  *
6469eca0a47SGerrit Renker  * This is designed with an extension in mind so that a list walk could be done
6479eca0a47SGerrit Renker  * before activating any features. However, the existing framework was found to
6489eca0a47SGerrit Renker  * work satisfactorily up until now, the automatic verification is left open.
6499eca0a47SGerrit Renker  * When adding new CCIDs, add a corresponding dependency table here.
6509eca0a47SGerrit Renker  */
6519eca0a47SGerrit Renker static const struct ccid_dependency *dccp_feat_ccid_deps(u8 ccid, bool is_local)
6529eca0a47SGerrit Renker {
6539eca0a47SGerrit Renker 	static const struct ccid_dependency ccid2_dependencies[2][2] = {
6549eca0a47SGerrit Renker 		/*
6559eca0a47SGerrit Renker 		 * CCID2 mandates Ack Vectors (RFC 4341, 4.): as CCID is a TX
6569eca0a47SGerrit Renker 		 * feature and Send Ack Vector is an RX feature, `is_local'
6579eca0a47SGerrit Renker 		 * needs to be reversed.
6589eca0a47SGerrit Renker 		 */
6599eca0a47SGerrit Renker 		{	/* Dependencies of the receiver-side (remote) CCID2 */
6609eca0a47SGerrit Renker 			{
6619eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
6629eca0a47SGerrit Renker 				.is_local	= true,
6639eca0a47SGerrit Renker 				.is_mandatory	= true,
6649eca0a47SGerrit Renker 				.val		= 1
6659eca0a47SGerrit Renker 			},
6669eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
6679eca0a47SGerrit Renker 		},
6689eca0a47SGerrit Renker 		{	/* Dependencies of the sender-side (local) CCID2 */
6699eca0a47SGerrit Renker 			{
6709eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
6719eca0a47SGerrit Renker 				.is_local	= false,
6729eca0a47SGerrit Renker 				.is_mandatory	= true,
6739eca0a47SGerrit Renker 				.val		= 1
6749eca0a47SGerrit Renker 			},
6759eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
6769eca0a47SGerrit Renker 		}
6779eca0a47SGerrit Renker 	};
6789eca0a47SGerrit Renker 	static const struct ccid_dependency ccid3_dependencies[2][5] = {
6799eca0a47SGerrit Renker 		{	/*
6809eca0a47SGerrit Renker 			 * Dependencies of the receiver-side CCID3
6819eca0a47SGerrit Renker 			 */
6829eca0a47SGerrit Renker 			{	/* locally disable Ack Vectors */
6839eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
6849eca0a47SGerrit Renker 				.is_local	= true,
6859eca0a47SGerrit Renker 				.is_mandatory	= false,
6869eca0a47SGerrit Renker 				.val		= 0
6879eca0a47SGerrit Renker 			},
6889eca0a47SGerrit Renker 			{	/* see below why Send Loss Event Rate is on */
6899eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_LEV_RATE,
6909eca0a47SGerrit Renker 				.is_local	= true,
6919eca0a47SGerrit Renker 				.is_mandatory	= true,
6929eca0a47SGerrit Renker 				.val		= 1
6939eca0a47SGerrit Renker 			},
6949eca0a47SGerrit Renker 			{	/* NDP Count is needed as per RFC 4342, 6.1.1 */
6959eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_NDP_COUNT,
6969eca0a47SGerrit Renker 				.is_local	= false,
6979eca0a47SGerrit Renker 				.is_mandatory	= true,
6989eca0a47SGerrit Renker 				.val		= 1
6999eca0a47SGerrit Renker 			},
7009eca0a47SGerrit Renker 			{ 0, 0, 0, 0 },
7019eca0a47SGerrit Renker 		},
7029eca0a47SGerrit Renker 		{	/*
7039eca0a47SGerrit Renker 			 * CCID3 at the TX side: we request that the HC-receiver
7049eca0a47SGerrit Renker 			 * will not send Ack Vectors (they will be ignored, so
7059eca0a47SGerrit Renker 			 * Mandatory is not set); we enable Send Loss Event Rate
7069eca0a47SGerrit Renker 			 * (Mandatory since the implementation does not support
7079eca0a47SGerrit Renker 			 * the Loss Intervals option of RFC 4342, 8.6).
7089eca0a47SGerrit Renker 			 * The last two options are for peer's information only.
7099eca0a47SGerrit Renker 			*/
7109eca0a47SGerrit Renker 			{
7119eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_ACK_VECTOR,
7129eca0a47SGerrit Renker 				.is_local	= false,
7139eca0a47SGerrit Renker 				.is_mandatory	= false,
7149eca0a47SGerrit Renker 				.val		= 0
7159eca0a47SGerrit Renker 			},
7169eca0a47SGerrit Renker 			{
7179eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_LEV_RATE,
7189eca0a47SGerrit Renker 				.is_local	= false,
7199eca0a47SGerrit Renker 				.is_mandatory	= true,
7209eca0a47SGerrit Renker 				.val		= 1
7219eca0a47SGerrit Renker 			},
7229eca0a47SGerrit Renker 			{	/* this CCID does not support Ack Ratio */
7239eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_ACK_RATIO,
7249eca0a47SGerrit Renker 				.is_local	= true,
7259eca0a47SGerrit Renker 				.is_mandatory	= false,
7269eca0a47SGerrit Renker 				.val		= 0
7279eca0a47SGerrit Renker 			},
7289eca0a47SGerrit Renker 			{	/* tell receiver we are sending NDP counts */
7299eca0a47SGerrit Renker 				.dependent_feat	= DCCPF_SEND_NDP_COUNT,
7309eca0a47SGerrit Renker 				.is_local	= true,
7319eca0a47SGerrit Renker 				.is_mandatory	= false,
7329eca0a47SGerrit Renker 				.val		= 1
7339eca0a47SGerrit Renker 			},
7349eca0a47SGerrit Renker 			{ 0, 0, 0, 0 }
7359eca0a47SGerrit Renker 		}
7369eca0a47SGerrit Renker 	};
7379eca0a47SGerrit Renker 	switch (ccid) {
7389eca0a47SGerrit Renker 	case DCCPC_CCID2:
7399eca0a47SGerrit Renker 		return ccid2_dependencies[is_local];
7409eca0a47SGerrit Renker 	case DCCPC_CCID3:
7419eca0a47SGerrit Renker 		return ccid3_dependencies[is_local];
7429eca0a47SGerrit Renker 	default:
7439eca0a47SGerrit Renker 		return NULL;
7449eca0a47SGerrit Renker 	}
7459eca0a47SGerrit Renker }
7469eca0a47SGerrit Renker 
7479eca0a47SGerrit Renker /**
7489eca0a47SGerrit Renker  * dccp_feat_propagate_ccid - Resolve dependencies of features on choice of CCID
7499eca0a47SGerrit Renker  * @fn: feature-negotiation list to update
7509eca0a47SGerrit Renker  * @id: CCID number to track
7519eca0a47SGerrit Renker  * @is_local: whether TX CCID (1) or RX CCID (0) is meant
7529eca0a47SGerrit Renker  * This function needs to be called after registering all other features.
7539eca0a47SGerrit Renker  */
7549eca0a47SGerrit Renker static int dccp_feat_propagate_ccid(struct list_head *fn, u8 id, bool is_local)
7559eca0a47SGerrit Renker {
7569eca0a47SGerrit Renker 	const struct ccid_dependency *table = dccp_feat_ccid_deps(id, is_local);
7579eca0a47SGerrit Renker 	int i, rc = (table == NULL);
7589eca0a47SGerrit Renker 
7599eca0a47SGerrit Renker 	for (i = 0; rc == 0 && table[i].dependent_feat != DCCPF_RESERVED; i++)
7609eca0a47SGerrit Renker 		if (dccp_feat_type(table[i].dependent_feat) == FEAT_SP)
7619eca0a47SGerrit Renker 			rc = __feat_register_sp(fn, table[i].dependent_feat,
7629eca0a47SGerrit Renker 						    table[i].is_local,
7639eca0a47SGerrit Renker 						    table[i].is_mandatory,
7649eca0a47SGerrit Renker 						    &table[i].val, 1);
7659eca0a47SGerrit Renker 		else
7669eca0a47SGerrit Renker 			rc = __feat_register_nn(fn, table[i].dependent_feat,
7679eca0a47SGerrit Renker 						    table[i].is_mandatory,
7689eca0a47SGerrit Renker 						    table[i].val);
7699eca0a47SGerrit Renker 	return rc;
7709eca0a47SGerrit Renker }
7719eca0a47SGerrit Renker 
7729eca0a47SGerrit Renker /**
7739eca0a47SGerrit Renker  * dccp_feat_finalise_settings  -  Finalise settings before starting negotiation
7749eca0a47SGerrit Renker  * @dp: client or listening socket (settings will be inherited)
7759eca0a47SGerrit Renker  * This is called after all registrations (socket initialisation, sysctls, and
7769eca0a47SGerrit Renker  * sockopt calls), and before sending the first packet containing Change options
7779eca0a47SGerrit Renker  * (ie. client-Request or server-Response), to ensure internal consistency.
7789eca0a47SGerrit Renker  */
7799eca0a47SGerrit Renker int dccp_feat_finalise_settings(struct dccp_sock *dp)
7809eca0a47SGerrit Renker {
7819eca0a47SGerrit Renker 	struct list_head *fn = &dp->dccps_featneg;
7829eca0a47SGerrit Renker 	struct dccp_feat_entry *entry;
7839eca0a47SGerrit Renker 	int i = 2, ccids[2] = { -1, -1 };
7849eca0a47SGerrit Renker 
7859eca0a47SGerrit Renker 	/*
7869eca0a47SGerrit Renker 	 * Propagating CCIDs:
7879eca0a47SGerrit Renker 	 * 1) not useful to propagate CCID settings if this host advertises more
7889eca0a47SGerrit Renker 	 *    than one CCID: the choice of CCID  may still change - if this is
7899eca0a47SGerrit Renker 	 *    the client, or if this is the server and the client sends
7909eca0a47SGerrit Renker 	 *    singleton CCID values.
7919eca0a47SGerrit Renker 	 * 2) since is that propagate_ccid changes the list, we defer changing
7929eca0a47SGerrit Renker 	 *    the sorted list until after the traversal.
7939eca0a47SGerrit Renker 	 */
7949eca0a47SGerrit Renker 	list_for_each_entry(entry, fn, node)
7959eca0a47SGerrit Renker 		if (entry->feat_num == DCCPF_CCID && entry->val.sp.len == 1)
7969eca0a47SGerrit Renker 			ccids[entry->is_local] = entry->val.sp.vec[0];
7979eca0a47SGerrit Renker 	while (i--)
7989eca0a47SGerrit Renker 		if (ccids[i] > 0 && dccp_feat_propagate_ccid(fn, ccids[i], i))
7999eca0a47SGerrit Renker 			return -1;
8009eca0a47SGerrit Renker 	return 0;
8019eca0a47SGerrit Renker }
8029eca0a47SGerrit Renker 
8030c116839SGerrit Renker /**
8040c116839SGerrit Renker  * dccp_feat_server_ccid_dependencies  -  Resolve CCID-dependent features
8050c116839SGerrit Renker  * It is the server which resolves the dependencies once the CCID has been
8060c116839SGerrit Renker  * fully negotiated. If no CCID has been negotiated, it uses the default CCID.
8070c116839SGerrit Renker  */
8080c116839SGerrit Renker int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq)
8090c116839SGerrit Renker {
8100c116839SGerrit Renker 	struct list_head *fn = &dreq->dreq_featneg;
8110c116839SGerrit Renker 	struct dccp_feat_entry *entry;
8120c116839SGerrit Renker 	u8 is_local, ccid;
8130c116839SGerrit Renker 
8140c116839SGerrit Renker 	for (is_local = 0; is_local <= 1; is_local++) {
8150c116839SGerrit Renker 		entry = dccp_feat_list_lookup(fn, DCCPF_CCID, is_local);
8160c116839SGerrit Renker 
8170c116839SGerrit Renker 		if (entry != NULL && !entry->empty_confirm)
8180c116839SGerrit Renker 			ccid = entry->val.sp.vec[0];
8190c116839SGerrit Renker 		else
8200c116839SGerrit Renker 			ccid = dccp_feat_default_value(DCCPF_CCID);
8210c116839SGerrit Renker 
8220c116839SGerrit Renker 		if (dccp_feat_propagate_ccid(fn, ccid, is_local))
8230c116839SGerrit Renker 			return -1;
8240c116839SGerrit Renker 	}
8250c116839SGerrit Renker 	return 0;
8260c116839SGerrit Renker }
8270c116839SGerrit Renker 
82875757a7dSGerrit Renker /* Select the first entry in @servlist that also occurs in @clilist (6.3.1) */
82975757a7dSGerrit Renker static int dccp_feat_preflist_match(u8 *servlist, u8 slen, u8 *clilist, u8 clen)
83075757a7dSGerrit Renker {
83175757a7dSGerrit Renker 	u8 c, s;
83275757a7dSGerrit Renker 
83375757a7dSGerrit Renker 	for (s = 0; s < slen; s++)
83475757a7dSGerrit Renker 		for (c = 0; c < clen; c++)
83575757a7dSGerrit Renker 			if (servlist[s] == clilist[c])
83675757a7dSGerrit Renker 				return servlist[s];
83775757a7dSGerrit Renker 	return -1;
83875757a7dSGerrit Renker }
83975757a7dSGerrit Renker 
84075757a7dSGerrit Renker /**
84175757a7dSGerrit Renker  * dccp_feat_prefer  -  Move preferred entry to the start of array
84275757a7dSGerrit Renker  * Reorder the @array_len elements in @array so that @preferred_value comes
84375757a7dSGerrit Renker  * first. Returns >0 to indicate that @preferred_value does occur in @array.
84475757a7dSGerrit Renker  */
84575757a7dSGerrit Renker static u8 dccp_feat_prefer(u8 preferred_value, u8 *array, u8 array_len)
84675757a7dSGerrit Renker {
84775757a7dSGerrit Renker 	u8 i, does_occur = 0;
84875757a7dSGerrit Renker 
84975757a7dSGerrit Renker 	if (array != NULL) {
85075757a7dSGerrit Renker 		for (i = 0; i < array_len; i++)
85175757a7dSGerrit Renker 			if (array[i] == preferred_value) {
85275757a7dSGerrit Renker 				array[i] = array[0];
85375757a7dSGerrit Renker 				does_occur++;
85475757a7dSGerrit Renker 			}
85575757a7dSGerrit Renker 		if (does_occur)
85675757a7dSGerrit Renker 			array[0] = preferred_value;
85775757a7dSGerrit Renker 	}
85875757a7dSGerrit Renker 	return does_occur;
85975757a7dSGerrit Renker }
86075757a7dSGerrit Renker 
86175757a7dSGerrit Renker /**
86275757a7dSGerrit Renker  * dccp_feat_reconcile  -  Reconcile SP preference lists
86375757a7dSGerrit Renker  *  @fval: SP list to reconcile into
86475757a7dSGerrit Renker  *  @arr: received SP preference list
86575757a7dSGerrit Renker  *  @len: length of @arr in bytes
86675757a7dSGerrit Renker  *  @is_server: whether this side is the server (and @fv is the server's list)
86775757a7dSGerrit Renker  *  @reorder: whether to reorder the list in @fv after reconciling with @arr
86875757a7dSGerrit Renker  * When successful, > 0 is returned and the reconciled list is in @fval.
86975757a7dSGerrit Renker  * A value of 0 means that negotiation failed (no shared entry).
87075757a7dSGerrit Renker  */
87175757a7dSGerrit Renker static int dccp_feat_reconcile(dccp_feat_val *fv, u8 *arr, u8 len,
87275757a7dSGerrit Renker 			       bool is_server, bool reorder)
87375757a7dSGerrit Renker {
87475757a7dSGerrit Renker 	int rc;
87575757a7dSGerrit Renker 
87675757a7dSGerrit Renker 	if (!fv->sp.vec || !arr) {
87775757a7dSGerrit Renker 		DCCP_CRIT("NULL feature value or array");
87875757a7dSGerrit Renker 		return 0;
87975757a7dSGerrit Renker 	}
88075757a7dSGerrit Renker 
88175757a7dSGerrit Renker 	if (is_server)
88275757a7dSGerrit Renker 		rc = dccp_feat_preflist_match(fv->sp.vec, fv->sp.len, arr, len);
88375757a7dSGerrit Renker 	else
88475757a7dSGerrit Renker 		rc = dccp_feat_preflist_match(arr, len, fv->sp.vec, fv->sp.len);
88575757a7dSGerrit Renker 
88675757a7dSGerrit Renker 	if (!reorder)
88775757a7dSGerrit Renker 		return rc;
88875757a7dSGerrit Renker 	if (rc < 0)
88975757a7dSGerrit Renker 		return 0;
89075757a7dSGerrit Renker 
89175757a7dSGerrit Renker 	/*
89275757a7dSGerrit Renker 	 * Reorder list: used for activating features and in dccp_insert_fn_opt.
89375757a7dSGerrit Renker 	 */
89475757a7dSGerrit Renker 	return dccp_feat_prefer(rc, fv->sp.vec, fv->sp.len);
89575757a7dSGerrit Renker }
89675757a7dSGerrit Renker 
897e77b8363SGerrit Renker /**
898e77b8363SGerrit Renker  * dccp_feat_change_recv  -  Process incoming ChangeL/R options
899e77b8363SGerrit Renker  * @fn: feature-negotiation list to update
900e77b8363SGerrit Renker  * @is_mandatory: whether the Change was preceded by a Mandatory option
901e77b8363SGerrit Renker  * @opt: %DCCPO_CHANGE_L or %DCCPO_CHANGE_R
902e77b8363SGerrit Renker  * @feat: one of %dccp_feature_numbers
903e77b8363SGerrit Renker  * @val: NN value or SP value/preference list
904e77b8363SGerrit Renker  * @len: length of @val in bytes
905e77b8363SGerrit Renker  * @server: whether this node is the server (1) or the client (0)
906e77b8363SGerrit Renker  */
907e77b8363SGerrit Renker static u8 dccp_feat_change_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
908e77b8363SGerrit Renker 				u8 feat, u8 *val, u8 len, const bool server)
909e77b8363SGerrit Renker {
910e77b8363SGerrit Renker 	u8 defval, type = dccp_feat_type(feat);
911e77b8363SGerrit Renker 	const bool local = (opt == DCCPO_CHANGE_R);
912e77b8363SGerrit Renker 	struct dccp_feat_entry *entry;
913e77b8363SGerrit Renker 	dccp_feat_val fval;
914e77b8363SGerrit Renker 
915e77b8363SGerrit Renker 	if (len == 0 || type == FEAT_UNKNOWN)		/* 6.1 and 6.6.8 */
916e77b8363SGerrit Renker 		goto unknown_feature_or_value;
917e77b8363SGerrit Renker 
918e77b8363SGerrit Renker 	/*
919e77b8363SGerrit Renker 	 *	Negotiation of NN features: Change R is invalid, so there is no
920e77b8363SGerrit Renker 	 *	simultaneous negotiation; hence we do not look up in the list.
921e77b8363SGerrit Renker 	 */
922e77b8363SGerrit Renker 	if (type == FEAT_NN) {
923e77b8363SGerrit Renker 		if (local || len > sizeof(fval.nn))
924e77b8363SGerrit Renker 			goto unknown_feature_or_value;
925e77b8363SGerrit Renker 
926e77b8363SGerrit Renker 		/* 6.3.2: "The feature remote MUST accept any valid value..." */
927e77b8363SGerrit Renker 		fval.nn = dccp_decode_value_var(val, len);
928e77b8363SGerrit Renker 		if (!dccp_feat_is_valid_nn_val(feat, fval.nn))
929e77b8363SGerrit Renker 			goto unknown_feature_or_value;
930e77b8363SGerrit Renker 
931e77b8363SGerrit Renker 		return dccp_feat_push_confirm(fn, feat, local, &fval);
932e77b8363SGerrit Renker 	}
933e77b8363SGerrit Renker 
934e77b8363SGerrit Renker 	/*
935e77b8363SGerrit Renker 	 *	Unidirectional/simultaneous negotiation of SP features (6.3.1)
936e77b8363SGerrit Renker 	 */
937e77b8363SGerrit Renker 	entry = dccp_feat_list_lookup(fn, feat, local);
938e77b8363SGerrit Renker 	if (entry == NULL) {
939e77b8363SGerrit Renker 		/*
940e77b8363SGerrit Renker 		 * No particular preferences have been registered. We deal with
941e77b8363SGerrit Renker 		 * this situation by assuming that all valid values are equally
942e77b8363SGerrit Renker 		 * acceptable, and apply the following checks:
943e77b8363SGerrit Renker 		 * - if the peer's list is a singleton, we accept a valid value;
944e77b8363SGerrit Renker 		 * - if we are the server, we first try to see if the peer (the
945e77b8363SGerrit Renker 		 *   client) advertises the default value. If yes, we use it,
946e77b8363SGerrit Renker 		 *   otherwise we accept the preferred value;
947e77b8363SGerrit Renker 		 * - else if we are the client, we use the first list element.
948e77b8363SGerrit Renker 		 */
949e77b8363SGerrit Renker 		if (dccp_feat_clone_sp_val(&fval, val, 1))
950e77b8363SGerrit Renker 			return DCCP_RESET_CODE_TOO_BUSY;
951e77b8363SGerrit Renker 
952e77b8363SGerrit Renker 		if (len > 1 && server) {
953e77b8363SGerrit Renker 			defval = dccp_feat_default_value(feat);
954e77b8363SGerrit Renker 			if (dccp_feat_preflist_match(&defval, 1, val, len) > -1)
955e77b8363SGerrit Renker 				fval.sp.vec[0] = defval;
956e77b8363SGerrit Renker 		} else if (!dccp_feat_is_valid_sp_val(feat, fval.sp.vec[0])) {
957e77b8363SGerrit Renker 			kfree(fval.sp.vec);
958e77b8363SGerrit Renker 			goto unknown_feature_or_value;
959e77b8363SGerrit Renker 		}
960e77b8363SGerrit Renker 
961e77b8363SGerrit Renker 		/* Treat unsupported CCIDs like invalid values */
962e77b8363SGerrit Renker 		if (feat == DCCPF_CCID && !ccid_support_check(fval.sp.vec, 1)) {
963e77b8363SGerrit Renker 			kfree(fval.sp.vec);
964e77b8363SGerrit Renker 			goto not_valid_or_not_known;
965e77b8363SGerrit Renker 		}
966e77b8363SGerrit Renker 
967e77b8363SGerrit Renker 		return dccp_feat_push_confirm(fn, feat, local, &fval);
968e77b8363SGerrit Renker 
969e77b8363SGerrit Renker 	} else if (entry->state == FEAT_UNSTABLE) {	/* 6.6.2 */
970e77b8363SGerrit Renker 		return 0;
971e77b8363SGerrit Renker 	}
972e77b8363SGerrit Renker 
973e77b8363SGerrit Renker 	if (dccp_feat_reconcile(&entry->val, val, len, server, true)) {
974e77b8363SGerrit Renker 		entry->empty_confirm = 0;
975e77b8363SGerrit Renker 	} else if (is_mandatory) {
976e77b8363SGerrit Renker 		return DCCP_RESET_CODE_MANDATORY_ERROR;
977e77b8363SGerrit Renker 	} else if (entry->state == FEAT_INITIALISING) {
978e77b8363SGerrit Renker 		/*
979e77b8363SGerrit Renker 		 * Failed simultaneous negotiation (server only): try to `save'
980e77b8363SGerrit Renker 		 * the connection by checking whether entry contains the default
981e77b8363SGerrit Renker 		 * value for @feat. If yes, send an empty Confirm to signal that
982e77b8363SGerrit Renker 		 * the received Change was not understood - which implies using
983e77b8363SGerrit Renker 		 * the default value.
984e77b8363SGerrit Renker 		 * If this also fails, we use Reset as the last resort.
985e77b8363SGerrit Renker 		 */
986e77b8363SGerrit Renker 		WARN_ON(!server);
987e77b8363SGerrit Renker 		defval = dccp_feat_default_value(feat);
988e77b8363SGerrit Renker 		if (!dccp_feat_reconcile(&entry->val, &defval, 1, server, true))
989e77b8363SGerrit Renker 			return DCCP_RESET_CODE_OPTION_ERROR;
990e77b8363SGerrit Renker 		entry->empty_confirm = 1;
991e77b8363SGerrit Renker 	}
992e77b8363SGerrit Renker 	entry->needs_confirm   = 1;
993e77b8363SGerrit Renker 	entry->needs_mandatory = 0;
994e77b8363SGerrit Renker 	entry->state	       = FEAT_STABLE;
995e77b8363SGerrit Renker 	return 0;
996e77b8363SGerrit Renker 
997e77b8363SGerrit Renker unknown_feature_or_value:
998e77b8363SGerrit Renker 	if (!is_mandatory)
999e77b8363SGerrit Renker 		return dccp_push_empty_confirm(fn, feat, local);
1000e77b8363SGerrit Renker 
1001e77b8363SGerrit Renker not_valid_or_not_known:
1002e77b8363SGerrit Renker 	return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1003e77b8363SGerrit Renker 			    : DCCP_RESET_CODE_OPTION_ERROR;
1004e77b8363SGerrit Renker }
1005e77b8363SGerrit Renker 
1006e77b8363SGerrit Renker /**
1007b1ad0042SGerrit Renker  * dccp_feat_confirm_recv  -  Process received Confirm options
1008b1ad0042SGerrit Renker  * @fn: feature-negotiation list to update
1009b1ad0042SGerrit Renker  * @is_mandatory: whether @opt was preceded by a Mandatory option
1010b1ad0042SGerrit Renker  * @opt: %DCCPO_CONFIRM_L or %DCCPO_CONFIRM_R
1011b1ad0042SGerrit Renker  * @feat: one of %dccp_feature_numbers
1012b1ad0042SGerrit Renker  * @val: NN value or SP value/preference list
1013b1ad0042SGerrit Renker  * @len: length of @val in bytes
1014b1ad0042SGerrit Renker  * @server: whether this node is server (1) or client (0)
1015b1ad0042SGerrit Renker  */
1016b1ad0042SGerrit Renker static u8 dccp_feat_confirm_recv(struct list_head *fn, u8 is_mandatory, u8 opt,
1017b1ad0042SGerrit Renker 				 u8 feat, u8 *val, u8 len, const bool server)
1018b1ad0042SGerrit Renker {
1019b1ad0042SGerrit Renker 	u8 *plist, plen, type = dccp_feat_type(feat);
1020b1ad0042SGerrit Renker 	const bool local = (opt == DCCPO_CONFIRM_R);
1021b1ad0042SGerrit Renker 	struct dccp_feat_entry *entry = dccp_feat_list_lookup(fn, feat, local);
1022b1ad0042SGerrit Renker 
1023b1ad0042SGerrit Renker 	if (entry == NULL) {	/* nothing queued: ignore or handle error */
1024b1ad0042SGerrit Renker 		if (is_mandatory && type == FEAT_UNKNOWN)
1025b1ad0042SGerrit Renker 			return DCCP_RESET_CODE_MANDATORY_ERROR;
1026b1ad0042SGerrit Renker 
1027b1ad0042SGerrit Renker 		if (!local && type == FEAT_NN)		/* 6.3.2 */
1028b1ad0042SGerrit Renker 			goto confirmation_failed;
1029b1ad0042SGerrit Renker 		return 0;
1030b1ad0042SGerrit Renker 	}
1031b1ad0042SGerrit Renker 
1032b1ad0042SGerrit Renker 	if (entry->state != FEAT_CHANGING)		/* 6.6.2 */
1033b1ad0042SGerrit Renker 		return 0;
1034b1ad0042SGerrit Renker 
1035b1ad0042SGerrit Renker 	if (len == 0) {
1036b1ad0042SGerrit Renker 		if (dccp_feat_must_be_understood(feat))	/* 6.6.7 */
1037b1ad0042SGerrit Renker 			goto confirmation_failed;
1038b1ad0042SGerrit Renker 		/*
1039b1ad0042SGerrit Renker 		 * Empty Confirm during connection setup: this means reverting
1040b1ad0042SGerrit Renker 		 * to the `old' value, which in this case is the default. Since
1041b1ad0042SGerrit Renker 		 * we handle default values automatically when no other values
1042b1ad0042SGerrit Renker 		 * have been set, we revert to the old value by removing this
1043b1ad0042SGerrit Renker 		 * entry from the list.
1044b1ad0042SGerrit Renker 		 */
1045b1ad0042SGerrit Renker 		dccp_feat_list_pop(entry);
1046b1ad0042SGerrit Renker 		return 0;
1047b1ad0042SGerrit Renker 	}
1048b1ad0042SGerrit Renker 
1049b1ad0042SGerrit Renker 	if (type == FEAT_NN) {
1050b1ad0042SGerrit Renker 		if (len > sizeof(entry->val.nn))
1051b1ad0042SGerrit Renker 			goto confirmation_failed;
1052b1ad0042SGerrit Renker 
1053b1ad0042SGerrit Renker 		if (entry->val.nn == dccp_decode_value_var(val, len))
1054b1ad0042SGerrit Renker 			goto confirmation_succeeded;
1055b1ad0042SGerrit Renker 
1056b1ad0042SGerrit Renker 		DCCP_WARN("Bogus Confirm for non-existing value\n");
1057b1ad0042SGerrit Renker 		goto confirmation_failed;
1058b1ad0042SGerrit Renker 	}
1059b1ad0042SGerrit Renker 
1060b1ad0042SGerrit Renker 	/*
1061b1ad0042SGerrit Renker 	 * Parsing SP Confirms: the first element of @val is the preferred
1062b1ad0042SGerrit Renker 	 * SP value which the peer confirms, the remainder depends on @len.
1063b1ad0042SGerrit Renker 	 * Note that only the confirmed value need to be a valid SP value.
1064b1ad0042SGerrit Renker 	 */
1065b1ad0042SGerrit Renker 	if (!dccp_feat_is_valid_sp_val(feat, *val))
1066b1ad0042SGerrit Renker 		goto confirmation_failed;
1067b1ad0042SGerrit Renker 
1068b1ad0042SGerrit Renker 	if (len == 1) {		/* peer didn't supply a preference list */
1069b1ad0042SGerrit Renker 		plist = val;
1070b1ad0042SGerrit Renker 		plen  = len;
1071b1ad0042SGerrit Renker 	} else {		/* preferred value + preference list */
1072b1ad0042SGerrit Renker 		plist = val + 1;
1073b1ad0042SGerrit Renker 		plen  = len - 1;
1074b1ad0042SGerrit Renker 	}
1075b1ad0042SGerrit Renker 
1076b1ad0042SGerrit Renker 	/* Check whether the peer got the reconciliation right (6.6.8) */
1077b1ad0042SGerrit Renker 	if (dccp_feat_reconcile(&entry->val, plist, plen, server, 0) != *val) {
1078b1ad0042SGerrit Renker 		DCCP_WARN("Confirm selected the wrong value %u\n", *val);
1079b1ad0042SGerrit Renker 		return DCCP_RESET_CODE_OPTION_ERROR;
1080b1ad0042SGerrit Renker 	}
1081b1ad0042SGerrit Renker 	entry->val.sp.vec[0] = *val;
1082b1ad0042SGerrit Renker 
1083b1ad0042SGerrit Renker confirmation_succeeded:
1084b1ad0042SGerrit Renker 	entry->state = FEAT_STABLE;
1085b1ad0042SGerrit Renker 	return 0;
1086b1ad0042SGerrit Renker 
1087b1ad0042SGerrit Renker confirmation_failed:
1088b1ad0042SGerrit Renker 	DCCP_WARN("Confirmation failed\n");
1089b1ad0042SGerrit Renker 	return is_mandatory ? DCCP_RESET_CODE_MANDATORY_ERROR
1090b1ad0042SGerrit Renker 			    : DCCP_RESET_CODE_OPTION_ERROR;
1091b1ad0042SGerrit Renker }
1092b1ad0042SGerrit Renker 
1093b1ad0042SGerrit Renker /**
1094e77b8363SGerrit Renker  * dccp_feat_parse_options  -  Process Feature-Negotiation Options
1095e77b8363SGerrit Renker  * @sk: for general use and used by the client during connection setup
1096e77b8363SGerrit Renker  * @dreq: used by the server during connection setup
1097e77b8363SGerrit Renker  * @mandatory: whether @opt was preceded by a Mandatory option
1098e77b8363SGerrit Renker  * @opt: %DCCPO_CHANGE_L | %DCCPO_CHANGE_R | %DCCPO_CONFIRM_L | %DCCPO_CONFIRM_R
1099e77b8363SGerrit Renker  * @feat: one of %dccp_feature_numbers
1100e77b8363SGerrit Renker  * @val: value contents of @opt
1101e77b8363SGerrit Renker  * @len: length of @val in bytes
1102e77b8363SGerrit Renker  * Returns 0 on success, a Reset code for ending the connection otherwise.
1103e77b8363SGerrit Renker  */
1104e77b8363SGerrit Renker int dccp_feat_parse_options(struct sock *sk, struct dccp_request_sock *dreq,
1105e77b8363SGerrit Renker 			    u8 mandatory, u8 opt, u8 feat, u8 *val, u8 len)
1106e77b8363SGerrit Renker {
1107e77b8363SGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
1108e77b8363SGerrit Renker 	struct list_head *fn = dreq ? &dreq->dreq_featneg : &dp->dccps_featneg;
1109e77b8363SGerrit Renker 	bool server = false;
1110e77b8363SGerrit Renker 
1111e77b8363SGerrit Renker 	switch (sk->sk_state) {
1112e77b8363SGerrit Renker 	/*
1113e77b8363SGerrit Renker 	 *	Negotiation during connection setup
1114e77b8363SGerrit Renker 	 */
1115e77b8363SGerrit Renker 	case DCCP_LISTEN:
1116e77b8363SGerrit Renker 		server = true;			/* fall through */
1117e77b8363SGerrit Renker 	case DCCP_REQUESTING:
1118e77b8363SGerrit Renker 		switch (opt) {
1119e77b8363SGerrit Renker 		case DCCPO_CHANGE_L:
1120e77b8363SGerrit Renker 		case DCCPO_CHANGE_R:
1121e77b8363SGerrit Renker 			return dccp_feat_change_recv(fn, mandatory, opt, feat,
1122e77b8363SGerrit Renker 						     val, len, server);
1123b1ad0042SGerrit Renker 		case DCCPO_CONFIRM_R:
1124b1ad0042SGerrit Renker 		case DCCPO_CONFIRM_L:
1125b1ad0042SGerrit Renker 			return dccp_feat_confirm_recv(fn, mandatory, opt, feat,
1126b1ad0042SGerrit Renker 						      val, len, server);
1127e77b8363SGerrit Renker 		}
1128e77b8363SGerrit Renker 	}
1129e77b8363SGerrit Renker 	return 0;	/* ignore FN options in all other states */
1130e77b8363SGerrit Renker }
1131e77b8363SGerrit Renker 
1132f90f92eeSGerrit Renker /**
1133f90f92eeSGerrit Renker  * dccp_feat_init  -  Seed feature negotiation with host-specific defaults
1134f90f92eeSGerrit Renker  * This initialises global defaults, depending on the value of the sysctls.
1135f90f92eeSGerrit Renker  * These can later be overridden by registering changes via setsockopt calls.
1136f90f92eeSGerrit Renker  * The last link in the chain is finalise_settings, to make sure that between
1137f90f92eeSGerrit Renker  * here and the start of actual feature negotiation no inconsistencies enter.
1138f90f92eeSGerrit Renker  *
1139f90f92eeSGerrit Renker  * All features not appearing below use either defaults or are otherwise
1140f90f92eeSGerrit Renker  * later adjusted through dccp_feat_finalise_settings().
1141f90f92eeSGerrit Renker  */
1142e8ef967aSGerrit Renker int dccp_feat_init(struct sock *sk)
1143afe00251SAndrea Bittau {
1144f90f92eeSGerrit Renker 	struct list_head *fn = &dccp_sk(sk)->dccps_featneg;
1145f90f92eeSGerrit Renker 	u8 on = 1, off = 0;
1146afe00251SAndrea Bittau 	int rc;
1147f90f92eeSGerrit Renker 	struct {
1148f90f92eeSGerrit Renker 		u8 *val;
1149f90f92eeSGerrit Renker 		u8 len;
1150f90f92eeSGerrit Renker 	} tx, rx;
1151afe00251SAndrea Bittau 
1152f90f92eeSGerrit Renker 	/* Non-negotiable (NN) features */
1153f90f92eeSGerrit Renker 	rc = __feat_register_nn(fn, DCCPF_SEQUENCE_WINDOW, 0,
1154*883ca833SGerrit Renker 				    sysctl_dccp_sequence_window);
1155f90f92eeSGerrit Renker 	if (rc)
1156f90f92eeSGerrit Renker 		return rc;
1157afe00251SAndrea Bittau 
1158f90f92eeSGerrit Renker 	/* Server-priority (SP) features */
1159f90f92eeSGerrit Renker 
1160f90f92eeSGerrit Renker 	/* Advertise that short seqnos are not supported (7.6.1) */
1161f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_SHORT_SEQNOS, true, true, &off, 1);
1162f90f92eeSGerrit Renker 	if (rc)
1163f90f92eeSGerrit Renker 		return rc;
1164f90f92eeSGerrit Renker 
1165f90f92eeSGerrit Renker 	/* RFC 4340 12.1: "If a DCCP is not ECN capable, ..." */
1166f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_ECN_INCAPABLE, true, true, &on, 1);
1167f90f92eeSGerrit Renker 	if (rc)
1168f90f92eeSGerrit Renker 		return rc;
1169f90f92eeSGerrit Renker 
1170f90f92eeSGerrit Renker 	/*
1171f90f92eeSGerrit Renker 	 * We advertise the available list of CCIDs and reorder according to
1172f90f92eeSGerrit Renker 	 * preferences, to avoid failure resulting from negotiating different
1173f90f92eeSGerrit Renker 	 * singleton values (which always leads to failure).
1174f90f92eeSGerrit Renker 	 * These settings can still (later) be overridden via sockopts.
1175f90f92eeSGerrit Renker 	 */
1176f90f92eeSGerrit Renker 	if (ccid_get_builtin_ccids(&tx.val, &tx.len) ||
1177f90f92eeSGerrit Renker 	    ccid_get_builtin_ccids(&rx.val, &rx.len))
1178f90f92eeSGerrit Renker 		return -ENOBUFS;
1179f90f92eeSGerrit Renker 
1180*883ca833SGerrit Renker 	if (!dccp_feat_prefer(sysctl_dccp_tx_ccid, tx.val, tx.len) ||
1181*883ca833SGerrit Renker 	    !dccp_feat_prefer(sysctl_dccp_rx_ccid, rx.val, rx.len))
1182f90f92eeSGerrit Renker 		goto free_ccid_lists;
1183f90f92eeSGerrit Renker 
1184f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_CCID, true, false, tx.val, tx.len);
1185f90f92eeSGerrit Renker 	if (rc)
1186f90f92eeSGerrit Renker 		goto free_ccid_lists;
1187f90f92eeSGerrit Renker 
1188f90f92eeSGerrit Renker 	rc = __feat_register_sp(fn, DCCPF_CCID, false, false, rx.val, rx.len);
1189f90f92eeSGerrit Renker 
1190f90f92eeSGerrit Renker free_ccid_lists:
1191f90f92eeSGerrit Renker 	kfree(tx.val);
1192f90f92eeSGerrit Renker 	kfree(rx.val);
1193afe00251SAndrea Bittau 	return rc;
1194afe00251SAndrea Bittau }
1195afe00251SAndrea Bittau 
1196422d9cdcSGerrit Renker int dccp_feat_activate_values(struct sock *sk, struct list_head *fn_list)
1197422d9cdcSGerrit Renker {
1198422d9cdcSGerrit Renker 	struct dccp_sock *dp = dccp_sk(sk);
1199422d9cdcSGerrit Renker 	struct dccp_feat_entry *cur, *next;
1200422d9cdcSGerrit Renker 	int idx;
1201422d9cdcSGerrit Renker 	dccp_feat_val *fvals[DCCP_FEAT_SUPPORTED_MAX][2] = {
1202422d9cdcSGerrit Renker 		 [0 ... DCCP_FEAT_SUPPORTED_MAX-1] = { NULL, NULL }
1203422d9cdcSGerrit Renker 	};
1204422d9cdcSGerrit Renker 
1205422d9cdcSGerrit Renker 	list_for_each_entry(cur, fn_list, node) {
1206422d9cdcSGerrit Renker 		/*
1207422d9cdcSGerrit Renker 		 * An empty Confirm means that either an unknown feature type
1208422d9cdcSGerrit Renker 		 * or an invalid value was present. In the first case there is
1209422d9cdcSGerrit Renker 		 * nothing to activate, in the other the default value is used.
1210422d9cdcSGerrit Renker 		 */
1211422d9cdcSGerrit Renker 		if (cur->empty_confirm)
1212422d9cdcSGerrit Renker 			continue;
1213422d9cdcSGerrit Renker 
1214422d9cdcSGerrit Renker 		idx = dccp_feat_index(cur->feat_num);
1215422d9cdcSGerrit Renker 		if (idx < 0) {
1216422d9cdcSGerrit Renker 			DCCP_BUG("Unknown feature %u", cur->feat_num);
1217422d9cdcSGerrit Renker 			goto activation_failed;
1218422d9cdcSGerrit Renker 		}
1219422d9cdcSGerrit Renker 		if (cur->state != FEAT_STABLE) {
1220422d9cdcSGerrit Renker 			DCCP_CRIT("Negotiation of %s %u failed in state %u",
1221422d9cdcSGerrit Renker 				  cur->is_local ? "local" : "remote",
1222422d9cdcSGerrit Renker 				  cur->feat_num, cur->state);
1223422d9cdcSGerrit Renker 			goto activation_failed;
1224422d9cdcSGerrit Renker 		}
1225422d9cdcSGerrit Renker 		fvals[idx][cur->is_local] = &cur->val;
1226422d9cdcSGerrit Renker 	}
1227422d9cdcSGerrit Renker 
1228422d9cdcSGerrit Renker 	/*
1229422d9cdcSGerrit Renker 	 * Activate in decreasing order of index, so that the CCIDs are always
1230422d9cdcSGerrit Renker 	 * activated as the last feature. This avoids the case where a CCID
1231422d9cdcSGerrit Renker 	 * relies on the initialisation of one or more features that it depends
1232422d9cdcSGerrit Renker 	 * on (e.g. Send NDP Count, Send Ack Vector, and Ack Ratio features).
1233422d9cdcSGerrit Renker 	 */
1234422d9cdcSGerrit Renker 	for (idx = DCCP_FEAT_SUPPORTED_MAX; --idx >= 0;)
1235422d9cdcSGerrit Renker 		if (__dccp_feat_activate(sk, idx, 0, fvals[idx][0]) ||
1236422d9cdcSGerrit Renker 		    __dccp_feat_activate(sk, idx, 1, fvals[idx][1])) {
1237422d9cdcSGerrit Renker 			DCCP_CRIT("Could not activate %d", idx);
1238422d9cdcSGerrit Renker 			goto activation_failed;
1239422d9cdcSGerrit Renker 		}
1240422d9cdcSGerrit Renker 
1241422d9cdcSGerrit Renker 	/* Clean up Change options which have been confirmed already */
1242422d9cdcSGerrit Renker 	list_for_each_entry_safe(cur, next, fn_list, node)
1243422d9cdcSGerrit Renker 		if (!cur->needs_confirm)
1244422d9cdcSGerrit Renker 			dccp_feat_list_pop(cur);
1245422d9cdcSGerrit Renker 
1246422d9cdcSGerrit Renker 	dccp_pr_debug("Activation OK\n");
1247422d9cdcSGerrit Renker 	return 0;
1248422d9cdcSGerrit Renker 
1249422d9cdcSGerrit Renker activation_failed:
1250422d9cdcSGerrit Renker 	/*
1251422d9cdcSGerrit Renker 	 * We clean up everything that may have been allocated, since
1252422d9cdcSGerrit Renker 	 * it is difficult to track at which stage negotiation failed.
1253422d9cdcSGerrit Renker 	 * This is ok, since all allocation functions below are robust
1254422d9cdcSGerrit Renker 	 * against NULL arguments.
1255422d9cdcSGerrit Renker 	 */
1256422d9cdcSGerrit Renker 	ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
1257422d9cdcSGerrit Renker 	ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
1258422d9cdcSGerrit Renker 	dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1259422d9cdcSGerrit Renker 	dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1260422d9cdcSGerrit Renker 	dp->dccps_hc_rx_ackvec = NULL;
1261422d9cdcSGerrit Renker 	return -1;
1262422d9cdcSGerrit Renker }
1263422d9cdcSGerrit Renker 
1264c02fdc0eSGerrit Renker #ifdef CONFIG_IP_DCCP_DEBUG
1265c02fdc0eSGerrit Renker const char *dccp_feat_typename(const u8 type)
1266c02fdc0eSGerrit Renker {
1267c02fdc0eSGerrit Renker 	switch(type) {
1268c02fdc0eSGerrit Renker 	case DCCPO_CHANGE_L:  return("ChangeL");
1269c02fdc0eSGerrit Renker 	case DCCPO_CONFIRM_L: return("ConfirmL");
1270c02fdc0eSGerrit Renker 	case DCCPO_CHANGE_R:  return("ChangeR");
1271c02fdc0eSGerrit Renker 	case DCCPO_CONFIRM_R: return("ConfirmR");
1272c02fdc0eSGerrit Renker 	/* the following case must not appear in feature negotation  */
1273c02fdc0eSGerrit Renker 	default:	      dccp_pr_debug("unknown type %d [BUG!]\n", type);
1274c02fdc0eSGerrit Renker 	}
1275c02fdc0eSGerrit Renker 	return NULL;
1276c02fdc0eSGerrit Renker }
1277c02fdc0eSGerrit Renker 
1278c02fdc0eSGerrit Renker const char *dccp_feat_name(const u8 feat)
1279c02fdc0eSGerrit Renker {
1280c02fdc0eSGerrit Renker 	static const char *feature_names[] = {
1281c02fdc0eSGerrit Renker 		[DCCPF_RESERVED]	= "Reserved",
1282c02fdc0eSGerrit Renker 		[DCCPF_CCID]		= "CCID",
1283c02fdc0eSGerrit Renker 		[DCCPF_SHORT_SEQNOS]	= "Allow Short Seqnos",
1284c02fdc0eSGerrit Renker 		[DCCPF_SEQUENCE_WINDOW]	= "Sequence Window",
1285c02fdc0eSGerrit Renker 		[DCCPF_ECN_INCAPABLE]	= "ECN Incapable",
1286c02fdc0eSGerrit Renker 		[DCCPF_ACK_RATIO]	= "Ack Ratio",
1287c02fdc0eSGerrit Renker 		[DCCPF_SEND_ACK_VECTOR]	= "Send ACK Vector",
1288c02fdc0eSGerrit Renker 		[DCCPF_SEND_NDP_COUNT]	= "Send NDP Count",
1289c02fdc0eSGerrit Renker 		[DCCPF_MIN_CSUM_COVER]	= "Min. Csum Coverage",
1290c02fdc0eSGerrit Renker 		[DCCPF_DATA_CHECKSUM]	= "Send Data Checksum",
1291c02fdc0eSGerrit Renker 	};
1292dd6303dfSGerrit Renker 	if (feat > DCCPF_DATA_CHECKSUM && feat < DCCPF_MIN_CCID_SPECIFIC)
1293dd6303dfSGerrit Renker 		return feature_names[DCCPF_RESERVED];
1294dd6303dfSGerrit Renker 
12957d43d1a0SGerrit Renker 	if (feat ==  DCCPF_SEND_LEV_RATE)
12967d43d1a0SGerrit Renker 		return "Send Loss Event Rate";
1297c02fdc0eSGerrit Renker 	if (feat >= DCCPF_MIN_CCID_SPECIFIC)
1298c02fdc0eSGerrit Renker 		return "CCID-specific";
1299c02fdc0eSGerrit Renker 
1300c02fdc0eSGerrit Renker 	return feature_names[feat];
1301c02fdc0eSGerrit Renker }
1302c02fdc0eSGerrit Renker #endif /* CONFIG_IP_DCCP_DEBUG */
1303