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