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