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