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