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