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