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