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