OSDN Git Service

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