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