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