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