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