GNU Linux-libre 4.19.286-gnu1
[releases.git] / net / netfilter / nf_conntrack_proto_sctp.c
1 /*
2  * Connection tracking protocol helper module for SCTP.
3  *
4  * Copyright (c) 2004 Kiran Kumar Immidi <immidi_kiran@yahoo.com>
5  * Copyright (c) 2004-2012 Patrick McHardy <kaber@trash.net>
6  *
7  * SCTP is defined in RFC 2960. References to various sections in this code
8  * are to this RFC.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #include <linux/types.h>
16 #include <linux/timer.h>
17 #include <linux/netfilter.h>
18 #include <linux/in.h>
19 #include <linux/ip.h>
20 #include <linux/sctp.h>
21 #include <linux/string.h>
22 #include <linux/seq_file.h>
23 #include <linux/spinlock.h>
24 #include <linux/interrupt.h>
25 #include <net/sctp/checksum.h>
26
27 #include <net/netfilter/nf_log.h>
28 #include <net/netfilter/nf_conntrack.h>
29 #include <net/netfilter/nf_conntrack_l4proto.h>
30 #include <net/netfilter/nf_conntrack_ecache.h>
31 #include <net/netfilter/nf_conntrack_timeout.h>
32
33 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
34    closely.  They're more complex. --RR
35
36    And so for me for SCTP :D -Kiran */
37
38 static const char *const sctp_conntrack_names[] = {
39         "NONE",
40         "CLOSED",
41         "COOKIE_WAIT",
42         "COOKIE_ECHOED",
43         "ESTABLISHED",
44         "SHUTDOWN_SENT",
45         "SHUTDOWN_RECD",
46         "SHUTDOWN_ACK_SENT",
47         "HEARTBEAT_SENT",
48         "HEARTBEAT_ACKED",
49 };
50
51 #define SECS  * HZ
52 #define MINS  * 60 SECS
53 #define HOURS * 60 MINS
54 #define DAYS  * 24 HOURS
55
56 static const unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX] = {
57         [SCTP_CONNTRACK_CLOSED]                 = 10 SECS,
58         [SCTP_CONNTRACK_COOKIE_WAIT]            = 3 SECS,
59         [SCTP_CONNTRACK_COOKIE_ECHOED]          = 3 SECS,
60         [SCTP_CONNTRACK_ESTABLISHED]            = 5 DAYS,
61         [SCTP_CONNTRACK_SHUTDOWN_SENT]          = 300 SECS / 1000,
62         [SCTP_CONNTRACK_SHUTDOWN_RECD]          = 300 SECS / 1000,
63         [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT]      = 3 SECS,
64         [SCTP_CONNTRACK_HEARTBEAT_SENT]         = 30 SECS,
65         [SCTP_CONNTRACK_HEARTBEAT_ACKED]        = 210 SECS,
66 };
67
68 #define SCTP_FLAG_HEARTBEAT_VTAG_FAILED 1
69
70 #define sNO SCTP_CONNTRACK_NONE
71 #define sCL SCTP_CONNTRACK_CLOSED
72 #define sCW SCTP_CONNTRACK_COOKIE_WAIT
73 #define sCE SCTP_CONNTRACK_COOKIE_ECHOED
74 #define sES SCTP_CONNTRACK_ESTABLISHED
75 #define sSS SCTP_CONNTRACK_SHUTDOWN_SENT
76 #define sSR SCTP_CONNTRACK_SHUTDOWN_RECD
77 #define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
78 #define sHS SCTP_CONNTRACK_HEARTBEAT_SENT
79 #define sHA SCTP_CONNTRACK_HEARTBEAT_ACKED
80 #define sIV SCTP_CONNTRACK_MAX
81
82 /*
83         These are the descriptions of the states:
84
85 NOTE: These state names are tantalizingly similar to the states of an
86 SCTP endpoint. But the interpretation of the states is a little different,
87 considering that these are the states of the connection and not of an end
88 point. Please note the subtleties. -Kiran
89
90 NONE              - Nothing so far.
91 COOKIE WAIT       - We have seen an INIT chunk in the original direction, or also
92                     an INIT_ACK chunk in the reply direction.
93 COOKIE ECHOED     - We have seen a COOKIE_ECHO chunk in the original direction.
94 ESTABLISHED       - We have seen a COOKIE_ACK in the reply direction.
95 SHUTDOWN_SENT     - We have seen a SHUTDOWN chunk in the original direction.
96 SHUTDOWN_RECD     - We have seen a SHUTDOWN chunk in the reply directoin.
97 SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite
98                     to that of the SHUTDOWN chunk.
99 CLOSED            - We have seen a SHUTDOWN_COMPLETE chunk in the direction of
100                     the SHUTDOWN chunk. Connection is closed.
101 HEARTBEAT_SENT    - We have seen a HEARTBEAT in a new flow.
102 HEARTBEAT_ACKED   - We have seen a HEARTBEAT-ACK in the direction opposite to
103                     that of the HEARTBEAT chunk. Secondary connection is
104                     established.
105 */
106
107 /* TODO
108  - I have assumed that the first INIT is in the original direction.
109  This messes things when an INIT comes in the reply direction in CLOSED
110  state.
111  - Check the error type in the reply dir before transitioning from
112 cookie echoed to closed.
113  - Sec 5.2.4 of RFC 2960
114  - Full Multi Homing support.
115 */
116
117 /* SCTP conntrack state transitions */
118 static const u8 sctp_conntracks[2][11][SCTP_CONNTRACK_MAX] = {
119         {
120 /*      ORIGINAL        */
121 /*                  sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
122 /* init         */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA, sCW, sHA},
123 /* init_ack     */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},
124 /* abort        */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL},
125 /* shutdown     */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA, sCL, sSS},
126 /* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA, sSA, sHA},
127 /* error        */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't have Stale cookie*/
128 /* cookie_echo  */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* 5.2.4 - Big TODO */
129 /* cookie_ack   */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sCL, sHA},/* Can't come in orig dir */
130 /* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL, sCL, sHA},
131 /* heartbeat    */ {sHS, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
132 /* heartbeat_ack*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA}
133         },
134         {
135 /*      REPLY   */
136 /*                  sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA */
137 /* init         */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* INIT in sCL Big TODO */
138 /* init_ack     */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},
139 /* abort        */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV, sCL},
140 /* shutdown     */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA, sIV, sSR},
141 /* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA, sIV, sHA},
142 /* error        */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA, sIV, sHA},
143 /* cookie_echo  */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sIV, sHA},/* Can't come in reply dir */
144 /* cookie_ack   */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA, sIV, sHA},
145 /* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL, sIV, sHA},
146 /* heartbeat    */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHS, sHA},
147 /* heartbeat_ack*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA, sHA, sHA}
148         }
149 };
150
151 static inline struct nf_sctp_net *sctp_pernet(struct net *net)
152 {
153         return &net->ct.nf_ct_proto.sctp;
154 }
155
156 #ifdef CONFIG_NF_CONNTRACK_PROCFS
157 /* Print out the private part of the conntrack. */
158 static void sctp_print_conntrack(struct seq_file *s, struct nf_conn *ct)
159 {
160         seq_printf(s, "%s ", sctp_conntrack_names[ct->proto.sctp.state]);
161 }
162 #endif
163
164 #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count)     \
165 for ((offset) = (dataoff) + sizeof(struct sctphdr), (count) = 0;        \
166         (offset) < (skb)->len &&                                        \
167         ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch)));   \
168         (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
169
170 /* Some validity checks to make sure the chunks are fine */
171 static int do_basic_checks(struct nf_conn *ct,
172                            const struct sk_buff *skb,
173                            unsigned int dataoff,
174                            unsigned long *map)
175 {
176         u_int32_t offset, count;
177         struct sctp_chunkhdr _sch, *sch;
178         int flag;
179
180         flag = 0;
181
182         for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
183                 pr_debug("Chunk Num: %d  Type: %d\n", count, sch->type);
184
185                 if (sch->type == SCTP_CID_INIT ||
186                     sch->type == SCTP_CID_INIT_ACK ||
187                     sch->type == SCTP_CID_SHUTDOWN_COMPLETE)
188                         flag = 1;
189
190                 /*
191                  * Cookie Ack/Echo chunks not the first OR
192                  * Init / Init Ack / Shutdown compl chunks not the only chunks
193                  * OR zero-length.
194                  */
195                 if (((sch->type == SCTP_CID_COOKIE_ACK ||
196                       sch->type == SCTP_CID_COOKIE_ECHO ||
197                       flag) &&
198                      count != 0) || !sch->length) {
199                         pr_debug("Basic checks failed\n");
200                         return 1;
201                 }
202
203                 if (map)
204                         set_bit(sch->type, map);
205         }
206
207         pr_debug("Basic checks passed\n");
208         return count == 0;
209 }
210
211 static int sctp_new_state(enum ip_conntrack_dir dir,
212                           enum sctp_conntrack cur_state,
213                           int chunk_type)
214 {
215         int i;
216
217         pr_debug("Chunk type: %d\n", chunk_type);
218
219         switch (chunk_type) {
220         case SCTP_CID_INIT:
221                 pr_debug("SCTP_CID_INIT\n");
222                 i = 0;
223                 break;
224         case SCTP_CID_INIT_ACK:
225                 pr_debug("SCTP_CID_INIT_ACK\n");
226                 i = 1;
227                 break;
228         case SCTP_CID_ABORT:
229                 pr_debug("SCTP_CID_ABORT\n");
230                 i = 2;
231                 break;
232         case SCTP_CID_SHUTDOWN:
233                 pr_debug("SCTP_CID_SHUTDOWN\n");
234                 i = 3;
235                 break;
236         case SCTP_CID_SHUTDOWN_ACK:
237                 pr_debug("SCTP_CID_SHUTDOWN_ACK\n");
238                 i = 4;
239                 break;
240         case SCTP_CID_ERROR:
241                 pr_debug("SCTP_CID_ERROR\n");
242                 i = 5;
243                 break;
244         case SCTP_CID_COOKIE_ECHO:
245                 pr_debug("SCTP_CID_COOKIE_ECHO\n");
246                 i = 6;
247                 break;
248         case SCTP_CID_COOKIE_ACK:
249                 pr_debug("SCTP_CID_COOKIE_ACK\n");
250                 i = 7;
251                 break;
252         case SCTP_CID_SHUTDOWN_COMPLETE:
253                 pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n");
254                 i = 8;
255                 break;
256         case SCTP_CID_HEARTBEAT:
257                 pr_debug("SCTP_CID_HEARTBEAT");
258                 i = 9;
259                 break;
260         case SCTP_CID_HEARTBEAT_ACK:
261                 pr_debug("SCTP_CID_HEARTBEAT_ACK");
262                 i = 10;
263                 break;
264         default:
265                 /* Other chunks like DATA or SACK do not change the state */
266                 pr_debug("Unknown chunk type, Will stay in %s\n",
267                          sctp_conntrack_names[cur_state]);
268                 return cur_state;
269         }
270
271         pr_debug("dir: %d   cur_state: %s  chunk_type: %d  new_state: %s\n",
272                  dir, sctp_conntrack_names[cur_state], chunk_type,
273                  sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
274
275         return sctp_conntracks[dir][i][cur_state];
276 }
277
278 /* Returns verdict for packet, or -NF_ACCEPT for invalid. */
279 static int sctp_packet(struct nf_conn *ct,
280                        const struct sk_buff *skb,
281                        unsigned int dataoff,
282                        enum ip_conntrack_info ctinfo)
283 {
284         enum sctp_conntrack new_state, old_state;
285         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
286         const struct sctphdr *sh;
287         struct sctphdr _sctph;
288         const struct sctp_chunkhdr *sch;
289         struct sctp_chunkhdr _sch;
290         u_int32_t offset, count;
291         unsigned int *timeouts;
292         unsigned long map[256 / sizeof(unsigned long)] = { 0 };
293         bool ignore = false;
294
295         sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
296         if (sh == NULL)
297                 goto out;
298
299         if (do_basic_checks(ct, skb, dataoff, map) != 0)
300                 goto out;
301
302         /* Check the verification tag (Sec 8.5) */
303         if (!test_bit(SCTP_CID_INIT, map) &&
304             !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) &&
305             !test_bit(SCTP_CID_COOKIE_ECHO, map) &&
306             !test_bit(SCTP_CID_ABORT, map) &&
307             !test_bit(SCTP_CID_SHUTDOWN_ACK, map) &&
308             !test_bit(SCTP_CID_HEARTBEAT, map) &&
309             !test_bit(SCTP_CID_HEARTBEAT_ACK, map) &&
310             sh->vtag != ct->proto.sctp.vtag[dir]) {
311                 pr_debug("Verification tag check failed\n");
312                 goto out;
313         }
314
315         old_state = new_state = SCTP_CONNTRACK_NONE;
316         spin_lock_bh(&ct->lock);
317         for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
318                 /* Special cases of Verification tag check (Sec 8.5.1) */
319                 if (sch->type == SCTP_CID_INIT) {
320                         /* (A) vtag MUST be zero */
321                         if (sh->vtag != 0)
322                                 goto out_unlock;
323                 } else if (sch->type == SCTP_CID_ABORT) {
324                         /* (B) vtag MUST match own vtag if T flag is unset OR
325                          * MUST match peer's vtag if T flag is set
326                          */
327                         if ((!(sch->flags & SCTP_CHUNK_FLAG_T) &&
328                              sh->vtag != ct->proto.sctp.vtag[dir]) ||
329                             ((sch->flags & SCTP_CHUNK_FLAG_T) &&
330                              sh->vtag != ct->proto.sctp.vtag[!dir]))
331                                 goto out_unlock;
332                 } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) {
333                         /* (C) vtag MUST match own vtag if T flag is unset OR
334                          * MUST match peer's vtag if T flag is set
335                          */
336                         if ((!(sch->flags & SCTP_CHUNK_FLAG_T) &&
337                              sh->vtag != ct->proto.sctp.vtag[dir]) ||
338                             ((sch->flags & SCTP_CHUNK_FLAG_T) &&
339                              sh->vtag != ct->proto.sctp.vtag[!dir]))
340                                 goto out_unlock;
341                 } else if (sch->type == SCTP_CID_COOKIE_ECHO) {
342                         /* (D) vtag must be same as init_vtag as found in INIT_ACK */
343                         if (sh->vtag != ct->proto.sctp.vtag[dir])
344                                 goto out_unlock;
345                 } else if (sch->type == SCTP_CID_HEARTBEAT) {
346                         if (ct->proto.sctp.vtag[dir] == 0) {
347                                 pr_debug("Setting %d vtag %x for dir %d\n", sch->type, sh->vtag, dir);
348                                 ct->proto.sctp.vtag[dir] = sh->vtag;
349                         } else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
350                                 if (test_bit(SCTP_CID_DATA, map) || ignore)
351                                         goto out_unlock;
352
353                                 ct->proto.sctp.flags |= SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
354                                 ct->proto.sctp.last_dir = dir;
355                                 ignore = true;
356                                 continue;
357                         } else if (ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) {
358                                 ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
359                         }
360                 } else if (sch->type == SCTP_CID_HEARTBEAT_ACK) {
361                         if (ct->proto.sctp.vtag[dir] == 0) {
362                                 pr_debug("Setting vtag %x for dir %d\n",
363                                          sh->vtag, dir);
364                                 ct->proto.sctp.vtag[dir] = sh->vtag;
365                         } else if (sh->vtag != ct->proto.sctp.vtag[dir]) {
366                                 if (test_bit(SCTP_CID_DATA, map) || ignore)
367                                         goto out_unlock;
368
369                                 if ((ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) == 0 ||
370                                     ct->proto.sctp.last_dir == dir)
371                                         goto out_unlock;
372
373                                 ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
374                                 ct->proto.sctp.vtag[dir] = sh->vtag;
375                                 ct->proto.sctp.vtag[!dir] = 0;
376                         } else if (ct->proto.sctp.flags & SCTP_FLAG_HEARTBEAT_VTAG_FAILED) {
377                                 ct->proto.sctp.flags &= ~SCTP_FLAG_HEARTBEAT_VTAG_FAILED;
378                         }
379                 }
380
381                 old_state = ct->proto.sctp.state;
382                 new_state = sctp_new_state(dir, old_state, sch->type);
383
384                 /* Invalid */
385                 if (new_state == SCTP_CONNTRACK_MAX) {
386                         pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u "
387                                  "conntrack=%u\n",
388                                  dir, sch->type, old_state);
389                         goto out_unlock;
390                 }
391
392                 /* If it is an INIT or an INIT ACK note down the vtag */
393                 if (sch->type == SCTP_CID_INIT ||
394                     sch->type == SCTP_CID_INIT_ACK) {
395                         struct sctp_inithdr _inithdr, *ih;
396
397                         ih = skb_header_pointer(skb, offset + sizeof(_sch),
398                                                 sizeof(_inithdr), &_inithdr);
399                         if (ih == NULL)
400                                 goto out_unlock;
401                         pr_debug("Setting vtag %x for dir %d\n",
402                                  ih->init_tag, !dir);
403                         ct->proto.sctp.vtag[!dir] = ih->init_tag;
404
405                         /* don't renew timeout on init retransmit so
406                          * port reuse by client or NAT middlebox cannot
407                          * keep entry alive indefinitely (incl. nat info).
408                          */
409                         if (new_state == SCTP_CONNTRACK_CLOSED &&
410                             old_state == SCTP_CONNTRACK_CLOSED &&
411                             nf_ct_is_confirmed(ct))
412                                 ignore = true;
413                 }
414
415                 ct->proto.sctp.state = new_state;
416                 if (old_state != new_state)
417                         nf_conntrack_event_cache(IPCT_PROTOINFO, ct);
418         }
419         spin_unlock_bh(&ct->lock);
420
421         /* allow but do not refresh timeout */
422         if (ignore)
423                 return NF_ACCEPT;
424
425         timeouts = nf_ct_timeout_lookup(ct);
426         if (!timeouts)
427                 timeouts = sctp_pernet(nf_ct_net(ct))->timeouts;
428
429         nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
430
431         if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED &&
432             dir == IP_CT_DIR_REPLY &&
433             new_state == SCTP_CONNTRACK_ESTABLISHED) {
434                 pr_debug("Setting assured bit\n");
435                 set_bit(IPS_ASSURED_BIT, &ct->status);
436                 nf_conntrack_event_cache(IPCT_ASSURED, ct);
437         }
438
439         return NF_ACCEPT;
440
441 out_unlock:
442         spin_unlock_bh(&ct->lock);
443 out:
444         return -NF_ACCEPT;
445 }
446
447 /* Called when a new connection for this protocol found. */
448 static bool sctp_new(struct nf_conn *ct, const struct sk_buff *skb,
449                      unsigned int dataoff)
450 {
451         enum sctp_conntrack new_state;
452         const struct sctphdr *sh;
453         struct sctphdr _sctph;
454         const struct sctp_chunkhdr *sch;
455         struct sctp_chunkhdr _sch;
456         u_int32_t offset, count;
457         unsigned long map[256 / sizeof(unsigned long)] = { 0 };
458
459         sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph);
460         if (sh == NULL)
461                 return false;
462
463         if (do_basic_checks(ct, skb, dataoff, map) != 0)
464                 return false;
465
466         /* If an OOTB packet has any of these chunks discard (Sec 8.4) */
467         if (test_bit(SCTP_CID_ABORT, map) ||
468             test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) ||
469             test_bit(SCTP_CID_COOKIE_ACK, map))
470                 return false;
471
472         memset(&ct->proto.sctp, 0, sizeof(ct->proto.sctp));
473         new_state = SCTP_CONNTRACK_MAX;
474         for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) {
475                 /* Don't need lock here: this conntrack not in circulation yet */
476                 new_state = sctp_new_state(IP_CT_DIR_ORIGINAL,
477                                            SCTP_CONNTRACK_NONE, sch->type);
478
479                 /* Invalid: delete conntrack */
480                 if (new_state == SCTP_CONNTRACK_NONE ||
481                     new_state == SCTP_CONNTRACK_MAX) {
482                         pr_debug("nf_conntrack_sctp: invalid new deleting.\n");
483                         return false;
484                 }
485
486                 /* Copy the vtag into the state info */
487                 if (sch->type == SCTP_CID_INIT) {
488                         struct sctp_inithdr _inithdr, *ih;
489                         /* Sec 8.5.1 (A) */
490                         if (sh->vtag)
491                                 return false;
492
493                         ih = skb_header_pointer(skb, offset + sizeof(_sch),
494                                                 sizeof(_inithdr), &_inithdr);
495                         if (!ih)
496                                 return false;
497
498                         pr_debug("Setting vtag %x for new conn\n",
499                                  ih->init_tag);
500
501                         ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = ih->init_tag;
502                 } else if (sch->type == SCTP_CID_HEARTBEAT) {
503                         pr_debug("Setting vtag %x for secondary conntrack\n",
504                                  sh->vtag);
505                         ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] = sh->vtag;
506                 }
507                 /* If it is a shutdown ack OOTB packet, we expect a return
508                    shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */
509                 else {
510                         pr_debug("Setting vtag %x for new conn OOTB\n",
511                                  sh->vtag);
512                         ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag;
513                 }
514
515                 ct->proto.sctp.state = new_state;
516         }
517
518         return true;
519 }
520
521 static int sctp_error(struct net *net, struct nf_conn *tpl, struct sk_buff *skb,
522                       unsigned int dataoff,
523                       u8 pf, unsigned int hooknum)
524 {
525         const struct sctphdr *sh;
526         const char *logmsg;
527
528         if (skb->len < dataoff + sizeof(struct sctphdr)) {
529                 logmsg = "nf_ct_sctp: short packet ";
530                 goto out_invalid;
531         }
532         if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
533             skb->ip_summed == CHECKSUM_NONE) {
534                 if (!skb_make_writable(skb, dataoff + sizeof(struct sctphdr))) {
535                         logmsg = "nf_ct_sctp: failed to read header ";
536                         goto out_invalid;
537                 }
538                 sh = (const struct sctphdr *)(skb->data + dataoff);
539                 if (sh->checksum != sctp_compute_cksum(skb, dataoff)) {
540                         logmsg = "nf_ct_sctp: bad CRC ";
541                         goto out_invalid;
542                 }
543                 skb->ip_summed = CHECKSUM_UNNECESSARY;
544         }
545         return NF_ACCEPT;
546 out_invalid:
547         nf_l4proto_log_invalid(skb, net, pf, IPPROTO_SCTP, "%s", logmsg);
548         return -NF_ACCEPT;
549 }
550
551 static bool sctp_can_early_drop(const struct nf_conn *ct)
552 {
553         switch (ct->proto.sctp.state) {
554         case SCTP_CONNTRACK_SHUTDOWN_SENT:
555         case SCTP_CONNTRACK_SHUTDOWN_RECD:
556         case SCTP_CONNTRACK_SHUTDOWN_ACK_SENT:
557                 return true;
558         default:
559                 break;
560         }
561
562         return false;
563 }
564
565 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
566
567 #include <linux/netfilter/nfnetlink.h>
568 #include <linux/netfilter/nfnetlink_conntrack.h>
569
570 static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,
571                           struct nf_conn *ct)
572 {
573         struct nlattr *nest_parms;
574
575         spin_lock_bh(&ct->lock);
576         nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP | NLA_F_NESTED);
577         if (!nest_parms)
578                 goto nla_put_failure;
579
580         if (nla_put_u8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state) ||
581             nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_ORIGINAL,
582                          ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]) ||
583             nla_put_be32(skb, CTA_PROTOINFO_SCTP_VTAG_REPLY,
584                          ct->proto.sctp.vtag[IP_CT_DIR_REPLY]))
585                 goto nla_put_failure;
586
587         spin_unlock_bh(&ct->lock);
588
589         nla_nest_end(skb, nest_parms);
590
591         return 0;
592
593 nla_put_failure:
594         spin_unlock_bh(&ct->lock);
595         return -1;
596 }
597
598 static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = {
599         [CTA_PROTOINFO_SCTP_STATE]          = { .type = NLA_U8 },
600         [CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]  = { .type = NLA_U32 },
601         [CTA_PROTOINFO_SCTP_VTAG_REPLY]     = { .type = NLA_U32 },
602 };
603
604 #define SCTP_NLATTR_SIZE ( \
605                 NLA_ALIGN(NLA_HDRLEN + 1) + \
606                 NLA_ALIGN(NLA_HDRLEN + 4) + \
607                 NLA_ALIGN(NLA_HDRLEN + 4))
608
609 static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct)
610 {
611         struct nlattr *attr = cda[CTA_PROTOINFO_SCTP];
612         struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1];
613         int err;
614
615         /* updates may not contain the internal protocol info, skip parsing */
616         if (!attr)
617                 return 0;
618
619         err = nla_parse_nested(tb, CTA_PROTOINFO_SCTP_MAX, attr,
620                                sctp_nla_policy, NULL);
621         if (err < 0)
622                 return err;
623
624         if (!tb[CTA_PROTOINFO_SCTP_STATE] ||
625             !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] ||
626             !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY])
627                 return -EINVAL;
628
629         spin_lock_bh(&ct->lock);
630         ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]);
631         ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] =
632                 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]);
633         ct->proto.sctp.vtag[IP_CT_DIR_REPLY] =
634                 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]);
635         spin_unlock_bh(&ct->lock);
636
637         return 0;
638 }
639 #endif
640
641 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
642
643 #include <linux/netfilter/nfnetlink.h>
644 #include <linux/netfilter/nfnetlink_cttimeout.h>
645
646 static int sctp_timeout_nlattr_to_obj(struct nlattr *tb[],
647                                       struct net *net, void *data)
648 {
649         unsigned int *timeouts = data;
650         struct nf_sctp_net *sn = sctp_pernet(net);
651         int i;
652
653         if (!timeouts)
654                 timeouts = sn->timeouts;
655
656         /* set default SCTP timeouts. */
657         for (i=0; i<SCTP_CONNTRACK_MAX; i++)
658                 timeouts[i] = sn->timeouts[i];
659
660         /* there's a 1:1 mapping between attributes and protocol states. */
661         for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
662                 if (tb[i]) {
663                         timeouts[i] = ntohl(nla_get_be32(tb[i])) * HZ;
664                 }
665         }
666
667         timeouts[CTA_TIMEOUT_SCTP_UNSPEC] = timeouts[CTA_TIMEOUT_SCTP_CLOSED];
668         return 0;
669 }
670
671 static int
672 sctp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
673 {
674         const unsigned int *timeouts = data;
675         int i;
676
677         for (i=CTA_TIMEOUT_SCTP_UNSPEC+1; i<CTA_TIMEOUT_SCTP_MAX+1; i++) {
678                 if (nla_put_be32(skb, i, htonl(timeouts[i] / HZ)))
679                         goto nla_put_failure;
680         }
681         return 0;
682
683 nla_put_failure:
684         return -ENOSPC;
685 }
686
687 static const struct nla_policy
688 sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = {
689         [CTA_TIMEOUT_SCTP_CLOSED]               = { .type = NLA_U32 },
690         [CTA_TIMEOUT_SCTP_COOKIE_WAIT]          = { .type = NLA_U32 },
691         [CTA_TIMEOUT_SCTP_COOKIE_ECHOED]        = { .type = NLA_U32 },
692         [CTA_TIMEOUT_SCTP_ESTABLISHED]          = { .type = NLA_U32 },
693         [CTA_TIMEOUT_SCTP_SHUTDOWN_SENT]        = { .type = NLA_U32 },
694         [CTA_TIMEOUT_SCTP_SHUTDOWN_RECD]        = { .type = NLA_U32 },
695         [CTA_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT]    = { .type = NLA_U32 },
696         [CTA_TIMEOUT_SCTP_HEARTBEAT_SENT]       = { .type = NLA_U32 },
697         [CTA_TIMEOUT_SCTP_HEARTBEAT_ACKED]      = { .type = NLA_U32 },
698 };
699 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
700
701
702 #ifdef CONFIG_SYSCTL
703 static struct ctl_table sctp_sysctl_table[] = {
704         {
705                 .procname       = "nf_conntrack_sctp_timeout_closed",
706                 .maxlen         = sizeof(unsigned int),
707                 .mode           = 0644,
708                 .proc_handler   = proc_dointvec_jiffies,
709         },
710         {
711                 .procname       = "nf_conntrack_sctp_timeout_cookie_wait",
712                 .maxlen         = sizeof(unsigned int),
713                 .mode           = 0644,
714                 .proc_handler   = proc_dointvec_jiffies,
715         },
716         {
717                 .procname       = "nf_conntrack_sctp_timeout_cookie_echoed",
718                 .maxlen         = sizeof(unsigned int),
719                 .mode           = 0644,
720                 .proc_handler   = proc_dointvec_jiffies,
721         },
722         {
723                 .procname       = "nf_conntrack_sctp_timeout_established",
724                 .maxlen         = sizeof(unsigned int),
725                 .mode           = 0644,
726                 .proc_handler   = proc_dointvec_jiffies,
727         },
728         {
729                 .procname       = "nf_conntrack_sctp_timeout_shutdown_sent",
730                 .maxlen         = sizeof(unsigned int),
731                 .mode           = 0644,
732                 .proc_handler   = proc_dointvec_jiffies,
733         },
734         {
735                 .procname       = "nf_conntrack_sctp_timeout_shutdown_recd",
736                 .maxlen         = sizeof(unsigned int),
737                 .mode           = 0644,
738                 .proc_handler   = proc_dointvec_jiffies,
739         },
740         {
741                 .procname       = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
742                 .maxlen         = sizeof(unsigned int),
743                 .mode           = 0644,
744                 .proc_handler   = proc_dointvec_jiffies,
745         },
746         {
747                 .procname       = "nf_conntrack_sctp_timeout_heartbeat_sent",
748                 .maxlen         = sizeof(unsigned int),
749                 .mode           = 0644,
750                 .proc_handler   = proc_dointvec_jiffies,
751         },
752         {
753                 .procname       = "nf_conntrack_sctp_timeout_heartbeat_acked",
754                 .maxlen         = sizeof(unsigned int),
755                 .mode           = 0644,
756                 .proc_handler   = proc_dointvec_jiffies,
757         },
758         { }
759 };
760 #endif
761
762 static int sctp_kmemdup_sysctl_table(struct nf_proto_net *pn,
763                                      struct nf_sctp_net *sn)
764 {
765 #ifdef CONFIG_SYSCTL
766         if (pn->ctl_table)
767                 return 0;
768
769         pn->ctl_table = kmemdup(sctp_sysctl_table,
770                                 sizeof(sctp_sysctl_table),
771                                 GFP_KERNEL);
772         if (!pn->ctl_table)
773                 return -ENOMEM;
774
775         pn->ctl_table[0].data = &sn->timeouts[SCTP_CONNTRACK_CLOSED];
776         pn->ctl_table[1].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_WAIT];
777         pn->ctl_table[2].data = &sn->timeouts[SCTP_CONNTRACK_COOKIE_ECHOED];
778         pn->ctl_table[3].data = &sn->timeouts[SCTP_CONNTRACK_ESTABLISHED];
779         pn->ctl_table[4].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT];
780         pn->ctl_table[5].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD];
781         pn->ctl_table[6].data = &sn->timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT];
782         pn->ctl_table[7].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_SENT];
783         pn->ctl_table[8].data = &sn->timeouts[SCTP_CONNTRACK_HEARTBEAT_ACKED];
784 #endif
785         return 0;
786 }
787
788 static int sctp_init_net(struct net *net, u_int16_t proto)
789 {
790         struct nf_sctp_net *sn = sctp_pernet(net);
791         struct nf_proto_net *pn = &sn->pn;
792
793         if (!pn->users) {
794                 int i;
795
796                 for (i = 0; i < SCTP_CONNTRACK_MAX; i++)
797                         sn->timeouts[i] = sctp_timeouts[i];
798
799                 /* timeouts[0] is unused, init it so ->timeouts[0] contains
800                  * 'new' timeout, like udp or icmp.
801                  */
802                 sn->timeouts[0] = sctp_timeouts[SCTP_CONNTRACK_CLOSED];
803         }
804
805         return sctp_kmemdup_sysctl_table(pn, sn);
806 }
807
808 static struct nf_proto_net *sctp_get_net_proto(struct net *net)
809 {
810         return &net->ct.nf_ct_proto.sctp.pn;
811 }
812
813 const struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 = {
814         .l3proto                = PF_INET,
815         .l4proto                = IPPROTO_SCTP,
816 #ifdef CONFIG_NF_CONNTRACK_PROCFS
817         .print_conntrack        = sctp_print_conntrack,
818 #endif
819         .packet                 = sctp_packet,
820         .new                    = sctp_new,
821         .error                  = sctp_error,
822         .can_early_drop         = sctp_can_early_drop,
823         .me                     = THIS_MODULE,
824 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
825         .nlattr_size            = SCTP_NLATTR_SIZE,
826         .to_nlattr              = sctp_to_nlattr,
827         .from_nlattr            = nlattr_to_sctp,
828         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
829         .nlattr_tuple_size      = nf_ct_port_nlattr_tuple_size,
830         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
831         .nla_policy             = nf_ct_port_nla_policy,
832 #endif
833 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
834         .ctnl_timeout           = {
835                 .nlattr_to_obj  = sctp_timeout_nlattr_to_obj,
836                 .obj_to_nlattr  = sctp_timeout_obj_to_nlattr,
837                 .nlattr_max     = CTA_TIMEOUT_SCTP_MAX,
838                 .obj_size       = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
839                 .nla_policy     = sctp_timeout_nla_policy,
840         },
841 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
842         .init_net               = sctp_init_net,
843         .get_net_proto          = sctp_get_net_proto,
844 };
845 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_sctp4);
846
847 const struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 = {
848         .l3proto                = PF_INET6,
849         .l4proto                = IPPROTO_SCTP,
850 #ifdef CONFIG_NF_CONNTRACK_PROCFS
851         .print_conntrack        = sctp_print_conntrack,
852 #endif
853         .packet                 = sctp_packet,
854         .new                    = sctp_new,
855         .error                  = sctp_error,
856         .can_early_drop         = sctp_can_early_drop,
857         .me                     = THIS_MODULE,
858 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
859         .nlattr_size            = SCTP_NLATTR_SIZE,
860         .to_nlattr              = sctp_to_nlattr,
861         .from_nlattr            = nlattr_to_sctp,
862         .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
863         .nlattr_tuple_size      = nf_ct_port_nlattr_tuple_size,
864         .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
865         .nla_policy             = nf_ct_port_nla_policy,
866 #endif
867 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
868         .ctnl_timeout           = {
869                 .nlattr_to_obj  = sctp_timeout_nlattr_to_obj,
870                 .obj_to_nlattr  = sctp_timeout_obj_to_nlattr,
871                 .nlattr_max     = CTA_TIMEOUT_SCTP_MAX,
872                 .obj_size       = sizeof(unsigned int) * SCTP_CONNTRACK_MAX,
873                 .nla_policy     = sctp_timeout_nla_policy,
874         },
875 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
876         .init_net               = sctp_init_net,
877         .get_net_proto          = sctp_get_net_proto,
878 };
879 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_sctp6);