OSDN Git Service

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