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