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