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