GNU Linux-libre 4.9.317-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                 return SCTP_DISPOSITION_ABORT;
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 }
3073
3074 /*
3075  * sctp_sf_eat_data_fast_4_4
3076  *
3077  * Section: 4 (4)
3078  * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3079  *    DATA chunks without delay.
3080  *
3081  * Verification Tag:  8.5 Verification Tag [Normal verification]
3082  * Inputs
3083  * (endpoint, asoc, chunk)
3084  *
3085  * Outputs
3086  * (asoc, reply_msg, msg_up, timers, counters)
3087  *
3088  * The return value is the disposition of the chunk.
3089  */
3090 sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3091                                      const struct sctp_endpoint *ep,
3092                                      const struct sctp_association *asoc,
3093                                      const sctp_subtype_t type,
3094                                      void *arg,
3095                                      sctp_cmd_seq_t *commands)
3096 {
3097         struct sctp_chunk *chunk = arg;
3098         int error;
3099
3100         if (!sctp_vtag_verify(chunk, asoc)) {
3101                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3102                                 SCTP_NULL());
3103                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3104         }
3105
3106         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3107                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3108                                                   commands);
3109
3110         error = sctp_eat_data(asoc, chunk, commands);
3111         switch (error) {
3112         case SCTP_IERROR_NO_ERROR:
3113         case SCTP_IERROR_HIGH_TSN:
3114         case SCTP_IERROR_DUP_TSN:
3115         case SCTP_IERROR_IGNORE_TSN:
3116         case SCTP_IERROR_BAD_STREAM:
3117                 break;
3118         case SCTP_IERROR_NO_DATA:
3119                 return SCTP_DISPOSITION_ABORT;
3120         case SCTP_IERROR_PROTO_VIOLATION:
3121                 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3122                         (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
3123         default:
3124                 BUG();
3125         }
3126
3127         /* Go a head and force a SACK, since we are shutting down. */
3128
3129         /* Implementor's Guide.
3130          *
3131          * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3132          * respond to each received packet containing one or more DATA chunk(s)
3133          * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3134          */
3135         if (chunk->end_of_packet) {
3136                 /* We must delay the chunk creation since the cumulative
3137                  * TSN has not been updated yet.
3138                  */
3139                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3140                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3141                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3142                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3143         }
3144
3145         return SCTP_DISPOSITION_CONSUME;
3146 }
3147
3148 /*
3149  * Section: 6.2  Processing a Received SACK
3150  * D) Any time a SACK arrives, the endpoint performs the following:
3151  *
3152  *     i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3153  *     then drop the SACK.   Since Cumulative TSN Ack is monotonically
3154  *     increasing, a SACK whose Cumulative TSN Ack is less than the
3155  *     Cumulative TSN Ack Point indicates an out-of-order SACK.
3156  *
3157  *     ii) Set rwnd equal to the newly received a_rwnd minus the number
3158  *     of bytes still outstanding after processing the Cumulative TSN Ack
3159  *     and the Gap Ack Blocks.
3160  *
3161  *     iii) If the SACK is missing a TSN that was previously
3162  *     acknowledged via a Gap Ack Block (e.g., the data receiver
3163  *     reneged on the data), then mark the corresponding DATA chunk
3164  *     as available for retransmit:  Mark it as missing for fast
3165  *     retransmit as described in Section 7.2.4 and if no retransmit
3166  *     timer is running for the destination address to which the DATA
3167  *     chunk was originally transmitted, then T3-rtx is started for
3168  *     that destination address.
3169  *
3170  * Verification Tag:  8.5 Verification Tag [Normal verification]
3171  *
3172  * Inputs
3173  * (endpoint, asoc, chunk)
3174  *
3175  * Outputs
3176  * (asoc, reply_msg, msg_up, timers, counters)
3177  *
3178  * The return value is the disposition of the chunk.
3179  */
3180 sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3181                                         const struct sctp_endpoint *ep,
3182                                         const struct sctp_association *asoc,
3183                                         const sctp_subtype_t type,
3184                                         void *arg,
3185                                         sctp_cmd_seq_t *commands)
3186 {
3187         struct sctp_chunk *chunk = arg;
3188         sctp_sackhdr_t *sackh;
3189         __u32 ctsn;
3190
3191         if (!sctp_vtag_verify(chunk, asoc))
3192                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3193
3194         /* Make sure that the SACK chunk has a valid length. */
3195         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3196                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3197                                                   commands);
3198
3199         /* Pull the SACK chunk from the data buffer */
3200         sackh = sctp_sm_pull_sack(chunk);
3201         /* Was this a bogus SACK? */
3202         if (!sackh)
3203                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3204         chunk->subh.sack_hdr = sackh;
3205         ctsn = ntohl(sackh->cum_tsn_ack);
3206
3207         /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3208          *     Ack Point, then drop the SACK.  Since Cumulative TSN
3209          *     Ack is monotonically increasing, a SACK whose
3210          *     Cumulative TSN Ack is less than the Cumulative TSN Ack
3211          *     Point indicates an out-of-order SACK.
3212          */
3213         if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3214                 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3215                          asoc->ctsn_ack_point);
3216
3217                 return SCTP_DISPOSITION_DISCARD;
3218         }
3219
3220         /* If Cumulative TSN Ack beyond the max tsn currently
3221          * send, terminating the association and respond to the
3222          * sender with an ABORT.
3223          */
3224         if (!TSN_lt(ctsn, asoc->next_tsn))
3225                 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3226
3227         /* Return this SACK for further processing.  */
3228         sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3229
3230         /* Note: We do the rest of the work on the PROCESS_SACK
3231          * sideeffect.
3232          */
3233         return SCTP_DISPOSITION_CONSUME;
3234 }
3235
3236 /*
3237  * Generate an ABORT in response to a packet.
3238  *
3239  * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3240  *
3241  * 8) The receiver should respond to the sender of the OOTB packet with
3242  *    an ABORT.  When sending the ABORT, the receiver of the OOTB packet
3243  *    MUST fill in the Verification Tag field of the outbound packet
3244  *    with the value found in the Verification Tag field of the OOTB
3245  *    packet and set the T-bit in the Chunk Flags to indicate that the
3246  *    Verification Tag is reflected.  After sending this ABORT, the
3247  *    receiver of the OOTB packet shall discard the OOTB packet and take
3248  *    no further action.
3249  *
3250  * Verification Tag:
3251  *
3252  * The return value is the disposition of the chunk.
3253 */
3254 static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3255                                         const struct sctp_endpoint *ep,
3256                                         const struct sctp_association *asoc,
3257                                         const sctp_subtype_t type,
3258                                         void *arg,
3259                                         sctp_cmd_seq_t *commands)
3260 {
3261         struct sctp_packet *packet = NULL;
3262         struct sctp_chunk *chunk = arg;
3263         struct sctp_chunk *abort;
3264
3265         packet = sctp_ootb_pkt_new(net, asoc, chunk);
3266
3267         if (packet) {
3268                 /* Make an ABORT. The T bit will be set if the asoc
3269                  * is NULL.
3270                  */
3271                 abort = sctp_make_abort(asoc, chunk, 0);
3272                 if (!abort) {
3273                         sctp_ootb_pkt_free(packet);
3274                         return SCTP_DISPOSITION_NOMEM;
3275                 }
3276
3277                 /* Reflect vtag if T-Bit is set */
3278                 if (sctp_test_T_bit(abort))
3279                         packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3280
3281                 /* Set the skb to the belonging sock for accounting.  */
3282                 abort->skb->sk = ep->base.sk;
3283
3284                 sctp_packet_append_chunk(packet, abort);
3285
3286                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3287                                 SCTP_PACKET(packet));
3288
3289                 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3290
3291                 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3292                 return SCTP_DISPOSITION_CONSUME;
3293         }
3294
3295         return SCTP_DISPOSITION_NOMEM;
3296 }
3297
3298 /*
3299  * Received an ERROR chunk from peer.  Generate SCTP_REMOTE_ERROR
3300  * event as ULP notification for each cause included in the chunk.
3301  *
3302  * API 5.3.1.3 - SCTP_REMOTE_ERROR
3303  *
3304  * The return value is the disposition of the chunk.
3305 */
3306 sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3307                                         const struct sctp_endpoint *ep,
3308                                         const struct sctp_association *asoc,
3309                                         const sctp_subtype_t type,
3310                                         void *arg,
3311                                         sctp_cmd_seq_t *commands)
3312 {
3313         struct sctp_chunk *chunk = arg;
3314         sctp_errhdr_t *err;
3315
3316         if (!sctp_vtag_verify(chunk, asoc))
3317                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3318
3319         /* Make sure that the ERROR chunk has a valid length. */
3320         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3321                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3322                                                   commands);
3323         sctp_walk_errors(err, chunk->chunk_hdr);
3324         if ((void *)err != (void *)chunk->chunk_end)
3325                 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3326                                                   (void *)err, commands);
3327
3328         sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3329                         SCTP_CHUNK(chunk));
3330
3331         return SCTP_DISPOSITION_CONSUME;
3332 }
3333
3334 /*
3335  * Process an inbound SHUTDOWN ACK.
3336  *
3337  * From Section 9.2:
3338  * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3339  * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3340  * peer, and remove all record of the association.
3341  *
3342  * The return value is the disposition.
3343  */
3344 sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3345                                         const struct sctp_endpoint *ep,
3346                                         const struct sctp_association *asoc,
3347                                         const sctp_subtype_t type,
3348                                         void *arg,
3349                                         sctp_cmd_seq_t *commands)
3350 {
3351         struct sctp_chunk *chunk = arg;
3352         struct sctp_chunk *reply;
3353         struct sctp_ulpevent *ev;
3354
3355         if (!sctp_vtag_verify(chunk, asoc))
3356                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3357
3358         /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3359         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3360                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3361                                                   commands);
3362         /* 10.2 H) SHUTDOWN COMPLETE notification
3363          *
3364          * When SCTP completes the shutdown procedures (section 9.2) this
3365          * notification is passed to the upper layer.
3366          */
3367         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3368                                              0, 0, 0, NULL, GFP_ATOMIC);
3369         if (!ev)
3370                 goto nomem;
3371
3372         /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3373         reply = sctp_make_shutdown_complete(asoc, chunk);
3374         if (!reply)
3375                 goto nomem_chunk;
3376
3377         /* Do all the commands now (after allocation), so that we
3378          * have consistent state if memory allocation failes
3379          */
3380         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3381
3382         /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3383          * stop the T2-shutdown timer,
3384          */
3385         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3386                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3387
3388         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3389                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3390
3391         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3392                         SCTP_STATE(SCTP_STATE_CLOSED));
3393         SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3394         SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3395         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3396
3397         /* ...and remove all record of the association. */
3398         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3399         return SCTP_DISPOSITION_DELETE_TCB;
3400
3401 nomem_chunk:
3402         sctp_ulpevent_free(ev);
3403 nomem:
3404         return SCTP_DISPOSITION_NOMEM;
3405 }
3406
3407 /*
3408  * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3409  *
3410  * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3411  *    respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3412  *    When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3413  *    packet must fill in the Verification Tag field of the outbound
3414  *    packet with the Verification Tag received in the SHUTDOWN ACK and
3415  *    set the T-bit in the Chunk Flags to indicate that the Verification
3416  *    Tag is reflected.
3417  *
3418  * 8) The receiver should respond to the sender of the OOTB packet with
3419  *    an ABORT.  When sending the ABORT, the receiver of the OOTB packet
3420  *    MUST fill in the Verification Tag field of the outbound packet
3421  *    with the value found in the Verification Tag field of the OOTB
3422  *    packet and set the T-bit in the Chunk Flags to indicate that the
3423  *    Verification Tag is reflected.  After sending this ABORT, the
3424  *    receiver of the OOTB packet shall discard the OOTB packet and take
3425  *    no further action.
3426  */
3427 sctp_disposition_t sctp_sf_ootb(struct net *net,
3428                                 const struct sctp_endpoint *ep,
3429                                 const struct sctp_association *asoc,
3430                                 const sctp_subtype_t type,
3431                                 void *arg,
3432                                 sctp_cmd_seq_t *commands)
3433 {
3434         struct sctp_chunk *chunk = arg;
3435         struct sk_buff *skb = chunk->skb;
3436         sctp_chunkhdr_t *ch;
3437         sctp_errhdr_t *err;
3438         __u8 *ch_end;
3439         int ootb_shut_ack = 0;
3440         int ootb_cookie_ack = 0;
3441
3442         SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3443
3444         ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3445         do {
3446                 /* Report violation if the chunk is less then minimal */
3447                 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3448                         return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3449                                                   commands);
3450
3451                 /* Report violation if chunk len overflows */
3452                 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3453                 if (ch_end > skb_tail_pointer(skb))
3454                         return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3455                                                   commands);
3456
3457                 /* Now that we know we at least have a chunk header,
3458                  * do things that are type appropriate.
3459                  */
3460                 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3461                         ootb_shut_ack = 1;
3462
3463                 /* RFC 2960, Section 3.3.7
3464                  *   Moreover, under any circumstances, an endpoint that
3465                  *   receives an ABORT  MUST NOT respond to that ABORT by
3466                  *   sending an ABORT of its own.
3467                  */
3468                 if (SCTP_CID_ABORT == ch->type)
3469                         return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3470
3471                 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3472                  * or a COOKIE ACK the SCTP Packet should be silently
3473                  * discarded.
3474                  */
3475
3476                 if (SCTP_CID_COOKIE_ACK == ch->type)
3477                         ootb_cookie_ack = 1;
3478
3479                 if (SCTP_CID_ERROR == ch->type) {
3480                         sctp_walk_errors(err, ch) {
3481                                 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3482                                         ootb_cookie_ack = 1;
3483                                         break;
3484                                 }
3485                         }
3486                 }
3487
3488                 ch = (sctp_chunkhdr_t *) ch_end;
3489         } while (ch_end < skb_tail_pointer(skb));
3490
3491         if (ootb_shut_ack)
3492                 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3493         else if (ootb_cookie_ack)
3494                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3495         else
3496                 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3497 }
3498
3499 /*
3500  * Handle an "Out of the blue" SHUTDOWN ACK.
3501  *
3502  * Section: 8.4 5, sctpimpguide 2.41.
3503  *
3504  * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3505  *    respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3506  *    When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3507  *    packet must fill in the Verification Tag field of the outbound
3508  *    packet with the Verification Tag received in the SHUTDOWN ACK and
3509  *    set the T-bit in the Chunk Flags to indicate that the Verification
3510  *    Tag is reflected.
3511  *
3512  * Inputs
3513  * (endpoint, asoc, type, arg, commands)
3514  *
3515  * Outputs
3516  * (sctp_disposition_t)
3517  *
3518  * The return value is the disposition of the chunk.
3519  */
3520 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3521                                              const struct sctp_endpoint *ep,
3522                                              const struct sctp_association *asoc,
3523                                              const sctp_subtype_t type,
3524                                              void *arg,
3525                                              sctp_cmd_seq_t *commands)
3526 {
3527         struct sctp_packet *packet = NULL;
3528         struct sctp_chunk *chunk = arg;
3529         struct sctp_chunk *shut;
3530
3531         packet = sctp_ootb_pkt_new(net, asoc, chunk);
3532
3533         if (packet) {
3534                 /* Make an SHUTDOWN_COMPLETE.
3535                  * The T bit will be set if the asoc is NULL.
3536                  */
3537                 shut = sctp_make_shutdown_complete(asoc, chunk);
3538                 if (!shut) {
3539                         sctp_ootb_pkt_free(packet);
3540                         return SCTP_DISPOSITION_NOMEM;
3541                 }
3542
3543                 /* Reflect vtag if T-Bit is set */
3544                 if (sctp_test_T_bit(shut))
3545                         packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3546
3547                 /* Set the skb to the belonging sock for accounting.  */
3548                 shut->skb->sk = ep->base.sk;
3549
3550                 sctp_packet_append_chunk(packet, shut);
3551
3552                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3553                                 SCTP_PACKET(packet));
3554
3555                 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3556
3557                 /* If the chunk length is invalid, we don't want to process
3558                  * the reset of the packet.
3559                  */
3560                 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3561                         return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3562
3563                 /* We need to discard the rest of the packet to prevent
3564                  * potential bomming attacks from additional bundled chunks.
3565                  * This is documented in SCTP Threats ID.
3566                  */
3567                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3568         }
3569
3570         return SCTP_DISPOSITION_NOMEM;
3571 }
3572
3573 /*
3574  * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3575  *
3576  * Verification Tag:  8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3577  *   If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3578  *   procedures in section 8.4 SHOULD be followed, in other words it
3579  *   should be treated as an Out Of The Blue packet.
3580  *   [This means that we do NOT check the Verification Tag on these
3581  *   chunks. --piggy ]
3582  *
3583  */
3584 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3585                                       const struct sctp_endpoint *ep,
3586                                       const struct sctp_association *asoc,
3587                                       const sctp_subtype_t type,
3588                                       void *arg,
3589                                       sctp_cmd_seq_t *commands)
3590 {
3591         struct sctp_chunk *chunk = arg;
3592
3593         /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3594         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3595                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3596                                                   commands);
3597
3598         /* Although we do have an association in this case, it corresponds
3599          * to a restarted association. So the packet is treated as an OOTB
3600          * packet and the state function that handles OOTB SHUTDOWN_ACK is
3601          * called with a NULL association.
3602          */
3603         SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3604
3605         return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3606 }
3607
3608 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk.  */
3609 sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3610                                      const struct sctp_endpoint *ep,
3611                                      const struct sctp_association *asoc,
3612                                      const sctp_subtype_t type, void *arg,
3613                                      sctp_cmd_seq_t *commands)
3614 {
3615         struct sctp_chunk       *chunk = arg;
3616         struct sctp_chunk       *asconf_ack = NULL;
3617         struct sctp_paramhdr    *err_param = NULL;
3618         sctp_addiphdr_t         *hdr;
3619         __u32                   serial;
3620
3621         if (!sctp_vtag_verify(chunk, asoc)) {
3622                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3623                                 SCTP_NULL());
3624                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3625         }
3626
3627         /* ADD-IP: Section 4.1.1
3628          * This chunk MUST be sent in an authenticated way by using
3629          * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3630          * is received unauthenticated it MUST be silently discarded as
3631          * described in [I-D.ietf-tsvwg-sctp-auth].
3632          */
3633         if (!net->sctp.addip_noauth && !chunk->auth)
3634                 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3635
3636         /* Make sure that the ASCONF ADDIP chunk has a valid length.  */
3637         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3638                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3639                                                   commands);
3640
3641         hdr = (sctp_addiphdr_t *)chunk->skb->data;
3642         serial = ntohl(hdr->serial);
3643
3644         /* Verify the ASCONF chunk before processing it. */
3645         if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3646                 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3647                                                   (void *)err_param, commands);
3648
3649         /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3650          * the endpoint stored in a new association variable
3651          * 'Peer-Serial-Number'.
3652          */
3653         if (serial == asoc->peer.addip_serial + 1) {
3654                 /* If this is the first instance of ASCONF in the packet,
3655                  * we can clean our old ASCONF-ACKs.
3656                  */
3657                 if (!chunk->has_asconf)
3658                         sctp_assoc_clean_asconf_ack_cache(asoc);
3659
3660                 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3661                  * expected, process the ASCONF as described below and after
3662                  * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3663                  * the response packet and cache a copy of it (in the event it
3664                  * later needs to be retransmitted).
3665                  *
3666                  * Essentially, do V1-V5.
3667                  */
3668                 asconf_ack = sctp_process_asconf((struct sctp_association *)
3669                                                  asoc, chunk);
3670                 if (!asconf_ack)
3671                         return SCTP_DISPOSITION_NOMEM;
3672         } else if (serial < asoc->peer.addip_serial + 1) {
3673                 /* ADDIP 5.2 E2)
3674                  * If the value found in the Sequence Number is less than the
3675                  * ('Peer- Sequence-Number' + 1), simply skip to the next
3676                  * ASCONF, and include in the outbound response packet
3677                  * any previously cached ASCONF-ACK response that was
3678                  * sent and saved that matches the Sequence Number of the
3679                  * ASCONF.  Note: It is possible that no cached ASCONF-ACK
3680                  * Chunk exists.  This will occur when an older ASCONF
3681                  * arrives out of order.  In such a case, the receiver
3682                  * should skip the ASCONF Chunk and not include ASCONF-ACK
3683                  * Chunk for that chunk.
3684                  */
3685                 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3686                 if (!asconf_ack)
3687                         return SCTP_DISPOSITION_DISCARD;
3688
3689                 /* Reset the transport so that we select the correct one
3690                  * this time around.  This is to make sure that we don't
3691                  * accidentally use a stale transport that's been removed.
3692                  */
3693                 asconf_ack->transport = NULL;
3694         } else {
3695                 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3696                  * it must be either a stale packet or from an attacker.
3697                  */
3698                 return SCTP_DISPOSITION_DISCARD;
3699         }
3700
3701         /* ADDIP 5.2 E6)  The destination address of the SCTP packet
3702          * containing the ASCONF-ACK Chunks MUST be the source address of
3703          * the SCTP packet that held the ASCONF Chunks.
3704          *
3705          * To do this properly, we'll set the destination address of the chunk
3706          * and at the transmit time, will try look up the transport to use.
3707          * Since ASCONFs may be bundled, the correct transport may not be
3708          * created until we process the entire packet, thus this workaround.
3709          */
3710         asconf_ack->dest = chunk->source;
3711         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3712         if (asoc->new_transport) {
3713                 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3714                 ((struct sctp_association *)asoc)->new_transport = NULL;
3715         }
3716
3717         return SCTP_DISPOSITION_CONSUME;
3718 }
3719
3720 /*
3721  * ADDIP Section 4.3 General rules for address manipulation
3722  * When building TLV parameters for the ASCONF Chunk that will add or
3723  * delete IP addresses the D0 to D13 rules should be applied:
3724  */
3725 sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3726                                          const struct sctp_endpoint *ep,
3727                                          const struct sctp_association *asoc,
3728                                          const sctp_subtype_t type, void *arg,
3729                                          sctp_cmd_seq_t *commands)
3730 {
3731         struct sctp_chunk       *asconf_ack = arg;
3732         struct sctp_chunk       *last_asconf = asoc->addip_last_asconf;
3733         struct sctp_chunk       *abort;
3734         struct sctp_paramhdr    *err_param = NULL;
3735         sctp_addiphdr_t         *addip_hdr;
3736         __u32                   sent_serial, rcvd_serial;
3737
3738         if (!sctp_vtag_verify(asconf_ack, asoc)) {
3739                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3740                                 SCTP_NULL());
3741                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3742         }
3743
3744         /* ADD-IP, Section 4.1.2:
3745          * This chunk MUST be sent in an authenticated way by using
3746          * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3747          * is received unauthenticated it MUST be silently discarded as
3748          * described in [I-D.ietf-tsvwg-sctp-auth].
3749          */
3750         if (!net->sctp.addip_noauth && !asconf_ack->auth)
3751                 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3752
3753         /* Make sure that the ADDIP chunk has a valid length.  */
3754         if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3755                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3756                                                   commands);
3757
3758         addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3759         rcvd_serial = ntohl(addip_hdr->serial);
3760
3761         /* Verify the ASCONF-ACK chunk before processing it. */
3762         if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
3763                 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3764                            (void *)err_param, commands);
3765
3766         if (last_asconf) {
3767                 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3768                 sent_serial = ntohl(addip_hdr->serial);
3769         } else {
3770                 sent_serial = asoc->addip_serial - 1;
3771         }
3772
3773         /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3774          * equal to the next serial number to be used but no ASCONF chunk is
3775          * outstanding the endpoint MUST ABORT the association. Note that a
3776          * sequence number is greater than if it is no more than 2^^31-1
3777          * larger than the current sequence number (using serial arithmetic).
3778          */
3779         if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3780             !(asoc->addip_last_asconf)) {
3781                 abort = sctp_make_abort(asoc, asconf_ack,
3782                                         sizeof(sctp_errhdr_t));
3783                 if (abort) {
3784                         sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3785                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3786                                         SCTP_CHUNK(abort));
3787                 }
3788                 /* We are going to ABORT, so we might as well stop
3789                  * processing the rest of the chunks in the packet.
3790                  */
3791                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3792                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3793                 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3794                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3795                                 SCTP_ERROR(ECONNABORTED));
3796                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3797                                 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3798                 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3799                 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3800                 return SCTP_DISPOSITION_ABORT;
3801         }
3802
3803         if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3804                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3805                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3806
3807                 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3808                                              asconf_ack)) {
3809                         /* Successfully processed ASCONF_ACK.  We can
3810                          * release the next asconf if we have one.
3811                          */
3812                         sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3813                                         SCTP_NULL());
3814                         return SCTP_DISPOSITION_CONSUME;
3815                 }
3816
3817                 abort = sctp_make_abort(asoc, asconf_ack,
3818                                         sizeof(sctp_errhdr_t));
3819                 if (abort) {
3820                         sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3821                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3822                                         SCTP_CHUNK(abort));
3823                 }
3824                 /* We are going to ABORT, so we might as well stop
3825                  * processing the rest of the chunks in the packet.
3826                  */
3827                 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3828                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3829                                 SCTP_ERROR(ECONNABORTED));
3830                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3831                                 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3832                 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3833                 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3834                 return SCTP_DISPOSITION_ABORT;
3835         }
3836
3837         return SCTP_DISPOSITION_DISCARD;
3838 }
3839
3840 /*
3841  * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3842  *
3843  * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3844  * its cumulative TSN point to the value carried in the FORWARD TSN
3845  * chunk, and then MUST further advance its cumulative TSN point locally
3846  * if possible.
3847  * After the above processing, the data receiver MUST stop reporting any
3848  * missing TSNs earlier than or equal to the new cumulative TSN point.
3849  *
3850  * Verification Tag:  8.5 Verification Tag [Normal verification]
3851  *
3852  * The return value is the disposition of the chunk.
3853  */
3854 sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3855                                        const struct sctp_endpoint *ep,
3856                                        const struct sctp_association *asoc,
3857                                        const sctp_subtype_t type,
3858                                        void *arg,
3859                                        sctp_cmd_seq_t *commands)
3860 {
3861         struct sctp_chunk *chunk = arg;
3862         struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3863         struct sctp_fwdtsn_skip *skip;
3864         __u16 len;
3865         __u32 tsn;
3866
3867         if (!sctp_vtag_verify(chunk, asoc)) {
3868                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3869                                 SCTP_NULL());
3870                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3871         }
3872
3873         /* Make sure that the FORWARD_TSN chunk has valid length.  */
3874         if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3875                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3876                                                   commands);
3877
3878         fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3879         chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3880         len = ntohs(chunk->chunk_hdr->length);
3881         len -= sizeof(struct sctp_chunkhdr);
3882         skb_pull(chunk->skb, len);
3883
3884         tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3885         pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3886
3887         /* The TSN is too high--silently discard the chunk and count on it
3888          * getting retransmitted later.
3889          */
3890         if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3891                 goto discard_noforce;
3892
3893         /* Silently discard the chunk if stream-id is not valid */
3894         sctp_walk_fwdtsn(skip, chunk) {
3895                 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3896                         goto discard_noforce;
3897         }
3898
3899         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3900         if (len > sizeof(struct sctp_fwdtsn_hdr))
3901                 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3902                                 SCTP_CHUNK(chunk));
3903
3904         /* Count this as receiving DATA. */
3905         if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3906                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3907                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3908         }
3909
3910         /* FIXME: For now send a SACK, but DATA processing may
3911          * send another.
3912          */
3913         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3914
3915         return SCTP_DISPOSITION_CONSUME;
3916
3917 discard_noforce:
3918         return SCTP_DISPOSITION_DISCARD;
3919 }
3920
3921 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3922         struct net *net,
3923         const struct sctp_endpoint *ep,
3924         const struct sctp_association *asoc,
3925         const sctp_subtype_t type,
3926         void *arg,
3927         sctp_cmd_seq_t *commands)
3928 {
3929         struct sctp_chunk *chunk = arg;
3930         struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3931         struct sctp_fwdtsn_skip *skip;
3932         __u16 len;
3933         __u32 tsn;
3934
3935         if (!sctp_vtag_verify(chunk, asoc)) {
3936                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3937                                 SCTP_NULL());
3938                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3939         }
3940
3941         /* Make sure that the FORWARD_TSN chunk has a valid length.  */
3942         if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3943                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3944                                                   commands);
3945
3946         fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3947         chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3948         len = ntohs(chunk->chunk_hdr->length);
3949         len -= sizeof(struct sctp_chunkhdr);
3950         skb_pull(chunk->skb, len);
3951
3952         tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3953         pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3954
3955         /* The TSN is too high--silently discard the chunk and count on it
3956          * getting retransmitted later.
3957          */
3958         if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3959                 goto gen_shutdown;
3960
3961         /* Silently discard the chunk if stream-id is not valid */
3962         sctp_walk_fwdtsn(skip, chunk) {
3963                 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3964                         goto gen_shutdown;
3965         }
3966
3967         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3968         if (len > sizeof(struct sctp_fwdtsn_hdr))
3969                 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3970                                 SCTP_CHUNK(chunk));
3971
3972         /* Go a head and force a SACK, since we are shutting down. */
3973 gen_shutdown:
3974         /* Implementor's Guide.
3975          *
3976          * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3977          * respond to each received packet containing one or more DATA chunk(s)
3978          * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3979          */
3980         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3981         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3982         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3983                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3984
3985         return SCTP_DISPOSITION_CONSUME;
3986 }
3987
3988 /*
3989  * SCTP-AUTH Section 6.3 Receiving authenticated chukns
3990  *
3991  *    The receiver MUST use the HMAC algorithm indicated in the HMAC
3992  *    Identifier field.  If this algorithm was not specified by the
3993  *    receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3994  *    during association setup, the AUTH chunk and all chunks after it MUST
3995  *    be discarded and an ERROR chunk SHOULD be sent with the error cause
3996  *    defined in Section 4.1.
3997  *
3998  *    If an endpoint with no shared key receives a Shared Key Identifier
3999  *    other than 0, it MUST silently discard all authenticated chunks.  If
4000  *    the endpoint has at least one endpoint pair shared key for the peer,
4001  *    it MUST use the key specified by the Shared Key Identifier if a
4002  *    key has been configured for that Shared Key Identifier.  If no
4003  *    endpoint pair shared key has been configured for that Shared Key
4004  *    Identifier, all authenticated chunks MUST be silently discarded.
4005  *
4006  * Verification Tag:  8.5 Verification Tag [Normal verification]
4007  *
4008  * The return value is the disposition of the chunk.
4009  */
4010 static sctp_ierror_t sctp_sf_authenticate(
4011                                     const struct sctp_association *asoc,
4012                                     struct sctp_chunk *chunk)
4013 {
4014         struct sctp_authhdr *auth_hdr;
4015         struct sctp_hmac *hmac;
4016         unsigned int sig_len;
4017         __u16 key_id;
4018         __u8 *save_digest;
4019         __u8 *digest;
4020
4021         /* Pull in the auth header, so we can do some more verification */
4022         auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4023         chunk->subh.auth_hdr = auth_hdr;
4024         skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4025
4026         /* Make sure that we support the HMAC algorithm from the auth
4027          * chunk.
4028          */
4029         if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4030                 return SCTP_IERROR_AUTH_BAD_HMAC;
4031
4032         /* Make sure that the provided shared key identifier has been
4033          * configured
4034          */
4035         key_id = ntohs(auth_hdr->shkey_id);
4036         if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4037                 return SCTP_IERROR_AUTH_BAD_KEYID;
4038
4039
4040         /* Make sure that the length of the signature matches what
4041          * we expect.
4042          */
4043         sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4044         hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4045         if (sig_len != hmac->hmac_len)
4046                 return SCTP_IERROR_PROTO_VIOLATION;
4047
4048         /* Now that we've done validation checks, we can compute and
4049          * verify the hmac.  The steps involved are:
4050          *  1. Save the digest from the chunk.
4051          *  2. Zero out the digest in the chunk.
4052          *  3. Compute the new digest
4053          *  4. Compare saved and new digests.
4054          */
4055         digest = auth_hdr->hmac;
4056         skb_pull(chunk->skb, sig_len);
4057
4058         save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4059         if (!save_digest)
4060                 goto nomem;
4061
4062         memset(digest, 0, sig_len);
4063
4064         sctp_auth_calculate_hmac(asoc, chunk->skb,
4065                                 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4066                                 GFP_ATOMIC);
4067
4068         /* Discard the packet if the digests do not match */
4069         if (memcmp(save_digest, digest, sig_len)) {
4070                 kfree(save_digest);
4071                 return SCTP_IERROR_BAD_SIG;
4072         }
4073
4074         kfree(save_digest);
4075         chunk->auth = 1;
4076
4077         return SCTP_IERROR_NO_ERROR;
4078 nomem:
4079         return SCTP_IERROR_NOMEM;
4080 }
4081
4082 sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4083                                     const struct sctp_endpoint *ep,
4084                                     const struct sctp_association *asoc,
4085                                     const sctp_subtype_t type,
4086                                     void *arg,
4087                                     sctp_cmd_seq_t *commands)
4088 {
4089         struct sctp_authhdr *auth_hdr;
4090         struct sctp_chunk *chunk = arg;
4091         struct sctp_chunk *err_chunk;
4092         sctp_ierror_t error;
4093
4094         /* Make sure that the peer has AUTH capable */
4095         if (!asoc->peer.auth_capable)
4096                 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4097
4098         if (!sctp_vtag_verify(chunk, asoc)) {
4099                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4100                                 SCTP_NULL());
4101                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4102         }
4103
4104         /* Make sure that the AUTH chunk has valid length.  */
4105         if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4106                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4107                                                   commands);
4108
4109         auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4110         error = sctp_sf_authenticate(asoc, chunk);
4111         switch (error) {
4112         case SCTP_IERROR_AUTH_BAD_HMAC:
4113                 /* Generate the ERROR chunk and discard the rest
4114                  * of the packet
4115                  */
4116                 err_chunk = sctp_make_op_error(asoc, chunk,
4117                                                SCTP_ERROR_UNSUP_HMAC,
4118                                                &auth_hdr->hmac_id,
4119                                                sizeof(__u16), 0);
4120                 if (err_chunk) {
4121                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4122                                         SCTP_CHUNK(err_chunk));
4123                 }
4124                 /* Fall Through */
4125         case SCTP_IERROR_AUTH_BAD_KEYID:
4126         case SCTP_IERROR_BAD_SIG:
4127                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4128
4129         case SCTP_IERROR_PROTO_VIOLATION:
4130                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4131                                                   commands);
4132
4133         case SCTP_IERROR_NOMEM:
4134                 return SCTP_DISPOSITION_NOMEM;
4135
4136         default:                        /* Prevent gcc warnings */
4137                 break;
4138         }
4139
4140         if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4141                 struct sctp_ulpevent *ev;
4142
4143                 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4144                                     SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4145
4146                 if (!ev)
4147                         return -ENOMEM;
4148
4149                 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4150                                 SCTP_ULPEVENT(ev));
4151         }
4152
4153         return SCTP_DISPOSITION_CONSUME;
4154 }
4155
4156 /*
4157  * Process an unknown chunk.
4158  *
4159  * Section: 3.2. Also, 2.1 in the implementor's guide.
4160  *
4161  * Chunk Types are encoded such that the highest-order two bits specify
4162  * the action that must be taken if the processing endpoint does not
4163  * recognize the Chunk Type.
4164  *
4165  * 00 - Stop processing this SCTP packet and discard it, do not process
4166  *      any further chunks within it.
4167  *
4168  * 01 - Stop processing this SCTP packet and discard it, do not process
4169  *      any further chunks within it, and report the unrecognized
4170  *      chunk in an 'Unrecognized Chunk Type'.
4171  *
4172  * 10 - Skip this chunk and continue processing.
4173  *
4174  * 11 - Skip this chunk and continue processing, but report in an ERROR
4175  *      Chunk using the 'Unrecognized Chunk Type' cause of error.
4176  *
4177  * The return value is the disposition of the chunk.
4178  */
4179 sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4180                                      const struct sctp_endpoint *ep,
4181                                      const struct sctp_association *asoc,
4182                                      const sctp_subtype_t type,
4183                                      void *arg,
4184                                      sctp_cmd_seq_t *commands)
4185 {
4186         struct sctp_chunk *unk_chunk = arg;
4187         struct sctp_chunk *err_chunk;
4188         sctp_chunkhdr_t *hdr;
4189
4190         pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4191
4192         if (!sctp_vtag_verify(unk_chunk, asoc))
4193                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4194
4195         /* Make sure that the chunk has a valid length.
4196          * Since we don't know the chunk type, we use a general
4197          * chunkhdr structure to make a comparison.
4198          */
4199         if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4200                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4201                                                   commands);
4202
4203         switch (type.chunk & SCTP_CID_ACTION_MASK) {
4204         case SCTP_CID_ACTION_DISCARD:
4205                 /* Discard the packet.  */
4206                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4207         case SCTP_CID_ACTION_DISCARD_ERR:
4208                 /* Generate an ERROR chunk as response. */
4209                 hdr = unk_chunk->chunk_hdr;
4210                 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4211                                                SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4212                                                SCTP_PAD4(ntohs(hdr->length)),
4213                                                0);
4214                 if (err_chunk) {
4215                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4216                                         SCTP_CHUNK(err_chunk));
4217                 }
4218
4219                 /* Discard the packet.  */
4220                 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4221                 return SCTP_DISPOSITION_CONSUME;
4222         case SCTP_CID_ACTION_SKIP:
4223                 /* Skip the chunk.  */
4224                 return SCTP_DISPOSITION_DISCARD;
4225         case SCTP_CID_ACTION_SKIP_ERR:
4226                 /* Generate an ERROR chunk as response. */
4227                 hdr = unk_chunk->chunk_hdr;
4228                 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4229                                                SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4230                                                SCTP_PAD4(ntohs(hdr->length)),
4231                                                0);
4232                 if (err_chunk) {
4233                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4234                                         SCTP_CHUNK(err_chunk));
4235                 }
4236                 /* Skip the chunk.  */
4237                 return SCTP_DISPOSITION_CONSUME;
4238         default:
4239                 break;
4240         }
4241
4242         return SCTP_DISPOSITION_DISCARD;
4243 }
4244
4245 /*
4246  * Discard the chunk.
4247  *
4248  * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4249  * [Too numerous to mention...]
4250  * Verification Tag: No verification needed.
4251  * Inputs
4252  * (endpoint, asoc, chunk)
4253  *
4254  * Outputs
4255  * (asoc, reply_msg, msg_up, timers, counters)
4256  *
4257  * The return value is the disposition of the chunk.
4258  */
4259 sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4260                                          const struct sctp_endpoint *ep,
4261                                          const struct sctp_association *asoc,
4262                                          const sctp_subtype_t type,
4263                                          void *arg,
4264                                          sctp_cmd_seq_t *commands)
4265 {
4266         struct sctp_chunk *chunk = arg;
4267
4268         /* Make sure that the chunk has a valid length.
4269          * Since we don't know the chunk type, we use a general
4270          * chunkhdr structure to make a comparison.
4271          */
4272         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4273                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4274                                                   commands);
4275
4276         pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4277
4278         return SCTP_DISPOSITION_DISCARD;
4279 }
4280
4281 /*
4282  * Discard the whole packet.
4283  *
4284  * Section: 8.4 2)
4285  *
4286  * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4287  *    silently discard the OOTB packet and take no further action.
4288  *
4289  * Verification Tag: No verification necessary
4290  *
4291  * Inputs
4292  * (endpoint, asoc, chunk)
4293  *
4294  * Outputs
4295  * (asoc, reply_msg, msg_up, timers, counters)
4296  *
4297  * The return value is the disposition of the chunk.
4298  */
4299 sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4300                                     const struct sctp_endpoint *ep,
4301                                     const struct sctp_association *asoc,
4302                                     const sctp_subtype_t type,
4303                                     void *arg,
4304                                     sctp_cmd_seq_t *commands)
4305 {
4306         SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4307         sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4308
4309         return SCTP_DISPOSITION_CONSUME;
4310 }
4311
4312
4313 /*
4314  * The other end is violating protocol.
4315  *
4316  * Section: Not specified
4317  * Verification Tag: Not specified
4318  * Inputs
4319  * (endpoint, asoc, chunk)
4320  *
4321  * Outputs
4322  * (asoc, reply_msg, msg_up, timers, counters)
4323  *
4324  * We simply tag the chunk as a violation.  The state machine will log
4325  * the violation and continue.
4326  */
4327 sctp_disposition_t sctp_sf_violation(struct net *net,
4328                                      const struct sctp_endpoint *ep,
4329                                      const struct sctp_association *asoc,
4330                                      const sctp_subtype_t type,
4331                                      void *arg,
4332                                      sctp_cmd_seq_t *commands)
4333 {
4334         struct sctp_chunk *chunk = arg;
4335
4336         if (!sctp_vtag_verify(chunk, asoc))
4337                 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4338
4339         /* Make sure that the chunk has a valid length. */
4340         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4341                 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4342                                                   commands);
4343
4344         return SCTP_DISPOSITION_VIOLATION;
4345 }
4346
4347 /*
4348  * Common function to handle a protocol violation.
4349  */
4350 static sctp_disposition_t sctp_sf_abort_violation(
4351                                      struct net *net,
4352                                      const struct sctp_endpoint *ep,
4353                                      const struct sctp_association *asoc,
4354                                      void *arg,
4355                                      sctp_cmd_seq_t *commands,
4356                                      const __u8 *payload,
4357                                      const size_t paylen)
4358 {
4359         struct sctp_packet *packet = NULL;
4360         struct sctp_chunk *chunk =  arg;
4361         struct sctp_chunk *abort = NULL;
4362
4363         /* SCTP-AUTH, Section 6.3:
4364          *    It should be noted that if the receiver wants to tear
4365          *    down an association in an authenticated way only, the
4366          *    handling of malformed packets should not result in
4367          *    tearing down the association.
4368          *
4369          * This means that if we only want to abort associations
4370          * in an authenticated way (i.e AUTH+ABORT), then we
4371          * can't destroy this association just because the packet
4372          * was malformed.
4373          */
4374         if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4375                 goto discard;
4376
4377         /* Make the abort chunk. */
4378         abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4379         if (!abort)
4380                 goto nomem;
4381
4382         if (asoc) {
4383                 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4384                 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4385                     !asoc->peer.i.init_tag) {
4386                         sctp_initack_chunk_t *initack;
4387
4388                         initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4389                         if (!sctp_chunk_length_valid(chunk,
4390                                                      sizeof(sctp_initack_chunk_t)))
4391                                 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4392                         else {
4393                                 unsigned int inittag;
4394
4395                                 inittag = ntohl(initack->init_hdr.init_tag);
4396                                 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4397                                                 SCTP_U32(inittag));
4398                         }
4399                 }
4400
4401                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4402                 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4403
4404                 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4405                         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4406                                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4407                         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4408                                         SCTP_ERROR(ECONNREFUSED));
4409                         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4410                                         SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4411                 } else {
4412                         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4413                                         SCTP_ERROR(ECONNABORTED));
4414                         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4415                                         SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4416                         SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4417                 }
4418         } else {
4419                 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4420
4421                 if (!packet)
4422                         goto nomem_pkt;
4423
4424                 if (sctp_test_T_bit(abort))
4425                         packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4426
4427                 abort->skb->sk = ep->base.sk;
4428
4429                 sctp_packet_append_chunk(packet, abort);
4430
4431                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4432                         SCTP_PACKET(packet));
4433
4434                 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4435         }
4436
4437         SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4438
4439 discard:
4440         sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4441         return SCTP_DISPOSITION_ABORT;
4442
4443 nomem_pkt:
4444         sctp_chunk_free(abort);
4445 nomem:
4446         return SCTP_DISPOSITION_NOMEM;
4447 }
4448
4449 /*
4450  * Handle a protocol violation when the chunk length is invalid.
4451  * "Invalid" length is identified as smaller than the minimal length a
4452  * given chunk can be.  For example, a SACK chunk has invalid length
4453  * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4454  *
4455  * We inform the other end by sending an ABORT with a Protocol Violation
4456  * error code.
4457  *
4458  * Section: Not specified
4459  * Verification Tag:  Nothing to do
4460  * Inputs
4461  * (endpoint, asoc, chunk)
4462  *
4463  * Outputs
4464  * (reply_msg, msg_up, counters)
4465  *
4466  * Generate an  ABORT chunk and terminate the association.
4467  */
4468 static sctp_disposition_t sctp_sf_violation_chunklen(
4469                                      struct net *net,
4470                                      const struct sctp_endpoint *ep,
4471                                      const struct sctp_association *asoc,
4472                                      const sctp_subtype_t type,
4473                                      void *arg,
4474                                      sctp_cmd_seq_t *commands)
4475 {
4476         static const char err_str[] = "The following chunk had invalid length:";
4477
4478         return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4479                                         sizeof(err_str));
4480 }
4481
4482 /*
4483  * Handle a protocol violation when the parameter length is invalid.
4484  * If the length is smaller than the minimum length of a given parameter,
4485  * or accumulated length in multi parameters exceeds the end of the chunk,
4486  * the length is considered as invalid.
4487  */
4488 static sctp_disposition_t sctp_sf_violation_paramlen(
4489                                      struct net *net,
4490                                      const struct sctp_endpoint *ep,
4491                                      const struct sctp_association *asoc,
4492                                      const sctp_subtype_t type,
4493                                      void *arg, void *ext,
4494                                      sctp_cmd_seq_t *commands)
4495 {
4496         struct sctp_chunk *chunk =  arg;
4497         struct sctp_paramhdr *param = ext;
4498         struct sctp_chunk *abort = NULL;
4499
4500         if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4501                 goto discard;
4502
4503         /* Make the abort chunk. */
4504         abort = sctp_make_violation_paramlen(asoc, chunk, param);
4505         if (!abort)
4506                 goto nomem;
4507
4508         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4509         SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4510
4511         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4512                         SCTP_ERROR(ECONNABORTED));
4513         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4514                         SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4515         SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4516         SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4517
4518 discard:
4519         sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4520         return SCTP_DISPOSITION_ABORT;
4521 nomem:
4522         return SCTP_DISPOSITION_NOMEM;
4523 }
4524
4525 /* Handle a protocol violation when the peer trying to advance the
4526  * cumulative tsn ack to a point beyond the max tsn currently sent.
4527  *
4528  * We inform the other end by sending an ABORT with a Protocol Violation
4529  * error code.
4530  */
4531 static sctp_disposition_t sctp_sf_violation_ctsn(
4532                                      struct net *net,
4533                                      const struct sctp_endpoint *ep,
4534                                      const struct sctp_association *asoc,
4535                                      const sctp_subtype_t type,
4536                                      void *arg,
4537                                      sctp_cmd_seq_t *commands)
4538 {
4539         static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4540
4541         return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4542                                         sizeof(err_str));
4543 }
4544
4545 /* Handle protocol violation of an invalid chunk bundling.  For example,
4546  * when we have an association and we receive bundled INIT-ACK, or
4547  * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4548  * statement from the specs.  Additionally, there might be an attacker
4549  * on the path and we may not want to continue this communication.
4550  */
4551 static sctp_disposition_t sctp_sf_violation_chunk(
4552                                      struct net *net,
4553                                      const struct sctp_endpoint *ep,
4554                                      const struct sctp_association *asoc,
4555                                      const sctp_subtype_t type,
4556                                      void *arg,
4557                                      sctp_cmd_seq_t *commands)
4558 {
4559         static const char err_str[] = "The following chunk violates protocol:";
4560
4561         if (!asoc)
4562                 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4563
4564         return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4565                                         sizeof(err_str));
4566 }
4567 /***************************************************************************
4568  * These are the state functions for handling primitive (Section 10) events.
4569  ***************************************************************************/
4570 /*
4571  * sctp_sf_do_prm_asoc
4572  *
4573  * Section: 10.1 ULP-to-SCTP
4574  * B) Associate
4575  *
4576  * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4577  * outbound stream count)
4578  * -> association id [,destination transport addr list] [,outbound stream
4579  * count]
4580  *
4581  * This primitive allows the upper layer to initiate an association to a
4582  * specific peer endpoint.
4583  *
4584  * The peer endpoint shall be specified by one of the transport addresses
4585  * which defines the endpoint (see Section 1.4).  If the local SCTP
4586  * instance has not been initialized, the ASSOCIATE is considered an
4587  * error.
4588  * [This is not relevant for the kernel implementation since we do all
4589  * initialization at boot time.  It we hadn't initialized we wouldn't
4590  * get anywhere near this code.]
4591  *
4592  * An association id, which is a local handle to the SCTP association,
4593  * will be returned on successful establishment of the association. If
4594  * SCTP is not able to open an SCTP association with the peer endpoint,
4595  * an error is returned.
4596  * [In the kernel implementation, the struct sctp_association needs to
4597  * be created BEFORE causing this primitive to run.]
4598  *
4599  * Other association parameters may be returned, including the
4600  * complete destination transport addresses of the peer as well as the
4601  * outbound stream count of the local endpoint. One of the transport
4602  * address from the returned destination addresses will be selected by
4603  * the local endpoint as default primary path for sending SCTP packets
4604  * to this peer.  The returned "destination transport addr list" can
4605  * be used by the ULP to change the default primary path or to force
4606  * sending a packet to a specific transport address.  [All of this
4607  * stuff happens when the INIT ACK arrives.  This is a NON-BLOCKING
4608  * function.]
4609  *
4610  * Mandatory attributes:
4611  *
4612  * o local SCTP instance name - obtained from the INITIALIZE operation.
4613  *   [This is the argument asoc.]
4614  * o destination transport addr - specified as one of the transport
4615  * addresses of the peer endpoint with which the association is to be
4616  * established.
4617  *  [This is asoc->peer.active_path.]
4618  * o outbound stream count - the number of outbound streams the ULP
4619  * would like to open towards this peer endpoint.
4620  * [BUG: This is not currently implemented.]
4621  * Optional attributes:
4622  *
4623  * None.
4624  *
4625  * The return value is a disposition.
4626  */
4627 sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4628                                        const struct sctp_endpoint *ep,
4629                                        const struct sctp_association *asoc,
4630                                        const sctp_subtype_t type,
4631                                        void *arg,
4632                                        sctp_cmd_seq_t *commands)
4633 {
4634         struct sctp_chunk *repl;
4635         struct sctp_association *my_asoc;
4636
4637         /* The comment below says that we enter COOKIE-WAIT AFTER
4638          * sending the INIT, but that doesn't actually work in our
4639          * implementation...
4640          */
4641         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4642                         SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4643
4644         /* RFC 2960 5.1 Normal Establishment of an Association
4645          *
4646          * A) "A" first sends an INIT chunk to "Z".  In the INIT, "A"
4647          * must provide its Verification Tag (Tag_A) in the Initiate
4648          * Tag field.  Tag_A SHOULD be a random number in the range of
4649          * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4650          */
4651
4652         repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4653         if (!repl)
4654                 goto nomem;
4655
4656         /* Choose transport for INIT. */
4657         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4658                         SCTP_CHUNK(repl));
4659
4660         /* Cast away the const modifier, as we want to just
4661          * rerun it through as a sideffect.
4662          */
4663         my_asoc = (struct sctp_association *)asoc;
4664         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4665
4666         /* After sending the INIT, "A" starts the T1-init timer and
4667          * enters the COOKIE-WAIT state.
4668          */
4669         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4670                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4671         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4672         return SCTP_DISPOSITION_CONSUME;
4673
4674 nomem:
4675         return SCTP_DISPOSITION_NOMEM;
4676 }
4677
4678 /*
4679  * Process the SEND primitive.
4680  *
4681  * Section: 10.1 ULP-to-SCTP
4682  * E) Send
4683  *
4684  * Format: SEND(association id, buffer address, byte count [,context]
4685  *         [,stream id] [,life time] [,destination transport address]
4686  *         [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4687  * -> result
4688  *
4689  * This is the main method to send user data via SCTP.
4690  *
4691  * Mandatory attributes:
4692  *
4693  *  o association id - local handle to the SCTP association
4694  *
4695  *  o buffer address - the location where the user message to be
4696  *    transmitted is stored;
4697  *
4698  *  o byte count - The size of the user data in number of bytes;
4699  *
4700  * Optional attributes:
4701  *
4702  *  o context - an optional 32 bit integer that will be carried in the
4703  *    sending failure notification to the ULP if the transportation of
4704  *    this User Message fails.
4705  *
4706  *  o stream id - to indicate which stream to send the data on. If not
4707  *    specified, stream 0 will be used.
4708  *
4709  *  o life time - specifies the life time of the user data. The user data
4710  *    will not be sent by SCTP after the life time expires. This
4711  *    parameter can be used to avoid efforts to transmit stale
4712  *    user messages. SCTP notifies the ULP if the data cannot be
4713  *    initiated to transport (i.e. sent to the destination via SCTP's
4714  *    send primitive) within the life time variable. However, the
4715  *    user data will be transmitted if SCTP has attempted to transmit a
4716  *    chunk before the life time expired.
4717  *
4718  *  o destination transport address - specified as one of the destination
4719  *    transport addresses of the peer endpoint to which this packet
4720  *    should be sent. Whenever possible, SCTP should use this destination
4721  *    transport address for sending the packets, instead of the current
4722  *    primary path.
4723  *
4724  *  o unorder flag - this flag, if present, indicates that the user
4725  *    would like the data delivered in an unordered fashion to the peer
4726  *    (i.e., the U flag is set to 1 on all DATA chunks carrying this
4727  *    message).
4728  *
4729  *  o no-bundle flag - instructs SCTP not to bundle this user data with
4730  *    other outbound DATA chunks. SCTP MAY still bundle even when
4731  *    this flag is present, when faced with network congestion.
4732  *
4733  *  o payload protocol-id - A 32 bit unsigned integer that is to be
4734  *    passed to the peer indicating the type of payload protocol data
4735  *    being transmitted. This value is passed as opaque data by SCTP.
4736  *
4737  * The return value is the disposition.
4738  */
4739 sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4740                                        const struct sctp_endpoint *ep,
4741                                        const struct sctp_association *asoc,
4742                                        const sctp_subtype_t type,
4743                                        void *arg,
4744                                        sctp_cmd_seq_t *commands)
4745 {
4746         struct sctp_datamsg *msg = arg;
4747
4748         sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4749         return SCTP_DISPOSITION_CONSUME;
4750 }
4751
4752 /*
4753  * Process the SHUTDOWN primitive.
4754  *
4755  * Section: 10.1:
4756  * C) Shutdown
4757  *
4758  * Format: SHUTDOWN(association id)
4759  * -> result
4760  *
4761  * Gracefully closes an association. Any locally queued user data
4762  * will be delivered to the peer. The association will be terminated only
4763  * after the peer acknowledges all the SCTP packets sent.  A success code
4764  * will be returned on successful termination of the association. If
4765  * attempting to terminate the association results in a failure, an error
4766  * code shall be returned.
4767  *
4768  * Mandatory attributes:
4769  *
4770  *  o association id - local handle to the SCTP association
4771  *
4772  * Optional attributes:
4773  *
4774  * None.
4775  *
4776  * The return value is the disposition.
4777  */
4778 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4779         struct net *net,
4780         const struct sctp_endpoint *ep,
4781         const struct sctp_association *asoc,
4782         const sctp_subtype_t type,
4783         void *arg,
4784         sctp_cmd_seq_t *commands)
4785 {
4786         int disposition;
4787
4788         /* From 9.2 Shutdown of an Association
4789          * Upon receipt of the SHUTDOWN primitive from its upper
4790          * layer, the endpoint enters SHUTDOWN-PENDING state and
4791          * remains there until all outstanding data has been
4792          * acknowledged by its peer. The endpoint accepts no new data
4793          * from its upper layer, but retransmits data to the far end
4794          * if necessary to fill gaps.
4795          */
4796         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4797                         SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4798
4799         disposition = SCTP_DISPOSITION_CONSUME;
4800         if (sctp_outq_is_empty(&asoc->outqueue)) {
4801                 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
4802                                                             arg, commands);
4803         }
4804         return disposition;
4805 }
4806
4807 /*
4808  * Process the ABORT primitive.
4809  *
4810  * Section: 10.1:
4811  * C) Abort
4812  *
4813  * Format: Abort(association id [, cause code])
4814  * -> result
4815  *
4816  * Ungracefully closes an association. Any locally queued user data
4817  * will be discarded and an ABORT chunk is sent to the peer.  A success code
4818  * will be returned on successful abortion of the association. If
4819  * attempting to abort the association results in a failure, an error
4820  * code shall be returned.
4821  *
4822  * Mandatory attributes:
4823  *
4824  *  o association id - local handle to the SCTP association
4825  *
4826  * Optional attributes:
4827  *
4828  *  o cause code - reason of the abort to be passed to the peer
4829  *
4830  * None.
4831  *
4832  * The return value is the disposition.
4833  */
4834 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4835         struct net *net,
4836         const struct sctp_endpoint *ep,
4837         const struct sctp_association *asoc,
4838         const sctp_subtype_t type,
4839         void *arg,
4840         sctp_cmd_seq_t *commands)
4841 {
4842         /* From 9.1 Abort of an Association
4843          * Upon receipt of the ABORT primitive from its upper
4844          * layer, the endpoint enters CLOSED state and
4845          * discard all outstanding data has been
4846          * acknowledged by its peer. The endpoint accepts no new data
4847          * from its upper layer, but retransmits data to the far end
4848          * if necessary to fill gaps.
4849          */
4850         struct sctp_chunk *abort = arg;
4851
4852         if (abort)
4853                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4854
4855         /* Even if we can't send the ABORT due to low memory delete the
4856          * TCB.  This is a departure from our typical NOMEM handling.
4857          */
4858
4859         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4860                         SCTP_ERROR(ECONNABORTED));
4861         /* Delete the established association. */
4862         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4863                         SCTP_PERR(SCTP_ERROR_USER_ABORT));
4864
4865         SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4866         SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4867
4868         return SCTP_DISPOSITION_ABORT;
4869 }
4870
4871 /* We tried an illegal operation on an association which is closed.  */
4872 sctp_disposition_t sctp_sf_error_closed(struct net *net,
4873                                         const struct sctp_endpoint *ep,
4874                                         const struct sctp_association *asoc,
4875                                         const sctp_subtype_t type,
4876                                         void *arg,
4877                                         sctp_cmd_seq_t *commands)
4878 {
4879         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4880         return SCTP_DISPOSITION_CONSUME;
4881 }
4882
4883 /* We tried an illegal operation on an association which is shutting
4884  * down.
4885  */
4886 sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4887                                           const struct sctp_endpoint *ep,
4888                                           const struct sctp_association *asoc,
4889                                           const sctp_subtype_t type,
4890                                           void *arg,
4891                                           sctp_cmd_seq_t *commands)
4892 {
4893         sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4894                         SCTP_ERROR(-ESHUTDOWN));
4895         return SCTP_DISPOSITION_CONSUME;
4896 }
4897
4898 /*
4899  * sctp_cookie_wait_prm_shutdown
4900  *
4901  * Section: 4 Note: 2
4902  * Verification Tag:
4903  * Inputs
4904  * (endpoint, asoc)
4905  *
4906  * The RFC does not explicitly address this issue, but is the route through the
4907  * state table when someone issues a shutdown while in COOKIE_WAIT state.
4908  *
4909  * Outputs
4910  * (timers)
4911  */
4912 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4913         struct net *net,
4914         const struct sctp_endpoint *ep,
4915         const struct sctp_association *asoc,
4916         const sctp_subtype_t type,
4917         void *arg,
4918         sctp_cmd_seq_t *commands)
4919 {
4920         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4921                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4922
4923         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4924                         SCTP_STATE(SCTP_STATE_CLOSED));
4925
4926         SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
4927
4928         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4929
4930         return SCTP_DISPOSITION_DELETE_TCB;
4931 }
4932
4933 /*
4934  * sctp_cookie_echoed_prm_shutdown
4935  *
4936  * Section: 4 Note: 2
4937  * Verification Tag:
4938  * Inputs
4939  * (endpoint, asoc)
4940  *
4941  * The RFC does not explcitly address this issue, but is the route through the
4942  * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4943  *
4944  * Outputs
4945  * (timers)
4946  */
4947 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4948         struct net *net,
4949         const struct sctp_endpoint *ep,
4950         const struct sctp_association *asoc,
4951         const sctp_subtype_t type,
4952         void *arg, sctp_cmd_seq_t *commands)
4953 {
4954         /* There is a single T1 timer, so we should be able to use
4955          * common function with the COOKIE-WAIT state.
4956          */
4957         return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
4958 }
4959
4960 /*
4961  * sctp_sf_cookie_wait_prm_abort
4962  *
4963  * Section: 4 Note: 2
4964  * Verification Tag:
4965  * Inputs
4966  * (endpoint, asoc)
4967  *
4968  * The RFC does not explicitly address this issue, but is the route through the
4969  * state table when someone issues an abort while in COOKIE_WAIT state.
4970  *
4971  * Outputs
4972  * (timers)
4973  */
4974 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4975         struct net *net,
4976         const struct sctp_endpoint *ep,
4977         const struct sctp_association *asoc,
4978         const sctp_subtype_t type,
4979         void *arg,
4980         sctp_cmd_seq_t *commands)
4981 {
4982         struct sctp_chunk *abort = arg;
4983
4984         /* Stop T1-init timer */
4985         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4986                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4987
4988         if (abort)
4989                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4990
4991         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4992                         SCTP_STATE(SCTP_STATE_CLOSED));
4993
4994         SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4995
4996         /* Even if we can't send the ABORT due to low memory delete the
4997          * TCB.  This is a departure from our typical NOMEM handling.
4998          */
4999
5000         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5001                         SCTP_ERROR(ECONNREFUSED));
5002         /* Delete the established association. */
5003         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5004                         SCTP_PERR(SCTP_ERROR_USER_ABORT));
5005
5006         return SCTP_DISPOSITION_ABORT;
5007 }
5008
5009 /*
5010  * sctp_sf_cookie_echoed_prm_abort
5011  *
5012  * Section: 4 Note: 3
5013  * Verification Tag:
5014  * Inputs
5015  * (endpoint, asoc)
5016  *
5017  * The RFC does not explcitly address this issue, but is the route through the
5018  * state table when someone issues an abort while in COOKIE_ECHOED state.
5019  *
5020  * Outputs
5021  * (timers)
5022  */
5023 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
5024         struct net *net,
5025         const struct sctp_endpoint *ep,
5026         const struct sctp_association *asoc,
5027         const sctp_subtype_t type,
5028         void *arg,
5029         sctp_cmd_seq_t *commands)
5030 {
5031         /* There is a single T1 timer, so we should be able to use
5032          * common function with the COOKIE-WAIT state.
5033          */
5034         return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5035 }
5036
5037 /*
5038  * sctp_sf_shutdown_pending_prm_abort
5039  *
5040  * Inputs
5041  * (endpoint, asoc)
5042  *
5043  * The RFC does not explicitly address this issue, but is the route through the
5044  * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5045  *
5046  * Outputs
5047  * (timers)
5048  */
5049 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
5050         struct net *net,
5051         const struct sctp_endpoint *ep,
5052         const struct sctp_association *asoc,
5053         const sctp_subtype_t type,
5054         void *arg,
5055         sctp_cmd_seq_t *commands)
5056 {
5057         /* Stop the T5-shutdown guard timer.  */
5058         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5059                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5060
5061         return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5062 }
5063
5064 /*
5065  * sctp_sf_shutdown_sent_prm_abort
5066  *
5067  * Inputs
5068  * (endpoint, asoc)
5069  *
5070  * The RFC does not explicitly address this issue, but is the route through the
5071  * state table when someone issues an abort while in SHUTDOWN-SENT state.
5072  *
5073  * Outputs
5074  * (timers)
5075  */
5076 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
5077         struct net *net,
5078         const struct sctp_endpoint *ep,
5079         const struct sctp_association *asoc,
5080         const sctp_subtype_t type,
5081         void *arg,
5082         sctp_cmd_seq_t *commands)
5083 {
5084         /* Stop the T2-shutdown timer.  */
5085         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5086                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5087
5088         /* Stop the T5-shutdown guard timer.  */
5089         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5090                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5091
5092         return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5093 }
5094
5095 /*
5096  * sctp_sf_cookie_echoed_prm_abort
5097  *
5098  * Inputs
5099  * (endpoint, asoc)
5100  *
5101  * The RFC does not explcitly address this issue, but is the route through the
5102  * state table when someone issues an abort while in COOKIE_ECHOED state.
5103  *
5104  * Outputs
5105  * (timers)
5106  */
5107 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
5108         struct net *net,
5109         const struct sctp_endpoint *ep,
5110         const struct sctp_association *asoc,
5111         const sctp_subtype_t type,
5112         void *arg,
5113         sctp_cmd_seq_t *commands)
5114 {
5115         /* The same T2 timer, so we should be able to use
5116          * common function with the SHUTDOWN-SENT state.
5117          */
5118         return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5119 }
5120
5121 /*
5122  * Process the REQUESTHEARTBEAT primitive
5123  *
5124  * 10.1 ULP-to-SCTP
5125  * J) Request Heartbeat
5126  *
5127  * Format: REQUESTHEARTBEAT(association id, destination transport address)
5128  *
5129  * -> result
5130  *
5131  * Instructs the local endpoint to perform a HeartBeat on the specified
5132  * destination transport address of the given association. The returned
5133  * result should indicate whether the transmission of the HEARTBEAT
5134  * chunk to the destination address is successful.
5135  *
5136  * Mandatory attributes:
5137  *
5138  * o association id - local handle to the SCTP association
5139  *
5140  * o destination transport address - the transport address of the
5141  *   association on which a heartbeat should be issued.
5142  */
5143 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5144                                         struct net *net,
5145                                         const struct sctp_endpoint *ep,
5146                                         const struct sctp_association *asoc,
5147                                         const sctp_subtype_t type,
5148                                         void *arg,
5149                                         sctp_cmd_seq_t *commands)
5150 {
5151         if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5152                                       (struct sctp_transport *)arg, commands))
5153                 return SCTP_DISPOSITION_NOMEM;
5154
5155         /*
5156          * RFC 2960 (bis), section 8.3
5157          *
5158          *    D) Request an on-demand HEARTBEAT on a specific destination
5159          *    transport address of a given association.
5160          *
5161          *    The endpoint should increment the respective error  counter of
5162          *    the destination transport address each time a HEARTBEAT is sent
5163          *    to that address and not acknowledged within one RTO.
5164          *
5165          */
5166         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5167                         SCTP_TRANSPORT(arg));
5168         return SCTP_DISPOSITION_CONSUME;
5169 }
5170
5171 /*
5172  * ADDIP Section 4.1 ASCONF Chunk Procedures
5173  * When an endpoint has an ASCONF signaled change to be sent to the
5174  * remote endpoint it should do A1 to A9
5175  */
5176 sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5177                                         const struct sctp_endpoint *ep,
5178                                         const struct sctp_association *asoc,
5179                                         const sctp_subtype_t type,
5180                                         void *arg,
5181                                         sctp_cmd_seq_t *commands)
5182 {
5183         struct sctp_chunk *chunk = arg;
5184
5185         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5186         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5187                         SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5188         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5189         return SCTP_DISPOSITION_CONSUME;
5190 }
5191
5192 /*
5193  * Ignore the primitive event
5194  *
5195  * The return value is the disposition of the primitive.
5196  */
5197 sctp_disposition_t sctp_sf_ignore_primitive(
5198         struct net *net,
5199         const struct sctp_endpoint *ep,
5200         const struct sctp_association *asoc,
5201         const sctp_subtype_t type,
5202         void *arg,
5203         sctp_cmd_seq_t *commands)
5204 {
5205         pr_debug("%s: primitive type:%d is ignored\n", __func__,
5206                  type.primitive);
5207
5208         return SCTP_DISPOSITION_DISCARD;
5209 }
5210
5211 /***************************************************************************
5212  * These are the state functions for the OTHER events.
5213  ***************************************************************************/
5214
5215 /*
5216  * When the SCTP stack has no more user data to send or retransmit, this
5217  * notification is given to the user. Also, at the time when a user app
5218  * subscribes to this event, if there is no data to be sent or
5219  * retransmit, the stack will immediately send up this notification.
5220  */
5221 sctp_disposition_t sctp_sf_do_no_pending_tsn(
5222         struct net *net,
5223         const struct sctp_endpoint *ep,
5224         const struct sctp_association *asoc,
5225         const sctp_subtype_t type,
5226         void *arg,
5227         sctp_cmd_seq_t *commands)
5228 {
5229         struct sctp_ulpevent *event;
5230
5231         event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5232         if (!event)
5233                 return SCTP_DISPOSITION_NOMEM;
5234
5235         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5236
5237         return SCTP_DISPOSITION_CONSUME;
5238 }
5239
5240 /*
5241  * Start the shutdown negotiation.
5242  *
5243  * From Section 9.2:
5244  * Once all its outstanding data has been acknowledged, the endpoint
5245  * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5246  * TSN Ack field the last sequential TSN it has received from the peer.
5247  * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5248  * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5249  * with the updated last sequential TSN received from its peer.
5250  *
5251  * The return value is the disposition.
5252  */
5253 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5254         struct net *net,
5255         const struct sctp_endpoint *ep,
5256         const struct sctp_association *asoc,
5257         const sctp_subtype_t type,
5258         void *arg,
5259         sctp_cmd_seq_t *commands)
5260 {
5261         struct sctp_chunk *reply;
5262
5263         /* Once all its outstanding data has been acknowledged, the
5264          * endpoint shall send a SHUTDOWN chunk to its peer including
5265          * in the Cumulative TSN Ack field the last sequential TSN it
5266          * has received from the peer.
5267          */
5268         reply = sctp_make_shutdown(asoc, NULL);
5269         if (!reply)
5270                 goto nomem;
5271
5272         /* Set the transport for the SHUTDOWN chunk and the timeout for the
5273          * T2-shutdown timer.
5274          */
5275         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5276
5277         /* It shall then start the T2-shutdown timer */
5278         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5279                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5280
5281         /* RFC 4960 Section 9.2
5282          * The sender of the SHUTDOWN MAY also start an overall guard timer
5283          * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5284          */
5285         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5286                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5287
5288         if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5289                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5290                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5291
5292         /* and enter the SHUTDOWN-SENT state.  */
5293         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5294                         SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5295
5296         /* sctp-implguide 2.10 Issues with Heartbeating and failover
5297          *
5298          * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5299          * or SHUTDOWN-ACK.
5300          */
5301         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5302
5303         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5304
5305         return SCTP_DISPOSITION_CONSUME;
5306
5307 nomem:
5308         return SCTP_DISPOSITION_NOMEM;
5309 }
5310
5311 /*
5312  * Generate a SHUTDOWN ACK now that everything is SACK'd.
5313  *
5314  * From Section 9.2:
5315  *
5316  * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5317  * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5318  * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5319  * endpoint must re-send the SHUTDOWN ACK.
5320  *
5321  * The return value is the disposition.
5322  */
5323 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5324         struct net *net,
5325         const struct sctp_endpoint *ep,
5326         const struct sctp_association *asoc,
5327         const sctp_subtype_t type,
5328         void *arg,
5329         sctp_cmd_seq_t *commands)
5330 {
5331         struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5332         struct sctp_chunk *reply;
5333
5334         /* There are 2 ways of getting here:
5335          *    1) called in response to a SHUTDOWN chunk
5336          *    2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5337          *
5338          * For the case (2), the arg parameter is set to NULL.  We need
5339          * to check that we have a chunk before accessing it's fields.
5340          */
5341         if (chunk) {
5342                 if (!sctp_vtag_verify(chunk, asoc))
5343                         return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
5344
5345                 /* Make sure that the SHUTDOWN chunk has a valid length. */
5346                 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5347                         return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
5348                                                           commands);
5349         }
5350
5351         /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5352          * shall send a SHUTDOWN ACK ...
5353          */
5354         reply = sctp_make_shutdown_ack(asoc, chunk);
5355         if (!reply)
5356                 goto nomem;
5357
5358         /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5359          * the T2-shutdown timer.
5360          */
5361         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5362
5363         /* and start/restart a T2-shutdown timer of its own, */
5364         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5365                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5366
5367         if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5368                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5369                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5370
5371         /* Enter the SHUTDOWN-ACK-SENT state.  */
5372         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5373                         SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5374
5375         /* sctp-implguide 2.10 Issues with Heartbeating and failover
5376          *
5377          * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5378          * or SHUTDOWN-ACK.
5379          */
5380         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5381
5382         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5383
5384         return SCTP_DISPOSITION_CONSUME;
5385
5386 nomem:
5387         return SCTP_DISPOSITION_NOMEM;
5388 }
5389
5390 /*
5391  * Ignore the event defined as other
5392  *
5393  * The return value is the disposition of the event.
5394  */
5395 sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5396                                         const struct sctp_endpoint *ep,
5397                                         const struct sctp_association *asoc,
5398                                         const sctp_subtype_t type,
5399                                         void *arg,
5400                                         sctp_cmd_seq_t *commands)
5401 {
5402         pr_debug("%s: the event other type:%d is ignored\n",
5403                  __func__, type.other);
5404
5405         return SCTP_DISPOSITION_DISCARD;
5406 }
5407
5408 /************************************************************
5409  * These are the state functions for handling timeout events.
5410  ************************************************************/
5411
5412 /*
5413  * RTX Timeout
5414  *
5415  * Section: 6.3.3 Handle T3-rtx Expiration
5416  *
5417  * Whenever the retransmission timer T3-rtx expires for a destination
5418  * address, do the following:
5419  * [See below]
5420  *
5421  * The return value is the disposition of the chunk.
5422  */
5423 sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5424                                         const struct sctp_endpoint *ep,
5425                                         const struct sctp_association *asoc,
5426                                         const sctp_subtype_t type,
5427                                         void *arg,
5428                                         sctp_cmd_seq_t *commands)
5429 {
5430         struct sctp_transport *transport = arg;
5431
5432         SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5433
5434         if (asoc->overall_error_count >= asoc->max_retrans) {
5435                 if (asoc->peer.zero_window_announced &&
5436                     asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5437                         /*
5438                          * We are here likely because the receiver had its rwnd
5439                          * closed for a while and we have not been able to
5440                          * transmit the locally queued data within the maximum
5441                          * retransmission attempts limit.  Start the T5
5442                          * shutdown guard timer to give the receiver one last
5443                          * chance and some additional time to recover before
5444                          * aborting.
5445                          */
5446                         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5447                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5448                 } else {
5449                         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5450                                         SCTP_ERROR(ETIMEDOUT));
5451                         /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5452                         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5453                                         SCTP_PERR(SCTP_ERROR_NO_ERROR));
5454                         SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5455                         SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5456                         return SCTP_DISPOSITION_DELETE_TCB;
5457                 }
5458         }
5459
5460         /* E1) For the destination address for which the timer
5461          * expires, adjust its ssthresh with rules defined in Section
5462          * 7.2.3 and set the cwnd <- MTU.
5463          */
5464
5465         /* E2) For the destination address for which the timer
5466          * expires, set RTO <- RTO * 2 ("back off the timer").  The
5467          * maximum value discussed in rule C7 above (RTO.max) may be
5468          * used to provide an upper bound to this doubling operation.
5469          */
5470
5471         /* E3) Determine how many of the earliest (i.e., lowest TSN)
5472          * outstanding DATA chunks for the address for which the
5473          * T3-rtx has expired will fit into a single packet, subject
5474          * to the MTU constraint for the path corresponding to the
5475          * destination transport address to which the retransmission
5476          * is being sent (this may be different from the address for
5477          * which the timer expires [see Section 6.4]).  Call this
5478          * value K. Bundle and retransmit those K DATA chunks in a
5479          * single packet to the destination endpoint.
5480          *
5481          * Note: Any DATA chunks that were sent to the address for
5482          * which the T3-rtx timer expired but did not fit in one MTU
5483          * (rule E3 above), should be marked for retransmission and
5484          * sent as soon as cwnd allows (normally when a SACK arrives).
5485          */
5486
5487         /* Do some failure management (Section 8.2). */
5488         sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5489
5490         /* NB: Rules E4 and F1 are implicit in R1.  */
5491         sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5492
5493         return SCTP_DISPOSITION_CONSUME;
5494 }
5495
5496 /*
5497  * Generate delayed SACK on timeout
5498  *
5499  * Section: 6.2  Acknowledgement on Reception of DATA Chunks
5500  *
5501  * The guidelines on delayed acknowledgement algorithm specified in
5502  * Section 4.2 of [RFC2581] SHOULD be followed.  Specifically, an
5503  * acknowledgement SHOULD be generated for at least every second packet
5504  * (not every second DATA chunk) received, and SHOULD be generated
5505  * within 200 ms of the arrival of any unacknowledged DATA chunk.  In
5506  * some situations it may be beneficial for an SCTP transmitter to be
5507  * more conservative than the algorithms detailed in this document
5508  * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5509  * the following algorithms allow.
5510  */
5511 sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5512                                        const struct sctp_endpoint *ep,
5513                                        const struct sctp_association *asoc,
5514                                        const sctp_subtype_t type,
5515                                        void *arg,
5516                                        sctp_cmd_seq_t *commands)
5517 {
5518         SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5519         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5520         return SCTP_DISPOSITION_CONSUME;
5521 }
5522
5523 /*
5524  * sctp_sf_t1_init_timer_expire
5525  *
5526  * Section: 4 Note: 2
5527  * Verification Tag:
5528  * Inputs
5529  * (endpoint, asoc)
5530  *
5531  *  RFC 2960 Section 4 Notes
5532  *  2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5533  *     and re-start the T1-init timer without changing state.  This MUST
5534  *     be repeated up to 'Max.Init.Retransmits' times.  After that, the
5535  *     endpoint MUST abort the initialization process and report the
5536  *     error to SCTP user.
5537  *
5538  * Outputs
5539  * (timers, events)
5540  *
5541  */
5542 sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5543                                            const struct sctp_endpoint *ep,
5544                                            const struct sctp_association *asoc,
5545                                            const sctp_subtype_t type,
5546                                            void *arg,
5547                                            sctp_cmd_seq_t *commands)
5548 {
5549         struct sctp_chunk *repl = NULL;
5550         struct sctp_bind_addr *bp;
5551         int attempts = asoc->init_err_counter + 1;
5552
5553         pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5554
5555         SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5556
5557         if (attempts <= asoc->max_init_attempts) {
5558                 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5559                 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5560                 if (!repl)
5561                         return SCTP_DISPOSITION_NOMEM;
5562
5563                 /* Choose transport for INIT. */
5564                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5565                                 SCTP_CHUNK(repl));
5566
5567                 /* Issue a sideeffect to do the needed accounting. */
5568                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5569                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5570
5571                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5572         } else {
5573                 pr_debug("%s: giving up on INIT, attempts:%d "
5574                          "max_init_attempts:%d\n", __func__, attempts,
5575                          asoc->max_init_attempts);
5576
5577                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5578                                 SCTP_ERROR(ETIMEDOUT));
5579                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5580                                 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5581                 return SCTP_DISPOSITION_DELETE_TCB;
5582         }
5583
5584         return SCTP_DISPOSITION_CONSUME;
5585 }
5586
5587 /*
5588  * sctp_sf_t1_cookie_timer_expire
5589  *
5590  * Section: 4 Note: 2
5591  * Verification Tag:
5592  * Inputs
5593  * (endpoint, asoc)
5594  *
5595  *  RFC 2960 Section 4 Notes
5596  *  3) If the T1-cookie timer expires, the endpoint MUST retransmit
5597  *     COOKIE ECHO and re-start the T1-cookie timer without changing
5598  *     state.  This MUST be repeated up to 'Max.Init.Retransmits' times.
5599  *     After that, the endpoint MUST abort the initialization process and
5600  *     report the error to SCTP user.
5601  *
5602  * Outputs
5603  * (timers, events)
5604  *
5605  */
5606 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5607                                            const struct sctp_endpoint *ep,
5608                                            const struct sctp_association *asoc,
5609                                            const sctp_subtype_t type,
5610                                            void *arg,
5611                                            sctp_cmd_seq_t *commands)
5612 {
5613         struct sctp_chunk *repl = NULL;
5614         int attempts = asoc->init_err_counter + 1;
5615
5616         pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5617
5618         SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5619
5620         if (attempts <= asoc->max_init_attempts) {
5621                 repl = sctp_make_cookie_echo(asoc, NULL);
5622                 if (!repl)
5623                         return SCTP_DISPOSITION_NOMEM;
5624
5625                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5626                                 SCTP_CHUNK(repl));
5627                 /* Issue a sideeffect to do the needed accounting. */
5628                 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5629                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5630
5631                 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5632         } else {
5633                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5634                                 SCTP_ERROR(ETIMEDOUT));
5635                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5636                                 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5637                 return SCTP_DISPOSITION_DELETE_TCB;
5638         }
5639
5640         return SCTP_DISPOSITION_CONSUME;
5641 }
5642
5643 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5644  * with the updated last sequential TSN received from its peer.
5645  *
5646  * An endpoint should limit the number of retransmissions of the
5647  * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5648  * If this threshold is exceeded the endpoint should destroy the TCB and
5649  * MUST report the peer endpoint unreachable to the upper layer (and
5650  * thus the association enters the CLOSED state).  The reception of any
5651  * packet from its peer (i.e. as the peer sends all of its queued DATA
5652  * chunks) should clear the endpoint's retransmission count and restart
5653  * the T2-Shutdown timer,  giving its peer ample opportunity to transmit
5654  * all of its queued DATA chunks that have not yet been sent.
5655  */
5656 sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5657                                            const struct sctp_endpoint *ep,
5658                                            const struct sctp_association *asoc,
5659                                            const sctp_subtype_t type,
5660                                            void *arg,
5661                                            sctp_cmd_seq_t *commands)
5662 {
5663         struct sctp_chunk *reply = NULL;
5664
5665         pr_debug("%s: timer T2 expired\n", __func__);
5666
5667         SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5668
5669         ((struct sctp_association *)asoc)->shutdown_retries++;
5670
5671         if (asoc->overall_error_count >= asoc->max_retrans) {
5672                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5673                                 SCTP_ERROR(ETIMEDOUT));
5674                 /* Note:  CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5675                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5676                                 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5677                 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5678                 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5679                 return SCTP_DISPOSITION_DELETE_TCB;
5680         }
5681
5682         switch (asoc->state) {
5683         case SCTP_STATE_SHUTDOWN_SENT:
5684                 reply = sctp_make_shutdown(asoc, NULL);
5685                 break;
5686
5687         case SCTP_STATE_SHUTDOWN_ACK_SENT:
5688                 reply = sctp_make_shutdown_ack(asoc, NULL);
5689                 break;
5690
5691         default:
5692                 BUG();
5693                 break;
5694         }
5695
5696         if (!reply)
5697                 goto nomem;
5698
5699         /* Do some failure management (Section 8.2).
5700          * If we remove the transport an SHUTDOWN was last sent to, don't
5701          * do failure management.
5702          */
5703         if (asoc->shutdown_last_sent_to)
5704                 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5705                                 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5706
5707         /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5708          * the T2-shutdown timer.
5709          */
5710         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5711
5712         /* Restart the T2-shutdown timer.  */
5713         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5714                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5715         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5716         return SCTP_DISPOSITION_CONSUME;
5717
5718 nomem:
5719         return SCTP_DISPOSITION_NOMEM;
5720 }
5721
5722 /*
5723  * ADDIP Section 4.1 ASCONF CHunk Procedures
5724  * If the T4 RTO timer expires the endpoint should do B1 to B5
5725  */
5726 sctp_disposition_t sctp_sf_t4_timer_expire(
5727         struct net *net,
5728         const struct sctp_endpoint *ep,
5729         const struct sctp_association *asoc,
5730         const sctp_subtype_t type,
5731         void *arg,
5732         sctp_cmd_seq_t *commands)
5733 {
5734         struct sctp_chunk *chunk = asoc->addip_last_asconf;
5735         struct sctp_transport *transport = chunk->transport;
5736
5737         SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5738
5739         /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5740          * detection on the appropriate destination address as defined in
5741          * RFC2960 [5] section 8.1 and 8.2.
5742          */
5743         if (transport)
5744                 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5745                                 SCTP_TRANSPORT(transport));
5746
5747         /* Reconfig T4 timer and transport. */
5748         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5749
5750         /* ADDIP 4.1 B2) Increment the association error counters and perform
5751          * endpoint failure detection on the association as defined in
5752          * RFC2960 [5] section 8.1 and 8.2.
5753          * association error counter is incremented in SCTP_CMD_STRIKE.
5754          */
5755         if (asoc->overall_error_count >= asoc->max_retrans) {
5756                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5757                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5758                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5759                                 SCTP_ERROR(ETIMEDOUT));
5760                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5761                                 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5762                 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5763                 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5764                 return SCTP_DISPOSITION_ABORT;
5765         }
5766
5767         /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5768          * the ASCONF chunk was sent by doubling the RTO timer value.
5769          * This is done in SCTP_CMD_STRIKE.
5770          */
5771
5772         /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5773          * choose an alternate destination address (please refer to RFC2960
5774          * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5775          * chunk, it MUST be the same (including its serial number) as the last
5776          * ASCONF sent.
5777          */
5778         sctp_chunk_hold(asoc->addip_last_asconf);
5779         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5780                         SCTP_CHUNK(asoc->addip_last_asconf));
5781
5782         /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5783          * destination is selected, then the RTO used will be that of the new
5784          * destination address.
5785          */
5786         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5787                         SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5788
5789         return SCTP_DISPOSITION_CONSUME;
5790 }
5791
5792 /* sctpimpguide-05 Section 2.12.2
5793  * The sender of the SHUTDOWN MAY also start an overall guard timer
5794  * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5795  * At the expiration of this timer the sender SHOULD abort the association
5796  * by sending an ABORT chunk.
5797  */
5798 sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5799                                            const struct sctp_endpoint *ep,
5800                                            const struct sctp_association *asoc,
5801                                            const sctp_subtype_t type,
5802                                            void *arg,
5803                                            sctp_cmd_seq_t *commands)
5804 {
5805         struct sctp_chunk *reply = NULL;
5806
5807         pr_debug("%s: timer T5 expired\n", __func__);
5808
5809         SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5810
5811         reply = sctp_make_abort(asoc, NULL, 0);
5812         if (!reply)
5813                 goto nomem;
5814
5815         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5816         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5817                         SCTP_ERROR(ETIMEDOUT));
5818         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5819                         SCTP_PERR(SCTP_ERROR_NO_ERROR));
5820
5821         SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5822         SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5823
5824         return SCTP_DISPOSITION_DELETE_TCB;
5825 nomem:
5826         return SCTP_DISPOSITION_NOMEM;
5827 }
5828
5829 /* Handle expiration of AUTOCLOSE timer.  When the autoclose timer expires,
5830  * the association is automatically closed by starting the shutdown process.
5831  * The work that needs to be done is same as when SHUTDOWN is initiated by
5832  * the user.  So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5833  */
5834 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5835         struct net *net,
5836         const struct sctp_endpoint *ep,
5837         const struct sctp_association *asoc,
5838         const sctp_subtype_t type,
5839         void *arg,
5840         sctp_cmd_seq_t *commands)
5841 {
5842         int disposition;
5843
5844         SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
5845
5846         /* From 9.2 Shutdown of an Association
5847          * Upon receipt of the SHUTDOWN primitive from its upper
5848          * layer, the endpoint enters SHUTDOWN-PENDING state and
5849          * remains there until all outstanding data has been
5850          * acknowledged by its peer. The endpoint accepts no new data
5851          * from its upper layer, but retransmits data to the far end
5852          * if necessary to fill gaps.
5853          */
5854         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5855                         SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5856
5857         disposition = SCTP_DISPOSITION_CONSUME;
5858         if (sctp_outq_is_empty(&asoc->outqueue)) {
5859                 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5860                                                             arg, commands);
5861         }
5862         return disposition;
5863 }
5864
5865 /*****************************************************************************
5866  * These are sa state functions which could apply to all types of events.
5867  ****************************************************************************/
5868
5869 /*
5870  * This table entry is not implemented.
5871  *
5872  * Inputs
5873  * (endpoint, asoc, chunk)
5874  *
5875  * The return value is the disposition of the chunk.
5876  */
5877 sctp_disposition_t sctp_sf_not_impl(struct net *net,
5878                                     const struct sctp_endpoint *ep,
5879                                     const struct sctp_association *asoc,
5880                                     const sctp_subtype_t type,
5881                                     void *arg,
5882                                     sctp_cmd_seq_t *commands)
5883 {
5884         return SCTP_DISPOSITION_NOT_IMPL;
5885 }
5886
5887 /*
5888  * This table entry represents a bug.
5889  *
5890  * Inputs
5891  * (endpoint, asoc, chunk)
5892  *
5893  * The return value is the disposition of the chunk.
5894  */
5895 sctp_disposition_t sctp_sf_bug(struct net *net,
5896                                const struct sctp_endpoint *ep,
5897                                const struct sctp_association *asoc,
5898                                const sctp_subtype_t type,
5899                                void *arg,
5900                                sctp_cmd_seq_t *commands)
5901 {
5902         return SCTP_DISPOSITION_BUG;
5903 }
5904
5905 /*
5906  * This table entry represents the firing of a timer in the wrong state.
5907  * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5908  * when the association is in the wrong state.   This event should
5909  * be ignored, so as to prevent any rearming of the timer.
5910  *
5911  * Inputs
5912  * (endpoint, asoc, chunk)
5913  *
5914  * The return value is the disposition of the chunk.
5915  */
5916 sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5917                                         const struct sctp_endpoint *ep,
5918                                         const struct sctp_association *asoc,
5919                                         const sctp_subtype_t type,
5920                                         void *arg,
5921                                         sctp_cmd_seq_t *commands)
5922 {
5923         pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5924
5925         return SCTP_DISPOSITION_CONSUME;
5926 }
5927
5928 /********************************************************************
5929  * 2nd Level Abstractions
5930  ********************************************************************/
5931
5932 /* Pull the SACK chunk based on the SACK header. */
5933 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5934 {
5935         struct sctp_sackhdr *sack;
5936         unsigned int len;
5937         __u16 num_blocks;
5938         __u16 num_dup_tsns;
5939
5940         /* Protect ourselves from reading too far into
5941          * the skb from a bogus sender.
5942          */
5943         sack = (struct sctp_sackhdr *) chunk->skb->data;
5944
5945         num_blocks = ntohs(sack->num_gap_ack_blocks);
5946         num_dup_tsns = ntohs(sack->num_dup_tsns);
5947         len = sizeof(struct sctp_sackhdr);
5948         len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5949         if (len > chunk->skb->len)
5950                 return NULL;
5951
5952         skb_pull(chunk->skb, len);
5953
5954         return sack;
5955 }
5956
5957 /* Create an ABORT packet to be sent as a response, with the specified
5958  * error causes.
5959  */
5960 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5961                                   const struct sctp_endpoint *ep,
5962                                   const struct sctp_association *asoc,
5963                                   struct sctp_chunk *chunk,
5964                                   const void *payload,
5965                                   size_t paylen)
5966 {
5967         struct sctp_packet *packet;
5968         struct sctp_chunk *abort;
5969
5970         packet = sctp_ootb_pkt_new(net, asoc, chunk);
5971
5972         if (packet) {
5973                 /* Make an ABORT.
5974                  * The T bit will be set if the asoc is NULL.
5975                  */
5976                 abort = sctp_make_abort(asoc, chunk, paylen);
5977                 if (!abort) {
5978                         sctp_ootb_pkt_free(packet);
5979                         return NULL;
5980                 }
5981
5982                 /* Reflect vtag if T-Bit is set */
5983                 if (sctp_test_T_bit(abort))
5984                         packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5985
5986                 /* Add specified error causes, i.e., payload, to the
5987                  * end of the chunk.
5988                  */
5989                 sctp_addto_chunk(abort, paylen, payload);
5990
5991                 /* Set the skb to the belonging sock for accounting.  */
5992                 abort->skb->sk = ep->base.sk;
5993
5994                 sctp_packet_append_chunk(packet, abort);
5995
5996         }
5997
5998         return packet;
5999 }
6000
6001 /* Allocate a packet for responding in the OOTB conditions.  */
6002 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
6003                                              const struct sctp_association *asoc,
6004                                              const struct sctp_chunk *chunk)
6005 {
6006         struct sctp_packet *packet;
6007         struct sctp_transport *transport;
6008         __u16 sport;
6009         __u16 dport;
6010         __u32 vtag;
6011
6012         /* Get the source and destination port from the inbound packet.  */
6013         sport = ntohs(chunk->sctp_hdr->dest);
6014         dport = ntohs(chunk->sctp_hdr->source);
6015
6016         /* The V-tag is going to be the same as the inbound packet if no
6017          * association exists, otherwise, use the peer's vtag.
6018          */
6019         if (asoc) {
6020                 /* Special case the INIT-ACK as there is no peer's vtag
6021                  * yet.
6022                  */
6023                 switch (chunk->chunk_hdr->type) {
6024                 case SCTP_CID_INIT:
6025                 case SCTP_CID_INIT_ACK:
6026                 {
6027                         sctp_initack_chunk_t *initack;
6028
6029                         initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6030                         vtag = ntohl(initack->init_hdr.init_tag);
6031                         break;
6032                 }
6033                 default:
6034                         vtag = asoc->peer.i.init_tag;
6035                         break;
6036                 }
6037         } else {
6038                 /* Special case the INIT and stale COOKIE_ECHO as there is no
6039                  * vtag yet.
6040                  */
6041                 switch (chunk->chunk_hdr->type) {
6042                 case SCTP_CID_INIT:
6043                 {
6044                         sctp_init_chunk_t *init;
6045
6046                         init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6047                         vtag = ntohl(init->init_hdr.init_tag);
6048                         break;
6049                 }
6050                 default:
6051                         vtag = ntohl(chunk->sctp_hdr->vtag);
6052                         break;
6053                 }
6054         }
6055
6056         /* Make a transport for the bucket, Eliza... */
6057         transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6058         if (!transport)
6059                 goto nomem;
6060
6061         /* Cache a route for the transport with the chunk's destination as
6062          * the source address.
6063          */
6064         sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6065                              sctp_sk(net->sctp.ctl_sock));
6066
6067         packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6068         packet = sctp_packet_config(packet, vtag, 0);
6069
6070         return packet;
6071
6072 nomem:
6073         return NULL;
6074 }
6075
6076 /* Free the packet allocated earlier for responding in the OOTB condition.  */
6077 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6078 {
6079         sctp_transport_free(packet->transport);
6080 }
6081
6082 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found  */
6083 static void sctp_send_stale_cookie_err(struct net *net,
6084                                        const struct sctp_endpoint *ep,
6085                                        const struct sctp_association *asoc,
6086                                        const struct sctp_chunk *chunk,
6087                                        sctp_cmd_seq_t *commands,
6088                                        struct sctp_chunk *err_chunk)
6089 {
6090         struct sctp_packet *packet;
6091
6092         if (err_chunk) {
6093                 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6094                 if (packet) {
6095                         struct sctp_signed_cookie *cookie;
6096
6097                         /* Override the OOTB vtag from the cookie. */
6098                         cookie = chunk->subh.cookie_hdr;
6099                         packet->vtag = cookie->c.peer_vtag;
6100
6101                         /* Set the skb to the belonging sock for accounting. */
6102                         err_chunk->skb->sk = ep->base.sk;
6103                         sctp_packet_append_chunk(packet, err_chunk);
6104                         sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6105                                         SCTP_PACKET(packet));
6106                         SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6107                 } else
6108                         sctp_chunk_free (err_chunk);
6109         }
6110 }
6111
6112
6113 /* Process a data chunk */
6114 static int sctp_eat_data(const struct sctp_association *asoc,
6115                          struct sctp_chunk *chunk,
6116                          sctp_cmd_seq_t *commands)
6117 {
6118         sctp_datahdr_t *data_hdr;
6119         struct sctp_chunk *err;
6120         size_t datalen;
6121         sctp_verb_t deliver;
6122         int tmp;
6123         __u32 tsn;
6124         struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6125         struct sock *sk = asoc->base.sk;
6126         struct net *net = sock_net(sk);
6127         u16 ssn;
6128         u16 sid;
6129         u8 ordered = 0;
6130
6131         data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6132         skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6133
6134         tsn = ntohl(data_hdr->tsn);
6135         pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6136
6137         /* ASSERT:  Now skb->data is really the user data.  */
6138
6139         /* Process ECN based congestion.
6140          *
6141          * Since the chunk structure is reused for all chunks within
6142          * a packet, we use ecn_ce_done to track if we've already
6143          * done CE processing for this packet.
6144          *
6145          * We need to do ECN processing even if we plan to discard the
6146          * chunk later.
6147          */
6148
6149         if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
6150                 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
6151                 chunk->ecn_ce_done = 1;
6152
6153                 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
6154                         /* Do real work as sideffect. */
6155                         sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6156                                         SCTP_U32(tsn));
6157                 }
6158         }
6159
6160         tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6161         if (tmp < 0) {
6162                 /* The TSN is too high--silently discard the chunk and
6163                  * count on it getting retransmitted later.
6164                  */
6165                 if (chunk->asoc)
6166                         chunk->asoc->stats.outofseqtsns++;
6167                 return SCTP_IERROR_HIGH_TSN;
6168         } else if (tmp > 0) {
6169                 /* This is a duplicate.  Record it.  */
6170                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6171                 return SCTP_IERROR_DUP_TSN;
6172         }
6173
6174         /* This is a new TSN.  */
6175
6176         /* Discard if there is no room in the receive window.
6177          * Actually, allow a little bit of overflow (up to a MTU).
6178          */
6179         datalen = ntohs(chunk->chunk_hdr->length);
6180         datalen -= sizeof(sctp_data_chunk_t);
6181
6182         deliver = SCTP_CMD_CHUNK_ULP;
6183
6184         /* Think about partial delivery. */
6185         if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6186
6187                 /* Even if we don't accept this chunk there is
6188                  * memory pressure.
6189                  */
6190                 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6191         }
6192
6193         /* Spill over rwnd a little bit.  Note: While allowed, this spill over
6194          * seems a bit troublesome in that frag_point varies based on
6195          * PMTU.  In cases, such as loopback, this might be a rather
6196          * large spill over.
6197          */
6198         if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6199             (datalen > asoc->rwnd + asoc->frag_point))) {
6200
6201                 /* If this is the next TSN, consider reneging to make
6202                  * room.   Note: Playing nice with a confused sender.  A
6203                  * malicious sender can still eat up all our buffer
6204                  * space and in the future we may want to detect and
6205                  * do more drastic reneging.
6206                  */
6207                 if (sctp_tsnmap_has_gap(map) &&
6208                     (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6209                         pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6210                         deliver = SCTP_CMD_RENEGE;
6211                 } else {
6212                         pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6213                                  __func__, tsn, datalen, asoc->rwnd);
6214
6215                         return SCTP_IERROR_IGNORE_TSN;
6216                 }
6217         }
6218
6219         /*
6220          * Also try to renege to limit our memory usage in the event that
6221          * we are under memory pressure
6222          * If we can't renege, don't worry about it, the sk_rmem_schedule
6223          * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6224          * memory usage too much
6225          */
6226         if (*sk->sk_prot_creator->memory_pressure) {
6227                 if (sctp_tsnmap_has_gap(map) &&
6228                     (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6229                         pr_debug("%s: under pressure, reneging for tsn:%u\n",
6230                                  __func__, tsn);
6231                         deliver = SCTP_CMD_RENEGE;
6232                  }
6233         }
6234
6235         /*
6236          * Section 3.3.10.9 No User Data (9)
6237          *
6238          * Cause of error
6239          * ---------------
6240          * No User Data:  This error cause is returned to the originator of a
6241          * DATA chunk if a received DATA chunk has no user data.
6242          */
6243         if (unlikely(0 == datalen)) {
6244                 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6245                 if (err) {
6246                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6247                                         SCTP_CHUNK(err));
6248                 }
6249                 /* We are going to ABORT, so we might as well stop
6250                  * processing the rest of the chunks in the packet.
6251                  */
6252                 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6253                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6254                                 SCTP_ERROR(ECONNABORTED));
6255                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6256                                 SCTP_PERR(SCTP_ERROR_NO_DATA));
6257                 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6258                 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6259                 return SCTP_IERROR_NO_DATA;
6260         }
6261
6262         chunk->data_accepted = 1;
6263
6264         /* Note: Some chunks may get overcounted (if we drop) or overcounted
6265          * if we renege and the chunk arrives again.
6266          */
6267         if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6268                 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6269                 if (chunk->asoc)
6270                         chunk->asoc->stats.iuodchunks++;
6271         } else {
6272                 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6273                 if (chunk->asoc)
6274                         chunk->asoc->stats.iodchunks++;
6275                 ordered = 1;
6276         }
6277
6278         /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6279          *
6280          * If an endpoint receive a DATA chunk with an invalid stream
6281          * identifier, it shall acknowledge the reception of the DATA chunk
6282          * following the normal procedure, immediately send an ERROR chunk
6283          * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6284          * and discard the DATA chunk.
6285          */
6286         sid = ntohs(data_hdr->stream);
6287         if (sid >= asoc->c.sinit_max_instreams) {
6288                 /* Mark tsn as received even though we drop it */
6289                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6290
6291                 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6292                                          &data_hdr->stream,
6293                                          sizeof(data_hdr->stream),
6294                                          sizeof(u16));
6295                 if (err)
6296                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6297                                         SCTP_CHUNK(err));
6298                 return SCTP_IERROR_BAD_STREAM;
6299         }
6300
6301         /* Check to see if the SSN is possible for this TSN.
6302          * The biggest gap we can record is 4K wide.  Since SSNs wrap
6303          * at an unsigned short, there is no way that an SSN can
6304          * wrap and for a valid TSN.  We can simply check if the current
6305          * SSN is smaller then the next expected one.  If it is, it wrapped
6306          * and is invalid.
6307          */
6308         ssn = ntohs(data_hdr->ssn);
6309         if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6310                 return SCTP_IERROR_PROTO_VIOLATION;
6311         }
6312
6313         /* Send the data up to the user.  Note:  Schedule  the
6314          * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6315          * chunk needs the updated rwnd.
6316          */
6317         sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6318
6319         return SCTP_IERROR_NO_ERROR;
6320 }