xref: /OK3568_Linux_fs/kernel/net/sctp/sm_statefuns.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* SCTP kernel implementation
3*4882a593Smuzhiyun  * (C) Copyright IBM Corp. 2001, 2004
4*4882a593Smuzhiyun  * Copyright (c) 1999-2000 Cisco, Inc.
5*4882a593Smuzhiyun  * Copyright (c) 1999-2001 Motorola, Inc.
6*4882a593Smuzhiyun  * Copyright (c) 2001-2002 Intel Corp.
7*4882a593Smuzhiyun  * Copyright (c) 2002      Nokia Corp.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This is part of the SCTP Linux Kernel Implementation.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * These are the state functions for the state machine.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * Please send any bug reports or fixes you make to the
14*4882a593Smuzhiyun  * email address(es):
15*4882a593Smuzhiyun  *    lksctp developers <linux-sctp@vger.kernel.org>
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * Written or modified by:
18*4882a593Smuzhiyun  *    La Monte H.P. Yarroll <piggy@acm.org>
19*4882a593Smuzhiyun  *    Karl Knutson          <karl@athena.chicago.il.us>
20*4882a593Smuzhiyun  *    Mathew Kotowsky       <kotowsky@sctp.org>
21*4882a593Smuzhiyun  *    Sridhar Samudrala     <samudrala@us.ibm.com>
22*4882a593Smuzhiyun  *    Jon Grimm             <jgrimm@us.ibm.com>
23*4882a593Smuzhiyun  *    Hui Huang 	    <hui.huang@nokia.com>
24*4882a593Smuzhiyun  *    Dajiang Zhang 	    <dajiang.zhang@nokia.com>
25*4882a593Smuzhiyun  *    Daisy Chang	    <daisyc@us.ibm.com>
26*4882a593Smuzhiyun  *    Ardelle Fan	    <ardelle.fan@intel.com>
27*4882a593Smuzhiyun  *    Ryan Layer	    <rmlayer@us.ibm.com>
28*4882a593Smuzhiyun  *    Kevin Gao		    <kevin.gao@intel.com>
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include <linux/types.h>
34*4882a593Smuzhiyun #include <linux/kernel.h>
35*4882a593Smuzhiyun #include <linux/ip.h>
36*4882a593Smuzhiyun #include <linux/ipv6.h>
37*4882a593Smuzhiyun #include <linux/net.h>
38*4882a593Smuzhiyun #include <linux/inet.h>
39*4882a593Smuzhiyun #include <linux/slab.h>
40*4882a593Smuzhiyun #include <net/sock.h>
41*4882a593Smuzhiyun #include <net/inet_ecn.h>
42*4882a593Smuzhiyun #include <linux/skbuff.h>
43*4882a593Smuzhiyun #include <net/sctp/sctp.h>
44*4882a593Smuzhiyun #include <net/sctp/sm.h>
45*4882a593Smuzhiyun #include <net/sctp/structs.h>
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define CREATE_TRACE_POINTS
48*4882a593Smuzhiyun #include <trace/events/sctp.h>
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun static struct sctp_packet *sctp_abort_pkt_new(
51*4882a593Smuzhiyun 					struct net *net,
52*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
53*4882a593Smuzhiyun 					const struct sctp_association *asoc,
54*4882a593Smuzhiyun 					struct sctp_chunk *chunk,
55*4882a593Smuzhiyun 					const void *payload, size_t paylen);
56*4882a593Smuzhiyun static int sctp_eat_data(const struct sctp_association *asoc,
57*4882a593Smuzhiyun 			 struct sctp_chunk *chunk,
58*4882a593Smuzhiyun 			 struct sctp_cmd_seq *commands);
59*4882a593Smuzhiyun static struct sctp_packet *sctp_ootb_pkt_new(
60*4882a593Smuzhiyun 					struct net *net,
61*4882a593Smuzhiyun 					const struct sctp_association *asoc,
62*4882a593Smuzhiyun 					const struct sctp_chunk *chunk);
63*4882a593Smuzhiyun static void sctp_send_stale_cookie_err(struct net *net,
64*4882a593Smuzhiyun 				       const struct sctp_endpoint *ep,
65*4882a593Smuzhiyun 				       const struct sctp_association *asoc,
66*4882a593Smuzhiyun 				       const struct sctp_chunk *chunk,
67*4882a593Smuzhiyun 				       struct sctp_cmd_seq *commands,
68*4882a593Smuzhiyun 				       struct sctp_chunk *err_chunk);
69*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_do_5_2_6_stale(
70*4882a593Smuzhiyun 					struct net *net,
71*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
72*4882a593Smuzhiyun 					const struct sctp_association *asoc,
73*4882a593Smuzhiyun 					const union sctp_subtype type,
74*4882a593Smuzhiyun 					void *arg,
75*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands);
76*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_shut_8_4_5(
77*4882a593Smuzhiyun 					struct net *net,
78*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
79*4882a593Smuzhiyun 					const struct sctp_association *asoc,
80*4882a593Smuzhiyun 					const union sctp_subtype type,
81*4882a593Smuzhiyun 					void *arg,
82*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands);
83*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_tabort_8_4_8(
84*4882a593Smuzhiyun 					struct net *net,
85*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
86*4882a593Smuzhiyun 					const struct sctp_association *asoc,
87*4882a593Smuzhiyun 					const union sctp_subtype type,
88*4882a593Smuzhiyun 					void *arg,
89*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands);
90*4882a593Smuzhiyun static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun static enum sctp_disposition sctp_stop_t1_and_abort(
93*4882a593Smuzhiyun 					struct net *net,
94*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands,
95*4882a593Smuzhiyun 					__be16 error, int sk_err,
96*4882a593Smuzhiyun 					const struct sctp_association *asoc,
97*4882a593Smuzhiyun 					struct sctp_transport *transport);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_abort_violation(
100*4882a593Smuzhiyun 					struct net *net,
101*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
102*4882a593Smuzhiyun 					const struct sctp_association *asoc,
103*4882a593Smuzhiyun 					void *arg,
104*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands,
105*4882a593Smuzhiyun 					const __u8 *payload,
106*4882a593Smuzhiyun 					const size_t paylen);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_violation_chunklen(
109*4882a593Smuzhiyun 					struct net *net,
110*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
111*4882a593Smuzhiyun 					const struct sctp_association *asoc,
112*4882a593Smuzhiyun 					const union sctp_subtype type,
113*4882a593Smuzhiyun 					void *arg,
114*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_violation_paramlen(
117*4882a593Smuzhiyun 					struct net *net,
118*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
119*4882a593Smuzhiyun 					const struct sctp_association *asoc,
120*4882a593Smuzhiyun 					const union sctp_subtype type,
121*4882a593Smuzhiyun 					void *arg, void *ext,
122*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_violation_ctsn(
125*4882a593Smuzhiyun 					struct net *net,
126*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
127*4882a593Smuzhiyun 					const struct sctp_association *asoc,
128*4882a593Smuzhiyun 					const union sctp_subtype type,
129*4882a593Smuzhiyun 					void *arg,
130*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_violation_chunk(
133*4882a593Smuzhiyun 					struct net *net,
134*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
135*4882a593Smuzhiyun 					const struct sctp_association *asoc,
136*4882a593Smuzhiyun 					const union sctp_subtype type,
137*4882a593Smuzhiyun 					void *arg,
138*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun static enum sctp_ierror sctp_sf_authenticate(
141*4882a593Smuzhiyun 					const struct sctp_association *asoc,
142*4882a593Smuzhiyun 					struct sctp_chunk *chunk);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun static enum sctp_disposition __sctp_sf_do_9_1_abort(
145*4882a593Smuzhiyun 					struct net *net,
146*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
147*4882a593Smuzhiyun 					const struct sctp_association *asoc,
148*4882a593Smuzhiyun 					const union sctp_subtype type,
149*4882a593Smuzhiyun 					void *arg,
150*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun static enum sctp_disposition
153*4882a593Smuzhiyun __sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
154*4882a593Smuzhiyun 			   const struct sctp_association *asoc,
155*4882a593Smuzhiyun 			   const union sctp_subtype type, void *arg,
156*4882a593Smuzhiyun 			   struct sctp_cmd_seq *commands);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /* Small helper function that checks if the chunk length
159*4882a593Smuzhiyun  * is of the appropriate length.  The 'required_length' argument
160*4882a593Smuzhiyun  * is set to be the size of a specific chunk we are testing.
161*4882a593Smuzhiyun  * Return Values:  true  = Valid length
162*4882a593Smuzhiyun  * 		   false = Invalid length
163*4882a593Smuzhiyun  *
164*4882a593Smuzhiyun  */
sctp_chunk_length_valid(struct sctp_chunk * chunk,__u16 required_length)165*4882a593Smuzhiyun static inline bool sctp_chunk_length_valid(struct sctp_chunk *chunk,
166*4882a593Smuzhiyun 					   __u16 required_length)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	__u16 chunk_length = ntohs(chunk->chunk_hdr->length);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	/* Previously already marked? */
171*4882a593Smuzhiyun 	if (unlikely(chunk->pdiscard))
172*4882a593Smuzhiyun 		return false;
173*4882a593Smuzhiyun 	if (unlikely(chunk_length < required_length))
174*4882a593Smuzhiyun 		return false;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	return true;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /* Check for format error in an ABORT chunk */
sctp_err_chunk_valid(struct sctp_chunk * chunk)180*4882a593Smuzhiyun static inline bool sctp_err_chunk_valid(struct sctp_chunk *chunk)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	struct sctp_errhdr *err;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	sctp_walk_errors(err, chunk->chunk_hdr);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	return (void *)err == (void *)chunk->chunk_end;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /**********************************************************
190*4882a593Smuzhiyun  * These are the state functions for handling chunk events.
191*4882a593Smuzhiyun  **********************************************************/
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun /*
194*4882a593Smuzhiyun  * Process the final SHUTDOWN COMPLETE.
195*4882a593Smuzhiyun  *
196*4882a593Smuzhiyun  * Section: 4 (C) (diagram), 9.2
197*4882a593Smuzhiyun  * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
198*4882a593Smuzhiyun  * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
199*4882a593Smuzhiyun  * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
200*4882a593Smuzhiyun  * should stop the T2-shutdown timer and remove all knowledge of the
201*4882a593Smuzhiyun  * association (and thus the association enters the CLOSED state).
202*4882a593Smuzhiyun  *
203*4882a593Smuzhiyun  * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
204*4882a593Smuzhiyun  * C) Rules for packet carrying SHUTDOWN COMPLETE:
205*4882a593Smuzhiyun  * ...
206*4882a593Smuzhiyun  * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
207*4882a593Smuzhiyun  *   if the Verification Tag field of the packet matches its own tag and
208*4882a593Smuzhiyun  *   the T bit is not set
209*4882a593Smuzhiyun  *   OR
210*4882a593Smuzhiyun  *   it is set to its peer's tag and the T bit is set in the Chunk
211*4882a593Smuzhiyun  *   Flags.
212*4882a593Smuzhiyun  *   Otherwise, the receiver MUST silently discard the packet
213*4882a593Smuzhiyun  *   and take no further action.  An endpoint MUST ignore the
214*4882a593Smuzhiyun  *   SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
215*4882a593Smuzhiyun  *
216*4882a593Smuzhiyun  * Inputs
217*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
218*4882a593Smuzhiyun  *
219*4882a593Smuzhiyun  * Outputs
220*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
221*4882a593Smuzhiyun  *
222*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
223*4882a593Smuzhiyun  */
sctp_sf_do_4_C(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)224*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_4_C(struct net *net,
225*4882a593Smuzhiyun 				     const struct sctp_endpoint *ep,
226*4882a593Smuzhiyun 				     const struct sctp_association *asoc,
227*4882a593Smuzhiyun 				     const union sctp_subtype type,
228*4882a593Smuzhiyun 				     void *arg, struct sctp_cmd_seq *commands)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
231*4882a593Smuzhiyun 	struct sctp_ulpevent *ev;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	if (!sctp_vtag_verify_either(chunk, asoc))
234*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	/* RFC 2960 6.10 Bundling
237*4882a593Smuzhiyun 	 *
238*4882a593Smuzhiyun 	 * An endpoint MUST NOT bundle INIT, INIT ACK or
239*4882a593Smuzhiyun 	 * SHUTDOWN COMPLETE with any other chunks.
240*4882a593Smuzhiyun 	 */
241*4882a593Smuzhiyun 	if (!chunk->singleton)
242*4882a593Smuzhiyun 		return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
245*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
246*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
247*4882a593Smuzhiyun 						  commands);
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	/* RFC 2960 10.2 SCTP-to-ULP
250*4882a593Smuzhiyun 	 *
251*4882a593Smuzhiyun 	 * H) SHUTDOWN COMPLETE notification
252*4882a593Smuzhiyun 	 *
253*4882a593Smuzhiyun 	 * When SCTP completes the shutdown procedures (section 9.2) this
254*4882a593Smuzhiyun 	 * notification is passed to the upper layer.
255*4882a593Smuzhiyun 	 */
256*4882a593Smuzhiyun 	ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
257*4882a593Smuzhiyun 					     0, 0, 0, NULL, GFP_ATOMIC);
258*4882a593Smuzhiyun 	if (ev)
259*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
260*4882a593Smuzhiyun 				SCTP_ULPEVENT(ev));
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
263*4882a593Smuzhiyun 	 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
264*4882a593Smuzhiyun 	 * not the chunk should be discarded. If the endpoint is in
265*4882a593Smuzhiyun 	 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
266*4882a593Smuzhiyun 	 * T2-shutdown timer and remove all knowledge of the
267*4882a593Smuzhiyun 	 * association (and thus the association enters the CLOSED
268*4882a593Smuzhiyun 	 * state).
269*4882a593Smuzhiyun 	 */
270*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
271*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
274*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
277*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_CLOSED));
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
280*4882a593Smuzhiyun 	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DELETE_TCB;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun  * Respond to a normal INIT chunk.
289*4882a593Smuzhiyun  * We are the side that is being asked for an association.
290*4882a593Smuzhiyun  *
291*4882a593Smuzhiyun  * Section: 5.1 Normal Establishment of an Association, B
292*4882a593Smuzhiyun  * B) "Z" shall respond immediately with an INIT ACK chunk.  The
293*4882a593Smuzhiyun  *    destination IP address of the INIT ACK MUST be set to the source
294*4882a593Smuzhiyun  *    IP address of the INIT to which this INIT ACK is responding.  In
295*4882a593Smuzhiyun  *    the response, besides filling in other parameters, "Z" must set the
296*4882a593Smuzhiyun  *    Verification Tag field to Tag_A, and also provide its own
297*4882a593Smuzhiyun  *    Verification Tag (Tag_Z) in the Initiate Tag field.
298*4882a593Smuzhiyun  *
299*4882a593Smuzhiyun  * Verification Tag: Must be 0.
300*4882a593Smuzhiyun  *
301*4882a593Smuzhiyun  * Inputs
302*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
303*4882a593Smuzhiyun  *
304*4882a593Smuzhiyun  * Outputs
305*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
306*4882a593Smuzhiyun  *
307*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
308*4882a593Smuzhiyun  */
sctp_sf_do_5_1B_init(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)309*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
310*4882a593Smuzhiyun 					   const struct sctp_endpoint *ep,
311*4882a593Smuzhiyun 					   const struct sctp_association *asoc,
312*4882a593Smuzhiyun 					   const union sctp_subtype type,
313*4882a593Smuzhiyun 					   void *arg,
314*4882a593Smuzhiyun 					   struct sctp_cmd_seq *commands)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg, *repl, *err_chunk;
317*4882a593Smuzhiyun 	struct sctp_unrecognized_param *unk_param;
318*4882a593Smuzhiyun 	struct sctp_association *new_asoc;
319*4882a593Smuzhiyun 	struct sctp_packet *packet;
320*4882a593Smuzhiyun 	int len;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	/* Update socket peer label if first association. */
323*4882a593Smuzhiyun 	if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
324*4882a593Smuzhiyun 					chunk->skb))
325*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	/* 6.10 Bundling
328*4882a593Smuzhiyun 	 * An endpoint MUST NOT bundle INIT, INIT ACK or
329*4882a593Smuzhiyun 	 * SHUTDOWN COMPLETE with any other chunks.
330*4882a593Smuzhiyun 	 *
331*4882a593Smuzhiyun 	 * IG Section 2.11.2
332*4882a593Smuzhiyun 	 * Furthermore, we require that the receiver of an INIT chunk MUST
333*4882a593Smuzhiyun 	 * enforce these rules by silently discarding an arriving packet
334*4882a593Smuzhiyun 	 * with an INIT chunk that is bundled with other chunks.
335*4882a593Smuzhiyun 	 */
336*4882a593Smuzhiyun 	if (!chunk->singleton)
337*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	/* Make sure that the INIT chunk has a valid length.
340*4882a593Smuzhiyun 	 * Normally, this would cause an ABORT with a Protocol Violation
341*4882a593Smuzhiyun 	 * error, but since we don't have an association, we'll
342*4882a593Smuzhiyun 	 * just discard the packet.
343*4882a593Smuzhiyun 	 */
344*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
345*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	/* If the packet is an OOTB packet which is temporarily on the
348*4882a593Smuzhiyun 	 * control endpoint, respond with an ABORT.
349*4882a593Smuzhiyun 	 */
350*4882a593Smuzhiyun 	if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
351*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
352*4882a593Smuzhiyun 		return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
353*4882a593Smuzhiyun 	}
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	/* 3.1 A packet containing an INIT chunk MUST have a zero Verification
356*4882a593Smuzhiyun 	 * Tag.
357*4882a593Smuzhiyun 	 */
358*4882a593Smuzhiyun 	if (chunk->sctp_hdr->vtag != 0)
359*4882a593Smuzhiyun 		return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	/* If the INIT is coming toward a closing socket, we'll send back
362*4882a593Smuzhiyun 	 * and ABORT.  Essentially, this catches the race of INIT being
363*4882a593Smuzhiyun 	 * backloged to the socket at the same time as the user isses close().
364*4882a593Smuzhiyun 	 * Since the socket and all its associations are going away, we
365*4882a593Smuzhiyun 	 * can treat this OOTB
366*4882a593Smuzhiyun 	 */
367*4882a593Smuzhiyun 	if (sctp_sstate(ep->base.sk, CLOSING))
368*4882a593Smuzhiyun 		return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	/* Verify the INIT chunk before processing it. */
371*4882a593Smuzhiyun 	err_chunk = NULL;
372*4882a593Smuzhiyun 	if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
373*4882a593Smuzhiyun 			      (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
374*4882a593Smuzhiyun 			      &err_chunk)) {
375*4882a593Smuzhiyun 		/* This chunk contains fatal error. It is to be discarded.
376*4882a593Smuzhiyun 		 * Send an ABORT, with causes if there is any.
377*4882a593Smuzhiyun 		 */
378*4882a593Smuzhiyun 		if (err_chunk) {
379*4882a593Smuzhiyun 			packet = sctp_abort_pkt_new(net, ep, asoc, arg,
380*4882a593Smuzhiyun 					(__u8 *)(err_chunk->chunk_hdr) +
381*4882a593Smuzhiyun 					sizeof(struct sctp_chunkhdr),
382*4882a593Smuzhiyun 					ntohs(err_chunk->chunk_hdr->length) -
383*4882a593Smuzhiyun 					sizeof(struct sctp_chunkhdr));
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 			sctp_chunk_free(err_chunk);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 			if (packet) {
388*4882a593Smuzhiyun 				sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
389*4882a593Smuzhiyun 						SCTP_PACKET(packet));
390*4882a593Smuzhiyun 				SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
391*4882a593Smuzhiyun 				return SCTP_DISPOSITION_CONSUME;
392*4882a593Smuzhiyun 			} else {
393*4882a593Smuzhiyun 				return SCTP_DISPOSITION_NOMEM;
394*4882a593Smuzhiyun 			}
395*4882a593Smuzhiyun 		} else {
396*4882a593Smuzhiyun 			return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
397*4882a593Smuzhiyun 						    commands);
398*4882a593Smuzhiyun 		}
399*4882a593Smuzhiyun 	}
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	/* Grab the INIT header.  */
402*4882a593Smuzhiyun 	chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	/* Tag the variable length parameters.  */
405*4882a593Smuzhiyun 	chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
408*4882a593Smuzhiyun 	if (!new_asoc)
409*4882a593Smuzhiyun 		goto nomem;
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
412*4882a593Smuzhiyun 					     sctp_scope(sctp_source(chunk)),
413*4882a593Smuzhiyun 					     GFP_ATOMIC) < 0)
414*4882a593Smuzhiyun 		goto nomem_init;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	/* The call, sctp_process_init(), can fail on memory allocation.  */
417*4882a593Smuzhiyun 	if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
418*4882a593Smuzhiyun 			       (struct sctp_init_chunk *)chunk->chunk_hdr,
419*4882a593Smuzhiyun 			       GFP_ATOMIC))
420*4882a593Smuzhiyun 		goto nomem_init;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	/* B) "Z" shall respond immediately with an INIT ACK chunk.  */
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	/* If there are errors need to be reported for unknown parameters,
425*4882a593Smuzhiyun 	 * make sure to reserve enough room in the INIT ACK for them.
426*4882a593Smuzhiyun 	 */
427*4882a593Smuzhiyun 	len = 0;
428*4882a593Smuzhiyun 	if (err_chunk)
429*4882a593Smuzhiyun 		len = ntohs(err_chunk->chunk_hdr->length) -
430*4882a593Smuzhiyun 		      sizeof(struct sctp_chunkhdr);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
433*4882a593Smuzhiyun 	if (!repl)
434*4882a593Smuzhiyun 		goto nomem_init;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	/* If there are errors need to be reported for unknown parameters,
437*4882a593Smuzhiyun 	 * include them in the outgoing INIT ACK as "Unrecognized parameter"
438*4882a593Smuzhiyun 	 * parameter.
439*4882a593Smuzhiyun 	 */
440*4882a593Smuzhiyun 	if (err_chunk) {
441*4882a593Smuzhiyun 		/* Get the "Unrecognized parameter" parameter(s) out of the
442*4882a593Smuzhiyun 		 * ERROR chunk generated by sctp_verify_init(). Since the
443*4882a593Smuzhiyun 		 * error cause code for "unknown parameter" and the
444*4882a593Smuzhiyun 		 * "Unrecognized parameter" type is the same, we can
445*4882a593Smuzhiyun 		 * construct the parameters in INIT ACK by copying the
446*4882a593Smuzhiyun 		 * ERROR causes over.
447*4882a593Smuzhiyun 		 */
448*4882a593Smuzhiyun 		unk_param = (struct sctp_unrecognized_param *)
449*4882a593Smuzhiyun 			    ((__u8 *)(err_chunk->chunk_hdr) +
450*4882a593Smuzhiyun 			    sizeof(struct sctp_chunkhdr));
451*4882a593Smuzhiyun 		/* Replace the cause code with the "Unrecognized parameter"
452*4882a593Smuzhiyun 		 * parameter type.
453*4882a593Smuzhiyun 		 */
454*4882a593Smuzhiyun 		sctp_addto_chunk(repl, len, unk_param);
455*4882a593Smuzhiyun 		sctp_chunk_free(err_chunk);
456*4882a593Smuzhiyun 	}
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	/*
463*4882a593Smuzhiyun 	 * Note:  After sending out INIT ACK with the State Cookie parameter,
464*4882a593Smuzhiyun 	 * "Z" MUST NOT allocate any resources, nor keep any states for the
465*4882a593Smuzhiyun 	 * new association.  Otherwise, "Z" will be vulnerable to resource
466*4882a593Smuzhiyun 	 * attacks.
467*4882a593Smuzhiyun 	 */
468*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DELETE_TCB;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun nomem_init:
473*4882a593Smuzhiyun 	sctp_association_free(new_asoc);
474*4882a593Smuzhiyun nomem:
475*4882a593Smuzhiyun 	if (err_chunk)
476*4882a593Smuzhiyun 		sctp_chunk_free(err_chunk);
477*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun /*
481*4882a593Smuzhiyun  * Respond to a normal INIT ACK chunk.
482*4882a593Smuzhiyun  * We are the side that is initiating the association.
483*4882a593Smuzhiyun  *
484*4882a593Smuzhiyun  * Section: 5.1 Normal Establishment of an Association, C
485*4882a593Smuzhiyun  * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
486*4882a593Smuzhiyun  *    timer and leave COOKIE-WAIT state. "A" shall then send the State
487*4882a593Smuzhiyun  *    Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
488*4882a593Smuzhiyun  *    the T1-cookie timer, and enter the COOKIE-ECHOED state.
489*4882a593Smuzhiyun  *
490*4882a593Smuzhiyun  *    Note: The COOKIE ECHO chunk can be bundled with any pending outbound
491*4882a593Smuzhiyun  *    DATA chunks, but it MUST be the first chunk in the packet and
492*4882a593Smuzhiyun  *    until the COOKIE ACK is returned the sender MUST NOT send any
493*4882a593Smuzhiyun  *    other packets to the peer.
494*4882a593Smuzhiyun  *
495*4882a593Smuzhiyun  * Verification Tag: 3.3.3
496*4882a593Smuzhiyun  *   If the value of the Initiate Tag in a received INIT ACK chunk is
497*4882a593Smuzhiyun  *   found to be 0, the receiver MUST treat it as an error and close the
498*4882a593Smuzhiyun  *   association by transmitting an ABORT.
499*4882a593Smuzhiyun  *
500*4882a593Smuzhiyun  * Inputs
501*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
502*4882a593Smuzhiyun  *
503*4882a593Smuzhiyun  * Outputs
504*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
505*4882a593Smuzhiyun  *
506*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
507*4882a593Smuzhiyun  */
sctp_sf_do_5_1C_ack(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)508*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
509*4882a593Smuzhiyun 					  const struct sctp_endpoint *ep,
510*4882a593Smuzhiyun 					  const struct sctp_association *asoc,
511*4882a593Smuzhiyun 					  const union sctp_subtype type,
512*4882a593Smuzhiyun 					  void *arg,
513*4882a593Smuzhiyun 					  struct sctp_cmd_seq *commands)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun 	struct sctp_init_chunk *initchunk;
516*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
517*4882a593Smuzhiyun 	struct sctp_chunk *err_chunk;
518*4882a593Smuzhiyun 	struct sctp_packet *packet;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
521*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	/* 6.10 Bundling
524*4882a593Smuzhiyun 	 * An endpoint MUST NOT bundle INIT, INIT ACK or
525*4882a593Smuzhiyun 	 * SHUTDOWN COMPLETE with any other chunks.
526*4882a593Smuzhiyun 	 */
527*4882a593Smuzhiyun 	if (!chunk->singleton)
528*4882a593Smuzhiyun 		return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	/* Make sure that the INIT-ACK chunk has a valid length */
531*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
532*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
533*4882a593Smuzhiyun 						  commands);
534*4882a593Smuzhiyun 	/* Grab the INIT header.  */
535*4882a593Smuzhiyun 	chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	/* Verify the INIT chunk before processing it. */
538*4882a593Smuzhiyun 	err_chunk = NULL;
539*4882a593Smuzhiyun 	if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
540*4882a593Smuzhiyun 			      (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
541*4882a593Smuzhiyun 			      &err_chunk)) {
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 		enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 		/* This chunk contains fatal error. It is to be discarded.
546*4882a593Smuzhiyun 		 * Send an ABORT, with causes.  If there are no causes,
547*4882a593Smuzhiyun 		 * then there wasn't enough memory.  Just terminate
548*4882a593Smuzhiyun 		 * the association.
549*4882a593Smuzhiyun 		 */
550*4882a593Smuzhiyun 		if (err_chunk) {
551*4882a593Smuzhiyun 			packet = sctp_abort_pkt_new(net, ep, asoc, arg,
552*4882a593Smuzhiyun 					(__u8 *)(err_chunk->chunk_hdr) +
553*4882a593Smuzhiyun 					sizeof(struct sctp_chunkhdr),
554*4882a593Smuzhiyun 					ntohs(err_chunk->chunk_hdr->length) -
555*4882a593Smuzhiyun 					sizeof(struct sctp_chunkhdr));
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 			sctp_chunk_free(err_chunk);
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 			if (packet) {
560*4882a593Smuzhiyun 				sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
561*4882a593Smuzhiyun 						SCTP_PACKET(packet));
562*4882a593Smuzhiyun 				SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
563*4882a593Smuzhiyun 				error = SCTP_ERROR_INV_PARAM;
564*4882a593Smuzhiyun 			}
565*4882a593Smuzhiyun 		}
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 		/* SCTP-AUTH, Section 6.3:
568*4882a593Smuzhiyun 		 *    It should be noted that if the receiver wants to tear
569*4882a593Smuzhiyun 		 *    down an association in an authenticated way only, the
570*4882a593Smuzhiyun 		 *    handling of malformed packets should not result in
571*4882a593Smuzhiyun 		 *    tearing down the association.
572*4882a593Smuzhiyun 		 *
573*4882a593Smuzhiyun 		 * This means that if we only want to abort associations
574*4882a593Smuzhiyun 		 * in an authenticated way (i.e AUTH+ABORT), then we
575*4882a593Smuzhiyun 		 * can't destroy this association just because the packet
576*4882a593Smuzhiyun 		 * was malformed.
577*4882a593Smuzhiyun 		 */
578*4882a593Smuzhiyun 		if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
579*4882a593Smuzhiyun 			return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
582*4882a593Smuzhiyun 		return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
583*4882a593Smuzhiyun 						asoc, chunk->transport);
584*4882a593Smuzhiyun 	}
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	/* Tag the variable length parameters.  Note that we never
587*4882a593Smuzhiyun 	 * convert the parameters in an INIT chunk.
588*4882a593Smuzhiyun 	 */
589*4882a593Smuzhiyun 	chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
594*4882a593Smuzhiyun 			SCTP_PEER_INIT(initchunk));
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	/* Reset init error count upon receipt of INIT-ACK.  */
597*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	/* 5.1 C) "A" shall stop the T1-init timer and leave
600*4882a593Smuzhiyun 	 * COOKIE-WAIT state.  "A" shall then ... start the T1-cookie
601*4882a593Smuzhiyun 	 * timer, and enter the COOKIE-ECHOED state.
602*4882a593Smuzhiyun 	 */
603*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
604*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
605*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
606*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
607*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
608*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	/* SCTP-AUTH: genereate the assocition shared keys so that
611*4882a593Smuzhiyun 	 * we can potentially signe the COOKIE-ECHO.
612*4882a593Smuzhiyun 	 */
613*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	/* 5.1 C) "A" shall then send the State Cookie received in the
616*4882a593Smuzhiyun 	 * INIT ACK chunk in a COOKIE ECHO chunk, ...
617*4882a593Smuzhiyun 	 */
618*4882a593Smuzhiyun 	/* If there is any errors to report, send the ERROR chunk generated
619*4882a593Smuzhiyun 	 * for unknown parameters as well.
620*4882a593Smuzhiyun 	 */
621*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
622*4882a593Smuzhiyun 			SCTP_CHUNK(err_chunk));
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun 
sctp_auth_chunk_verify(struct net * net,struct sctp_chunk * chunk,const struct sctp_association * asoc)627*4882a593Smuzhiyun static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
628*4882a593Smuzhiyun 				   const struct sctp_association *asoc)
629*4882a593Smuzhiyun {
630*4882a593Smuzhiyun 	struct sctp_chunk auth;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	if (!chunk->auth_chunk)
633*4882a593Smuzhiyun 		return true;
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	/* SCTP-AUTH:  auth_chunk pointer is only set when the cookie-echo
636*4882a593Smuzhiyun 	 * is supposed to be authenticated and we have to do delayed
637*4882a593Smuzhiyun 	 * authentication.  We've just recreated the association using
638*4882a593Smuzhiyun 	 * the information in the cookie and now it's much easier to
639*4882a593Smuzhiyun 	 * do the authentication.
640*4882a593Smuzhiyun 	 */
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	/* Make sure that we and the peer are AUTH capable */
643*4882a593Smuzhiyun 	if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
644*4882a593Smuzhiyun 		return false;
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	/* set-up our fake chunk so that we can process it */
647*4882a593Smuzhiyun 	auth.skb = chunk->auth_chunk;
648*4882a593Smuzhiyun 	auth.asoc = chunk->asoc;
649*4882a593Smuzhiyun 	auth.sctp_hdr = chunk->sctp_hdr;
650*4882a593Smuzhiyun 	auth.chunk_hdr = (struct sctp_chunkhdr *)
651*4882a593Smuzhiyun 				skb_push(chunk->auth_chunk,
652*4882a593Smuzhiyun 					 sizeof(struct sctp_chunkhdr));
653*4882a593Smuzhiyun 	skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
654*4882a593Smuzhiyun 	auth.transport = chunk->transport;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
657*4882a593Smuzhiyun }
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun /*
660*4882a593Smuzhiyun  * Respond to a normal COOKIE ECHO chunk.
661*4882a593Smuzhiyun  * We are the side that is being asked for an association.
662*4882a593Smuzhiyun  *
663*4882a593Smuzhiyun  * Section: 5.1 Normal Establishment of an Association, D
664*4882a593Smuzhiyun  * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
665*4882a593Smuzhiyun  *    with a COOKIE ACK chunk after building a TCB and moving to
666*4882a593Smuzhiyun  *    the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
667*4882a593Smuzhiyun  *    any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
668*4882a593Smuzhiyun  *    chunk MUST be the first chunk in the packet.
669*4882a593Smuzhiyun  *
670*4882a593Smuzhiyun  *   IMPLEMENTATION NOTE: An implementation may choose to send the
671*4882a593Smuzhiyun  *   Communication Up notification to the SCTP user upon reception
672*4882a593Smuzhiyun  *   of a valid COOKIE ECHO chunk.
673*4882a593Smuzhiyun  *
674*4882a593Smuzhiyun  * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
675*4882a593Smuzhiyun  * D) Rules for packet carrying a COOKIE ECHO
676*4882a593Smuzhiyun  *
677*4882a593Smuzhiyun  * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
678*4882a593Smuzhiyun  *   Initial Tag received in the INIT ACK.
679*4882a593Smuzhiyun  *
680*4882a593Smuzhiyun  * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
681*4882a593Smuzhiyun  *
682*4882a593Smuzhiyun  * Inputs
683*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
684*4882a593Smuzhiyun  *
685*4882a593Smuzhiyun  * Outputs
686*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
687*4882a593Smuzhiyun  *
688*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
689*4882a593Smuzhiyun  */
sctp_sf_do_5_1D_ce(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)690*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
691*4882a593Smuzhiyun 					 const struct sctp_endpoint *ep,
692*4882a593Smuzhiyun 					 const struct sctp_association *asoc,
693*4882a593Smuzhiyun 					 const union sctp_subtype type,
694*4882a593Smuzhiyun 					 void *arg,
695*4882a593Smuzhiyun 					 struct sctp_cmd_seq *commands)
696*4882a593Smuzhiyun {
697*4882a593Smuzhiyun 	struct sctp_ulpevent *ev, *ai_ev = NULL, *auth_ev = NULL;
698*4882a593Smuzhiyun 	struct sctp_association *new_asoc;
699*4882a593Smuzhiyun 	struct sctp_init_chunk *peer_init;
700*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
701*4882a593Smuzhiyun 	struct sctp_chunk *err_chk_p;
702*4882a593Smuzhiyun 	struct sctp_chunk *repl;
703*4882a593Smuzhiyun 	struct sock *sk;
704*4882a593Smuzhiyun 	int error = 0;
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	if (asoc && !sctp_vtag_verify(chunk, asoc))
707*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	/* If the packet is an OOTB packet which is temporarily on the
710*4882a593Smuzhiyun 	 * control endpoint, respond with an ABORT.
711*4882a593Smuzhiyun 	 */
712*4882a593Smuzhiyun 	if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
713*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
714*4882a593Smuzhiyun 		return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
715*4882a593Smuzhiyun 	}
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	/* Make sure that the COOKIE_ECHO chunk has a valid length.
718*4882a593Smuzhiyun 	 * In this case, we check that we have enough for at least a
719*4882a593Smuzhiyun 	 * chunk header.  More detailed verification is done
720*4882a593Smuzhiyun 	 * in sctp_unpack_cookie().
721*4882a593Smuzhiyun 	 */
722*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
723*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
724*4882a593Smuzhiyun 						  commands);
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	/* If the endpoint is not listening or if the number of associations
727*4882a593Smuzhiyun 	 * on the TCP-style socket exceed the max backlog, respond with an
728*4882a593Smuzhiyun 	 * ABORT.
729*4882a593Smuzhiyun 	 */
730*4882a593Smuzhiyun 	sk = ep->base.sk;
731*4882a593Smuzhiyun 	if (!sctp_sstate(sk, LISTENING) ||
732*4882a593Smuzhiyun 	    (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
733*4882a593Smuzhiyun 		return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	/* "Decode" the chunk.  We have no optional parameters so we
736*4882a593Smuzhiyun 	 * are in good shape.
737*4882a593Smuzhiyun 	 */
738*4882a593Smuzhiyun 	chunk->subh.cookie_hdr =
739*4882a593Smuzhiyun 		(struct sctp_signed_cookie *)chunk->skb->data;
740*4882a593Smuzhiyun 	if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
741*4882a593Smuzhiyun 					 sizeof(struct sctp_chunkhdr)))
742*4882a593Smuzhiyun 		goto nomem;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	/* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
745*4882a593Smuzhiyun 	 * "Z" will reply with a COOKIE ACK chunk after building a TCB
746*4882a593Smuzhiyun 	 * and moving to the ESTABLISHED state.
747*4882a593Smuzhiyun 	 */
748*4882a593Smuzhiyun 	new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
749*4882a593Smuzhiyun 				      &err_chk_p);
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	/* FIXME:
752*4882a593Smuzhiyun 	 * If the re-build failed, what is the proper error path
753*4882a593Smuzhiyun 	 * from here?
754*4882a593Smuzhiyun 	 *
755*4882a593Smuzhiyun 	 * [We should abort the association. --piggy]
756*4882a593Smuzhiyun 	 */
757*4882a593Smuzhiyun 	if (!new_asoc) {
758*4882a593Smuzhiyun 		/* FIXME: Several errors are possible.  A bad cookie should
759*4882a593Smuzhiyun 		 * be silently discarded, but think about logging it too.
760*4882a593Smuzhiyun 		 */
761*4882a593Smuzhiyun 		switch (error) {
762*4882a593Smuzhiyun 		case -SCTP_IERROR_NOMEM:
763*4882a593Smuzhiyun 			goto nomem;
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 		case -SCTP_IERROR_STALE_COOKIE:
766*4882a593Smuzhiyun 			sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
767*4882a593Smuzhiyun 						   err_chk_p);
768*4882a593Smuzhiyun 			return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 		case -SCTP_IERROR_BAD_SIG:
771*4882a593Smuzhiyun 		default:
772*4882a593Smuzhiyun 			return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
773*4882a593Smuzhiyun 		}
774*4882a593Smuzhiyun 	}
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	/* Delay state machine commands until later.
778*4882a593Smuzhiyun 	 *
779*4882a593Smuzhiyun 	 * Re-build the bind address for the association is done in
780*4882a593Smuzhiyun 	 * the sctp_unpack_cookie() already.
781*4882a593Smuzhiyun 	 */
782*4882a593Smuzhiyun 	/* This is a brand-new association, so these are not yet side
783*4882a593Smuzhiyun 	 * effects--it is safe to run them here.
784*4882a593Smuzhiyun 	 */
785*4882a593Smuzhiyun 	peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	if (!sctp_process_init(new_asoc, chunk,
788*4882a593Smuzhiyun 			       &chunk->subh.cookie_hdr->c.peer_addr,
789*4882a593Smuzhiyun 			       peer_init, GFP_ATOMIC))
790*4882a593Smuzhiyun 		goto nomem_init;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	/* SCTP-AUTH:  Now that we've populate required fields in
793*4882a593Smuzhiyun 	 * sctp_process_init, set up the assocaition shared keys as
794*4882a593Smuzhiyun 	 * necessary so that we can potentially authenticate the ACK
795*4882a593Smuzhiyun 	 */
796*4882a593Smuzhiyun 	error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
797*4882a593Smuzhiyun 	if (error)
798*4882a593Smuzhiyun 		goto nomem_init;
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
801*4882a593Smuzhiyun 		sctp_association_free(new_asoc);
802*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
803*4882a593Smuzhiyun 	}
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	repl = sctp_make_cookie_ack(new_asoc, chunk);
806*4882a593Smuzhiyun 	if (!repl)
807*4882a593Smuzhiyun 		goto nomem_init;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	/* RFC 2960 5.1 Normal Establishment of an Association
810*4882a593Smuzhiyun 	 *
811*4882a593Smuzhiyun 	 * D) IMPLEMENTATION NOTE: An implementation may choose to
812*4882a593Smuzhiyun 	 * send the Communication Up notification to the SCTP user
813*4882a593Smuzhiyun 	 * upon reception of a valid COOKIE ECHO chunk.
814*4882a593Smuzhiyun 	 */
815*4882a593Smuzhiyun 	ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
816*4882a593Smuzhiyun 					     new_asoc->c.sinit_num_ostreams,
817*4882a593Smuzhiyun 					     new_asoc->c.sinit_max_instreams,
818*4882a593Smuzhiyun 					     NULL, GFP_ATOMIC);
819*4882a593Smuzhiyun 	if (!ev)
820*4882a593Smuzhiyun 		goto nomem_ev;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	/* Sockets API Draft Section 5.3.1.6
823*4882a593Smuzhiyun 	 * When a peer sends a Adaptation Layer Indication parameter , SCTP
824*4882a593Smuzhiyun 	 * delivers this notification to inform the application that of the
825*4882a593Smuzhiyun 	 * peers requested adaptation layer.
826*4882a593Smuzhiyun 	 */
827*4882a593Smuzhiyun 	if (new_asoc->peer.adaptation_ind) {
828*4882a593Smuzhiyun 		ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
829*4882a593Smuzhiyun 							    GFP_ATOMIC);
830*4882a593Smuzhiyun 		if (!ai_ev)
831*4882a593Smuzhiyun 			goto nomem_aiev;
832*4882a593Smuzhiyun 	}
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	if (!new_asoc->peer.auth_capable) {
835*4882a593Smuzhiyun 		auth_ev = sctp_ulpevent_make_authkey(new_asoc, 0,
836*4882a593Smuzhiyun 						     SCTP_AUTH_NO_AUTH,
837*4882a593Smuzhiyun 						     GFP_ATOMIC);
838*4882a593Smuzhiyun 		if (!auth_ev)
839*4882a593Smuzhiyun 			goto nomem_authev;
840*4882a593Smuzhiyun 	}
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	/* Add all the state machine commands now since we've created
843*4882a593Smuzhiyun 	 * everything.  This way we don't introduce memory corruptions
844*4882a593Smuzhiyun 	 * during side-effect processing and correclty count established
845*4882a593Smuzhiyun 	 * associations.
846*4882a593Smuzhiyun 	 */
847*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
848*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
849*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_ESTABLISHED));
850*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
851*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
852*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
855*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
856*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	/* This will send the COOKIE ACK */
859*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	/* Queue the ASSOC_CHANGE event */
862*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	/* Send up the Adaptation Layer Indication event */
865*4882a593Smuzhiyun 	if (ai_ev)
866*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
867*4882a593Smuzhiyun 				SCTP_ULPEVENT(ai_ev));
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	if (auth_ev)
870*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
871*4882a593Smuzhiyun 				SCTP_ULPEVENT(auth_ev));
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun nomem_authev:
876*4882a593Smuzhiyun 	sctp_ulpevent_free(ai_ev);
877*4882a593Smuzhiyun nomem_aiev:
878*4882a593Smuzhiyun 	sctp_ulpevent_free(ev);
879*4882a593Smuzhiyun nomem_ev:
880*4882a593Smuzhiyun 	sctp_chunk_free(repl);
881*4882a593Smuzhiyun nomem_init:
882*4882a593Smuzhiyun 	sctp_association_free(new_asoc);
883*4882a593Smuzhiyun nomem:
884*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
885*4882a593Smuzhiyun }
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun /*
888*4882a593Smuzhiyun  * Respond to a normal COOKIE ACK chunk.
889*4882a593Smuzhiyun  * We are the side that is asking for an association.
890*4882a593Smuzhiyun  *
891*4882a593Smuzhiyun  * RFC 2960 5.1 Normal Establishment of an Association
892*4882a593Smuzhiyun  *
893*4882a593Smuzhiyun  * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
894*4882a593Smuzhiyun  *    COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
895*4882a593Smuzhiyun  *    timer. It may also notify its ULP about the successful
896*4882a593Smuzhiyun  *    establishment of the association with a Communication Up
897*4882a593Smuzhiyun  *    notification (see Section 10).
898*4882a593Smuzhiyun  *
899*4882a593Smuzhiyun  * Verification Tag:
900*4882a593Smuzhiyun  * Inputs
901*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
902*4882a593Smuzhiyun  *
903*4882a593Smuzhiyun  * Outputs
904*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
905*4882a593Smuzhiyun  *
906*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
907*4882a593Smuzhiyun  */
sctp_sf_do_5_1E_ca(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)908*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_5_1E_ca(struct net *net,
909*4882a593Smuzhiyun 					 const struct sctp_endpoint *ep,
910*4882a593Smuzhiyun 					 const struct sctp_association *asoc,
911*4882a593Smuzhiyun 					 const union sctp_subtype type,
912*4882a593Smuzhiyun 					 void *arg,
913*4882a593Smuzhiyun 					 struct sctp_cmd_seq *commands)
914*4882a593Smuzhiyun {
915*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
916*4882a593Smuzhiyun 	struct sctp_ulpevent *ev;
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
919*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	/* Verify that the chunk length for the COOKIE-ACK is OK.
922*4882a593Smuzhiyun 	 * If we don't do this, any bundled chunks may be junked.
923*4882a593Smuzhiyun 	 */
924*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
925*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
926*4882a593Smuzhiyun 						  commands);
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	/* Reset init error count upon receipt of COOKIE-ACK,
929*4882a593Smuzhiyun 	 * to avoid problems with the managemement of this
930*4882a593Smuzhiyun 	 * counter in stale cookie situations when a transition back
931*4882a593Smuzhiyun 	 * from the COOKIE-ECHOED state to the COOKIE-WAIT
932*4882a593Smuzhiyun 	 * state is performed.
933*4882a593Smuzhiyun 	 */
934*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	/* Set peer label for connection. */
937*4882a593Smuzhiyun 	security_inet_conn_established(ep->base.sk, chunk->skb);
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	/* RFC 2960 5.1 Normal Establishment of an Association
940*4882a593Smuzhiyun 	 *
941*4882a593Smuzhiyun 	 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
942*4882a593Smuzhiyun 	 * from the COOKIE-ECHOED state to the ESTABLISHED state,
943*4882a593Smuzhiyun 	 * stopping the T1-cookie timer.
944*4882a593Smuzhiyun 	 */
945*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
946*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
947*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
948*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_ESTABLISHED));
949*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
950*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
951*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
952*4882a593Smuzhiyun 	if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
953*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
954*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 	/* It may also notify its ULP about the successful
957*4882a593Smuzhiyun 	 * establishment of the association with a Communication Up
958*4882a593Smuzhiyun 	 * notification (see Section 10).
959*4882a593Smuzhiyun 	 */
960*4882a593Smuzhiyun 	ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
961*4882a593Smuzhiyun 					     0, asoc->c.sinit_num_ostreams,
962*4882a593Smuzhiyun 					     asoc->c.sinit_max_instreams,
963*4882a593Smuzhiyun 					     NULL, GFP_ATOMIC);
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	if (!ev)
966*4882a593Smuzhiyun 		goto nomem;
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	/* Sockets API Draft Section 5.3.1.6
971*4882a593Smuzhiyun 	 * When a peer sends a Adaptation Layer Indication parameter , SCTP
972*4882a593Smuzhiyun 	 * delivers this notification to inform the application that of the
973*4882a593Smuzhiyun 	 * peers requested adaptation layer.
974*4882a593Smuzhiyun 	 */
975*4882a593Smuzhiyun 	if (asoc->peer.adaptation_ind) {
976*4882a593Smuzhiyun 		ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
977*4882a593Smuzhiyun 		if (!ev)
978*4882a593Smuzhiyun 			goto nomem;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
981*4882a593Smuzhiyun 				SCTP_ULPEVENT(ev));
982*4882a593Smuzhiyun 	}
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	if (!asoc->peer.auth_capable) {
985*4882a593Smuzhiyun 		ev = sctp_ulpevent_make_authkey(asoc, 0, SCTP_AUTH_NO_AUTH,
986*4882a593Smuzhiyun 						GFP_ATOMIC);
987*4882a593Smuzhiyun 		if (!ev)
988*4882a593Smuzhiyun 			goto nomem;
989*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
990*4882a593Smuzhiyun 				SCTP_ULPEVENT(ev));
991*4882a593Smuzhiyun 	}
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
994*4882a593Smuzhiyun nomem:
995*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun /* Generate and sendout a heartbeat packet.  */
sctp_sf_heartbeat(const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)999*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_heartbeat(
1000*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
1001*4882a593Smuzhiyun 					const struct sctp_association *asoc,
1002*4882a593Smuzhiyun 					const union sctp_subtype type,
1003*4882a593Smuzhiyun 					void *arg,
1004*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
1005*4882a593Smuzhiyun {
1006*4882a593Smuzhiyun 	struct sctp_transport *transport = (struct sctp_transport *) arg;
1007*4882a593Smuzhiyun 	struct sctp_chunk *reply;
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	/* Send a heartbeat to our peer.  */
1010*4882a593Smuzhiyun 	reply = sctp_make_heartbeat(asoc, transport);
1011*4882a593Smuzhiyun 	if (!reply)
1012*4882a593Smuzhiyun 		return SCTP_DISPOSITION_NOMEM;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	/* Set rto_pending indicating that an RTT measurement
1015*4882a593Smuzhiyun 	 * is started with this heartbeat chunk.
1016*4882a593Smuzhiyun 	 */
1017*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
1018*4882a593Smuzhiyun 			SCTP_TRANSPORT(transport));
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1021*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun /* Generate a HEARTBEAT packet on the given transport.  */
sctp_sf_sendbeat_8_3(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)1025*4882a593Smuzhiyun enum sctp_disposition sctp_sf_sendbeat_8_3(struct net *net,
1026*4882a593Smuzhiyun 					   const struct sctp_endpoint *ep,
1027*4882a593Smuzhiyun 					   const struct sctp_association *asoc,
1028*4882a593Smuzhiyun 					   const union sctp_subtype type,
1029*4882a593Smuzhiyun 					   void *arg,
1030*4882a593Smuzhiyun 					   struct sctp_cmd_seq *commands)
1031*4882a593Smuzhiyun {
1032*4882a593Smuzhiyun 	struct sctp_transport *transport = (struct sctp_transport *) arg;
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 	if (asoc->overall_error_count >= asoc->max_retrans) {
1035*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1036*4882a593Smuzhiyun 				SCTP_ERROR(ETIMEDOUT));
1037*4882a593Smuzhiyun 		/* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1038*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1039*4882a593Smuzhiyun 				SCTP_PERR(SCTP_ERROR_NO_ERROR));
1040*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1041*4882a593Smuzhiyun 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1042*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DELETE_TCB;
1043*4882a593Smuzhiyun 	}
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 	/* Section 3.3.5.
1046*4882a593Smuzhiyun 	 * The Sender-specific Heartbeat Info field should normally include
1047*4882a593Smuzhiyun 	 * information about the sender's current time when this HEARTBEAT
1048*4882a593Smuzhiyun 	 * chunk is sent and the destination transport address to which this
1049*4882a593Smuzhiyun 	 * HEARTBEAT is sent (see Section 8.3).
1050*4882a593Smuzhiyun 	 */
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	if (transport->param_flags & SPP_HB_ENABLE) {
1053*4882a593Smuzhiyun 		if (SCTP_DISPOSITION_NOMEM ==
1054*4882a593Smuzhiyun 				sctp_sf_heartbeat(ep, asoc, type, arg,
1055*4882a593Smuzhiyun 						  commands))
1056*4882a593Smuzhiyun 			return SCTP_DISPOSITION_NOMEM;
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 		/* Set transport error counter and association error counter
1059*4882a593Smuzhiyun 		 * when sending heartbeat.
1060*4882a593Smuzhiyun 		 */
1061*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1062*4882a593Smuzhiyun 				SCTP_TRANSPORT(transport));
1063*4882a593Smuzhiyun 	}
1064*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1065*4882a593Smuzhiyun 			SCTP_TRANSPORT(transport));
1066*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1067*4882a593Smuzhiyun 			SCTP_TRANSPORT(transport));
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
1070*4882a593Smuzhiyun }
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun /* resend asoc strreset_chunk.  */
sctp_sf_send_reconf(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)1073*4882a593Smuzhiyun enum sctp_disposition sctp_sf_send_reconf(struct net *net,
1074*4882a593Smuzhiyun 					  const struct sctp_endpoint *ep,
1075*4882a593Smuzhiyun 					  const struct sctp_association *asoc,
1076*4882a593Smuzhiyun 					  const union sctp_subtype type,
1077*4882a593Smuzhiyun 					  void *arg,
1078*4882a593Smuzhiyun 					  struct sctp_cmd_seq *commands)
1079*4882a593Smuzhiyun {
1080*4882a593Smuzhiyun 	struct sctp_transport *transport = arg;
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 	if (asoc->overall_error_count >= asoc->max_retrans) {
1083*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1084*4882a593Smuzhiyun 				SCTP_ERROR(ETIMEDOUT));
1085*4882a593Smuzhiyun 		/* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1086*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1087*4882a593Smuzhiyun 				SCTP_PERR(SCTP_ERROR_NO_ERROR));
1088*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1089*4882a593Smuzhiyun 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1090*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DELETE_TCB;
1091*4882a593Smuzhiyun 	}
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	sctp_chunk_hold(asoc->strreset_chunk);
1094*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1095*4882a593Smuzhiyun 			SCTP_CHUNK(asoc->strreset_chunk));
1096*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
1099*4882a593Smuzhiyun }
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun /*
1102*4882a593Smuzhiyun  * Process an heartbeat request.
1103*4882a593Smuzhiyun  *
1104*4882a593Smuzhiyun  * Section: 8.3 Path Heartbeat
1105*4882a593Smuzhiyun  * The receiver of the HEARTBEAT should immediately respond with a
1106*4882a593Smuzhiyun  * HEARTBEAT ACK that contains the Heartbeat Information field copied
1107*4882a593Smuzhiyun  * from the received HEARTBEAT chunk.
1108*4882a593Smuzhiyun  *
1109*4882a593Smuzhiyun  * Verification Tag:  8.5 Verification Tag [Normal verification]
1110*4882a593Smuzhiyun  * When receiving an SCTP packet, the endpoint MUST ensure that the
1111*4882a593Smuzhiyun  * value in the Verification Tag field of the received SCTP packet
1112*4882a593Smuzhiyun  * matches its own Tag. If the received Verification Tag value does not
1113*4882a593Smuzhiyun  * match the receiver's own tag value, the receiver shall silently
1114*4882a593Smuzhiyun  * discard the packet and shall not process it any further except for
1115*4882a593Smuzhiyun  * those cases listed in Section 8.5.1 below.
1116*4882a593Smuzhiyun  *
1117*4882a593Smuzhiyun  * Inputs
1118*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
1119*4882a593Smuzhiyun  *
1120*4882a593Smuzhiyun  * Outputs
1121*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
1122*4882a593Smuzhiyun  *
1123*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
1124*4882a593Smuzhiyun  */
sctp_sf_beat_8_3(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)1125*4882a593Smuzhiyun enum sctp_disposition sctp_sf_beat_8_3(struct net *net,
1126*4882a593Smuzhiyun 				       const struct sctp_endpoint *ep,
1127*4882a593Smuzhiyun 				       const struct sctp_association *asoc,
1128*4882a593Smuzhiyun 				       const union sctp_subtype type,
1129*4882a593Smuzhiyun 				       void *arg, struct sctp_cmd_seq *commands)
1130*4882a593Smuzhiyun {
1131*4882a593Smuzhiyun 	struct sctp_paramhdr *param_hdr;
1132*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
1133*4882a593Smuzhiyun 	struct sctp_chunk *reply;
1134*4882a593Smuzhiyun 	size_t paylen = 0;
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
1137*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun 	/* Make sure that the HEARTBEAT chunk has a valid length. */
1140*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk,
1141*4882a593Smuzhiyun 				     sizeof(struct sctp_heartbeat_chunk)))
1142*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1143*4882a593Smuzhiyun 						  commands);
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	/* 8.3 The receiver of the HEARTBEAT should immediately
1146*4882a593Smuzhiyun 	 * respond with a HEARTBEAT ACK that contains the Heartbeat
1147*4882a593Smuzhiyun 	 * Information field copied from the received HEARTBEAT chunk.
1148*4882a593Smuzhiyun 	 */
1149*4882a593Smuzhiyun 	chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
1150*4882a593Smuzhiyun 	param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
1151*4882a593Smuzhiyun 	paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 	if (ntohs(param_hdr->length) > paylen)
1154*4882a593Smuzhiyun 		return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1155*4882a593Smuzhiyun 						  param_hdr, commands);
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	if (!pskb_pull(chunk->skb, paylen))
1158*4882a593Smuzhiyun 		goto nomem;
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun 	reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1161*4882a593Smuzhiyun 	if (!reply)
1162*4882a593Smuzhiyun 		goto nomem;
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1165*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun nomem:
1168*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun /*
1172*4882a593Smuzhiyun  * Process the returning HEARTBEAT ACK.
1173*4882a593Smuzhiyun  *
1174*4882a593Smuzhiyun  * Section: 8.3 Path Heartbeat
1175*4882a593Smuzhiyun  * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1176*4882a593Smuzhiyun  * should clear the error counter of the destination transport
1177*4882a593Smuzhiyun  * address to which the HEARTBEAT was sent, and mark the destination
1178*4882a593Smuzhiyun  * transport address as active if it is not so marked. The endpoint may
1179*4882a593Smuzhiyun  * optionally report to the upper layer when an inactive destination
1180*4882a593Smuzhiyun  * address is marked as active due to the reception of the latest
1181*4882a593Smuzhiyun  * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1182*4882a593Smuzhiyun  * clear the association overall error count as well (as defined
1183*4882a593Smuzhiyun  * in section 8.1).
1184*4882a593Smuzhiyun  *
1185*4882a593Smuzhiyun  * The receiver of the HEARTBEAT ACK should also perform an RTT
1186*4882a593Smuzhiyun  * measurement for that destination transport address using the time
1187*4882a593Smuzhiyun  * value carried in the HEARTBEAT ACK chunk.
1188*4882a593Smuzhiyun  *
1189*4882a593Smuzhiyun  * Verification Tag:  8.5 Verification Tag [Normal verification]
1190*4882a593Smuzhiyun  *
1191*4882a593Smuzhiyun  * Inputs
1192*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
1193*4882a593Smuzhiyun  *
1194*4882a593Smuzhiyun  * Outputs
1195*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
1196*4882a593Smuzhiyun  *
1197*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
1198*4882a593Smuzhiyun  */
sctp_sf_backbeat_8_3(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)1199*4882a593Smuzhiyun enum sctp_disposition sctp_sf_backbeat_8_3(struct net *net,
1200*4882a593Smuzhiyun 					   const struct sctp_endpoint *ep,
1201*4882a593Smuzhiyun 					   const struct sctp_association *asoc,
1202*4882a593Smuzhiyun 					   const union sctp_subtype type,
1203*4882a593Smuzhiyun 					   void *arg,
1204*4882a593Smuzhiyun 					   struct sctp_cmd_seq *commands)
1205*4882a593Smuzhiyun {
1206*4882a593Smuzhiyun 	struct sctp_sender_hb_info *hbinfo;
1207*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
1208*4882a593Smuzhiyun 	struct sctp_transport *link;
1209*4882a593Smuzhiyun 	unsigned long max_interval;
1210*4882a593Smuzhiyun 	union sctp_addr from_addr;
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
1213*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1214*4882a593Smuzhiyun 
1215*4882a593Smuzhiyun 	/* Make sure that the HEARTBEAT-ACK chunk has a valid length.  */
1216*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
1217*4882a593Smuzhiyun 					    sizeof(*hbinfo)))
1218*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1219*4882a593Smuzhiyun 						  commands);
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun 	hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data;
1222*4882a593Smuzhiyun 	/* Make sure that the length of the parameter is what we expect */
1223*4882a593Smuzhiyun 	if (ntohs(hbinfo->param_hdr.length) != sizeof(*hbinfo))
1224*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 	from_addr = hbinfo->daddr;
1227*4882a593Smuzhiyun 	link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 	/* This should never happen, but lets log it if so.  */
1230*4882a593Smuzhiyun 	if (unlikely(!link)) {
1231*4882a593Smuzhiyun 		if (from_addr.sa.sa_family == AF_INET6) {
1232*4882a593Smuzhiyun 			net_warn_ratelimited("%s association %p could not find address %pI6\n",
1233*4882a593Smuzhiyun 					     __func__,
1234*4882a593Smuzhiyun 					     asoc,
1235*4882a593Smuzhiyun 					     &from_addr.v6.sin6_addr);
1236*4882a593Smuzhiyun 		} else {
1237*4882a593Smuzhiyun 			net_warn_ratelimited("%s association %p could not find address %pI4\n",
1238*4882a593Smuzhiyun 					     __func__,
1239*4882a593Smuzhiyun 					     asoc,
1240*4882a593Smuzhiyun 					     &from_addr.v4.sin_addr.s_addr);
1241*4882a593Smuzhiyun 		}
1242*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
1243*4882a593Smuzhiyun 	}
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	/* Validate the 64-bit random nonce. */
1246*4882a593Smuzhiyun 	if (hbinfo->hb_nonce != link->hb_nonce)
1247*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 	max_interval = link->hbinterval + link->rto;
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	/* Check if the timestamp looks valid.  */
1252*4882a593Smuzhiyun 	if (time_after(hbinfo->sent_at, jiffies) ||
1253*4882a593Smuzhiyun 	    time_after(jiffies, hbinfo->sent_at + max_interval)) {
1254*4882a593Smuzhiyun 		pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1255*4882a593Smuzhiyun 			 "for transport:%p\n", __func__, link);
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
1258*4882a593Smuzhiyun 	}
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 	/* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1261*4882a593Smuzhiyun 	 * the HEARTBEAT should clear the error counter of the
1262*4882a593Smuzhiyun 	 * destination transport address to which the HEARTBEAT was
1263*4882a593Smuzhiyun 	 * sent and mark the destination transport address as active if
1264*4882a593Smuzhiyun 	 * it is not so marked.
1265*4882a593Smuzhiyun 	 */
1266*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
1269*4882a593Smuzhiyun }
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun /* Helper function to send out an abort for the restart
1272*4882a593Smuzhiyun  * condition.
1273*4882a593Smuzhiyun  */
sctp_sf_send_restart_abort(struct net * net,union sctp_addr * ssa,struct sctp_chunk * init,struct sctp_cmd_seq * commands)1274*4882a593Smuzhiyun static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1275*4882a593Smuzhiyun 				      struct sctp_chunk *init,
1276*4882a593Smuzhiyun 				      struct sctp_cmd_seq *commands)
1277*4882a593Smuzhiyun {
1278*4882a593Smuzhiyun 	struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1279*4882a593Smuzhiyun 	union sctp_addr_param *addrparm;
1280*4882a593Smuzhiyun 	struct sctp_errhdr *errhdr;
1281*4882a593Smuzhiyun 	char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
1282*4882a593Smuzhiyun 	struct sctp_endpoint *ep;
1283*4882a593Smuzhiyun 	struct sctp_packet *pkt;
1284*4882a593Smuzhiyun 	int len;
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	/* Build the error on the stack.   We are way to malloc crazy
1287*4882a593Smuzhiyun 	 * throughout the code today.
1288*4882a593Smuzhiyun 	 */
1289*4882a593Smuzhiyun 	errhdr = (struct sctp_errhdr *)buffer;
1290*4882a593Smuzhiyun 	addrparm = (union sctp_addr_param *)errhdr->variable;
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 	/* Copy into a parm format. */
1293*4882a593Smuzhiyun 	len = af->to_addr_param(ssa, addrparm);
1294*4882a593Smuzhiyun 	len += sizeof(*errhdr);
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun 	errhdr->cause = SCTP_ERROR_RESTART;
1297*4882a593Smuzhiyun 	errhdr->length = htons(len);
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun 	/* Assign to the control socket. */
1300*4882a593Smuzhiyun 	ep = sctp_sk(net->sctp.ctl_sock)->ep;
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 	/* Association is NULL since this may be a restart attack and we
1303*4882a593Smuzhiyun 	 * want to send back the attacker's vtag.
1304*4882a593Smuzhiyun 	 */
1305*4882a593Smuzhiyun 	pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 	if (!pkt)
1308*4882a593Smuzhiyun 		goto out;
1309*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun 	/* Discard the rest of the inbound packet. */
1314*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun out:
1317*4882a593Smuzhiyun 	/* Even if there is no memory, treat as a failure so
1318*4882a593Smuzhiyun 	 * the packet will get dropped.
1319*4882a593Smuzhiyun 	 */
1320*4882a593Smuzhiyun 	return 0;
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun 
list_has_sctp_addr(const struct list_head * list,union sctp_addr * ipaddr)1323*4882a593Smuzhiyun static bool list_has_sctp_addr(const struct list_head *list,
1324*4882a593Smuzhiyun 			       union sctp_addr *ipaddr)
1325*4882a593Smuzhiyun {
1326*4882a593Smuzhiyun 	struct sctp_transport *addr;
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 	list_for_each_entry(addr, list, transports) {
1329*4882a593Smuzhiyun 		if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1330*4882a593Smuzhiyun 			return true;
1331*4882a593Smuzhiyun 	}
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	return false;
1334*4882a593Smuzhiyun }
1335*4882a593Smuzhiyun /* A restart is occurring, check to make sure no new addresses
1336*4882a593Smuzhiyun  * are being added as we may be under a takeover attack.
1337*4882a593Smuzhiyun  */
sctp_sf_check_restart_addrs(const struct sctp_association * new_asoc,const struct sctp_association * asoc,struct sctp_chunk * init,struct sctp_cmd_seq * commands)1338*4882a593Smuzhiyun static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1339*4882a593Smuzhiyun 				       const struct sctp_association *asoc,
1340*4882a593Smuzhiyun 				       struct sctp_chunk *init,
1341*4882a593Smuzhiyun 				       struct sctp_cmd_seq *commands)
1342*4882a593Smuzhiyun {
1343*4882a593Smuzhiyun 	struct net *net = new_asoc->base.net;
1344*4882a593Smuzhiyun 	struct sctp_transport *new_addr;
1345*4882a593Smuzhiyun 	int ret = 1;
1346*4882a593Smuzhiyun 
1347*4882a593Smuzhiyun 	/* Implementor's Guide - Section 5.2.2
1348*4882a593Smuzhiyun 	 * ...
1349*4882a593Smuzhiyun 	 * Before responding the endpoint MUST check to see if the
1350*4882a593Smuzhiyun 	 * unexpected INIT adds new addresses to the association. If new
1351*4882a593Smuzhiyun 	 * addresses are added to the association, the endpoint MUST respond
1352*4882a593Smuzhiyun 	 * with an ABORT..
1353*4882a593Smuzhiyun 	 */
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 	/* Search through all current addresses and make sure
1356*4882a593Smuzhiyun 	 * we aren't adding any new ones.
1357*4882a593Smuzhiyun 	 */
1358*4882a593Smuzhiyun 	list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1359*4882a593Smuzhiyun 			    transports) {
1360*4882a593Smuzhiyun 		if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1361*4882a593Smuzhiyun 					&new_addr->ipaddr)) {
1362*4882a593Smuzhiyun 			sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1363*4882a593Smuzhiyun 						   commands);
1364*4882a593Smuzhiyun 			ret = 0;
1365*4882a593Smuzhiyun 			break;
1366*4882a593Smuzhiyun 		}
1367*4882a593Smuzhiyun 	}
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	/* Return success if all addresses were found. */
1370*4882a593Smuzhiyun 	return ret;
1371*4882a593Smuzhiyun }
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun /* Populate the verification/tie tags based on overlapping INIT
1374*4882a593Smuzhiyun  * scenario.
1375*4882a593Smuzhiyun  *
1376*4882a593Smuzhiyun  * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1377*4882a593Smuzhiyun  */
sctp_tietags_populate(struct sctp_association * new_asoc,const struct sctp_association * asoc)1378*4882a593Smuzhiyun static void sctp_tietags_populate(struct sctp_association *new_asoc,
1379*4882a593Smuzhiyun 				  const struct sctp_association *asoc)
1380*4882a593Smuzhiyun {
1381*4882a593Smuzhiyun 	switch (asoc->state) {
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	/* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	case SCTP_STATE_COOKIE_WAIT:
1386*4882a593Smuzhiyun 		new_asoc->c.my_vtag     = asoc->c.my_vtag;
1387*4882a593Smuzhiyun 		new_asoc->c.my_ttag     = asoc->c.my_vtag;
1388*4882a593Smuzhiyun 		new_asoc->c.peer_ttag   = 0;
1389*4882a593Smuzhiyun 		break;
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 	case SCTP_STATE_COOKIE_ECHOED:
1392*4882a593Smuzhiyun 		new_asoc->c.my_vtag     = asoc->c.my_vtag;
1393*4882a593Smuzhiyun 		new_asoc->c.my_ttag     = asoc->c.my_vtag;
1394*4882a593Smuzhiyun 		new_asoc->c.peer_ttag   = asoc->c.peer_vtag;
1395*4882a593Smuzhiyun 		break;
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun 	/* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1398*4882a593Smuzhiyun 	 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1399*4882a593Smuzhiyun 	 */
1400*4882a593Smuzhiyun 	default:
1401*4882a593Smuzhiyun 		new_asoc->c.my_ttag   = asoc->c.my_vtag;
1402*4882a593Smuzhiyun 		new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1403*4882a593Smuzhiyun 		break;
1404*4882a593Smuzhiyun 	}
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	/* Other parameters for the endpoint SHOULD be copied from the
1407*4882a593Smuzhiyun 	 * existing parameters of the association (e.g. number of
1408*4882a593Smuzhiyun 	 * outbound streams) into the INIT ACK and cookie.
1409*4882a593Smuzhiyun 	 */
1410*4882a593Smuzhiyun 	new_asoc->rwnd                  = asoc->rwnd;
1411*4882a593Smuzhiyun 	new_asoc->c.sinit_num_ostreams  = asoc->c.sinit_num_ostreams;
1412*4882a593Smuzhiyun 	new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1413*4882a593Smuzhiyun 	new_asoc->c.initial_tsn         = asoc->c.initial_tsn;
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun /*
1417*4882a593Smuzhiyun  * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1418*4882a593Smuzhiyun  * handling action.
1419*4882a593Smuzhiyun  *
1420*4882a593Smuzhiyun  * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1421*4882a593Smuzhiyun  *
1422*4882a593Smuzhiyun  * Returns value representing action to be taken.   These action values
1423*4882a593Smuzhiyun  * correspond to Action/Description values in RFC 2960, Table 2.
1424*4882a593Smuzhiyun  */
sctp_tietags_compare(struct sctp_association * new_asoc,const struct sctp_association * asoc)1425*4882a593Smuzhiyun static char sctp_tietags_compare(struct sctp_association *new_asoc,
1426*4882a593Smuzhiyun 				 const struct sctp_association *asoc)
1427*4882a593Smuzhiyun {
1428*4882a593Smuzhiyun 	/* In this case, the peer may have restarted.  */
1429*4882a593Smuzhiyun 	if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1430*4882a593Smuzhiyun 	    (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1431*4882a593Smuzhiyun 	    (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1432*4882a593Smuzhiyun 	    (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1433*4882a593Smuzhiyun 		return 'A';
1434*4882a593Smuzhiyun 
1435*4882a593Smuzhiyun 	/* Collision case B. */
1436*4882a593Smuzhiyun 	if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1437*4882a593Smuzhiyun 	    ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1438*4882a593Smuzhiyun 	     (0 == asoc->c.peer_vtag))) {
1439*4882a593Smuzhiyun 		return 'B';
1440*4882a593Smuzhiyun 	}
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun 	/* Collision case D. */
1443*4882a593Smuzhiyun 	if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1444*4882a593Smuzhiyun 	    (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1445*4882a593Smuzhiyun 		return 'D';
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun 	/* Collision case C. */
1448*4882a593Smuzhiyun 	if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1449*4882a593Smuzhiyun 	    (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1450*4882a593Smuzhiyun 	    (0 == new_asoc->c.my_ttag) &&
1451*4882a593Smuzhiyun 	    (0 == new_asoc->c.peer_ttag))
1452*4882a593Smuzhiyun 		return 'C';
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun 	/* No match to any of the special cases; discard this packet. */
1455*4882a593Smuzhiyun 	return 'E';
1456*4882a593Smuzhiyun }
1457*4882a593Smuzhiyun 
1458*4882a593Smuzhiyun /* Common helper routine for both duplicate and simulataneous INIT
1459*4882a593Smuzhiyun  * chunk handling.
1460*4882a593Smuzhiyun  */
sctp_sf_do_unexpected_init(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)1461*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_do_unexpected_init(
1462*4882a593Smuzhiyun 					struct net *net,
1463*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
1464*4882a593Smuzhiyun 					const struct sctp_association *asoc,
1465*4882a593Smuzhiyun 					const union sctp_subtype type,
1466*4882a593Smuzhiyun 					void *arg,
1467*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
1468*4882a593Smuzhiyun {
1469*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg, *repl, *err_chunk;
1470*4882a593Smuzhiyun 	struct sctp_unrecognized_param *unk_param;
1471*4882a593Smuzhiyun 	struct sctp_association *new_asoc;
1472*4882a593Smuzhiyun 	enum sctp_disposition retval;
1473*4882a593Smuzhiyun 	struct sctp_packet *packet;
1474*4882a593Smuzhiyun 	int len;
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun 	/* Update socket peer label if first association. */
1477*4882a593Smuzhiyun 	if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
1478*4882a593Smuzhiyun 					chunk->skb))
1479*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun 	/* 6.10 Bundling
1482*4882a593Smuzhiyun 	 * An endpoint MUST NOT bundle INIT, INIT ACK or
1483*4882a593Smuzhiyun 	 * SHUTDOWN COMPLETE with any other chunks.
1484*4882a593Smuzhiyun 	 *
1485*4882a593Smuzhiyun 	 * IG Section 2.11.2
1486*4882a593Smuzhiyun 	 * Furthermore, we require that the receiver of an INIT chunk MUST
1487*4882a593Smuzhiyun 	 * enforce these rules by silently discarding an arriving packet
1488*4882a593Smuzhiyun 	 * with an INIT chunk that is bundled with other chunks.
1489*4882a593Smuzhiyun 	 */
1490*4882a593Smuzhiyun 	if (!chunk->singleton)
1491*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 	/* Make sure that the INIT chunk has a valid length. */
1494*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
1495*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun 	/* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1498*4882a593Smuzhiyun 	 * Tag.
1499*4882a593Smuzhiyun 	 */
1500*4882a593Smuzhiyun 	if (chunk->sctp_hdr->vtag != 0)
1501*4882a593Smuzhiyun 		return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	/* Grab the INIT header.  */
1504*4882a593Smuzhiyun 	chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun 	/* Tag the variable length parameters.  */
1507*4882a593Smuzhiyun 	chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
1508*4882a593Smuzhiyun 
1509*4882a593Smuzhiyun 	/* Verify the INIT chunk before processing it. */
1510*4882a593Smuzhiyun 	err_chunk = NULL;
1511*4882a593Smuzhiyun 	if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
1512*4882a593Smuzhiyun 			      (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
1513*4882a593Smuzhiyun 			      &err_chunk)) {
1514*4882a593Smuzhiyun 		/* This chunk contains fatal error. It is to be discarded.
1515*4882a593Smuzhiyun 		 * Send an ABORT, with causes if there is any.
1516*4882a593Smuzhiyun 		 */
1517*4882a593Smuzhiyun 		if (err_chunk) {
1518*4882a593Smuzhiyun 			packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1519*4882a593Smuzhiyun 					(__u8 *)(err_chunk->chunk_hdr) +
1520*4882a593Smuzhiyun 					sizeof(struct sctp_chunkhdr),
1521*4882a593Smuzhiyun 					ntohs(err_chunk->chunk_hdr->length) -
1522*4882a593Smuzhiyun 					sizeof(struct sctp_chunkhdr));
1523*4882a593Smuzhiyun 
1524*4882a593Smuzhiyun 			if (packet) {
1525*4882a593Smuzhiyun 				sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1526*4882a593Smuzhiyun 						SCTP_PACKET(packet));
1527*4882a593Smuzhiyun 				SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1528*4882a593Smuzhiyun 				retval = SCTP_DISPOSITION_CONSUME;
1529*4882a593Smuzhiyun 			} else {
1530*4882a593Smuzhiyun 				retval = SCTP_DISPOSITION_NOMEM;
1531*4882a593Smuzhiyun 			}
1532*4882a593Smuzhiyun 			goto cleanup;
1533*4882a593Smuzhiyun 		} else {
1534*4882a593Smuzhiyun 			return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1535*4882a593Smuzhiyun 						    commands);
1536*4882a593Smuzhiyun 		}
1537*4882a593Smuzhiyun 	}
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 	/*
1540*4882a593Smuzhiyun 	 * Other parameters for the endpoint SHOULD be copied from the
1541*4882a593Smuzhiyun 	 * existing parameters of the association (e.g. number of
1542*4882a593Smuzhiyun 	 * outbound streams) into the INIT ACK and cookie.
1543*4882a593Smuzhiyun 	 * FIXME:  We are copying parameters from the endpoint not the
1544*4882a593Smuzhiyun 	 * association.
1545*4882a593Smuzhiyun 	 */
1546*4882a593Smuzhiyun 	new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1547*4882a593Smuzhiyun 	if (!new_asoc)
1548*4882a593Smuzhiyun 		goto nomem;
1549*4882a593Smuzhiyun 
1550*4882a593Smuzhiyun 	if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1551*4882a593Smuzhiyun 				sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1552*4882a593Smuzhiyun 		goto nomem;
1553*4882a593Smuzhiyun 
1554*4882a593Smuzhiyun 	/* In the outbound INIT ACK the endpoint MUST copy its current
1555*4882a593Smuzhiyun 	 * Verification Tag and Peers Verification tag into a reserved
1556*4882a593Smuzhiyun 	 * place (local tie-tag and per tie-tag) within the state cookie.
1557*4882a593Smuzhiyun 	 */
1558*4882a593Smuzhiyun 	if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1559*4882a593Smuzhiyun 			       (struct sctp_init_chunk *)chunk->chunk_hdr,
1560*4882a593Smuzhiyun 			       GFP_ATOMIC))
1561*4882a593Smuzhiyun 		goto nomem;
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun 	/* Make sure no new addresses are being added during the
1564*4882a593Smuzhiyun 	 * restart.   Do not do this check for COOKIE-WAIT state,
1565*4882a593Smuzhiyun 	 * since there are no peer addresses to check against.
1566*4882a593Smuzhiyun 	 * Upon return an ABORT will have been sent if needed.
1567*4882a593Smuzhiyun 	 */
1568*4882a593Smuzhiyun 	if (!sctp_state(asoc, COOKIE_WAIT)) {
1569*4882a593Smuzhiyun 		if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1570*4882a593Smuzhiyun 						 commands)) {
1571*4882a593Smuzhiyun 			retval = SCTP_DISPOSITION_CONSUME;
1572*4882a593Smuzhiyun 			goto nomem_retval;
1573*4882a593Smuzhiyun 		}
1574*4882a593Smuzhiyun 	}
1575*4882a593Smuzhiyun 
1576*4882a593Smuzhiyun 	sctp_tietags_populate(new_asoc, asoc);
1577*4882a593Smuzhiyun 
1578*4882a593Smuzhiyun 	/* B) "Z" shall respond immediately with an INIT ACK chunk.  */
1579*4882a593Smuzhiyun 
1580*4882a593Smuzhiyun 	/* If there are errors need to be reported for unknown parameters,
1581*4882a593Smuzhiyun 	 * make sure to reserve enough room in the INIT ACK for them.
1582*4882a593Smuzhiyun 	 */
1583*4882a593Smuzhiyun 	len = 0;
1584*4882a593Smuzhiyun 	if (err_chunk) {
1585*4882a593Smuzhiyun 		len = ntohs(err_chunk->chunk_hdr->length) -
1586*4882a593Smuzhiyun 		      sizeof(struct sctp_chunkhdr);
1587*4882a593Smuzhiyun 	}
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun 	repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1590*4882a593Smuzhiyun 	if (!repl)
1591*4882a593Smuzhiyun 		goto nomem;
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun 	/* If there are errors need to be reported for unknown parameters,
1594*4882a593Smuzhiyun 	 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1595*4882a593Smuzhiyun 	 * parameter.
1596*4882a593Smuzhiyun 	 */
1597*4882a593Smuzhiyun 	if (err_chunk) {
1598*4882a593Smuzhiyun 		/* Get the "Unrecognized parameter" parameter(s) out of the
1599*4882a593Smuzhiyun 		 * ERROR chunk generated by sctp_verify_init(). Since the
1600*4882a593Smuzhiyun 		 * error cause code for "unknown parameter" and the
1601*4882a593Smuzhiyun 		 * "Unrecognized parameter" type is the same, we can
1602*4882a593Smuzhiyun 		 * construct the parameters in INIT ACK by copying the
1603*4882a593Smuzhiyun 		 * ERROR causes over.
1604*4882a593Smuzhiyun 		 */
1605*4882a593Smuzhiyun 		unk_param = (struct sctp_unrecognized_param *)
1606*4882a593Smuzhiyun 			    ((__u8 *)(err_chunk->chunk_hdr) +
1607*4882a593Smuzhiyun 			    sizeof(struct sctp_chunkhdr));
1608*4882a593Smuzhiyun 		/* Replace the cause code with the "Unrecognized parameter"
1609*4882a593Smuzhiyun 		 * parameter type.
1610*4882a593Smuzhiyun 		 */
1611*4882a593Smuzhiyun 		sctp_addto_chunk(repl, len, unk_param);
1612*4882a593Smuzhiyun 	}
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1615*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun 	/*
1618*4882a593Smuzhiyun 	 * Note: After sending out INIT ACK with the State Cookie parameter,
1619*4882a593Smuzhiyun 	 * "Z" MUST NOT allocate any resources for this new association.
1620*4882a593Smuzhiyun 	 * Otherwise, "Z" will be vulnerable to resource attacks.
1621*4882a593Smuzhiyun 	 */
1622*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1623*4882a593Smuzhiyun 	retval = SCTP_DISPOSITION_CONSUME;
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun 	return retval;
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun nomem:
1628*4882a593Smuzhiyun 	retval = SCTP_DISPOSITION_NOMEM;
1629*4882a593Smuzhiyun nomem_retval:
1630*4882a593Smuzhiyun 	if (new_asoc)
1631*4882a593Smuzhiyun 		sctp_association_free(new_asoc);
1632*4882a593Smuzhiyun cleanup:
1633*4882a593Smuzhiyun 	if (err_chunk)
1634*4882a593Smuzhiyun 		sctp_chunk_free(err_chunk);
1635*4882a593Smuzhiyun 	return retval;
1636*4882a593Smuzhiyun }
1637*4882a593Smuzhiyun 
1638*4882a593Smuzhiyun /*
1639*4882a593Smuzhiyun  * Handle simultaneous INIT.
1640*4882a593Smuzhiyun  * This means we started an INIT and then we got an INIT request from
1641*4882a593Smuzhiyun  * our peer.
1642*4882a593Smuzhiyun  *
1643*4882a593Smuzhiyun  * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1644*4882a593Smuzhiyun  * This usually indicates an initialization collision, i.e., each
1645*4882a593Smuzhiyun  * endpoint is attempting, at about the same time, to establish an
1646*4882a593Smuzhiyun  * association with the other endpoint.
1647*4882a593Smuzhiyun  *
1648*4882a593Smuzhiyun  * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1649*4882a593Smuzhiyun  * endpoint MUST respond with an INIT ACK using the same parameters it
1650*4882a593Smuzhiyun  * sent in its original INIT chunk (including its Verification Tag,
1651*4882a593Smuzhiyun  * unchanged). These original parameters are combined with those from the
1652*4882a593Smuzhiyun  * newly received INIT chunk. The endpoint shall also generate a State
1653*4882a593Smuzhiyun  * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1654*4882a593Smuzhiyun  * INIT to calculate the State Cookie.
1655*4882a593Smuzhiyun  *
1656*4882a593Smuzhiyun  * After that, the endpoint MUST NOT change its state, the T1-init
1657*4882a593Smuzhiyun  * timer shall be left running and the corresponding TCB MUST NOT be
1658*4882a593Smuzhiyun  * destroyed. The normal procedures for handling State Cookies when
1659*4882a593Smuzhiyun  * a TCB exists will resolve the duplicate INITs to a single association.
1660*4882a593Smuzhiyun  *
1661*4882a593Smuzhiyun  * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1662*4882a593Smuzhiyun  * its Tie-Tags with the Tag information of itself and its peer (see
1663*4882a593Smuzhiyun  * section 5.2.2 for a description of the Tie-Tags).
1664*4882a593Smuzhiyun  *
1665*4882a593Smuzhiyun  * Verification Tag: Not explicit, but an INIT can not have a valid
1666*4882a593Smuzhiyun  * verification tag, so we skip the check.
1667*4882a593Smuzhiyun  *
1668*4882a593Smuzhiyun  * Inputs
1669*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
1670*4882a593Smuzhiyun  *
1671*4882a593Smuzhiyun  * Outputs
1672*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
1673*4882a593Smuzhiyun  *
1674*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
1675*4882a593Smuzhiyun  */
sctp_sf_do_5_2_1_siminit(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)1676*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_5_2_1_siminit(
1677*4882a593Smuzhiyun 					struct net *net,
1678*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
1679*4882a593Smuzhiyun 					const struct sctp_association *asoc,
1680*4882a593Smuzhiyun 					const union sctp_subtype type,
1681*4882a593Smuzhiyun 					void *arg,
1682*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
1683*4882a593Smuzhiyun {
1684*4882a593Smuzhiyun 	/* Call helper to do the real work for both simulataneous and
1685*4882a593Smuzhiyun 	 * duplicate INIT chunk handling.
1686*4882a593Smuzhiyun 	 */
1687*4882a593Smuzhiyun 	return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1688*4882a593Smuzhiyun }
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun /*
1691*4882a593Smuzhiyun  * Handle duplicated INIT messages.  These are usually delayed
1692*4882a593Smuzhiyun  * restransmissions.
1693*4882a593Smuzhiyun  *
1694*4882a593Smuzhiyun  * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1695*4882a593Smuzhiyun  * COOKIE-ECHOED and COOKIE-WAIT
1696*4882a593Smuzhiyun  *
1697*4882a593Smuzhiyun  * Unless otherwise stated, upon reception of an unexpected INIT for
1698*4882a593Smuzhiyun  * this association, the endpoint shall generate an INIT ACK with a
1699*4882a593Smuzhiyun  * State Cookie.  In the outbound INIT ACK the endpoint MUST copy its
1700*4882a593Smuzhiyun  * current Verification Tag and peer's Verification Tag into a reserved
1701*4882a593Smuzhiyun  * place within the state cookie.  We shall refer to these locations as
1702*4882a593Smuzhiyun  * the Peer's-Tie-Tag and the Local-Tie-Tag.  The outbound SCTP packet
1703*4882a593Smuzhiyun  * containing this INIT ACK MUST carry a Verification Tag value equal to
1704*4882a593Smuzhiyun  * the Initiation Tag found in the unexpected INIT.  And the INIT ACK
1705*4882a593Smuzhiyun  * MUST contain a new Initiation Tag (randomly generated see Section
1706*4882a593Smuzhiyun  * 5.3.1).  Other parameters for the endpoint SHOULD be copied from the
1707*4882a593Smuzhiyun  * existing parameters of the association (e.g. number of outbound
1708*4882a593Smuzhiyun  * streams) into the INIT ACK and cookie.
1709*4882a593Smuzhiyun  *
1710*4882a593Smuzhiyun  * After sending out the INIT ACK, the endpoint shall take no further
1711*4882a593Smuzhiyun  * actions, i.e., the existing association, including its current state,
1712*4882a593Smuzhiyun  * and the corresponding TCB MUST NOT be changed.
1713*4882a593Smuzhiyun  *
1714*4882a593Smuzhiyun  * Note: Only when a TCB exists and the association is not in a COOKIE-
1715*4882a593Smuzhiyun  * WAIT state are the Tie-Tags populated.  For a normal association INIT
1716*4882a593Smuzhiyun  * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1717*4882a593Smuzhiyun  * set to 0 (indicating that no previous TCB existed).  The INIT ACK and
1718*4882a593Smuzhiyun  * State Cookie are populated as specified in section 5.2.1.
1719*4882a593Smuzhiyun  *
1720*4882a593Smuzhiyun  * Verification Tag: Not specified, but an INIT has no way of knowing
1721*4882a593Smuzhiyun  * what the verification tag could be, so we ignore it.
1722*4882a593Smuzhiyun  *
1723*4882a593Smuzhiyun  * Inputs
1724*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
1725*4882a593Smuzhiyun  *
1726*4882a593Smuzhiyun  * Outputs
1727*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
1728*4882a593Smuzhiyun  *
1729*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
1730*4882a593Smuzhiyun  */
sctp_sf_do_5_2_2_dupinit(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)1731*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_5_2_2_dupinit(
1732*4882a593Smuzhiyun 					struct net *net,
1733*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
1734*4882a593Smuzhiyun 					const struct sctp_association *asoc,
1735*4882a593Smuzhiyun 					const union sctp_subtype type,
1736*4882a593Smuzhiyun 					void *arg,
1737*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
1738*4882a593Smuzhiyun {
1739*4882a593Smuzhiyun 	/* Call helper to do the real work for both simulataneous and
1740*4882a593Smuzhiyun 	 * duplicate INIT chunk handling.
1741*4882a593Smuzhiyun 	 */
1742*4882a593Smuzhiyun 	return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1743*4882a593Smuzhiyun }
1744*4882a593Smuzhiyun 
1745*4882a593Smuzhiyun 
1746*4882a593Smuzhiyun /*
1747*4882a593Smuzhiyun  * Unexpected INIT-ACK handler.
1748*4882a593Smuzhiyun  *
1749*4882a593Smuzhiyun  * Section 5.2.3
1750*4882a593Smuzhiyun  * If an INIT ACK received by an endpoint in any state other than the
1751*4882a593Smuzhiyun  * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1752*4882a593Smuzhiyun  * An unexpected INIT ACK usually indicates the processing of an old or
1753*4882a593Smuzhiyun  * duplicated INIT chunk.
1754*4882a593Smuzhiyun */
sctp_sf_do_5_2_3_initack(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)1755*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_5_2_3_initack(
1756*4882a593Smuzhiyun 					struct net *net,
1757*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
1758*4882a593Smuzhiyun 					const struct sctp_association *asoc,
1759*4882a593Smuzhiyun 					const union sctp_subtype type,
1760*4882a593Smuzhiyun 					void *arg,
1761*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
1762*4882a593Smuzhiyun {
1763*4882a593Smuzhiyun 	/* Per the above section, we'll discard the chunk if we have an
1764*4882a593Smuzhiyun 	 * endpoint.  If this is an OOTB INIT-ACK, treat it as such.
1765*4882a593Smuzhiyun 	 */
1766*4882a593Smuzhiyun 	if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1767*4882a593Smuzhiyun 		return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1768*4882a593Smuzhiyun 	else
1769*4882a593Smuzhiyun 		return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1770*4882a593Smuzhiyun }
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1773*4882a593Smuzhiyun  *
1774*4882a593Smuzhiyun  * Section 5.2.4
1775*4882a593Smuzhiyun  *  A)  In this case, the peer may have restarted.
1776*4882a593Smuzhiyun  */
sctp_sf_do_dupcook_a(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,struct sctp_chunk * chunk,struct sctp_cmd_seq * commands,struct sctp_association * new_asoc)1777*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_do_dupcook_a(
1778*4882a593Smuzhiyun 					struct net *net,
1779*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
1780*4882a593Smuzhiyun 					const struct sctp_association *asoc,
1781*4882a593Smuzhiyun 					struct sctp_chunk *chunk,
1782*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands,
1783*4882a593Smuzhiyun 					struct sctp_association *new_asoc)
1784*4882a593Smuzhiyun {
1785*4882a593Smuzhiyun 	struct sctp_init_chunk *peer_init;
1786*4882a593Smuzhiyun 	enum sctp_disposition disposition;
1787*4882a593Smuzhiyun 	struct sctp_ulpevent *ev;
1788*4882a593Smuzhiyun 	struct sctp_chunk *repl;
1789*4882a593Smuzhiyun 	struct sctp_chunk *err;
1790*4882a593Smuzhiyun 
1791*4882a593Smuzhiyun 	/* new_asoc is a brand-new association, so these are not yet
1792*4882a593Smuzhiyun 	 * side effects--it is safe to run them here.
1793*4882a593Smuzhiyun 	 */
1794*4882a593Smuzhiyun 	peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1795*4882a593Smuzhiyun 
1796*4882a593Smuzhiyun 	if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1797*4882a593Smuzhiyun 			       GFP_ATOMIC))
1798*4882a593Smuzhiyun 		goto nomem;
1799*4882a593Smuzhiyun 
1800*4882a593Smuzhiyun 	if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
1801*4882a593Smuzhiyun 		goto nomem;
1802*4882a593Smuzhiyun 
1803*4882a593Smuzhiyun 	if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1804*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 	/* Make sure no new addresses are being added during the
1807*4882a593Smuzhiyun 	 * restart.  Though this is a pretty complicated attack
1808*4882a593Smuzhiyun 	 * since you'd have to get inside the cookie.
1809*4882a593Smuzhiyun 	 */
1810*4882a593Smuzhiyun 	if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
1811*4882a593Smuzhiyun 		return SCTP_DISPOSITION_CONSUME;
1812*4882a593Smuzhiyun 
1813*4882a593Smuzhiyun 	/* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1814*4882a593Smuzhiyun 	 * the peer has restarted (Action A), it MUST NOT setup a new
1815*4882a593Smuzhiyun 	 * association but instead resend the SHUTDOWN ACK and send an ERROR
1816*4882a593Smuzhiyun 	 * chunk with a "Cookie Received while Shutting Down" error cause to
1817*4882a593Smuzhiyun 	 * its peer.
1818*4882a593Smuzhiyun 	*/
1819*4882a593Smuzhiyun 	if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1820*4882a593Smuzhiyun 		disposition = __sctp_sf_do_9_2_reshutack(net, ep, asoc,
1821*4882a593Smuzhiyun 							 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1822*4882a593Smuzhiyun 							 chunk, commands);
1823*4882a593Smuzhiyun 		if (SCTP_DISPOSITION_NOMEM == disposition)
1824*4882a593Smuzhiyun 			goto nomem;
1825*4882a593Smuzhiyun 
1826*4882a593Smuzhiyun 		err = sctp_make_op_error(asoc, chunk,
1827*4882a593Smuzhiyun 					 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1828*4882a593Smuzhiyun 					 NULL, 0, 0);
1829*4882a593Smuzhiyun 		if (err)
1830*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1831*4882a593Smuzhiyun 					SCTP_CHUNK(err));
1832*4882a593Smuzhiyun 
1833*4882a593Smuzhiyun 		return SCTP_DISPOSITION_CONSUME;
1834*4882a593Smuzhiyun 	}
1835*4882a593Smuzhiyun 
1836*4882a593Smuzhiyun 	/* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1837*4882a593Smuzhiyun 	 * data. Consider the optional choice of resending of this data.
1838*4882a593Smuzhiyun 	 */
1839*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1840*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1841*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1842*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1843*4882a593Smuzhiyun 
1844*4882a593Smuzhiyun 	/* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1845*4882a593Smuzhiyun 	 * and ASCONF-ACK cache.
1846*4882a593Smuzhiyun 	 */
1847*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1848*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1849*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1850*4882a593Smuzhiyun 
1851*4882a593Smuzhiyun 	/* Update the content of current association. */
1852*4882a593Smuzhiyun 	if (sctp_assoc_update((struct sctp_association *)asoc, new_asoc)) {
1853*4882a593Smuzhiyun 		struct sctp_chunk *abort;
1854*4882a593Smuzhiyun 
1855*4882a593Smuzhiyun 		abort = sctp_make_abort(asoc, NULL, sizeof(struct sctp_errhdr));
1856*4882a593Smuzhiyun 		if (abort) {
1857*4882a593Smuzhiyun 			sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
1858*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
1859*4882a593Smuzhiyun 		}
1860*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNABORTED));
1861*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1862*4882a593Smuzhiyun 				SCTP_PERR(SCTP_ERROR_RSRC_LOW));
1863*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1864*4882a593Smuzhiyun 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1865*4882a593Smuzhiyun 		goto nomem;
1866*4882a593Smuzhiyun 	}
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun 	repl = sctp_make_cookie_ack(asoc, chunk);
1869*4882a593Smuzhiyun 	if (!repl)
1870*4882a593Smuzhiyun 		goto nomem;
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun 	/* Report association restart to upper layer. */
1873*4882a593Smuzhiyun 	ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1874*4882a593Smuzhiyun 					     asoc->c.sinit_num_ostreams,
1875*4882a593Smuzhiyun 					     asoc->c.sinit_max_instreams,
1876*4882a593Smuzhiyun 					     NULL, GFP_ATOMIC);
1877*4882a593Smuzhiyun 	if (!ev)
1878*4882a593Smuzhiyun 		goto nomem_ev;
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1881*4882a593Smuzhiyun 	if ((sctp_state(asoc, SHUTDOWN_PENDING) ||
1882*4882a593Smuzhiyun 	     sctp_state(asoc, SHUTDOWN_SENT)) &&
1883*4882a593Smuzhiyun 	    (sctp_sstate(asoc->base.sk, CLOSING) ||
1884*4882a593Smuzhiyun 	     sock_flag(asoc->base.sk, SOCK_DEAD))) {
1885*4882a593Smuzhiyun 		/* If the socket has been closed by user, don't
1886*4882a593Smuzhiyun 		 * transition to ESTABLISHED. Instead trigger SHUTDOWN
1887*4882a593Smuzhiyun 		 * bundled with COOKIE_ACK.
1888*4882a593Smuzhiyun 		 */
1889*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1890*4882a593Smuzhiyun 		return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1891*4882a593Smuzhiyun 						     SCTP_ST_CHUNK(0), repl,
1892*4882a593Smuzhiyun 						     commands);
1893*4882a593Smuzhiyun 	} else {
1894*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1895*4882a593Smuzhiyun 				SCTP_STATE(SCTP_STATE_ESTABLISHED));
1896*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1897*4882a593Smuzhiyun 	}
1898*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
1899*4882a593Smuzhiyun 
1900*4882a593Smuzhiyun nomem_ev:
1901*4882a593Smuzhiyun 	sctp_chunk_free(repl);
1902*4882a593Smuzhiyun nomem:
1903*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
1904*4882a593Smuzhiyun }
1905*4882a593Smuzhiyun 
1906*4882a593Smuzhiyun /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1907*4882a593Smuzhiyun  *
1908*4882a593Smuzhiyun  * Section 5.2.4
1909*4882a593Smuzhiyun  *   B) In this case, both sides may be attempting to start an association
1910*4882a593Smuzhiyun  *      at about the same time but the peer endpoint started its INIT
1911*4882a593Smuzhiyun  *      after responding to the local endpoint's INIT
1912*4882a593Smuzhiyun  */
1913*4882a593Smuzhiyun /* This case represents an initialization collision.  */
sctp_sf_do_dupcook_b(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,struct sctp_chunk * chunk,struct sctp_cmd_seq * commands,struct sctp_association * new_asoc)1914*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_do_dupcook_b(
1915*4882a593Smuzhiyun 					struct net *net,
1916*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
1917*4882a593Smuzhiyun 					const struct sctp_association *asoc,
1918*4882a593Smuzhiyun 					struct sctp_chunk *chunk,
1919*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands,
1920*4882a593Smuzhiyun 					struct sctp_association *new_asoc)
1921*4882a593Smuzhiyun {
1922*4882a593Smuzhiyun 	struct sctp_init_chunk *peer_init;
1923*4882a593Smuzhiyun 	struct sctp_chunk *repl;
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun 	/* new_asoc is a brand-new association, so these are not yet
1926*4882a593Smuzhiyun 	 * side effects--it is safe to run them here.
1927*4882a593Smuzhiyun 	 */
1928*4882a593Smuzhiyun 	peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1929*4882a593Smuzhiyun 	if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1930*4882a593Smuzhiyun 			       GFP_ATOMIC))
1931*4882a593Smuzhiyun 		goto nomem;
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun 	if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
1934*4882a593Smuzhiyun 		goto nomem;
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1937*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	/* Update the content of current association.  */
1940*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1941*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1942*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_ESTABLISHED));
1943*4882a593Smuzhiyun 	if (asoc->state < SCTP_STATE_ESTABLISHED)
1944*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1945*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1946*4882a593Smuzhiyun 
1947*4882a593Smuzhiyun 	repl = sctp_make_cookie_ack(new_asoc, chunk);
1948*4882a593Smuzhiyun 	if (!repl)
1949*4882a593Smuzhiyun 		goto nomem;
1950*4882a593Smuzhiyun 
1951*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun 	/* RFC 2960 5.1 Normal Establishment of an Association
1954*4882a593Smuzhiyun 	 *
1955*4882a593Smuzhiyun 	 * D) IMPLEMENTATION NOTE: An implementation may choose to
1956*4882a593Smuzhiyun 	 * send the Communication Up notification to the SCTP user
1957*4882a593Smuzhiyun 	 * upon reception of a valid COOKIE ECHO chunk.
1958*4882a593Smuzhiyun 	 *
1959*4882a593Smuzhiyun 	 * Sadly, this needs to be implemented as a side-effect, because
1960*4882a593Smuzhiyun 	 * we are not guaranteed to have set the association id of the real
1961*4882a593Smuzhiyun 	 * association and so these notifications need to be delayed until
1962*4882a593Smuzhiyun 	 * the association id is allocated.
1963*4882a593Smuzhiyun 	 */
1964*4882a593Smuzhiyun 
1965*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun 	/* Sockets API Draft Section 5.3.1.6
1968*4882a593Smuzhiyun 	 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1969*4882a593Smuzhiyun 	 * delivers this notification to inform the application that of the
1970*4882a593Smuzhiyun 	 * peers requested adaptation layer.
1971*4882a593Smuzhiyun 	 *
1972*4882a593Smuzhiyun 	 * This also needs to be done as a side effect for the same reason as
1973*4882a593Smuzhiyun 	 * above.
1974*4882a593Smuzhiyun 	 */
1975*4882a593Smuzhiyun 	if (asoc->peer.adaptation_ind)
1976*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1977*4882a593Smuzhiyun 
1978*4882a593Smuzhiyun 	if (!asoc->peer.auth_capable)
1979*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_PEER_NO_AUTH, SCTP_NULL());
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun nomem:
1984*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
1985*4882a593Smuzhiyun }
1986*4882a593Smuzhiyun 
1987*4882a593Smuzhiyun /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1988*4882a593Smuzhiyun  *
1989*4882a593Smuzhiyun  * Section 5.2.4
1990*4882a593Smuzhiyun  *  C) In this case, the local endpoint's cookie has arrived late.
1991*4882a593Smuzhiyun  *     Before it arrived, the local endpoint sent an INIT and received an
1992*4882a593Smuzhiyun  *     INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1993*4882a593Smuzhiyun  *     but a new tag of its own.
1994*4882a593Smuzhiyun  */
1995*4882a593Smuzhiyun /* This case represents an initialization collision.  */
sctp_sf_do_dupcook_c(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,struct sctp_chunk * chunk,struct sctp_cmd_seq * commands,struct sctp_association * new_asoc)1996*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_do_dupcook_c(
1997*4882a593Smuzhiyun 					struct net *net,
1998*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
1999*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2000*4882a593Smuzhiyun 					struct sctp_chunk *chunk,
2001*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands,
2002*4882a593Smuzhiyun 					struct sctp_association *new_asoc)
2003*4882a593Smuzhiyun {
2004*4882a593Smuzhiyun 	/* The cookie should be silently discarded.
2005*4882a593Smuzhiyun 	 * The endpoint SHOULD NOT change states and should leave
2006*4882a593Smuzhiyun 	 * any timers running.
2007*4882a593Smuzhiyun 	 */
2008*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DISCARD;
2009*4882a593Smuzhiyun }
2010*4882a593Smuzhiyun 
2011*4882a593Smuzhiyun /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
2012*4882a593Smuzhiyun  *
2013*4882a593Smuzhiyun  * Section 5.2.4
2014*4882a593Smuzhiyun  *
2015*4882a593Smuzhiyun  * D) When both local and remote tags match the endpoint should always
2016*4882a593Smuzhiyun  *    enter the ESTABLISHED state, if it has not already done so.
2017*4882a593Smuzhiyun  */
2018*4882a593Smuzhiyun /* This case represents an initialization collision.  */
sctp_sf_do_dupcook_d(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,struct sctp_chunk * chunk,struct sctp_cmd_seq * commands,struct sctp_association * new_asoc)2019*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_do_dupcook_d(
2020*4882a593Smuzhiyun 					struct net *net,
2021*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2022*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2023*4882a593Smuzhiyun 					struct sctp_chunk *chunk,
2024*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands,
2025*4882a593Smuzhiyun 					struct sctp_association *new_asoc)
2026*4882a593Smuzhiyun {
2027*4882a593Smuzhiyun 	struct sctp_ulpevent *ev = NULL, *ai_ev = NULL, *auth_ev = NULL;
2028*4882a593Smuzhiyun 	struct sctp_chunk *repl;
2029*4882a593Smuzhiyun 
2030*4882a593Smuzhiyun 	/* Clarification from Implementor's Guide:
2031*4882a593Smuzhiyun 	 * D) When both local and remote tags match the endpoint should
2032*4882a593Smuzhiyun 	 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
2033*4882a593Smuzhiyun 	 * It should stop any cookie timer that may be running and send
2034*4882a593Smuzhiyun 	 * a COOKIE ACK.
2035*4882a593Smuzhiyun 	 */
2036*4882a593Smuzhiyun 
2037*4882a593Smuzhiyun 	if (!sctp_auth_chunk_verify(net, chunk, asoc))
2038*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun 	/* Don't accidentally move back into established state. */
2041*4882a593Smuzhiyun 	if (asoc->state < SCTP_STATE_ESTABLISHED) {
2042*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2043*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2044*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2045*4882a593Smuzhiyun 				SCTP_STATE(SCTP_STATE_ESTABLISHED));
2046*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
2047*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
2048*4882a593Smuzhiyun 				SCTP_NULL());
2049*4882a593Smuzhiyun 
2050*4882a593Smuzhiyun 		/* RFC 2960 5.1 Normal Establishment of an Association
2051*4882a593Smuzhiyun 		 *
2052*4882a593Smuzhiyun 		 * D) IMPLEMENTATION NOTE: An implementation may choose
2053*4882a593Smuzhiyun 		 * to send the Communication Up notification to the
2054*4882a593Smuzhiyun 		 * SCTP user upon reception of a valid COOKIE
2055*4882a593Smuzhiyun 		 * ECHO chunk.
2056*4882a593Smuzhiyun 		 */
2057*4882a593Smuzhiyun 		ev = sctp_ulpevent_make_assoc_change(asoc, 0,
2058*4882a593Smuzhiyun 					     SCTP_COMM_UP, 0,
2059*4882a593Smuzhiyun 					     asoc->c.sinit_num_ostreams,
2060*4882a593Smuzhiyun 					     asoc->c.sinit_max_instreams,
2061*4882a593Smuzhiyun 					     NULL, GFP_ATOMIC);
2062*4882a593Smuzhiyun 		if (!ev)
2063*4882a593Smuzhiyun 			goto nomem;
2064*4882a593Smuzhiyun 
2065*4882a593Smuzhiyun 		/* Sockets API Draft Section 5.3.1.6
2066*4882a593Smuzhiyun 		 * When a peer sends a Adaptation Layer Indication parameter,
2067*4882a593Smuzhiyun 		 * SCTP delivers this notification to inform the application
2068*4882a593Smuzhiyun 		 * that of the peers requested adaptation layer.
2069*4882a593Smuzhiyun 		 */
2070*4882a593Smuzhiyun 		if (asoc->peer.adaptation_ind) {
2071*4882a593Smuzhiyun 			ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
2072*4882a593Smuzhiyun 								 GFP_ATOMIC);
2073*4882a593Smuzhiyun 			if (!ai_ev)
2074*4882a593Smuzhiyun 				goto nomem;
2075*4882a593Smuzhiyun 
2076*4882a593Smuzhiyun 		}
2077*4882a593Smuzhiyun 
2078*4882a593Smuzhiyun 		if (!asoc->peer.auth_capable) {
2079*4882a593Smuzhiyun 			auth_ev = sctp_ulpevent_make_authkey(asoc, 0,
2080*4882a593Smuzhiyun 							     SCTP_AUTH_NO_AUTH,
2081*4882a593Smuzhiyun 							     GFP_ATOMIC);
2082*4882a593Smuzhiyun 			if (!auth_ev)
2083*4882a593Smuzhiyun 				goto nomem;
2084*4882a593Smuzhiyun 		}
2085*4882a593Smuzhiyun 	}
2086*4882a593Smuzhiyun 
2087*4882a593Smuzhiyun 	repl = sctp_make_cookie_ack(asoc, chunk);
2088*4882a593Smuzhiyun 	if (!repl)
2089*4882a593Smuzhiyun 		goto nomem;
2090*4882a593Smuzhiyun 
2091*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
2092*4882a593Smuzhiyun 
2093*4882a593Smuzhiyun 	if (ev)
2094*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2095*4882a593Smuzhiyun 				SCTP_ULPEVENT(ev));
2096*4882a593Smuzhiyun 	if (ai_ev)
2097*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2098*4882a593Smuzhiyun 					SCTP_ULPEVENT(ai_ev));
2099*4882a593Smuzhiyun 	if (auth_ev)
2100*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2101*4882a593Smuzhiyun 				SCTP_ULPEVENT(auth_ev));
2102*4882a593Smuzhiyun 
2103*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
2104*4882a593Smuzhiyun 
2105*4882a593Smuzhiyun nomem:
2106*4882a593Smuzhiyun 	if (auth_ev)
2107*4882a593Smuzhiyun 		sctp_ulpevent_free(auth_ev);
2108*4882a593Smuzhiyun 	if (ai_ev)
2109*4882a593Smuzhiyun 		sctp_ulpevent_free(ai_ev);
2110*4882a593Smuzhiyun 	if (ev)
2111*4882a593Smuzhiyun 		sctp_ulpevent_free(ev);
2112*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
2113*4882a593Smuzhiyun }
2114*4882a593Smuzhiyun 
2115*4882a593Smuzhiyun /*
2116*4882a593Smuzhiyun  * Handle a duplicate COOKIE-ECHO.  This usually means a cookie-carrying
2117*4882a593Smuzhiyun  * chunk was retransmitted and then delayed in the network.
2118*4882a593Smuzhiyun  *
2119*4882a593Smuzhiyun  * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2120*4882a593Smuzhiyun  *
2121*4882a593Smuzhiyun  * Verification Tag: None.  Do cookie validation.
2122*4882a593Smuzhiyun  *
2123*4882a593Smuzhiyun  * Inputs
2124*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
2125*4882a593Smuzhiyun  *
2126*4882a593Smuzhiyun  * Outputs
2127*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
2128*4882a593Smuzhiyun  *
2129*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
2130*4882a593Smuzhiyun  */
sctp_sf_do_5_2_4_dupcook(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2131*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
2132*4882a593Smuzhiyun 					struct net *net,
2133*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2134*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2135*4882a593Smuzhiyun 					const union sctp_subtype type,
2136*4882a593Smuzhiyun 					void *arg,
2137*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2138*4882a593Smuzhiyun {
2139*4882a593Smuzhiyun 	struct sctp_association *new_asoc;
2140*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
2141*4882a593Smuzhiyun 	enum sctp_disposition retval;
2142*4882a593Smuzhiyun 	struct sctp_chunk *err_chk_p;
2143*4882a593Smuzhiyun 	int error = 0;
2144*4882a593Smuzhiyun 	char action;
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun 	/* Make sure that the chunk has a valid length from the protocol
2147*4882a593Smuzhiyun 	 * perspective.  In this case check to make sure we have at least
2148*4882a593Smuzhiyun 	 * enough for the chunk header.  Cookie length verification is
2149*4882a593Smuzhiyun 	 * done later.
2150*4882a593Smuzhiyun 	 */
2151*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr))) {
2152*4882a593Smuzhiyun 		if (!sctp_vtag_verify(chunk, asoc))
2153*4882a593Smuzhiyun 			asoc = NULL;
2154*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg, commands);
2155*4882a593Smuzhiyun 	}
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun 	/* "Decode" the chunk.  We have no optional parameters so we
2158*4882a593Smuzhiyun 	 * are in good shape.
2159*4882a593Smuzhiyun 	 */
2160*4882a593Smuzhiyun 	chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2161*4882a593Smuzhiyun 	if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2162*4882a593Smuzhiyun 					sizeof(struct sctp_chunkhdr)))
2163*4882a593Smuzhiyun 		goto nomem;
2164*4882a593Smuzhiyun 
2165*4882a593Smuzhiyun 	/* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2166*4882a593Smuzhiyun 	 * of a duplicate COOKIE ECHO match the Verification Tags of the
2167*4882a593Smuzhiyun 	 * current association, consider the State Cookie valid even if
2168*4882a593Smuzhiyun 	 * the lifespan is exceeded.
2169*4882a593Smuzhiyun 	 */
2170*4882a593Smuzhiyun 	new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2171*4882a593Smuzhiyun 				      &err_chk_p);
2172*4882a593Smuzhiyun 
2173*4882a593Smuzhiyun 	/* FIXME:
2174*4882a593Smuzhiyun 	 * If the re-build failed, what is the proper error path
2175*4882a593Smuzhiyun 	 * from here?
2176*4882a593Smuzhiyun 	 *
2177*4882a593Smuzhiyun 	 * [We should abort the association. --piggy]
2178*4882a593Smuzhiyun 	 */
2179*4882a593Smuzhiyun 	if (!new_asoc) {
2180*4882a593Smuzhiyun 		/* FIXME: Several errors are possible.  A bad cookie should
2181*4882a593Smuzhiyun 		 * be silently discarded, but think about logging it too.
2182*4882a593Smuzhiyun 		 */
2183*4882a593Smuzhiyun 		switch (error) {
2184*4882a593Smuzhiyun 		case -SCTP_IERROR_NOMEM:
2185*4882a593Smuzhiyun 			goto nomem;
2186*4882a593Smuzhiyun 
2187*4882a593Smuzhiyun 		case -SCTP_IERROR_STALE_COOKIE:
2188*4882a593Smuzhiyun 			sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2189*4882a593Smuzhiyun 						   err_chk_p);
2190*4882a593Smuzhiyun 			return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2191*4882a593Smuzhiyun 		case -SCTP_IERROR_BAD_SIG:
2192*4882a593Smuzhiyun 		default:
2193*4882a593Smuzhiyun 			return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2194*4882a593Smuzhiyun 		}
2195*4882a593Smuzhiyun 	}
2196*4882a593Smuzhiyun 
2197*4882a593Smuzhiyun 	/* Update socket peer label if first association. */
2198*4882a593Smuzhiyun 	if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
2199*4882a593Smuzhiyun 					chunk->skb)) {
2200*4882a593Smuzhiyun 		sctp_association_free(new_asoc);
2201*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2202*4882a593Smuzhiyun 	}
2203*4882a593Smuzhiyun 
2204*4882a593Smuzhiyun 	/* Set temp so that it won't be added into hashtable */
2205*4882a593Smuzhiyun 	new_asoc->temp = 1;
2206*4882a593Smuzhiyun 
2207*4882a593Smuzhiyun 	/* Compare the tie_tag in cookie with the verification tag of
2208*4882a593Smuzhiyun 	 * current association.
2209*4882a593Smuzhiyun 	 */
2210*4882a593Smuzhiyun 	action = sctp_tietags_compare(new_asoc, asoc);
2211*4882a593Smuzhiyun 
2212*4882a593Smuzhiyun 	switch (action) {
2213*4882a593Smuzhiyun 	case 'A': /* Association restart. */
2214*4882a593Smuzhiyun 		retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2215*4882a593Smuzhiyun 					      new_asoc);
2216*4882a593Smuzhiyun 		break;
2217*4882a593Smuzhiyun 
2218*4882a593Smuzhiyun 	case 'B': /* Collision case B. */
2219*4882a593Smuzhiyun 		retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2220*4882a593Smuzhiyun 					      new_asoc);
2221*4882a593Smuzhiyun 		break;
2222*4882a593Smuzhiyun 
2223*4882a593Smuzhiyun 	case 'C': /* Collision case C. */
2224*4882a593Smuzhiyun 		retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2225*4882a593Smuzhiyun 					      new_asoc);
2226*4882a593Smuzhiyun 		break;
2227*4882a593Smuzhiyun 
2228*4882a593Smuzhiyun 	case 'D': /* Collision case D. */
2229*4882a593Smuzhiyun 		retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2230*4882a593Smuzhiyun 					      new_asoc);
2231*4882a593Smuzhiyun 		break;
2232*4882a593Smuzhiyun 
2233*4882a593Smuzhiyun 	default: /* Discard packet for all others. */
2234*4882a593Smuzhiyun 		retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2235*4882a593Smuzhiyun 		break;
2236*4882a593Smuzhiyun 	}
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun 	/* Delete the tempory new association. */
2239*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2240*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2241*4882a593Smuzhiyun 
2242*4882a593Smuzhiyun 	/* Restore association pointer to provide SCTP command interpeter
2243*4882a593Smuzhiyun 	 * with a valid context in case it needs to manipulate
2244*4882a593Smuzhiyun 	 * the queues */
2245*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2246*4882a593Smuzhiyun 			 SCTP_ASOC((struct sctp_association *)asoc));
2247*4882a593Smuzhiyun 
2248*4882a593Smuzhiyun 	return retval;
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun nomem:
2251*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
2252*4882a593Smuzhiyun }
2253*4882a593Smuzhiyun 
2254*4882a593Smuzhiyun /*
2255*4882a593Smuzhiyun  * Process an ABORT.  (SHUTDOWN-PENDING state)
2256*4882a593Smuzhiyun  *
2257*4882a593Smuzhiyun  * See sctp_sf_do_9_1_abort().
2258*4882a593Smuzhiyun  */
sctp_sf_shutdown_pending_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2259*4882a593Smuzhiyun enum sctp_disposition sctp_sf_shutdown_pending_abort(
2260*4882a593Smuzhiyun 					struct net *net,
2261*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2262*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2263*4882a593Smuzhiyun 					const union sctp_subtype type,
2264*4882a593Smuzhiyun 					void *arg,
2265*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2266*4882a593Smuzhiyun {
2267*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
2268*4882a593Smuzhiyun 
2269*4882a593Smuzhiyun 	if (!sctp_vtag_verify_either(chunk, asoc))
2270*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2271*4882a593Smuzhiyun 
2272*4882a593Smuzhiyun 	/* Make sure that the ABORT chunk has a valid length.
2273*4882a593Smuzhiyun 	 * Since this is an ABORT chunk, we have to discard it
2274*4882a593Smuzhiyun 	 * because of the following text:
2275*4882a593Smuzhiyun 	 * RFC 2960, Section 3.3.7
2276*4882a593Smuzhiyun 	 *    If an endpoint receives an ABORT with a format error or for an
2277*4882a593Smuzhiyun 	 *    association that doesn't exist, it MUST silently discard it.
2278*4882a593Smuzhiyun 	 * Because the length is "invalid", we can't really discard just
2279*4882a593Smuzhiyun 	 * as we do not know its true length.  So, to be safe, discard the
2280*4882a593Smuzhiyun 	 * packet.
2281*4882a593Smuzhiyun 	 */
2282*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2283*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2284*4882a593Smuzhiyun 
2285*4882a593Smuzhiyun 	/* ADD-IP: Special case for ABORT chunks
2286*4882a593Smuzhiyun 	 * F4)  One special consideration is that ABORT Chunks arriving
2287*4882a593Smuzhiyun 	 * destined to the IP address being deleted MUST be
2288*4882a593Smuzhiyun 	 * ignored (see Section 5.3.1 for further details).
2289*4882a593Smuzhiyun 	 */
2290*4882a593Smuzhiyun 	if (SCTP_ADDR_DEL ==
2291*4882a593Smuzhiyun 		    sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2292*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2293*4882a593Smuzhiyun 
2294*4882a593Smuzhiyun 	if (!sctp_err_chunk_valid(chunk))
2295*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2296*4882a593Smuzhiyun 
2297*4882a593Smuzhiyun 	return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2298*4882a593Smuzhiyun }
2299*4882a593Smuzhiyun 
2300*4882a593Smuzhiyun /*
2301*4882a593Smuzhiyun  * Process an ABORT.  (SHUTDOWN-SENT state)
2302*4882a593Smuzhiyun  *
2303*4882a593Smuzhiyun  * See sctp_sf_do_9_1_abort().
2304*4882a593Smuzhiyun  */
sctp_sf_shutdown_sent_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2305*4882a593Smuzhiyun enum sctp_disposition sctp_sf_shutdown_sent_abort(
2306*4882a593Smuzhiyun 					struct net *net,
2307*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2308*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2309*4882a593Smuzhiyun 					const union sctp_subtype type,
2310*4882a593Smuzhiyun 					void *arg,
2311*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2312*4882a593Smuzhiyun {
2313*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
2314*4882a593Smuzhiyun 
2315*4882a593Smuzhiyun 	if (!sctp_vtag_verify_either(chunk, asoc))
2316*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2317*4882a593Smuzhiyun 
2318*4882a593Smuzhiyun 	/* Make sure that the ABORT chunk has a valid length.
2319*4882a593Smuzhiyun 	 * Since this is an ABORT chunk, we have to discard it
2320*4882a593Smuzhiyun 	 * because of the following text:
2321*4882a593Smuzhiyun 	 * RFC 2960, Section 3.3.7
2322*4882a593Smuzhiyun 	 *    If an endpoint receives an ABORT with a format error or for an
2323*4882a593Smuzhiyun 	 *    association that doesn't exist, it MUST silently discard it.
2324*4882a593Smuzhiyun 	 * Because the length is "invalid", we can't really discard just
2325*4882a593Smuzhiyun 	 * as we do not know its true length.  So, to be safe, discard the
2326*4882a593Smuzhiyun 	 * packet.
2327*4882a593Smuzhiyun 	 */
2328*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2329*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2330*4882a593Smuzhiyun 
2331*4882a593Smuzhiyun 	/* ADD-IP: Special case for ABORT chunks
2332*4882a593Smuzhiyun 	 * F4)  One special consideration is that ABORT Chunks arriving
2333*4882a593Smuzhiyun 	 * destined to the IP address being deleted MUST be
2334*4882a593Smuzhiyun 	 * ignored (see Section 5.3.1 for further details).
2335*4882a593Smuzhiyun 	 */
2336*4882a593Smuzhiyun 	if (SCTP_ADDR_DEL ==
2337*4882a593Smuzhiyun 		    sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2338*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2339*4882a593Smuzhiyun 
2340*4882a593Smuzhiyun 	if (!sctp_err_chunk_valid(chunk))
2341*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2342*4882a593Smuzhiyun 
2343*4882a593Smuzhiyun 	/* Stop the T2-shutdown timer. */
2344*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2345*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2346*4882a593Smuzhiyun 
2347*4882a593Smuzhiyun 	/* Stop the T5-shutdown guard timer.  */
2348*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2349*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2350*4882a593Smuzhiyun 
2351*4882a593Smuzhiyun 	return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2352*4882a593Smuzhiyun }
2353*4882a593Smuzhiyun 
2354*4882a593Smuzhiyun /*
2355*4882a593Smuzhiyun  * Process an ABORT.  (SHUTDOWN-ACK-SENT state)
2356*4882a593Smuzhiyun  *
2357*4882a593Smuzhiyun  * See sctp_sf_do_9_1_abort().
2358*4882a593Smuzhiyun  */
sctp_sf_shutdown_ack_sent_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2359*4882a593Smuzhiyun enum sctp_disposition sctp_sf_shutdown_ack_sent_abort(
2360*4882a593Smuzhiyun 					struct net *net,
2361*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2362*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2363*4882a593Smuzhiyun 					const union sctp_subtype type,
2364*4882a593Smuzhiyun 					void *arg,
2365*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2366*4882a593Smuzhiyun {
2367*4882a593Smuzhiyun 	/* The same T2 timer, so we should be able to use
2368*4882a593Smuzhiyun 	 * common function with the SHUTDOWN-SENT state.
2369*4882a593Smuzhiyun 	 */
2370*4882a593Smuzhiyun 	return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2371*4882a593Smuzhiyun }
2372*4882a593Smuzhiyun 
2373*4882a593Smuzhiyun /*
2374*4882a593Smuzhiyun  * Handle an Error received in COOKIE_ECHOED state.
2375*4882a593Smuzhiyun  *
2376*4882a593Smuzhiyun  * Only handle the error type of stale COOKIE Error, the other errors will
2377*4882a593Smuzhiyun  * be ignored.
2378*4882a593Smuzhiyun  *
2379*4882a593Smuzhiyun  * Inputs
2380*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
2381*4882a593Smuzhiyun  *
2382*4882a593Smuzhiyun  * Outputs
2383*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
2384*4882a593Smuzhiyun  *
2385*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
2386*4882a593Smuzhiyun  */
sctp_sf_cookie_echoed_err(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2387*4882a593Smuzhiyun enum sctp_disposition sctp_sf_cookie_echoed_err(
2388*4882a593Smuzhiyun 					struct net *net,
2389*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2390*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2391*4882a593Smuzhiyun 					const union sctp_subtype type,
2392*4882a593Smuzhiyun 					void *arg,
2393*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2394*4882a593Smuzhiyun {
2395*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
2396*4882a593Smuzhiyun 	struct sctp_errhdr *err;
2397*4882a593Smuzhiyun 
2398*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
2399*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2400*4882a593Smuzhiyun 
2401*4882a593Smuzhiyun 	/* Make sure that the ERROR chunk has a valid length.
2402*4882a593Smuzhiyun 	 * The parameter walking depends on this as well.
2403*4882a593Smuzhiyun 	 */
2404*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
2405*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2406*4882a593Smuzhiyun 						  commands);
2407*4882a593Smuzhiyun 
2408*4882a593Smuzhiyun 	/* Process the error here */
2409*4882a593Smuzhiyun 	/* FUTURE FIXME:  When PR-SCTP related and other optional
2410*4882a593Smuzhiyun 	 * parms are emitted, this will have to change to handle multiple
2411*4882a593Smuzhiyun 	 * errors.
2412*4882a593Smuzhiyun 	 */
2413*4882a593Smuzhiyun 	sctp_walk_errors(err, chunk->chunk_hdr) {
2414*4882a593Smuzhiyun 		if (SCTP_ERROR_STALE_COOKIE == err->cause)
2415*4882a593Smuzhiyun 			return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2416*4882a593Smuzhiyun 							arg, commands);
2417*4882a593Smuzhiyun 	}
2418*4882a593Smuzhiyun 
2419*4882a593Smuzhiyun 	/* It is possible to have malformed error causes, and that
2420*4882a593Smuzhiyun 	 * will cause us to end the walk early.  However, since
2421*4882a593Smuzhiyun 	 * we are discarding the packet, there should be no adverse
2422*4882a593Smuzhiyun 	 * affects.
2423*4882a593Smuzhiyun 	 */
2424*4882a593Smuzhiyun 	return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2425*4882a593Smuzhiyun }
2426*4882a593Smuzhiyun 
2427*4882a593Smuzhiyun /*
2428*4882a593Smuzhiyun  * Handle a Stale COOKIE Error
2429*4882a593Smuzhiyun  *
2430*4882a593Smuzhiyun  * Section: 5.2.6 Handle Stale COOKIE Error
2431*4882a593Smuzhiyun  * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2432*4882a593Smuzhiyun  * one of the following three alternatives.
2433*4882a593Smuzhiyun  * ...
2434*4882a593Smuzhiyun  * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2435*4882a593Smuzhiyun  *    Preservative parameter requesting an extension to the lifetime of
2436*4882a593Smuzhiyun  *    the State Cookie. When calculating the time extension, an
2437*4882a593Smuzhiyun  *    implementation SHOULD use the RTT information measured based on the
2438*4882a593Smuzhiyun  *    previous COOKIE ECHO / ERROR exchange, and should add no more
2439*4882a593Smuzhiyun  *    than 1 second beyond the measured RTT, due to long State Cookie
2440*4882a593Smuzhiyun  *    lifetimes making the endpoint more subject to a replay attack.
2441*4882a593Smuzhiyun  *
2442*4882a593Smuzhiyun  * Verification Tag:  Not explicit, but safe to ignore.
2443*4882a593Smuzhiyun  *
2444*4882a593Smuzhiyun  * Inputs
2445*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
2446*4882a593Smuzhiyun  *
2447*4882a593Smuzhiyun  * Outputs
2448*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
2449*4882a593Smuzhiyun  *
2450*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
2451*4882a593Smuzhiyun  */
sctp_sf_do_5_2_6_stale(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2452*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_do_5_2_6_stale(
2453*4882a593Smuzhiyun 					struct net *net,
2454*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2455*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2456*4882a593Smuzhiyun 					const union sctp_subtype type,
2457*4882a593Smuzhiyun 					void *arg,
2458*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2459*4882a593Smuzhiyun {
2460*4882a593Smuzhiyun 	int attempts = asoc->init_err_counter + 1;
2461*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg, *reply;
2462*4882a593Smuzhiyun 	struct sctp_cookie_preserve_param bht;
2463*4882a593Smuzhiyun 	struct sctp_bind_addr *bp;
2464*4882a593Smuzhiyun 	struct sctp_errhdr *err;
2465*4882a593Smuzhiyun 	u32 stale;
2466*4882a593Smuzhiyun 
2467*4882a593Smuzhiyun 	if (attempts > asoc->max_init_attempts) {
2468*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2469*4882a593Smuzhiyun 				SCTP_ERROR(ETIMEDOUT));
2470*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2471*4882a593Smuzhiyun 				SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2472*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DELETE_TCB;
2473*4882a593Smuzhiyun 	}
2474*4882a593Smuzhiyun 
2475*4882a593Smuzhiyun 	err = (struct sctp_errhdr *)(chunk->skb->data);
2476*4882a593Smuzhiyun 
2477*4882a593Smuzhiyun 	/* When calculating the time extension, an implementation
2478*4882a593Smuzhiyun 	 * SHOULD use the RTT information measured based on the
2479*4882a593Smuzhiyun 	 * previous COOKIE ECHO / ERROR exchange, and should add no
2480*4882a593Smuzhiyun 	 * more than 1 second beyond the measured RTT, due to long
2481*4882a593Smuzhiyun 	 * State Cookie lifetimes making the endpoint more subject to
2482*4882a593Smuzhiyun 	 * a replay attack.
2483*4882a593Smuzhiyun 	 * Measure of Staleness's unit is usec. (1/1000000 sec)
2484*4882a593Smuzhiyun 	 * Suggested Cookie Life-span Increment's unit is msec.
2485*4882a593Smuzhiyun 	 * (1/1000 sec)
2486*4882a593Smuzhiyun 	 * In general, if you use the suggested cookie life, the value
2487*4882a593Smuzhiyun 	 * found in the field of measure of staleness should be doubled
2488*4882a593Smuzhiyun 	 * to give ample time to retransmit the new cookie and thus
2489*4882a593Smuzhiyun 	 * yield a higher probability of success on the reattempt.
2490*4882a593Smuzhiyun 	 */
2491*4882a593Smuzhiyun 	stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
2492*4882a593Smuzhiyun 	stale = (stale * 2) / 1000;
2493*4882a593Smuzhiyun 
2494*4882a593Smuzhiyun 	bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2495*4882a593Smuzhiyun 	bht.param_hdr.length = htons(sizeof(bht));
2496*4882a593Smuzhiyun 	bht.lifespan_increment = htonl(stale);
2497*4882a593Smuzhiyun 
2498*4882a593Smuzhiyun 	/* Build that new INIT chunk.  */
2499*4882a593Smuzhiyun 	bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2500*4882a593Smuzhiyun 	reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2501*4882a593Smuzhiyun 	if (!reply)
2502*4882a593Smuzhiyun 		goto nomem;
2503*4882a593Smuzhiyun 
2504*4882a593Smuzhiyun 	sctp_addto_chunk(reply, sizeof(bht), &bht);
2505*4882a593Smuzhiyun 
2506*4882a593Smuzhiyun 	/* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2507*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2508*4882a593Smuzhiyun 
2509*4882a593Smuzhiyun 	/* Stop pending T3-rtx and heartbeat timers */
2510*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2511*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2512*4882a593Smuzhiyun 
2513*4882a593Smuzhiyun 	/* Delete non-primary peer ip addresses since we are transitioning
2514*4882a593Smuzhiyun 	 * back to the COOKIE-WAIT state
2515*4882a593Smuzhiyun 	 */
2516*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2517*4882a593Smuzhiyun 
2518*4882a593Smuzhiyun 	/* If we've sent any data bundled with COOKIE-ECHO we will need to
2519*4882a593Smuzhiyun 	 * resend
2520*4882a593Smuzhiyun 	 */
2521*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2522*4882a593Smuzhiyun 			SCTP_TRANSPORT(asoc->peer.primary_path));
2523*4882a593Smuzhiyun 
2524*4882a593Smuzhiyun 	/* Cast away the const modifier, as we want to just
2525*4882a593Smuzhiyun 	 * rerun it through as a sideffect.
2526*4882a593Smuzhiyun 	 */
2527*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2528*4882a593Smuzhiyun 
2529*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2530*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2531*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2532*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2533*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2534*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2535*4882a593Smuzhiyun 
2536*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2537*4882a593Smuzhiyun 
2538*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
2539*4882a593Smuzhiyun 
2540*4882a593Smuzhiyun nomem:
2541*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
2542*4882a593Smuzhiyun }
2543*4882a593Smuzhiyun 
2544*4882a593Smuzhiyun /*
2545*4882a593Smuzhiyun  * Process an ABORT.
2546*4882a593Smuzhiyun  *
2547*4882a593Smuzhiyun  * Section: 9.1
2548*4882a593Smuzhiyun  * After checking the Verification Tag, the receiving endpoint shall
2549*4882a593Smuzhiyun  * remove the association from its record, and shall report the
2550*4882a593Smuzhiyun  * termination to its upper layer.
2551*4882a593Smuzhiyun  *
2552*4882a593Smuzhiyun  * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2553*4882a593Smuzhiyun  * B) Rules for packet carrying ABORT:
2554*4882a593Smuzhiyun  *
2555*4882a593Smuzhiyun  *  - The endpoint shall always fill in the Verification Tag field of the
2556*4882a593Smuzhiyun  *    outbound packet with the destination endpoint's tag value if it
2557*4882a593Smuzhiyun  *    is known.
2558*4882a593Smuzhiyun  *
2559*4882a593Smuzhiyun  *  - If the ABORT is sent in response to an OOTB packet, the endpoint
2560*4882a593Smuzhiyun  *    MUST follow the procedure described in Section 8.4.
2561*4882a593Smuzhiyun  *
2562*4882a593Smuzhiyun  *  - The receiver MUST accept the packet if the Verification Tag
2563*4882a593Smuzhiyun  *    matches either its own tag, OR the tag of its peer. Otherwise, the
2564*4882a593Smuzhiyun  *    receiver MUST silently discard the packet and take no further
2565*4882a593Smuzhiyun  *    action.
2566*4882a593Smuzhiyun  *
2567*4882a593Smuzhiyun  * Inputs
2568*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
2569*4882a593Smuzhiyun  *
2570*4882a593Smuzhiyun  * Outputs
2571*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
2572*4882a593Smuzhiyun  *
2573*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
2574*4882a593Smuzhiyun  */
sctp_sf_do_9_1_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2575*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_9_1_abort(
2576*4882a593Smuzhiyun 					struct net *net,
2577*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2578*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2579*4882a593Smuzhiyun 					const union sctp_subtype type,
2580*4882a593Smuzhiyun 					void *arg,
2581*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2582*4882a593Smuzhiyun {
2583*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
2584*4882a593Smuzhiyun 
2585*4882a593Smuzhiyun 	if (!sctp_vtag_verify_either(chunk, asoc))
2586*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2587*4882a593Smuzhiyun 
2588*4882a593Smuzhiyun 	/* Make sure that the ABORT chunk has a valid length.
2589*4882a593Smuzhiyun 	 * Since this is an ABORT chunk, we have to discard it
2590*4882a593Smuzhiyun 	 * because of the following text:
2591*4882a593Smuzhiyun 	 * RFC 2960, Section 3.3.7
2592*4882a593Smuzhiyun 	 *    If an endpoint receives an ABORT with a format error or for an
2593*4882a593Smuzhiyun 	 *    association that doesn't exist, it MUST silently discard it.
2594*4882a593Smuzhiyun 	 * Because the length is "invalid", we can't really discard just
2595*4882a593Smuzhiyun 	 * as we do not know its true length.  So, to be safe, discard the
2596*4882a593Smuzhiyun 	 * packet.
2597*4882a593Smuzhiyun 	 */
2598*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2599*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2600*4882a593Smuzhiyun 
2601*4882a593Smuzhiyun 	/* ADD-IP: Special case for ABORT chunks
2602*4882a593Smuzhiyun 	 * F4)  One special consideration is that ABORT Chunks arriving
2603*4882a593Smuzhiyun 	 * destined to the IP address being deleted MUST be
2604*4882a593Smuzhiyun 	 * ignored (see Section 5.3.1 for further details).
2605*4882a593Smuzhiyun 	 */
2606*4882a593Smuzhiyun 	if (SCTP_ADDR_DEL ==
2607*4882a593Smuzhiyun 		    sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2608*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2609*4882a593Smuzhiyun 
2610*4882a593Smuzhiyun 	if (!sctp_err_chunk_valid(chunk))
2611*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2612*4882a593Smuzhiyun 
2613*4882a593Smuzhiyun 	return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2614*4882a593Smuzhiyun }
2615*4882a593Smuzhiyun 
__sctp_sf_do_9_1_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2616*4882a593Smuzhiyun static enum sctp_disposition __sctp_sf_do_9_1_abort(
2617*4882a593Smuzhiyun 					struct net *net,
2618*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2619*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2620*4882a593Smuzhiyun 					const union sctp_subtype type,
2621*4882a593Smuzhiyun 					void *arg,
2622*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2623*4882a593Smuzhiyun {
2624*4882a593Smuzhiyun 	__be16 error = SCTP_ERROR_NO_ERROR;
2625*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
2626*4882a593Smuzhiyun 	unsigned int len;
2627*4882a593Smuzhiyun 
2628*4882a593Smuzhiyun 	/* See if we have an error cause code in the chunk.  */
2629*4882a593Smuzhiyun 	len = ntohs(chunk->chunk_hdr->length);
2630*4882a593Smuzhiyun 	if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2631*4882a593Smuzhiyun 		error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2632*4882a593Smuzhiyun 
2633*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2634*4882a593Smuzhiyun 	/* ASSOC_FAILED will DELETE_TCB. */
2635*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2636*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2637*4882a593Smuzhiyun 	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2638*4882a593Smuzhiyun 
2639*4882a593Smuzhiyun 	return SCTP_DISPOSITION_ABORT;
2640*4882a593Smuzhiyun }
2641*4882a593Smuzhiyun 
2642*4882a593Smuzhiyun /*
2643*4882a593Smuzhiyun  * Process an ABORT.  (COOKIE-WAIT state)
2644*4882a593Smuzhiyun  *
2645*4882a593Smuzhiyun  * See sctp_sf_do_9_1_abort() above.
2646*4882a593Smuzhiyun  */
sctp_sf_cookie_wait_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2647*4882a593Smuzhiyun enum sctp_disposition sctp_sf_cookie_wait_abort(
2648*4882a593Smuzhiyun 					struct net *net,
2649*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2650*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2651*4882a593Smuzhiyun 					const union sctp_subtype type,
2652*4882a593Smuzhiyun 					void *arg,
2653*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2654*4882a593Smuzhiyun {
2655*4882a593Smuzhiyun 	__be16 error = SCTP_ERROR_NO_ERROR;
2656*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
2657*4882a593Smuzhiyun 	unsigned int len;
2658*4882a593Smuzhiyun 
2659*4882a593Smuzhiyun 	if (!sctp_vtag_verify_either(chunk, asoc))
2660*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2661*4882a593Smuzhiyun 
2662*4882a593Smuzhiyun 	/* Make sure that the ABORT chunk has a valid length.
2663*4882a593Smuzhiyun 	 * Since this is an ABORT chunk, we have to discard it
2664*4882a593Smuzhiyun 	 * because of the following text:
2665*4882a593Smuzhiyun 	 * RFC 2960, Section 3.3.7
2666*4882a593Smuzhiyun 	 *    If an endpoint receives an ABORT with a format error or for an
2667*4882a593Smuzhiyun 	 *    association that doesn't exist, it MUST silently discard it.
2668*4882a593Smuzhiyun 	 * Because the length is "invalid", we can't really discard just
2669*4882a593Smuzhiyun 	 * as we do not know its true length.  So, to be safe, discard the
2670*4882a593Smuzhiyun 	 * packet.
2671*4882a593Smuzhiyun 	 */
2672*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2673*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2674*4882a593Smuzhiyun 
2675*4882a593Smuzhiyun 	/* See if we have an error cause code in the chunk.  */
2676*4882a593Smuzhiyun 	len = ntohs(chunk->chunk_hdr->length);
2677*4882a593Smuzhiyun 	if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2678*4882a593Smuzhiyun 		error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2679*4882a593Smuzhiyun 
2680*4882a593Smuzhiyun 	return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2681*4882a593Smuzhiyun 				      chunk->transport);
2682*4882a593Smuzhiyun }
2683*4882a593Smuzhiyun 
2684*4882a593Smuzhiyun /*
2685*4882a593Smuzhiyun  * Process an incoming ICMP as an ABORT.  (COOKIE-WAIT state)
2686*4882a593Smuzhiyun  */
sctp_sf_cookie_wait_icmp_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2687*4882a593Smuzhiyun enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
2688*4882a593Smuzhiyun 					struct net *net,
2689*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2690*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2691*4882a593Smuzhiyun 					const union sctp_subtype type,
2692*4882a593Smuzhiyun 					void *arg,
2693*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2694*4882a593Smuzhiyun {
2695*4882a593Smuzhiyun 	return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2696*4882a593Smuzhiyun 				      ENOPROTOOPT, asoc,
2697*4882a593Smuzhiyun 				      (struct sctp_transport *)arg);
2698*4882a593Smuzhiyun }
2699*4882a593Smuzhiyun 
2700*4882a593Smuzhiyun /*
2701*4882a593Smuzhiyun  * Process an ABORT.  (COOKIE-ECHOED state)
2702*4882a593Smuzhiyun  */
sctp_sf_cookie_echoed_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2703*4882a593Smuzhiyun enum sctp_disposition sctp_sf_cookie_echoed_abort(
2704*4882a593Smuzhiyun 					struct net *net,
2705*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2706*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2707*4882a593Smuzhiyun 					const union sctp_subtype type,
2708*4882a593Smuzhiyun 					void *arg,
2709*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2710*4882a593Smuzhiyun {
2711*4882a593Smuzhiyun 	/* There is a single T1 timer, so we should be able to use
2712*4882a593Smuzhiyun 	 * common function with the COOKIE-WAIT state.
2713*4882a593Smuzhiyun 	 */
2714*4882a593Smuzhiyun 	return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2715*4882a593Smuzhiyun }
2716*4882a593Smuzhiyun 
2717*4882a593Smuzhiyun /*
2718*4882a593Smuzhiyun  * Stop T1 timer and abort association with "INIT failed".
2719*4882a593Smuzhiyun  *
2720*4882a593Smuzhiyun  * This is common code called by several sctp_sf_*_abort() functions above.
2721*4882a593Smuzhiyun  */
sctp_stop_t1_and_abort(struct net * net,struct sctp_cmd_seq * commands,__be16 error,int sk_err,const struct sctp_association * asoc,struct sctp_transport * transport)2722*4882a593Smuzhiyun static enum sctp_disposition sctp_stop_t1_and_abort(
2723*4882a593Smuzhiyun 					struct net *net,
2724*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands,
2725*4882a593Smuzhiyun 					__be16 error, int sk_err,
2726*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2727*4882a593Smuzhiyun 					struct sctp_transport *transport)
2728*4882a593Smuzhiyun {
2729*4882a593Smuzhiyun 	pr_debug("%s: ABORT received (INIT)\n", __func__);
2730*4882a593Smuzhiyun 
2731*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2732*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_CLOSED));
2733*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2734*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2735*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2736*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2737*4882a593Smuzhiyun 	/* CMD_INIT_FAILED will DELETE_TCB. */
2738*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2739*4882a593Smuzhiyun 			SCTP_PERR(error));
2740*4882a593Smuzhiyun 
2741*4882a593Smuzhiyun 	return SCTP_DISPOSITION_ABORT;
2742*4882a593Smuzhiyun }
2743*4882a593Smuzhiyun 
2744*4882a593Smuzhiyun /*
2745*4882a593Smuzhiyun  * sctp_sf_do_9_2_shut
2746*4882a593Smuzhiyun  *
2747*4882a593Smuzhiyun  * Section: 9.2
2748*4882a593Smuzhiyun  * Upon the reception of the SHUTDOWN, the peer endpoint shall
2749*4882a593Smuzhiyun  *  - enter the SHUTDOWN-RECEIVED state,
2750*4882a593Smuzhiyun  *
2751*4882a593Smuzhiyun  *  - stop accepting new data from its SCTP user
2752*4882a593Smuzhiyun  *
2753*4882a593Smuzhiyun  *  - verify, by checking the Cumulative TSN Ack field of the chunk,
2754*4882a593Smuzhiyun  *    that all its outstanding DATA chunks have been received by the
2755*4882a593Smuzhiyun  *    SHUTDOWN sender.
2756*4882a593Smuzhiyun  *
2757*4882a593Smuzhiyun  * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2758*4882a593Smuzhiyun  * send a SHUTDOWN in response to a ULP request. And should discard
2759*4882a593Smuzhiyun  * subsequent SHUTDOWN chunks.
2760*4882a593Smuzhiyun  *
2761*4882a593Smuzhiyun  * If there are still outstanding DATA chunks left, the SHUTDOWN
2762*4882a593Smuzhiyun  * receiver shall continue to follow normal data transmission
2763*4882a593Smuzhiyun  * procedures defined in Section 6 until all outstanding DATA chunks
2764*4882a593Smuzhiyun  * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2765*4882a593Smuzhiyun  * new data from its SCTP user.
2766*4882a593Smuzhiyun  *
2767*4882a593Smuzhiyun  * Verification Tag:  8.5 Verification Tag [Normal verification]
2768*4882a593Smuzhiyun  *
2769*4882a593Smuzhiyun  * Inputs
2770*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
2771*4882a593Smuzhiyun  *
2772*4882a593Smuzhiyun  * Outputs
2773*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
2774*4882a593Smuzhiyun  *
2775*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
2776*4882a593Smuzhiyun  */
sctp_sf_do_9_2_shutdown(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2777*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_9_2_shutdown(
2778*4882a593Smuzhiyun 					struct net *net,
2779*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2780*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2781*4882a593Smuzhiyun 					const union sctp_subtype type,
2782*4882a593Smuzhiyun 					void *arg,
2783*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2784*4882a593Smuzhiyun {
2785*4882a593Smuzhiyun 	enum sctp_disposition disposition;
2786*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
2787*4882a593Smuzhiyun 	struct sctp_shutdownhdr *sdh;
2788*4882a593Smuzhiyun 	struct sctp_ulpevent *ev;
2789*4882a593Smuzhiyun 	__u32 ctsn;
2790*4882a593Smuzhiyun 
2791*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
2792*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2793*4882a593Smuzhiyun 
2794*4882a593Smuzhiyun 	/* Make sure that the SHUTDOWN chunk has a valid length. */
2795*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2796*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2797*4882a593Smuzhiyun 						  commands);
2798*4882a593Smuzhiyun 
2799*4882a593Smuzhiyun 	/* Convert the elaborate header.  */
2800*4882a593Smuzhiyun 	sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2801*4882a593Smuzhiyun 	skb_pull(chunk->skb, sizeof(*sdh));
2802*4882a593Smuzhiyun 	chunk->subh.shutdown_hdr = sdh;
2803*4882a593Smuzhiyun 	ctsn = ntohl(sdh->cum_tsn_ack);
2804*4882a593Smuzhiyun 
2805*4882a593Smuzhiyun 	if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2806*4882a593Smuzhiyun 		pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2807*4882a593Smuzhiyun 			 asoc->ctsn_ack_point);
2808*4882a593Smuzhiyun 
2809*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
2810*4882a593Smuzhiyun 	}
2811*4882a593Smuzhiyun 
2812*4882a593Smuzhiyun 	/* If Cumulative TSN Ack beyond the max tsn currently
2813*4882a593Smuzhiyun 	 * send, terminating the association and respond to the
2814*4882a593Smuzhiyun 	 * sender with an ABORT.
2815*4882a593Smuzhiyun 	 */
2816*4882a593Smuzhiyun 	if (!TSN_lt(ctsn, asoc->next_tsn))
2817*4882a593Smuzhiyun 		return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2818*4882a593Smuzhiyun 
2819*4882a593Smuzhiyun 	/* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2820*4882a593Smuzhiyun 	 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2821*4882a593Smuzhiyun 	 * inform the application that it should cease sending data.
2822*4882a593Smuzhiyun 	 */
2823*4882a593Smuzhiyun 	ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2824*4882a593Smuzhiyun 	if (!ev) {
2825*4882a593Smuzhiyun 		disposition = SCTP_DISPOSITION_NOMEM;
2826*4882a593Smuzhiyun 		goto out;
2827*4882a593Smuzhiyun 	}
2828*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2829*4882a593Smuzhiyun 
2830*4882a593Smuzhiyun 	/* Upon the reception of the SHUTDOWN, the peer endpoint shall
2831*4882a593Smuzhiyun 	 *  - enter the SHUTDOWN-RECEIVED state,
2832*4882a593Smuzhiyun 	 *  - stop accepting new data from its SCTP user
2833*4882a593Smuzhiyun 	 *
2834*4882a593Smuzhiyun 	 * [This is implicit in the new state.]
2835*4882a593Smuzhiyun 	 */
2836*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2837*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2838*4882a593Smuzhiyun 	disposition = SCTP_DISPOSITION_CONSUME;
2839*4882a593Smuzhiyun 
2840*4882a593Smuzhiyun 	if (sctp_outq_is_empty(&asoc->outqueue)) {
2841*4882a593Smuzhiyun 		disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2842*4882a593Smuzhiyun 							  arg, commands);
2843*4882a593Smuzhiyun 	}
2844*4882a593Smuzhiyun 
2845*4882a593Smuzhiyun 	if (SCTP_DISPOSITION_NOMEM == disposition)
2846*4882a593Smuzhiyun 		goto out;
2847*4882a593Smuzhiyun 
2848*4882a593Smuzhiyun 	/*  - verify, by checking the Cumulative TSN Ack field of the
2849*4882a593Smuzhiyun 	 *    chunk, that all its outstanding DATA chunks have been
2850*4882a593Smuzhiyun 	 *    received by the SHUTDOWN sender.
2851*4882a593Smuzhiyun 	 */
2852*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2853*4882a593Smuzhiyun 			SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2854*4882a593Smuzhiyun 
2855*4882a593Smuzhiyun out:
2856*4882a593Smuzhiyun 	return disposition;
2857*4882a593Smuzhiyun }
2858*4882a593Smuzhiyun 
2859*4882a593Smuzhiyun /*
2860*4882a593Smuzhiyun  * sctp_sf_do_9_2_shut_ctsn
2861*4882a593Smuzhiyun  *
2862*4882a593Smuzhiyun  * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2863*4882a593Smuzhiyun  * it MUST NOT send a SHUTDOWN in response to a ULP request.
2864*4882a593Smuzhiyun  * The Cumulative TSN Ack of the received SHUTDOWN chunk
2865*4882a593Smuzhiyun  * MUST be processed.
2866*4882a593Smuzhiyun  */
sctp_sf_do_9_2_shut_ctsn(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2867*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
2868*4882a593Smuzhiyun 					struct net *net,
2869*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
2870*4882a593Smuzhiyun 					const struct sctp_association *asoc,
2871*4882a593Smuzhiyun 					const union sctp_subtype type,
2872*4882a593Smuzhiyun 					void *arg,
2873*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
2874*4882a593Smuzhiyun {
2875*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
2876*4882a593Smuzhiyun 	struct sctp_shutdownhdr *sdh;
2877*4882a593Smuzhiyun 	__u32 ctsn;
2878*4882a593Smuzhiyun 
2879*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
2880*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2881*4882a593Smuzhiyun 
2882*4882a593Smuzhiyun 	/* Make sure that the SHUTDOWN chunk has a valid length. */
2883*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2884*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2885*4882a593Smuzhiyun 						  commands);
2886*4882a593Smuzhiyun 
2887*4882a593Smuzhiyun 	sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2888*4882a593Smuzhiyun 	ctsn = ntohl(sdh->cum_tsn_ack);
2889*4882a593Smuzhiyun 
2890*4882a593Smuzhiyun 	if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2891*4882a593Smuzhiyun 		pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2892*4882a593Smuzhiyun 			 asoc->ctsn_ack_point);
2893*4882a593Smuzhiyun 
2894*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
2895*4882a593Smuzhiyun 	}
2896*4882a593Smuzhiyun 
2897*4882a593Smuzhiyun 	/* If Cumulative TSN Ack beyond the max tsn currently
2898*4882a593Smuzhiyun 	 * send, terminating the association and respond to the
2899*4882a593Smuzhiyun 	 * sender with an ABORT.
2900*4882a593Smuzhiyun 	 */
2901*4882a593Smuzhiyun 	if (!TSN_lt(ctsn, asoc->next_tsn))
2902*4882a593Smuzhiyun 		return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2903*4882a593Smuzhiyun 
2904*4882a593Smuzhiyun 	/* verify, by checking the Cumulative TSN Ack field of the
2905*4882a593Smuzhiyun 	 * chunk, that all its outstanding DATA chunks have been
2906*4882a593Smuzhiyun 	 * received by the SHUTDOWN sender.
2907*4882a593Smuzhiyun 	 */
2908*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2909*4882a593Smuzhiyun 			SCTP_BE32(sdh->cum_tsn_ack));
2910*4882a593Smuzhiyun 
2911*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
2912*4882a593Smuzhiyun }
2913*4882a593Smuzhiyun 
2914*4882a593Smuzhiyun /* RFC 2960 9.2
2915*4882a593Smuzhiyun  * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2916*4882a593Smuzhiyun  * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2917*4882a593Smuzhiyun  * transport addresses (either in the IP addresses or in the INIT chunk)
2918*4882a593Smuzhiyun  * that belong to this association, it should discard the INIT chunk and
2919*4882a593Smuzhiyun  * retransmit the SHUTDOWN ACK chunk.
2920*4882a593Smuzhiyun  */
2921*4882a593Smuzhiyun static enum sctp_disposition
__sctp_sf_do_9_2_reshutack(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2922*4882a593Smuzhiyun __sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
2923*4882a593Smuzhiyun 			   const struct sctp_association *asoc,
2924*4882a593Smuzhiyun 			   const union sctp_subtype type, void *arg,
2925*4882a593Smuzhiyun 			   struct sctp_cmd_seq *commands)
2926*4882a593Smuzhiyun {
2927*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
2928*4882a593Smuzhiyun 	struct sctp_chunk *reply;
2929*4882a593Smuzhiyun 
2930*4882a593Smuzhiyun 	/* Make sure that the chunk has a valid length */
2931*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2932*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2933*4882a593Smuzhiyun 						  commands);
2934*4882a593Smuzhiyun 
2935*4882a593Smuzhiyun 	/* Since we are not going to really process this INIT, there
2936*4882a593Smuzhiyun 	 * is no point in verifying chunk boundries.  Just generate
2937*4882a593Smuzhiyun 	 * the SHUTDOWN ACK.
2938*4882a593Smuzhiyun 	 */
2939*4882a593Smuzhiyun 	reply = sctp_make_shutdown_ack(asoc, chunk);
2940*4882a593Smuzhiyun 	if (NULL == reply)
2941*4882a593Smuzhiyun 		goto nomem;
2942*4882a593Smuzhiyun 
2943*4882a593Smuzhiyun 	/* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2944*4882a593Smuzhiyun 	 * the T2-SHUTDOWN timer.
2945*4882a593Smuzhiyun 	 */
2946*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2947*4882a593Smuzhiyun 
2948*4882a593Smuzhiyun 	/* and restart the T2-shutdown timer. */
2949*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2950*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2951*4882a593Smuzhiyun 
2952*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2953*4882a593Smuzhiyun 
2954*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
2955*4882a593Smuzhiyun nomem:
2956*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
2957*4882a593Smuzhiyun }
2958*4882a593Smuzhiyun 
2959*4882a593Smuzhiyun enum sctp_disposition
sctp_sf_do_9_2_reshutack(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)2960*4882a593Smuzhiyun sctp_sf_do_9_2_reshutack(struct net *net, const struct sctp_endpoint *ep,
2961*4882a593Smuzhiyun 			 const struct sctp_association *asoc,
2962*4882a593Smuzhiyun 			 const union sctp_subtype type, void *arg,
2963*4882a593Smuzhiyun 			 struct sctp_cmd_seq *commands)
2964*4882a593Smuzhiyun {
2965*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
2966*4882a593Smuzhiyun 
2967*4882a593Smuzhiyun 	if (!chunk->singleton)
2968*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2969*4882a593Smuzhiyun 
2970*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
2971*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2972*4882a593Smuzhiyun 
2973*4882a593Smuzhiyun 	if (chunk->sctp_hdr->vtag != 0)
2974*4882a593Smuzhiyun 		return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
2975*4882a593Smuzhiyun 
2976*4882a593Smuzhiyun 	return __sctp_sf_do_9_2_reshutack(net, ep, asoc, type, arg, commands);
2977*4882a593Smuzhiyun }
2978*4882a593Smuzhiyun 
2979*4882a593Smuzhiyun /*
2980*4882a593Smuzhiyun  * sctp_sf_do_ecn_cwr
2981*4882a593Smuzhiyun  *
2982*4882a593Smuzhiyun  * Section:  Appendix A: Explicit Congestion Notification
2983*4882a593Smuzhiyun  *
2984*4882a593Smuzhiyun  * CWR:
2985*4882a593Smuzhiyun  *
2986*4882a593Smuzhiyun  * RFC 2481 details a specific bit for a sender to send in the header of
2987*4882a593Smuzhiyun  * its next outbound TCP segment to indicate to its peer that it has
2988*4882a593Smuzhiyun  * reduced its congestion window.  This is termed the CWR bit.  For
2989*4882a593Smuzhiyun  * SCTP the same indication is made by including the CWR chunk.
2990*4882a593Smuzhiyun  * This chunk contains one data element, i.e. the TSN number that
2991*4882a593Smuzhiyun  * was sent in the ECNE chunk.  This element represents the lowest
2992*4882a593Smuzhiyun  * TSN number in the datagram that was originally marked with the
2993*4882a593Smuzhiyun  * CE bit.
2994*4882a593Smuzhiyun  *
2995*4882a593Smuzhiyun  * Verification Tag: 8.5 Verification Tag [Normal verification]
2996*4882a593Smuzhiyun  * Inputs
2997*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
2998*4882a593Smuzhiyun  *
2999*4882a593Smuzhiyun  * Outputs
3000*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
3001*4882a593Smuzhiyun  *
3002*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
3003*4882a593Smuzhiyun  */
sctp_sf_do_ecn_cwr(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3004*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
3005*4882a593Smuzhiyun 					 const struct sctp_endpoint *ep,
3006*4882a593Smuzhiyun 					 const struct sctp_association *asoc,
3007*4882a593Smuzhiyun 					 const union sctp_subtype type,
3008*4882a593Smuzhiyun 					 void *arg,
3009*4882a593Smuzhiyun 					 struct sctp_cmd_seq *commands)
3010*4882a593Smuzhiyun {
3011*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3012*4882a593Smuzhiyun 	struct sctp_cwrhdr *cwr;
3013*4882a593Smuzhiyun 	u32 lowest_tsn;
3014*4882a593Smuzhiyun 
3015*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
3016*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3017*4882a593Smuzhiyun 
3018*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
3019*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3020*4882a593Smuzhiyun 						  commands);
3021*4882a593Smuzhiyun 
3022*4882a593Smuzhiyun 	cwr = (struct sctp_cwrhdr *)chunk->skb->data;
3023*4882a593Smuzhiyun 	skb_pull(chunk->skb, sizeof(*cwr));
3024*4882a593Smuzhiyun 
3025*4882a593Smuzhiyun 	lowest_tsn = ntohl(cwr->lowest_tsn);
3026*4882a593Smuzhiyun 
3027*4882a593Smuzhiyun 	/* Does this CWR ack the last sent congestion notification? */
3028*4882a593Smuzhiyun 	if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
3029*4882a593Smuzhiyun 		/* Stop sending ECNE. */
3030*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands,
3031*4882a593Smuzhiyun 				SCTP_CMD_ECN_CWR,
3032*4882a593Smuzhiyun 				SCTP_U32(lowest_tsn));
3033*4882a593Smuzhiyun 	}
3034*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
3035*4882a593Smuzhiyun }
3036*4882a593Smuzhiyun 
3037*4882a593Smuzhiyun /*
3038*4882a593Smuzhiyun  * sctp_sf_do_ecne
3039*4882a593Smuzhiyun  *
3040*4882a593Smuzhiyun  * Section:  Appendix A: Explicit Congestion Notification
3041*4882a593Smuzhiyun  *
3042*4882a593Smuzhiyun  * ECN-Echo
3043*4882a593Smuzhiyun  *
3044*4882a593Smuzhiyun  * RFC 2481 details a specific bit for a receiver to send back in its
3045*4882a593Smuzhiyun  * TCP acknowledgements to notify the sender of the Congestion
3046*4882a593Smuzhiyun  * Experienced (CE) bit having arrived from the network.  For SCTP this
3047*4882a593Smuzhiyun  * same indication is made by including the ECNE chunk.  This chunk
3048*4882a593Smuzhiyun  * contains one data element, i.e. the lowest TSN associated with the IP
3049*4882a593Smuzhiyun  * datagram marked with the CE bit.....
3050*4882a593Smuzhiyun  *
3051*4882a593Smuzhiyun  * Verification Tag: 8.5 Verification Tag [Normal verification]
3052*4882a593Smuzhiyun  * Inputs
3053*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
3054*4882a593Smuzhiyun  *
3055*4882a593Smuzhiyun  * Outputs
3056*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
3057*4882a593Smuzhiyun  *
3058*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
3059*4882a593Smuzhiyun  */
sctp_sf_do_ecne(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3060*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_ecne(struct net *net,
3061*4882a593Smuzhiyun 				      const struct sctp_endpoint *ep,
3062*4882a593Smuzhiyun 				      const struct sctp_association *asoc,
3063*4882a593Smuzhiyun 				      const union sctp_subtype type,
3064*4882a593Smuzhiyun 				      void *arg, struct sctp_cmd_seq *commands)
3065*4882a593Smuzhiyun {
3066*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3067*4882a593Smuzhiyun 	struct sctp_ecnehdr *ecne;
3068*4882a593Smuzhiyun 
3069*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
3070*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3071*4882a593Smuzhiyun 
3072*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
3073*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3074*4882a593Smuzhiyun 						  commands);
3075*4882a593Smuzhiyun 
3076*4882a593Smuzhiyun 	ecne = (struct sctp_ecnehdr *)chunk->skb->data;
3077*4882a593Smuzhiyun 	skb_pull(chunk->skb, sizeof(*ecne));
3078*4882a593Smuzhiyun 
3079*4882a593Smuzhiyun 	/* If this is a newer ECNE than the last CWR packet we sent out */
3080*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
3081*4882a593Smuzhiyun 			SCTP_U32(ntohl(ecne->lowest_tsn)));
3082*4882a593Smuzhiyun 
3083*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
3084*4882a593Smuzhiyun }
3085*4882a593Smuzhiyun 
3086*4882a593Smuzhiyun /*
3087*4882a593Smuzhiyun  * Section: 6.2  Acknowledgement on Reception of DATA Chunks
3088*4882a593Smuzhiyun  *
3089*4882a593Smuzhiyun  * The SCTP endpoint MUST always acknowledge the reception of each valid
3090*4882a593Smuzhiyun  * DATA chunk.
3091*4882a593Smuzhiyun  *
3092*4882a593Smuzhiyun  * The guidelines on delayed acknowledgement algorithm specified in
3093*4882a593Smuzhiyun  * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
3094*4882a593Smuzhiyun  * acknowledgement SHOULD be generated for at least every second packet
3095*4882a593Smuzhiyun  * (not every second DATA chunk) received, and SHOULD be generated within
3096*4882a593Smuzhiyun  * 200 ms of the arrival of any unacknowledged DATA chunk. In some
3097*4882a593Smuzhiyun  * situations it may be beneficial for an SCTP transmitter to be more
3098*4882a593Smuzhiyun  * conservative than the algorithms detailed in this document allow.
3099*4882a593Smuzhiyun  * However, an SCTP transmitter MUST NOT be more aggressive than the
3100*4882a593Smuzhiyun  * following algorithms allow.
3101*4882a593Smuzhiyun  *
3102*4882a593Smuzhiyun  * A SCTP receiver MUST NOT generate more than one SACK for every
3103*4882a593Smuzhiyun  * incoming packet, other than to update the offered window as the
3104*4882a593Smuzhiyun  * receiving application consumes new data.
3105*4882a593Smuzhiyun  *
3106*4882a593Smuzhiyun  * Verification Tag:  8.5 Verification Tag [Normal verification]
3107*4882a593Smuzhiyun  *
3108*4882a593Smuzhiyun  * Inputs
3109*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
3110*4882a593Smuzhiyun  *
3111*4882a593Smuzhiyun  * Outputs
3112*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
3113*4882a593Smuzhiyun  *
3114*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
3115*4882a593Smuzhiyun  */
sctp_sf_eat_data_6_2(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3116*4882a593Smuzhiyun enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
3117*4882a593Smuzhiyun 					   const struct sctp_endpoint *ep,
3118*4882a593Smuzhiyun 					   const struct sctp_association *asoc,
3119*4882a593Smuzhiyun 					   const union sctp_subtype type,
3120*4882a593Smuzhiyun 					   void *arg,
3121*4882a593Smuzhiyun 					   struct sctp_cmd_seq *commands)
3122*4882a593Smuzhiyun {
3123*4882a593Smuzhiyun 	union sctp_arg force = SCTP_NOFORCE();
3124*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3125*4882a593Smuzhiyun 	int error;
3126*4882a593Smuzhiyun 
3127*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc)) {
3128*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3129*4882a593Smuzhiyun 				SCTP_NULL());
3130*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3131*4882a593Smuzhiyun 	}
3132*4882a593Smuzhiyun 
3133*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3134*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3135*4882a593Smuzhiyun 						  commands);
3136*4882a593Smuzhiyun 
3137*4882a593Smuzhiyun 	error = sctp_eat_data(asoc, chunk, commands);
3138*4882a593Smuzhiyun 	switch (error) {
3139*4882a593Smuzhiyun 	case SCTP_IERROR_NO_ERROR:
3140*4882a593Smuzhiyun 		break;
3141*4882a593Smuzhiyun 	case SCTP_IERROR_HIGH_TSN:
3142*4882a593Smuzhiyun 	case SCTP_IERROR_BAD_STREAM:
3143*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3144*4882a593Smuzhiyun 		goto discard_noforce;
3145*4882a593Smuzhiyun 	case SCTP_IERROR_DUP_TSN:
3146*4882a593Smuzhiyun 	case SCTP_IERROR_IGNORE_TSN:
3147*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3148*4882a593Smuzhiyun 		goto discard_force;
3149*4882a593Smuzhiyun 	case SCTP_IERROR_NO_DATA:
3150*4882a593Smuzhiyun 		return SCTP_DISPOSITION_ABORT;
3151*4882a593Smuzhiyun 	case SCTP_IERROR_PROTO_VIOLATION:
3152*4882a593Smuzhiyun 		return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3153*4882a593Smuzhiyun 					       (u8 *)chunk->subh.data_hdr,
3154*4882a593Smuzhiyun 					       sctp_datahdr_len(&asoc->stream));
3155*4882a593Smuzhiyun 	default:
3156*4882a593Smuzhiyun 		BUG();
3157*4882a593Smuzhiyun 	}
3158*4882a593Smuzhiyun 
3159*4882a593Smuzhiyun 	if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3160*4882a593Smuzhiyun 		force = SCTP_FORCE();
3161*4882a593Smuzhiyun 
3162*4882a593Smuzhiyun 	if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3163*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3164*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3165*4882a593Smuzhiyun 	}
3166*4882a593Smuzhiyun 
3167*4882a593Smuzhiyun 	/* If this is the last chunk in a packet, we need to count it
3168*4882a593Smuzhiyun 	 * toward sack generation.  Note that we need to SACK every
3169*4882a593Smuzhiyun 	 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3170*4882a593Smuzhiyun 	 * THEM.  We elect to NOT generate SACK's if the chunk fails
3171*4882a593Smuzhiyun 	 * the verification tag test.
3172*4882a593Smuzhiyun 	 *
3173*4882a593Smuzhiyun 	 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3174*4882a593Smuzhiyun 	 *
3175*4882a593Smuzhiyun 	 * The SCTP endpoint MUST always acknowledge the reception of
3176*4882a593Smuzhiyun 	 * each valid DATA chunk.
3177*4882a593Smuzhiyun 	 *
3178*4882a593Smuzhiyun 	 * The guidelines on delayed acknowledgement algorithm
3179*4882a593Smuzhiyun 	 * specified in  Section 4.2 of [RFC2581] SHOULD be followed.
3180*4882a593Smuzhiyun 	 * Specifically, an acknowledgement SHOULD be generated for at
3181*4882a593Smuzhiyun 	 * least every second packet (not every second DATA chunk)
3182*4882a593Smuzhiyun 	 * received, and SHOULD be generated within 200 ms of the
3183*4882a593Smuzhiyun 	 * arrival of any unacknowledged DATA chunk.  In some
3184*4882a593Smuzhiyun 	 * situations it may be beneficial for an SCTP transmitter to
3185*4882a593Smuzhiyun 	 * be more conservative than the algorithms detailed in this
3186*4882a593Smuzhiyun 	 * document allow. However, an SCTP transmitter MUST NOT be
3187*4882a593Smuzhiyun 	 * more aggressive than the following algorithms allow.
3188*4882a593Smuzhiyun 	 */
3189*4882a593Smuzhiyun 	if (chunk->end_of_packet)
3190*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3191*4882a593Smuzhiyun 
3192*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
3193*4882a593Smuzhiyun 
3194*4882a593Smuzhiyun discard_force:
3195*4882a593Smuzhiyun 	/* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3196*4882a593Smuzhiyun 	 *
3197*4882a593Smuzhiyun 	 * When a packet arrives with duplicate DATA chunk(s) and with
3198*4882a593Smuzhiyun 	 * no new DATA chunk(s), the endpoint MUST immediately send a
3199*4882a593Smuzhiyun 	 * SACK with no delay.  If a packet arrives with duplicate
3200*4882a593Smuzhiyun 	 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3201*4882a593Smuzhiyun 	 * MAY immediately send a SACK.  Normally receipt of duplicate
3202*4882a593Smuzhiyun 	 * DATA chunks will occur when the original SACK chunk was lost
3203*4882a593Smuzhiyun 	 * and the peer's RTO has expired.  The duplicate TSN number(s)
3204*4882a593Smuzhiyun 	 * SHOULD be reported in the SACK as duplicate.
3205*4882a593Smuzhiyun 	 */
3206*4882a593Smuzhiyun 	/* In our case, we split the MAY SACK advice up whether or not
3207*4882a593Smuzhiyun 	 * the last chunk is a duplicate.'
3208*4882a593Smuzhiyun 	 */
3209*4882a593Smuzhiyun 	if (chunk->end_of_packet)
3210*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3211*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DISCARD;
3212*4882a593Smuzhiyun 
3213*4882a593Smuzhiyun discard_noforce:
3214*4882a593Smuzhiyun 	if (chunk->end_of_packet)
3215*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3216*4882a593Smuzhiyun 
3217*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DISCARD;
3218*4882a593Smuzhiyun }
3219*4882a593Smuzhiyun 
3220*4882a593Smuzhiyun /*
3221*4882a593Smuzhiyun  * sctp_sf_eat_data_fast_4_4
3222*4882a593Smuzhiyun  *
3223*4882a593Smuzhiyun  * Section: 4 (4)
3224*4882a593Smuzhiyun  * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3225*4882a593Smuzhiyun  *    DATA chunks without delay.
3226*4882a593Smuzhiyun  *
3227*4882a593Smuzhiyun  * Verification Tag:  8.5 Verification Tag [Normal verification]
3228*4882a593Smuzhiyun  * Inputs
3229*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
3230*4882a593Smuzhiyun  *
3231*4882a593Smuzhiyun  * Outputs
3232*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
3233*4882a593Smuzhiyun  *
3234*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
3235*4882a593Smuzhiyun  */
sctp_sf_eat_data_fast_4_4(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3236*4882a593Smuzhiyun enum sctp_disposition sctp_sf_eat_data_fast_4_4(
3237*4882a593Smuzhiyun 					struct net *net,
3238*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
3239*4882a593Smuzhiyun 					const struct sctp_association *asoc,
3240*4882a593Smuzhiyun 					const union sctp_subtype type,
3241*4882a593Smuzhiyun 					void *arg,
3242*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
3243*4882a593Smuzhiyun {
3244*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3245*4882a593Smuzhiyun 	int error;
3246*4882a593Smuzhiyun 
3247*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc)) {
3248*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3249*4882a593Smuzhiyun 				SCTP_NULL());
3250*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3251*4882a593Smuzhiyun 	}
3252*4882a593Smuzhiyun 
3253*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3254*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3255*4882a593Smuzhiyun 						  commands);
3256*4882a593Smuzhiyun 
3257*4882a593Smuzhiyun 	error = sctp_eat_data(asoc, chunk, commands);
3258*4882a593Smuzhiyun 	switch (error) {
3259*4882a593Smuzhiyun 	case SCTP_IERROR_NO_ERROR:
3260*4882a593Smuzhiyun 	case SCTP_IERROR_HIGH_TSN:
3261*4882a593Smuzhiyun 	case SCTP_IERROR_DUP_TSN:
3262*4882a593Smuzhiyun 	case SCTP_IERROR_IGNORE_TSN:
3263*4882a593Smuzhiyun 	case SCTP_IERROR_BAD_STREAM:
3264*4882a593Smuzhiyun 		break;
3265*4882a593Smuzhiyun 	case SCTP_IERROR_NO_DATA:
3266*4882a593Smuzhiyun 		return SCTP_DISPOSITION_ABORT;
3267*4882a593Smuzhiyun 	case SCTP_IERROR_PROTO_VIOLATION:
3268*4882a593Smuzhiyun 		return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3269*4882a593Smuzhiyun 					       (u8 *)chunk->subh.data_hdr,
3270*4882a593Smuzhiyun 					       sctp_datahdr_len(&asoc->stream));
3271*4882a593Smuzhiyun 	default:
3272*4882a593Smuzhiyun 		BUG();
3273*4882a593Smuzhiyun 	}
3274*4882a593Smuzhiyun 
3275*4882a593Smuzhiyun 	/* Go a head and force a SACK, since we are shutting down. */
3276*4882a593Smuzhiyun 
3277*4882a593Smuzhiyun 	/* Implementor's Guide.
3278*4882a593Smuzhiyun 	 *
3279*4882a593Smuzhiyun 	 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3280*4882a593Smuzhiyun 	 * respond to each received packet containing one or more DATA chunk(s)
3281*4882a593Smuzhiyun 	 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3282*4882a593Smuzhiyun 	 */
3283*4882a593Smuzhiyun 	if (chunk->end_of_packet) {
3284*4882a593Smuzhiyun 		/* We must delay the chunk creation since the cumulative
3285*4882a593Smuzhiyun 		 * TSN has not been updated yet.
3286*4882a593Smuzhiyun 		 */
3287*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3288*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3289*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3290*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3291*4882a593Smuzhiyun 	}
3292*4882a593Smuzhiyun 
3293*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
3294*4882a593Smuzhiyun }
3295*4882a593Smuzhiyun 
3296*4882a593Smuzhiyun /*
3297*4882a593Smuzhiyun  * Section: 6.2  Processing a Received SACK
3298*4882a593Smuzhiyun  * D) Any time a SACK arrives, the endpoint performs the following:
3299*4882a593Smuzhiyun  *
3300*4882a593Smuzhiyun  *     i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3301*4882a593Smuzhiyun  *     then drop the SACK.   Since Cumulative TSN Ack is monotonically
3302*4882a593Smuzhiyun  *     increasing, a SACK whose Cumulative TSN Ack is less than the
3303*4882a593Smuzhiyun  *     Cumulative TSN Ack Point indicates an out-of-order SACK.
3304*4882a593Smuzhiyun  *
3305*4882a593Smuzhiyun  *     ii) Set rwnd equal to the newly received a_rwnd minus the number
3306*4882a593Smuzhiyun  *     of bytes still outstanding after processing the Cumulative TSN Ack
3307*4882a593Smuzhiyun  *     and the Gap Ack Blocks.
3308*4882a593Smuzhiyun  *
3309*4882a593Smuzhiyun  *     iii) If the SACK is missing a TSN that was previously
3310*4882a593Smuzhiyun  *     acknowledged via a Gap Ack Block (e.g., the data receiver
3311*4882a593Smuzhiyun  *     reneged on the data), then mark the corresponding DATA chunk
3312*4882a593Smuzhiyun  *     as available for retransmit:  Mark it as missing for fast
3313*4882a593Smuzhiyun  *     retransmit as described in Section 7.2.4 and if no retransmit
3314*4882a593Smuzhiyun  *     timer is running for the destination address to which the DATA
3315*4882a593Smuzhiyun  *     chunk was originally transmitted, then T3-rtx is started for
3316*4882a593Smuzhiyun  *     that destination address.
3317*4882a593Smuzhiyun  *
3318*4882a593Smuzhiyun  * Verification Tag:  8.5 Verification Tag [Normal verification]
3319*4882a593Smuzhiyun  *
3320*4882a593Smuzhiyun  * Inputs
3321*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
3322*4882a593Smuzhiyun  *
3323*4882a593Smuzhiyun  * Outputs
3324*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
3325*4882a593Smuzhiyun  *
3326*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
3327*4882a593Smuzhiyun  */
sctp_sf_eat_sack_6_2(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3328*4882a593Smuzhiyun enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
3329*4882a593Smuzhiyun 					   const struct sctp_endpoint *ep,
3330*4882a593Smuzhiyun 					   const struct sctp_association *asoc,
3331*4882a593Smuzhiyun 					   const union sctp_subtype type,
3332*4882a593Smuzhiyun 					   void *arg,
3333*4882a593Smuzhiyun 					   struct sctp_cmd_seq *commands)
3334*4882a593Smuzhiyun {
3335*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3336*4882a593Smuzhiyun 	struct sctp_sackhdr *sackh;
3337*4882a593Smuzhiyun 	__u32 ctsn;
3338*4882a593Smuzhiyun 
3339*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
3340*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3341*4882a593Smuzhiyun 
3342*4882a593Smuzhiyun 	/* Make sure that the SACK chunk has a valid length. */
3343*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
3344*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3345*4882a593Smuzhiyun 						  commands);
3346*4882a593Smuzhiyun 
3347*4882a593Smuzhiyun 	/* Pull the SACK chunk from the data buffer */
3348*4882a593Smuzhiyun 	sackh = sctp_sm_pull_sack(chunk);
3349*4882a593Smuzhiyun 	/* Was this a bogus SACK? */
3350*4882a593Smuzhiyun 	if (!sackh)
3351*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3352*4882a593Smuzhiyun 	chunk->subh.sack_hdr = sackh;
3353*4882a593Smuzhiyun 	ctsn = ntohl(sackh->cum_tsn_ack);
3354*4882a593Smuzhiyun 
3355*4882a593Smuzhiyun 	/* If Cumulative TSN Ack beyond the max tsn currently
3356*4882a593Smuzhiyun 	 * send, terminating the association and respond to the
3357*4882a593Smuzhiyun 	 * sender with an ABORT.
3358*4882a593Smuzhiyun 	 */
3359*4882a593Smuzhiyun 	if (TSN_lte(asoc->next_tsn, ctsn))
3360*4882a593Smuzhiyun 		return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3361*4882a593Smuzhiyun 
3362*4882a593Smuzhiyun 	trace_sctp_probe(ep, asoc, chunk);
3363*4882a593Smuzhiyun 
3364*4882a593Smuzhiyun 	/* i) If Cumulative TSN Ack is less than the Cumulative TSN
3365*4882a593Smuzhiyun 	 *     Ack Point, then drop the SACK.  Since Cumulative TSN
3366*4882a593Smuzhiyun 	 *     Ack is monotonically increasing, a SACK whose
3367*4882a593Smuzhiyun 	 *     Cumulative TSN Ack is less than the Cumulative TSN Ack
3368*4882a593Smuzhiyun 	 *     Point indicates an out-of-order SACK.
3369*4882a593Smuzhiyun 	 */
3370*4882a593Smuzhiyun 	if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3371*4882a593Smuzhiyun 		pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3372*4882a593Smuzhiyun 			 asoc->ctsn_ack_point);
3373*4882a593Smuzhiyun 
3374*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
3375*4882a593Smuzhiyun 	}
3376*4882a593Smuzhiyun 
3377*4882a593Smuzhiyun 	/* Return this SACK for further processing.  */
3378*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3379*4882a593Smuzhiyun 
3380*4882a593Smuzhiyun 	/* Note: We do the rest of the work on the PROCESS_SACK
3381*4882a593Smuzhiyun 	 * sideeffect.
3382*4882a593Smuzhiyun 	 */
3383*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
3384*4882a593Smuzhiyun }
3385*4882a593Smuzhiyun 
3386*4882a593Smuzhiyun /*
3387*4882a593Smuzhiyun  * Generate an ABORT in response to a packet.
3388*4882a593Smuzhiyun  *
3389*4882a593Smuzhiyun  * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3390*4882a593Smuzhiyun  *
3391*4882a593Smuzhiyun  * 8) The receiver should respond to the sender of the OOTB packet with
3392*4882a593Smuzhiyun  *    an ABORT.  When sending the ABORT, the receiver of the OOTB packet
3393*4882a593Smuzhiyun  *    MUST fill in the Verification Tag field of the outbound packet
3394*4882a593Smuzhiyun  *    with the value found in the Verification Tag field of the OOTB
3395*4882a593Smuzhiyun  *    packet and set the T-bit in the Chunk Flags to indicate that the
3396*4882a593Smuzhiyun  *    Verification Tag is reflected.  After sending this ABORT, the
3397*4882a593Smuzhiyun  *    receiver of the OOTB packet shall discard the OOTB packet and take
3398*4882a593Smuzhiyun  *    no further action.
3399*4882a593Smuzhiyun  *
3400*4882a593Smuzhiyun  * Verification Tag:
3401*4882a593Smuzhiyun  *
3402*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
3403*4882a593Smuzhiyun */
sctp_sf_tabort_8_4_8(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3404*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_tabort_8_4_8(
3405*4882a593Smuzhiyun 					struct net *net,
3406*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
3407*4882a593Smuzhiyun 					const struct sctp_association *asoc,
3408*4882a593Smuzhiyun 					const union sctp_subtype type,
3409*4882a593Smuzhiyun 					void *arg,
3410*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
3411*4882a593Smuzhiyun {
3412*4882a593Smuzhiyun 	struct sctp_packet *packet = NULL;
3413*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3414*4882a593Smuzhiyun 	struct sctp_chunk *abort;
3415*4882a593Smuzhiyun 
3416*4882a593Smuzhiyun 	packet = sctp_ootb_pkt_new(net, asoc, chunk);
3417*4882a593Smuzhiyun 	if (!packet)
3418*4882a593Smuzhiyun 		return SCTP_DISPOSITION_NOMEM;
3419*4882a593Smuzhiyun 
3420*4882a593Smuzhiyun 	/* Make an ABORT. The T bit will be set if the asoc
3421*4882a593Smuzhiyun 	 * is NULL.
3422*4882a593Smuzhiyun 	 */
3423*4882a593Smuzhiyun 	abort = sctp_make_abort(asoc, chunk, 0);
3424*4882a593Smuzhiyun 	if (!abort) {
3425*4882a593Smuzhiyun 		sctp_ootb_pkt_free(packet);
3426*4882a593Smuzhiyun 		return SCTP_DISPOSITION_NOMEM;
3427*4882a593Smuzhiyun 	}
3428*4882a593Smuzhiyun 
3429*4882a593Smuzhiyun 	/* Reflect vtag if T-Bit is set */
3430*4882a593Smuzhiyun 	if (sctp_test_T_bit(abort))
3431*4882a593Smuzhiyun 		packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3432*4882a593Smuzhiyun 
3433*4882a593Smuzhiyun 	/* Set the skb to the belonging sock for accounting.  */
3434*4882a593Smuzhiyun 	abort->skb->sk = ep->base.sk;
3435*4882a593Smuzhiyun 
3436*4882a593Smuzhiyun 	sctp_packet_append_chunk(packet, abort);
3437*4882a593Smuzhiyun 
3438*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3439*4882a593Smuzhiyun 			SCTP_PACKET(packet));
3440*4882a593Smuzhiyun 
3441*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3442*4882a593Smuzhiyun 
3443*4882a593Smuzhiyun 	sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3444*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
3445*4882a593Smuzhiyun }
3446*4882a593Smuzhiyun 
3447*4882a593Smuzhiyun /*
3448*4882a593Smuzhiyun  * Received an ERROR chunk from peer.  Generate SCTP_REMOTE_ERROR
3449*4882a593Smuzhiyun  * event as ULP notification for each cause included in the chunk.
3450*4882a593Smuzhiyun  *
3451*4882a593Smuzhiyun  * API 5.3.1.3 - SCTP_REMOTE_ERROR
3452*4882a593Smuzhiyun  *
3453*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
3454*4882a593Smuzhiyun */
sctp_sf_operr_notify(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3455*4882a593Smuzhiyun enum sctp_disposition sctp_sf_operr_notify(struct net *net,
3456*4882a593Smuzhiyun 					   const struct sctp_endpoint *ep,
3457*4882a593Smuzhiyun 					   const struct sctp_association *asoc,
3458*4882a593Smuzhiyun 					   const union sctp_subtype type,
3459*4882a593Smuzhiyun 					   void *arg,
3460*4882a593Smuzhiyun 					   struct sctp_cmd_seq *commands)
3461*4882a593Smuzhiyun {
3462*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3463*4882a593Smuzhiyun 	struct sctp_errhdr *err;
3464*4882a593Smuzhiyun 
3465*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
3466*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3467*4882a593Smuzhiyun 
3468*4882a593Smuzhiyun 	/* Make sure that the ERROR chunk has a valid length. */
3469*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
3470*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3471*4882a593Smuzhiyun 						  commands);
3472*4882a593Smuzhiyun 	sctp_walk_errors(err, chunk->chunk_hdr);
3473*4882a593Smuzhiyun 	if ((void *)err != (void *)chunk->chunk_end)
3474*4882a593Smuzhiyun 		return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3475*4882a593Smuzhiyun 						  (void *)err, commands);
3476*4882a593Smuzhiyun 
3477*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3478*4882a593Smuzhiyun 			SCTP_CHUNK(chunk));
3479*4882a593Smuzhiyun 
3480*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
3481*4882a593Smuzhiyun }
3482*4882a593Smuzhiyun 
3483*4882a593Smuzhiyun /*
3484*4882a593Smuzhiyun  * Process an inbound SHUTDOWN ACK.
3485*4882a593Smuzhiyun  *
3486*4882a593Smuzhiyun  * From Section 9.2:
3487*4882a593Smuzhiyun  * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3488*4882a593Smuzhiyun  * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3489*4882a593Smuzhiyun  * peer, and remove all record of the association.
3490*4882a593Smuzhiyun  *
3491*4882a593Smuzhiyun  * The return value is the disposition.
3492*4882a593Smuzhiyun  */
sctp_sf_do_9_2_final(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3493*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
3494*4882a593Smuzhiyun 					   const struct sctp_endpoint *ep,
3495*4882a593Smuzhiyun 					   const struct sctp_association *asoc,
3496*4882a593Smuzhiyun 					   const union sctp_subtype type,
3497*4882a593Smuzhiyun 					   void *arg,
3498*4882a593Smuzhiyun 					   struct sctp_cmd_seq *commands)
3499*4882a593Smuzhiyun {
3500*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3501*4882a593Smuzhiyun 	struct sctp_chunk *reply;
3502*4882a593Smuzhiyun 	struct sctp_ulpevent *ev;
3503*4882a593Smuzhiyun 
3504*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
3505*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3506*4882a593Smuzhiyun 
3507*4882a593Smuzhiyun 	/* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3508*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3509*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3510*4882a593Smuzhiyun 						  commands);
3511*4882a593Smuzhiyun 	/* 10.2 H) SHUTDOWN COMPLETE notification
3512*4882a593Smuzhiyun 	 *
3513*4882a593Smuzhiyun 	 * When SCTP completes the shutdown procedures (section 9.2) this
3514*4882a593Smuzhiyun 	 * notification is passed to the upper layer.
3515*4882a593Smuzhiyun 	 */
3516*4882a593Smuzhiyun 	ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3517*4882a593Smuzhiyun 					     0, 0, 0, NULL, GFP_ATOMIC);
3518*4882a593Smuzhiyun 	if (!ev)
3519*4882a593Smuzhiyun 		goto nomem;
3520*4882a593Smuzhiyun 
3521*4882a593Smuzhiyun 	/* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3522*4882a593Smuzhiyun 	reply = sctp_make_shutdown_complete(asoc, chunk);
3523*4882a593Smuzhiyun 	if (!reply)
3524*4882a593Smuzhiyun 		goto nomem_chunk;
3525*4882a593Smuzhiyun 
3526*4882a593Smuzhiyun 	/* Do all the commands now (after allocation), so that we
3527*4882a593Smuzhiyun 	 * have consistent state if memory allocation failes
3528*4882a593Smuzhiyun 	 */
3529*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3530*4882a593Smuzhiyun 
3531*4882a593Smuzhiyun 	/* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3532*4882a593Smuzhiyun 	 * stop the T2-shutdown timer,
3533*4882a593Smuzhiyun 	 */
3534*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3535*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3536*4882a593Smuzhiyun 
3537*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3538*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3539*4882a593Smuzhiyun 
3540*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3541*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_CLOSED));
3542*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3543*4882a593Smuzhiyun 	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3544*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3545*4882a593Smuzhiyun 
3546*4882a593Smuzhiyun 	/* ...and remove all record of the association. */
3547*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3548*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DELETE_TCB;
3549*4882a593Smuzhiyun 
3550*4882a593Smuzhiyun nomem_chunk:
3551*4882a593Smuzhiyun 	sctp_ulpevent_free(ev);
3552*4882a593Smuzhiyun nomem:
3553*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
3554*4882a593Smuzhiyun }
3555*4882a593Smuzhiyun 
3556*4882a593Smuzhiyun /*
3557*4882a593Smuzhiyun  * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3558*4882a593Smuzhiyun  *
3559*4882a593Smuzhiyun  * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3560*4882a593Smuzhiyun  *    respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3561*4882a593Smuzhiyun  *    When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3562*4882a593Smuzhiyun  *    packet must fill in the Verification Tag field of the outbound
3563*4882a593Smuzhiyun  *    packet with the Verification Tag received in the SHUTDOWN ACK and
3564*4882a593Smuzhiyun  *    set the T-bit in the Chunk Flags to indicate that the Verification
3565*4882a593Smuzhiyun  *    Tag is reflected.
3566*4882a593Smuzhiyun  *
3567*4882a593Smuzhiyun  * 8) The receiver should respond to the sender of the OOTB packet with
3568*4882a593Smuzhiyun  *    an ABORT.  When sending the ABORT, the receiver of the OOTB packet
3569*4882a593Smuzhiyun  *    MUST fill in the Verification Tag field of the outbound packet
3570*4882a593Smuzhiyun  *    with the value found in the Verification Tag field of the OOTB
3571*4882a593Smuzhiyun  *    packet and set the T-bit in the Chunk Flags to indicate that the
3572*4882a593Smuzhiyun  *    Verification Tag is reflected.  After sending this ABORT, the
3573*4882a593Smuzhiyun  *    receiver of the OOTB packet shall discard the OOTB packet and take
3574*4882a593Smuzhiyun  *    no further action.
3575*4882a593Smuzhiyun  */
sctp_sf_ootb(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3576*4882a593Smuzhiyun enum sctp_disposition sctp_sf_ootb(struct net *net,
3577*4882a593Smuzhiyun 				   const struct sctp_endpoint *ep,
3578*4882a593Smuzhiyun 				   const struct sctp_association *asoc,
3579*4882a593Smuzhiyun 				   const union sctp_subtype type,
3580*4882a593Smuzhiyun 				   void *arg, struct sctp_cmd_seq *commands)
3581*4882a593Smuzhiyun {
3582*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3583*4882a593Smuzhiyun 	struct sk_buff *skb = chunk->skb;
3584*4882a593Smuzhiyun 	struct sctp_chunkhdr *ch;
3585*4882a593Smuzhiyun 	struct sctp_errhdr *err;
3586*4882a593Smuzhiyun 	int ootb_cookie_ack = 0;
3587*4882a593Smuzhiyun 	int ootb_shut_ack = 0;
3588*4882a593Smuzhiyun 	__u8 *ch_end;
3589*4882a593Smuzhiyun 
3590*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3591*4882a593Smuzhiyun 
3592*4882a593Smuzhiyun 	if (asoc && !sctp_vtag_verify(chunk, asoc))
3593*4882a593Smuzhiyun 		asoc = NULL;
3594*4882a593Smuzhiyun 
3595*4882a593Smuzhiyun 	ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
3596*4882a593Smuzhiyun 	do {
3597*4882a593Smuzhiyun 		/* Report violation if the chunk is less then minimal */
3598*4882a593Smuzhiyun 		if (ntohs(ch->length) < sizeof(*ch))
3599*4882a593Smuzhiyun 			return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3600*4882a593Smuzhiyun 						  commands);
3601*4882a593Smuzhiyun 
3602*4882a593Smuzhiyun 		/* Report violation if chunk len overflows */
3603*4882a593Smuzhiyun 		ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3604*4882a593Smuzhiyun 		if (ch_end > skb_tail_pointer(skb))
3605*4882a593Smuzhiyun 			return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3606*4882a593Smuzhiyun 						  commands);
3607*4882a593Smuzhiyun 
3608*4882a593Smuzhiyun 		/* Now that we know we at least have a chunk header,
3609*4882a593Smuzhiyun 		 * do things that are type appropriate.
3610*4882a593Smuzhiyun 		 */
3611*4882a593Smuzhiyun 		if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3612*4882a593Smuzhiyun 			ootb_shut_ack = 1;
3613*4882a593Smuzhiyun 
3614*4882a593Smuzhiyun 		/* RFC 2960, Section 3.3.7
3615*4882a593Smuzhiyun 		 *   Moreover, under any circumstances, an endpoint that
3616*4882a593Smuzhiyun 		 *   receives an ABORT  MUST NOT respond to that ABORT by
3617*4882a593Smuzhiyun 		 *   sending an ABORT of its own.
3618*4882a593Smuzhiyun 		 */
3619*4882a593Smuzhiyun 		if (SCTP_CID_ABORT == ch->type)
3620*4882a593Smuzhiyun 			return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3621*4882a593Smuzhiyun 
3622*4882a593Smuzhiyun 		/* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3623*4882a593Smuzhiyun 		 * or a COOKIE ACK the SCTP Packet should be silently
3624*4882a593Smuzhiyun 		 * discarded.
3625*4882a593Smuzhiyun 		 */
3626*4882a593Smuzhiyun 
3627*4882a593Smuzhiyun 		if (SCTP_CID_COOKIE_ACK == ch->type)
3628*4882a593Smuzhiyun 			ootb_cookie_ack = 1;
3629*4882a593Smuzhiyun 
3630*4882a593Smuzhiyun 		if (SCTP_CID_ERROR == ch->type) {
3631*4882a593Smuzhiyun 			sctp_walk_errors(err, ch) {
3632*4882a593Smuzhiyun 				if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3633*4882a593Smuzhiyun 					ootb_cookie_ack = 1;
3634*4882a593Smuzhiyun 					break;
3635*4882a593Smuzhiyun 				}
3636*4882a593Smuzhiyun 			}
3637*4882a593Smuzhiyun 		}
3638*4882a593Smuzhiyun 
3639*4882a593Smuzhiyun 		ch = (struct sctp_chunkhdr *)ch_end;
3640*4882a593Smuzhiyun 	} while (ch_end < skb_tail_pointer(skb));
3641*4882a593Smuzhiyun 
3642*4882a593Smuzhiyun 	if (ootb_shut_ack)
3643*4882a593Smuzhiyun 		return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3644*4882a593Smuzhiyun 	else if (ootb_cookie_ack)
3645*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3646*4882a593Smuzhiyun 	else
3647*4882a593Smuzhiyun 		return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3648*4882a593Smuzhiyun }
3649*4882a593Smuzhiyun 
3650*4882a593Smuzhiyun /*
3651*4882a593Smuzhiyun  * Handle an "Out of the blue" SHUTDOWN ACK.
3652*4882a593Smuzhiyun  *
3653*4882a593Smuzhiyun  * Section: 8.4 5, sctpimpguide 2.41.
3654*4882a593Smuzhiyun  *
3655*4882a593Smuzhiyun  * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3656*4882a593Smuzhiyun  *    respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3657*4882a593Smuzhiyun  *    When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3658*4882a593Smuzhiyun  *    packet must fill in the Verification Tag field of the outbound
3659*4882a593Smuzhiyun  *    packet with the Verification Tag received in the SHUTDOWN ACK and
3660*4882a593Smuzhiyun  *    set the T-bit in the Chunk Flags to indicate that the Verification
3661*4882a593Smuzhiyun  *    Tag is reflected.
3662*4882a593Smuzhiyun  *
3663*4882a593Smuzhiyun  * Inputs
3664*4882a593Smuzhiyun  * (endpoint, asoc, type, arg, commands)
3665*4882a593Smuzhiyun  *
3666*4882a593Smuzhiyun  * Outputs
3667*4882a593Smuzhiyun  * (enum sctp_disposition)
3668*4882a593Smuzhiyun  *
3669*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
3670*4882a593Smuzhiyun  */
sctp_sf_shut_8_4_5(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3671*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_shut_8_4_5(
3672*4882a593Smuzhiyun 					struct net *net,
3673*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
3674*4882a593Smuzhiyun 					const struct sctp_association *asoc,
3675*4882a593Smuzhiyun 					const union sctp_subtype type,
3676*4882a593Smuzhiyun 					void *arg,
3677*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
3678*4882a593Smuzhiyun {
3679*4882a593Smuzhiyun 	struct sctp_packet *packet = NULL;
3680*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3681*4882a593Smuzhiyun 	struct sctp_chunk *shut;
3682*4882a593Smuzhiyun 
3683*4882a593Smuzhiyun 	packet = sctp_ootb_pkt_new(net, asoc, chunk);
3684*4882a593Smuzhiyun 	if (!packet)
3685*4882a593Smuzhiyun 		return SCTP_DISPOSITION_NOMEM;
3686*4882a593Smuzhiyun 
3687*4882a593Smuzhiyun 	/* Make an SHUTDOWN_COMPLETE.
3688*4882a593Smuzhiyun 	 * The T bit will be set if the asoc is NULL.
3689*4882a593Smuzhiyun 	 */
3690*4882a593Smuzhiyun 	shut = sctp_make_shutdown_complete(asoc, chunk);
3691*4882a593Smuzhiyun 	if (!shut) {
3692*4882a593Smuzhiyun 		sctp_ootb_pkt_free(packet);
3693*4882a593Smuzhiyun 		return SCTP_DISPOSITION_NOMEM;
3694*4882a593Smuzhiyun 	}
3695*4882a593Smuzhiyun 
3696*4882a593Smuzhiyun 	/* Reflect vtag if T-Bit is set */
3697*4882a593Smuzhiyun 	if (sctp_test_T_bit(shut))
3698*4882a593Smuzhiyun 		packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3699*4882a593Smuzhiyun 
3700*4882a593Smuzhiyun 	/* Set the skb to the belonging sock for accounting.  */
3701*4882a593Smuzhiyun 	shut->skb->sk = ep->base.sk;
3702*4882a593Smuzhiyun 
3703*4882a593Smuzhiyun 	sctp_packet_append_chunk(packet, shut);
3704*4882a593Smuzhiyun 
3705*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3706*4882a593Smuzhiyun 			SCTP_PACKET(packet));
3707*4882a593Smuzhiyun 
3708*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3709*4882a593Smuzhiyun 
3710*4882a593Smuzhiyun 	/* We need to discard the rest of the packet to prevent
3711*4882a593Smuzhiyun 	 * potential bomming attacks from additional bundled chunks.
3712*4882a593Smuzhiyun 	 * This is documented in SCTP Threats ID.
3713*4882a593Smuzhiyun 	 */
3714*4882a593Smuzhiyun 	return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3715*4882a593Smuzhiyun }
3716*4882a593Smuzhiyun 
3717*4882a593Smuzhiyun /*
3718*4882a593Smuzhiyun  * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3719*4882a593Smuzhiyun  *
3720*4882a593Smuzhiyun  * Verification Tag:  8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3721*4882a593Smuzhiyun  *   If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3722*4882a593Smuzhiyun  *   procedures in section 8.4 SHOULD be followed, in other words it
3723*4882a593Smuzhiyun  *   should be treated as an Out Of The Blue packet.
3724*4882a593Smuzhiyun  *   [This means that we do NOT check the Verification Tag on these
3725*4882a593Smuzhiyun  *   chunks. --piggy ]
3726*4882a593Smuzhiyun  *
3727*4882a593Smuzhiyun  */
sctp_sf_do_8_5_1_E_sa(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3728*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
3729*4882a593Smuzhiyun 					    const struct sctp_endpoint *ep,
3730*4882a593Smuzhiyun 					    const struct sctp_association *asoc,
3731*4882a593Smuzhiyun 					    const union sctp_subtype type,
3732*4882a593Smuzhiyun 					    void *arg,
3733*4882a593Smuzhiyun 					    struct sctp_cmd_seq *commands)
3734*4882a593Smuzhiyun {
3735*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3736*4882a593Smuzhiyun 
3737*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
3738*4882a593Smuzhiyun 		asoc = NULL;
3739*4882a593Smuzhiyun 
3740*4882a593Smuzhiyun 	/* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3741*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3742*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3743*4882a593Smuzhiyun 						  commands);
3744*4882a593Smuzhiyun 
3745*4882a593Smuzhiyun 	/* Although we do have an association in this case, it corresponds
3746*4882a593Smuzhiyun 	 * to a restarted association. So the packet is treated as an OOTB
3747*4882a593Smuzhiyun 	 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3748*4882a593Smuzhiyun 	 * called with a NULL association.
3749*4882a593Smuzhiyun 	 */
3750*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3751*4882a593Smuzhiyun 
3752*4882a593Smuzhiyun 	return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3753*4882a593Smuzhiyun }
3754*4882a593Smuzhiyun 
3755*4882a593Smuzhiyun /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk.  */
sctp_sf_do_asconf(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3756*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_asconf(struct net *net,
3757*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
3758*4882a593Smuzhiyun 					const struct sctp_association *asoc,
3759*4882a593Smuzhiyun 					const union sctp_subtype type,
3760*4882a593Smuzhiyun 					void *arg,
3761*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
3762*4882a593Smuzhiyun {
3763*4882a593Smuzhiyun 	struct sctp_paramhdr *err_param = NULL;
3764*4882a593Smuzhiyun 	struct sctp_chunk *asconf_ack = NULL;
3765*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
3766*4882a593Smuzhiyun 	struct sctp_addiphdr *hdr;
3767*4882a593Smuzhiyun 	__u32 serial;
3768*4882a593Smuzhiyun 
3769*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc)) {
3770*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3771*4882a593Smuzhiyun 				SCTP_NULL());
3772*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3773*4882a593Smuzhiyun 	}
3774*4882a593Smuzhiyun 
3775*4882a593Smuzhiyun 	/* Make sure that the ASCONF ADDIP chunk has a valid length.  */
3776*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
3777*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3778*4882a593Smuzhiyun 						  commands);
3779*4882a593Smuzhiyun 
3780*4882a593Smuzhiyun 	/* ADD-IP: Section 4.1.1
3781*4882a593Smuzhiyun 	 * This chunk MUST be sent in an authenticated way by using
3782*4882a593Smuzhiyun 	 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3783*4882a593Smuzhiyun 	 * is received unauthenticated it MUST be silently discarded as
3784*4882a593Smuzhiyun 	 * described in [I-D.ietf-tsvwg-sctp-auth].
3785*4882a593Smuzhiyun 	 */
3786*4882a593Smuzhiyun 	if (!asoc->peer.asconf_capable ||
3787*4882a593Smuzhiyun 	    (!net->sctp.addip_noauth && !chunk->auth))
3788*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3789*4882a593Smuzhiyun 
3790*4882a593Smuzhiyun 	hdr = (struct sctp_addiphdr *)chunk->skb->data;
3791*4882a593Smuzhiyun 	serial = ntohl(hdr->serial);
3792*4882a593Smuzhiyun 
3793*4882a593Smuzhiyun 	/* Verify the ASCONF chunk before processing it. */
3794*4882a593Smuzhiyun 	if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3795*4882a593Smuzhiyun 		return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3796*4882a593Smuzhiyun 						  (void *)err_param, commands);
3797*4882a593Smuzhiyun 
3798*4882a593Smuzhiyun 	/* ADDIP 5.2 E1) Compare the value of the serial number to the value
3799*4882a593Smuzhiyun 	 * the endpoint stored in a new association variable
3800*4882a593Smuzhiyun 	 * 'Peer-Serial-Number'.
3801*4882a593Smuzhiyun 	 */
3802*4882a593Smuzhiyun 	if (serial == asoc->peer.addip_serial + 1) {
3803*4882a593Smuzhiyun 		/* If this is the first instance of ASCONF in the packet,
3804*4882a593Smuzhiyun 		 * we can clean our old ASCONF-ACKs.
3805*4882a593Smuzhiyun 		 */
3806*4882a593Smuzhiyun 		if (!chunk->has_asconf)
3807*4882a593Smuzhiyun 			sctp_assoc_clean_asconf_ack_cache(asoc);
3808*4882a593Smuzhiyun 
3809*4882a593Smuzhiyun 		/* ADDIP 5.2 E4) When the Sequence Number matches the next one
3810*4882a593Smuzhiyun 		 * expected, process the ASCONF as described below and after
3811*4882a593Smuzhiyun 		 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3812*4882a593Smuzhiyun 		 * the response packet and cache a copy of it (in the event it
3813*4882a593Smuzhiyun 		 * later needs to be retransmitted).
3814*4882a593Smuzhiyun 		 *
3815*4882a593Smuzhiyun 		 * Essentially, do V1-V5.
3816*4882a593Smuzhiyun 		 */
3817*4882a593Smuzhiyun 		asconf_ack = sctp_process_asconf((struct sctp_association *)
3818*4882a593Smuzhiyun 						 asoc, chunk);
3819*4882a593Smuzhiyun 		if (!asconf_ack)
3820*4882a593Smuzhiyun 			return SCTP_DISPOSITION_NOMEM;
3821*4882a593Smuzhiyun 	} else if (serial < asoc->peer.addip_serial + 1) {
3822*4882a593Smuzhiyun 		/* ADDIP 5.2 E2)
3823*4882a593Smuzhiyun 		 * If the value found in the Sequence Number is less than the
3824*4882a593Smuzhiyun 		 * ('Peer- Sequence-Number' + 1), simply skip to the next
3825*4882a593Smuzhiyun 		 * ASCONF, and include in the outbound response packet
3826*4882a593Smuzhiyun 		 * any previously cached ASCONF-ACK response that was
3827*4882a593Smuzhiyun 		 * sent and saved that matches the Sequence Number of the
3828*4882a593Smuzhiyun 		 * ASCONF.  Note: It is possible that no cached ASCONF-ACK
3829*4882a593Smuzhiyun 		 * Chunk exists.  This will occur when an older ASCONF
3830*4882a593Smuzhiyun 		 * arrives out of order.  In such a case, the receiver
3831*4882a593Smuzhiyun 		 * should skip the ASCONF Chunk and not include ASCONF-ACK
3832*4882a593Smuzhiyun 		 * Chunk for that chunk.
3833*4882a593Smuzhiyun 		 */
3834*4882a593Smuzhiyun 		asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3835*4882a593Smuzhiyun 		if (!asconf_ack)
3836*4882a593Smuzhiyun 			return SCTP_DISPOSITION_DISCARD;
3837*4882a593Smuzhiyun 
3838*4882a593Smuzhiyun 		/* Reset the transport so that we select the correct one
3839*4882a593Smuzhiyun 		 * this time around.  This is to make sure that we don't
3840*4882a593Smuzhiyun 		 * accidentally use a stale transport that's been removed.
3841*4882a593Smuzhiyun 		 */
3842*4882a593Smuzhiyun 		asconf_ack->transport = NULL;
3843*4882a593Smuzhiyun 	} else {
3844*4882a593Smuzhiyun 		/* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3845*4882a593Smuzhiyun 		 * it must be either a stale packet or from an attacker.
3846*4882a593Smuzhiyun 		 */
3847*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
3848*4882a593Smuzhiyun 	}
3849*4882a593Smuzhiyun 
3850*4882a593Smuzhiyun 	/* ADDIP 5.2 E6)  The destination address of the SCTP packet
3851*4882a593Smuzhiyun 	 * containing the ASCONF-ACK Chunks MUST be the source address of
3852*4882a593Smuzhiyun 	 * the SCTP packet that held the ASCONF Chunks.
3853*4882a593Smuzhiyun 	 *
3854*4882a593Smuzhiyun 	 * To do this properly, we'll set the destination address of the chunk
3855*4882a593Smuzhiyun 	 * and at the transmit time, will try look up the transport to use.
3856*4882a593Smuzhiyun 	 * Since ASCONFs may be bundled, the correct transport may not be
3857*4882a593Smuzhiyun 	 * created until we process the entire packet, thus this workaround.
3858*4882a593Smuzhiyun 	 */
3859*4882a593Smuzhiyun 	asconf_ack->dest = chunk->source;
3860*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3861*4882a593Smuzhiyun 	if (asoc->new_transport) {
3862*4882a593Smuzhiyun 		sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3863*4882a593Smuzhiyun 		((struct sctp_association *)asoc)->new_transport = NULL;
3864*4882a593Smuzhiyun 	}
3865*4882a593Smuzhiyun 
3866*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
3867*4882a593Smuzhiyun }
3868*4882a593Smuzhiyun 
sctp_send_next_asconf(struct net * net,const struct sctp_endpoint * ep,struct sctp_association * asoc,const union sctp_subtype type,struct sctp_cmd_seq * commands)3869*4882a593Smuzhiyun static enum sctp_disposition sctp_send_next_asconf(
3870*4882a593Smuzhiyun 					struct net *net,
3871*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
3872*4882a593Smuzhiyun 					struct sctp_association *asoc,
3873*4882a593Smuzhiyun 					const union sctp_subtype type,
3874*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
3875*4882a593Smuzhiyun {
3876*4882a593Smuzhiyun 	struct sctp_chunk *asconf;
3877*4882a593Smuzhiyun 	struct list_head *entry;
3878*4882a593Smuzhiyun 
3879*4882a593Smuzhiyun 	if (list_empty(&asoc->addip_chunk_list))
3880*4882a593Smuzhiyun 		return SCTP_DISPOSITION_CONSUME;
3881*4882a593Smuzhiyun 
3882*4882a593Smuzhiyun 	entry = asoc->addip_chunk_list.next;
3883*4882a593Smuzhiyun 	asconf = list_entry(entry, struct sctp_chunk, list);
3884*4882a593Smuzhiyun 
3885*4882a593Smuzhiyun 	list_del_init(entry);
3886*4882a593Smuzhiyun 	sctp_chunk_hold(asconf);
3887*4882a593Smuzhiyun 	asoc->addip_last_asconf = asconf;
3888*4882a593Smuzhiyun 
3889*4882a593Smuzhiyun 	return sctp_sf_do_prm_asconf(net, ep, asoc, type, asconf, commands);
3890*4882a593Smuzhiyun }
3891*4882a593Smuzhiyun 
3892*4882a593Smuzhiyun /*
3893*4882a593Smuzhiyun  * ADDIP Section 4.3 General rules for address manipulation
3894*4882a593Smuzhiyun  * When building TLV parameters for the ASCONF Chunk that will add or
3895*4882a593Smuzhiyun  * delete IP addresses the D0 to D13 rules should be applied:
3896*4882a593Smuzhiyun  */
sctp_sf_do_asconf_ack(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)3897*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
3898*4882a593Smuzhiyun 					    const struct sctp_endpoint *ep,
3899*4882a593Smuzhiyun 					    const struct sctp_association *asoc,
3900*4882a593Smuzhiyun 					    const union sctp_subtype type,
3901*4882a593Smuzhiyun 					    void *arg,
3902*4882a593Smuzhiyun 					    struct sctp_cmd_seq *commands)
3903*4882a593Smuzhiyun {
3904*4882a593Smuzhiyun 	struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3905*4882a593Smuzhiyun 	struct sctp_paramhdr *err_param = NULL;
3906*4882a593Smuzhiyun 	struct sctp_chunk *asconf_ack = arg;
3907*4882a593Smuzhiyun 	struct sctp_addiphdr *addip_hdr;
3908*4882a593Smuzhiyun 	__u32 sent_serial, rcvd_serial;
3909*4882a593Smuzhiyun 	struct sctp_chunk *abort;
3910*4882a593Smuzhiyun 
3911*4882a593Smuzhiyun 	if (!sctp_vtag_verify(asconf_ack, asoc)) {
3912*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3913*4882a593Smuzhiyun 				SCTP_NULL());
3914*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3915*4882a593Smuzhiyun 	}
3916*4882a593Smuzhiyun 
3917*4882a593Smuzhiyun 	/* Make sure that the ADDIP chunk has a valid length.  */
3918*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(asconf_ack,
3919*4882a593Smuzhiyun 				     sizeof(struct sctp_addip_chunk)))
3920*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3921*4882a593Smuzhiyun 						  commands);
3922*4882a593Smuzhiyun 
3923*4882a593Smuzhiyun 	/* ADD-IP, Section 4.1.2:
3924*4882a593Smuzhiyun 	 * This chunk MUST be sent in an authenticated way by using
3925*4882a593Smuzhiyun 	 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3926*4882a593Smuzhiyun 	 * is received unauthenticated it MUST be silently discarded as
3927*4882a593Smuzhiyun 	 * described in [I-D.ietf-tsvwg-sctp-auth].
3928*4882a593Smuzhiyun 	 */
3929*4882a593Smuzhiyun 	if (!asoc->peer.asconf_capable ||
3930*4882a593Smuzhiyun 	    (!net->sctp.addip_noauth && !asconf_ack->auth))
3931*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3932*4882a593Smuzhiyun 
3933*4882a593Smuzhiyun 	addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
3934*4882a593Smuzhiyun 	rcvd_serial = ntohl(addip_hdr->serial);
3935*4882a593Smuzhiyun 
3936*4882a593Smuzhiyun 	/* Verify the ASCONF-ACK chunk before processing it. */
3937*4882a593Smuzhiyun 	if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
3938*4882a593Smuzhiyun 		return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3939*4882a593Smuzhiyun 			   (void *)err_param, commands);
3940*4882a593Smuzhiyun 
3941*4882a593Smuzhiyun 	if (last_asconf) {
3942*4882a593Smuzhiyun 		addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
3943*4882a593Smuzhiyun 		sent_serial = ntohl(addip_hdr->serial);
3944*4882a593Smuzhiyun 	} else {
3945*4882a593Smuzhiyun 		sent_serial = asoc->addip_serial - 1;
3946*4882a593Smuzhiyun 	}
3947*4882a593Smuzhiyun 
3948*4882a593Smuzhiyun 	/* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3949*4882a593Smuzhiyun 	 * equal to the next serial number to be used but no ASCONF chunk is
3950*4882a593Smuzhiyun 	 * outstanding the endpoint MUST ABORT the association. Note that a
3951*4882a593Smuzhiyun 	 * sequence number is greater than if it is no more than 2^^31-1
3952*4882a593Smuzhiyun 	 * larger than the current sequence number (using serial arithmetic).
3953*4882a593Smuzhiyun 	 */
3954*4882a593Smuzhiyun 	if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3955*4882a593Smuzhiyun 	    !(asoc->addip_last_asconf)) {
3956*4882a593Smuzhiyun 		abort = sctp_make_abort(asoc, asconf_ack,
3957*4882a593Smuzhiyun 					sizeof(struct sctp_errhdr));
3958*4882a593Smuzhiyun 		if (abort) {
3959*4882a593Smuzhiyun 			sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3960*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3961*4882a593Smuzhiyun 					SCTP_CHUNK(abort));
3962*4882a593Smuzhiyun 		}
3963*4882a593Smuzhiyun 		/* We are going to ABORT, so we might as well stop
3964*4882a593Smuzhiyun 		 * processing the rest of the chunks in the packet.
3965*4882a593Smuzhiyun 		 */
3966*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3967*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3968*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3969*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3970*4882a593Smuzhiyun 				SCTP_ERROR(ECONNABORTED));
3971*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3972*4882a593Smuzhiyun 				SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3973*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3974*4882a593Smuzhiyun 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3975*4882a593Smuzhiyun 		return SCTP_DISPOSITION_ABORT;
3976*4882a593Smuzhiyun 	}
3977*4882a593Smuzhiyun 
3978*4882a593Smuzhiyun 	if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3979*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3980*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3981*4882a593Smuzhiyun 
3982*4882a593Smuzhiyun 		if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3983*4882a593Smuzhiyun 					     asconf_ack))
3984*4882a593Smuzhiyun 			return sctp_send_next_asconf(net, ep,
3985*4882a593Smuzhiyun 					(struct sctp_association *)asoc,
3986*4882a593Smuzhiyun 							type, commands);
3987*4882a593Smuzhiyun 
3988*4882a593Smuzhiyun 		abort = sctp_make_abort(asoc, asconf_ack,
3989*4882a593Smuzhiyun 					sizeof(struct sctp_errhdr));
3990*4882a593Smuzhiyun 		if (abort) {
3991*4882a593Smuzhiyun 			sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3992*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3993*4882a593Smuzhiyun 					SCTP_CHUNK(abort));
3994*4882a593Smuzhiyun 		}
3995*4882a593Smuzhiyun 		/* We are going to ABORT, so we might as well stop
3996*4882a593Smuzhiyun 		 * processing the rest of the chunks in the packet.
3997*4882a593Smuzhiyun 		 */
3998*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3999*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4000*4882a593Smuzhiyun 				SCTP_ERROR(ECONNABORTED));
4001*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4002*4882a593Smuzhiyun 				SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
4003*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4004*4882a593Smuzhiyun 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4005*4882a593Smuzhiyun 		return SCTP_DISPOSITION_ABORT;
4006*4882a593Smuzhiyun 	}
4007*4882a593Smuzhiyun 
4008*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DISCARD;
4009*4882a593Smuzhiyun }
4010*4882a593Smuzhiyun 
4011*4882a593Smuzhiyun /* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
sctp_sf_do_reconf(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4012*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_reconf(struct net *net,
4013*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
4014*4882a593Smuzhiyun 					const struct sctp_association *asoc,
4015*4882a593Smuzhiyun 					const union sctp_subtype type,
4016*4882a593Smuzhiyun 					void *arg,
4017*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
4018*4882a593Smuzhiyun {
4019*4882a593Smuzhiyun 	struct sctp_paramhdr *err_param = NULL;
4020*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
4021*4882a593Smuzhiyun 	struct sctp_reconf_chunk *hdr;
4022*4882a593Smuzhiyun 	union sctp_params param;
4023*4882a593Smuzhiyun 
4024*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc)) {
4025*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4026*4882a593Smuzhiyun 				SCTP_NULL());
4027*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4028*4882a593Smuzhiyun 	}
4029*4882a593Smuzhiyun 
4030*4882a593Smuzhiyun 	/* Make sure that the RECONF chunk has a valid length.  */
4031*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
4032*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4033*4882a593Smuzhiyun 						  commands);
4034*4882a593Smuzhiyun 
4035*4882a593Smuzhiyun 	if (!sctp_verify_reconf(asoc, chunk, &err_param))
4036*4882a593Smuzhiyun 		return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
4037*4882a593Smuzhiyun 						  (void *)err_param, commands);
4038*4882a593Smuzhiyun 
4039*4882a593Smuzhiyun 	hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
4040*4882a593Smuzhiyun 	sctp_walk_params(param, hdr, params) {
4041*4882a593Smuzhiyun 		struct sctp_chunk *reply = NULL;
4042*4882a593Smuzhiyun 		struct sctp_ulpevent *ev = NULL;
4043*4882a593Smuzhiyun 
4044*4882a593Smuzhiyun 		if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
4045*4882a593Smuzhiyun 			reply = sctp_process_strreset_outreq(
4046*4882a593Smuzhiyun 				(struct sctp_association *)asoc, param, &ev);
4047*4882a593Smuzhiyun 		else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
4048*4882a593Smuzhiyun 			reply = sctp_process_strreset_inreq(
4049*4882a593Smuzhiyun 				(struct sctp_association *)asoc, param, &ev);
4050*4882a593Smuzhiyun 		else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
4051*4882a593Smuzhiyun 			reply = sctp_process_strreset_tsnreq(
4052*4882a593Smuzhiyun 				(struct sctp_association *)asoc, param, &ev);
4053*4882a593Smuzhiyun 		else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
4054*4882a593Smuzhiyun 			reply = sctp_process_strreset_addstrm_out(
4055*4882a593Smuzhiyun 				(struct sctp_association *)asoc, param, &ev);
4056*4882a593Smuzhiyun 		else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
4057*4882a593Smuzhiyun 			reply = sctp_process_strreset_addstrm_in(
4058*4882a593Smuzhiyun 				(struct sctp_association *)asoc, param, &ev);
4059*4882a593Smuzhiyun 		else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
4060*4882a593Smuzhiyun 			reply = sctp_process_strreset_resp(
4061*4882a593Smuzhiyun 				(struct sctp_association *)asoc, param, &ev);
4062*4882a593Smuzhiyun 
4063*4882a593Smuzhiyun 		if (ev)
4064*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4065*4882a593Smuzhiyun 					SCTP_ULPEVENT(ev));
4066*4882a593Smuzhiyun 
4067*4882a593Smuzhiyun 		if (reply)
4068*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4069*4882a593Smuzhiyun 					SCTP_CHUNK(reply));
4070*4882a593Smuzhiyun 	}
4071*4882a593Smuzhiyun 
4072*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
4073*4882a593Smuzhiyun }
4074*4882a593Smuzhiyun 
4075*4882a593Smuzhiyun /*
4076*4882a593Smuzhiyun  * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
4077*4882a593Smuzhiyun  *
4078*4882a593Smuzhiyun  * When a FORWARD TSN chunk arrives, the data receiver MUST first update
4079*4882a593Smuzhiyun  * its cumulative TSN point to the value carried in the FORWARD TSN
4080*4882a593Smuzhiyun  * chunk, and then MUST further advance its cumulative TSN point locally
4081*4882a593Smuzhiyun  * if possible.
4082*4882a593Smuzhiyun  * After the above processing, the data receiver MUST stop reporting any
4083*4882a593Smuzhiyun  * missing TSNs earlier than or equal to the new cumulative TSN point.
4084*4882a593Smuzhiyun  *
4085*4882a593Smuzhiyun  * Verification Tag:  8.5 Verification Tag [Normal verification]
4086*4882a593Smuzhiyun  *
4087*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
4088*4882a593Smuzhiyun  */
sctp_sf_eat_fwd_tsn(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4089*4882a593Smuzhiyun enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
4090*4882a593Smuzhiyun 					  const struct sctp_endpoint *ep,
4091*4882a593Smuzhiyun 					  const struct sctp_association *asoc,
4092*4882a593Smuzhiyun 					  const union sctp_subtype type,
4093*4882a593Smuzhiyun 					  void *arg,
4094*4882a593Smuzhiyun 					  struct sctp_cmd_seq *commands)
4095*4882a593Smuzhiyun {
4096*4882a593Smuzhiyun 	struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4097*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
4098*4882a593Smuzhiyun 	__u16 len;
4099*4882a593Smuzhiyun 	__u32 tsn;
4100*4882a593Smuzhiyun 
4101*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc)) {
4102*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4103*4882a593Smuzhiyun 				SCTP_NULL());
4104*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4105*4882a593Smuzhiyun 	}
4106*4882a593Smuzhiyun 
4107*4882a593Smuzhiyun 	if (!asoc->peer.prsctp_capable)
4108*4882a593Smuzhiyun 		return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4109*4882a593Smuzhiyun 
4110*4882a593Smuzhiyun 	/* Make sure that the FORWARD_TSN chunk has valid length.  */
4111*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4112*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4113*4882a593Smuzhiyun 						  commands);
4114*4882a593Smuzhiyun 
4115*4882a593Smuzhiyun 	fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4116*4882a593Smuzhiyun 	chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4117*4882a593Smuzhiyun 	len = ntohs(chunk->chunk_hdr->length);
4118*4882a593Smuzhiyun 	len -= sizeof(struct sctp_chunkhdr);
4119*4882a593Smuzhiyun 	skb_pull(chunk->skb, len);
4120*4882a593Smuzhiyun 
4121*4882a593Smuzhiyun 	tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4122*4882a593Smuzhiyun 	pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4123*4882a593Smuzhiyun 
4124*4882a593Smuzhiyun 	/* The TSN is too high--silently discard the chunk and count on it
4125*4882a593Smuzhiyun 	 * getting retransmitted later.
4126*4882a593Smuzhiyun 	 */
4127*4882a593Smuzhiyun 	if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4128*4882a593Smuzhiyun 		goto discard_noforce;
4129*4882a593Smuzhiyun 
4130*4882a593Smuzhiyun 	if (!asoc->stream.si->validate_ftsn(chunk))
4131*4882a593Smuzhiyun 		goto discard_noforce;
4132*4882a593Smuzhiyun 
4133*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4134*4882a593Smuzhiyun 	if (len > sctp_ftsnhdr_len(&asoc->stream))
4135*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4136*4882a593Smuzhiyun 				SCTP_CHUNK(chunk));
4137*4882a593Smuzhiyun 
4138*4882a593Smuzhiyun 	/* Count this as receiving DATA. */
4139*4882a593Smuzhiyun 	if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
4140*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4141*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4142*4882a593Smuzhiyun 	}
4143*4882a593Smuzhiyun 
4144*4882a593Smuzhiyun 	/* FIXME: For now send a SACK, but DATA processing may
4145*4882a593Smuzhiyun 	 * send another.
4146*4882a593Smuzhiyun 	 */
4147*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
4148*4882a593Smuzhiyun 
4149*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
4150*4882a593Smuzhiyun 
4151*4882a593Smuzhiyun discard_noforce:
4152*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DISCARD;
4153*4882a593Smuzhiyun }
4154*4882a593Smuzhiyun 
sctp_sf_eat_fwd_tsn_fast(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4155*4882a593Smuzhiyun enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
4156*4882a593Smuzhiyun 					struct net *net,
4157*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
4158*4882a593Smuzhiyun 					const struct sctp_association *asoc,
4159*4882a593Smuzhiyun 					const union sctp_subtype type,
4160*4882a593Smuzhiyun 					void *arg,
4161*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
4162*4882a593Smuzhiyun {
4163*4882a593Smuzhiyun 	struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4164*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
4165*4882a593Smuzhiyun 	__u16 len;
4166*4882a593Smuzhiyun 	__u32 tsn;
4167*4882a593Smuzhiyun 
4168*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc)) {
4169*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4170*4882a593Smuzhiyun 				SCTP_NULL());
4171*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4172*4882a593Smuzhiyun 	}
4173*4882a593Smuzhiyun 
4174*4882a593Smuzhiyun 	if (!asoc->peer.prsctp_capable)
4175*4882a593Smuzhiyun 		return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4176*4882a593Smuzhiyun 
4177*4882a593Smuzhiyun 	/* Make sure that the FORWARD_TSN chunk has a valid length.  */
4178*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4179*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4180*4882a593Smuzhiyun 						  commands);
4181*4882a593Smuzhiyun 
4182*4882a593Smuzhiyun 	fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4183*4882a593Smuzhiyun 	chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4184*4882a593Smuzhiyun 	len = ntohs(chunk->chunk_hdr->length);
4185*4882a593Smuzhiyun 	len -= sizeof(struct sctp_chunkhdr);
4186*4882a593Smuzhiyun 	skb_pull(chunk->skb, len);
4187*4882a593Smuzhiyun 
4188*4882a593Smuzhiyun 	tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4189*4882a593Smuzhiyun 	pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4190*4882a593Smuzhiyun 
4191*4882a593Smuzhiyun 	/* The TSN is too high--silently discard the chunk and count on it
4192*4882a593Smuzhiyun 	 * getting retransmitted later.
4193*4882a593Smuzhiyun 	 */
4194*4882a593Smuzhiyun 	if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4195*4882a593Smuzhiyun 		goto gen_shutdown;
4196*4882a593Smuzhiyun 
4197*4882a593Smuzhiyun 	if (!asoc->stream.si->validate_ftsn(chunk))
4198*4882a593Smuzhiyun 		goto gen_shutdown;
4199*4882a593Smuzhiyun 
4200*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4201*4882a593Smuzhiyun 	if (len > sctp_ftsnhdr_len(&asoc->stream))
4202*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4203*4882a593Smuzhiyun 				SCTP_CHUNK(chunk));
4204*4882a593Smuzhiyun 
4205*4882a593Smuzhiyun 	/* Go a head and force a SACK, since we are shutting down. */
4206*4882a593Smuzhiyun gen_shutdown:
4207*4882a593Smuzhiyun 	/* Implementor's Guide.
4208*4882a593Smuzhiyun 	 *
4209*4882a593Smuzhiyun 	 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4210*4882a593Smuzhiyun 	 * respond to each received packet containing one or more DATA chunk(s)
4211*4882a593Smuzhiyun 	 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4212*4882a593Smuzhiyun 	 */
4213*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4214*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4215*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4216*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4217*4882a593Smuzhiyun 
4218*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
4219*4882a593Smuzhiyun }
4220*4882a593Smuzhiyun 
4221*4882a593Smuzhiyun /*
4222*4882a593Smuzhiyun  * SCTP-AUTH Section 6.3 Receiving authenticated chukns
4223*4882a593Smuzhiyun  *
4224*4882a593Smuzhiyun  *    The receiver MUST use the HMAC algorithm indicated in the HMAC
4225*4882a593Smuzhiyun  *    Identifier field.  If this algorithm was not specified by the
4226*4882a593Smuzhiyun  *    receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4227*4882a593Smuzhiyun  *    during association setup, the AUTH chunk and all chunks after it MUST
4228*4882a593Smuzhiyun  *    be discarded and an ERROR chunk SHOULD be sent with the error cause
4229*4882a593Smuzhiyun  *    defined in Section 4.1.
4230*4882a593Smuzhiyun  *
4231*4882a593Smuzhiyun  *    If an endpoint with no shared key receives a Shared Key Identifier
4232*4882a593Smuzhiyun  *    other than 0, it MUST silently discard all authenticated chunks.  If
4233*4882a593Smuzhiyun  *    the endpoint has at least one endpoint pair shared key for the peer,
4234*4882a593Smuzhiyun  *    it MUST use the key specified by the Shared Key Identifier if a
4235*4882a593Smuzhiyun  *    key has been configured for that Shared Key Identifier.  If no
4236*4882a593Smuzhiyun  *    endpoint pair shared key has been configured for that Shared Key
4237*4882a593Smuzhiyun  *    Identifier, all authenticated chunks MUST be silently discarded.
4238*4882a593Smuzhiyun  *
4239*4882a593Smuzhiyun  * Verification Tag:  8.5 Verification Tag [Normal verification]
4240*4882a593Smuzhiyun  *
4241*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
4242*4882a593Smuzhiyun  */
sctp_sf_authenticate(const struct sctp_association * asoc,struct sctp_chunk * chunk)4243*4882a593Smuzhiyun static enum sctp_ierror sctp_sf_authenticate(
4244*4882a593Smuzhiyun 					const struct sctp_association *asoc,
4245*4882a593Smuzhiyun 					struct sctp_chunk *chunk)
4246*4882a593Smuzhiyun {
4247*4882a593Smuzhiyun 	struct sctp_shared_key *sh_key = NULL;
4248*4882a593Smuzhiyun 	struct sctp_authhdr *auth_hdr;
4249*4882a593Smuzhiyun 	__u8 *save_digest, *digest;
4250*4882a593Smuzhiyun 	struct sctp_hmac *hmac;
4251*4882a593Smuzhiyun 	unsigned int sig_len;
4252*4882a593Smuzhiyun 	__u16 key_id;
4253*4882a593Smuzhiyun 
4254*4882a593Smuzhiyun 	/* Pull in the auth header, so we can do some more verification */
4255*4882a593Smuzhiyun 	auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4256*4882a593Smuzhiyun 	chunk->subh.auth_hdr = auth_hdr;
4257*4882a593Smuzhiyun 	skb_pull(chunk->skb, sizeof(*auth_hdr));
4258*4882a593Smuzhiyun 
4259*4882a593Smuzhiyun 	/* Make sure that we support the HMAC algorithm from the auth
4260*4882a593Smuzhiyun 	 * chunk.
4261*4882a593Smuzhiyun 	 */
4262*4882a593Smuzhiyun 	if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4263*4882a593Smuzhiyun 		return SCTP_IERROR_AUTH_BAD_HMAC;
4264*4882a593Smuzhiyun 
4265*4882a593Smuzhiyun 	/* Make sure that the provided shared key identifier has been
4266*4882a593Smuzhiyun 	 * configured
4267*4882a593Smuzhiyun 	 */
4268*4882a593Smuzhiyun 	key_id = ntohs(auth_hdr->shkey_id);
4269*4882a593Smuzhiyun 	if (key_id != asoc->active_key_id) {
4270*4882a593Smuzhiyun 		sh_key = sctp_auth_get_shkey(asoc, key_id);
4271*4882a593Smuzhiyun 		if (!sh_key)
4272*4882a593Smuzhiyun 			return SCTP_IERROR_AUTH_BAD_KEYID;
4273*4882a593Smuzhiyun 	}
4274*4882a593Smuzhiyun 
4275*4882a593Smuzhiyun 	/* Make sure that the length of the signature matches what
4276*4882a593Smuzhiyun 	 * we expect.
4277*4882a593Smuzhiyun 	 */
4278*4882a593Smuzhiyun 	sig_len = ntohs(chunk->chunk_hdr->length) -
4279*4882a593Smuzhiyun 		  sizeof(struct sctp_auth_chunk);
4280*4882a593Smuzhiyun 	hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4281*4882a593Smuzhiyun 	if (sig_len != hmac->hmac_len)
4282*4882a593Smuzhiyun 		return SCTP_IERROR_PROTO_VIOLATION;
4283*4882a593Smuzhiyun 
4284*4882a593Smuzhiyun 	/* Now that we've done validation checks, we can compute and
4285*4882a593Smuzhiyun 	 * verify the hmac.  The steps involved are:
4286*4882a593Smuzhiyun 	 *  1. Save the digest from the chunk.
4287*4882a593Smuzhiyun 	 *  2. Zero out the digest in the chunk.
4288*4882a593Smuzhiyun 	 *  3. Compute the new digest
4289*4882a593Smuzhiyun 	 *  4. Compare saved and new digests.
4290*4882a593Smuzhiyun 	 */
4291*4882a593Smuzhiyun 	digest = auth_hdr->hmac;
4292*4882a593Smuzhiyun 	skb_pull(chunk->skb, sig_len);
4293*4882a593Smuzhiyun 
4294*4882a593Smuzhiyun 	save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4295*4882a593Smuzhiyun 	if (!save_digest)
4296*4882a593Smuzhiyun 		goto nomem;
4297*4882a593Smuzhiyun 
4298*4882a593Smuzhiyun 	memset(digest, 0, sig_len);
4299*4882a593Smuzhiyun 
4300*4882a593Smuzhiyun 	sctp_auth_calculate_hmac(asoc, chunk->skb,
4301*4882a593Smuzhiyun 				 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4302*4882a593Smuzhiyun 				 sh_key, GFP_ATOMIC);
4303*4882a593Smuzhiyun 
4304*4882a593Smuzhiyun 	/* Discard the packet if the digests do not match */
4305*4882a593Smuzhiyun 	if (memcmp(save_digest, digest, sig_len)) {
4306*4882a593Smuzhiyun 		kfree(save_digest);
4307*4882a593Smuzhiyun 		return SCTP_IERROR_BAD_SIG;
4308*4882a593Smuzhiyun 	}
4309*4882a593Smuzhiyun 
4310*4882a593Smuzhiyun 	kfree(save_digest);
4311*4882a593Smuzhiyun 	chunk->auth = 1;
4312*4882a593Smuzhiyun 
4313*4882a593Smuzhiyun 	return SCTP_IERROR_NO_ERROR;
4314*4882a593Smuzhiyun nomem:
4315*4882a593Smuzhiyun 	return SCTP_IERROR_NOMEM;
4316*4882a593Smuzhiyun }
4317*4882a593Smuzhiyun 
sctp_sf_eat_auth(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4318*4882a593Smuzhiyun enum sctp_disposition sctp_sf_eat_auth(struct net *net,
4319*4882a593Smuzhiyun 				       const struct sctp_endpoint *ep,
4320*4882a593Smuzhiyun 				       const struct sctp_association *asoc,
4321*4882a593Smuzhiyun 				       const union sctp_subtype type,
4322*4882a593Smuzhiyun 				       void *arg, struct sctp_cmd_seq *commands)
4323*4882a593Smuzhiyun {
4324*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
4325*4882a593Smuzhiyun 	struct sctp_authhdr *auth_hdr;
4326*4882a593Smuzhiyun 	struct sctp_chunk *err_chunk;
4327*4882a593Smuzhiyun 	enum sctp_ierror error;
4328*4882a593Smuzhiyun 
4329*4882a593Smuzhiyun 	/* Make sure that the peer has AUTH capable */
4330*4882a593Smuzhiyun 	if (!asoc->peer.auth_capable)
4331*4882a593Smuzhiyun 		return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4332*4882a593Smuzhiyun 
4333*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc)) {
4334*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4335*4882a593Smuzhiyun 				SCTP_NULL());
4336*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4337*4882a593Smuzhiyun 	}
4338*4882a593Smuzhiyun 
4339*4882a593Smuzhiyun 	/* Make sure that the AUTH chunk has valid length.  */
4340*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4341*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4342*4882a593Smuzhiyun 						  commands);
4343*4882a593Smuzhiyun 
4344*4882a593Smuzhiyun 	auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4345*4882a593Smuzhiyun 	error = sctp_sf_authenticate(asoc, chunk);
4346*4882a593Smuzhiyun 	switch (error) {
4347*4882a593Smuzhiyun 	case SCTP_IERROR_AUTH_BAD_HMAC:
4348*4882a593Smuzhiyun 		/* Generate the ERROR chunk and discard the rest
4349*4882a593Smuzhiyun 		 * of the packet
4350*4882a593Smuzhiyun 		 */
4351*4882a593Smuzhiyun 		err_chunk = sctp_make_op_error(asoc, chunk,
4352*4882a593Smuzhiyun 					       SCTP_ERROR_UNSUP_HMAC,
4353*4882a593Smuzhiyun 					       &auth_hdr->hmac_id,
4354*4882a593Smuzhiyun 					       sizeof(__u16), 0);
4355*4882a593Smuzhiyun 		if (err_chunk) {
4356*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4357*4882a593Smuzhiyun 					SCTP_CHUNK(err_chunk));
4358*4882a593Smuzhiyun 		}
4359*4882a593Smuzhiyun 		fallthrough;
4360*4882a593Smuzhiyun 	case SCTP_IERROR_AUTH_BAD_KEYID:
4361*4882a593Smuzhiyun 	case SCTP_IERROR_BAD_SIG:
4362*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4363*4882a593Smuzhiyun 
4364*4882a593Smuzhiyun 	case SCTP_IERROR_PROTO_VIOLATION:
4365*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4366*4882a593Smuzhiyun 						  commands);
4367*4882a593Smuzhiyun 
4368*4882a593Smuzhiyun 	case SCTP_IERROR_NOMEM:
4369*4882a593Smuzhiyun 		return SCTP_DISPOSITION_NOMEM;
4370*4882a593Smuzhiyun 
4371*4882a593Smuzhiyun 	default:			/* Prevent gcc warnings */
4372*4882a593Smuzhiyun 		break;
4373*4882a593Smuzhiyun 	}
4374*4882a593Smuzhiyun 
4375*4882a593Smuzhiyun 	if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4376*4882a593Smuzhiyun 		struct sctp_ulpevent *ev;
4377*4882a593Smuzhiyun 
4378*4882a593Smuzhiyun 		ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4379*4882a593Smuzhiyun 				    SCTP_AUTH_NEW_KEY, GFP_ATOMIC);
4380*4882a593Smuzhiyun 
4381*4882a593Smuzhiyun 		if (!ev)
4382*4882a593Smuzhiyun 			return -ENOMEM;
4383*4882a593Smuzhiyun 
4384*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4385*4882a593Smuzhiyun 				SCTP_ULPEVENT(ev));
4386*4882a593Smuzhiyun 	}
4387*4882a593Smuzhiyun 
4388*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
4389*4882a593Smuzhiyun }
4390*4882a593Smuzhiyun 
4391*4882a593Smuzhiyun /*
4392*4882a593Smuzhiyun  * Process an unknown chunk.
4393*4882a593Smuzhiyun  *
4394*4882a593Smuzhiyun  * Section: 3.2. Also, 2.1 in the implementor's guide.
4395*4882a593Smuzhiyun  *
4396*4882a593Smuzhiyun  * Chunk Types are encoded such that the highest-order two bits specify
4397*4882a593Smuzhiyun  * the action that must be taken if the processing endpoint does not
4398*4882a593Smuzhiyun  * recognize the Chunk Type.
4399*4882a593Smuzhiyun  *
4400*4882a593Smuzhiyun  * 00 - Stop processing this SCTP packet and discard it, do not process
4401*4882a593Smuzhiyun  *      any further chunks within it.
4402*4882a593Smuzhiyun  *
4403*4882a593Smuzhiyun  * 01 - Stop processing this SCTP packet and discard it, do not process
4404*4882a593Smuzhiyun  *      any further chunks within it, and report the unrecognized
4405*4882a593Smuzhiyun  *      chunk in an 'Unrecognized Chunk Type'.
4406*4882a593Smuzhiyun  *
4407*4882a593Smuzhiyun  * 10 - Skip this chunk and continue processing.
4408*4882a593Smuzhiyun  *
4409*4882a593Smuzhiyun  * 11 - Skip this chunk and continue processing, but report in an ERROR
4410*4882a593Smuzhiyun  *      Chunk using the 'Unrecognized Chunk Type' cause of error.
4411*4882a593Smuzhiyun  *
4412*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
4413*4882a593Smuzhiyun  */
sctp_sf_unk_chunk(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4414*4882a593Smuzhiyun enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
4415*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
4416*4882a593Smuzhiyun 					const struct sctp_association *asoc,
4417*4882a593Smuzhiyun 					const union sctp_subtype type,
4418*4882a593Smuzhiyun 					void *arg,
4419*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
4420*4882a593Smuzhiyun {
4421*4882a593Smuzhiyun 	struct sctp_chunk *unk_chunk = arg;
4422*4882a593Smuzhiyun 	struct sctp_chunk *err_chunk;
4423*4882a593Smuzhiyun 	struct sctp_chunkhdr *hdr;
4424*4882a593Smuzhiyun 
4425*4882a593Smuzhiyun 	pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4426*4882a593Smuzhiyun 
4427*4882a593Smuzhiyun 	if (!sctp_vtag_verify(unk_chunk, asoc))
4428*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4429*4882a593Smuzhiyun 
4430*4882a593Smuzhiyun 	/* Make sure that the chunk has a valid length.
4431*4882a593Smuzhiyun 	 * Since we don't know the chunk type, we use a general
4432*4882a593Smuzhiyun 	 * chunkhdr structure to make a comparison.
4433*4882a593Smuzhiyun 	 */
4434*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
4435*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4436*4882a593Smuzhiyun 						  commands);
4437*4882a593Smuzhiyun 
4438*4882a593Smuzhiyun 	switch (type.chunk & SCTP_CID_ACTION_MASK) {
4439*4882a593Smuzhiyun 	case SCTP_CID_ACTION_DISCARD:
4440*4882a593Smuzhiyun 		/* Discard the packet.  */
4441*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4442*4882a593Smuzhiyun 	case SCTP_CID_ACTION_DISCARD_ERR:
4443*4882a593Smuzhiyun 		/* Generate an ERROR chunk as response. */
4444*4882a593Smuzhiyun 		hdr = unk_chunk->chunk_hdr;
4445*4882a593Smuzhiyun 		err_chunk = sctp_make_op_error(asoc, unk_chunk,
4446*4882a593Smuzhiyun 					       SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4447*4882a593Smuzhiyun 					       SCTP_PAD4(ntohs(hdr->length)),
4448*4882a593Smuzhiyun 					       0);
4449*4882a593Smuzhiyun 		if (err_chunk) {
4450*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4451*4882a593Smuzhiyun 					SCTP_CHUNK(err_chunk));
4452*4882a593Smuzhiyun 		}
4453*4882a593Smuzhiyun 
4454*4882a593Smuzhiyun 		/* Discard the packet.  */
4455*4882a593Smuzhiyun 		sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4456*4882a593Smuzhiyun 		return SCTP_DISPOSITION_CONSUME;
4457*4882a593Smuzhiyun 	case SCTP_CID_ACTION_SKIP:
4458*4882a593Smuzhiyun 		/* Skip the chunk.  */
4459*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DISCARD;
4460*4882a593Smuzhiyun 	case SCTP_CID_ACTION_SKIP_ERR:
4461*4882a593Smuzhiyun 		/* Generate an ERROR chunk as response. */
4462*4882a593Smuzhiyun 		hdr = unk_chunk->chunk_hdr;
4463*4882a593Smuzhiyun 		err_chunk = sctp_make_op_error(asoc, unk_chunk,
4464*4882a593Smuzhiyun 					       SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4465*4882a593Smuzhiyun 					       SCTP_PAD4(ntohs(hdr->length)),
4466*4882a593Smuzhiyun 					       0);
4467*4882a593Smuzhiyun 		if (err_chunk) {
4468*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4469*4882a593Smuzhiyun 					SCTP_CHUNK(err_chunk));
4470*4882a593Smuzhiyun 		}
4471*4882a593Smuzhiyun 		/* Skip the chunk.  */
4472*4882a593Smuzhiyun 		return SCTP_DISPOSITION_CONSUME;
4473*4882a593Smuzhiyun 	default:
4474*4882a593Smuzhiyun 		break;
4475*4882a593Smuzhiyun 	}
4476*4882a593Smuzhiyun 
4477*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DISCARD;
4478*4882a593Smuzhiyun }
4479*4882a593Smuzhiyun 
4480*4882a593Smuzhiyun /*
4481*4882a593Smuzhiyun  * Discard the chunk.
4482*4882a593Smuzhiyun  *
4483*4882a593Smuzhiyun  * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4484*4882a593Smuzhiyun  * [Too numerous to mention...]
4485*4882a593Smuzhiyun  * Verification Tag: No verification needed.
4486*4882a593Smuzhiyun  * Inputs
4487*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
4488*4882a593Smuzhiyun  *
4489*4882a593Smuzhiyun  * Outputs
4490*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
4491*4882a593Smuzhiyun  *
4492*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
4493*4882a593Smuzhiyun  */
sctp_sf_discard_chunk(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4494*4882a593Smuzhiyun enum sctp_disposition sctp_sf_discard_chunk(struct net *net,
4495*4882a593Smuzhiyun 					    const struct sctp_endpoint *ep,
4496*4882a593Smuzhiyun 					    const struct sctp_association *asoc,
4497*4882a593Smuzhiyun 					    const union sctp_subtype type,
4498*4882a593Smuzhiyun 					    void *arg,
4499*4882a593Smuzhiyun 					    struct sctp_cmd_seq *commands)
4500*4882a593Smuzhiyun {
4501*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
4502*4882a593Smuzhiyun 
4503*4882a593Smuzhiyun 	if (asoc && !sctp_vtag_verify(chunk, asoc))
4504*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4505*4882a593Smuzhiyun 
4506*4882a593Smuzhiyun 	/* Make sure that the chunk has a valid length.
4507*4882a593Smuzhiyun 	 * Since we don't know the chunk type, we use a general
4508*4882a593Smuzhiyun 	 * chunkhdr structure to make a comparison.
4509*4882a593Smuzhiyun 	 */
4510*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4511*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4512*4882a593Smuzhiyun 						  commands);
4513*4882a593Smuzhiyun 
4514*4882a593Smuzhiyun 	pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4515*4882a593Smuzhiyun 
4516*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DISCARD;
4517*4882a593Smuzhiyun }
4518*4882a593Smuzhiyun 
4519*4882a593Smuzhiyun /*
4520*4882a593Smuzhiyun  * Discard the whole packet.
4521*4882a593Smuzhiyun  *
4522*4882a593Smuzhiyun  * Section: 8.4 2)
4523*4882a593Smuzhiyun  *
4524*4882a593Smuzhiyun  * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4525*4882a593Smuzhiyun  *    silently discard the OOTB packet and take no further action.
4526*4882a593Smuzhiyun  *
4527*4882a593Smuzhiyun  * Verification Tag: No verification necessary
4528*4882a593Smuzhiyun  *
4529*4882a593Smuzhiyun  * Inputs
4530*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
4531*4882a593Smuzhiyun  *
4532*4882a593Smuzhiyun  * Outputs
4533*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
4534*4882a593Smuzhiyun  *
4535*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
4536*4882a593Smuzhiyun  */
sctp_sf_pdiscard(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4537*4882a593Smuzhiyun enum sctp_disposition sctp_sf_pdiscard(struct net *net,
4538*4882a593Smuzhiyun 				       const struct sctp_endpoint *ep,
4539*4882a593Smuzhiyun 				       const struct sctp_association *asoc,
4540*4882a593Smuzhiyun 				       const union sctp_subtype type,
4541*4882a593Smuzhiyun 				       void *arg, struct sctp_cmd_seq *commands)
4542*4882a593Smuzhiyun {
4543*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4544*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4545*4882a593Smuzhiyun 
4546*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
4547*4882a593Smuzhiyun }
4548*4882a593Smuzhiyun 
4549*4882a593Smuzhiyun 
4550*4882a593Smuzhiyun /*
4551*4882a593Smuzhiyun  * The other end is violating protocol.
4552*4882a593Smuzhiyun  *
4553*4882a593Smuzhiyun  * Section: Not specified
4554*4882a593Smuzhiyun  * Verification Tag: Not specified
4555*4882a593Smuzhiyun  * Inputs
4556*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
4557*4882a593Smuzhiyun  *
4558*4882a593Smuzhiyun  * Outputs
4559*4882a593Smuzhiyun  * (asoc, reply_msg, msg_up, timers, counters)
4560*4882a593Smuzhiyun  *
4561*4882a593Smuzhiyun  * We simply tag the chunk as a violation.  The state machine will log
4562*4882a593Smuzhiyun  * the violation and continue.
4563*4882a593Smuzhiyun  */
sctp_sf_violation(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4564*4882a593Smuzhiyun enum sctp_disposition sctp_sf_violation(struct net *net,
4565*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
4566*4882a593Smuzhiyun 					const struct sctp_association *asoc,
4567*4882a593Smuzhiyun 					const union sctp_subtype type,
4568*4882a593Smuzhiyun 					void *arg,
4569*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
4570*4882a593Smuzhiyun {
4571*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
4572*4882a593Smuzhiyun 
4573*4882a593Smuzhiyun 	if (!sctp_vtag_verify(chunk, asoc))
4574*4882a593Smuzhiyun 		return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4575*4882a593Smuzhiyun 
4576*4882a593Smuzhiyun 	/* Make sure that the chunk has a valid length. */
4577*4882a593Smuzhiyun 	if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4578*4882a593Smuzhiyun 		return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4579*4882a593Smuzhiyun 						  commands);
4580*4882a593Smuzhiyun 
4581*4882a593Smuzhiyun 	return SCTP_DISPOSITION_VIOLATION;
4582*4882a593Smuzhiyun }
4583*4882a593Smuzhiyun 
4584*4882a593Smuzhiyun /*
4585*4882a593Smuzhiyun  * Common function to handle a protocol violation.
4586*4882a593Smuzhiyun  */
sctp_sf_abort_violation(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,void * arg,struct sctp_cmd_seq * commands,const __u8 * payload,const size_t paylen)4587*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_abort_violation(
4588*4882a593Smuzhiyun 					struct net *net,
4589*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
4590*4882a593Smuzhiyun 					const struct sctp_association *asoc,
4591*4882a593Smuzhiyun 					void *arg,
4592*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands,
4593*4882a593Smuzhiyun 					const __u8 *payload,
4594*4882a593Smuzhiyun 					const size_t paylen)
4595*4882a593Smuzhiyun {
4596*4882a593Smuzhiyun 	struct sctp_packet *packet = NULL;
4597*4882a593Smuzhiyun 	struct sctp_chunk *chunk =  arg;
4598*4882a593Smuzhiyun 	struct sctp_chunk *abort = NULL;
4599*4882a593Smuzhiyun 
4600*4882a593Smuzhiyun 	/* SCTP-AUTH, Section 6.3:
4601*4882a593Smuzhiyun 	 *    It should be noted that if the receiver wants to tear
4602*4882a593Smuzhiyun 	 *    down an association in an authenticated way only, the
4603*4882a593Smuzhiyun 	 *    handling of malformed packets should not result in
4604*4882a593Smuzhiyun 	 *    tearing down the association.
4605*4882a593Smuzhiyun 	 *
4606*4882a593Smuzhiyun 	 * This means that if we only want to abort associations
4607*4882a593Smuzhiyun 	 * in an authenticated way (i.e AUTH+ABORT), then we
4608*4882a593Smuzhiyun 	 * can't destroy this association just because the packet
4609*4882a593Smuzhiyun 	 * was malformed.
4610*4882a593Smuzhiyun 	 */
4611*4882a593Smuzhiyun 	if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4612*4882a593Smuzhiyun 		goto discard;
4613*4882a593Smuzhiyun 
4614*4882a593Smuzhiyun 	/* Make the abort chunk. */
4615*4882a593Smuzhiyun 	abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4616*4882a593Smuzhiyun 	if (!abort)
4617*4882a593Smuzhiyun 		goto nomem;
4618*4882a593Smuzhiyun 
4619*4882a593Smuzhiyun 	if (asoc) {
4620*4882a593Smuzhiyun 		/* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4621*4882a593Smuzhiyun 		if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4622*4882a593Smuzhiyun 		    !asoc->peer.i.init_tag) {
4623*4882a593Smuzhiyun 			struct sctp_initack_chunk *initack;
4624*4882a593Smuzhiyun 
4625*4882a593Smuzhiyun 			initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4626*4882a593Smuzhiyun 			if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
4627*4882a593Smuzhiyun 				abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4628*4882a593Smuzhiyun 			else {
4629*4882a593Smuzhiyun 				unsigned int inittag;
4630*4882a593Smuzhiyun 
4631*4882a593Smuzhiyun 				inittag = ntohl(initack->init_hdr.init_tag);
4632*4882a593Smuzhiyun 				sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4633*4882a593Smuzhiyun 						SCTP_U32(inittag));
4634*4882a593Smuzhiyun 			}
4635*4882a593Smuzhiyun 		}
4636*4882a593Smuzhiyun 
4637*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4638*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4639*4882a593Smuzhiyun 
4640*4882a593Smuzhiyun 		if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4641*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4642*4882a593Smuzhiyun 					SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4643*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4644*4882a593Smuzhiyun 					SCTP_ERROR(ECONNREFUSED));
4645*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4646*4882a593Smuzhiyun 					SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4647*4882a593Smuzhiyun 		} else {
4648*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4649*4882a593Smuzhiyun 					SCTP_ERROR(ECONNABORTED));
4650*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4651*4882a593Smuzhiyun 					SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4652*4882a593Smuzhiyun 			SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4653*4882a593Smuzhiyun 		}
4654*4882a593Smuzhiyun 	} else {
4655*4882a593Smuzhiyun 		packet = sctp_ootb_pkt_new(net, asoc, chunk);
4656*4882a593Smuzhiyun 
4657*4882a593Smuzhiyun 		if (!packet)
4658*4882a593Smuzhiyun 			goto nomem_pkt;
4659*4882a593Smuzhiyun 
4660*4882a593Smuzhiyun 		if (sctp_test_T_bit(abort))
4661*4882a593Smuzhiyun 			packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4662*4882a593Smuzhiyun 
4663*4882a593Smuzhiyun 		abort->skb->sk = ep->base.sk;
4664*4882a593Smuzhiyun 
4665*4882a593Smuzhiyun 		sctp_packet_append_chunk(packet, abort);
4666*4882a593Smuzhiyun 
4667*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4668*4882a593Smuzhiyun 			SCTP_PACKET(packet));
4669*4882a593Smuzhiyun 
4670*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4671*4882a593Smuzhiyun 	}
4672*4882a593Smuzhiyun 
4673*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4674*4882a593Smuzhiyun 
4675*4882a593Smuzhiyun discard:
4676*4882a593Smuzhiyun 	sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4677*4882a593Smuzhiyun 	return SCTP_DISPOSITION_ABORT;
4678*4882a593Smuzhiyun 
4679*4882a593Smuzhiyun nomem_pkt:
4680*4882a593Smuzhiyun 	sctp_chunk_free(abort);
4681*4882a593Smuzhiyun nomem:
4682*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
4683*4882a593Smuzhiyun }
4684*4882a593Smuzhiyun 
4685*4882a593Smuzhiyun /*
4686*4882a593Smuzhiyun  * Handle a protocol violation when the chunk length is invalid.
4687*4882a593Smuzhiyun  * "Invalid" length is identified as smaller than the minimal length a
4688*4882a593Smuzhiyun  * given chunk can be.  For example, a SACK chunk has invalid length
4689*4882a593Smuzhiyun  * if its length is set to be smaller than the size of struct sctp_sack_chunk.
4690*4882a593Smuzhiyun  *
4691*4882a593Smuzhiyun  * We inform the other end by sending an ABORT with a Protocol Violation
4692*4882a593Smuzhiyun  * error code.
4693*4882a593Smuzhiyun  *
4694*4882a593Smuzhiyun  * Section: Not specified
4695*4882a593Smuzhiyun  * Verification Tag:  Nothing to do
4696*4882a593Smuzhiyun  * Inputs
4697*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
4698*4882a593Smuzhiyun  *
4699*4882a593Smuzhiyun  * Outputs
4700*4882a593Smuzhiyun  * (reply_msg, msg_up, counters)
4701*4882a593Smuzhiyun  *
4702*4882a593Smuzhiyun  * Generate an  ABORT chunk and terminate the association.
4703*4882a593Smuzhiyun  */
sctp_sf_violation_chunklen(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4704*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_violation_chunklen(
4705*4882a593Smuzhiyun 					struct net *net,
4706*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
4707*4882a593Smuzhiyun 					const struct sctp_association *asoc,
4708*4882a593Smuzhiyun 					const union sctp_subtype type,
4709*4882a593Smuzhiyun 					void *arg,
4710*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
4711*4882a593Smuzhiyun {
4712*4882a593Smuzhiyun 	static const char err_str[] = "The following chunk had invalid length:";
4713*4882a593Smuzhiyun 
4714*4882a593Smuzhiyun 	return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4715*4882a593Smuzhiyun 				       sizeof(err_str));
4716*4882a593Smuzhiyun }
4717*4882a593Smuzhiyun 
4718*4882a593Smuzhiyun /*
4719*4882a593Smuzhiyun  * Handle a protocol violation when the parameter length is invalid.
4720*4882a593Smuzhiyun  * If the length is smaller than the minimum length of a given parameter,
4721*4882a593Smuzhiyun  * or accumulated length in multi parameters exceeds the end of the chunk,
4722*4882a593Smuzhiyun  * the length is considered as invalid.
4723*4882a593Smuzhiyun  */
sctp_sf_violation_paramlen(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,void * ext,struct sctp_cmd_seq * commands)4724*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_violation_paramlen(
4725*4882a593Smuzhiyun 					struct net *net,
4726*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
4727*4882a593Smuzhiyun 					const struct sctp_association *asoc,
4728*4882a593Smuzhiyun 					const union sctp_subtype type,
4729*4882a593Smuzhiyun 					void *arg, void *ext,
4730*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
4731*4882a593Smuzhiyun {
4732*4882a593Smuzhiyun 	struct sctp_paramhdr *param = ext;
4733*4882a593Smuzhiyun 	struct sctp_chunk *abort = NULL;
4734*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
4735*4882a593Smuzhiyun 
4736*4882a593Smuzhiyun 	if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4737*4882a593Smuzhiyun 		goto discard;
4738*4882a593Smuzhiyun 
4739*4882a593Smuzhiyun 	/* Make the abort chunk. */
4740*4882a593Smuzhiyun 	abort = sctp_make_violation_paramlen(asoc, chunk, param);
4741*4882a593Smuzhiyun 	if (!abort)
4742*4882a593Smuzhiyun 		goto nomem;
4743*4882a593Smuzhiyun 
4744*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4745*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4746*4882a593Smuzhiyun 
4747*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4748*4882a593Smuzhiyun 			SCTP_ERROR(ECONNABORTED));
4749*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4750*4882a593Smuzhiyun 			SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4751*4882a593Smuzhiyun 	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4752*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4753*4882a593Smuzhiyun 
4754*4882a593Smuzhiyun discard:
4755*4882a593Smuzhiyun 	sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4756*4882a593Smuzhiyun 	return SCTP_DISPOSITION_ABORT;
4757*4882a593Smuzhiyun nomem:
4758*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
4759*4882a593Smuzhiyun }
4760*4882a593Smuzhiyun 
4761*4882a593Smuzhiyun /* Handle a protocol violation when the peer trying to advance the
4762*4882a593Smuzhiyun  * cumulative tsn ack to a point beyond the max tsn currently sent.
4763*4882a593Smuzhiyun  *
4764*4882a593Smuzhiyun  * We inform the other end by sending an ABORT with a Protocol Violation
4765*4882a593Smuzhiyun  * error code.
4766*4882a593Smuzhiyun  */
sctp_sf_violation_ctsn(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4767*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_violation_ctsn(
4768*4882a593Smuzhiyun 					struct net *net,
4769*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
4770*4882a593Smuzhiyun 					const struct sctp_association *asoc,
4771*4882a593Smuzhiyun 					const union sctp_subtype type,
4772*4882a593Smuzhiyun 					void *arg,
4773*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
4774*4882a593Smuzhiyun {
4775*4882a593Smuzhiyun 	static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4776*4882a593Smuzhiyun 
4777*4882a593Smuzhiyun 	return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4778*4882a593Smuzhiyun 				       sizeof(err_str));
4779*4882a593Smuzhiyun }
4780*4882a593Smuzhiyun 
4781*4882a593Smuzhiyun /* Handle protocol violation of an invalid chunk bundling.  For example,
4782*4882a593Smuzhiyun  * when we have an association and we receive bundled INIT-ACK, or
4783*4882a593Smuzhiyun  * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4784*4882a593Smuzhiyun  * statement from the specs.  Additionally, there might be an attacker
4785*4882a593Smuzhiyun  * on the path and we may not want to continue this communication.
4786*4882a593Smuzhiyun  */
sctp_sf_violation_chunk(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4787*4882a593Smuzhiyun static enum sctp_disposition sctp_sf_violation_chunk(
4788*4882a593Smuzhiyun 					struct net *net,
4789*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
4790*4882a593Smuzhiyun 					const struct sctp_association *asoc,
4791*4882a593Smuzhiyun 					const union sctp_subtype type,
4792*4882a593Smuzhiyun 					void *arg,
4793*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
4794*4882a593Smuzhiyun {
4795*4882a593Smuzhiyun 	static const char err_str[] = "The following chunk violates protocol:";
4796*4882a593Smuzhiyun 
4797*4882a593Smuzhiyun 	if (!asoc)
4798*4882a593Smuzhiyun 		return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4799*4882a593Smuzhiyun 
4800*4882a593Smuzhiyun 	return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4801*4882a593Smuzhiyun 				       sizeof(err_str));
4802*4882a593Smuzhiyun }
4803*4882a593Smuzhiyun /***************************************************************************
4804*4882a593Smuzhiyun  * These are the state functions for handling primitive (Section 10) events.
4805*4882a593Smuzhiyun  ***************************************************************************/
4806*4882a593Smuzhiyun /*
4807*4882a593Smuzhiyun  * sctp_sf_do_prm_asoc
4808*4882a593Smuzhiyun  *
4809*4882a593Smuzhiyun  * Section: 10.1 ULP-to-SCTP
4810*4882a593Smuzhiyun  * B) Associate
4811*4882a593Smuzhiyun  *
4812*4882a593Smuzhiyun  * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4813*4882a593Smuzhiyun  * outbound stream count)
4814*4882a593Smuzhiyun  * -> association id [,destination transport addr list] [,outbound stream
4815*4882a593Smuzhiyun  * count]
4816*4882a593Smuzhiyun  *
4817*4882a593Smuzhiyun  * This primitive allows the upper layer to initiate an association to a
4818*4882a593Smuzhiyun  * specific peer endpoint.
4819*4882a593Smuzhiyun  *
4820*4882a593Smuzhiyun  * The peer endpoint shall be specified by one of the transport addresses
4821*4882a593Smuzhiyun  * which defines the endpoint (see Section 1.4).  If the local SCTP
4822*4882a593Smuzhiyun  * instance has not been initialized, the ASSOCIATE is considered an
4823*4882a593Smuzhiyun  * error.
4824*4882a593Smuzhiyun  * [This is not relevant for the kernel implementation since we do all
4825*4882a593Smuzhiyun  * initialization at boot time.  It we hadn't initialized we wouldn't
4826*4882a593Smuzhiyun  * get anywhere near this code.]
4827*4882a593Smuzhiyun  *
4828*4882a593Smuzhiyun  * An association id, which is a local handle to the SCTP association,
4829*4882a593Smuzhiyun  * will be returned on successful establishment of the association. If
4830*4882a593Smuzhiyun  * SCTP is not able to open an SCTP association with the peer endpoint,
4831*4882a593Smuzhiyun  * an error is returned.
4832*4882a593Smuzhiyun  * [In the kernel implementation, the struct sctp_association needs to
4833*4882a593Smuzhiyun  * be created BEFORE causing this primitive to run.]
4834*4882a593Smuzhiyun  *
4835*4882a593Smuzhiyun  * Other association parameters may be returned, including the
4836*4882a593Smuzhiyun  * complete destination transport addresses of the peer as well as the
4837*4882a593Smuzhiyun  * outbound stream count of the local endpoint. One of the transport
4838*4882a593Smuzhiyun  * address from the returned destination addresses will be selected by
4839*4882a593Smuzhiyun  * the local endpoint as default primary path for sending SCTP packets
4840*4882a593Smuzhiyun  * to this peer.  The returned "destination transport addr list" can
4841*4882a593Smuzhiyun  * be used by the ULP to change the default primary path or to force
4842*4882a593Smuzhiyun  * sending a packet to a specific transport address.  [All of this
4843*4882a593Smuzhiyun  * stuff happens when the INIT ACK arrives.  This is a NON-BLOCKING
4844*4882a593Smuzhiyun  * function.]
4845*4882a593Smuzhiyun  *
4846*4882a593Smuzhiyun  * Mandatory attributes:
4847*4882a593Smuzhiyun  *
4848*4882a593Smuzhiyun  * o local SCTP instance name - obtained from the INITIALIZE operation.
4849*4882a593Smuzhiyun  *   [This is the argument asoc.]
4850*4882a593Smuzhiyun  * o destination transport addr - specified as one of the transport
4851*4882a593Smuzhiyun  * addresses of the peer endpoint with which the association is to be
4852*4882a593Smuzhiyun  * established.
4853*4882a593Smuzhiyun  *  [This is asoc->peer.active_path.]
4854*4882a593Smuzhiyun  * o outbound stream count - the number of outbound streams the ULP
4855*4882a593Smuzhiyun  * would like to open towards this peer endpoint.
4856*4882a593Smuzhiyun  * [BUG: This is not currently implemented.]
4857*4882a593Smuzhiyun  * Optional attributes:
4858*4882a593Smuzhiyun  *
4859*4882a593Smuzhiyun  * None.
4860*4882a593Smuzhiyun  *
4861*4882a593Smuzhiyun  * The return value is a disposition.
4862*4882a593Smuzhiyun  */
sctp_sf_do_prm_asoc(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4863*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
4864*4882a593Smuzhiyun 					  const struct sctp_endpoint *ep,
4865*4882a593Smuzhiyun 					  const struct sctp_association *asoc,
4866*4882a593Smuzhiyun 					  const union sctp_subtype type,
4867*4882a593Smuzhiyun 					  void *arg,
4868*4882a593Smuzhiyun 					  struct sctp_cmd_seq *commands)
4869*4882a593Smuzhiyun {
4870*4882a593Smuzhiyun 	struct sctp_association *my_asoc;
4871*4882a593Smuzhiyun 	struct sctp_chunk *repl;
4872*4882a593Smuzhiyun 
4873*4882a593Smuzhiyun 	/* The comment below says that we enter COOKIE-WAIT AFTER
4874*4882a593Smuzhiyun 	 * sending the INIT, but that doesn't actually work in our
4875*4882a593Smuzhiyun 	 * implementation...
4876*4882a593Smuzhiyun 	 */
4877*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4878*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4879*4882a593Smuzhiyun 
4880*4882a593Smuzhiyun 	/* RFC 2960 5.1 Normal Establishment of an Association
4881*4882a593Smuzhiyun 	 *
4882*4882a593Smuzhiyun 	 * A) "A" first sends an INIT chunk to "Z".  In the INIT, "A"
4883*4882a593Smuzhiyun 	 * must provide its Verification Tag (Tag_A) in the Initiate
4884*4882a593Smuzhiyun 	 * Tag field.  Tag_A SHOULD be a random number in the range of
4885*4882a593Smuzhiyun 	 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4886*4882a593Smuzhiyun 	 */
4887*4882a593Smuzhiyun 
4888*4882a593Smuzhiyun 	repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4889*4882a593Smuzhiyun 	if (!repl)
4890*4882a593Smuzhiyun 		goto nomem;
4891*4882a593Smuzhiyun 
4892*4882a593Smuzhiyun 	/* Choose transport for INIT. */
4893*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4894*4882a593Smuzhiyun 			SCTP_CHUNK(repl));
4895*4882a593Smuzhiyun 
4896*4882a593Smuzhiyun 	/* Cast away the const modifier, as we want to just
4897*4882a593Smuzhiyun 	 * rerun it through as a sideffect.
4898*4882a593Smuzhiyun 	 */
4899*4882a593Smuzhiyun 	my_asoc = (struct sctp_association *)asoc;
4900*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4901*4882a593Smuzhiyun 
4902*4882a593Smuzhiyun 	/* After sending the INIT, "A" starts the T1-init timer and
4903*4882a593Smuzhiyun 	 * enters the COOKIE-WAIT state.
4904*4882a593Smuzhiyun 	 */
4905*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4906*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4907*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4908*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
4909*4882a593Smuzhiyun 
4910*4882a593Smuzhiyun nomem:
4911*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
4912*4882a593Smuzhiyun }
4913*4882a593Smuzhiyun 
4914*4882a593Smuzhiyun /*
4915*4882a593Smuzhiyun  * Process the SEND primitive.
4916*4882a593Smuzhiyun  *
4917*4882a593Smuzhiyun  * Section: 10.1 ULP-to-SCTP
4918*4882a593Smuzhiyun  * E) Send
4919*4882a593Smuzhiyun  *
4920*4882a593Smuzhiyun  * Format: SEND(association id, buffer address, byte count [,context]
4921*4882a593Smuzhiyun  *         [,stream id] [,life time] [,destination transport address]
4922*4882a593Smuzhiyun  *         [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4923*4882a593Smuzhiyun  * -> result
4924*4882a593Smuzhiyun  *
4925*4882a593Smuzhiyun  * This is the main method to send user data via SCTP.
4926*4882a593Smuzhiyun  *
4927*4882a593Smuzhiyun  * Mandatory attributes:
4928*4882a593Smuzhiyun  *
4929*4882a593Smuzhiyun  *  o association id - local handle to the SCTP association
4930*4882a593Smuzhiyun  *
4931*4882a593Smuzhiyun  *  o buffer address - the location where the user message to be
4932*4882a593Smuzhiyun  *    transmitted is stored;
4933*4882a593Smuzhiyun  *
4934*4882a593Smuzhiyun  *  o byte count - The size of the user data in number of bytes;
4935*4882a593Smuzhiyun  *
4936*4882a593Smuzhiyun  * Optional attributes:
4937*4882a593Smuzhiyun  *
4938*4882a593Smuzhiyun  *  o context - an optional 32 bit integer that will be carried in the
4939*4882a593Smuzhiyun  *    sending failure notification to the ULP if the transportation of
4940*4882a593Smuzhiyun  *    this User Message fails.
4941*4882a593Smuzhiyun  *
4942*4882a593Smuzhiyun  *  o stream id - to indicate which stream to send the data on. If not
4943*4882a593Smuzhiyun  *    specified, stream 0 will be used.
4944*4882a593Smuzhiyun  *
4945*4882a593Smuzhiyun  *  o life time - specifies the life time of the user data. The user data
4946*4882a593Smuzhiyun  *    will not be sent by SCTP after the life time expires. This
4947*4882a593Smuzhiyun  *    parameter can be used to avoid efforts to transmit stale
4948*4882a593Smuzhiyun  *    user messages. SCTP notifies the ULP if the data cannot be
4949*4882a593Smuzhiyun  *    initiated to transport (i.e. sent to the destination via SCTP's
4950*4882a593Smuzhiyun  *    send primitive) within the life time variable. However, the
4951*4882a593Smuzhiyun  *    user data will be transmitted if SCTP has attempted to transmit a
4952*4882a593Smuzhiyun  *    chunk before the life time expired.
4953*4882a593Smuzhiyun  *
4954*4882a593Smuzhiyun  *  o destination transport address - specified as one of the destination
4955*4882a593Smuzhiyun  *    transport addresses of the peer endpoint to which this packet
4956*4882a593Smuzhiyun  *    should be sent. Whenever possible, SCTP should use this destination
4957*4882a593Smuzhiyun  *    transport address for sending the packets, instead of the current
4958*4882a593Smuzhiyun  *    primary path.
4959*4882a593Smuzhiyun  *
4960*4882a593Smuzhiyun  *  o unorder flag - this flag, if present, indicates that the user
4961*4882a593Smuzhiyun  *    would like the data delivered in an unordered fashion to the peer
4962*4882a593Smuzhiyun  *    (i.e., the U flag is set to 1 on all DATA chunks carrying this
4963*4882a593Smuzhiyun  *    message).
4964*4882a593Smuzhiyun  *
4965*4882a593Smuzhiyun  *  o no-bundle flag - instructs SCTP not to bundle this user data with
4966*4882a593Smuzhiyun  *    other outbound DATA chunks. SCTP MAY still bundle even when
4967*4882a593Smuzhiyun  *    this flag is present, when faced with network congestion.
4968*4882a593Smuzhiyun  *
4969*4882a593Smuzhiyun  *  o payload protocol-id - A 32 bit unsigned integer that is to be
4970*4882a593Smuzhiyun  *    passed to the peer indicating the type of payload protocol data
4971*4882a593Smuzhiyun  *    being transmitted. This value is passed as opaque data by SCTP.
4972*4882a593Smuzhiyun  *
4973*4882a593Smuzhiyun  * The return value is the disposition.
4974*4882a593Smuzhiyun  */
sctp_sf_do_prm_send(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)4975*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
4976*4882a593Smuzhiyun 					  const struct sctp_endpoint *ep,
4977*4882a593Smuzhiyun 					  const struct sctp_association *asoc,
4978*4882a593Smuzhiyun 					  const union sctp_subtype type,
4979*4882a593Smuzhiyun 					  void *arg,
4980*4882a593Smuzhiyun 					  struct sctp_cmd_seq *commands)
4981*4882a593Smuzhiyun {
4982*4882a593Smuzhiyun 	struct sctp_datamsg *msg = arg;
4983*4882a593Smuzhiyun 
4984*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4985*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
4986*4882a593Smuzhiyun }
4987*4882a593Smuzhiyun 
4988*4882a593Smuzhiyun /*
4989*4882a593Smuzhiyun  * Process the SHUTDOWN primitive.
4990*4882a593Smuzhiyun  *
4991*4882a593Smuzhiyun  * Section: 10.1:
4992*4882a593Smuzhiyun  * C) Shutdown
4993*4882a593Smuzhiyun  *
4994*4882a593Smuzhiyun  * Format: SHUTDOWN(association id)
4995*4882a593Smuzhiyun  * -> result
4996*4882a593Smuzhiyun  *
4997*4882a593Smuzhiyun  * Gracefully closes an association. Any locally queued user data
4998*4882a593Smuzhiyun  * will be delivered to the peer. The association will be terminated only
4999*4882a593Smuzhiyun  * after the peer acknowledges all the SCTP packets sent.  A success code
5000*4882a593Smuzhiyun  * will be returned on successful termination of the association. If
5001*4882a593Smuzhiyun  * attempting to terminate the association results in a failure, an error
5002*4882a593Smuzhiyun  * code shall be returned.
5003*4882a593Smuzhiyun  *
5004*4882a593Smuzhiyun  * Mandatory attributes:
5005*4882a593Smuzhiyun  *
5006*4882a593Smuzhiyun  *  o association id - local handle to the SCTP association
5007*4882a593Smuzhiyun  *
5008*4882a593Smuzhiyun  * Optional attributes:
5009*4882a593Smuzhiyun  *
5010*4882a593Smuzhiyun  * None.
5011*4882a593Smuzhiyun  *
5012*4882a593Smuzhiyun  * The return value is the disposition.
5013*4882a593Smuzhiyun  */
sctp_sf_do_9_2_prm_shutdown(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5014*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
5015*4882a593Smuzhiyun 					struct net *net,
5016*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5017*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5018*4882a593Smuzhiyun 					const union sctp_subtype type,
5019*4882a593Smuzhiyun 					void *arg,
5020*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5021*4882a593Smuzhiyun {
5022*4882a593Smuzhiyun 	enum sctp_disposition disposition;
5023*4882a593Smuzhiyun 
5024*4882a593Smuzhiyun 	/* From 9.2 Shutdown of an Association
5025*4882a593Smuzhiyun 	 * Upon receipt of the SHUTDOWN primitive from its upper
5026*4882a593Smuzhiyun 	 * layer, the endpoint enters SHUTDOWN-PENDING state and
5027*4882a593Smuzhiyun 	 * remains there until all outstanding data has been
5028*4882a593Smuzhiyun 	 * acknowledged by its peer. The endpoint accepts no new data
5029*4882a593Smuzhiyun 	 * from its upper layer, but retransmits data to the far end
5030*4882a593Smuzhiyun 	 * if necessary to fill gaps.
5031*4882a593Smuzhiyun 	 */
5032*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5033*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5034*4882a593Smuzhiyun 
5035*4882a593Smuzhiyun 	disposition = SCTP_DISPOSITION_CONSUME;
5036*4882a593Smuzhiyun 	if (sctp_outq_is_empty(&asoc->outqueue)) {
5037*4882a593Smuzhiyun 		disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5038*4882a593Smuzhiyun 							    arg, commands);
5039*4882a593Smuzhiyun 	}
5040*4882a593Smuzhiyun 
5041*4882a593Smuzhiyun 	return disposition;
5042*4882a593Smuzhiyun }
5043*4882a593Smuzhiyun 
5044*4882a593Smuzhiyun /*
5045*4882a593Smuzhiyun  * Process the ABORT primitive.
5046*4882a593Smuzhiyun  *
5047*4882a593Smuzhiyun  * Section: 10.1:
5048*4882a593Smuzhiyun  * C) Abort
5049*4882a593Smuzhiyun  *
5050*4882a593Smuzhiyun  * Format: Abort(association id [, cause code])
5051*4882a593Smuzhiyun  * -> result
5052*4882a593Smuzhiyun  *
5053*4882a593Smuzhiyun  * Ungracefully closes an association. Any locally queued user data
5054*4882a593Smuzhiyun  * will be discarded and an ABORT chunk is sent to the peer.  A success code
5055*4882a593Smuzhiyun  * will be returned on successful abortion of the association. If
5056*4882a593Smuzhiyun  * attempting to abort the association results in a failure, an error
5057*4882a593Smuzhiyun  * code shall be returned.
5058*4882a593Smuzhiyun  *
5059*4882a593Smuzhiyun  * Mandatory attributes:
5060*4882a593Smuzhiyun  *
5061*4882a593Smuzhiyun  *  o association id - local handle to the SCTP association
5062*4882a593Smuzhiyun  *
5063*4882a593Smuzhiyun  * Optional attributes:
5064*4882a593Smuzhiyun  *
5065*4882a593Smuzhiyun  *  o cause code - reason of the abort to be passed to the peer
5066*4882a593Smuzhiyun  *
5067*4882a593Smuzhiyun  * None.
5068*4882a593Smuzhiyun  *
5069*4882a593Smuzhiyun  * The return value is the disposition.
5070*4882a593Smuzhiyun  */
sctp_sf_do_9_1_prm_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5071*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_9_1_prm_abort(
5072*4882a593Smuzhiyun 					struct net *net,
5073*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5074*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5075*4882a593Smuzhiyun 					const union sctp_subtype type,
5076*4882a593Smuzhiyun 					void *arg,
5077*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5078*4882a593Smuzhiyun {
5079*4882a593Smuzhiyun 	/* From 9.1 Abort of an Association
5080*4882a593Smuzhiyun 	 * Upon receipt of the ABORT primitive from its upper
5081*4882a593Smuzhiyun 	 * layer, the endpoint enters CLOSED state and
5082*4882a593Smuzhiyun 	 * discard all outstanding data has been
5083*4882a593Smuzhiyun 	 * acknowledged by its peer. The endpoint accepts no new data
5084*4882a593Smuzhiyun 	 * from its upper layer, but retransmits data to the far end
5085*4882a593Smuzhiyun 	 * if necessary to fill gaps.
5086*4882a593Smuzhiyun 	 */
5087*4882a593Smuzhiyun 	struct sctp_chunk *abort = arg;
5088*4882a593Smuzhiyun 
5089*4882a593Smuzhiyun 	if (abort)
5090*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5091*4882a593Smuzhiyun 
5092*4882a593Smuzhiyun 	/* Even if we can't send the ABORT due to low memory delete the
5093*4882a593Smuzhiyun 	 * TCB.  This is a departure from our typical NOMEM handling.
5094*4882a593Smuzhiyun 	 */
5095*4882a593Smuzhiyun 
5096*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5097*4882a593Smuzhiyun 			SCTP_ERROR(ECONNABORTED));
5098*4882a593Smuzhiyun 	/* Delete the established association. */
5099*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5100*4882a593Smuzhiyun 			SCTP_PERR(SCTP_ERROR_USER_ABORT));
5101*4882a593Smuzhiyun 
5102*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5103*4882a593Smuzhiyun 	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5104*4882a593Smuzhiyun 
5105*4882a593Smuzhiyun 	return SCTP_DISPOSITION_ABORT;
5106*4882a593Smuzhiyun }
5107*4882a593Smuzhiyun 
5108*4882a593Smuzhiyun /* We tried an illegal operation on an association which is closed.  */
sctp_sf_error_closed(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5109*4882a593Smuzhiyun enum sctp_disposition sctp_sf_error_closed(struct net *net,
5110*4882a593Smuzhiyun 					   const struct sctp_endpoint *ep,
5111*4882a593Smuzhiyun 					   const struct sctp_association *asoc,
5112*4882a593Smuzhiyun 					   const union sctp_subtype type,
5113*4882a593Smuzhiyun 					   void *arg,
5114*4882a593Smuzhiyun 					   struct sctp_cmd_seq *commands)
5115*4882a593Smuzhiyun {
5116*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
5117*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5118*4882a593Smuzhiyun }
5119*4882a593Smuzhiyun 
5120*4882a593Smuzhiyun /* We tried an illegal operation on an association which is shutting
5121*4882a593Smuzhiyun  * down.
5122*4882a593Smuzhiyun  */
sctp_sf_error_shutdown(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5123*4882a593Smuzhiyun enum sctp_disposition sctp_sf_error_shutdown(
5124*4882a593Smuzhiyun 					struct net *net,
5125*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5126*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5127*4882a593Smuzhiyun 					const union sctp_subtype type,
5128*4882a593Smuzhiyun 					void *arg,
5129*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5130*4882a593Smuzhiyun {
5131*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
5132*4882a593Smuzhiyun 			SCTP_ERROR(-ESHUTDOWN));
5133*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5134*4882a593Smuzhiyun }
5135*4882a593Smuzhiyun 
5136*4882a593Smuzhiyun /*
5137*4882a593Smuzhiyun  * sctp_cookie_wait_prm_shutdown
5138*4882a593Smuzhiyun  *
5139*4882a593Smuzhiyun  * Section: 4 Note: 2
5140*4882a593Smuzhiyun  * Verification Tag:
5141*4882a593Smuzhiyun  * Inputs
5142*4882a593Smuzhiyun  * (endpoint, asoc)
5143*4882a593Smuzhiyun  *
5144*4882a593Smuzhiyun  * The RFC does not explicitly address this issue, but is the route through the
5145*4882a593Smuzhiyun  * state table when someone issues a shutdown while in COOKIE_WAIT state.
5146*4882a593Smuzhiyun  *
5147*4882a593Smuzhiyun  * Outputs
5148*4882a593Smuzhiyun  * (timers)
5149*4882a593Smuzhiyun  */
sctp_sf_cookie_wait_prm_shutdown(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5150*4882a593Smuzhiyun enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
5151*4882a593Smuzhiyun 					struct net *net,
5152*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5153*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5154*4882a593Smuzhiyun 					const union sctp_subtype type,
5155*4882a593Smuzhiyun 					void *arg,
5156*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5157*4882a593Smuzhiyun {
5158*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5159*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5160*4882a593Smuzhiyun 
5161*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5162*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_CLOSED));
5163*4882a593Smuzhiyun 
5164*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
5165*4882a593Smuzhiyun 
5166*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5167*4882a593Smuzhiyun 
5168*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DELETE_TCB;
5169*4882a593Smuzhiyun }
5170*4882a593Smuzhiyun 
5171*4882a593Smuzhiyun /*
5172*4882a593Smuzhiyun  * sctp_cookie_echoed_prm_shutdown
5173*4882a593Smuzhiyun  *
5174*4882a593Smuzhiyun  * Section: 4 Note: 2
5175*4882a593Smuzhiyun  * Verification Tag:
5176*4882a593Smuzhiyun  * Inputs
5177*4882a593Smuzhiyun  * (endpoint, asoc)
5178*4882a593Smuzhiyun  *
5179*4882a593Smuzhiyun  * The RFC does not explcitly address this issue, but is the route through the
5180*4882a593Smuzhiyun  * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5181*4882a593Smuzhiyun  *
5182*4882a593Smuzhiyun  * Outputs
5183*4882a593Smuzhiyun  * (timers)
5184*4882a593Smuzhiyun  */
sctp_sf_cookie_echoed_prm_shutdown(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5185*4882a593Smuzhiyun enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
5186*4882a593Smuzhiyun 					struct net *net,
5187*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5188*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5189*4882a593Smuzhiyun 					const union sctp_subtype type,
5190*4882a593Smuzhiyun 					void *arg,
5191*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5192*4882a593Smuzhiyun {
5193*4882a593Smuzhiyun 	/* There is a single T1 timer, so we should be able to use
5194*4882a593Smuzhiyun 	 * common function with the COOKIE-WAIT state.
5195*4882a593Smuzhiyun 	 */
5196*4882a593Smuzhiyun 	return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
5197*4882a593Smuzhiyun }
5198*4882a593Smuzhiyun 
5199*4882a593Smuzhiyun /*
5200*4882a593Smuzhiyun  * sctp_sf_cookie_wait_prm_abort
5201*4882a593Smuzhiyun  *
5202*4882a593Smuzhiyun  * Section: 4 Note: 2
5203*4882a593Smuzhiyun  * Verification Tag:
5204*4882a593Smuzhiyun  * Inputs
5205*4882a593Smuzhiyun  * (endpoint, asoc)
5206*4882a593Smuzhiyun  *
5207*4882a593Smuzhiyun  * The RFC does not explicitly address this issue, but is the route through the
5208*4882a593Smuzhiyun  * state table when someone issues an abort while in COOKIE_WAIT state.
5209*4882a593Smuzhiyun  *
5210*4882a593Smuzhiyun  * Outputs
5211*4882a593Smuzhiyun  * (timers)
5212*4882a593Smuzhiyun  */
sctp_sf_cookie_wait_prm_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5213*4882a593Smuzhiyun enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
5214*4882a593Smuzhiyun 					struct net *net,
5215*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5216*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5217*4882a593Smuzhiyun 					const union sctp_subtype type,
5218*4882a593Smuzhiyun 					void *arg,
5219*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5220*4882a593Smuzhiyun {
5221*4882a593Smuzhiyun 	struct sctp_chunk *abort = arg;
5222*4882a593Smuzhiyun 
5223*4882a593Smuzhiyun 	/* Stop T1-init timer */
5224*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5225*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5226*4882a593Smuzhiyun 
5227*4882a593Smuzhiyun 	if (abort)
5228*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5229*4882a593Smuzhiyun 
5230*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5231*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_CLOSED));
5232*4882a593Smuzhiyun 
5233*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5234*4882a593Smuzhiyun 
5235*4882a593Smuzhiyun 	/* Even if we can't send the ABORT due to low memory delete the
5236*4882a593Smuzhiyun 	 * TCB.  This is a departure from our typical NOMEM handling.
5237*4882a593Smuzhiyun 	 */
5238*4882a593Smuzhiyun 
5239*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5240*4882a593Smuzhiyun 			SCTP_ERROR(ECONNREFUSED));
5241*4882a593Smuzhiyun 	/* Delete the established association. */
5242*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5243*4882a593Smuzhiyun 			SCTP_PERR(SCTP_ERROR_USER_ABORT));
5244*4882a593Smuzhiyun 
5245*4882a593Smuzhiyun 	return SCTP_DISPOSITION_ABORT;
5246*4882a593Smuzhiyun }
5247*4882a593Smuzhiyun 
5248*4882a593Smuzhiyun /*
5249*4882a593Smuzhiyun  * sctp_sf_cookie_echoed_prm_abort
5250*4882a593Smuzhiyun  *
5251*4882a593Smuzhiyun  * Section: 4 Note: 3
5252*4882a593Smuzhiyun  * Verification Tag:
5253*4882a593Smuzhiyun  * Inputs
5254*4882a593Smuzhiyun  * (endpoint, asoc)
5255*4882a593Smuzhiyun  *
5256*4882a593Smuzhiyun  * The RFC does not explcitly address this issue, but is the route through the
5257*4882a593Smuzhiyun  * state table when someone issues an abort while in COOKIE_ECHOED state.
5258*4882a593Smuzhiyun  *
5259*4882a593Smuzhiyun  * Outputs
5260*4882a593Smuzhiyun  * (timers)
5261*4882a593Smuzhiyun  */
sctp_sf_cookie_echoed_prm_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5262*4882a593Smuzhiyun enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
5263*4882a593Smuzhiyun 					struct net *net,
5264*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5265*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5266*4882a593Smuzhiyun 					const union sctp_subtype type,
5267*4882a593Smuzhiyun 					void *arg,
5268*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5269*4882a593Smuzhiyun {
5270*4882a593Smuzhiyun 	/* There is a single T1 timer, so we should be able to use
5271*4882a593Smuzhiyun 	 * common function with the COOKIE-WAIT state.
5272*4882a593Smuzhiyun 	 */
5273*4882a593Smuzhiyun 	return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5274*4882a593Smuzhiyun }
5275*4882a593Smuzhiyun 
5276*4882a593Smuzhiyun /*
5277*4882a593Smuzhiyun  * sctp_sf_shutdown_pending_prm_abort
5278*4882a593Smuzhiyun  *
5279*4882a593Smuzhiyun  * Inputs
5280*4882a593Smuzhiyun  * (endpoint, asoc)
5281*4882a593Smuzhiyun  *
5282*4882a593Smuzhiyun  * The RFC does not explicitly address this issue, but is the route through the
5283*4882a593Smuzhiyun  * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5284*4882a593Smuzhiyun  *
5285*4882a593Smuzhiyun  * Outputs
5286*4882a593Smuzhiyun  * (timers)
5287*4882a593Smuzhiyun  */
sctp_sf_shutdown_pending_prm_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5288*4882a593Smuzhiyun enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
5289*4882a593Smuzhiyun 					struct net *net,
5290*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5291*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5292*4882a593Smuzhiyun 					const union sctp_subtype type,
5293*4882a593Smuzhiyun 					void *arg,
5294*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5295*4882a593Smuzhiyun {
5296*4882a593Smuzhiyun 	/* Stop the T5-shutdown guard timer.  */
5297*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5298*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5299*4882a593Smuzhiyun 
5300*4882a593Smuzhiyun 	return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5301*4882a593Smuzhiyun }
5302*4882a593Smuzhiyun 
5303*4882a593Smuzhiyun /*
5304*4882a593Smuzhiyun  * sctp_sf_shutdown_sent_prm_abort
5305*4882a593Smuzhiyun  *
5306*4882a593Smuzhiyun  * Inputs
5307*4882a593Smuzhiyun  * (endpoint, asoc)
5308*4882a593Smuzhiyun  *
5309*4882a593Smuzhiyun  * The RFC does not explicitly address this issue, but is the route through the
5310*4882a593Smuzhiyun  * state table when someone issues an abort while in SHUTDOWN-SENT state.
5311*4882a593Smuzhiyun  *
5312*4882a593Smuzhiyun  * Outputs
5313*4882a593Smuzhiyun  * (timers)
5314*4882a593Smuzhiyun  */
sctp_sf_shutdown_sent_prm_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5315*4882a593Smuzhiyun enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
5316*4882a593Smuzhiyun 					struct net *net,
5317*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5318*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5319*4882a593Smuzhiyun 					const union sctp_subtype type,
5320*4882a593Smuzhiyun 					void *arg,
5321*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5322*4882a593Smuzhiyun {
5323*4882a593Smuzhiyun 	/* Stop the T2-shutdown timer.  */
5324*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5325*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5326*4882a593Smuzhiyun 
5327*4882a593Smuzhiyun 	/* Stop the T5-shutdown guard timer.  */
5328*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5329*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5330*4882a593Smuzhiyun 
5331*4882a593Smuzhiyun 	return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5332*4882a593Smuzhiyun }
5333*4882a593Smuzhiyun 
5334*4882a593Smuzhiyun /*
5335*4882a593Smuzhiyun  * sctp_sf_cookie_echoed_prm_abort
5336*4882a593Smuzhiyun  *
5337*4882a593Smuzhiyun  * Inputs
5338*4882a593Smuzhiyun  * (endpoint, asoc)
5339*4882a593Smuzhiyun  *
5340*4882a593Smuzhiyun  * The RFC does not explcitly address this issue, but is the route through the
5341*4882a593Smuzhiyun  * state table when someone issues an abort while in COOKIE_ECHOED state.
5342*4882a593Smuzhiyun  *
5343*4882a593Smuzhiyun  * Outputs
5344*4882a593Smuzhiyun  * (timers)
5345*4882a593Smuzhiyun  */
sctp_sf_shutdown_ack_sent_prm_abort(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5346*4882a593Smuzhiyun enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
5347*4882a593Smuzhiyun 					struct net *net,
5348*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5349*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5350*4882a593Smuzhiyun 					const union sctp_subtype type,
5351*4882a593Smuzhiyun 					void *arg,
5352*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5353*4882a593Smuzhiyun {
5354*4882a593Smuzhiyun 	/* The same T2 timer, so we should be able to use
5355*4882a593Smuzhiyun 	 * common function with the SHUTDOWN-SENT state.
5356*4882a593Smuzhiyun 	 */
5357*4882a593Smuzhiyun 	return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5358*4882a593Smuzhiyun }
5359*4882a593Smuzhiyun 
5360*4882a593Smuzhiyun /*
5361*4882a593Smuzhiyun  * Process the REQUESTHEARTBEAT primitive
5362*4882a593Smuzhiyun  *
5363*4882a593Smuzhiyun  * 10.1 ULP-to-SCTP
5364*4882a593Smuzhiyun  * J) Request Heartbeat
5365*4882a593Smuzhiyun  *
5366*4882a593Smuzhiyun  * Format: REQUESTHEARTBEAT(association id, destination transport address)
5367*4882a593Smuzhiyun  *
5368*4882a593Smuzhiyun  * -> result
5369*4882a593Smuzhiyun  *
5370*4882a593Smuzhiyun  * Instructs the local endpoint to perform a HeartBeat on the specified
5371*4882a593Smuzhiyun  * destination transport address of the given association. The returned
5372*4882a593Smuzhiyun  * result should indicate whether the transmission of the HEARTBEAT
5373*4882a593Smuzhiyun  * chunk to the destination address is successful.
5374*4882a593Smuzhiyun  *
5375*4882a593Smuzhiyun  * Mandatory attributes:
5376*4882a593Smuzhiyun  *
5377*4882a593Smuzhiyun  * o association id - local handle to the SCTP association
5378*4882a593Smuzhiyun  *
5379*4882a593Smuzhiyun  * o destination transport address - the transport address of the
5380*4882a593Smuzhiyun  *   association on which a heartbeat should be issued.
5381*4882a593Smuzhiyun  */
sctp_sf_do_prm_requestheartbeat(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5382*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
5383*4882a593Smuzhiyun 					struct net *net,
5384*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5385*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5386*4882a593Smuzhiyun 					const union sctp_subtype type,
5387*4882a593Smuzhiyun 					void *arg,
5388*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5389*4882a593Smuzhiyun {
5390*4882a593Smuzhiyun 	if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5391*4882a593Smuzhiyun 				      (struct sctp_transport *)arg, commands))
5392*4882a593Smuzhiyun 		return SCTP_DISPOSITION_NOMEM;
5393*4882a593Smuzhiyun 
5394*4882a593Smuzhiyun 	/*
5395*4882a593Smuzhiyun 	 * RFC 2960 (bis), section 8.3
5396*4882a593Smuzhiyun 	 *
5397*4882a593Smuzhiyun 	 *    D) Request an on-demand HEARTBEAT on a specific destination
5398*4882a593Smuzhiyun 	 *    transport address of a given association.
5399*4882a593Smuzhiyun 	 *
5400*4882a593Smuzhiyun 	 *    The endpoint should increment the respective error  counter of
5401*4882a593Smuzhiyun 	 *    the destination transport address each time a HEARTBEAT is sent
5402*4882a593Smuzhiyun 	 *    to that address and not acknowledged within one RTO.
5403*4882a593Smuzhiyun 	 *
5404*4882a593Smuzhiyun 	 */
5405*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5406*4882a593Smuzhiyun 			SCTP_TRANSPORT(arg));
5407*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5408*4882a593Smuzhiyun }
5409*4882a593Smuzhiyun 
5410*4882a593Smuzhiyun /*
5411*4882a593Smuzhiyun  * ADDIP Section 4.1 ASCONF Chunk Procedures
5412*4882a593Smuzhiyun  * When an endpoint has an ASCONF signaled change to be sent to the
5413*4882a593Smuzhiyun  * remote endpoint it should do A1 to A9
5414*4882a593Smuzhiyun  */
sctp_sf_do_prm_asconf(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5415*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
5416*4882a593Smuzhiyun 					    const struct sctp_endpoint *ep,
5417*4882a593Smuzhiyun 					    const struct sctp_association *asoc,
5418*4882a593Smuzhiyun 					    const union sctp_subtype type,
5419*4882a593Smuzhiyun 					    void *arg,
5420*4882a593Smuzhiyun 					    struct sctp_cmd_seq *commands)
5421*4882a593Smuzhiyun {
5422*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
5423*4882a593Smuzhiyun 
5424*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5425*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5426*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5427*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5428*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5429*4882a593Smuzhiyun }
5430*4882a593Smuzhiyun 
5431*4882a593Smuzhiyun /* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
sctp_sf_do_prm_reconf(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5432*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
5433*4882a593Smuzhiyun 					    const struct sctp_endpoint *ep,
5434*4882a593Smuzhiyun 					    const struct sctp_association *asoc,
5435*4882a593Smuzhiyun 					    const union sctp_subtype type,
5436*4882a593Smuzhiyun 					    void *arg,
5437*4882a593Smuzhiyun 					    struct sctp_cmd_seq *commands)
5438*4882a593Smuzhiyun {
5439*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
5440*4882a593Smuzhiyun 
5441*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5442*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5443*4882a593Smuzhiyun }
5444*4882a593Smuzhiyun 
5445*4882a593Smuzhiyun /*
5446*4882a593Smuzhiyun  * Ignore the primitive event
5447*4882a593Smuzhiyun  *
5448*4882a593Smuzhiyun  * The return value is the disposition of the primitive.
5449*4882a593Smuzhiyun  */
sctp_sf_ignore_primitive(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5450*4882a593Smuzhiyun enum sctp_disposition sctp_sf_ignore_primitive(
5451*4882a593Smuzhiyun 					struct net *net,
5452*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5453*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5454*4882a593Smuzhiyun 					const union sctp_subtype type,
5455*4882a593Smuzhiyun 					void *arg,
5456*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5457*4882a593Smuzhiyun {
5458*4882a593Smuzhiyun 	pr_debug("%s: primitive type:%d is ignored\n", __func__,
5459*4882a593Smuzhiyun 		 type.primitive);
5460*4882a593Smuzhiyun 
5461*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DISCARD;
5462*4882a593Smuzhiyun }
5463*4882a593Smuzhiyun 
5464*4882a593Smuzhiyun /***************************************************************************
5465*4882a593Smuzhiyun  * These are the state functions for the OTHER events.
5466*4882a593Smuzhiyun  ***************************************************************************/
5467*4882a593Smuzhiyun 
5468*4882a593Smuzhiyun /*
5469*4882a593Smuzhiyun  * When the SCTP stack has no more user data to send or retransmit, this
5470*4882a593Smuzhiyun  * notification is given to the user. Also, at the time when a user app
5471*4882a593Smuzhiyun  * subscribes to this event, if there is no data to be sent or
5472*4882a593Smuzhiyun  * retransmit, the stack will immediately send up this notification.
5473*4882a593Smuzhiyun  */
sctp_sf_do_no_pending_tsn(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5474*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_no_pending_tsn(
5475*4882a593Smuzhiyun 					struct net *net,
5476*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5477*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5478*4882a593Smuzhiyun 					const union sctp_subtype type,
5479*4882a593Smuzhiyun 					void *arg,
5480*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5481*4882a593Smuzhiyun {
5482*4882a593Smuzhiyun 	struct sctp_ulpevent *event;
5483*4882a593Smuzhiyun 
5484*4882a593Smuzhiyun 	event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5485*4882a593Smuzhiyun 	if (!event)
5486*4882a593Smuzhiyun 		return SCTP_DISPOSITION_NOMEM;
5487*4882a593Smuzhiyun 
5488*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5489*4882a593Smuzhiyun 
5490*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5491*4882a593Smuzhiyun }
5492*4882a593Smuzhiyun 
5493*4882a593Smuzhiyun /*
5494*4882a593Smuzhiyun  * Start the shutdown negotiation.
5495*4882a593Smuzhiyun  *
5496*4882a593Smuzhiyun  * From Section 9.2:
5497*4882a593Smuzhiyun  * Once all its outstanding data has been acknowledged, the endpoint
5498*4882a593Smuzhiyun  * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5499*4882a593Smuzhiyun  * TSN Ack field the last sequential TSN it has received from the peer.
5500*4882a593Smuzhiyun  * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5501*4882a593Smuzhiyun  * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5502*4882a593Smuzhiyun  * with the updated last sequential TSN received from its peer.
5503*4882a593Smuzhiyun  *
5504*4882a593Smuzhiyun  * The return value is the disposition.
5505*4882a593Smuzhiyun  */
sctp_sf_do_9_2_start_shutdown(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5506*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
5507*4882a593Smuzhiyun 					struct net *net,
5508*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5509*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5510*4882a593Smuzhiyun 					const union sctp_subtype type,
5511*4882a593Smuzhiyun 					void *arg,
5512*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5513*4882a593Smuzhiyun {
5514*4882a593Smuzhiyun 	struct sctp_chunk *reply;
5515*4882a593Smuzhiyun 
5516*4882a593Smuzhiyun 	/* Once all its outstanding data has been acknowledged, the
5517*4882a593Smuzhiyun 	 * endpoint shall send a SHUTDOWN chunk to its peer including
5518*4882a593Smuzhiyun 	 * in the Cumulative TSN Ack field the last sequential TSN it
5519*4882a593Smuzhiyun 	 * has received from the peer.
5520*4882a593Smuzhiyun 	 */
5521*4882a593Smuzhiyun 	reply = sctp_make_shutdown(asoc, arg);
5522*4882a593Smuzhiyun 	if (!reply)
5523*4882a593Smuzhiyun 		goto nomem;
5524*4882a593Smuzhiyun 
5525*4882a593Smuzhiyun 	/* Set the transport for the SHUTDOWN chunk and the timeout for the
5526*4882a593Smuzhiyun 	 * T2-shutdown timer.
5527*4882a593Smuzhiyun 	 */
5528*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5529*4882a593Smuzhiyun 
5530*4882a593Smuzhiyun 	/* It shall then start the T2-shutdown timer */
5531*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5532*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5533*4882a593Smuzhiyun 
5534*4882a593Smuzhiyun 	/* RFC 4960 Section 9.2
5535*4882a593Smuzhiyun 	 * The sender of the SHUTDOWN MAY also start an overall guard timer
5536*4882a593Smuzhiyun 	 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5537*4882a593Smuzhiyun 	 */
5538*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5539*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5540*4882a593Smuzhiyun 
5541*4882a593Smuzhiyun 	if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5542*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5543*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5544*4882a593Smuzhiyun 
5545*4882a593Smuzhiyun 	/* and enter the SHUTDOWN-SENT state.  */
5546*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5547*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5548*4882a593Smuzhiyun 
5549*4882a593Smuzhiyun 	/* sctp-implguide 2.10 Issues with Heartbeating and failover
5550*4882a593Smuzhiyun 	 *
5551*4882a593Smuzhiyun 	 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5552*4882a593Smuzhiyun 	 * or SHUTDOWN-ACK.
5553*4882a593Smuzhiyun 	 */
5554*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5555*4882a593Smuzhiyun 
5556*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5557*4882a593Smuzhiyun 
5558*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5559*4882a593Smuzhiyun 
5560*4882a593Smuzhiyun nomem:
5561*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
5562*4882a593Smuzhiyun }
5563*4882a593Smuzhiyun 
5564*4882a593Smuzhiyun /*
5565*4882a593Smuzhiyun  * Generate a SHUTDOWN ACK now that everything is SACK'd.
5566*4882a593Smuzhiyun  *
5567*4882a593Smuzhiyun  * From Section 9.2:
5568*4882a593Smuzhiyun  *
5569*4882a593Smuzhiyun  * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5570*4882a593Smuzhiyun  * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5571*4882a593Smuzhiyun  * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5572*4882a593Smuzhiyun  * endpoint must re-send the SHUTDOWN ACK.
5573*4882a593Smuzhiyun  *
5574*4882a593Smuzhiyun  * The return value is the disposition.
5575*4882a593Smuzhiyun  */
sctp_sf_do_9_2_shutdown_ack(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5576*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
5577*4882a593Smuzhiyun 					struct net *net,
5578*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5579*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5580*4882a593Smuzhiyun 					const union sctp_subtype type,
5581*4882a593Smuzhiyun 					void *arg,
5582*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5583*4882a593Smuzhiyun {
5584*4882a593Smuzhiyun 	struct sctp_chunk *chunk = arg;
5585*4882a593Smuzhiyun 	struct sctp_chunk *reply;
5586*4882a593Smuzhiyun 
5587*4882a593Smuzhiyun 	/* There are 2 ways of getting here:
5588*4882a593Smuzhiyun 	 *    1) called in response to a SHUTDOWN chunk
5589*4882a593Smuzhiyun 	 *    2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5590*4882a593Smuzhiyun 	 *
5591*4882a593Smuzhiyun 	 * For the case (2), the arg parameter is set to NULL.  We need
5592*4882a593Smuzhiyun 	 * to check that we have a chunk before accessing it's fields.
5593*4882a593Smuzhiyun 	 */
5594*4882a593Smuzhiyun 	if (chunk) {
5595*4882a593Smuzhiyun 		if (!sctp_vtag_verify(chunk, asoc))
5596*4882a593Smuzhiyun 			return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5597*4882a593Smuzhiyun 						commands);
5598*4882a593Smuzhiyun 
5599*4882a593Smuzhiyun 		/* Make sure that the SHUTDOWN chunk has a valid length. */
5600*4882a593Smuzhiyun 		if (!sctp_chunk_length_valid(
5601*4882a593Smuzhiyun 				chunk, sizeof(struct sctp_shutdown_chunk)))
5602*4882a593Smuzhiyun 			return sctp_sf_violation_chunklen(net, ep, asoc, type,
5603*4882a593Smuzhiyun 							  arg, commands);
5604*4882a593Smuzhiyun 	}
5605*4882a593Smuzhiyun 
5606*4882a593Smuzhiyun 	/* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5607*4882a593Smuzhiyun 	 * shall send a SHUTDOWN ACK ...
5608*4882a593Smuzhiyun 	 */
5609*4882a593Smuzhiyun 	reply = sctp_make_shutdown_ack(asoc, chunk);
5610*4882a593Smuzhiyun 	if (!reply)
5611*4882a593Smuzhiyun 		goto nomem;
5612*4882a593Smuzhiyun 
5613*4882a593Smuzhiyun 	/* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5614*4882a593Smuzhiyun 	 * the T2-shutdown timer.
5615*4882a593Smuzhiyun 	 */
5616*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5617*4882a593Smuzhiyun 
5618*4882a593Smuzhiyun 	/* and start/restart a T2-shutdown timer of its own, */
5619*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5620*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5621*4882a593Smuzhiyun 
5622*4882a593Smuzhiyun 	if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5623*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5624*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5625*4882a593Smuzhiyun 
5626*4882a593Smuzhiyun 	/* Enter the SHUTDOWN-ACK-SENT state.  */
5627*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5628*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5629*4882a593Smuzhiyun 
5630*4882a593Smuzhiyun 	/* sctp-implguide 2.10 Issues with Heartbeating and failover
5631*4882a593Smuzhiyun 	 *
5632*4882a593Smuzhiyun 	 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5633*4882a593Smuzhiyun 	 * or SHUTDOWN-ACK.
5634*4882a593Smuzhiyun 	 */
5635*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5636*4882a593Smuzhiyun 
5637*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5638*4882a593Smuzhiyun 
5639*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5640*4882a593Smuzhiyun 
5641*4882a593Smuzhiyun nomem:
5642*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
5643*4882a593Smuzhiyun }
5644*4882a593Smuzhiyun 
5645*4882a593Smuzhiyun /*
5646*4882a593Smuzhiyun  * Ignore the event defined as other
5647*4882a593Smuzhiyun  *
5648*4882a593Smuzhiyun  * The return value is the disposition of the event.
5649*4882a593Smuzhiyun  */
sctp_sf_ignore_other(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5650*4882a593Smuzhiyun enum sctp_disposition sctp_sf_ignore_other(struct net *net,
5651*4882a593Smuzhiyun 					   const struct sctp_endpoint *ep,
5652*4882a593Smuzhiyun 					   const struct sctp_association *asoc,
5653*4882a593Smuzhiyun 					   const union sctp_subtype type,
5654*4882a593Smuzhiyun 					   void *arg,
5655*4882a593Smuzhiyun 					   struct sctp_cmd_seq *commands)
5656*4882a593Smuzhiyun {
5657*4882a593Smuzhiyun 	pr_debug("%s: the event other type:%d is ignored\n",
5658*4882a593Smuzhiyun 		 __func__, type.other);
5659*4882a593Smuzhiyun 
5660*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DISCARD;
5661*4882a593Smuzhiyun }
5662*4882a593Smuzhiyun 
5663*4882a593Smuzhiyun /************************************************************
5664*4882a593Smuzhiyun  * These are the state functions for handling timeout events.
5665*4882a593Smuzhiyun  ************************************************************/
5666*4882a593Smuzhiyun 
5667*4882a593Smuzhiyun /*
5668*4882a593Smuzhiyun  * RTX Timeout
5669*4882a593Smuzhiyun  *
5670*4882a593Smuzhiyun  * Section: 6.3.3 Handle T3-rtx Expiration
5671*4882a593Smuzhiyun  *
5672*4882a593Smuzhiyun  * Whenever the retransmission timer T3-rtx expires for a destination
5673*4882a593Smuzhiyun  * address, do the following:
5674*4882a593Smuzhiyun  * [See below]
5675*4882a593Smuzhiyun  *
5676*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
5677*4882a593Smuzhiyun  */
sctp_sf_do_6_3_3_rtx(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5678*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
5679*4882a593Smuzhiyun 					   const struct sctp_endpoint *ep,
5680*4882a593Smuzhiyun 					   const struct sctp_association *asoc,
5681*4882a593Smuzhiyun 					   const union sctp_subtype type,
5682*4882a593Smuzhiyun 					   void *arg,
5683*4882a593Smuzhiyun 					   struct sctp_cmd_seq *commands)
5684*4882a593Smuzhiyun {
5685*4882a593Smuzhiyun 	struct sctp_transport *transport = arg;
5686*4882a593Smuzhiyun 
5687*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5688*4882a593Smuzhiyun 
5689*4882a593Smuzhiyun 	if (asoc->overall_error_count >= asoc->max_retrans) {
5690*4882a593Smuzhiyun 		if (asoc->peer.zero_window_announced &&
5691*4882a593Smuzhiyun 		    asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5692*4882a593Smuzhiyun 			/*
5693*4882a593Smuzhiyun 			 * We are here likely because the receiver had its rwnd
5694*4882a593Smuzhiyun 			 * closed for a while and we have not been able to
5695*4882a593Smuzhiyun 			 * transmit the locally queued data within the maximum
5696*4882a593Smuzhiyun 			 * retransmission attempts limit.  Start the T5
5697*4882a593Smuzhiyun 			 * shutdown guard timer to give the receiver one last
5698*4882a593Smuzhiyun 			 * chance and some additional time to recover before
5699*4882a593Smuzhiyun 			 * aborting.
5700*4882a593Smuzhiyun 			 */
5701*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5702*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5703*4882a593Smuzhiyun 		} else {
5704*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5705*4882a593Smuzhiyun 					SCTP_ERROR(ETIMEDOUT));
5706*4882a593Smuzhiyun 			/* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5707*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5708*4882a593Smuzhiyun 					SCTP_PERR(SCTP_ERROR_NO_ERROR));
5709*4882a593Smuzhiyun 			SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5710*4882a593Smuzhiyun 			SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5711*4882a593Smuzhiyun 			return SCTP_DISPOSITION_DELETE_TCB;
5712*4882a593Smuzhiyun 		}
5713*4882a593Smuzhiyun 	}
5714*4882a593Smuzhiyun 
5715*4882a593Smuzhiyun 	/* E1) For the destination address for which the timer
5716*4882a593Smuzhiyun 	 * expires, adjust its ssthresh with rules defined in Section
5717*4882a593Smuzhiyun 	 * 7.2.3 and set the cwnd <- MTU.
5718*4882a593Smuzhiyun 	 */
5719*4882a593Smuzhiyun 
5720*4882a593Smuzhiyun 	/* E2) For the destination address for which the timer
5721*4882a593Smuzhiyun 	 * expires, set RTO <- RTO * 2 ("back off the timer").  The
5722*4882a593Smuzhiyun 	 * maximum value discussed in rule C7 above (RTO.max) may be
5723*4882a593Smuzhiyun 	 * used to provide an upper bound to this doubling operation.
5724*4882a593Smuzhiyun 	 */
5725*4882a593Smuzhiyun 
5726*4882a593Smuzhiyun 	/* E3) Determine how many of the earliest (i.e., lowest TSN)
5727*4882a593Smuzhiyun 	 * outstanding DATA chunks for the address for which the
5728*4882a593Smuzhiyun 	 * T3-rtx has expired will fit into a single packet, subject
5729*4882a593Smuzhiyun 	 * to the MTU constraint for the path corresponding to the
5730*4882a593Smuzhiyun 	 * destination transport address to which the retransmission
5731*4882a593Smuzhiyun 	 * is being sent (this may be different from the address for
5732*4882a593Smuzhiyun 	 * which the timer expires [see Section 6.4]).  Call this
5733*4882a593Smuzhiyun 	 * value K. Bundle and retransmit those K DATA chunks in a
5734*4882a593Smuzhiyun 	 * single packet to the destination endpoint.
5735*4882a593Smuzhiyun 	 *
5736*4882a593Smuzhiyun 	 * Note: Any DATA chunks that were sent to the address for
5737*4882a593Smuzhiyun 	 * which the T3-rtx timer expired but did not fit in one MTU
5738*4882a593Smuzhiyun 	 * (rule E3 above), should be marked for retransmission and
5739*4882a593Smuzhiyun 	 * sent as soon as cwnd allows (normally when a SACK arrives).
5740*4882a593Smuzhiyun 	 */
5741*4882a593Smuzhiyun 
5742*4882a593Smuzhiyun 	/* Do some failure management (Section 8.2). */
5743*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5744*4882a593Smuzhiyun 
5745*4882a593Smuzhiyun 	/* NB: Rules E4 and F1 are implicit in R1.  */
5746*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5747*4882a593Smuzhiyun 
5748*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5749*4882a593Smuzhiyun }
5750*4882a593Smuzhiyun 
5751*4882a593Smuzhiyun /*
5752*4882a593Smuzhiyun  * Generate delayed SACK on timeout
5753*4882a593Smuzhiyun  *
5754*4882a593Smuzhiyun  * Section: 6.2  Acknowledgement on Reception of DATA Chunks
5755*4882a593Smuzhiyun  *
5756*4882a593Smuzhiyun  * The guidelines on delayed acknowledgement algorithm specified in
5757*4882a593Smuzhiyun  * Section 4.2 of [RFC2581] SHOULD be followed.  Specifically, an
5758*4882a593Smuzhiyun  * acknowledgement SHOULD be generated for at least every second packet
5759*4882a593Smuzhiyun  * (not every second DATA chunk) received, and SHOULD be generated
5760*4882a593Smuzhiyun  * within 200 ms of the arrival of any unacknowledged DATA chunk.  In
5761*4882a593Smuzhiyun  * some situations it may be beneficial for an SCTP transmitter to be
5762*4882a593Smuzhiyun  * more conservative than the algorithms detailed in this document
5763*4882a593Smuzhiyun  * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5764*4882a593Smuzhiyun  * the following algorithms allow.
5765*4882a593Smuzhiyun  */
sctp_sf_do_6_2_sack(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5766*4882a593Smuzhiyun enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
5767*4882a593Smuzhiyun 					  const struct sctp_endpoint *ep,
5768*4882a593Smuzhiyun 					  const struct sctp_association *asoc,
5769*4882a593Smuzhiyun 					  const union sctp_subtype type,
5770*4882a593Smuzhiyun 					  void *arg,
5771*4882a593Smuzhiyun 					  struct sctp_cmd_seq *commands)
5772*4882a593Smuzhiyun {
5773*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5774*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5775*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5776*4882a593Smuzhiyun }
5777*4882a593Smuzhiyun 
5778*4882a593Smuzhiyun /*
5779*4882a593Smuzhiyun  * sctp_sf_t1_init_timer_expire
5780*4882a593Smuzhiyun  *
5781*4882a593Smuzhiyun  * Section: 4 Note: 2
5782*4882a593Smuzhiyun  * Verification Tag:
5783*4882a593Smuzhiyun  * Inputs
5784*4882a593Smuzhiyun  * (endpoint, asoc)
5785*4882a593Smuzhiyun  *
5786*4882a593Smuzhiyun  *  RFC 2960 Section 4 Notes
5787*4882a593Smuzhiyun  *  2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5788*4882a593Smuzhiyun  *     and re-start the T1-init timer without changing state.  This MUST
5789*4882a593Smuzhiyun  *     be repeated up to 'Max.Init.Retransmits' times.  After that, the
5790*4882a593Smuzhiyun  *     endpoint MUST abort the initialization process and report the
5791*4882a593Smuzhiyun  *     error to SCTP user.
5792*4882a593Smuzhiyun  *
5793*4882a593Smuzhiyun  * Outputs
5794*4882a593Smuzhiyun  * (timers, events)
5795*4882a593Smuzhiyun  *
5796*4882a593Smuzhiyun  */
sctp_sf_t1_init_timer_expire(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5797*4882a593Smuzhiyun enum sctp_disposition sctp_sf_t1_init_timer_expire(
5798*4882a593Smuzhiyun 					struct net *net,
5799*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5800*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5801*4882a593Smuzhiyun 					const union sctp_subtype type,
5802*4882a593Smuzhiyun 					void *arg,
5803*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5804*4882a593Smuzhiyun {
5805*4882a593Smuzhiyun 	int attempts = asoc->init_err_counter + 1;
5806*4882a593Smuzhiyun 	struct sctp_chunk *repl = NULL;
5807*4882a593Smuzhiyun 	struct sctp_bind_addr *bp;
5808*4882a593Smuzhiyun 
5809*4882a593Smuzhiyun 	pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5810*4882a593Smuzhiyun 
5811*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5812*4882a593Smuzhiyun 
5813*4882a593Smuzhiyun 	if (attempts <= asoc->max_init_attempts) {
5814*4882a593Smuzhiyun 		bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5815*4882a593Smuzhiyun 		repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5816*4882a593Smuzhiyun 		if (!repl)
5817*4882a593Smuzhiyun 			return SCTP_DISPOSITION_NOMEM;
5818*4882a593Smuzhiyun 
5819*4882a593Smuzhiyun 		/* Choose transport for INIT. */
5820*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5821*4882a593Smuzhiyun 				SCTP_CHUNK(repl));
5822*4882a593Smuzhiyun 
5823*4882a593Smuzhiyun 		/* Issue a sideeffect to do the needed accounting. */
5824*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5825*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5826*4882a593Smuzhiyun 
5827*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5828*4882a593Smuzhiyun 	} else {
5829*4882a593Smuzhiyun 		pr_debug("%s: giving up on INIT, attempts:%d "
5830*4882a593Smuzhiyun 			 "max_init_attempts:%d\n", __func__, attempts,
5831*4882a593Smuzhiyun 			 asoc->max_init_attempts);
5832*4882a593Smuzhiyun 
5833*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5834*4882a593Smuzhiyun 				SCTP_ERROR(ETIMEDOUT));
5835*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5836*4882a593Smuzhiyun 				SCTP_PERR(SCTP_ERROR_NO_ERROR));
5837*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DELETE_TCB;
5838*4882a593Smuzhiyun 	}
5839*4882a593Smuzhiyun 
5840*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5841*4882a593Smuzhiyun }
5842*4882a593Smuzhiyun 
5843*4882a593Smuzhiyun /*
5844*4882a593Smuzhiyun  * sctp_sf_t1_cookie_timer_expire
5845*4882a593Smuzhiyun  *
5846*4882a593Smuzhiyun  * Section: 4 Note: 2
5847*4882a593Smuzhiyun  * Verification Tag:
5848*4882a593Smuzhiyun  * Inputs
5849*4882a593Smuzhiyun  * (endpoint, asoc)
5850*4882a593Smuzhiyun  *
5851*4882a593Smuzhiyun  *  RFC 2960 Section 4 Notes
5852*4882a593Smuzhiyun  *  3) If the T1-cookie timer expires, the endpoint MUST retransmit
5853*4882a593Smuzhiyun  *     COOKIE ECHO and re-start the T1-cookie timer without changing
5854*4882a593Smuzhiyun  *     state.  This MUST be repeated up to 'Max.Init.Retransmits' times.
5855*4882a593Smuzhiyun  *     After that, the endpoint MUST abort the initialization process and
5856*4882a593Smuzhiyun  *     report the error to SCTP user.
5857*4882a593Smuzhiyun  *
5858*4882a593Smuzhiyun  * Outputs
5859*4882a593Smuzhiyun  * (timers, events)
5860*4882a593Smuzhiyun  *
5861*4882a593Smuzhiyun  */
sctp_sf_t1_cookie_timer_expire(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5862*4882a593Smuzhiyun enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
5863*4882a593Smuzhiyun 					struct net *net,
5864*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5865*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5866*4882a593Smuzhiyun 					const union sctp_subtype type,
5867*4882a593Smuzhiyun 					void *arg,
5868*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5869*4882a593Smuzhiyun {
5870*4882a593Smuzhiyun 	int attempts = asoc->init_err_counter + 1;
5871*4882a593Smuzhiyun 	struct sctp_chunk *repl = NULL;
5872*4882a593Smuzhiyun 
5873*4882a593Smuzhiyun 	pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5874*4882a593Smuzhiyun 
5875*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5876*4882a593Smuzhiyun 
5877*4882a593Smuzhiyun 	if (attempts <= asoc->max_init_attempts) {
5878*4882a593Smuzhiyun 		repl = sctp_make_cookie_echo(asoc, NULL);
5879*4882a593Smuzhiyun 		if (!repl)
5880*4882a593Smuzhiyun 			return SCTP_DISPOSITION_NOMEM;
5881*4882a593Smuzhiyun 
5882*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5883*4882a593Smuzhiyun 				SCTP_CHUNK(repl));
5884*4882a593Smuzhiyun 		/* Issue a sideeffect to do the needed accounting. */
5885*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5886*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5887*4882a593Smuzhiyun 
5888*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5889*4882a593Smuzhiyun 	} else {
5890*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5891*4882a593Smuzhiyun 				SCTP_ERROR(ETIMEDOUT));
5892*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5893*4882a593Smuzhiyun 				SCTP_PERR(SCTP_ERROR_NO_ERROR));
5894*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DELETE_TCB;
5895*4882a593Smuzhiyun 	}
5896*4882a593Smuzhiyun 
5897*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5898*4882a593Smuzhiyun }
5899*4882a593Smuzhiyun 
5900*4882a593Smuzhiyun /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5901*4882a593Smuzhiyun  * with the updated last sequential TSN received from its peer.
5902*4882a593Smuzhiyun  *
5903*4882a593Smuzhiyun  * An endpoint should limit the number of retransmissions of the
5904*4882a593Smuzhiyun  * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5905*4882a593Smuzhiyun  * If this threshold is exceeded the endpoint should destroy the TCB and
5906*4882a593Smuzhiyun  * MUST report the peer endpoint unreachable to the upper layer (and
5907*4882a593Smuzhiyun  * thus the association enters the CLOSED state).  The reception of any
5908*4882a593Smuzhiyun  * packet from its peer (i.e. as the peer sends all of its queued DATA
5909*4882a593Smuzhiyun  * chunks) should clear the endpoint's retransmission count and restart
5910*4882a593Smuzhiyun  * the T2-Shutdown timer,  giving its peer ample opportunity to transmit
5911*4882a593Smuzhiyun  * all of its queued DATA chunks that have not yet been sent.
5912*4882a593Smuzhiyun  */
sctp_sf_t2_timer_expire(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5913*4882a593Smuzhiyun enum sctp_disposition sctp_sf_t2_timer_expire(
5914*4882a593Smuzhiyun 					struct net *net,
5915*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5916*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5917*4882a593Smuzhiyun 					const union sctp_subtype type,
5918*4882a593Smuzhiyun 					void *arg,
5919*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5920*4882a593Smuzhiyun {
5921*4882a593Smuzhiyun 	struct sctp_chunk *reply = NULL;
5922*4882a593Smuzhiyun 
5923*4882a593Smuzhiyun 	pr_debug("%s: timer T2 expired\n", __func__);
5924*4882a593Smuzhiyun 
5925*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5926*4882a593Smuzhiyun 
5927*4882a593Smuzhiyun 	((struct sctp_association *)asoc)->shutdown_retries++;
5928*4882a593Smuzhiyun 
5929*4882a593Smuzhiyun 	if (asoc->overall_error_count >= asoc->max_retrans) {
5930*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5931*4882a593Smuzhiyun 				SCTP_ERROR(ETIMEDOUT));
5932*4882a593Smuzhiyun 		/* Note:  CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5933*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5934*4882a593Smuzhiyun 				SCTP_PERR(SCTP_ERROR_NO_ERROR));
5935*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5936*4882a593Smuzhiyun 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5937*4882a593Smuzhiyun 		return SCTP_DISPOSITION_DELETE_TCB;
5938*4882a593Smuzhiyun 	}
5939*4882a593Smuzhiyun 
5940*4882a593Smuzhiyun 	switch (asoc->state) {
5941*4882a593Smuzhiyun 	case SCTP_STATE_SHUTDOWN_SENT:
5942*4882a593Smuzhiyun 		reply = sctp_make_shutdown(asoc, NULL);
5943*4882a593Smuzhiyun 		break;
5944*4882a593Smuzhiyun 
5945*4882a593Smuzhiyun 	case SCTP_STATE_SHUTDOWN_ACK_SENT:
5946*4882a593Smuzhiyun 		reply = sctp_make_shutdown_ack(asoc, NULL);
5947*4882a593Smuzhiyun 		break;
5948*4882a593Smuzhiyun 
5949*4882a593Smuzhiyun 	default:
5950*4882a593Smuzhiyun 		BUG();
5951*4882a593Smuzhiyun 		break;
5952*4882a593Smuzhiyun 	}
5953*4882a593Smuzhiyun 
5954*4882a593Smuzhiyun 	if (!reply)
5955*4882a593Smuzhiyun 		goto nomem;
5956*4882a593Smuzhiyun 
5957*4882a593Smuzhiyun 	/* Do some failure management (Section 8.2).
5958*4882a593Smuzhiyun 	 * If we remove the transport an SHUTDOWN was last sent to, don't
5959*4882a593Smuzhiyun 	 * do failure management.
5960*4882a593Smuzhiyun 	 */
5961*4882a593Smuzhiyun 	if (asoc->shutdown_last_sent_to)
5962*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5963*4882a593Smuzhiyun 				SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5964*4882a593Smuzhiyun 
5965*4882a593Smuzhiyun 	/* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5966*4882a593Smuzhiyun 	 * the T2-shutdown timer.
5967*4882a593Smuzhiyun 	 */
5968*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5969*4882a593Smuzhiyun 
5970*4882a593Smuzhiyun 	/* Restart the T2-shutdown timer.  */
5971*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5972*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5973*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5974*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
5975*4882a593Smuzhiyun 
5976*4882a593Smuzhiyun nomem:
5977*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
5978*4882a593Smuzhiyun }
5979*4882a593Smuzhiyun 
5980*4882a593Smuzhiyun /*
5981*4882a593Smuzhiyun  * ADDIP Section 4.1 ASCONF CHunk Procedures
5982*4882a593Smuzhiyun  * If the T4 RTO timer expires the endpoint should do B1 to B5
5983*4882a593Smuzhiyun  */
sctp_sf_t4_timer_expire(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)5984*4882a593Smuzhiyun enum sctp_disposition sctp_sf_t4_timer_expire(
5985*4882a593Smuzhiyun 					struct net *net,
5986*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
5987*4882a593Smuzhiyun 					const struct sctp_association *asoc,
5988*4882a593Smuzhiyun 					const union sctp_subtype type,
5989*4882a593Smuzhiyun 					void *arg,
5990*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
5991*4882a593Smuzhiyun {
5992*4882a593Smuzhiyun 	struct sctp_chunk *chunk = asoc->addip_last_asconf;
5993*4882a593Smuzhiyun 	struct sctp_transport *transport = chunk->transport;
5994*4882a593Smuzhiyun 
5995*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5996*4882a593Smuzhiyun 
5997*4882a593Smuzhiyun 	/* ADDIP 4.1 B1) Increment the error counters and perform path failure
5998*4882a593Smuzhiyun 	 * detection on the appropriate destination address as defined in
5999*4882a593Smuzhiyun 	 * RFC2960 [5] section 8.1 and 8.2.
6000*4882a593Smuzhiyun 	 */
6001*4882a593Smuzhiyun 	if (transport)
6002*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
6003*4882a593Smuzhiyun 				SCTP_TRANSPORT(transport));
6004*4882a593Smuzhiyun 
6005*4882a593Smuzhiyun 	/* Reconfig T4 timer and transport. */
6006*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
6007*4882a593Smuzhiyun 
6008*4882a593Smuzhiyun 	/* ADDIP 4.1 B2) Increment the association error counters and perform
6009*4882a593Smuzhiyun 	 * endpoint failure detection on the association as defined in
6010*4882a593Smuzhiyun 	 * RFC2960 [5] section 8.1 and 8.2.
6011*4882a593Smuzhiyun 	 * association error counter is incremented in SCTP_CMD_STRIKE.
6012*4882a593Smuzhiyun 	 */
6013*4882a593Smuzhiyun 	if (asoc->overall_error_count >= asoc->max_retrans) {
6014*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
6015*4882a593Smuzhiyun 				SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
6016*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6017*4882a593Smuzhiyun 				SCTP_ERROR(ETIMEDOUT));
6018*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6019*4882a593Smuzhiyun 				SCTP_PERR(SCTP_ERROR_NO_ERROR));
6020*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6021*4882a593Smuzhiyun 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6022*4882a593Smuzhiyun 		return SCTP_DISPOSITION_ABORT;
6023*4882a593Smuzhiyun 	}
6024*4882a593Smuzhiyun 
6025*4882a593Smuzhiyun 	/* ADDIP 4.1 B3) Back-off the destination address RTO value to which
6026*4882a593Smuzhiyun 	 * the ASCONF chunk was sent by doubling the RTO timer value.
6027*4882a593Smuzhiyun 	 * This is done in SCTP_CMD_STRIKE.
6028*4882a593Smuzhiyun 	 */
6029*4882a593Smuzhiyun 
6030*4882a593Smuzhiyun 	/* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
6031*4882a593Smuzhiyun 	 * choose an alternate destination address (please refer to RFC2960
6032*4882a593Smuzhiyun 	 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
6033*4882a593Smuzhiyun 	 * chunk, it MUST be the same (including its serial number) as the last
6034*4882a593Smuzhiyun 	 * ASCONF sent.
6035*4882a593Smuzhiyun 	 */
6036*4882a593Smuzhiyun 	sctp_chunk_hold(asoc->addip_last_asconf);
6037*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6038*4882a593Smuzhiyun 			SCTP_CHUNK(asoc->addip_last_asconf));
6039*4882a593Smuzhiyun 
6040*4882a593Smuzhiyun 	/* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
6041*4882a593Smuzhiyun 	 * destination is selected, then the RTO used will be that of the new
6042*4882a593Smuzhiyun 	 * destination address.
6043*4882a593Smuzhiyun 	 */
6044*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
6045*4882a593Smuzhiyun 			SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
6046*4882a593Smuzhiyun 
6047*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
6048*4882a593Smuzhiyun }
6049*4882a593Smuzhiyun 
6050*4882a593Smuzhiyun /* sctpimpguide-05 Section 2.12.2
6051*4882a593Smuzhiyun  * The sender of the SHUTDOWN MAY also start an overall guard timer
6052*4882a593Smuzhiyun  * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
6053*4882a593Smuzhiyun  * At the expiration of this timer the sender SHOULD abort the association
6054*4882a593Smuzhiyun  * by sending an ABORT chunk.
6055*4882a593Smuzhiyun  */
sctp_sf_t5_timer_expire(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)6056*4882a593Smuzhiyun enum sctp_disposition sctp_sf_t5_timer_expire(
6057*4882a593Smuzhiyun 					struct net *net,
6058*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
6059*4882a593Smuzhiyun 					const struct sctp_association *asoc,
6060*4882a593Smuzhiyun 					const union sctp_subtype type,
6061*4882a593Smuzhiyun 					void *arg,
6062*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
6063*4882a593Smuzhiyun {
6064*4882a593Smuzhiyun 	struct sctp_chunk *reply = NULL;
6065*4882a593Smuzhiyun 
6066*4882a593Smuzhiyun 	pr_debug("%s: timer T5 expired\n", __func__);
6067*4882a593Smuzhiyun 
6068*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
6069*4882a593Smuzhiyun 
6070*4882a593Smuzhiyun 	reply = sctp_make_abort(asoc, NULL, 0);
6071*4882a593Smuzhiyun 	if (!reply)
6072*4882a593Smuzhiyun 		goto nomem;
6073*4882a593Smuzhiyun 
6074*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
6075*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6076*4882a593Smuzhiyun 			SCTP_ERROR(ETIMEDOUT));
6077*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6078*4882a593Smuzhiyun 			SCTP_PERR(SCTP_ERROR_NO_ERROR));
6079*4882a593Smuzhiyun 
6080*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6081*4882a593Smuzhiyun 	SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6082*4882a593Smuzhiyun 
6083*4882a593Smuzhiyun 	return SCTP_DISPOSITION_DELETE_TCB;
6084*4882a593Smuzhiyun nomem:
6085*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOMEM;
6086*4882a593Smuzhiyun }
6087*4882a593Smuzhiyun 
6088*4882a593Smuzhiyun /* Handle expiration of AUTOCLOSE timer.  When the autoclose timer expires,
6089*4882a593Smuzhiyun  * the association is automatically closed by starting the shutdown process.
6090*4882a593Smuzhiyun  * The work that needs to be done is same as when SHUTDOWN is initiated by
6091*4882a593Smuzhiyun  * the user.  So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
6092*4882a593Smuzhiyun  */
sctp_sf_autoclose_timer_expire(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)6093*4882a593Smuzhiyun enum sctp_disposition sctp_sf_autoclose_timer_expire(
6094*4882a593Smuzhiyun 					struct net *net,
6095*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
6096*4882a593Smuzhiyun 					const struct sctp_association *asoc,
6097*4882a593Smuzhiyun 					const union sctp_subtype type,
6098*4882a593Smuzhiyun 					void *arg,
6099*4882a593Smuzhiyun 					struct sctp_cmd_seq *commands)
6100*4882a593Smuzhiyun {
6101*4882a593Smuzhiyun 	enum sctp_disposition disposition;
6102*4882a593Smuzhiyun 
6103*4882a593Smuzhiyun 	SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
6104*4882a593Smuzhiyun 
6105*4882a593Smuzhiyun 	/* From 9.2 Shutdown of an Association
6106*4882a593Smuzhiyun 	 * Upon receipt of the SHUTDOWN primitive from its upper
6107*4882a593Smuzhiyun 	 * layer, the endpoint enters SHUTDOWN-PENDING state and
6108*4882a593Smuzhiyun 	 * remains there until all outstanding data has been
6109*4882a593Smuzhiyun 	 * acknowledged by its peer. The endpoint accepts no new data
6110*4882a593Smuzhiyun 	 * from its upper layer, but retransmits data to the far end
6111*4882a593Smuzhiyun 	 * if necessary to fill gaps.
6112*4882a593Smuzhiyun 	 */
6113*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
6114*4882a593Smuzhiyun 			SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
6115*4882a593Smuzhiyun 
6116*4882a593Smuzhiyun 	disposition = SCTP_DISPOSITION_CONSUME;
6117*4882a593Smuzhiyun 	if (sctp_outq_is_empty(&asoc->outqueue)) {
6118*4882a593Smuzhiyun 		disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
6119*4882a593Smuzhiyun 							    NULL, commands);
6120*4882a593Smuzhiyun 	}
6121*4882a593Smuzhiyun 
6122*4882a593Smuzhiyun 	return disposition;
6123*4882a593Smuzhiyun }
6124*4882a593Smuzhiyun 
6125*4882a593Smuzhiyun /*****************************************************************************
6126*4882a593Smuzhiyun  * These are sa state functions which could apply to all types of events.
6127*4882a593Smuzhiyun  ****************************************************************************/
6128*4882a593Smuzhiyun 
6129*4882a593Smuzhiyun /*
6130*4882a593Smuzhiyun  * This table entry is not implemented.
6131*4882a593Smuzhiyun  *
6132*4882a593Smuzhiyun  * Inputs
6133*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
6134*4882a593Smuzhiyun  *
6135*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
6136*4882a593Smuzhiyun  */
sctp_sf_not_impl(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)6137*4882a593Smuzhiyun enum sctp_disposition sctp_sf_not_impl(struct net *net,
6138*4882a593Smuzhiyun 				       const struct sctp_endpoint *ep,
6139*4882a593Smuzhiyun 				       const struct sctp_association *asoc,
6140*4882a593Smuzhiyun 				       const union sctp_subtype type,
6141*4882a593Smuzhiyun 				       void *arg, struct sctp_cmd_seq *commands)
6142*4882a593Smuzhiyun {
6143*4882a593Smuzhiyun 	return SCTP_DISPOSITION_NOT_IMPL;
6144*4882a593Smuzhiyun }
6145*4882a593Smuzhiyun 
6146*4882a593Smuzhiyun /*
6147*4882a593Smuzhiyun  * This table entry represents a bug.
6148*4882a593Smuzhiyun  *
6149*4882a593Smuzhiyun  * Inputs
6150*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
6151*4882a593Smuzhiyun  *
6152*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
6153*4882a593Smuzhiyun  */
sctp_sf_bug(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)6154*4882a593Smuzhiyun enum sctp_disposition sctp_sf_bug(struct net *net,
6155*4882a593Smuzhiyun 				  const struct sctp_endpoint *ep,
6156*4882a593Smuzhiyun 				  const struct sctp_association *asoc,
6157*4882a593Smuzhiyun 				  const union sctp_subtype type,
6158*4882a593Smuzhiyun 				  void *arg, struct sctp_cmd_seq *commands)
6159*4882a593Smuzhiyun {
6160*4882a593Smuzhiyun 	return SCTP_DISPOSITION_BUG;
6161*4882a593Smuzhiyun }
6162*4882a593Smuzhiyun 
6163*4882a593Smuzhiyun /*
6164*4882a593Smuzhiyun  * This table entry represents the firing of a timer in the wrong state.
6165*4882a593Smuzhiyun  * Since timer deletion cannot be guaranteed a timer 'may' end up firing
6166*4882a593Smuzhiyun  * when the association is in the wrong state.   This event should
6167*4882a593Smuzhiyun  * be ignored, so as to prevent any rearming of the timer.
6168*4882a593Smuzhiyun  *
6169*4882a593Smuzhiyun  * Inputs
6170*4882a593Smuzhiyun  * (endpoint, asoc, chunk)
6171*4882a593Smuzhiyun  *
6172*4882a593Smuzhiyun  * The return value is the disposition of the chunk.
6173*4882a593Smuzhiyun  */
sctp_sf_timer_ignore(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const union sctp_subtype type,void * arg,struct sctp_cmd_seq * commands)6174*4882a593Smuzhiyun enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
6175*4882a593Smuzhiyun 					   const struct sctp_endpoint *ep,
6176*4882a593Smuzhiyun 					   const struct sctp_association *asoc,
6177*4882a593Smuzhiyun 					   const union sctp_subtype type,
6178*4882a593Smuzhiyun 					   void *arg,
6179*4882a593Smuzhiyun 					   struct sctp_cmd_seq *commands)
6180*4882a593Smuzhiyun {
6181*4882a593Smuzhiyun 	pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6182*4882a593Smuzhiyun 
6183*4882a593Smuzhiyun 	return SCTP_DISPOSITION_CONSUME;
6184*4882a593Smuzhiyun }
6185*4882a593Smuzhiyun 
6186*4882a593Smuzhiyun /********************************************************************
6187*4882a593Smuzhiyun  * 2nd Level Abstractions
6188*4882a593Smuzhiyun  ********************************************************************/
6189*4882a593Smuzhiyun 
6190*4882a593Smuzhiyun /* Pull the SACK chunk based on the SACK header. */
sctp_sm_pull_sack(struct sctp_chunk * chunk)6191*4882a593Smuzhiyun static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6192*4882a593Smuzhiyun {
6193*4882a593Smuzhiyun 	struct sctp_sackhdr *sack;
6194*4882a593Smuzhiyun 	__u16 num_dup_tsns;
6195*4882a593Smuzhiyun 	unsigned int len;
6196*4882a593Smuzhiyun 	__u16 num_blocks;
6197*4882a593Smuzhiyun 
6198*4882a593Smuzhiyun 	/* Protect ourselves from reading too far into
6199*4882a593Smuzhiyun 	 * the skb from a bogus sender.
6200*4882a593Smuzhiyun 	 */
6201*4882a593Smuzhiyun 	sack = (struct sctp_sackhdr *) chunk->skb->data;
6202*4882a593Smuzhiyun 
6203*4882a593Smuzhiyun 	num_blocks = ntohs(sack->num_gap_ack_blocks);
6204*4882a593Smuzhiyun 	num_dup_tsns = ntohs(sack->num_dup_tsns);
6205*4882a593Smuzhiyun 	len = sizeof(struct sctp_sackhdr);
6206*4882a593Smuzhiyun 	len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6207*4882a593Smuzhiyun 	if (len > chunk->skb->len)
6208*4882a593Smuzhiyun 		return NULL;
6209*4882a593Smuzhiyun 
6210*4882a593Smuzhiyun 	skb_pull(chunk->skb, len);
6211*4882a593Smuzhiyun 
6212*4882a593Smuzhiyun 	return sack;
6213*4882a593Smuzhiyun }
6214*4882a593Smuzhiyun 
6215*4882a593Smuzhiyun /* Create an ABORT packet to be sent as a response, with the specified
6216*4882a593Smuzhiyun  * error causes.
6217*4882a593Smuzhiyun  */
sctp_abort_pkt_new(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,struct sctp_chunk * chunk,const void * payload,size_t paylen)6218*4882a593Smuzhiyun static struct sctp_packet *sctp_abort_pkt_new(
6219*4882a593Smuzhiyun 					struct net *net,
6220*4882a593Smuzhiyun 					const struct sctp_endpoint *ep,
6221*4882a593Smuzhiyun 					const struct sctp_association *asoc,
6222*4882a593Smuzhiyun 					struct sctp_chunk *chunk,
6223*4882a593Smuzhiyun 					const void *payload, size_t paylen)
6224*4882a593Smuzhiyun {
6225*4882a593Smuzhiyun 	struct sctp_packet *packet;
6226*4882a593Smuzhiyun 	struct sctp_chunk *abort;
6227*4882a593Smuzhiyun 
6228*4882a593Smuzhiyun 	packet = sctp_ootb_pkt_new(net, asoc, chunk);
6229*4882a593Smuzhiyun 
6230*4882a593Smuzhiyun 	if (packet) {
6231*4882a593Smuzhiyun 		/* Make an ABORT.
6232*4882a593Smuzhiyun 		 * The T bit will be set if the asoc is NULL.
6233*4882a593Smuzhiyun 		 */
6234*4882a593Smuzhiyun 		abort = sctp_make_abort(asoc, chunk, paylen);
6235*4882a593Smuzhiyun 		if (!abort) {
6236*4882a593Smuzhiyun 			sctp_ootb_pkt_free(packet);
6237*4882a593Smuzhiyun 			return NULL;
6238*4882a593Smuzhiyun 		}
6239*4882a593Smuzhiyun 
6240*4882a593Smuzhiyun 		/* Reflect vtag if T-Bit is set */
6241*4882a593Smuzhiyun 		if (sctp_test_T_bit(abort))
6242*4882a593Smuzhiyun 			packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6243*4882a593Smuzhiyun 
6244*4882a593Smuzhiyun 		/* Add specified error causes, i.e., payload, to the
6245*4882a593Smuzhiyun 		 * end of the chunk.
6246*4882a593Smuzhiyun 		 */
6247*4882a593Smuzhiyun 		sctp_addto_chunk(abort, paylen, payload);
6248*4882a593Smuzhiyun 
6249*4882a593Smuzhiyun 		/* Set the skb to the belonging sock for accounting.  */
6250*4882a593Smuzhiyun 		abort->skb->sk = ep->base.sk;
6251*4882a593Smuzhiyun 
6252*4882a593Smuzhiyun 		sctp_packet_append_chunk(packet, abort);
6253*4882a593Smuzhiyun 
6254*4882a593Smuzhiyun 	}
6255*4882a593Smuzhiyun 
6256*4882a593Smuzhiyun 	return packet;
6257*4882a593Smuzhiyun }
6258*4882a593Smuzhiyun 
6259*4882a593Smuzhiyun /* Allocate a packet for responding in the OOTB conditions.  */
sctp_ootb_pkt_new(struct net * net,const struct sctp_association * asoc,const struct sctp_chunk * chunk)6260*4882a593Smuzhiyun static struct sctp_packet *sctp_ootb_pkt_new(
6261*4882a593Smuzhiyun 					struct net *net,
6262*4882a593Smuzhiyun 					const struct sctp_association *asoc,
6263*4882a593Smuzhiyun 					const struct sctp_chunk *chunk)
6264*4882a593Smuzhiyun {
6265*4882a593Smuzhiyun 	struct sctp_transport *transport;
6266*4882a593Smuzhiyun 	struct sctp_packet *packet;
6267*4882a593Smuzhiyun 	__u16 sport, dport;
6268*4882a593Smuzhiyun 	__u32 vtag;
6269*4882a593Smuzhiyun 
6270*4882a593Smuzhiyun 	/* Get the source and destination port from the inbound packet.  */
6271*4882a593Smuzhiyun 	sport = ntohs(chunk->sctp_hdr->dest);
6272*4882a593Smuzhiyun 	dport = ntohs(chunk->sctp_hdr->source);
6273*4882a593Smuzhiyun 
6274*4882a593Smuzhiyun 	/* The V-tag is going to be the same as the inbound packet if no
6275*4882a593Smuzhiyun 	 * association exists, otherwise, use the peer's vtag.
6276*4882a593Smuzhiyun 	 */
6277*4882a593Smuzhiyun 	if (asoc) {
6278*4882a593Smuzhiyun 		/* Special case the INIT-ACK as there is no peer's vtag
6279*4882a593Smuzhiyun 		 * yet.
6280*4882a593Smuzhiyun 		 */
6281*4882a593Smuzhiyun 		switch (chunk->chunk_hdr->type) {
6282*4882a593Smuzhiyun 		case SCTP_CID_INIT:
6283*4882a593Smuzhiyun 		case SCTP_CID_INIT_ACK:
6284*4882a593Smuzhiyun 		{
6285*4882a593Smuzhiyun 			struct sctp_initack_chunk *initack;
6286*4882a593Smuzhiyun 
6287*4882a593Smuzhiyun 			initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
6288*4882a593Smuzhiyun 			vtag = ntohl(initack->init_hdr.init_tag);
6289*4882a593Smuzhiyun 			break;
6290*4882a593Smuzhiyun 		}
6291*4882a593Smuzhiyun 		default:
6292*4882a593Smuzhiyun 			vtag = asoc->peer.i.init_tag;
6293*4882a593Smuzhiyun 			break;
6294*4882a593Smuzhiyun 		}
6295*4882a593Smuzhiyun 	} else {
6296*4882a593Smuzhiyun 		/* Special case the INIT and stale COOKIE_ECHO as there is no
6297*4882a593Smuzhiyun 		 * vtag yet.
6298*4882a593Smuzhiyun 		 */
6299*4882a593Smuzhiyun 		switch (chunk->chunk_hdr->type) {
6300*4882a593Smuzhiyun 		case SCTP_CID_INIT:
6301*4882a593Smuzhiyun 		{
6302*4882a593Smuzhiyun 			struct sctp_init_chunk *init;
6303*4882a593Smuzhiyun 
6304*4882a593Smuzhiyun 			init = (struct sctp_init_chunk *)chunk->chunk_hdr;
6305*4882a593Smuzhiyun 			vtag = ntohl(init->init_hdr.init_tag);
6306*4882a593Smuzhiyun 			break;
6307*4882a593Smuzhiyun 		}
6308*4882a593Smuzhiyun 		default:
6309*4882a593Smuzhiyun 			vtag = ntohl(chunk->sctp_hdr->vtag);
6310*4882a593Smuzhiyun 			break;
6311*4882a593Smuzhiyun 		}
6312*4882a593Smuzhiyun 	}
6313*4882a593Smuzhiyun 
6314*4882a593Smuzhiyun 	/* Make a transport for the bucket, Eliza... */
6315*4882a593Smuzhiyun 	transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6316*4882a593Smuzhiyun 	if (!transport)
6317*4882a593Smuzhiyun 		goto nomem;
6318*4882a593Smuzhiyun 
6319*4882a593Smuzhiyun 	/* Cache a route for the transport with the chunk's destination as
6320*4882a593Smuzhiyun 	 * the source address.
6321*4882a593Smuzhiyun 	 */
6322*4882a593Smuzhiyun 	sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6323*4882a593Smuzhiyun 			     sctp_sk(net->sctp.ctl_sock));
6324*4882a593Smuzhiyun 
6325*4882a593Smuzhiyun 	packet = &transport->packet;
6326*4882a593Smuzhiyun 	sctp_packet_init(packet, transport, sport, dport);
6327*4882a593Smuzhiyun 	sctp_packet_config(packet, vtag, 0);
6328*4882a593Smuzhiyun 
6329*4882a593Smuzhiyun 	return packet;
6330*4882a593Smuzhiyun 
6331*4882a593Smuzhiyun nomem:
6332*4882a593Smuzhiyun 	return NULL;
6333*4882a593Smuzhiyun }
6334*4882a593Smuzhiyun 
6335*4882a593Smuzhiyun /* Free the packet allocated earlier for responding in the OOTB condition.  */
sctp_ootb_pkt_free(struct sctp_packet * packet)6336*4882a593Smuzhiyun void sctp_ootb_pkt_free(struct sctp_packet *packet)
6337*4882a593Smuzhiyun {
6338*4882a593Smuzhiyun 	sctp_transport_free(packet->transport);
6339*4882a593Smuzhiyun }
6340*4882a593Smuzhiyun 
6341*4882a593Smuzhiyun /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found  */
sctp_send_stale_cookie_err(struct net * net,const struct sctp_endpoint * ep,const struct sctp_association * asoc,const struct sctp_chunk * chunk,struct sctp_cmd_seq * commands,struct sctp_chunk * err_chunk)6342*4882a593Smuzhiyun static void sctp_send_stale_cookie_err(struct net *net,
6343*4882a593Smuzhiyun 				       const struct sctp_endpoint *ep,
6344*4882a593Smuzhiyun 				       const struct sctp_association *asoc,
6345*4882a593Smuzhiyun 				       const struct sctp_chunk *chunk,
6346*4882a593Smuzhiyun 				       struct sctp_cmd_seq *commands,
6347*4882a593Smuzhiyun 				       struct sctp_chunk *err_chunk)
6348*4882a593Smuzhiyun {
6349*4882a593Smuzhiyun 	struct sctp_packet *packet;
6350*4882a593Smuzhiyun 
6351*4882a593Smuzhiyun 	if (err_chunk) {
6352*4882a593Smuzhiyun 		packet = sctp_ootb_pkt_new(net, asoc, chunk);
6353*4882a593Smuzhiyun 		if (packet) {
6354*4882a593Smuzhiyun 			struct sctp_signed_cookie *cookie;
6355*4882a593Smuzhiyun 
6356*4882a593Smuzhiyun 			/* Override the OOTB vtag from the cookie. */
6357*4882a593Smuzhiyun 			cookie = chunk->subh.cookie_hdr;
6358*4882a593Smuzhiyun 			packet->vtag = cookie->c.peer_vtag;
6359*4882a593Smuzhiyun 
6360*4882a593Smuzhiyun 			/* Set the skb to the belonging sock for accounting. */
6361*4882a593Smuzhiyun 			err_chunk->skb->sk = ep->base.sk;
6362*4882a593Smuzhiyun 			sctp_packet_append_chunk(packet, err_chunk);
6363*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6364*4882a593Smuzhiyun 					SCTP_PACKET(packet));
6365*4882a593Smuzhiyun 			SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6366*4882a593Smuzhiyun 		} else
6367*4882a593Smuzhiyun 			sctp_chunk_free (err_chunk);
6368*4882a593Smuzhiyun 	}
6369*4882a593Smuzhiyun }
6370*4882a593Smuzhiyun 
6371*4882a593Smuzhiyun 
6372*4882a593Smuzhiyun /* Process a data chunk */
sctp_eat_data(const struct sctp_association * asoc,struct sctp_chunk * chunk,struct sctp_cmd_seq * commands)6373*4882a593Smuzhiyun static int sctp_eat_data(const struct sctp_association *asoc,
6374*4882a593Smuzhiyun 			 struct sctp_chunk *chunk,
6375*4882a593Smuzhiyun 			 struct sctp_cmd_seq *commands)
6376*4882a593Smuzhiyun {
6377*4882a593Smuzhiyun 	struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6378*4882a593Smuzhiyun 	struct sock *sk = asoc->base.sk;
6379*4882a593Smuzhiyun 	struct net *net = sock_net(sk);
6380*4882a593Smuzhiyun 	struct sctp_datahdr *data_hdr;
6381*4882a593Smuzhiyun 	struct sctp_chunk *err;
6382*4882a593Smuzhiyun 	enum sctp_verb deliver;
6383*4882a593Smuzhiyun 	size_t datalen;
6384*4882a593Smuzhiyun 	__u32 tsn;
6385*4882a593Smuzhiyun 	int tmp;
6386*4882a593Smuzhiyun 
6387*4882a593Smuzhiyun 	data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6388*4882a593Smuzhiyun 	chunk->subh.data_hdr = data_hdr;
6389*4882a593Smuzhiyun 	skb_pull(chunk->skb, sctp_datahdr_len(&asoc->stream));
6390*4882a593Smuzhiyun 
6391*4882a593Smuzhiyun 	tsn = ntohl(data_hdr->tsn);
6392*4882a593Smuzhiyun 	pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6393*4882a593Smuzhiyun 
6394*4882a593Smuzhiyun 	/* ASSERT:  Now skb->data is really the user data.  */
6395*4882a593Smuzhiyun 
6396*4882a593Smuzhiyun 	/* Process ECN based congestion.
6397*4882a593Smuzhiyun 	 *
6398*4882a593Smuzhiyun 	 * Since the chunk structure is reused for all chunks within
6399*4882a593Smuzhiyun 	 * a packet, we use ecn_ce_done to track if we've already
6400*4882a593Smuzhiyun 	 * done CE processing for this packet.
6401*4882a593Smuzhiyun 	 *
6402*4882a593Smuzhiyun 	 * We need to do ECN processing even if we plan to discard the
6403*4882a593Smuzhiyun 	 * chunk later.
6404*4882a593Smuzhiyun 	 */
6405*4882a593Smuzhiyun 
6406*4882a593Smuzhiyun 	if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
6407*4882a593Smuzhiyun 		struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
6408*4882a593Smuzhiyun 		chunk->ecn_ce_done = 1;
6409*4882a593Smuzhiyun 
6410*4882a593Smuzhiyun 		if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
6411*4882a593Smuzhiyun 			/* Do real work as sideffect. */
6412*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6413*4882a593Smuzhiyun 					SCTP_U32(tsn));
6414*4882a593Smuzhiyun 		}
6415*4882a593Smuzhiyun 	}
6416*4882a593Smuzhiyun 
6417*4882a593Smuzhiyun 	tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6418*4882a593Smuzhiyun 	if (tmp < 0) {
6419*4882a593Smuzhiyun 		/* The TSN is too high--silently discard the chunk and
6420*4882a593Smuzhiyun 		 * count on it getting retransmitted later.
6421*4882a593Smuzhiyun 		 */
6422*4882a593Smuzhiyun 		if (chunk->asoc)
6423*4882a593Smuzhiyun 			chunk->asoc->stats.outofseqtsns++;
6424*4882a593Smuzhiyun 		return SCTP_IERROR_HIGH_TSN;
6425*4882a593Smuzhiyun 	} else if (tmp > 0) {
6426*4882a593Smuzhiyun 		/* This is a duplicate.  Record it.  */
6427*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6428*4882a593Smuzhiyun 		return SCTP_IERROR_DUP_TSN;
6429*4882a593Smuzhiyun 	}
6430*4882a593Smuzhiyun 
6431*4882a593Smuzhiyun 	/* This is a new TSN.  */
6432*4882a593Smuzhiyun 
6433*4882a593Smuzhiyun 	/* Discard if there is no room in the receive window.
6434*4882a593Smuzhiyun 	 * Actually, allow a little bit of overflow (up to a MTU).
6435*4882a593Smuzhiyun 	 */
6436*4882a593Smuzhiyun 	datalen = ntohs(chunk->chunk_hdr->length);
6437*4882a593Smuzhiyun 	datalen -= sctp_datachk_len(&asoc->stream);
6438*4882a593Smuzhiyun 
6439*4882a593Smuzhiyun 	deliver = SCTP_CMD_CHUNK_ULP;
6440*4882a593Smuzhiyun 
6441*4882a593Smuzhiyun 	/* Think about partial delivery. */
6442*4882a593Smuzhiyun 	if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6443*4882a593Smuzhiyun 
6444*4882a593Smuzhiyun 		/* Even if we don't accept this chunk there is
6445*4882a593Smuzhiyun 		 * memory pressure.
6446*4882a593Smuzhiyun 		 */
6447*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6448*4882a593Smuzhiyun 	}
6449*4882a593Smuzhiyun 
6450*4882a593Smuzhiyun 	/* Spill over rwnd a little bit.  Note: While allowed, this spill over
6451*4882a593Smuzhiyun 	 * seems a bit troublesome in that frag_point varies based on
6452*4882a593Smuzhiyun 	 * PMTU.  In cases, such as loopback, this might be a rather
6453*4882a593Smuzhiyun 	 * large spill over.
6454*4882a593Smuzhiyun 	 */
6455*4882a593Smuzhiyun 	if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6456*4882a593Smuzhiyun 	    (datalen > asoc->rwnd + asoc->frag_point))) {
6457*4882a593Smuzhiyun 
6458*4882a593Smuzhiyun 		/* If this is the next TSN, consider reneging to make
6459*4882a593Smuzhiyun 		 * room.   Note: Playing nice with a confused sender.  A
6460*4882a593Smuzhiyun 		 * malicious sender can still eat up all our buffer
6461*4882a593Smuzhiyun 		 * space and in the future we may want to detect and
6462*4882a593Smuzhiyun 		 * do more drastic reneging.
6463*4882a593Smuzhiyun 		 */
6464*4882a593Smuzhiyun 		if (sctp_tsnmap_has_gap(map) &&
6465*4882a593Smuzhiyun 		    (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6466*4882a593Smuzhiyun 			pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6467*4882a593Smuzhiyun 			deliver = SCTP_CMD_RENEGE;
6468*4882a593Smuzhiyun 		} else {
6469*4882a593Smuzhiyun 			pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6470*4882a593Smuzhiyun 				 __func__, tsn, datalen, asoc->rwnd);
6471*4882a593Smuzhiyun 
6472*4882a593Smuzhiyun 			return SCTP_IERROR_IGNORE_TSN;
6473*4882a593Smuzhiyun 		}
6474*4882a593Smuzhiyun 	}
6475*4882a593Smuzhiyun 
6476*4882a593Smuzhiyun 	/*
6477*4882a593Smuzhiyun 	 * Also try to renege to limit our memory usage in the event that
6478*4882a593Smuzhiyun 	 * we are under memory pressure
6479*4882a593Smuzhiyun 	 * If we can't renege, don't worry about it, the sk_rmem_schedule
6480*4882a593Smuzhiyun 	 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6481*4882a593Smuzhiyun 	 * memory usage too much
6482*4882a593Smuzhiyun 	 */
6483*4882a593Smuzhiyun 	if (sk_under_memory_pressure(sk)) {
6484*4882a593Smuzhiyun 		if (sctp_tsnmap_has_gap(map) &&
6485*4882a593Smuzhiyun 		    (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6486*4882a593Smuzhiyun 			pr_debug("%s: under pressure, reneging for tsn:%u\n",
6487*4882a593Smuzhiyun 				 __func__, tsn);
6488*4882a593Smuzhiyun 			deliver = SCTP_CMD_RENEGE;
6489*4882a593Smuzhiyun 		} else {
6490*4882a593Smuzhiyun 			sk_mem_reclaim(sk);
6491*4882a593Smuzhiyun 		}
6492*4882a593Smuzhiyun 	}
6493*4882a593Smuzhiyun 
6494*4882a593Smuzhiyun 	/*
6495*4882a593Smuzhiyun 	 * Section 3.3.10.9 No User Data (9)
6496*4882a593Smuzhiyun 	 *
6497*4882a593Smuzhiyun 	 * Cause of error
6498*4882a593Smuzhiyun 	 * ---------------
6499*4882a593Smuzhiyun 	 * No User Data:  This error cause is returned to the originator of a
6500*4882a593Smuzhiyun 	 * DATA chunk if a received DATA chunk has no user data.
6501*4882a593Smuzhiyun 	 */
6502*4882a593Smuzhiyun 	if (unlikely(0 == datalen)) {
6503*4882a593Smuzhiyun 		err = sctp_make_abort_no_data(asoc, chunk, tsn);
6504*4882a593Smuzhiyun 		if (err) {
6505*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6506*4882a593Smuzhiyun 					SCTP_CHUNK(err));
6507*4882a593Smuzhiyun 		}
6508*4882a593Smuzhiyun 		/* We are going to ABORT, so we might as well stop
6509*4882a593Smuzhiyun 		 * processing the rest of the chunks in the packet.
6510*4882a593Smuzhiyun 		 */
6511*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6512*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6513*4882a593Smuzhiyun 				SCTP_ERROR(ECONNABORTED));
6514*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6515*4882a593Smuzhiyun 				SCTP_PERR(SCTP_ERROR_NO_DATA));
6516*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6517*4882a593Smuzhiyun 		SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6518*4882a593Smuzhiyun 		return SCTP_IERROR_NO_DATA;
6519*4882a593Smuzhiyun 	}
6520*4882a593Smuzhiyun 
6521*4882a593Smuzhiyun 	chunk->data_accepted = 1;
6522*4882a593Smuzhiyun 
6523*4882a593Smuzhiyun 	/* Note: Some chunks may get overcounted (if we drop) or overcounted
6524*4882a593Smuzhiyun 	 * if we renege and the chunk arrives again.
6525*4882a593Smuzhiyun 	 */
6526*4882a593Smuzhiyun 	if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6527*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6528*4882a593Smuzhiyun 		if (chunk->asoc)
6529*4882a593Smuzhiyun 			chunk->asoc->stats.iuodchunks++;
6530*4882a593Smuzhiyun 	} else {
6531*4882a593Smuzhiyun 		SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6532*4882a593Smuzhiyun 		if (chunk->asoc)
6533*4882a593Smuzhiyun 			chunk->asoc->stats.iodchunks++;
6534*4882a593Smuzhiyun 	}
6535*4882a593Smuzhiyun 
6536*4882a593Smuzhiyun 	/* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6537*4882a593Smuzhiyun 	 *
6538*4882a593Smuzhiyun 	 * If an endpoint receive a DATA chunk with an invalid stream
6539*4882a593Smuzhiyun 	 * identifier, it shall acknowledge the reception of the DATA chunk
6540*4882a593Smuzhiyun 	 * following the normal procedure, immediately send an ERROR chunk
6541*4882a593Smuzhiyun 	 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6542*4882a593Smuzhiyun 	 * and discard the DATA chunk.
6543*4882a593Smuzhiyun 	 */
6544*4882a593Smuzhiyun 	if (ntohs(data_hdr->stream) >= asoc->stream.incnt) {
6545*4882a593Smuzhiyun 		/* Mark tsn as received even though we drop it */
6546*4882a593Smuzhiyun 		sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6547*4882a593Smuzhiyun 
6548*4882a593Smuzhiyun 		err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6549*4882a593Smuzhiyun 					 &data_hdr->stream,
6550*4882a593Smuzhiyun 					 sizeof(data_hdr->stream),
6551*4882a593Smuzhiyun 					 sizeof(u16));
6552*4882a593Smuzhiyun 		if (err)
6553*4882a593Smuzhiyun 			sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6554*4882a593Smuzhiyun 					SCTP_CHUNK(err));
6555*4882a593Smuzhiyun 		return SCTP_IERROR_BAD_STREAM;
6556*4882a593Smuzhiyun 	}
6557*4882a593Smuzhiyun 
6558*4882a593Smuzhiyun 	/* Check to see if the SSN is possible for this TSN.
6559*4882a593Smuzhiyun 	 * The biggest gap we can record is 4K wide.  Since SSNs wrap
6560*4882a593Smuzhiyun 	 * at an unsigned short, there is no way that an SSN can
6561*4882a593Smuzhiyun 	 * wrap and for a valid TSN.  We can simply check if the current
6562*4882a593Smuzhiyun 	 * SSN is smaller then the next expected one.  If it is, it wrapped
6563*4882a593Smuzhiyun 	 * and is invalid.
6564*4882a593Smuzhiyun 	 */
6565*4882a593Smuzhiyun 	if (!asoc->stream.si->validate_data(chunk))
6566*4882a593Smuzhiyun 		return SCTP_IERROR_PROTO_VIOLATION;
6567*4882a593Smuzhiyun 
6568*4882a593Smuzhiyun 	/* Send the data up to the user.  Note:  Schedule  the
6569*4882a593Smuzhiyun 	 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6570*4882a593Smuzhiyun 	 * chunk needs the updated rwnd.
6571*4882a593Smuzhiyun 	 */
6572*4882a593Smuzhiyun 	sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6573*4882a593Smuzhiyun 
6574*4882a593Smuzhiyun 	return SCTP_IERROR_NO_ERROR;
6575*4882a593Smuzhiyun }
6576