[POWERPC] spufs: fix mismerge, making context signal{1,2} files readable again
[sfrench/cifs-2.6.git] / net / sctp / sm_statefuns.c
1 /* SCTP kernel reference 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 file is part of the SCTP kernel reference Implementation
9  *
10  * This is part of the SCTP Linux Kernel Reference Implementation.
11  *
12  * These are the state functions for the state machine.
13  *
14  * The SCTP reference implementation is free software;
15  * you can redistribute it and/or modify it under the terms of
16  * the GNU General Public License as published by
17  * the Free Software Foundation; either version 2, or (at your option)
18  * any later version.
19  *
20  * The SCTP reference implementation is distributed in the hope that it
21  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
22  *                 ************************
23  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24  * See the GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with GNU CC; see the file COPYING.  If not, write to
28  * the Free Software Foundation, 59 Temple Place - Suite 330,
29  * Boston, MA 02111-1307, USA.
30  *
31  * Please send any bug reports or fixes you make to the
32  * email address(es):
33  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
34  *
35  * Or submit a bug report through the following website:
36  *    http://www.sf.net/projects/lksctp
37  *
38  * Written or modified by:
39  *    La Monte H.P. Yarroll <piggy@acm.org>
40  *    Karl Knutson          <karl@athena.chicago.il.us>
41  *    Mathew Kotowsky       <kotowsky@sctp.org>
42  *    Sridhar Samudrala     <samudrala@us.ibm.com>
43  *    Jon Grimm             <jgrimm@us.ibm.com>
44  *    Hui Huang             <hui.huang@nokia.com>
45  *    Dajiang Zhang         <dajiang.zhang@nokia.com>
46  *    Daisy Chang           <daisyc@us.ibm.com>
47  *    Ardelle Fan           <ardelle.fan@intel.com>
48  *    Ryan Layer            <rmlayer@us.ibm.com>
49  *    Kevin Gao             <kevin.gao@intel.com>
50  *
51  * Any bugs reported given to us we will try to fix... any fixes shared will
52  * be incorporated into the next SCTP release.
53  */
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 <net/sock.h>
62 #include <net/inet_ecn.h>
63 #include <linux/skbuff.h>
64 #include <net/sctp/sctp.h>
65 #include <net/sctp/sm.h>
66 #include <net/sctp/structs.h>
67
68 static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
69                                   const struct sctp_association *asoc,
70                                   struct sctp_chunk *chunk,
71                                   const void *payload,
72                                   size_t paylen);
73 static int sctp_eat_data(const struct sctp_association *asoc,
74                          struct sctp_chunk *chunk,
75                          sctp_cmd_seq_t *commands);
76 static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
77                                              const struct sctp_chunk *chunk);
78 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
79                                        const struct sctp_association *asoc,
80                                        const struct sctp_chunk *chunk,
81                                        sctp_cmd_seq_t *commands,
82                                        struct sctp_chunk *err_chunk);
83 static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
84                                                  const struct sctp_association *asoc,
85                                                  const sctp_subtype_t type,
86                                                  void *arg,
87                                                  sctp_cmd_seq_t *commands);
88 static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
89                                              const struct sctp_association *asoc,
90                                              const sctp_subtype_t type,
91                                              void *arg,
92                                              sctp_cmd_seq_t *commands);
93 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
94
95 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
96                                            __be16 error, int sk_err,
97                                            const struct sctp_association *asoc,
98                                            struct sctp_transport *transport);
99
100 static sctp_disposition_t sctp_sf_abort_violation(
101                                      const struct sctp_association *asoc,
102                                      void *arg,
103                                      sctp_cmd_seq_t *commands,
104                                      const __u8 *payload,
105                                      const size_t paylen);
106
107 static sctp_disposition_t sctp_sf_violation_chunklen(
108                                      const struct sctp_endpoint *ep,
109                                      const struct sctp_association *asoc,
110                                      const sctp_subtype_t type,
111                                      void *arg,
112                                      sctp_cmd_seq_t *commands);
113
114 static sctp_disposition_t sctp_sf_violation_ctsn(
115                                      const struct sctp_endpoint *ep,
116                                      const struct sctp_association *asoc,
117                                      const sctp_subtype_t type,
118                                      void *arg,
119                                      sctp_cmd_seq_t *commands);
120
121 /* Small helper function that checks if the chunk length
122  * is of the appropriate length.  The 'required_length' argument
123  * is set to be the size of a specific chunk we are testing.
124  * Return Values:  1 = Valid length
125  *                 0 = Invalid length
126  *
127  */
128 static inline int
129 sctp_chunk_length_valid(struct sctp_chunk *chunk,
130                            __u16 required_length)
131 {
132         __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
133
134         if (unlikely(chunk_length < required_length))
135                 return 0;
136
137         return 1;
138 }
139
140 /**********************************************************
141  * These are the state functions for handling chunk events.
142  **********************************************************/
143
144 /*
145  * Process the final SHUTDOWN COMPLETE.
146  *
147  * Section: 4 (C) (diagram), 9.2
148  * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
149  * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
150  * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
151  * should stop the T2-shutdown timer and remove all knowledge of the
152  * association (and thus the association enters the CLOSED state).
153  *
154  * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
155  * C) Rules for packet carrying SHUTDOWN COMPLETE:
156  * ...
157  * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
158  *   if the Verification Tag field of the packet matches its own tag and
159  *   the T bit is not set
160  *   OR
161  *   it is set to its peer's tag and the T bit is set in the Chunk
162  *   Flags.
163  *   Otherwise, the receiver MUST silently discard the packet
164  *   and take no further action.  An endpoint MUST ignore the
165  *   SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
166  *
167  * Inputs
168  * (endpoint, asoc, chunk)
169  *
170  * Outputs
171  * (asoc, reply_msg, msg_up, timers, counters)
172  *
173  * The return value is the disposition of the chunk.
174  */
175 sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
176                                   const struct sctp_association *asoc,
177                                   const sctp_subtype_t type,
178                                   void *arg,
179                                   sctp_cmd_seq_t *commands)
180 {
181         struct sctp_chunk *chunk = arg;
182         struct sctp_ulpevent *ev;
183
184         /* RFC 2960 6.10 Bundling
185          *
186          * An endpoint MUST NOT bundle INIT, INIT ACK or
187          * SHUTDOWN COMPLETE with any other chunks.
188          */
189         if (!chunk->singleton)
190                 return SCTP_DISPOSITION_VIOLATION;
191
192         if (!sctp_vtag_verify_either(chunk, asoc))
193                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
194
195         /* RFC 2960 10.2 SCTP-to-ULP
196          *
197          * H) SHUTDOWN COMPLETE notification
198          *
199          * When SCTP completes the shutdown procedures (section 9.2) this
200          * notification is passed to the upper layer.
201          */
202         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
203                                              0, 0, 0, NULL, GFP_ATOMIC);
204         if (ev)
205                 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
206                                 SCTP_ULPEVENT(ev));
207
208         /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
209          * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
210          * not the chunk should be discarded. If the endpoint is in
211          * the SHUTDOWN-ACK-SENT state the endpoint should stop the
212          * T2-shutdown timer and remove all knowledge of the
213          * association (and thus the association enters the CLOSED
214          * state).
215          */
216         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
217                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
218
219         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
220                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
221
222         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
223                         SCTP_STATE(SCTP_STATE_CLOSED));
224
225         SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
226         SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
227
228         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
229
230         return SCTP_DISPOSITION_DELETE_TCB;
231 }
232
233 /*
234  * Respond to a normal INIT chunk.
235  * We are the side that is being asked for an association.
236  *
237  * Section: 5.1 Normal Establishment of an Association, B
238  * B) "Z" shall respond immediately with an INIT ACK chunk.  The
239  *    destination IP address of the INIT ACK MUST be set to the source
240  *    IP address of the INIT to which this INIT ACK is responding.  In
241  *    the response, besides filling in other parameters, "Z" must set the
242  *    Verification Tag field to Tag_A, and also provide its own
243  *    Verification Tag (Tag_Z) in the Initiate Tag field.
244  *
245  * Verification Tag: Must be 0.
246  *
247  * Inputs
248  * (endpoint, asoc, chunk)
249  *
250  * Outputs
251  * (asoc, reply_msg, msg_up, timers, counters)
252  *
253  * The return value is the disposition of the chunk.
254  */
255 sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
256                                         const struct sctp_association *asoc,
257                                         const sctp_subtype_t type,
258                                         void *arg,
259                                         sctp_cmd_seq_t *commands)
260 {
261         struct sctp_chunk *chunk = arg;
262         struct sctp_chunk *repl;
263         struct sctp_association *new_asoc;
264         struct sctp_chunk *err_chunk;
265         struct sctp_packet *packet;
266         sctp_unrecognized_param_t *unk_param;
267         int len;
268
269         /* 6.10 Bundling
270          * An endpoint MUST NOT bundle INIT, INIT ACK or
271          * SHUTDOWN COMPLETE with any other chunks.
272          *
273          * IG Section 2.11.2
274          * Furthermore, we require that the receiver of an INIT chunk MUST
275          * enforce these rules by silently discarding an arriving packet
276          * with an INIT chunk that is bundled with other chunks.
277          */
278         if (!chunk->singleton)
279                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
280
281         /* If the packet is an OOTB packet which is temporarily on the
282          * control endpoint, respond with an ABORT.
283          */
284         if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
285                 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
286
287         /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
288          * Tag.
289          */
290         if (chunk->sctp_hdr->vtag != 0)
291                 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
292
293         /* Make sure that the INIT chunk has a valid length.
294          * Normally, this would cause an ABORT with a Protocol Violation
295          * error, but since we don't have an association, we'll
296          * just discard the packet.
297          */
298         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
299                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
300
301         /* Verify the INIT chunk before processing it. */
302         err_chunk = NULL;
303         if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
304                               (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
305                               &err_chunk)) {
306                 /* This chunk contains fatal error. It is to be discarded.
307                  * Send an ABORT, with causes if there is any.
308                  */
309                 if (err_chunk) {
310                         packet = sctp_abort_pkt_new(ep, asoc, arg,
311                                         (__u8 *)(err_chunk->chunk_hdr) +
312                                         sizeof(sctp_chunkhdr_t),
313                                         ntohs(err_chunk->chunk_hdr->length) -
314                                         sizeof(sctp_chunkhdr_t));
315
316                         sctp_chunk_free(err_chunk);
317
318                         if (packet) {
319                                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
320                                                 SCTP_PACKET(packet));
321                                 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
322                                 return SCTP_DISPOSITION_CONSUME;
323                         } else {
324                                 return SCTP_DISPOSITION_NOMEM;
325                         }
326                 } else {
327                         return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
328                                                     commands);
329                 }
330         }
331
332         /* Grab the INIT header.  */
333         chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
334
335         /* Tag the variable length parameters.  */
336         chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
337
338         new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
339         if (!new_asoc)
340                 goto nomem;
341
342         /* The call, sctp_process_init(), can fail on memory allocation.  */
343         if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
344                                sctp_source(chunk),
345                                (sctp_init_chunk_t *)chunk->chunk_hdr,
346                                GFP_ATOMIC))
347                 goto nomem_init;
348
349         /* B) "Z" shall respond immediately with an INIT ACK chunk.  */
350
351         /* If there are errors need to be reported for unknown parameters,
352          * make sure to reserve enough room in the INIT ACK for them.
353          */
354         len = 0;
355         if (err_chunk)
356                 len = ntohs(err_chunk->chunk_hdr->length) -
357                         sizeof(sctp_chunkhdr_t);
358
359         if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
360                 goto nomem_init;
361
362         repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
363         if (!repl)
364                 goto nomem_init;
365
366         /* If there are errors need to be reported for unknown parameters,
367          * include them in the outgoing INIT ACK as "Unrecognized parameter"
368          * parameter.
369          */
370         if (err_chunk) {
371                 /* Get the "Unrecognized parameter" parameter(s) out of the
372                  * ERROR chunk generated by sctp_verify_init(). Since the
373                  * error cause code for "unknown parameter" and the
374                  * "Unrecognized parameter" type is the same, we can
375                  * construct the parameters in INIT ACK by copying the
376                  * ERROR causes over.
377                  */
378                 unk_param = (sctp_unrecognized_param_t *)
379                             ((__u8 *)(err_chunk->chunk_hdr) +
380                             sizeof(sctp_chunkhdr_t));
381                 /* Replace the cause code with the "Unrecognized parameter"
382                  * parameter type.
383                  */
384                 sctp_addto_chunk(repl, len, unk_param);
385                 sctp_chunk_free(err_chunk);
386         }
387
388         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
389
390         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
391
392         /*
393          * Note:  After sending out INIT ACK with the State Cookie parameter,
394          * "Z" MUST NOT allocate any resources, nor keep any states for the
395          * new association.  Otherwise, "Z" will be vulnerable to resource
396          * attacks.
397          */
398         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
399
400         return SCTP_DISPOSITION_DELETE_TCB;
401
402 nomem_init:
403         sctp_association_free(new_asoc);
404 nomem:
405         if (err_chunk)
406                 sctp_chunk_free(err_chunk);
407         return SCTP_DISPOSITION_NOMEM;
408 }
409
410 /*
411  * Respond to a normal INIT ACK chunk.
412  * We are the side that is initiating the association.
413  *
414  * Section: 5.1 Normal Establishment of an Association, C
415  * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
416  *    timer and leave COOKIE-WAIT state. "A" shall then send the State
417  *    Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
418  *    the T1-cookie timer, and enter the COOKIE-ECHOED state.
419  *
420  *    Note: The COOKIE ECHO chunk can be bundled with any pending outbound
421  *    DATA chunks, but it MUST be the first chunk in the packet and
422  *    until the COOKIE ACK is returned the sender MUST NOT send any
423  *    other packets to the peer.
424  *
425  * Verification Tag: 3.3.3
426  *   If the value of the Initiate Tag in a received INIT ACK chunk is
427  *   found to be 0, the receiver MUST treat it as an error and close the
428  *   association by transmitting an ABORT.
429  *
430  * Inputs
431  * (endpoint, asoc, chunk)
432  *
433  * Outputs
434  * (asoc, reply_msg, msg_up, timers, counters)
435  *
436  * The return value is the disposition of the chunk.
437  */
438 sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
439                                        const struct sctp_association *asoc,
440                                        const sctp_subtype_t type,
441                                        void *arg,
442                                        sctp_cmd_seq_t *commands)
443 {
444         struct sctp_chunk *chunk = arg;
445         sctp_init_chunk_t *initchunk;
446         struct sctp_chunk *err_chunk;
447         struct sctp_packet *packet;
448         sctp_error_t error;
449
450         if (!sctp_vtag_verify(chunk, asoc))
451                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
452
453         /* Make sure that the INIT-ACK chunk has a valid length */
454         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
455                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
456                                                   commands);
457         /* 6.10 Bundling
458          * An endpoint MUST NOT bundle INIT, INIT ACK or
459          * SHUTDOWN COMPLETE with any other chunks.
460          */
461         if (!chunk->singleton)
462                 return SCTP_DISPOSITION_VIOLATION;
463
464         /* Grab the INIT header.  */
465         chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
466
467         /* Verify the INIT chunk before processing it. */
468         err_chunk = NULL;
469         if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
470                               (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
471                               &err_chunk)) {
472
473                 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
474
475                 /* This chunk contains fatal error. It is to be discarded.
476                  * Send an ABORT, with causes if there is any.
477                  */
478                 if (err_chunk) {
479                         packet = sctp_abort_pkt_new(ep, asoc, arg,
480                                         (__u8 *)(err_chunk->chunk_hdr) +
481                                         sizeof(sctp_chunkhdr_t),
482                                         ntohs(err_chunk->chunk_hdr->length) -
483                                         sizeof(sctp_chunkhdr_t));
484
485                         sctp_chunk_free(err_chunk);
486
487                         if (packet) {
488                                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
489                                                 SCTP_PACKET(packet));
490                                 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
491                                 error = SCTP_ERROR_INV_PARAM;
492                         } else {
493                                 error = SCTP_ERROR_NO_RESOURCE;
494                         }
495                 } else {
496                         sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
497                         error = SCTP_ERROR_INV_PARAM;
498                 }
499                 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
500                                                 asoc, chunk->transport);
501         }
502
503         /* Tag the variable length parameters.  Note that we never
504          * convert the parameters in an INIT chunk.
505          */
506         chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
507
508         initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
509
510         sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
511                         SCTP_PEER_INIT(initchunk));
512
513         /* Reset init error count upon receipt of INIT-ACK.  */
514         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
515
516         /* 5.1 C) "A" shall stop the T1-init timer and leave
517          * COOKIE-WAIT state.  "A" shall then ... start the T1-cookie
518          * timer, and enter the COOKIE-ECHOED state.
519          */
520         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
521                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
522         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
523                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
524         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
525                         SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
526
527         /* 5.1 C) "A" shall then send the State Cookie received in the
528          * INIT ACK chunk in a COOKIE ECHO chunk, ...
529          */
530         /* If there is any errors to report, send the ERROR chunk generated
531          * for unknown parameters as well.
532          */
533         sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
534                         SCTP_CHUNK(err_chunk));
535
536         return SCTP_DISPOSITION_CONSUME;
537 }
538
539 /*
540  * Respond to a normal COOKIE ECHO chunk.
541  * We are the side that is being asked for an association.
542  *
543  * Section: 5.1 Normal Establishment of an Association, D
544  * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
545  *    with a COOKIE ACK chunk after building a TCB and moving to
546  *    the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
547  *    any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
548  *    chunk MUST be the first chunk in the packet.
549  *
550  *   IMPLEMENTATION NOTE: An implementation may choose to send the
551  *   Communication Up notification to the SCTP user upon reception
552  *   of a valid COOKIE ECHO chunk.
553  *
554  * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
555  * D) Rules for packet carrying a COOKIE ECHO
556  *
557  * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
558  *   Initial Tag received in the INIT ACK.
559  *
560  * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
561  *
562  * Inputs
563  * (endpoint, asoc, chunk)
564  *
565  * Outputs
566  * (asoc, reply_msg, msg_up, timers, counters)
567  *
568  * The return value is the disposition of the chunk.
569  */
570 sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
571                                       const struct sctp_association *asoc,
572                                       const sctp_subtype_t type, void *arg,
573                                       sctp_cmd_seq_t *commands)
574 {
575         struct sctp_chunk *chunk = arg;
576         struct sctp_association *new_asoc;
577         sctp_init_chunk_t *peer_init;
578         struct sctp_chunk *repl;
579         struct sctp_ulpevent *ev, *ai_ev = NULL;
580         int error = 0;
581         struct sctp_chunk *err_chk_p;
582         struct sock *sk;
583
584         /* If the packet is an OOTB packet which is temporarily on the
585          * control endpoint, respond with an ABORT.
586          */
587         if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
588                 return sctp_sf_ootb(ep, asoc, type, arg, commands);
589
590         /* Make sure that the COOKIE_ECHO chunk has a valid length.
591          * In this case, we check that we have enough for at least a
592          * chunk header.  More detailed verification is done
593          * in sctp_unpack_cookie().
594          */
595         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
596                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
597
598         /* If the endpoint is not listening or if the number of associations
599          * on the TCP-style socket exceed the max backlog, respond with an
600          * ABORT.
601          */
602         sk = ep->base.sk;
603         if (!sctp_sstate(sk, LISTENING) ||
604             (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
605                 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
606
607         /* "Decode" the chunk.  We have no optional parameters so we
608          * are in good shape.
609          */
610         chunk->subh.cookie_hdr =
611                 (struct sctp_signed_cookie *)chunk->skb->data;
612         if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
613                                          sizeof(sctp_chunkhdr_t)))
614                 goto nomem;
615
616         /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
617          * "Z" will reply with a COOKIE ACK chunk after building a TCB
618          * and moving to the ESTABLISHED state.
619          */
620         new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
621                                       &err_chk_p);
622
623         /* FIXME:
624          * If the re-build failed, what is the proper error path
625          * from here?
626          *
627          * [We should abort the association. --piggy]
628          */
629         if (!new_asoc) {
630                 /* FIXME: Several errors are possible.  A bad cookie should
631                  * be silently discarded, but think about logging it too.
632                  */
633                 switch (error) {
634                 case -SCTP_IERROR_NOMEM:
635                         goto nomem;
636
637                 case -SCTP_IERROR_STALE_COOKIE:
638                         sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
639                                                    err_chk_p);
640                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
641
642                 case -SCTP_IERROR_BAD_SIG:
643                 default:
644                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
645                 }
646         }
647
648
649         /* Delay state machine commands until later.
650          *
651          * Re-build the bind address for the association is done in
652          * the sctp_unpack_cookie() already.
653          */
654         /* This is a brand-new association, so these are not yet side
655          * effects--it is safe to run them here.
656          */
657         peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
658
659         if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
660                                &chunk->subh.cookie_hdr->c.peer_addr,
661                                peer_init, GFP_ATOMIC))
662                 goto nomem_init;
663
664         repl = sctp_make_cookie_ack(new_asoc, chunk);
665         if (!repl)
666                 goto nomem_init;
667
668         /* RFC 2960 5.1 Normal Establishment of an Association
669          *
670          * D) IMPLEMENTATION NOTE: An implementation may choose to
671          * send the Communication Up notification to the SCTP user
672          * upon reception of a valid COOKIE ECHO chunk.
673          */
674         ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
675                                              new_asoc->c.sinit_num_ostreams,
676                                              new_asoc->c.sinit_max_instreams,
677                                              NULL, GFP_ATOMIC);
678         if (!ev)
679                 goto nomem_ev;
680
681         /* Sockets API Draft Section 5.3.1.6
682          * When a peer sends a Adaptation Layer Indication parameter , SCTP
683          * delivers this notification to inform the application that of the
684          * peers requested adaptation layer.
685          */
686         if (new_asoc->peer.adaptation_ind) {
687                 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
688                                                             GFP_ATOMIC);
689                 if (!ai_ev)
690                         goto nomem_aiev;
691         }
692
693         /* Add all the state machine commands now since we've created
694          * everything.  This way we don't introduce memory corruptions
695          * during side-effect processing and correclty count established
696          * associations.
697          */
698         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
699         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
700                         SCTP_STATE(SCTP_STATE_ESTABLISHED));
701         SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
702         SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
703         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
704
705         if (new_asoc->autoclose)
706                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
707                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
708
709         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
710
711         /* This will send the COOKIE ACK */
712         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
713
714         /* Queue the ASSOC_CHANGE event */
715         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
716
717         /* Send up the Adaptation Layer Indication event */
718         if (ai_ev)
719                 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
720                                 SCTP_ULPEVENT(ai_ev));
721
722         return SCTP_DISPOSITION_CONSUME;
723
724 nomem_aiev:
725         sctp_ulpevent_free(ev);
726 nomem_ev:
727         sctp_chunk_free(repl);
728 nomem_init:
729         sctp_association_free(new_asoc);
730 nomem:
731         return SCTP_DISPOSITION_NOMEM;
732 }
733
734 /*
735  * Respond to a normal COOKIE ACK chunk.
736  * We are the side that is being asked for an association.
737  *
738  * RFC 2960 5.1 Normal Establishment of an Association
739  *
740  * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
741  *    COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
742  *    timer. It may also notify its ULP about the successful
743  *    establishment of the association with a Communication Up
744  *    notification (see Section 10).
745  *
746  * Verification Tag:
747  * Inputs
748  * (endpoint, asoc, chunk)
749  *
750  * Outputs
751  * (asoc, reply_msg, msg_up, timers, counters)
752  *
753  * The return value is the disposition of the chunk.
754  */
755 sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
756                                       const struct sctp_association *asoc,
757                                       const sctp_subtype_t type, void *arg,
758                                       sctp_cmd_seq_t *commands)
759 {
760         struct sctp_chunk *chunk = arg;
761         struct sctp_ulpevent *ev;
762
763         if (!sctp_vtag_verify(chunk, asoc))
764                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
765
766         /* Verify that the chunk length for the COOKIE-ACK is OK.
767          * If we don't do this, any bundled chunks may be junked.
768          */
769         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
770                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
771                                                   commands);
772
773         /* Reset init error count upon receipt of COOKIE-ACK,
774          * to avoid problems with the managemement of this
775          * counter in stale cookie situations when a transition back
776          * from the COOKIE-ECHOED state to the COOKIE-WAIT
777          * state is performed.
778          */
779         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
780
781         /* RFC 2960 5.1 Normal Establishment of an Association
782          *
783          * E) Upon reception of the COOKIE ACK, endpoint "A" will move
784          * from the COOKIE-ECHOED state to the ESTABLISHED state,
785          * stopping the T1-cookie timer.
786          */
787         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
788                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
789         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
790                         SCTP_STATE(SCTP_STATE_ESTABLISHED));
791         SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
792         SCTP_INC_STATS(SCTP_MIB_ACTIVEESTABS);
793         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
794         if (asoc->autoclose)
795                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
796                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
797         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
798
799         /* It may also notify its ULP about the successful
800          * establishment of the association with a Communication Up
801          * notification (see Section 10).
802          */
803         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
804                                              0, asoc->c.sinit_num_ostreams,
805                                              asoc->c.sinit_max_instreams,
806                                              NULL, GFP_ATOMIC);
807
808         if (!ev)
809                 goto nomem;
810
811         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
812
813         /* Sockets API Draft Section 5.3.1.6
814          * When a peer sends a Adaptation Layer Indication parameter , SCTP
815          * delivers this notification to inform the application that of the
816          * peers requested adaptation layer.
817          */
818         if (asoc->peer.adaptation_ind) {
819                 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
820                 if (!ev)
821                         goto nomem;
822
823                 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
824                                 SCTP_ULPEVENT(ev));
825         }
826
827         return SCTP_DISPOSITION_CONSUME;
828 nomem:
829         return SCTP_DISPOSITION_NOMEM;
830 }
831
832 /* Generate and sendout a heartbeat packet.  */
833 static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
834                                             const struct sctp_association *asoc,
835                                             const sctp_subtype_t type,
836                                             void *arg,
837                                             sctp_cmd_seq_t *commands)
838 {
839         struct sctp_transport *transport = (struct sctp_transport *) arg;
840         struct sctp_chunk *reply;
841         sctp_sender_hb_info_t hbinfo;
842         size_t paylen = 0;
843
844         hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
845         hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
846         hbinfo.daddr = transport->ipaddr;
847         hbinfo.sent_at = jiffies;
848         hbinfo.hb_nonce = transport->hb_nonce;
849
850         /* Send a heartbeat to our peer.  */
851         paylen = sizeof(sctp_sender_hb_info_t);
852         reply = sctp_make_heartbeat(asoc, transport, &hbinfo, paylen);
853         if (!reply)
854                 return SCTP_DISPOSITION_NOMEM;
855
856         /* Set rto_pending indicating that an RTT measurement
857          * is started with this heartbeat chunk.
858          */
859         sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
860                         SCTP_TRANSPORT(transport));
861
862         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
863         return SCTP_DISPOSITION_CONSUME;
864 }
865
866 /* Generate a HEARTBEAT packet on the given transport.  */
867 sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
868                                         const struct sctp_association *asoc,
869                                         const sctp_subtype_t type,
870                                         void *arg,
871                                         sctp_cmd_seq_t *commands)
872 {
873         struct sctp_transport *transport = (struct sctp_transport *) arg;
874
875         if (asoc->overall_error_count >= asoc->max_retrans) {
876                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
877                                 SCTP_ERROR(ETIMEDOUT));
878                 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
879                 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
880                                 SCTP_PERR(SCTP_ERROR_NO_ERROR));
881                 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
882                 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
883                 return SCTP_DISPOSITION_DELETE_TCB;
884         }
885
886         /* Section 3.3.5.
887          * The Sender-specific Heartbeat Info field should normally include
888          * information about the sender's current time when this HEARTBEAT
889          * chunk is sent and the destination transport address to which this
890          * HEARTBEAT is sent (see Section 8.3).
891          */
892
893         if (transport->param_flags & SPP_HB_ENABLE) {
894                 if (SCTP_DISPOSITION_NOMEM ==
895                                 sctp_sf_heartbeat(ep, asoc, type, arg,
896                                                   commands))
897                         return SCTP_DISPOSITION_NOMEM;
898                 /* Set transport error counter and association error counter
899                  * when sending heartbeat.
900                  */
901                 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_RESET,
902                                 SCTP_TRANSPORT(transport));
903         }
904         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
905                         SCTP_TRANSPORT(transport));
906
907         return SCTP_DISPOSITION_CONSUME;
908 }
909
910 /*
911  * Process an heartbeat request.
912  *
913  * Section: 8.3 Path Heartbeat
914  * The receiver of the HEARTBEAT should immediately respond with a
915  * HEARTBEAT ACK that contains the Heartbeat Information field copied
916  * from the received HEARTBEAT chunk.
917  *
918  * Verification Tag:  8.5 Verification Tag [Normal verification]
919  * When receiving an SCTP packet, the endpoint MUST ensure that the
920  * value in the Verification Tag field of the received SCTP packet
921  * matches its own Tag. If the received Verification Tag value does not
922  * match the receiver's own tag value, the receiver shall silently
923  * discard the packet and shall not process it any further except for
924  * those cases listed in Section 8.5.1 below.
925  *
926  * Inputs
927  * (endpoint, asoc, chunk)
928  *
929  * Outputs
930  * (asoc, reply_msg, msg_up, timers, counters)
931  *
932  * The return value is the disposition of the chunk.
933  */
934 sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
935                                     const struct sctp_association *asoc,
936                                     const sctp_subtype_t type,
937                                     void *arg,
938                                     sctp_cmd_seq_t *commands)
939 {
940         struct sctp_chunk *chunk = arg;
941         struct sctp_chunk *reply;
942         size_t paylen = 0;
943
944         if (!sctp_vtag_verify(chunk, asoc))
945                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
946
947         /* Make sure that the HEARTBEAT chunk has a valid length. */
948         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
949                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
950                                                   commands);
951
952         /* 8.3 The receiver of the HEARTBEAT should immediately
953          * respond with a HEARTBEAT ACK that contains the Heartbeat
954          * Information field copied from the received HEARTBEAT chunk.
955          */
956         chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
957         paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
958         if (!pskb_pull(chunk->skb, paylen))
959                 goto nomem;
960
961         reply = sctp_make_heartbeat_ack(asoc, chunk,
962                                         chunk->subh.hb_hdr, paylen);
963         if (!reply)
964                 goto nomem;
965
966         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
967         return SCTP_DISPOSITION_CONSUME;
968
969 nomem:
970         return SCTP_DISPOSITION_NOMEM;
971 }
972
973 /*
974  * Process the returning HEARTBEAT ACK.
975  *
976  * Section: 8.3 Path Heartbeat
977  * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
978  * should clear the error counter of the destination transport
979  * address to which the HEARTBEAT was sent, and mark the destination
980  * transport address as active if it is not so marked. The endpoint may
981  * optionally report to the upper layer when an inactive destination
982  * address is marked as active due to the reception of the latest
983  * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
984  * clear the association overall error count as well (as defined
985  * in section 8.1).
986  *
987  * The receiver of the HEARTBEAT ACK should also perform an RTT
988  * measurement for that destination transport address using the time
989  * value carried in the HEARTBEAT ACK chunk.
990  *
991  * Verification Tag:  8.5 Verification Tag [Normal verification]
992  *
993  * Inputs
994  * (endpoint, asoc, chunk)
995  *
996  * Outputs
997  * (asoc, reply_msg, msg_up, timers, counters)
998  *
999  * The return value is the disposition of the chunk.
1000  */
1001 sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
1002                                         const struct sctp_association *asoc,
1003                                         const sctp_subtype_t type,
1004                                         void *arg,
1005                                         sctp_cmd_seq_t *commands)
1006 {
1007         struct sctp_chunk *chunk = arg;
1008         union sctp_addr from_addr;
1009         struct sctp_transport *link;
1010         sctp_sender_hb_info_t *hbinfo;
1011         unsigned long max_interval;
1012
1013         if (!sctp_vtag_verify(chunk, asoc))
1014                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1015
1016         /* Make sure that the HEARTBEAT-ACK chunk has a valid length.  */
1017         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1018                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1019                                                   commands);
1020
1021         hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1022         /* Make sure that the length of the parameter is what we expect */
1023         if (ntohs(hbinfo->param_hdr.length) !=
1024                                     sizeof(sctp_sender_hb_info_t)) {
1025                 return SCTP_DISPOSITION_DISCARD;
1026         }
1027
1028         from_addr = hbinfo->daddr;
1029         link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1030
1031         /* This should never happen, but lets log it if so.  */
1032         if (unlikely(!link)) {
1033                 if (from_addr.sa.sa_family == AF_INET6) {
1034                         if (net_ratelimit())
1035                                 printk(KERN_WARNING
1036                                     "%s association %p could not find address "
1037                                     NIP6_FMT "\n",
1038                                     __FUNCTION__,
1039                                     asoc,
1040                                     NIP6(from_addr.v6.sin6_addr));
1041                 } else {
1042                         if (net_ratelimit())
1043                                 printk(KERN_WARNING
1044                                     "%s association %p could not find address "
1045                                     NIPQUAD_FMT "\n",
1046                                     __FUNCTION__,
1047                                     asoc,
1048                                     NIPQUAD(from_addr.v4.sin_addr.s_addr));
1049                 }
1050                 return SCTP_DISPOSITION_DISCARD;
1051         }
1052
1053         /* Validate the 64-bit random nonce. */
1054         if (hbinfo->hb_nonce != link->hb_nonce)
1055                 return SCTP_DISPOSITION_DISCARD;
1056
1057         max_interval = link->hbinterval + link->rto;
1058
1059         /* Check if the timestamp looks valid.  */
1060         if (time_after(hbinfo->sent_at, jiffies) ||
1061             time_after(jiffies, hbinfo->sent_at + max_interval)) {
1062                 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp"
1063                                   "received for transport: %p\n",
1064                                    __FUNCTION__, link);
1065                 return SCTP_DISPOSITION_DISCARD;
1066         }
1067
1068         /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1069          * the HEARTBEAT should clear the error counter of the
1070          * destination transport address to which the HEARTBEAT was
1071          * sent and mark the destination transport address as active if
1072          * it is not so marked.
1073          */
1074         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1075
1076         return SCTP_DISPOSITION_CONSUME;
1077 }
1078
1079 /* Helper function to send out an abort for the restart
1080  * condition.
1081  */
1082 static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1083                                       struct sctp_chunk *init,
1084                                       sctp_cmd_seq_t *commands)
1085 {
1086         int len;
1087         struct sctp_packet *pkt;
1088         union sctp_addr_param *addrparm;
1089         struct sctp_errhdr *errhdr;
1090         struct sctp_endpoint *ep;
1091         char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1092         struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1093
1094         /* Build the error on the stack.   We are way to malloc crazy
1095          * throughout the code today.
1096          */
1097         errhdr = (struct sctp_errhdr *)buffer;
1098         addrparm = (union sctp_addr_param *)errhdr->variable;
1099
1100         /* Copy into a parm format. */
1101         len = af->to_addr_param(ssa, addrparm);
1102         len += sizeof(sctp_errhdr_t);
1103
1104         errhdr->cause = SCTP_ERROR_RESTART;
1105         errhdr->length = htons(len);
1106
1107         /* Assign to the control socket. */
1108         ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1109
1110         /* Association is NULL since this may be a restart attack and we
1111          * want to send back the attacker's vtag.
1112          */
1113         pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1114
1115         if (!pkt)
1116                 goto out;
1117         sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1118
1119         SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1120
1121         /* Discard the rest of the inbound packet. */
1122         sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1123
1124 out:
1125         /* Even if there is no memory, treat as a failure so
1126          * the packet will get dropped.
1127          */
1128         return 0;
1129 }
1130
1131 /* A restart is occurring, check to make sure no new addresses
1132  * are being added as we may be under a takeover attack.
1133  */
1134 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1135                                        const struct sctp_association *asoc,
1136                                        struct sctp_chunk *init,
1137                                        sctp_cmd_seq_t *commands)
1138 {
1139         struct sctp_transport *new_addr, *addr;
1140         struct list_head *pos, *pos2;
1141         int found;
1142
1143         /* Implementor's Guide - Sectin 5.2.2
1144          * ...
1145          * Before responding the endpoint MUST check to see if the
1146          * unexpected INIT adds new addresses to the association. If new
1147          * addresses are added to the association, the endpoint MUST respond
1148          * with an ABORT..
1149          */
1150
1151         /* Search through all current addresses and make sure
1152          * we aren't adding any new ones.
1153          */
1154         new_addr = NULL;
1155         found = 0;
1156
1157         list_for_each(pos, &new_asoc->peer.transport_addr_list) {
1158                 new_addr = list_entry(pos, struct sctp_transport, transports);
1159                 found = 0;
1160                 list_for_each(pos2, &asoc->peer.transport_addr_list) {
1161                         addr = list_entry(pos2, struct sctp_transport,
1162                                           transports);
1163                         if (sctp_cmp_addr_exact(&new_addr->ipaddr,
1164                                                 &addr->ipaddr)) {
1165                                 found = 1;
1166                                 break;
1167                         }
1168                 }
1169                 if (!found)
1170                         break;
1171         }
1172
1173         /* If a new address was added, ABORT the sender. */
1174         if (!found && new_addr) {
1175                 sctp_sf_send_restart_abort(&new_addr->ipaddr, init, commands);
1176         }
1177
1178         /* Return success if all addresses were found. */
1179         return found;
1180 }
1181
1182 /* Populate the verification/tie tags based on overlapping INIT
1183  * scenario.
1184  *
1185  * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1186  */
1187 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1188                                   const struct sctp_association *asoc)
1189 {
1190         switch (asoc->state) {
1191
1192         /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1193
1194         case SCTP_STATE_COOKIE_WAIT:
1195                 new_asoc->c.my_vtag     = asoc->c.my_vtag;
1196                 new_asoc->c.my_ttag     = asoc->c.my_vtag;
1197                 new_asoc->c.peer_ttag   = 0;
1198                 break;
1199
1200         case SCTP_STATE_COOKIE_ECHOED:
1201                 new_asoc->c.my_vtag     = asoc->c.my_vtag;
1202                 new_asoc->c.my_ttag     = asoc->c.my_vtag;
1203                 new_asoc->c.peer_ttag   = asoc->c.peer_vtag;
1204                 break;
1205
1206         /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1207          * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1208          */
1209         default:
1210                 new_asoc->c.my_ttag   = asoc->c.my_vtag;
1211                 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1212                 break;
1213         }
1214
1215         /* Other parameters for the endpoint SHOULD be copied from the
1216          * existing parameters of the association (e.g. number of
1217          * outbound streams) into the INIT ACK and cookie.
1218          */
1219         new_asoc->rwnd                  = asoc->rwnd;
1220         new_asoc->c.sinit_num_ostreams  = asoc->c.sinit_num_ostreams;
1221         new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1222         new_asoc->c.initial_tsn         = asoc->c.initial_tsn;
1223 }
1224
1225 /*
1226  * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1227  * handling action.
1228  *
1229  * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1230  *
1231  * Returns value representing action to be taken.   These action values
1232  * correspond to Action/Description values in RFC 2960, Table 2.
1233  */
1234 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1235                                  const struct sctp_association *asoc)
1236 {
1237         /* In this case, the peer may have restarted.  */
1238         if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1239             (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1240             (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1241             (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1242                 return 'A';
1243
1244         /* Collision case B. */
1245         if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1246             ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1247              (0 == asoc->c.peer_vtag))) {
1248                 return 'B';
1249         }
1250
1251         /* Collision case D. */
1252         if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1253             (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1254                 return 'D';
1255
1256         /* Collision case C. */
1257         if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1258             (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1259             (0 == new_asoc->c.my_ttag) &&
1260             (0 == new_asoc->c.peer_ttag))
1261                 return 'C';
1262
1263         /* No match to any of the special cases; discard this packet. */
1264         return 'E';
1265 }
1266
1267 /* Common helper routine for both duplicate and simulataneous INIT
1268  * chunk handling.
1269  */
1270 static sctp_disposition_t sctp_sf_do_unexpected_init(
1271         const struct sctp_endpoint *ep,
1272         const struct sctp_association *asoc,
1273         const sctp_subtype_t type,
1274         void *arg, sctp_cmd_seq_t *commands)
1275 {
1276         sctp_disposition_t retval;
1277         struct sctp_chunk *chunk = arg;
1278         struct sctp_chunk *repl;
1279         struct sctp_association *new_asoc;
1280         struct sctp_chunk *err_chunk;
1281         struct sctp_packet *packet;
1282         sctp_unrecognized_param_t *unk_param;
1283         int len;
1284
1285         /* 6.10 Bundling
1286          * An endpoint MUST NOT bundle INIT, INIT ACK or
1287          * SHUTDOWN COMPLETE with any other chunks.
1288          *
1289          * IG Section 2.11.2
1290          * Furthermore, we require that the receiver of an INIT chunk MUST
1291          * enforce these rules by silently discarding an arriving packet
1292          * with an INIT chunk that is bundled with other chunks.
1293          */
1294         if (!chunk->singleton)
1295                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1296
1297         /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1298          * Tag.
1299          */
1300         if (chunk->sctp_hdr->vtag != 0)
1301                 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1302
1303         /* Make sure that the INIT chunk has a valid length.
1304          * In this case, we generate a protocol violation since we have
1305          * an association established.
1306          */
1307         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1308                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1309                                                   commands);
1310         /* Grab the INIT header.  */
1311         chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1312
1313         /* Tag the variable length parameters.  */
1314         chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1315
1316         /* Verify the INIT chunk before processing it. */
1317         err_chunk = NULL;
1318         if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1319                               (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1320                               &err_chunk)) {
1321                 /* This chunk contains fatal error. It is to be discarded.
1322                  * Send an ABORT, with causes if there is any.
1323                  */
1324                 if (err_chunk) {
1325                         packet = sctp_abort_pkt_new(ep, asoc, arg,
1326                                         (__u8 *)(err_chunk->chunk_hdr) +
1327                                         sizeof(sctp_chunkhdr_t),
1328                                         ntohs(err_chunk->chunk_hdr->length) -
1329                                         sizeof(sctp_chunkhdr_t));
1330
1331                         if (packet) {
1332                                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1333                                                 SCTP_PACKET(packet));
1334                                 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1335                                 retval = SCTP_DISPOSITION_CONSUME;
1336                         } else {
1337                                 retval = SCTP_DISPOSITION_NOMEM;
1338                         }
1339                         goto cleanup;
1340                 } else {
1341                         return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1342                                                     commands);
1343                 }
1344         }
1345
1346         /*
1347          * Other parameters for the endpoint SHOULD be copied from the
1348          * existing parameters of the association (e.g. number of
1349          * outbound streams) into the INIT ACK and cookie.
1350          * FIXME:  We are copying parameters from the endpoint not the
1351          * association.
1352          */
1353         new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1354         if (!new_asoc)
1355                 goto nomem;
1356
1357         /* In the outbound INIT ACK the endpoint MUST copy its current
1358          * Verification Tag and Peers Verification tag into a reserved
1359          * place (local tie-tag and per tie-tag) within the state cookie.
1360          */
1361         if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1362                                sctp_source(chunk),
1363                                (sctp_init_chunk_t *)chunk->chunk_hdr,
1364                                GFP_ATOMIC))
1365                 goto nomem;
1366
1367         /* Make sure no new addresses are being added during the
1368          * restart.   Do not do this check for COOKIE-WAIT state,
1369          * since there are no peer addresses to check against.
1370          * Upon return an ABORT will have been sent if needed.
1371          */
1372         if (!sctp_state(asoc, COOKIE_WAIT)) {
1373                 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1374                                                  commands)) {
1375                         retval = SCTP_DISPOSITION_CONSUME;
1376                         goto nomem_retval;
1377                 }
1378         }
1379
1380         sctp_tietags_populate(new_asoc, asoc);
1381
1382         /* B) "Z" shall respond immediately with an INIT ACK chunk.  */
1383
1384         /* If there are errors need to be reported for unknown parameters,
1385          * make sure to reserve enough room in the INIT ACK for them.
1386          */
1387         len = 0;
1388         if (err_chunk) {
1389                 len = ntohs(err_chunk->chunk_hdr->length) -
1390                         sizeof(sctp_chunkhdr_t);
1391         }
1392
1393         if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
1394                 goto nomem;
1395
1396         repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1397         if (!repl)
1398                 goto nomem;
1399
1400         /* If there are errors need to be reported for unknown parameters,
1401          * include them in the outgoing INIT ACK as "Unrecognized parameter"
1402          * parameter.
1403          */
1404         if (err_chunk) {
1405                 /* Get the "Unrecognized parameter" parameter(s) out of the
1406                  * ERROR chunk generated by sctp_verify_init(). Since the
1407                  * error cause code for "unknown parameter" and the
1408                  * "Unrecognized parameter" type is the same, we can
1409                  * construct the parameters in INIT ACK by copying the
1410                  * ERROR causes over.
1411                  */
1412                 unk_param = (sctp_unrecognized_param_t *)
1413                             ((__u8 *)(err_chunk->chunk_hdr) +
1414                             sizeof(sctp_chunkhdr_t));
1415                 /* Replace the cause code with the "Unrecognized parameter"
1416                  * parameter type.
1417                  */
1418                 sctp_addto_chunk(repl, len, unk_param);
1419         }
1420
1421         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1422         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1423
1424         /*
1425          * Note: After sending out INIT ACK with the State Cookie parameter,
1426          * "Z" MUST NOT allocate any resources for this new association.
1427          * Otherwise, "Z" will be vulnerable to resource attacks.
1428          */
1429         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1430         retval = SCTP_DISPOSITION_CONSUME;
1431
1432         return retval;
1433
1434 nomem:
1435         retval = SCTP_DISPOSITION_NOMEM;
1436 nomem_retval:
1437         if (new_asoc)
1438                 sctp_association_free(new_asoc);
1439 cleanup:
1440         if (err_chunk)
1441                 sctp_chunk_free(err_chunk);
1442         return retval;
1443 }
1444
1445 /*
1446  * Handle simultanous INIT.
1447  * This means we started an INIT and then we got an INIT request from
1448  * our peer.
1449  *
1450  * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1451  * This usually indicates an initialization collision, i.e., each
1452  * endpoint is attempting, at about the same time, to establish an
1453  * association with the other endpoint.
1454  *
1455  * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1456  * endpoint MUST respond with an INIT ACK using the same parameters it
1457  * sent in its original INIT chunk (including its Verification Tag,
1458  * unchanged). These original parameters are combined with those from the
1459  * newly received INIT chunk. The endpoint shall also generate a State
1460  * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1461  * INIT to calculate the State Cookie.
1462  *
1463  * After that, the endpoint MUST NOT change its state, the T1-init
1464  * timer shall be left running and the corresponding TCB MUST NOT be
1465  * destroyed. The normal procedures for handling State Cookies when
1466  * a TCB exists will resolve the duplicate INITs to a single association.
1467  *
1468  * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1469  * its Tie-Tags with the Tag information of itself and its peer (see
1470  * section 5.2.2 for a description of the Tie-Tags).
1471  *
1472  * Verification Tag: Not explicit, but an INIT can not have a valid
1473  * verification tag, so we skip the check.
1474  *
1475  * Inputs
1476  * (endpoint, asoc, chunk)
1477  *
1478  * Outputs
1479  * (asoc, reply_msg, msg_up, timers, counters)
1480  *
1481  * The return value is the disposition of the chunk.
1482  */
1483 sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1484                                     const struct sctp_association *asoc,
1485                                     const sctp_subtype_t type,
1486                                     void *arg,
1487                                     sctp_cmd_seq_t *commands)
1488 {
1489         /* Call helper to do the real work for both simulataneous and
1490          * duplicate INIT chunk handling.
1491          */
1492         return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1493 }
1494
1495 /*
1496  * Handle duplicated INIT messages.  These are usually delayed
1497  * restransmissions.
1498  *
1499  * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1500  * COOKIE-ECHOED and COOKIE-WAIT
1501  *
1502  * Unless otherwise stated, upon reception of an unexpected INIT for
1503  * this association, the endpoint shall generate an INIT ACK with a
1504  * State Cookie.  In the outbound INIT ACK the endpoint MUST copy its
1505  * current Verification Tag and peer's Verification Tag into a reserved
1506  * place within the state cookie.  We shall refer to these locations as
1507  * the Peer's-Tie-Tag and the Local-Tie-Tag.  The outbound SCTP packet
1508  * containing this INIT ACK MUST carry a Verification Tag value equal to
1509  * the Initiation Tag found in the unexpected INIT.  And the INIT ACK
1510  * MUST contain a new Initiation Tag (randomly generated see Section
1511  * 5.3.1).  Other parameters for the endpoint SHOULD be copied from the
1512  * existing parameters of the association (e.g. number of outbound
1513  * streams) into the INIT ACK and cookie.
1514  *
1515  * After sending out the INIT ACK, the endpoint shall take no further
1516  * actions, i.e., the existing association, including its current state,
1517  * and the corresponding TCB MUST NOT be changed.
1518  *
1519  * Note: Only when a TCB exists and the association is not in a COOKIE-
1520  * WAIT state are the Tie-Tags populated.  For a normal association INIT
1521  * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1522  * set to 0 (indicating that no previous TCB existed).  The INIT ACK and
1523  * State Cookie are populated as specified in section 5.2.1.
1524  *
1525  * Verification Tag: Not specified, but an INIT has no way of knowing
1526  * what the verification tag could be, so we ignore it.
1527  *
1528  * Inputs
1529  * (endpoint, asoc, chunk)
1530  *
1531  * Outputs
1532  * (asoc, reply_msg, msg_up, timers, counters)
1533  *
1534  * The return value is the disposition of the chunk.
1535  */
1536 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1537                                         const struct sctp_association *asoc,
1538                                         const sctp_subtype_t type,
1539                                         void *arg,
1540                                         sctp_cmd_seq_t *commands)
1541 {
1542         /* Call helper to do the real work for both simulataneous and
1543          * duplicate INIT chunk handling.
1544          */
1545         return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1546 }
1547
1548
1549 /*
1550  * Unexpected INIT-ACK handler.
1551  *
1552  * Section 5.2.3
1553  * If an INIT ACK received by an endpoint in any state other than the
1554  * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1555  * An unexpected INIT ACK usually indicates the processing of an old or
1556  * duplicated INIT chunk.
1557 */
1558 sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1559                                             const struct sctp_association *asoc,
1560                                             const sctp_subtype_t type,
1561                                             void *arg, sctp_cmd_seq_t *commands)
1562 {
1563         /* Per the above section, we'll discard the chunk if we have an
1564          * endpoint.  If this is an OOTB INIT-ACK, treat it as such.
1565          */
1566         if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
1567                 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1568         else
1569                 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1570 }
1571
1572 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1573  *
1574  * Section 5.2.4
1575  *  A)  In this case, the peer may have restarted.
1576  */
1577 static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1578                                         const struct sctp_association *asoc,
1579                                         struct sctp_chunk *chunk,
1580                                         sctp_cmd_seq_t *commands,
1581                                         struct sctp_association *new_asoc)
1582 {
1583         sctp_init_chunk_t *peer_init;
1584         struct sctp_ulpevent *ev;
1585         struct sctp_chunk *repl;
1586         struct sctp_chunk *err;
1587         sctp_disposition_t disposition;
1588
1589         /* new_asoc is a brand-new association, so these are not yet
1590          * side effects--it is safe to run them here.
1591          */
1592         peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1593
1594         if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1595                                sctp_source(chunk), peer_init,
1596                                GFP_ATOMIC))
1597                 goto nomem;
1598
1599         /* Make sure no new addresses are being added during the
1600          * restart.  Though this is a pretty complicated attack
1601          * since you'd have to get inside the cookie.
1602          */
1603         if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1604                 return SCTP_DISPOSITION_CONSUME;
1605         }
1606
1607         /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1608          * the peer has restarted (Action A), it MUST NOT setup a new
1609          * association but instead resend the SHUTDOWN ACK and send an ERROR
1610          * chunk with a "Cookie Received while Shutting Down" error cause to
1611          * its peer.
1612         */
1613         if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1614                 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1615                                 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1616                                 chunk, commands);
1617                 if (SCTP_DISPOSITION_NOMEM == disposition)
1618                         goto nomem;
1619
1620                 err = sctp_make_op_error(asoc, chunk,
1621                                          SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1622                                          NULL, 0);
1623                 if (err)
1624                         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1625                                         SCTP_CHUNK(err));
1626
1627                 return SCTP_DISPOSITION_CONSUME;
1628         }
1629
1630         /* For now, fail any unsent/unacked data.  Consider the optional
1631          * choice of resending of this data.
1632          */
1633         sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1634
1635         repl = sctp_make_cookie_ack(new_asoc, chunk);
1636         if (!repl)
1637                 goto nomem;
1638
1639         /* Report association restart to upper layer. */
1640         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1641                                              new_asoc->c.sinit_num_ostreams,
1642                                              new_asoc->c.sinit_max_instreams,
1643                                              NULL, GFP_ATOMIC);
1644         if (!ev)
1645                 goto nomem_ev;
1646
1647         /* Update the content of current association. */
1648         sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1649         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1650         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1651         return SCTP_DISPOSITION_CONSUME;
1652
1653 nomem_ev:
1654         sctp_chunk_free(repl);
1655 nomem:
1656         return SCTP_DISPOSITION_NOMEM;
1657 }
1658
1659 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1660  *
1661  * Section 5.2.4
1662  *   B) In this case, both sides may be attempting to start an association
1663  *      at about the same time but the peer endpoint started its INIT
1664  *      after responding to the local endpoint's INIT
1665  */
1666 /* This case represents an initialization collision.  */
1667 static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1668                                         const struct sctp_association *asoc,
1669                                         struct sctp_chunk *chunk,
1670                                         sctp_cmd_seq_t *commands,
1671                                         struct sctp_association *new_asoc)
1672 {
1673         sctp_init_chunk_t *peer_init;
1674         struct sctp_chunk *repl;
1675
1676         /* new_asoc is a brand-new association, so these are not yet
1677          * side effects--it is safe to run them here.
1678          */
1679         peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1680         if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1681                                sctp_source(chunk), peer_init,
1682                                GFP_ATOMIC))
1683                 goto nomem;
1684
1685         /* Update the content of current association.  */
1686         sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1687         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1688                         SCTP_STATE(SCTP_STATE_ESTABLISHED));
1689         SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1690         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1691
1692         repl = sctp_make_cookie_ack(new_asoc, chunk);
1693         if (!repl)
1694                 goto nomem;
1695
1696         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1697         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1698
1699         /* RFC 2960 5.1 Normal Establishment of an Association
1700          *
1701          * D) IMPLEMENTATION NOTE: An implementation may choose to
1702          * send the Communication Up notification to the SCTP user
1703          * upon reception of a valid COOKIE ECHO chunk.
1704          *
1705          * Sadly, this needs to be implemented as a side-effect, because
1706          * we are not guaranteed to have set the association id of the real
1707          * association and so these notifications need to be delayed until
1708          * the association id is allocated.
1709          */
1710
1711         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1712
1713         /* Sockets API Draft Section 5.3.1.6
1714          * When a peer sends a Adaptation Layer Indication parameter , SCTP
1715          * delivers this notification to inform the application that of the
1716          * peers requested adaptation layer.
1717          *
1718          * This also needs to be done as a side effect for the same reason as
1719          * above.
1720          */
1721         if (asoc->peer.adaptation_ind)
1722                 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1723
1724         return SCTP_DISPOSITION_CONSUME;
1725
1726 nomem:
1727         return SCTP_DISPOSITION_NOMEM;
1728 }
1729
1730 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1731  *
1732  * Section 5.2.4
1733  *  C) In this case, the local endpoint's cookie has arrived late.
1734  *     Before it arrived, the local endpoint sent an INIT and received an
1735  *     INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1736  *     but a new tag of its own.
1737  */
1738 /* This case represents an initialization collision.  */
1739 static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1740                                         const struct sctp_association *asoc,
1741                                         struct sctp_chunk *chunk,
1742                                         sctp_cmd_seq_t *commands,
1743                                         struct sctp_association *new_asoc)
1744 {
1745         /* The cookie should be silently discarded.
1746          * The endpoint SHOULD NOT change states and should leave
1747          * any timers running.
1748          */
1749         return SCTP_DISPOSITION_DISCARD;
1750 }
1751
1752 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1753  *
1754  * Section 5.2.4
1755  *
1756  * D) When both local and remote tags match the endpoint should always
1757  *    enter the ESTABLISHED state, if it has not already done so.
1758  */
1759 /* This case represents an initialization collision.  */
1760 static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1761                                         const struct sctp_association *asoc,
1762                                         struct sctp_chunk *chunk,
1763                                         sctp_cmd_seq_t *commands,
1764                                         struct sctp_association *new_asoc)
1765 {
1766         struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1767         struct sctp_chunk *repl;
1768
1769         /* Clarification from Implementor's Guide:
1770          * D) When both local and remote tags match the endpoint should
1771          * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1772          * It should stop any cookie timer that may be running and send
1773          * a COOKIE ACK.
1774          */
1775
1776         /* Don't accidentally move back into established state. */
1777         if (asoc->state < SCTP_STATE_ESTABLISHED) {
1778                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1779                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1780                 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1781                                 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1782                 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1783                 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1784                                 SCTP_NULL());
1785
1786                 /* RFC 2960 5.1 Normal Establishment of an Association
1787                  *
1788                  * D) IMPLEMENTATION NOTE: An implementation may choose
1789                  * to send the Communication Up notification to the
1790                  * SCTP user upon reception of a valid COOKIE
1791                  * ECHO chunk.
1792                  */
1793                 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1794                                              SCTP_COMM_UP, 0,
1795                                              asoc->c.sinit_num_ostreams,
1796                                              asoc->c.sinit_max_instreams,
1797                                              NULL, GFP_ATOMIC);
1798                 if (!ev)
1799                         goto nomem;
1800
1801                 /* Sockets API Draft Section 5.3.1.6
1802                  * When a peer sends a Adaptation Layer Indication parameter,
1803                  * SCTP delivers this notification to inform the application
1804                  * that of the peers requested adaptation layer.
1805                  */
1806                 if (asoc->peer.adaptation_ind) {
1807                         ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1808                                                                  GFP_ATOMIC);
1809                         if (!ai_ev)
1810                                 goto nomem;
1811
1812                 }
1813         }
1814         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1815
1816         repl = sctp_make_cookie_ack(new_asoc, chunk);
1817         if (!repl)
1818                 goto nomem;
1819
1820         if (ev)
1821                 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1822                                 SCTP_ULPEVENT(ev));
1823         if (ai_ev)
1824                 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1825                                         SCTP_ULPEVENT(ai_ev));
1826
1827         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1828         sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1829
1830         return SCTP_DISPOSITION_CONSUME;
1831
1832 nomem:
1833         if (ai_ev)
1834                 sctp_ulpevent_free(ai_ev);
1835         if (ev)
1836                 sctp_ulpevent_free(ev);
1837         return SCTP_DISPOSITION_NOMEM;
1838 }
1839
1840 /*
1841  * Handle a duplicate COOKIE-ECHO.  This usually means a cookie-carrying
1842  * chunk was retransmitted and then delayed in the network.
1843  *
1844  * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1845  *
1846  * Verification Tag: None.  Do cookie validation.
1847  *
1848  * Inputs
1849  * (endpoint, asoc, chunk)
1850  *
1851  * Outputs
1852  * (asoc, reply_msg, msg_up, timers, counters)
1853  *
1854  * The return value is the disposition of the chunk.
1855  */
1856 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1857                                         const struct sctp_association *asoc,
1858                                         const sctp_subtype_t type,
1859                                         void *arg,
1860                                         sctp_cmd_seq_t *commands)
1861 {
1862         sctp_disposition_t retval;
1863         struct sctp_chunk *chunk = arg;
1864         struct sctp_association *new_asoc;
1865         int error = 0;
1866         char action;
1867         struct sctp_chunk *err_chk_p;
1868
1869         /* Make sure that the chunk has a valid length from the protocol
1870          * perspective.  In this case check to make sure we have at least
1871          * enough for the chunk header.  Cookie length verification is
1872          * done later.
1873          */
1874         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1875                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1876                                                   commands);
1877
1878         /* "Decode" the chunk.  We have no optional parameters so we
1879          * are in good shape.
1880          */
1881         chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
1882         if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1883                                         sizeof(sctp_chunkhdr_t)))
1884                 goto nomem;
1885
1886         /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1887          * of a duplicate COOKIE ECHO match the Verification Tags of the
1888          * current association, consider the State Cookie valid even if
1889          * the lifespan is exceeded.
1890          */
1891         new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1892                                       &err_chk_p);
1893
1894         /* FIXME:
1895          * If the re-build failed, what is the proper error path
1896          * from here?
1897          *
1898          * [We should abort the association. --piggy]
1899          */
1900         if (!new_asoc) {
1901                 /* FIXME: Several errors are possible.  A bad cookie should
1902                  * be silently discarded, but think about logging it too.
1903                  */
1904                 switch (error) {
1905                 case -SCTP_IERROR_NOMEM:
1906                         goto nomem;
1907
1908                 case -SCTP_IERROR_STALE_COOKIE:
1909                         sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
1910                                                    err_chk_p);
1911                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1912                 case -SCTP_IERROR_BAD_SIG:
1913                 default:
1914                         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1915                 }
1916         }
1917
1918         /* Compare the tie_tag in cookie with the verification tag of
1919          * current association.
1920          */
1921         action = sctp_tietags_compare(new_asoc, asoc);
1922
1923         switch (action) {
1924         case 'A': /* Association restart. */
1925                 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
1926                                               new_asoc);
1927                 break;
1928
1929         case 'B': /* Collision case B. */
1930                 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
1931                                               new_asoc);
1932                 break;
1933
1934         case 'C': /* Collision case C. */
1935                 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
1936                                               new_asoc);
1937                 break;
1938
1939         case 'D': /* Collision case D. */
1940                 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
1941                                               new_asoc);
1942                 break;
1943
1944         default: /* Discard packet for all others. */
1945                 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1946                 break;
1947         }
1948
1949         /* Delete the tempory new association. */
1950         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1951         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1952
1953         return retval;
1954
1955 nomem:
1956         return SCTP_DISPOSITION_NOMEM;
1957 }
1958
1959 /*
1960  * Process an ABORT.  (SHUTDOWN-PENDING state)
1961  *
1962  * See sctp_sf_do_9_1_abort().
1963  */
1964 sctp_disposition_t sctp_sf_shutdown_pending_abort(
1965         const struct sctp_endpoint *ep,
1966         const struct sctp_association *asoc,
1967         const sctp_subtype_t type,
1968         void *arg,
1969         sctp_cmd_seq_t *commands)
1970 {
1971         struct sctp_chunk *chunk = arg;
1972
1973         if (!sctp_vtag_verify_either(chunk, asoc))
1974                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1975
1976         /* Make sure that the ABORT chunk has a valid length.
1977          * Since this is an ABORT chunk, we have to discard it
1978          * because of the following text:
1979          * RFC 2960, Section 3.3.7
1980          *    If an endpoint receives an ABORT with a format error or for an
1981          *    association that doesn't exist, it MUST silently discard it.
1982          * Becasue the length is "invalid", we can't really discard just
1983          * as we do not know its true length.  So, to be safe, discard the
1984          * packet.
1985          */
1986         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
1987                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1988
1989         /* Stop the T5-shutdown guard timer.  */
1990         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1991                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
1992
1993         return sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
1994 }
1995
1996 /*
1997  * Process an ABORT.  (SHUTDOWN-SENT state)
1998  *
1999  * See sctp_sf_do_9_1_abort().
2000  */
2001 sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2002                                         const struct sctp_association *asoc,
2003                                         const sctp_subtype_t type,
2004                                         void *arg,
2005                                         sctp_cmd_seq_t *commands)
2006 {
2007         struct sctp_chunk *chunk = arg;
2008
2009         if (!sctp_vtag_verify_either(chunk, asoc))
2010                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2011
2012         /* Make sure that the ABORT chunk has a valid length.
2013          * Since this is an ABORT chunk, we have to discard it
2014          * because of the following text:
2015          * RFC 2960, Section 3.3.7
2016          *    If an endpoint receives an ABORT with a format error or for an
2017          *    association that doesn't exist, it MUST silently discard it.
2018          * Becasue the length is "invalid", we can't really discard just
2019          * as we do not know its true length.  So, to be safe, discard the
2020          * packet.
2021          */
2022         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2023                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2024
2025         /* Stop the T2-shutdown timer. */
2026         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2027                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2028
2029         /* Stop the T5-shutdown guard timer.  */
2030         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2031                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2032
2033         return sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2034 }
2035
2036 /*
2037  * Process an ABORT.  (SHUTDOWN-ACK-SENT state)
2038  *
2039  * See sctp_sf_do_9_1_abort().
2040  */
2041 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2042         const struct sctp_endpoint *ep,
2043         const struct sctp_association *asoc,
2044         const sctp_subtype_t type,
2045         void *arg,
2046         sctp_cmd_seq_t *commands)
2047 {
2048         /* The same T2 timer, so we should be able to use
2049          * common function with the SHUTDOWN-SENT state.
2050          */
2051         return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2052 }
2053
2054 /*
2055  * Handle an Error received in COOKIE_ECHOED state.
2056  *
2057  * Only handle the error type of stale COOKIE Error, the other errors will
2058  * be ignored.
2059  *
2060  * Inputs
2061  * (endpoint, asoc, chunk)
2062  *
2063  * Outputs
2064  * (asoc, reply_msg, msg_up, timers, counters)
2065  *
2066  * The return value is the disposition of the chunk.
2067  */
2068 sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2069                                         const struct sctp_association *asoc,
2070                                         const sctp_subtype_t type,
2071                                         void *arg,
2072                                         sctp_cmd_seq_t *commands)
2073 {
2074         struct sctp_chunk *chunk = arg;
2075         sctp_errhdr_t *err;
2076
2077         if (!sctp_vtag_verify(chunk, asoc))
2078                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2079
2080         /* Make sure that the ERROR chunk has a valid length.
2081          * The parameter walking depends on this as well.
2082          */
2083         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2084                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2085                                                   commands);
2086
2087         /* Process the error here */
2088         /* FUTURE FIXME:  When PR-SCTP related and other optional
2089          * parms are emitted, this will have to change to handle multiple
2090          * errors.
2091          */
2092         sctp_walk_errors(err, chunk->chunk_hdr) {
2093                 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2094                         return sctp_sf_do_5_2_6_stale(ep, asoc, type,
2095                                                         arg, commands);
2096         }
2097
2098         /* It is possible to have malformed error causes, and that
2099          * will cause us to end the walk early.  However, since
2100          * we are discarding the packet, there should be no adverse
2101          * affects.
2102          */
2103         return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2104 }
2105
2106 /*
2107  * Handle a Stale COOKIE Error
2108  *
2109  * Section: 5.2.6 Handle Stale COOKIE Error
2110  * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2111  * one of the following three alternatives.
2112  * ...
2113  * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2114  *    Preservative parameter requesting an extension to the lifetime of
2115  *    the State Cookie. When calculating the time extension, an
2116  *    implementation SHOULD use the RTT information measured based on the
2117  *    previous COOKIE ECHO / ERROR exchange, and should add no more
2118  *    than 1 second beyond the measured RTT, due to long State Cookie
2119  *    lifetimes making the endpoint more subject to a replay attack.
2120  *
2121  * Verification Tag:  Not explicit, but safe to ignore.
2122  *
2123  * Inputs
2124  * (endpoint, asoc, chunk)
2125  *
2126  * Outputs
2127  * (asoc, reply_msg, msg_up, timers, counters)
2128  *
2129  * The return value is the disposition of the chunk.
2130  */
2131 static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2132                                                  const struct sctp_association *asoc,
2133                                                  const sctp_subtype_t type,
2134                                                  void *arg,
2135                                                  sctp_cmd_seq_t *commands)
2136 {
2137         struct sctp_chunk *chunk = arg;
2138         time_t stale;
2139         sctp_cookie_preserve_param_t bht;
2140         sctp_errhdr_t *err;
2141         struct sctp_chunk *reply;
2142         struct sctp_bind_addr *bp;
2143         int attempts = asoc->init_err_counter + 1;
2144
2145         if (attempts > asoc->max_init_attempts) {
2146                 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2147                                 SCTP_ERROR(ETIMEDOUT));
2148                 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2149                                 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2150                 return SCTP_DISPOSITION_DELETE_TCB;
2151         }
2152
2153         err = (sctp_errhdr_t *)(chunk->skb->data);
2154
2155         /* When calculating the time extension, an implementation
2156          * SHOULD use the RTT information measured based on the
2157          * previous COOKIE ECHO / ERROR exchange, and should add no
2158          * more than 1 second beyond the measured RTT, due to long
2159          * State Cookie lifetimes making the endpoint more subject to
2160          * a replay attack.
2161          * Measure of Staleness's unit is usec. (1/1000000 sec)
2162          * Suggested Cookie Life-span Increment's unit is msec.
2163          * (1/1000 sec)
2164          * In general, if you use the suggested cookie life, the value
2165          * found in the field of measure of staleness should be doubled
2166          * to give ample time to retransmit the new cookie and thus
2167          * yield a higher probability of success on the reattempt.
2168          */
2169         stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2170         stale = (stale * 2) / 1000;
2171
2172         bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2173         bht.param_hdr.length = htons(sizeof(bht));
2174         bht.lifespan_increment = htonl(stale);
2175
2176         /* Build that new INIT chunk.  */
2177         bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2178         reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2179         if (!reply)
2180                 goto nomem;
2181
2182         sctp_addto_chunk(reply, sizeof(bht), &bht);
2183
2184         /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2185         sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2186
2187         /* Stop pending T3-rtx and heartbeat timers */
2188         sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2189         sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2190
2191         /* Delete non-primary peer ip addresses since we are transitioning
2192          * back to the COOKIE-WAIT state
2193          */
2194         sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2195
2196         /* If we've sent any data bundled with COOKIE-ECHO we will need to
2197          * resend
2198          */
2199         sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN,
2200                         SCTP_TRANSPORT(asoc->peer.primary_path));
2201
2202         /* Cast away the const modifier, as we want to just
2203          * rerun it through as a sideffect.
2204          */
2205         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2206
2207         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2208                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2209         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2210                         SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2211         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2212                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2213
2214         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2215
2216         return SCTP_DISPOSITION_CONSUME;
2217
2218 nomem:
2219         return SCTP_DISPOSITION_NOMEM;
2220 }
2221
2222 /*
2223  * Process an ABORT.
2224  *
2225  * Section: 9.1
2226  * After checking the Verification Tag, the receiving endpoint shall
2227  * remove the association from its record, and shall report the
2228  * termination to its upper layer.
2229  *
2230  * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2231  * B) Rules for packet carrying ABORT:
2232  *
2233  *  - The endpoint shall always fill in the Verification Tag field of the
2234  *    outbound packet with the destination endpoint's tag value if it
2235  *    is known.
2236  *
2237  *  - If the ABORT is sent in response to an OOTB packet, the endpoint
2238  *    MUST follow the procedure described in Section 8.4.
2239  *
2240  *  - The receiver MUST accept the packet if the Verification Tag
2241  *    matches either its own tag, OR the tag of its peer. Otherwise, the
2242  *    receiver MUST silently discard the packet and take no further
2243  *    action.
2244  *
2245  * Inputs
2246  * (endpoint, asoc, chunk)
2247  *
2248  * Outputs
2249  * (asoc, reply_msg, msg_up, timers, counters)
2250  *
2251  * The return value is the disposition of the chunk.
2252  */
2253 sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2254                                         const struct sctp_association *asoc,
2255                                         const sctp_subtype_t type,
2256                                         void *arg,
2257                                         sctp_cmd_seq_t *commands)
2258 {
2259         struct sctp_chunk *chunk = arg;
2260         unsigned len;
2261         __be16 error = SCTP_ERROR_NO_ERROR;
2262
2263         if (!sctp_vtag_verify_either(chunk, asoc))
2264                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2265
2266         /* Make sure that the ABORT chunk has a valid length.
2267          * Since this is an ABORT chunk, we have to discard it
2268          * because of the following text:
2269          * RFC 2960, Section 3.3.7
2270          *    If an endpoint receives an ABORT with a format error or for an
2271          *    association that doesn't exist, it MUST silently discard it.
2272          * Becasue the length is "invalid", we can't really discard just
2273          * as we do not know its true length.  So, to be safe, discard the
2274          * packet.
2275          */
2276         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2277                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2278
2279         /* See if we have an error cause code in the chunk.  */
2280         len = ntohs(chunk->chunk_hdr->length);
2281         if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2282                 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2283
2284         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2285         /* ASSOC_FAILED will DELETE_TCB. */
2286         sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2287         SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2288         SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2289
2290         return SCTP_DISPOSITION_ABORT;
2291 }
2292
2293 /*
2294  * Process an ABORT.  (COOKIE-WAIT state)
2295  *
2296  * See sctp_sf_do_9_1_abort() above.
2297  */
2298 sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2299                                      const struct sctp_association *asoc,
2300                                      const sctp_subtype_t type,
2301                                      void *arg,
2302                                      sctp_cmd_seq_t *commands)
2303 {
2304         struct sctp_chunk *chunk = arg;
2305         unsigned len;
2306         __be16 error = SCTP_ERROR_NO_ERROR;
2307
2308         if (!sctp_vtag_verify_either(chunk, asoc))
2309                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2310
2311         /* Make sure that the ABORT chunk has a valid length.
2312          * Since this is an ABORT chunk, we have to discard it
2313          * because of the following text:
2314          * RFC 2960, Section 3.3.7
2315          *    If an endpoint receives an ABORT with a format error or for an
2316          *    association that doesn't exist, it MUST silently discard it.
2317          * Becasue the length is "invalid", we can't really discard just
2318          * as we do not know its true length.  So, to be safe, discard the
2319          * packet.
2320          */
2321         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2322                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2323
2324         /* See if we have an error cause code in the chunk.  */
2325         len = ntohs(chunk->chunk_hdr->length);
2326         if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2327                 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2328
2329         return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2330                                       chunk->transport);
2331 }
2332
2333 /*
2334  * Process an incoming ICMP as an ABORT.  (COOKIE-WAIT state)
2335  */
2336 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2337                                         const struct sctp_association *asoc,
2338                                         const sctp_subtype_t type,
2339                                         void *arg,
2340                                         sctp_cmd_seq_t *commands)
2341 {
2342         return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2343                                       ENOPROTOOPT, asoc,
2344                                       (struct sctp_transport *)arg);
2345 }
2346
2347 /*
2348  * Process an ABORT.  (COOKIE-ECHOED state)
2349  */
2350 sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2351                                                const struct sctp_association *asoc,
2352                                                const sctp_subtype_t type,
2353                                                void *arg,
2354                                                sctp_cmd_seq_t *commands)
2355 {
2356         /* There is a single T1 timer, so we should be able to use
2357          * common function with the COOKIE-WAIT state.
2358          */
2359         return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2360 }
2361
2362 /*
2363  * Stop T1 timer and abort association with "INIT failed".
2364  *
2365  * This is common code called by several sctp_sf_*_abort() functions above.
2366  */
2367 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
2368                                            __be16 error, int sk_err,
2369                                            const struct sctp_association *asoc,
2370                                            struct sctp_transport *transport)
2371 {
2372         SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
2373         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2374                         SCTP_STATE(SCTP_STATE_CLOSED));
2375         SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2376         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2377                         SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2378         sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2379         /* CMD_INIT_FAILED will DELETE_TCB. */
2380         sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2381                         SCTP_PERR(error));
2382         return SCTP_DISPOSITION_ABORT;
2383 }
2384
2385 /*
2386  * sctp_sf_do_9_2_shut
2387  *
2388  * Section: 9.2
2389  * Upon the reception of the SHUTDOWN, the peer endpoint shall
2390  *  - enter the SHUTDOWN-RECEIVED state,
2391  *
2392  *  - stop accepting new data from its SCTP user
2393  *
2394  *  - verify, by checking the Cumulative TSN Ack field of the chunk,
2395  *    that all its outstanding DATA chunks have been received by the
2396  *    SHUTDOWN sender.
2397  *
2398  * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2399  * send a SHUTDOWN in response to a ULP request. And should discard
2400  * subsequent SHUTDOWN chunks.
2401  *
2402  * If there are still outstanding DATA chunks left, the SHUTDOWN
2403  * receiver shall continue to follow normal data transmission
2404  * procedures defined in Section 6 until all outstanding DATA chunks
2405  * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2406  * new data from its SCTP user.
2407  *
2408  * Verification Tag:  8.5 Verification Tag [Normal verification]
2409  *
2410  * Inputs
2411  * (endpoint, asoc, chunk)
2412  *
2413  * Outputs
2414  * (asoc, reply_msg, msg_up, timers, counters)
2415  *
2416  * The return value is the disposition of the chunk.
2417  */
2418 sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2419                                            const struct sctp_association *asoc,
2420                                            const sctp_subtype_t type,
2421                                            void *arg,
2422                                            sctp_cmd_seq_t *commands)
2423 {
2424         struct sctp_chunk *chunk = arg;
2425         sctp_shutdownhdr_t *sdh;
2426         sctp_disposition_t disposition;
2427         struct sctp_ulpevent *ev;
2428
2429         if (!sctp_vtag_verify(chunk, asoc))
2430                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2431
2432         /* Make sure that the SHUTDOWN chunk has a valid length. */
2433         if (!sctp_chunk_length_valid(chunk,
2434                                       sizeof(struct sctp_shutdown_chunk_t)))
2435                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2436                                                   commands);
2437
2438         /* Convert the elaborate header.  */
2439         sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2440         skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2441         chunk->subh.shutdown_hdr = sdh;
2442
2443         /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2444          * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2445          * inform the application that it should cease sending data.
2446          */
2447         ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2448         if (!ev) {
2449                 disposition = SCTP_DISPOSITION_NOMEM;
2450                 goto out;
2451         }
2452         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2453
2454         /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2455          *  - enter the SHUTDOWN-RECEIVED state,
2456          *  - stop accepting new data from its SCTP user
2457          *
2458          * [This is implicit in the new state.]
2459          */
2460         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2461                         SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2462         disposition = SCTP_DISPOSITION_CONSUME;
2463
2464         if (sctp_outq_is_empty(&asoc->outqueue)) {
2465                 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2466                                                           arg, commands);
2467         }
2468
2469         if (SCTP_DISPOSITION_NOMEM == disposition)
2470                 goto out;
2471
2472         /*  - verify, by checking the Cumulative TSN Ack field of the
2473          *    chunk, that all its outstanding DATA chunks have been
2474          *    received by the SHUTDOWN sender.
2475          */
2476         sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2477                         SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2478
2479 out:
2480         return disposition;
2481 }
2482
2483 /* RFC 2960 9.2
2484  * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2485  * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2486  * transport addresses (either in the IP addresses or in the INIT chunk)
2487  * that belong to this association, it should discard the INIT chunk and
2488  * retransmit the SHUTDOWN ACK chunk.
2489  */
2490 sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2491                                     const struct sctp_association *asoc,
2492                                     const sctp_subtype_t type,
2493                                     void *arg,
2494                                     sctp_cmd_seq_t *commands)
2495 {
2496         struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2497         struct sctp_chunk *reply;
2498
2499         /* Since we are not going to really process this INIT, there
2500          * is no point in verifying chunk boundries.  Just generate
2501          * the SHUTDOWN ACK.
2502          */
2503         reply = sctp_make_shutdown_ack(asoc, chunk);
2504         if (NULL == reply)
2505                 goto nomem;
2506
2507         /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2508          * the T2-SHUTDOWN timer.
2509          */
2510         sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2511
2512         /* and restart the T2-shutdown timer. */
2513         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2514                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2515
2516         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2517
2518         return SCTP_DISPOSITION_CONSUME;
2519 nomem:
2520         return SCTP_DISPOSITION_NOMEM;
2521 }
2522
2523 /*
2524  * sctp_sf_do_ecn_cwr
2525  *
2526  * Section:  Appendix A: Explicit Congestion Notification
2527  *
2528  * CWR:
2529  *
2530  * RFC 2481 details a specific bit for a sender to send in the header of
2531  * its next outbound TCP segment to indicate to its peer that it has
2532  * reduced its congestion window.  This is termed the CWR bit.  For
2533  * SCTP the same indication is made by including the CWR chunk.
2534  * This chunk contains one data element, i.e. the TSN number that
2535  * was sent in the ECNE chunk.  This element represents the lowest
2536  * TSN number in the datagram that was originally marked with the
2537  * CE bit.
2538  *
2539  * Verification Tag: 8.5 Verification Tag [Normal verification]
2540  * Inputs
2541  * (endpoint, asoc, chunk)
2542  *
2543  * Outputs
2544  * (asoc, reply_msg, msg_up, timers, counters)
2545  *
2546  * The return value is the disposition of the chunk.
2547  */
2548 sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2549                                       const struct sctp_association *asoc,
2550                                       const sctp_subtype_t type,
2551                                       void *arg,
2552                                       sctp_cmd_seq_t *commands)
2553 {
2554         sctp_cwrhdr_t *cwr;
2555         struct sctp_chunk *chunk = arg;
2556         u32 lowest_tsn;
2557
2558         if (!sctp_vtag_verify(chunk, asoc))
2559                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2560
2561         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2562                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2563                                                   commands);
2564
2565         cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2566         skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2567
2568         lowest_tsn = ntohl(cwr->lowest_tsn);
2569
2570         /* Does this CWR ack the last sent congestion notification? */
2571         if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2572                 /* Stop sending ECNE. */
2573                 sctp_add_cmd_sf(commands,
2574                                 SCTP_CMD_ECN_CWR,
2575                                 SCTP_U32(lowest_tsn));
2576         }
2577         return SCTP_DISPOSITION_CONSUME;
2578 }
2579
2580 /*
2581  * sctp_sf_do_ecne
2582  *
2583  * Section:  Appendix A: Explicit Congestion Notification
2584  *
2585  * ECN-Echo
2586  *
2587  * RFC 2481 details a specific bit for a receiver to send back in its
2588  * TCP acknowledgements to notify the sender of the Congestion
2589  * Experienced (CE) bit having arrived from the network.  For SCTP this
2590  * same indication is made by including the ECNE chunk.  This chunk
2591  * contains one data element, i.e. the lowest TSN associated with the IP
2592  * datagram marked with the CE bit.....
2593  *
2594  * Verification Tag: 8.5 Verification Tag [Normal verification]
2595  * Inputs
2596  * (endpoint, asoc, chunk)
2597  *
2598  * Outputs
2599  * (asoc, reply_msg, msg_up, timers, counters)
2600  *
2601  * The return value is the disposition of the chunk.
2602  */
2603 sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2604                                    const struct sctp_association *asoc,
2605                                    const sctp_subtype_t type,
2606                                    void *arg,
2607                                    sctp_cmd_seq_t *commands)
2608 {
2609         sctp_ecnehdr_t *ecne;
2610         struct sctp_chunk *chunk = arg;
2611
2612         if (!sctp_vtag_verify(chunk, asoc))
2613                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2614
2615         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2616                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2617                                                   commands);
2618
2619         ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2620         skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2621
2622         /* If this is a newer ECNE than the last CWR packet we sent out */
2623         sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2624                         SCTP_U32(ntohl(ecne->lowest_tsn)));
2625
2626         return SCTP_DISPOSITION_CONSUME;
2627 }
2628
2629 /*
2630  * Section: 6.2  Acknowledgement on Reception of DATA Chunks
2631  *
2632  * The SCTP endpoint MUST always acknowledge the reception of each valid
2633  * DATA chunk.
2634  *
2635  * The guidelines on delayed acknowledgement algorithm specified in
2636  * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2637  * acknowledgement SHOULD be generated for at least every second packet
2638  * (not every second DATA chunk) received, and SHOULD be generated within
2639  * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2640  * situations it may be beneficial for an SCTP transmitter to be more
2641  * conservative than the algorithms detailed in this document allow.
2642  * However, an SCTP transmitter MUST NOT be more aggressive than the
2643  * following algorithms allow.
2644  *
2645  * A SCTP receiver MUST NOT generate more than one SACK for every
2646  * incoming packet, other than to update the offered window as the
2647  * receiving application consumes new data.
2648  *
2649  * Verification Tag:  8.5 Verification Tag [Normal verification]
2650  *
2651  * Inputs
2652  * (endpoint, asoc, chunk)
2653  *
2654  * Outputs
2655  * (asoc, reply_msg, msg_up, timers, counters)
2656  *
2657  * The return value is the disposition of the chunk.
2658  */
2659 sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2660                                         const struct sctp_association *asoc,
2661                                         const sctp_subtype_t type,
2662                                         void *arg,
2663                                         sctp_cmd_seq_t *commands)
2664 {
2665         struct sctp_chunk *chunk = arg;
2666         int error;
2667
2668         if (!sctp_vtag_verify(chunk, asoc)) {
2669                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2670                                 SCTP_NULL());
2671                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2672         }
2673
2674         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2675                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2676                                                   commands);
2677
2678         error = sctp_eat_data(asoc, chunk, commands );
2679         switch (error) {
2680         case SCTP_IERROR_NO_ERROR:
2681                 break;
2682         case SCTP_IERROR_HIGH_TSN:
2683         case SCTP_IERROR_BAD_STREAM:
2684                 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2685                 goto discard_noforce;
2686         case SCTP_IERROR_DUP_TSN:
2687         case SCTP_IERROR_IGNORE_TSN:
2688                 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2689                 goto discard_force;
2690         case SCTP_IERROR_NO_DATA:
2691                 goto consume;
2692         default:
2693                 BUG();
2694         }
2695
2696         if (asoc->autoclose) {
2697                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2698                                 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2699         }
2700
2701         /* If this is the last chunk in a packet, we need to count it
2702          * toward sack generation.  Note that we need to SACK every
2703          * OTHER packet containing data chunks, EVEN IF WE DISCARD
2704          * THEM.  We elect to NOT generate SACK's if the chunk fails
2705          * the verification tag test.
2706          *
2707          * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2708          *
2709          * The SCTP endpoint MUST always acknowledge the reception of
2710          * each valid DATA chunk.
2711          *
2712          * The guidelines on delayed acknowledgement algorithm
2713          * specified in  Section 4.2 of [RFC2581] SHOULD be followed.
2714          * Specifically, an acknowledgement SHOULD be generated for at
2715          * least every second packet (not every second DATA chunk)
2716          * received, and SHOULD be generated within 200 ms of the
2717          * arrival of any unacknowledged DATA chunk.  In some
2718          * situations it may be beneficial for an SCTP transmitter to
2719          * be more conservative than the algorithms detailed in this
2720          * document allow. However, an SCTP transmitter MUST NOT be
2721          * more aggressive than the following algorithms allow.
2722          */
2723         if (chunk->end_of_packet)
2724                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2725
2726         return SCTP_DISPOSITION_CONSUME;
2727
2728 discard_force:
2729         /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2730          *
2731          * When a packet arrives with duplicate DATA chunk(s) and with
2732          * no new DATA chunk(s), the endpoint MUST immediately send a
2733          * SACK with no delay.  If a packet arrives with duplicate
2734          * DATA chunk(s) bundled with new DATA chunks, the endpoint
2735          * MAY immediately send a SACK.  Normally receipt of duplicate
2736          * DATA chunks will occur when the original SACK chunk was lost
2737          * and the peer's RTO has expired.  The duplicate TSN number(s)
2738          * SHOULD be reported in the SACK as duplicate.
2739          */
2740         /* In our case, we split the MAY SACK advice up whether or not
2741          * the last chunk is a duplicate.'
2742          */
2743         if (chunk->end_of_packet)
2744                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2745         return SCTP_DISPOSITION_DISCARD;
2746
2747 discard_noforce:
2748         if (chunk->end_of_packet)
2749                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2750
2751         return SCTP_DISPOSITION_DISCARD;
2752 consume:
2753         return SCTP_DISPOSITION_CONSUME;
2754
2755 }
2756
2757 /*
2758  * sctp_sf_eat_data_fast_4_4
2759  *
2760  * Section: 4 (4)
2761  * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2762  *    DATA chunks without delay.
2763  *
2764  * Verification Tag:  8.5 Verification Tag [Normal verification]
2765  * Inputs
2766  * (endpoint, asoc, chunk)
2767  *
2768  * Outputs
2769  * (asoc, reply_msg, msg_up, timers, counters)
2770  *
2771  * The return value is the disposition of the chunk.
2772  */
2773 sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2774                                      const struct sctp_association *asoc,
2775                                      const sctp_subtype_t type,
2776                                      void *arg,
2777                                      sctp_cmd_seq_t *commands)
2778 {
2779         struct sctp_chunk *chunk = arg;
2780         int error;
2781
2782         if (!sctp_vtag_verify(chunk, asoc)) {
2783                 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2784                                 SCTP_NULL());
2785                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2786         }
2787
2788         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2789                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2790                                                   commands);
2791
2792         error = sctp_eat_data(asoc, chunk, commands );
2793         switch (error) {
2794         case SCTP_IERROR_NO_ERROR:
2795         case SCTP_IERROR_HIGH_TSN:
2796         case SCTP_IERROR_DUP_TSN:
2797         case SCTP_IERROR_IGNORE_TSN:
2798         case SCTP_IERROR_BAD_STREAM:
2799                 break;
2800         case SCTP_IERROR_NO_DATA:
2801                 goto consume;
2802         default:
2803                 BUG();
2804         }
2805
2806         /* Go a head and force a SACK, since we are shutting down. */
2807
2808         /* Implementor's Guide.
2809          *
2810          * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
2811          * respond to each received packet containing one or more DATA chunk(s)
2812          * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
2813          */
2814         if (chunk->end_of_packet) {
2815                 /* We must delay the chunk creation since the cumulative
2816                  * TSN has not been updated yet.
2817                  */
2818                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
2819                 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2820                 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2821                                 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2822         }
2823
2824 consume:
2825         return SCTP_DISPOSITION_CONSUME;
2826 }
2827
2828 /*
2829  * Section: 6.2  Processing a Received SACK
2830  * D) Any time a SACK arrives, the endpoint performs the following:
2831  *
2832  *     i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
2833  *     then drop the SACK.   Since Cumulative TSN Ack is monotonically
2834  *     increasing, a SACK whose Cumulative TSN Ack is less than the
2835  *     Cumulative TSN Ack Point indicates an out-of-order SACK.
2836  *
2837  *     ii) Set rwnd equal to the newly received a_rwnd minus the number
2838  *     of bytes still outstanding after processing the Cumulative TSN Ack
2839  *     and the Gap Ack Blocks.
2840  *
2841  *     iii) If the SACK is missing a TSN that was previously
2842  *     acknowledged via a Gap Ack Block (e.g., the data receiver
2843  *     reneged on the data), then mark the corresponding DATA chunk
2844  *     as available for retransmit:  Mark it as missing for fast
2845  *     retransmit as described in Section 7.2.4 and if no retransmit
2846  *     timer is running for the destination address to which the DATA
2847  *     chunk was originally transmitted, then T3-rtx is started for
2848  *     that destination address.
2849  *
2850  * Verification Tag:  8.5 Verification Tag [Normal verification]
2851  *
2852  * Inputs
2853  * (endpoint, asoc, chunk)
2854  *
2855  * Outputs
2856  * (asoc, reply_msg, msg_up, timers, counters)
2857  *
2858  * The return value is the disposition of the chunk.
2859  */
2860 sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
2861                                         const struct sctp_association *asoc,
2862                                         const sctp_subtype_t type,
2863                                         void *arg,
2864                                         sctp_cmd_seq_t *commands)
2865 {
2866         struct sctp_chunk *chunk = arg;
2867         sctp_sackhdr_t *sackh;
2868         __u32 ctsn;
2869
2870         if (!sctp_vtag_verify(chunk, asoc))
2871                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2872
2873         /* Make sure that the SACK chunk has a valid length. */
2874         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
2875                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2876                                                   commands);
2877
2878         /* Pull the SACK chunk from the data buffer */
2879         sackh = sctp_sm_pull_sack(chunk);
2880         /* Was this a bogus SACK? */
2881         if (!sackh)
2882                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2883         chunk->subh.sack_hdr = sackh;
2884         ctsn = ntohl(sackh->cum_tsn_ack);
2885
2886         /* i) If Cumulative TSN Ack is less than the Cumulative TSN
2887          *     Ack Point, then drop the SACK.  Since Cumulative TSN
2888          *     Ack is monotonically increasing, a SACK whose
2889          *     Cumulative TSN Ack is less than the Cumulative TSN Ack
2890          *     Point indicates an out-of-order SACK.
2891          */
2892         if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2893                 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2894                 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2895                 return SCTP_DISPOSITION_DISCARD;
2896         }
2897
2898         /* If Cumulative TSN Ack beyond the max tsn currently
2899          * send, terminating the association and respond to the
2900          * sender with an ABORT.
2901          */
2902         if (!TSN_lt(ctsn, asoc->next_tsn))
2903                 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2904
2905         /* Return this SACK for further processing.  */
2906         sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
2907
2908         /* Note: We do the rest of the work on the PROCESS_SACK
2909          * sideeffect.
2910          */
2911         return SCTP_DISPOSITION_CONSUME;
2912 }
2913
2914 /*
2915  * Generate an ABORT in response to a packet.
2916  *
2917  * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
2918  *
2919  * 8) The receiver should respond to the sender of the OOTB packet with
2920  *    an ABORT.  When sending the ABORT, the receiver of the OOTB packet
2921  *    MUST fill in the Verification Tag field of the outbound packet
2922  *    with the value found in the Verification Tag field of the OOTB
2923  *    packet and set the T-bit in the Chunk Flags to indicate that the
2924  *    Verification Tag is reflected.  After sending this ABORT, the
2925  *    receiver of the OOTB packet shall discard the OOTB packet and take
2926  *    no further action.
2927  *
2928  * Verification Tag:
2929  *
2930  * The return value is the disposition of the chunk.
2931 */
2932 sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
2933                                         const struct sctp_association *asoc,
2934                                         const sctp_subtype_t type,
2935                                         void *arg,
2936                                         sctp_cmd_seq_t *commands)
2937 {
2938         struct sctp_packet *packet = NULL;
2939         struct sctp_chunk *chunk = arg;
2940         struct sctp_chunk *abort;
2941
2942         packet = sctp_ootb_pkt_new(asoc, chunk);
2943
2944         if (packet) {
2945                 /* Make an ABORT. The T bit will be set if the asoc
2946                  * is NULL.
2947                  */
2948                 abort = sctp_make_abort(asoc, chunk, 0);
2949                 if (!abort) {
2950                         sctp_ootb_pkt_free(packet);
2951                         return SCTP_DISPOSITION_NOMEM;
2952                 }
2953
2954                 /* Reflect vtag if T-Bit is set */
2955                 if (sctp_test_T_bit(abort))
2956                         packet->vtag = ntohl(chunk->sctp_hdr->vtag);
2957
2958                 /* Set the skb to the belonging sock for accounting.  */
2959                 abort->skb->sk = ep->base.sk;
2960
2961                 sctp_packet_append_chunk(packet, abort);
2962
2963                 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
2964                                 SCTP_PACKET(packet));
2965
2966                 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
2967
2968                 return SCTP_DISPOSITION_CONSUME;
2969         }
2970
2971         return SCTP_DISPOSITION_NOMEM;
2972 }
2973
2974 /*
2975  * Received an ERROR chunk from peer.  Generate SCTP_REMOTE_ERROR
2976  * event as ULP notification for each cause included in the chunk.
2977  *
2978  * API 5.3.1.3 - SCTP_REMOTE_ERROR
2979  *
2980  * The return value is the disposition of the chunk.
2981 */
2982 sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
2983                                         const struct sctp_association *asoc,
2984                                         const sctp_subtype_t type,
2985                                         void *arg,
2986                                         sctp_cmd_seq_t *commands)
2987 {
2988         struct sctp_chunk *chunk = arg;
2989         struct sctp_ulpevent *ev;
2990
2991         if (!sctp_vtag_verify(chunk, asoc))
2992                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2993
2994         /* Make sure that the ERROR chunk has a valid length. */
2995         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2996                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2997                                                   commands);
2998
2999         while (chunk->chunk_end > chunk->skb->data) {
3000                 ev = sctp_ulpevent_make_remote_error(asoc, chunk, 0,
3001                                                      GFP_ATOMIC);
3002                 if (!ev)
3003                         goto nomem;
3004
3005                 if (!sctp_add_cmd(commands, SCTP_CMD_EVENT_ULP,
3006                                   SCTP_ULPEVENT(ev))) {
3007                         sctp_ulpevent_free(ev);
3008                         goto nomem;
3009                 }
3010
3011                 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3012                                 SCTP_CHUNK(chunk));
3013         }
3014         return SCTP_DISPOSITION_CONSUME;
3015
3016 nomem:
3017         return SCTP_DISPOSITION_NOMEM;
3018 }
3019
3020 /*
3021  * Process an inbound SHUTDOWN ACK.
3022  *
3023  * From Section 9.2:
3024  * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3025  * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3026  * peer, and remove all record of the association.
3027  *
3028  * The return value is the disposition.
3029  */
3030 sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3031                                         const struct sctp_association *asoc,
3032                                         const sctp_subtype_t type,
3033                                         void *arg,
3034                                         sctp_cmd_seq_t *commands)
3035 {
3036         struct sctp_chunk *chunk = arg;
3037         struct sctp_chunk *reply;
3038         struct sctp_ulpevent *ev;
3039
3040         if (!sctp_vtag_verify(chunk, asoc))
3041                 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3042
3043         /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3044         if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3045                 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3046                                                   commands);
3047         /* 10.2 H) SHUTDOWN COMPLETE notification
3048          *
3049          * When SCTP completes the shutdown procedures (section 9.2) this
3050          * notification is passed to the upper layer.
3051          */
3052         ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3053                                              0, 0, 0, NULL, GFP_ATOMIC);
3054         if (!ev)
3055                 goto nomem;
3056
3057         /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3058         reply = sctp_make_shutdown_complete(asoc, chunk);
3059         if (!reply)
3060                 goto nomem_chunk;
3061
3062         /* Do all the commands now (after allocation), so that we
3063          * have consistent state if memory allocation failes
3064          */
3065         sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3066
3067         /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3068          * stop the T2-shutdown timer,
3069          */
3070         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3071                         SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3072
3073         sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3074                         SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3075
3076         sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3077                         SCTP_STATE(SCTP_STATE_CLOSED));
3078         SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3079         SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3080         sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3081
3082         /* ...and remove all record of the association. */
3083         sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3084         return SCTP_DISPOSITION_DELETE_TCB;
3085
3086 nomem_chunk:
3087         sctp_ulpevent_free(ev);
3088 nomem:
3089         return SCTP_DISPOSITION_NOMEM;
3090 }
3091
3092 /*
3093  * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3094  *
3095  * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3096  *    respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3097  *    When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3098  *    packet must fill in the Verification Tag field of the outbound
3099  *    packet with the Verification Tag received in the SHUTDOWN ACK and
3100  *    set the T-bit in the Chunk Flags to indicate that the Verification
3101  *    Tag is reflected.
3102  *
3103  * 8) The receiver should respond to the sender of the OOTB packet with
3104  *    an ABORT.  When sending the ABORT, the receiver of the OOTB packet
3105  *    MUST fill in the Verification Tag field of the outbound packet
3106  *    with the value found in the Verification Tag field of the OOTB
3107  *    packet and set the T-bit in the Chunk Flags to indicate that the
3108  *    Verification Tag is reflected.  After sending this ABORT, the
3109  *    receiver of the OOTB packet shall discard the OOTB packet and take
3110  *    no further action.
3111  */
3112 sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3113                                 const struct sctp_association *asoc,
3114                                 const sctp_subtype_t type,
3115                                 void *arg,
3116                                 sctp_cmd_seq_t *commands)
3117 {