12 #include <linux/types.h>
14 #include <linux/netfilter.h>
15 #include <linux/module.h>
19 #include <linux/string.h>
33 static const char *
const sctp_conntrack_names[] = {
45 #define MINS * 60 SECS
46 #define HOURS * 60 MINS
47 #define DAYS * 24 HOURS
59 #define sNO SCTP_CONNTRACK_NONE
60 #define sCL SCTP_CONNTRACK_CLOSED
61 #define sCW SCTP_CONNTRACK_COOKIE_WAIT
62 #define sCE SCTP_CONNTRACK_COOKIE_ECHOED
63 #define sES SCTP_CONNTRACK_ESTABLISHED
64 #define sSS SCTP_CONNTRACK_SHUTDOWN_SENT
65 #define sSR SCTP_CONNTRACK_SHUTDOWN_RECD
66 #define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT
67 #define sIV SCTP_CONNTRACK_MAX
141 static bool sctp_pkt_to_tuple(
const struct sk_buff *
skb,
unsigned int dataoff,
148 hp = skb_header_pointer(skb, dataoff, 8, &_hdr);
152 tuple->
src.u.sctp.port = hp->source;
153 tuple->
dst.u.sctp.port = hp->dest;
160 tuple->
src.u.sctp.port = orig->
dst.u.sctp.port;
161 tuple->
dst.u.sctp.port = orig->
src.u.sctp.port;
166 static int sctp_print_tuple(
struct seq_file *
s,
179 spin_lock_bh(&ct->
lock);
181 spin_unlock_bh(&ct->
lock);
183 return seq_printf(s,
"%s ", sctp_conntrack_names[state]);
186 #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \
187 for ((offset) = (dataoff) + sizeof(sctp_sctphdr_t), (count) = 0; \
188 (offset) < (skb)->len && \
189 ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \
190 (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++)
193 static int do_basic_checks(
struct nf_conn *
ct,
195 unsigned int dataoff,
220 count != 0) || !sch->
length) {
239 pr_debug(
"Chunk type: %d\n", chunk_type);
241 switch (chunk_type) {
259 pr_debug(
"SCTP_CID_SHUTDOWN_ACK\n");
275 pr_debug(
"SCTP_CID_SHUTDOWN_COMPLETE\n");
281 pr_debug(
"Unknown chunk type, Will stay in %s\n",
282 sctp_conntrack_names[cur_state]);
286 pr_debug(
"dir: %d cur_state: %s chunk_type: %d new_state: %s\n",
287 dir, sctp_conntrack_names[cur_state], chunk_type,
288 sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]);
290 return sctp_conntracks[dir][
i][cur_state];
293 static unsigned int *sctp_get_timeouts(
struct net *
net)
295 return sctp_pernet(net)->timeouts;
301 unsigned int dataoff,
305 unsigned int *timeouts)
314 unsigned long map[256 /
sizeof(
unsigned long)] = { 0 };
316 sh = skb_header_pointer(skb, dataoff,
sizeof(_sctph), &_sctph);
320 if (do_basic_checks(ct, skb, dataoff,
map) != 0)
330 pr_debug(
"Verification tag check failed\n");
335 spin_lock_bh(&ct->
lock);
360 new_state = sctp_new_state(dir, old_state, sch->
type);
364 pr_debug(
"nf_conntrack_sctp: Invalid dir=%i ctype=%u "
366 dir, sch->
type, old_state);
376 sizeof(_inithdr), &_inithdr);
379 pr_debug(
"Setting vtag %x for dir %d\n",
385 if (old_state != new_state)
388 spin_unlock_bh(&ct->
lock);
390 nf_ct_refresh_acct(ct, ctinfo, skb, timeouts[new_state]);
403 spin_unlock_bh(&ct->
lock);
409 static bool sctp_new(
struct nf_conn *ct,
const struct sk_buff *skb,
410 unsigned int dataoff,
unsigned int *timeouts)
418 unsigned long map[256 /
sizeof(
unsigned long)] = { 0 };
420 sh = skb_header_pointer(skb, dataoff,
sizeof(_sctph), &_sctph);
424 if (do_basic_checks(ct, skb, dataoff,
map) != 0)
443 pr_debug(
"nf_conntrack_sctp: invalid new deleting.\n");
453 sizeof(_inithdr), &_inithdr);
457 pr_debug(
"Setting vtag %x for new conn\n",
470 pr_debug(
"Setting vtag %x for new conn OOTB\n",
481 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
483 #include <linux/netfilter/nfnetlink.h>
489 struct nlattr *nest_parms;
491 spin_lock_bh(&ct->
lock);
494 goto nla_put_failure;
501 goto nla_put_failure;
503 spin_unlock_bh(&ct->
lock);
505 nla_nest_end(skb, nest_parms);
510 spin_unlock_bh(&ct->
lock);
530 err = nla_parse_nested(tb,
542 spin_lock_bh(&ct->
lock);
547 nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]);
548 spin_unlock_bh(&ct->
lock);
553 static int sctp_nlattr_size(
void)
555 return nla_total_size(0)
560 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
562 #include <linux/netfilter/nfnetlink.h>
565 static int sctp_timeout_nlattr_to_obj(
struct nlattr *tb[],
568 unsigned int *timeouts =
data;
579 timeouts[
i] =
ntohl(nla_get_be32(tb[i])) *
HZ;
586 sctp_timeout_obj_to_nlattr(
struct sk_buff *skb,
const void *data)
588 const unsigned int *timeouts =
data;
592 if (nla_put_be32(skb, i,
htonl(timeouts[i] /
HZ)))
593 goto nla_put_failure;
602 sctp_timeout_nla_policy[CTA_TIMEOUT_SCTP_MAX+1] = {
615 static struct ctl_table sctp_sysctl_table[] = {
617 .
procname =
"nf_conntrack_sctp_timeout_closed",
618 .maxlen =
sizeof(
unsigned int),
623 .procname =
"nf_conntrack_sctp_timeout_cookie_wait",
624 .maxlen =
sizeof(
unsigned int),
629 .procname =
"nf_conntrack_sctp_timeout_cookie_echoed",
630 .maxlen =
sizeof(
unsigned int),
635 .procname =
"nf_conntrack_sctp_timeout_established",
636 .maxlen =
sizeof(
unsigned int),
641 .procname =
"nf_conntrack_sctp_timeout_shutdown_sent",
642 .maxlen =
sizeof(
unsigned int),
647 .procname =
"nf_conntrack_sctp_timeout_shutdown_recd",
648 .maxlen =
sizeof(
unsigned int),
653 .procname =
"nf_conntrack_sctp_timeout_shutdown_ack_sent",
654 .maxlen =
sizeof(
unsigned int),
661 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
662 static struct ctl_table sctp_compat_sysctl_table[] = {
664 .
procname =
"ip_conntrack_sctp_timeout_closed",
665 .maxlen =
sizeof(
unsigned int),
670 .procname =
"ip_conntrack_sctp_timeout_cookie_wait",
671 .maxlen =
sizeof(
unsigned int),
676 .procname =
"ip_conntrack_sctp_timeout_cookie_echoed",
677 .maxlen =
sizeof(
unsigned int),
682 .procname =
"ip_conntrack_sctp_timeout_established",
683 .maxlen =
sizeof(
unsigned int),
688 .procname =
"ip_conntrack_sctp_timeout_shutdown_sent",
689 .maxlen =
sizeof(
unsigned int),
694 .procname =
"ip_conntrack_sctp_timeout_shutdown_recd",
695 .maxlen =
sizeof(
unsigned int),
700 .procname =
"ip_conntrack_sctp_timeout_shutdown_ack_sent",
701 .maxlen =
sizeof(
unsigned int),
717 pn->ctl_table =
kmemdup(sctp_sysctl_table,
718 sizeof(sctp_sysctl_table),
734 static int sctp_kmemdup_compat_sysctl_table(
struct nf_proto_net *pn,
738 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
739 pn->ctl_compat_table =
kmemdup(sctp_compat_sysctl_table,
740 sizeof(sctp_compat_sysctl_table),
742 if (!pn->ctl_compat_table)
760 struct sctp_net *sn = sctp_pernet(net);
771 ret = sctp_kmemdup_compat_sysctl_table(pn, sn);
775 ret = sctp_kmemdup_sysctl_table(pn, sn);
777 nf_ct_kfree_compat_sysctl_table(pn);
779 ret = sctp_kmemdup_sysctl_table(pn, sn);
788 .pkt_to_tuple = sctp_pkt_to_tuple,
789 .invert_tuple = sctp_invert_tuple,
790 .print_tuple = sctp_print_tuple,
791 .print_conntrack = sctp_print_conntrack,
793 .get_timeouts = sctp_get_timeouts,
796 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
797 .to_nlattr = sctp_to_nlattr,
798 .nlattr_size = sctp_nlattr_size,
799 .from_nlattr = nlattr_to_sctp,
805 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
807 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
808 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
810 .obj_size =
sizeof(
unsigned int) * SCTP_CONNTRACK_MAX,
822 .pkt_to_tuple = sctp_pkt_to_tuple,
823 .invert_tuple = sctp_invert_tuple,
824 .print_tuple = sctp_print_tuple,
825 .print_conntrack = sctp_print_conntrack,
827 .get_timeouts = sctp_get_timeouts,
830 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
831 .to_nlattr = sctp_to_nlattr,
832 .nlattr_size = sctp_nlattr_size,
833 .from_nlattr = nlattr_to_sctp,
838 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
840 .nlattr_to_obj = sctp_timeout_nlattr_to_obj,
841 .obj_to_nlattr = sctp_timeout_obj_to_nlattr,
843 .obj_size =
sizeof(
unsigned int) * SCTP_CONNTRACK_MAX,
852 static int sctp_net_init(
struct net *net)
857 &nf_conntrack_l4proto_sctp4);
859 pr_err(
"nf_conntrack_l4proto_sctp4 :protocol register failed.\n");
863 &nf_conntrack_l4proto_sctp6);
865 pr_err(
"nf_conntrack_l4proto_sctp6 :protocol register failed.\n");
872 &nf_conntrack_l4proto_sctp4);
877 static void sctp_net_exit(
struct net *net)
880 &nf_conntrack_l4proto_sctp6);
882 &nf_conntrack_l4proto_sctp4);
886 .init = sctp_net_init,
887 .exit = sctp_net_exit,
892 static int __init nf_conntrack_proto_sctp_init(
void)
897 static void __exit nf_conntrack_proto_sctp_fini(
void)