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