xref: /openbmc/linux/net/dccp/feat.c (revision 6ffd30fbbb1b4a01a9f56049521693375a864d3e)
1afe00251SAndrea Bittau /*
2afe00251SAndrea Bittau  *  net/dccp/feat.c
3afe00251SAndrea Bittau  *
4afe00251SAndrea Bittau  *  An implementation of the DCCP protocol
5afe00251SAndrea Bittau  *  Andrea Bittau <a.bittau@cs.ucl.ac.uk>
6afe00251SAndrea Bittau  *
7afe00251SAndrea Bittau  *      This program is free software; you can redistribute it and/or
8afe00251SAndrea Bittau  *      modify it under the terms of the GNU General Public License
9afe00251SAndrea Bittau  *      as published by the Free Software Foundation; either version
10afe00251SAndrea Bittau  *      2 of the License, or (at your option) any later version.
11afe00251SAndrea Bittau  */
12afe00251SAndrea Bittau 
13afe00251SAndrea Bittau #include <linux/config.h>
14afe00251SAndrea Bittau #include <linux/module.h>
15afe00251SAndrea Bittau 
16afe00251SAndrea Bittau #include "dccp.h"
17*6ffd30fbSAndrea Bittau #include "ccid.h"
18afe00251SAndrea Bittau #include "feat.h"
19afe00251SAndrea Bittau 
20afe00251SAndrea Bittau #define DCCP_FEAT_SP_NOAGREE (-123)
21afe00251SAndrea Bittau 
22afe00251SAndrea Bittau int dccp_feat_change(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len,
23afe00251SAndrea Bittau 		     gfp_t gfp)
24afe00251SAndrea Bittau {
25afe00251SAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
26afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
27afe00251SAndrea Bittau 
28afe00251SAndrea Bittau 	dccp_pr_debug("feat change type=%d feat=%d\n", type, feature);
29afe00251SAndrea Bittau 
30*6ffd30fbSAndrea Bittau 	/* XXX sanity check feat change request */
31*6ffd30fbSAndrea Bittau 
32afe00251SAndrea Bittau 	/* check if that feature is already being negotiated */
33afe00251SAndrea Bittau 	list_for_each_entry(opt, &dp->dccps_options.dccpo_pending,
34afe00251SAndrea Bittau 			    dccpop_node) {
35afe00251SAndrea Bittau 		/* ok we found a negotiation for this option already */
36afe00251SAndrea Bittau 		if (opt->dccpop_feat == feature && opt->dccpop_type == type) {
37afe00251SAndrea Bittau 			dccp_pr_debug("Replacing old\n");
38afe00251SAndrea Bittau 			/* replace */
39afe00251SAndrea Bittau 			BUG_ON(opt->dccpop_val == NULL);
40afe00251SAndrea Bittau 			kfree(opt->dccpop_val);
41afe00251SAndrea Bittau 			opt->dccpop_val	 = val;
42afe00251SAndrea Bittau 			opt->dccpop_len	 = len;
43afe00251SAndrea Bittau 			opt->dccpop_conf = 0;
44afe00251SAndrea Bittau 			return 0;
45afe00251SAndrea Bittau 		}
46afe00251SAndrea Bittau 	}
47afe00251SAndrea Bittau 
48afe00251SAndrea Bittau 	/* negotiation for a new feature */
49afe00251SAndrea Bittau 	opt = kmalloc(sizeof(*opt), gfp);
50afe00251SAndrea Bittau 	if (opt == NULL)
51afe00251SAndrea Bittau 		return -ENOMEM;
52afe00251SAndrea Bittau 
53afe00251SAndrea Bittau 	opt->dccpop_type = type;
54afe00251SAndrea Bittau 	opt->dccpop_feat = feature;
55afe00251SAndrea Bittau 	opt->dccpop_len	 = len;
56afe00251SAndrea Bittau 	opt->dccpop_val	 = val;
57afe00251SAndrea Bittau 	opt->dccpop_conf = 0;
58afe00251SAndrea Bittau 	opt->dccpop_sc	 = NULL;
59afe00251SAndrea Bittau 
60afe00251SAndrea Bittau 	BUG_ON(opt->dccpop_val == NULL);
61afe00251SAndrea Bittau 
62afe00251SAndrea Bittau 	list_add_tail(&opt->dccpop_node, &dp->dccps_options.dccpo_pending);
63afe00251SAndrea Bittau 	return 0;
64afe00251SAndrea Bittau }
65afe00251SAndrea Bittau 
66afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_change);
67afe00251SAndrea Bittau 
68*6ffd30fbSAndrea Bittau static int dccp_feat_update_ccid(struct sock *sk, u8 type, u8 new_ccid_nr)
69*6ffd30fbSAndrea Bittau {
70*6ffd30fbSAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
71*6ffd30fbSAndrea Bittau 	/* figure out if we are changing our CCID or the peer's */
72*6ffd30fbSAndrea Bittau 	const int rx = type == DCCPO_CHANGE_R;
73*6ffd30fbSAndrea Bittau 	const u8 ccid_nr = rx ? dp->dccps_options.dccpo_rx_ccid :
74*6ffd30fbSAndrea Bittau 				dp->dccps_options.dccpo_tx_ccid;
75*6ffd30fbSAndrea Bittau 	struct ccid *new_ccid;
76*6ffd30fbSAndrea Bittau 
77*6ffd30fbSAndrea Bittau 	/* Check if nothing is being changed. */
78*6ffd30fbSAndrea Bittau 	if (ccid_nr == new_ccid_nr)
79*6ffd30fbSAndrea Bittau 		return 0;
80*6ffd30fbSAndrea Bittau 
81*6ffd30fbSAndrea Bittau 	new_ccid = ccid_new(new_ccid_nr, sk, rx, GFP_ATOMIC);
82*6ffd30fbSAndrea Bittau 	if (new_ccid == NULL)
83*6ffd30fbSAndrea Bittau 		return -ENOMEM;
84*6ffd30fbSAndrea Bittau 
85*6ffd30fbSAndrea Bittau 	if (rx) {
86*6ffd30fbSAndrea Bittau 		ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
87*6ffd30fbSAndrea Bittau 		dp->dccps_hc_rx_ccid = new_ccid;
88*6ffd30fbSAndrea Bittau 		dp->dccps_options.dccpo_rx_ccid = new_ccid_nr;
89*6ffd30fbSAndrea Bittau 	} else {
90*6ffd30fbSAndrea Bittau 		ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
91*6ffd30fbSAndrea Bittau 		dp->dccps_hc_tx_ccid = new_ccid;
92*6ffd30fbSAndrea Bittau 		dp->dccps_options.dccpo_tx_ccid = new_ccid_nr;
93*6ffd30fbSAndrea Bittau 	}
94*6ffd30fbSAndrea Bittau 
95*6ffd30fbSAndrea Bittau 	return 0;
96*6ffd30fbSAndrea Bittau }
97*6ffd30fbSAndrea Bittau 
98afe00251SAndrea Bittau /* XXX taking only u8 vals */
99afe00251SAndrea Bittau static int dccp_feat_update(struct sock *sk, u8 type, u8 feat, u8 val)
100afe00251SAndrea Bittau {
101afe00251SAndrea Bittau 	dccp_pr_debug("changing [%d] feat %d to %d\n", type, feat, val);
102*6ffd30fbSAndrea Bittau 
103*6ffd30fbSAndrea Bittau 	switch (feat) {
104*6ffd30fbSAndrea Bittau 	case DCCPF_CCID:
105*6ffd30fbSAndrea Bittau 		return dccp_feat_update_ccid(sk, type, val);
106*6ffd30fbSAndrea Bittau 	default:
107*6ffd30fbSAndrea Bittau 		dccp_pr_debug("IMPLEMENT changing [%d] feat %d to %d\n",
108*6ffd30fbSAndrea Bittau 			      type, feat, val);
109*6ffd30fbSAndrea Bittau 		break;
110*6ffd30fbSAndrea Bittau 	}
111afe00251SAndrea Bittau 	return 0;
112afe00251SAndrea Bittau }
113afe00251SAndrea Bittau 
114afe00251SAndrea Bittau static int dccp_feat_reconcile(struct sock *sk, struct dccp_opt_pend *opt,
115afe00251SAndrea Bittau 			       u8 *rpref, u8 rlen)
116afe00251SAndrea Bittau {
117afe00251SAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
118afe00251SAndrea Bittau 	u8 *spref, slen, *res = NULL;
119afe00251SAndrea Bittau 	int i, j, rc, agree = 1;
120afe00251SAndrea Bittau 
121afe00251SAndrea Bittau 	BUG_ON(rpref == NULL);
122afe00251SAndrea Bittau 
123afe00251SAndrea Bittau 	/* check if we are the black sheep */
124afe00251SAndrea Bittau 	if (dp->dccps_role == DCCP_ROLE_CLIENT) {
125afe00251SAndrea Bittau 		spref = rpref;
126afe00251SAndrea Bittau 		slen  = rlen;
127afe00251SAndrea Bittau 		rpref = opt->dccpop_val;
128afe00251SAndrea Bittau 		rlen  = opt->dccpop_len;
129afe00251SAndrea Bittau 	} else {
130afe00251SAndrea Bittau 		spref = opt->dccpop_val;
131afe00251SAndrea Bittau 		slen  = opt->dccpop_len;
132afe00251SAndrea Bittau 	}
133afe00251SAndrea Bittau 	/*
134afe00251SAndrea Bittau 	 * Now we have server preference list in spref and client preference in
135afe00251SAndrea Bittau 	 * rpref
136afe00251SAndrea Bittau 	 */
137afe00251SAndrea Bittau 	BUG_ON(spref == NULL);
138afe00251SAndrea Bittau 	BUG_ON(rpref == NULL);
139afe00251SAndrea Bittau 
140afe00251SAndrea Bittau 	/* FIXME sanity check vals */
141afe00251SAndrea Bittau 
142afe00251SAndrea Bittau 	/* Are values in any order?  XXX Lame "algorithm" here */
143afe00251SAndrea Bittau 	/* XXX assume values are 1 byte */
144afe00251SAndrea Bittau 	for (i = 0; i < slen; i++) {
145afe00251SAndrea Bittau 		for (j = 0; j < rlen; j++) {
146afe00251SAndrea Bittau 			if (spref[i] == rpref[j]) {
147afe00251SAndrea Bittau 				res = &spref[i];
148afe00251SAndrea Bittau 				break;
149afe00251SAndrea Bittau 			}
150afe00251SAndrea Bittau 		}
151afe00251SAndrea Bittau 		if (res)
152afe00251SAndrea Bittau 			break;
153afe00251SAndrea Bittau 	}
154afe00251SAndrea Bittau 
155afe00251SAndrea Bittau 	/* we didn't agree on anything */
156afe00251SAndrea Bittau 	if (res == NULL) {
157afe00251SAndrea Bittau 		/* confirm previous value */
158afe00251SAndrea Bittau 		switch (opt->dccpop_feat) {
159afe00251SAndrea Bittau 		case DCCPF_CCID:
160afe00251SAndrea Bittau 			/* XXX did i get this right? =P */
161afe00251SAndrea Bittau 			if (opt->dccpop_type == DCCPO_CHANGE_L)
162afe00251SAndrea Bittau 				res = &dp->dccps_options.dccpo_tx_ccid;
163afe00251SAndrea Bittau 			else
164afe00251SAndrea Bittau 				res = &dp->dccps_options.dccpo_rx_ccid;
165afe00251SAndrea Bittau 			break;
166afe00251SAndrea Bittau 
167afe00251SAndrea Bittau 		default:
168afe00251SAndrea Bittau 			WARN_ON(1); /* XXX implement res */
169afe00251SAndrea Bittau 			return -EFAULT;
170afe00251SAndrea Bittau 		}
171afe00251SAndrea Bittau 
172afe00251SAndrea Bittau 		dccp_pr_debug("Don't agree... reconfirming %d\n", *res);
173afe00251SAndrea Bittau 		agree = 0; /* this is used for mandatory options... */
174afe00251SAndrea Bittau 	}
175afe00251SAndrea Bittau 
176afe00251SAndrea Bittau 	/* need to put result and our preference list */
177afe00251SAndrea Bittau 	/* XXX assume 1 byte vals */
178afe00251SAndrea Bittau 	rlen = 1 + opt->dccpop_len;
179afe00251SAndrea Bittau 	rpref = kmalloc(rlen, GFP_ATOMIC);
180afe00251SAndrea Bittau 	if (rpref == NULL)
181afe00251SAndrea Bittau 		return -ENOMEM;
182afe00251SAndrea Bittau 
183afe00251SAndrea Bittau 	*rpref = *res;
184afe00251SAndrea Bittau 	memcpy(&rpref[1], opt->dccpop_val, opt->dccpop_len);
185afe00251SAndrea Bittau 
186afe00251SAndrea Bittau 	/* put it in the "confirm queue" */
187afe00251SAndrea Bittau 	if (opt->dccpop_sc == NULL) {
188afe00251SAndrea Bittau 		opt->dccpop_sc = kmalloc(sizeof(*opt->dccpop_sc), GFP_ATOMIC);
189afe00251SAndrea Bittau 		if (opt->dccpop_sc == NULL) {
190afe00251SAndrea Bittau 			kfree(rpref);
191afe00251SAndrea Bittau 			return -ENOMEM;
192afe00251SAndrea Bittau 		}
193afe00251SAndrea Bittau 	} else {
194afe00251SAndrea Bittau 		/* recycle the confirm slot */
195afe00251SAndrea Bittau 		BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
196afe00251SAndrea Bittau 		kfree(opt->dccpop_sc->dccpoc_val);
197afe00251SAndrea Bittau 		dccp_pr_debug("recycling confirm slot\n");
198afe00251SAndrea Bittau 	}
199afe00251SAndrea Bittau 	memset(opt->dccpop_sc, 0, sizeof(*opt->dccpop_sc));
200afe00251SAndrea Bittau 
201afe00251SAndrea Bittau 	opt->dccpop_sc->dccpoc_val = rpref;
202afe00251SAndrea Bittau 	opt->dccpop_sc->dccpoc_len = rlen;
203afe00251SAndrea Bittau 
204afe00251SAndrea Bittau 	/* update the option on our side [we are about to send the confirm] */
205afe00251SAndrea Bittau 	rc = dccp_feat_update(sk, opt->dccpop_type, opt->dccpop_feat, *res);
206afe00251SAndrea Bittau 	if (rc) {
207afe00251SAndrea Bittau 		kfree(opt->dccpop_sc->dccpoc_val);
208afe00251SAndrea Bittau 		kfree(opt->dccpop_sc);
209afe00251SAndrea Bittau 		opt->dccpop_sc = 0;
210afe00251SAndrea Bittau 		return rc;
211afe00251SAndrea Bittau 	}
212afe00251SAndrea Bittau 
213afe00251SAndrea Bittau 	dccp_pr_debug("Will confirm %d\n", *rpref);
214afe00251SAndrea Bittau 
215afe00251SAndrea Bittau 	/* say we want to change to X but we just got a confirm X, suppress our
216afe00251SAndrea Bittau 	 * change
217afe00251SAndrea Bittau 	 */
218afe00251SAndrea Bittau 	if (!opt->dccpop_conf) {
219afe00251SAndrea Bittau 		if (*opt->dccpop_val == *res)
220afe00251SAndrea Bittau 			opt->dccpop_conf = 1;
221afe00251SAndrea Bittau 		dccp_pr_debug("won't ask for change of same feature\n");
222afe00251SAndrea Bittau 	}
223afe00251SAndrea Bittau 
224afe00251SAndrea Bittau 	return agree ? 0 : DCCP_FEAT_SP_NOAGREE; /* used for mandatory opts */
225afe00251SAndrea Bittau }
226afe00251SAndrea Bittau 
227afe00251SAndrea Bittau static int dccp_feat_sp(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
228afe00251SAndrea Bittau {
229afe00251SAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
230afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
231afe00251SAndrea Bittau 	int rc = 1;
232afe00251SAndrea Bittau 	u8 t;
233afe00251SAndrea Bittau 
234afe00251SAndrea Bittau 	/*
235afe00251SAndrea Bittau 	 * We received a CHANGE.  We gotta match it against our own preference
236afe00251SAndrea Bittau 	 * list.  If we got a CHANGE_R it means it's a change for us, so we need
237afe00251SAndrea Bittau 	 * to compare our CHANGE_L list.
238afe00251SAndrea Bittau 	 */
239afe00251SAndrea Bittau 	if (type == DCCPO_CHANGE_L)
240afe00251SAndrea Bittau 		t = DCCPO_CHANGE_R;
241afe00251SAndrea Bittau 	else
242afe00251SAndrea Bittau 		t = DCCPO_CHANGE_L;
243afe00251SAndrea Bittau 
244afe00251SAndrea Bittau 	/* find our preference list for this feature */
245afe00251SAndrea Bittau 	list_for_each_entry(opt, &dp->dccps_options.dccpo_pending,
246afe00251SAndrea Bittau 			    dccpop_node) {
247afe00251SAndrea Bittau 		if (opt->dccpop_type != t || opt->dccpop_feat != feature)
248afe00251SAndrea Bittau 			continue;
249afe00251SAndrea Bittau 
250afe00251SAndrea Bittau 		/* find the winner from the two preference lists */
251afe00251SAndrea Bittau 		rc = dccp_feat_reconcile(sk, opt, val, len);
252afe00251SAndrea Bittau 		break;
253afe00251SAndrea Bittau 	}
254afe00251SAndrea Bittau 
255afe00251SAndrea Bittau 	/* We didn't deal with the change.  This can happen if we have no
256afe00251SAndrea Bittau 	 * preference list for the feature.  In fact, it just shouldn't
257afe00251SAndrea Bittau 	 * happen---if we understand a feature, we should have a preference list
258afe00251SAndrea Bittau 	 * with at least the default value.
259afe00251SAndrea Bittau 	 */
260afe00251SAndrea Bittau 	BUG_ON(rc == 1);
261afe00251SAndrea Bittau 
262afe00251SAndrea Bittau 	return rc;
263afe00251SAndrea Bittau }
264afe00251SAndrea Bittau 
265afe00251SAndrea Bittau static int dccp_feat_nn(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
266afe00251SAndrea Bittau {
267afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
268afe00251SAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
269afe00251SAndrea Bittau 	u8 *copy;
270afe00251SAndrea Bittau 	int rc;
271afe00251SAndrea Bittau 
272afe00251SAndrea Bittau 	/* NN features must be change L */
273afe00251SAndrea Bittau 	if (type == DCCPO_CHANGE_R) {
274afe00251SAndrea Bittau 		dccp_pr_debug("received CHANGE_R %d for NN feat %d\n",
275afe00251SAndrea Bittau 			      type, feature);
276afe00251SAndrea Bittau 		return -EFAULT;
277afe00251SAndrea Bittau 	}
278afe00251SAndrea Bittau 
279afe00251SAndrea Bittau 	/* XXX sanity check opt val */
280afe00251SAndrea Bittau 
281afe00251SAndrea Bittau 	/* copy option so we can confirm it */
282afe00251SAndrea Bittau 	opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
283afe00251SAndrea Bittau 	if (opt == NULL)
284afe00251SAndrea Bittau 		return -ENOMEM;
285afe00251SAndrea Bittau 
286afe00251SAndrea Bittau 	copy = kmalloc(len, GFP_ATOMIC);
287afe00251SAndrea Bittau 	if (copy == NULL) {
288afe00251SAndrea Bittau 		kfree(opt);
289afe00251SAndrea Bittau 		return -ENOMEM;
290afe00251SAndrea Bittau 	}
291afe00251SAndrea Bittau 	memcpy(copy, val, len);
292afe00251SAndrea Bittau 
293afe00251SAndrea Bittau 	opt->dccpop_type = DCCPO_CONFIRM_R; /* NN can only confirm R */
294afe00251SAndrea Bittau 	opt->dccpop_feat = feature;
295afe00251SAndrea Bittau 	opt->dccpop_val	 = copy;
296afe00251SAndrea Bittau 	opt->dccpop_len	 = len;
297afe00251SAndrea Bittau 
298afe00251SAndrea Bittau 	/* change feature */
299afe00251SAndrea Bittau 	rc = dccp_feat_update(sk, type, feature, *val);
300afe00251SAndrea Bittau 	if (rc) {
301afe00251SAndrea Bittau 		kfree(opt->dccpop_val);
302afe00251SAndrea Bittau 		kfree(opt);
303afe00251SAndrea Bittau 		return rc;
304afe00251SAndrea Bittau 	}
305afe00251SAndrea Bittau 
306afe00251SAndrea Bittau 	dccp_pr_debug("Confirming NN feature %d (val=%d)\n", feature, *copy);
307afe00251SAndrea Bittau 	list_add_tail(&opt->dccpop_node, &dp->dccps_options.dccpo_conf);
308afe00251SAndrea Bittau 
309afe00251SAndrea Bittau 	return 0;
310afe00251SAndrea Bittau }
311afe00251SAndrea Bittau 
312afe00251SAndrea Bittau static void dccp_feat_empty_confirm(struct sock *sk, u8 type, u8 feature)
313afe00251SAndrea Bittau {
314afe00251SAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
315afe00251SAndrea Bittau 	/* XXX check if other confirms for that are queued and recycle slot */
316afe00251SAndrea Bittau 	struct dccp_opt_pend *opt = kzalloc(sizeof(*opt), GFP_ATOMIC);
317afe00251SAndrea Bittau 
318afe00251SAndrea Bittau 	if (opt == NULL) {
319afe00251SAndrea Bittau 		/* XXX what do we do?  Ignoring should be fine.  It's a change
320afe00251SAndrea Bittau 		 * after all =P
321afe00251SAndrea Bittau 		 */
322afe00251SAndrea Bittau 		return;
323afe00251SAndrea Bittau 	}
324afe00251SAndrea Bittau 
325afe00251SAndrea Bittau 	opt->dccpop_type = type == DCCPO_CHANGE_L ? DCCPO_CONFIRM_R :
326afe00251SAndrea Bittau 						    DCCPO_CONFIRM_L;
327afe00251SAndrea Bittau 	opt->dccpop_feat = feature;
328afe00251SAndrea Bittau 	opt->dccpop_val	 = 0;
329afe00251SAndrea Bittau 	opt->dccpop_len	 = 0;
330afe00251SAndrea Bittau 
331afe00251SAndrea Bittau 	/* change feature */
332afe00251SAndrea Bittau 	dccp_pr_debug("Empty confirm feature %d type %d\n", feature, type);
333afe00251SAndrea Bittau 	list_add_tail(&opt->dccpop_node, &dp->dccps_options.dccpo_conf);
334afe00251SAndrea Bittau }
335afe00251SAndrea Bittau 
336afe00251SAndrea Bittau static void dccp_feat_flush_confirm(struct sock *sk)
337afe00251SAndrea Bittau {
338afe00251SAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
339afe00251SAndrea Bittau 	/* Check if there is anything to confirm in the first place */
340afe00251SAndrea Bittau 	int yes = !list_empty(&dp->dccps_options.dccpo_conf);
341afe00251SAndrea Bittau 
342afe00251SAndrea Bittau 	if (!yes) {
343afe00251SAndrea Bittau 		struct dccp_opt_pend *opt;
344afe00251SAndrea Bittau 
345afe00251SAndrea Bittau 		list_for_each_entry(opt, &dp->dccps_options.dccpo_pending,
346afe00251SAndrea Bittau 				    dccpop_node) {
347afe00251SAndrea Bittau 			if (opt->dccpop_conf) {
348afe00251SAndrea Bittau 				yes = 1;
349afe00251SAndrea Bittau 				break;
350afe00251SAndrea Bittau 			}
351afe00251SAndrea Bittau 		}
352afe00251SAndrea Bittau 	}
353afe00251SAndrea Bittau 
354afe00251SAndrea Bittau 	if (!yes)
355afe00251SAndrea Bittau 		return;
356afe00251SAndrea Bittau 
357afe00251SAndrea Bittau 	/* OK there is something to confirm... */
358afe00251SAndrea Bittau 	/* XXX check if packet is in flight?  Send delayed ack?? */
359afe00251SAndrea Bittau 	if (sk->sk_state == DCCP_OPEN)
360afe00251SAndrea Bittau 		dccp_send_ack(sk);
361afe00251SAndrea Bittau }
362afe00251SAndrea Bittau 
363afe00251SAndrea Bittau int dccp_feat_change_recv(struct sock *sk, u8 type, u8 feature, u8 *val, u8 len)
364afe00251SAndrea Bittau {
365afe00251SAndrea Bittau 	int rc;
366afe00251SAndrea Bittau 
367afe00251SAndrea Bittau 	dccp_pr_debug("got feat change type=%d feat=%d\n", type, feature);
368afe00251SAndrea Bittau 
369afe00251SAndrea Bittau 	/* figure out if it's SP or NN feature */
370afe00251SAndrea Bittau 	switch (feature) {
371afe00251SAndrea Bittau 	/* deal with SP features */
372afe00251SAndrea Bittau 	case DCCPF_CCID:
373afe00251SAndrea Bittau 		rc = dccp_feat_sp(sk, type, feature, val, len);
374afe00251SAndrea Bittau 		break;
375afe00251SAndrea Bittau 
376afe00251SAndrea Bittau 	/* deal with NN features */
377afe00251SAndrea Bittau 	case DCCPF_ACK_RATIO:
378afe00251SAndrea Bittau 		rc = dccp_feat_nn(sk, type, feature, val, len);
379afe00251SAndrea Bittau 		break;
380afe00251SAndrea Bittau 
381afe00251SAndrea Bittau 	/* XXX implement other features */
382afe00251SAndrea Bittau 	default:
383afe00251SAndrea Bittau 		rc = -EFAULT;
384afe00251SAndrea Bittau 		break;
385afe00251SAndrea Bittau 	}
386afe00251SAndrea Bittau 
387afe00251SAndrea Bittau 	/* check if there were problems changing features */
388afe00251SAndrea Bittau 	if (rc) {
389afe00251SAndrea Bittau 		/* If we don't agree on SP, we sent a confirm for old value.
390afe00251SAndrea Bittau 		 * However we propagate rc to caller in case option was
391afe00251SAndrea Bittau 		 * mandatory
392afe00251SAndrea Bittau 		 */
393afe00251SAndrea Bittau 		if (rc != DCCP_FEAT_SP_NOAGREE)
394afe00251SAndrea Bittau 			dccp_feat_empty_confirm(sk, type, feature);
395afe00251SAndrea Bittau 	}
396afe00251SAndrea Bittau 
397afe00251SAndrea Bittau 	/* generate the confirm [if required] */
398afe00251SAndrea Bittau 	dccp_feat_flush_confirm(sk);
399afe00251SAndrea Bittau 
400afe00251SAndrea Bittau 	return rc;
401afe00251SAndrea Bittau }
402afe00251SAndrea Bittau 
403afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_change_recv);
404afe00251SAndrea Bittau 
405afe00251SAndrea Bittau int dccp_feat_confirm_recv(struct sock *sk, u8 type, u8 feature,
406afe00251SAndrea Bittau 			   u8 *val, u8 len)
407afe00251SAndrea Bittau {
408afe00251SAndrea Bittau 	u8 t;
409afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
410afe00251SAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
411afe00251SAndrea Bittau 	int rc = 1;
412afe00251SAndrea Bittau 	int all_confirmed = 1;
413afe00251SAndrea Bittau 
414afe00251SAndrea Bittau 	dccp_pr_debug("got feat confirm type=%d feat=%d\n", type, feature);
415afe00251SAndrea Bittau 
416afe00251SAndrea Bittau 	/* XXX sanity check type & feat */
417afe00251SAndrea Bittau 
418afe00251SAndrea Bittau 	/* locate our change request */
419afe00251SAndrea Bittau 	t = type == DCCPO_CONFIRM_L ? DCCPO_CHANGE_R : DCCPO_CHANGE_L;
420afe00251SAndrea Bittau 
421afe00251SAndrea Bittau 	list_for_each_entry(opt, &dp->dccps_options.dccpo_pending,
422afe00251SAndrea Bittau 			    dccpop_node) {
423afe00251SAndrea Bittau 		if (!opt->dccpop_conf && opt->dccpop_type == t &&
424afe00251SAndrea Bittau 		    opt->dccpop_feat == feature) {
425afe00251SAndrea Bittau 			/* we found it */
426afe00251SAndrea Bittau 			/* XXX do sanity check */
427afe00251SAndrea Bittau 
428afe00251SAndrea Bittau 			opt->dccpop_conf = 1;
429afe00251SAndrea Bittau 
430afe00251SAndrea Bittau 			/* We got a confirmation---change the option */
431afe00251SAndrea Bittau 			dccp_feat_update(sk, opt->dccpop_type,
432afe00251SAndrea Bittau 					 opt->dccpop_feat, *val);
433afe00251SAndrea Bittau 
434afe00251SAndrea Bittau 			dccp_pr_debug("feat %d type %d confirmed %d\n",
435afe00251SAndrea Bittau 				      feature, type, *val);
436afe00251SAndrea Bittau 			rc = 0;
437afe00251SAndrea Bittau 			break;
438afe00251SAndrea Bittau 		}
439afe00251SAndrea Bittau 
440afe00251SAndrea Bittau 		if (!opt->dccpop_conf)
441afe00251SAndrea Bittau 			all_confirmed = 0;
442afe00251SAndrea Bittau 	}
443afe00251SAndrea Bittau 
444afe00251SAndrea Bittau 	/* fix re-transmit timer */
445afe00251SAndrea Bittau 	/* XXX gotta make sure that no option negotiation occurs during
446afe00251SAndrea Bittau 	 * connection shutdown.  Consider that the CLOSEREQ is sent and timer is
447afe00251SAndrea Bittau 	 * on.  if all options are confirmed it might kill timer which should
448afe00251SAndrea Bittau 	 * remain alive until close is received.
449afe00251SAndrea Bittau 	 */
450afe00251SAndrea Bittau 	if (all_confirmed) {
451afe00251SAndrea Bittau 		dccp_pr_debug("clear feat negotiation timer %p\n", sk);
452afe00251SAndrea Bittau 		inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
453afe00251SAndrea Bittau 	}
454afe00251SAndrea Bittau 
455afe00251SAndrea Bittau 	if (rc)
456afe00251SAndrea Bittau 		dccp_pr_debug("feat %d type %d never requested\n",
457afe00251SAndrea Bittau 			      feature, type);
458afe00251SAndrea Bittau 	return 0;
459afe00251SAndrea Bittau }
460afe00251SAndrea Bittau 
461afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_confirm_recv);
462afe00251SAndrea Bittau 
463afe00251SAndrea Bittau void dccp_feat_clean(struct sock *sk)
464afe00251SAndrea Bittau {
465afe00251SAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
466afe00251SAndrea Bittau 	struct dccp_opt_pend *opt, *next;
467afe00251SAndrea Bittau 
468afe00251SAndrea Bittau 	list_for_each_entry_safe(opt, next, &dp->dccps_options.dccpo_pending,
469afe00251SAndrea Bittau 				 dccpop_node) {
470afe00251SAndrea Bittau                 BUG_ON(opt->dccpop_val == NULL);
471afe00251SAndrea Bittau                 kfree(opt->dccpop_val);
472afe00251SAndrea Bittau 
473afe00251SAndrea Bittau 		if (opt->dccpop_sc != NULL) {
474afe00251SAndrea Bittau 			BUG_ON(opt->dccpop_sc->dccpoc_val == NULL);
475afe00251SAndrea Bittau 			kfree(opt->dccpop_sc->dccpoc_val);
476afe00251SAndrea Bittau 			kfree(opt->dccpop_sc);
477afe00251SAndrea Bittau 		}
478afe00251SAndrea Bittau 
479afe00251SAndrea Bittau                 kfree(opt);
480afe00251SAndrea Bittau         }
481afe00251SAndrea Bittau 	INIT_LIST_HEAD(&dp->dccps_options.dccpo_pending);
482afe00251SAndrea Bittau 
483afe00251SAndrea Bittau 	list_for_each_entry_safe(opt, next, &dp->dccps_options.dccpo_conf,
484afe00251SAndrea Bittau 				 dccpop_node) {
485afe00251SAndrea Bittau 		BUG_ON(opt == NULL);
486afe00251SAndrea Bittau 		if (opt->dccpop_val != NULL)
487afe00251SAndrea Bittau 			kfree(opt->dccpop_val);
488afe00251SAndrea Bittau 		kfree(opt);
489afe00251SAndrea Bittau 	}
490afe00251SAndrea Bittau 	INIT_LIST_HEAD(&dp->dccps_options.dccpo_conf);
491afe00251SAndrea Bittau }
492afe00251SAndrea Bittau 
493afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_clean);
494afe00251SAndrea Bittau 
495afe00251SAndrea Bittau /* this is to be called only when a listening sock creates its child.  It is
496afe00251SAndrea Bittau  * assumed by the function---the confirm is not duplicated, but rather it is
497afe00251SAndrea Bittau  * "passed on".
498afe00251SAndrea Bittau  */
499afe00251SAndrea Bittau int dccp_feat_clone(struct sock *oldsk, struct sock *newsk)
500afe00251SAndrea Bittau {
501afe00251SAndrea Bittau 	struct dccp_sock *olddp = dccp_sk(oldsk);
502afe00251SAndrea Bittau 	struct dccp_sock *newdp = dccp_sk(newsk);
503afe00251SAndrea Bittau 	struct dccp_opt_pend *opt;
504afe00251SAndrea Bittau 	int rc = 0;
505afe00251SAndrea Bittau 
506afe00251SAndrea Bittau 	INIT_LIST_HEAD(&newdp->dccps_options.dccpo_pending);
507afe00251SAndrea Bittau 	INIT_LIST_HEAD(&newdp->dccps_options.dccpo_conf);
508afe00251SAndrea Bittau 
509afe00251SAndrea Bittau 	list_for_each_entry(opt, &olddp->dccps_options.dccpo_pending,
510afe00251SAndrea Bittau 			    dccpop_node) {
511afe00251SAndrea Bittau 		struct dccp_opt_pend *newopt;
512afe00251SAndrea Bittau 		/* copy the value of the option */
513afe00251SAndrea Bittau 		u8 *val = kmalloc(opt->dccpop_len, GFP_ATOMIC);
514afe00251SAndrea Bittau 
515afe00251SAndrea Bittau 		if (val == NULL)
516afe00251SAndrea Bittau 			goto out_clean;
517afe00251SAndrea Bittau 		memcpy(val, opt->dccpop_val, opt->dccpop_len);
518afe00251SAndrea Bittau 
519afe00251SAndrea Bittau 		newopt = kmalloc(sizeof(*newopt), GFP_ATOMIC);
520afe00251SAndrea Bittau 		if (newopt == NULL) {
521afe00251SAndrea Bittau 			kfree(val);
522afe00251SAndrea Bittau 			goto out_clean;
523afe00251SAndrea Bittau 		}
524afe00251SAndrea Bittau 
525afe00251SAndrea Bittau 		/* insert the option */
526afe00251SAndrea Bittau 		memcpy(newopt, opt, sizeof(*newopt));
527afe00251SAndrea Bittau 		newopt->dccpop_val = val;
528afe00251SAndrea Bittau 		list_add_tail(&newopt->dccpop_node,
529afe00251SAndrea Bittau 			      &newdp->dccps_options.dccpo_pending);
530afe00251SAndrea Bittau 
531afe00251SAndrea Bittau 		/* XXX what happens with backlogs and multiple connections at
532afe00251SAndrea Bittau 		 * once...
533afe00251SAndrea Bittau 		 */
534afe00251SAndrea Bittau 		/* the master socket no longer needs to worry about confirms */
535afe00251SAndrea Bittau 		opt->dccpop_sc = 0; /* it's not a memleak---new socket has it */
536afe00251SAndrea Bittau 
537afe00251SAndrea Bittau 		/* reset state for a new socket */
538afe00251SAndrea Bittau 		opt->dccpop_conf = 0;
539afe00251SAndrea Bittau 	}
540afe00251SAndrea Bittau 
541afe00251SAndrea Bittau 	/* XXX not doing anything about the conf queue */
542afe00251SAndrea Bittau 
543afe00251SAndrea Bittau out:
544afe00251SAndrea Bittau 	return rc;
545afe00251SAndrea Bittau 
546afe00251SAndrea Bittau out_clean:
547afe00251SAndrea Bittau 	dccp_feat_clean(newsk);
548afe00251SAndrea Bittau 	rc = -ENOMEM;
549afe00251SAndrea Bittau 	goto out;
550afe00251SAndrea Bittau }
551afe00251SAndrea Bittau 
552afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_clone);
553afe00251SAndrea Bittau 
554afe00251SAndrea Bittau static int __dccp_feat_init(struct sock *sk, u8 type, u8 feat, u8 *val, u8 len)
555afe00251SAndrea Bittau {
556afe00251SAndrea Bittau 	int rc = -ENOMEM;
557afe00251SAndrea Bittau 	u8 *copy = kmalloc(len, GFP_KERNEL);
558afe00251SAndrea Bittau 
559afe00251SAndrea Bittau 	if (copy != NULL) {
560afe00251SAndrea Bittau 		memcpy(copy, val, len);
561afe00251SAndrea Bittau 		rc = dccp_feat_change(sk, type, feat, copy, len, GFP_KERNEL);
562afe00251SAndrea Bittau 		if (rc)
563afe00251SAndrea Bittau 			kfree(copy);
564afe00251SAndrea Bittau 	}
565afe00251SAndrea Bittau 	return rc;
566afe00251SAndrea Bittau }
567afe00251SAndrea Bittau 
568afe00251SAndrea Bittau int dccp_feat_init(struct sock *sk)
569afe00251SAndrea Bittau {
570afe00251SAndrea Bittau 	struct dccp_sock *dp = dccp_sk(sk);
571afe00251SAndrea Bittau 	int rc;
572afe00251SAndrea Bittau 
573afe00251SAndrea Bittau 	INIT_LIST_HEAD(&dp->dccps_options.dccpo_pending);
574afe00251SAndrea Bittau 	INIT_LIST_HEAD(&dp->dccps_options.dccpo_conf);
575afe00251SAndrea Bittau 
576afe00251SAndrea Bittau 	/* CCID L */
577afe00251SAndrea Bittau 	rc = __dccp_feat_init(sk, DCCPO_CHANGE_L, DCCPF_CCID,
578afe00251SAndrea Bittau 			      &dp->dccps_options.dccpo_tx_ccid, 1);
579afe00251SAndrea Bittau 	if (rc)
580afe00251SAndrea Bittau 		goto out;
581afe00251SAndrea Bittau 
582afe00251SAndrea Bittau 	/* CCID R */
583afe00251SAndrea Bittau 	rc = __dccp_feat_init(sk, DCCPO_CHANGE_R, DCCPF_CCID,
584afe00251SAndrea Bittau 			      &dp->dccps_options.dccpo_rx_ccid, 1);
585afe00251SAndrea Bittau 	if (rc)
586afe00251SAndrea Bittau 		goto out;
587afe00251SAndrea Bittau 
588afe00251SAndrea Bittau 	/* Ack ratio */
589afe00251SAndrea Bittau 	rc = __dccp_feat_init(sk, DCCPO_CHANGE_L, DCCPF_ACK_RATIO,
590afe00251SAndrea Bittau 			      &dp->dccps_options.dccpo_ack_ratio, 1);
591afe00251SAndrea Bittau out:
592afe00251SAndrea Bittau 	return rc;
593afe00251SAndrea Bittau }
594afe00251SAndrea Bittau 
595afe00251SAndrea Bittau EXPORT_SYMBOL_GPL(dccp_feat_init);
596