1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
4 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
6 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/netlink.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/nf_tables.h>
15 #include <net/netfilter/nf_tables.h>
16 #include <net/netfilter/nf_conntrack.h>
17 #include <net/netfilter/nf_conntrack_acct.h>
18 #include <net/netfilter/nf_conntrack_tuple.h>
19 #include <net/netfilter/nf_conntrack_helper.h>
20 #include <net/netfilter/nf_conntrack_ecache.h>
21 #include <net/netfilter/nf_conntrack_labels.h>
22 #include <net/netfilter/nf_conntrack_timeout.h>
23 #include <net/netfilter/nf_conntrack_l4proto.h>
24 #include <net/netfilter/nf_conntrack_expect.h>
27 enum nft_ct_keys key
:8;
28 enum ip_conntrack_dir dir
:8;
30 enum nft_registers dreg
:8;
31 enum nft_registers sreg
:8;
35 struct nft_ct_helper_obj
{
36 struct nf_conntrack_helper
*helper4
;
37 struct nf_conntrack_helper
*helper6
;
41 #ifdef CONFIG_NF_CONNTRACK_ZONES
42 static DEFINE_PER_CPU(struct nf_conn
*, nft_ct_pcpu_template
);
43 static unsigned int nft_ct_pcpu_template_refcnt __read_mostly
;
46 static u64
nft_ct_get_eval_counter(const struct nf_conn_counter
*c
,
48 enum ip_conntrack_dir d
)
50 if (d
< IP_CT_DIR_MAX
)
51 return k
== NFT_CT_BYTES
? atomic64_read(&c
[d
].bytes
) :
52 atomic64_read(&c
[d
].packets
);
54 return nft_ct_get_eval_counter(c
, k
, IP_CT_DIR_ORIGINAL
) +
55 nft_ct_get_eval_counter(c
, k
, IP_CT_DIR_REPLY
);
58 static void nft_ct_get_eval(const struct nft_expr
*expr
,
59 struct nft_regs
*regs
,
60 const struct nft_pktinfo
*pkt
)
62 const struct nft_ct
*priv
= nft_expr_priv(expr
);
63 u32
*dest
= ®s
->data
[priv
->dreg
];
64 enum ip_conntrack_info ctinfo
;
65 const struct nf_conn
*ct
;
66 const struct nf_conn_help
*help
;
67 const struct nf_conntrack_tuple
*tuple
;
68 const struct nf_conntrack_helper
*helper
;
71 ct
= nf_ct_get(pkt
->skb
, &ctinfo
);
76 state
= NF_CT_STATE_BIT(ctinfo
);
77 else if (ctinfo
== IP_CT_UNTRACKED
)
78 state
= NF_CT_STATE_UNTRACKED_BIT
;
80 state
= NF_CT_STATE_INVALID_BIT
;
91 case NFT_CT_DIRECTION
:
92 nft_reg_store8(dest
, CTINFO2DIR(ctinfo
));
97 #ifdef CONFIG_NF_CONNTRACK_MARK
102 #ifdef CONFIG_NF_CONNTRACK_SECMARK
107 case NFT_CT_EXPIRATION
:
108 *dest
= jiffies_to_msecs(nf_ct_expires(ct
));
111 if (ct
->master
== NULL
)
113 help
= nfct_help(ct
->master
);
116 helper
= rcu_dereference(help
->helper
);
119 strncpy((char *)dest
, helper
->name
, NF_CT_HELPER_NAME_LEN
);
121 #ifdef CONFIG_NF_CONNTRACK_LABELS
122 case NFT_CT_LABELS
: {
123 struct nf_conn_labels
*labels
= nf_ct_labels_find(ct
);
126 memcpy(dest
, labels
->bits
, NF_CT_LABELS_MAX_SIZE
);
128 memset(dest
, 0, NF_CT_LABELS_MAX_SIZE
);
134 const struct nf_conn_acct
*acct
= nf_conn_acct_find(ct
);
138 count
= nft_ct_get_eval_counter(acct
->counter
,
139 priv
->key
, priv
->dir
);
140 memcpy(dest
, &count
, sizeof(count
));
143 case NFT_CT_AVGPKT
: {
144 const struct nf_conn_acct
*acct
= nf_conn_acct_find(ct
);
145 u64 avgcnt
= 0, bcnt
= 0, pcnt
= 0;
148 pcnt
= nft_ct_get_eval_counter(acct
->counter
,
149 NFT_CT_PKTS
, priv
->dir
);
150 bcnt
= nft_ct_get_eval_counter(acct
->counter
,
151 NFT_CT_BYTES
, priv
->dir
);
153 avgcnt
= div64_u64(bcnt
, pcnt
);
156 memcpy(dest
, &avgcnt
, sizeof(avgcnt
));
159 case NFT_CT_L3PROTOCOL
:
160 nft_reg_store8(dest
, nf_ct_l3num(ct
));
162 case NFT_CT_PROTOCOL
:
163 nft_reg_store8(dest
, nf_ct_protonum(ct
));
165 #ifdef CONFIG_NF_CONNTRACK_ZONES
167 const struct nf_conntrack_zone
*zone
= nf_ct_zone(ct
);
170 if (priv
->dir
< IP_CT_DIR_MAX
)
171 zoneid
= nf_ct_zone_id(zone
, priv
->dir
);
175 nft_reg_store16(dest
, zoneid
);
180 *dest
= nf_ct_get_id(ct
);
186 tuple
= &ct
->tuplehash
[priv
->dir
].tuple
;
189 memcpy(dest
, tuple
->src
.u3
.all
,
190 nf_ct_l3num(ct
) == NFPROTO_IPV4
? 4 : 16);
193 memcpy(dest
, tuple
->dst
.u3
.all
,
194 nf_ct_l3num(ct
) == NFPROTO_IPV4
? 4 : 16);
196 case NFT_CT_PROTO_SRC
:
197 nft_reg_store16(dest
, (__force u16
)tuple
->src
.u
.all
);
199 case NFT_CT_PROTO_DST
:
200 nft_reg_store16(dest
, (__force u16
)tuple
->dst
.u
.all
);
203 if (nf_ct_l3num(ct
) != NFPROTO_IPV4
)
205 *dest
= tuple
->src
.u3
.ip
;
208 if (nf_ct_l3num(ct
) != NFPROTO_IPV4
)
210 *dest
= tuple
->dst
.u3
.ip
;
213 if (nf_ct_l3num(ct
) != NFPROTO_IPV6
)
215 memcpy(dest
, tuple
->src
.u3
.ip6
, sizeof(struct in6_addr
));
218 if (nf_ct_l3num(ct
) != NFPROTO_IPV6
)
220 memcpy(dest
, tuple
->dst
.u3
.ip6
, sizeof(struct in6_addr
));
227 regs
->verdict
.code
= NFT_BREAK
;
230 #ifdef CONFIG_NF_CONNTRACK_ZONES
231 static void nft_ct_set_zone_eval(const struct nft_expr
*expr
,
232 struct nft_regs
*regs
,
233 const struct nft_pktinfo
*pkt
)
235 struct nf_conntrack_zone zone
= { .dir
= NF_CT_DEFAULT_ZONE_DIR
};
236 const struct nft_ct
*priv
= nft_expr_priv(expr
);
237 struct sk_buff
*skb
= pkt
->skb
;
238 enum ip_conntrack_info ctinfo
;
239 u16 value
= nft_reg_load16(®s
->data
[priv
->sreg
]);
242 ct
= nf_ct_get(skb
, &ctinfo
);
243 if (ct
) /* already tracked */
249 case IP_CT_DIR_ORIGINAL
:
250 zone
.dir
= NF_CT_ZONE_DIR_ORIG
;
252 case IP_CT_DIR_REPLY
:
253 zone
.dir
= NF_CT_ZONE_DIR_REPL
;
259 ct
= this_cpu_read(nft_ct_pcpu_template
);
261 if (likely(atomic_read(&ct
->ct_general
.use
) == 1)) {
262 nf_ct_zone_add(ct
, &zone
);
264 /* previous skb got queued to userspace */
265 ct
= nf_ct_tmpl_alloc(nft_net(pkt
), &zone
, GFP_ATOMIC
);
267 regs
->verdict
.code
= NF_DROP
;
272 atomic_inc(&ct
->ct_general
.use
);
273 nf_ct_set(skb
, ct
, IP_CT_NEW
);
277 static void nft_ct_set_eval(const struct nft_expr
*expr
,
278 struct nft_regs
*regs
,
279 const struct nft_pktinfo
*pkt
)
281 const struct nft_ct
*priv
= nft_expr_priv(expr
);
282 struct sk_buff
*skb
= pkt
->skb
;
283 #if defined(CONFIG_NF_CONNTRACK_MARK) || defined(CONFIG_NF_CONNTRACK_SECMARK)
284 u32 value
= regs
->data
[priv
->sreg
];
286 enum ip_conntrack_info ctinfo
;
289 ct
= nf_ct_get(skb
, &ctinfo
);
290 if (ct
== NULL
|| nf_ct_is_template(ct
))
294 #ifdef CONFIG_NF_CONNTRACK_MARK
296 if (ct
->mark
!= value
) {
298 nf_conntrack_event_cache(IPCT_MARK
, ct
);
302 #ifdef CONFIG_NF_CONNTRACK_SECMARK
304 if (ct
->secmark
!= value
) {
306 nf_conntrack_event_cache(IPCT_SECMARK
, ct
);
310 #ifdef CONFIG_NF_CONNTRACK_LABELS
312 nf_connlabels_replace(ct
,
313 ®s
->data
[priv
->sreg
],
314 ®s
->data
[priv
->sreg
],
315 NF_CT_LABELS_MAX_SIZE
/ sizeof(u32
));
318 #ifdef CONFIG_NF_CONNTRACK_EVENTS
319 case NFT_CT_EVENTMASK
: {
320 struct nf_conntrack_ecache
*e
= nf_ct_ecache_find(ct
);
321 u32 ctmask
= regs
->data
[priv
->sreg
];
324 if (e
->ctmask
!= ctmask
)
329 if (ctmask
&& !nf_ct_is_confirmed(ct
))
330 nf_ct_ecache_ext_add(ct
, ctmask
, 0, GFP_ATOMIC
);
339 static const struct nla_policy nft_ct_policy
[NFTA_CT_MAX
+ 1] = {
340 [NFTA_CT_DREG
] = { .type
= NLA_U32
},
341 [NFTA_CT_KEY
] = { .type
= NLA_U32
},
342 [NFTA_CT_DIRECTION
] = { .type
= NLA_U8
},
343 [NFTA_CT_SREG
] = { .type
= NLA_U32
},
346 #ifdef CONFIG_NF_CONNTRACK_ZONES
347 static void nft_ct_tmpl_put_pcpu(void)
352 for_each_possible_cpu(cpu
) {
353 ct
= per_cpu(nft_ct_pcpu_template
, cpu
);
357 per_cpu(nft_ct_pcpu_template
, cpu
) = NULL
;
361 static bool nft_ct_tmpl_alloc_pcpu(void)
363 struct nf_conntrack_zone zone
= { .id
= 0 };
367 if (nft_ct_pcpu_template_refcnt
)
370 for_each_possible_cpu(cpu
) {
371 tmp
= nf_ct_tmpl_alloc(&init_net
, &zone
, GFP_KERNEL
);
373 nft_ct_tmpl_put_pcpu();
377 atomic_set(&tmp
->ct_general
.use
, 1);
378 per_cpu(nft_ct_pcpu_template
, cpu
) = tmp
;
385 static int nft_ct_get_init(const struct nft_ctx
*ctx
,
386 const struct nft_expr
*expr
,
387 const struct nlattr
* const tb
[])
389 struct nft_ct
*priv
= nft_expr_priv(expr
);
393 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
394 priv
->dir
= IP_CT_DIR_MAX
;
396 case NFT_CT_DIRECTION
:
397 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
403 #ifdef CONFIG_NF_CONNTRACK_MARK
406 #ifdef CONFIG_NF_CONNTRACK_SECMARK
409 case NFT_CT_EXPIRATION
:
410 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
414 #ifdef CONFIG_NF_CONNTRACK_LABELS
416 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
418 len
= NF_CT_LABELS_MAX_SIZE
;
422 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
424 len
= NF_CT_HELPER_NAME_LEN
;
427 case NFT_CT_L3PROTOCOL
:
428 case NFT_CT_PROTOCOL
:
429 /* For compatibility, do not report error if NFTA_CT_DIRECTION
430 * attribute is specified.
436 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
439 switch (ctx
->family
) {
441 len
= sizeof_field(struct nf_conntrack_tuple
,
446 len
= sizeof_field(struct nf_conntrack_tuple
,
450 return -EAFNOSUPPORT
;
455 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
458 len
= sizeof_field(struct nf_conntrack_tuple
, src
.u3
.ip
);
462 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
465 len
= sizeof_field(struct nf_conntrack_tuple
, src
.u3
.ip6
);
467 case NFT_CT_PROTO_SRC
:
468 case NFT_CT_PROTO_DST
:
469 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
471 len
= sizeof_field(struct nf_conntrack_tuple
, src
.u
.all
);
478 #ifdef CONFIG_NF_CONNTRACK_ZONES
490 if (tb
[NFTA_CT_DIRECTION
] != NULL
) {
491 priv
->dir
= nla_get_u8(tb
[NFTA_CT_DIRECTION
]);
493 case IP_CT_DIR_ORIGINAL
:
494 case IP_CT_DIR_REPLY
:
501 priv
->dreg
= nft_parse_register(tb
[NFTA_CT_DREG
]);
502 err
= nft_validate_register_store(ctx
, priv
->dreg
, NULL
,
503 NFT_DATA_VALUE
, len
);
507 err
= nf_ct_netns_get(ctx
->net
, ctx
->family
);
511 if (priv
->key
== NFT_CT_BYTES
||
512 priv
->key
== NFT_CT_PKTS
||
513 priv
->key
== NFT_CT_AVGPKT
)
514 nf_ct_set_acct(ctx
->net
, true);
519 static void __nft_ct_set_destroy(const struct nft_ctx
*ctx
, struct nft_ct
*priv
)
522 #ifdef CONFIG_NF_CONNTRACK_LABELS
524 nf_connlabels_put(ctx
->net
);
527 #ifdef CONFIG_NF_CONNTRACK_ZONES
529 if (--nft_ct_pcpu_template_refcnt
== 0)
530 nft_ct_tmpl_put_pcpu();
537 static int nft_ct_set_init(const struct nft_ctx
*ctx
,
538 const struct nft_expr
*expr
,
539 const struct nlattr
* const tb
[])
541 struct nft_ct
*priv
= nft_expr_priv(expr
);
545 priv
->dir
= IP_CT_DIR_MAX
;
546 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
548 #ifdef CONFIG_NF_CONNTRACK_MARK
550 if (tb
[NFTA_CT_DIRECTION
])
552 len
= sizeof_field(struct nf_conn
, mark
);
555 #ifdef CONFIG_NF_CONNTRACK_LABELS
557 if (tb
[NFTA_CT_DIRECTION
])
559 len
= NF_CT_LABELS_MAX_SIZE
;
560 err
= nf_connlabels_get(ctx
->net
, (len
* BITS_PER_BYTE
) - 1);
565 #ifdef CONFIG_NF_CONNTRACK_ZONES
567 if (!nft_ct_tmpl_alloc_pcpu())
569 nft_ct_pcpu_template_refcnt
++;
573 #ifdef CONFIG_NF_CONNTRACK_EVENTS
574 case NFT_CT_EVENTMASK
:
575 if (tb
[NFTA_CT_DIRECTION
])
580 #ifdef CONFIG_NF_CONNTRACK_SECMARK
582 if (tb
[NFTA_CT_DIRECTION
])
591 if (tb
[NFTA_CT_DIRECTION
]) {
592 priv
->dir
= nla_get_u8(tb
[NFTA_CT_DIRECTION
]);
594 case IP_CT_DIR_ORIGINAL
:
595 case IP_CT_DIR_REPLY
:
603 priv
->sreg
= nft_parse_register(tb
[NFTA_CT_SREG
]);
604 err
= nft_validate_register_load(priv
->sreg
, len
);
608 err
= nf_ct_netns_get(ctx
->net
, ctx
->family
);
615 __nft_ct_set_destroy(ctx
, priv
);
619 static void nft_ct_get_destroy(const struct nft_ctx
*ctx
,
620 const struct nft_expr
*expr
)
622 nf_ct_netns_put(ctx
->net
, ctx
->family
);
625 static void nft_ct_set_destroy(const struct nft_ctx
*ctx
,
626 const struct nft_expr
*expr
)
628 struct nft_ct
*priv
= nft_expr_priv(expr
);
630 __nft_ct_set_destroy(ctx
, priv
);
631 nf_ct_netns_put(ctx
->net
, ctx
->family
);
634 static int nft_ct_get_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
636 const struct nft_ct
*priv
= nft_expr_priv(expr
);
638 if (nft_dump_register(skb
, NFTA_CT_DREG
, priv
->dreg
))
639 goto nla_put_failure
;
640 if (nla_put_be32(skb
, NFTA_CT_KEY
, htonl(priv
->key
)))
641 goto nla_put_failure
;
650 case NFT_CT_PROTO_SRC
:
651 case NFT_CT_PROTO_DST
:
652 if (nla_put_u8(skb
, NFTA_CT_DIRECTION
, priv
->dir
))
653 goto nla_put_failure
;
659 if (priv
->dir
< IP_CT_DIR_MAX
&&
660 nla_put_u8(skb
, NFTA_CT_DIRECTION
, priv
->dir
))
661 goto nla_put_failure
;
673 static int nft_ct_set_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
675 const struct nft_ct
*priv
= nft_expr_priv(expr
);
677 if (nft_dump_register(skb
, NFTA_CT_SREG
, priv
->sreg
))
678 goto nla_put_failure
;
679 if (nla_put_be32(skb
, NFTA_CT_KEY
, htonl(priv
->key
)))
680 goto nla_put_failure
;
684 if (priv
->dir
< IP_CT_DIR_MAX
&&
685 nla_put_u8(skb
, NFTA_CT_DIRECTION
, priv
->dir
))
686 goto nla_put_failure
;
698 static struct nft_expr_type nft_ct_type
;
699 static const struct nft_expr_ops nft_ct_get_ops
= {
700 .type
= &nft_ct_type
,
701 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
702 .eval
= nft_ct_get_eval
,
703 .init
= nft_ct_get_init
,
704 .destroy
= nft_ct_get_destroy
,
705 .dump
= nft_ct_get_dump
,
708 static const struct nft_expr_ops nft_ct_set_ops
= {
709 .type
= &nft_ct_type
,
710 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
711 .eval
= nft_ct_set_eval
,
712 .init
= nft_ct_set_init
,
713 .destroy
= nft_ct_set_destroy
,
714 .dump
= nft_ct_set_dump
,
717 #ifdef CONFIG_NF_CONNTRACK_ZONES
718 static const struct nft_expr_ops nft_ct_set_zone_ops
= {
719 .type
= &nft_ct_type
,
720 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
721 .eval
= nft_ct_set_zone_eval
,
722 .init
= nft_ct_set_init
,
723 .destroy
= nft_ct_set_destroy
,
724 .dump
= nft_ct_set_dump
,
728 static const struct nft_expr_ops
*
729 nft_ct_select_ops(const struct nft_ctx
*ctx
,
730 const struct nlattr
* const tb
[])
732 if (tb
[NFTA_CT_KEY
] == NULL
)
733 return ERR_PTR(-EINVAL
);
735 if (tb
[NFTA_CT_DREG
] && tb
[NFTA_CT_SREG
])
736 return ERR_PTR(-EINVAL
);
738 if (tb
[NFTA_CT_DREG
])
739 return &nft_ct_get_ops
;
741 if (tb
[NFTA_CT_SREG
]) {
742 #ifdef CONFIG_NF_CONNTRACK_ZONES
743 if (nla_get_be32(tb
[NFTA_CT_KEY
]) == htonl(NFT_CT_ZONE
))
744 return &nft_ct_set_zone_ops
;
746 return &nft_ct_set_ops
;
749 return ERR_PTR(-EINVAL
);
752 static struct nft_expr_type nft_ct_type __read_mostly
= {
754 .select_ops
= nft_ct_select_ops
,
755 .policy
= nft_ct_policy
,
756 .maxattr
= NFTA_CT_MAX
,
757 .owner
= THIS_MODULE
,
760 static void nft_notrack_eval(const struct nft_expr
*expr
,
761 struct nft_regs
*regs
,
762 const struct nft_pktinfo
*pkt
)
764 struct sk_buff
*skb
= pkt
->skb
;
765 enum ip_conntrack_info ctinfo
;
768 ct
= nf_ct_get(pkt
->skb
, &ctinfo
);
769 /* Previously seen (loopback or untracked)? Ignore. */
770 if (ct
|| ctinfo
== IP_CT_UNTRACKED
)
773 nf_ct_set(skb
, ct
, IP_CT_UNTRACKED
);
776 static struct nft_expr_type nft_notrack_type
;
777 static const struct nft_expr_ops nft_notrack_ops
= {
778 .type
= &nft_notrack_type
,
779 .size
= NFT_EXPR_SIZE(0),
780 .eval
= nft_notrack_eval
,
783 static struct nft_expr_type nft_notrack_type __read_mostly
= {
785 .ops
= &nft_notrack_ops
,
786 .owner
= THIS_MODULE
,
789 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
791 nft_ct_timeout_parse_policy(void *timeouts
,
792 const struct nf_conntrack_l4proto
*l4proto
,
793 struct net
*net
, const struct nlattr
*attr
)
798 tb
= kcalloc(l4proto
->ctnl_timeout
.nlattr_max
+ 1, sizeof(*tb
),
804 ret
= nla_parse_nested_deprecated(tb
,
805 l4proto
->ctnl_timeout
.nlattr_max
,
807 l4proto
->ctnl_timeout
.nla_policy
,
812 ret
= l4proto
->ctnl_timeout
.nlattr_to_obj(tb
, net
, timeouts
);
819 struct nft_ct_timeout_obj
{
820 struct nf_ct_timeout
*timeout
;
824 static void nft_ct_timeout_obj_eval(struct nft_object
*obj
,
825 struct nft_regs
*regs
,
826 const struct nft_pktinfo
*pkt
)
828 const struct nft_ct_timeout_obj
*priv
= nft_obj_data(obj
);
829 struct nf_conn
*ct
= (struct nf_conn
*)skb_nfct(pkt
->skb
);
830 struct nf_conn_timeout
*timeout
;
831 const unsigned int *values
;
833 if (priv
->l4proto
!= pkt
->tprot
)
836 if (!ct
|| nf_ct_is_template(ct
) || nf_ct_is_confirmed(ct
))
839 timeout
= nf_ct_timeout_find(ct
);
841 timeout
= nf_ct_timeout_ext_add(ct
, priv
->timeout
, GFP_ATOMIC
);
843 regs
->verdict
.code
= NF_DROP
;
848 rcu_assign_pointer(timeout
->timeout
, priv
->timeout
);
850 /* adjust the timeout as per 'new' state. ct is unconfirmed,
851 * so the current timestamp must not be added.
853 values
= nf_ct_timeout_data(timeout
);
855 nf_ct_refresh(ct
, pkt
->skb
, values
[0]);
858 static int nft_ct_timeout_obj_init(const struct nft_ctx
*ctx
,
859 const struct nlattr
* const tb
[],
860 struct nft_object
*obj
)
862 struct nft_ct_timeout_obj
*priv
= nft_obj_data(obj
);
863 const struct nf_conntrack_l4proto
*l4proto
;
864 struct nf_ct_timeout
*timeout
;
865 int l3num
= ctx
->family
;
869 if (!tb
[NFTA_CT_TIMEOUT_L4PROTO
] ||
870 !tb
[NFTA_CT_TIMEOUT_DATA
])
873 if (tb
[NFTA_CT_TIMEOUT_L3PROTO
])
874 l3num
= ntohs(nla_get_be16(tb
[NFTA_CT_TIMEOUT_L3PROTO
]));
876 l4num
= nla_get_u8(tb
[NFTA_CT_TIMEOUT_L4PROTO
]);
877 priv
->l4proto
= l4num
;
879 l4proto
= nf_ct_l4proto_find(l4num
);
881 if (l4proto
->l4proto
!= l4num
) {
886 timeout
= kzalloc(sizeof(struct nf_ct_timeout
) +
887 l4proto
->ctnl_timeout
.obj_size
, GFP_KERNEL
);
888 if (timeout
== NULL
) {
893 ret
= nft_ct_timeout_parse_policy(&timeout
->data
, l4proto
, ctx
->net
,
894 tb
[NFTA_CT_TIMEOUT_DATA
]);
896 goto err_free_timeout
;
898 timeout
->l3num
= l3num
;
899 timeout
->l4proto
= l4proto
;
901 ret
= nf_ct_netns_get(ctx
->net
, ctx
->family
);
903 goto err_free_timeout
;
905 priv
->timeout
= timeout
;
914 static void nft_ct_timeout_obj_destroy(const struct nft_ctx
*ctx
,
915 struct nft_object
*obj
)
917 struct nft_ct_timeout_obj
*priv
= nft_obj_data(obj
);
918 struct nf_ct_timeout
*timeout
= priv
->timeout
;
920 nf_ct_untimeout(ctx
->net
, timeout
);
921 nf_ct_netns_put(ctx
->net
, ctx
->family
);
922 kfree(priv
->timeout
);
925 static int nft_ct_timeout_obj_dump(struct sk_buff
*skb
,
926 struct nft_object
*obj
, bool reset
)
928 const struct nft_ct_timeout_obj
*priv
= nft_obj_data(obj
);
929 const struct nf_ct_timeout
*timeout
= priv
->timeout
;
930 struct nlattr
*nest_params
;
933 if (nla_put_u8(skb
, NFTA_CT_TIMEOUT_L4PROTO
, timeout
->l4proto
->l4proto
) ||
934 nla_put_be16(skb
, NFTA_CT_TIMEOUT_L3PROTO
, htons(timeout
->l3num
)))
937 nest_params
= nla_nest_start(skb
, NFTA_CT_TIMEOUT_DATA
);
941 ret
= timeout
->l4proto
->ctnl_timeout
.obj_to_nlattr(skb
, &timeout
->data
);
944 nla_nest_end(skb
, nest_params
);
948 static const struct nla_policy nft_ct_timeout_policy
[NFTA_CT_TIMEOUT_MAX
+ 1] = {
949 [NFTA_CT_TIMEOUT_L3PROTO
] = {.type
= NLA_U16
},
950 [NFTA_CT_TIMEOUT_L4PROTO
] = {.type
= NLA_U8
},
951 [NFTA_CT_TIMEOUT_DATA
] = {.type
= NLA_NESTED
},
954 static struct nft_object_type nft_ct_timeout_obj_type
;
956 static const struct nft_object_ops nft_ct_timeout_obj_ops
= {
957 .type
= &nft_ct_timeout_obj_type
,
958 .size
= sizeof(struct nft_ct_timeout_obj
),
959 .eval
= nft_ct_timeout_obj_eval
,
960 .init
= nft_ct_timeout_obj_init
,
961 .destroy
= nft_ct_timeout_obj_destroy
,
962 .dump
= nft_ct_timeout_obj_dump
,
965 static struct nft_object_type nft_ct_timeout_obj_type __read_mostly
= {
966 .type
= NFT_OBJECT_CT_TIMEOUT
,
967 .ops
= &nft_ct_timeout_obj_ops
,
968 .maxattr
= NFTA_CT_TIMEOUT_MAX
,
969 .policy
= nft_ct_timeout_policy
,
970 .owner
= THIS_MODULE
,
972 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
974 static int nft_ct_helper_obj_init(const struct nft_ctx
*ctx
,
975 const struct nlattr
* const tb
[],
976 struct nft_object
*obj
)
978 struct nft_ct_helper_obj
*priv
= nft_obj_data(obj
);
979 struct nf_conntrack_helper
*help4
, *help6
;
980 char name
[NF_CT_HELPER_NAME_LEN
];
981 int family
= ctx
->family
;
984 if (!tb
[NFTA_CT_HELPER_NAME
] || !tb
[NFTA_CT_HELPER_L4PROTO
])
987 priv
->l4proto
= nla_get_u8(tb
[NFTA_CT_HELPER_L4PROTO
]);
991 nla_strscpy(name
, tb
[NFTA_CT_HELPER_NAME
], sizeof(name
));
993 if (tb
[NFTA_CT_HELPER_L3PROTO
])
994 family
= ntohs(nla_get_be16(tb
[NFTA_CT_HELPER_L3PROTO
]));
1001 if (ctx
->family
== NFPROTO_IPV6
)
1004 help4
= nf_conntrack_helper_try_module_get(name
, family
,
1008 if (ctx
->family
== NFPROTO_IPV4
)
1011 help6
= nf_conntrack_helper_try_module_get(name
, family
,
1014 case NFPROTO_NETDEV
:
1015 case NFPROTO_BRIDGE
:
1017 help4
= nf_conntrack_helper_try_module_get(name
, NFPROTO_IPV4
,
1019 help6
= nf_conntrack_helper_try_module_get(name
, NFPROTO_IPV6
,
1023 return -EAFNOSUPPORT
;
1026 /* && is intentional; only error if INET found neither ipv4 or ipv6 */
1027 if (!help4
&& !help6
)
1030 priv
->helper4
= help4
;
1031 priv
->helper6
= help6
;
1033 err
= nf_ct_netns_get(ctx
->net
, ctx
->family
);
1035 goto err_put_helper
;
1041 nf_conntrack_helper_put(priv
->helper4
);
1043 nf_conntrack_helper_put(priv
->helper6
);
1047 static void nft_ct_helper_obj_destroy(const struct nft_ctx
*ctx
,
1048 struct nft_object
*obj
)
1050 struct nft_ct_helper_obj
*priv
= nft_obj_data(obj
);
1053 nf_conntrack_helper_put(priv
->helper4
);
1055 nf_conntrack_helper_put(priv
->helper6
);
1057 nf_ct_netns_put(ctx
->net
, ctx
->family
);
1060 static void nft_ct_helper_obj_eval(struct nft_object
*obj
,
1061 struct nft_regs
*regs
,
1062 const struct nft_pktinfo
*pkt
)
1064 const struct nft_ct_helper_obj
*priv
= nft_obj_data(obj
);
1065 struct nf_conn
*ct
= (struct nf_conn
*)skb_nfct(pkt
->skb
);
1066 struct nf_conntrack_helper
*to_assign
= NULL
;
1067 struct nf_conn_help
*help
;
1070 nf_ct_is_confirmed(ct
) ||
1071 nf_ct_is_template(ct
) ||
1072 priv
->l4proto
!= nf_ct_protonum(ct
))
1075 switch (nf_ct_l3num(ct
)) {
1077 to_assign
= priv
->helper4
;
1080 to_assign
= priv
->helper6
;
1090 if (test_bit(IPS_HELPER_BIT
, &ct
->status
))
1093 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1095 rcu_assign_pointer(help
->helper
, to_assign
);
1096 set_bit(IPS_HELPER_BIT
, &ct
->status
);
1100 static int nft_ct_helper_obj_dump(struct sk_buff
*skb
,
1101 struct nft_object
*obj
, bool reset
)
1103 const struct nft_ct_helper_obj
*priv
= nft_obj_data(obj
);
1104 const struct nf_conntrack_helper
*helper
;
1107 if (priv
->helper4
&& priv
->helper6
) {
1108 family
= NFPROTO_INET
;
1109 helper
= priv
->helper4
;
1110 } else if (priv
->helper6
) {
1111 family
= NFPROTO_IPV6
;
1112 helper
= priv
->helper6
;
1114 family
= NFPROTO_IPV4
;
1115 helper
= priv
->helper4
;
1118 if (nla_put_string(skb
, NFTA_CT_HELPER_NAME
, helper
->name
))
1121 if (nla_put_u8(skb
, NFTA_CT_HELPER_L4PROTO
, priv
->l4proto
))
1124 if (nla_put_be16(skb
, NFTA_CT_HELPER_L3PROTO
, htons(family
)))
1130 static const struct nla_policy nft_ct_helper_policy
[NFTA_CT_HELPER_MAX
+ 1] = {
1131 [NFTA_CT_HELPER_NAME
] = { .type
= NLA_STRING
,
1132 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
1133 [NFTA_CT_HELPER_L3PROTO
] = { .type
= NLA_U16
},
1134 [NFTA_CT_HELPER_L4PROTO
] = { .type
= NLA_U8
},
1137 static struct nft_object_type nft_ct_helper_obj_type
;
1138 static const struct nft_object_ops nft_ct_helper_obj_ops
= {
1139 .type
= &nft_ct_helper_obj_type
,
1140 .size
= sizeof(struct nft_ct_helper_obj
),
1141 .eval
= nft_ct_helper_obj_eval
,
1142 .init
= nft_ct_helper_obj_init
,
1143 .destroy
= nft_ct_helper_obj_destroy
,
1144 .dump
= nft_ct_helper_obj_dump
,
1147 static struct nft_object_type nft_ct_helper_obj_type __read_mostly
= {
1148 .type
= NFT_OBJECT_CT_HELPER
,
1149 .ops
= &nft_ct_helper_obj_ops
,
1150 .maxattr
= NFTA_CT_HELPER_MAX
,
1151 .policy
= nft_ct_helper_policy
,
1152 .owner
= THIS_MODULE
,
1155 struct nft_ct_expect_obj
{
1163 static int nft_ct_expect_obj_init(const struct nft_ctx
*ctx
,
1164 const struct nlattr
* const tb
[],
1165 struct nft_object
*obj
)
1167 struct nft_ct_expect_obj
*priv
= nft_obj_data(obj
);
1169 if (!tb
[NFTA_CT_EXPECT_L4PROTO
] ||
1170 !tb
[NFTA_CT_EXPECT_DPORT
] ||
1171 !tb
[NFTA_CT_EXPECT_TIMEOUT
] ||
1172 !tb
[NFTA_CT_EXPECT_SIZE
])
1175 priv
->l3num
= ctx
->family
;
1176 if (tb
[NFTA_CT_EXPECT_L3PROTO
])
1177 priv
->l3num
= ntohs(nla_get_be16(tb
[NFTA_CT_EXPECT_L3PROTO
]));
1179 priv
->l4proto
= nla_get_u8(tb
[NFTA_CT_EXPECT_L4PROTO
]);
1180 priv
->dport
= nla_get_be16(tb
[NFTA_CT_EXPECT_DPORT
]);
1181 priv
->timeout
= nla_get_u32(tb
[NFTA_CT_EXPECT_TIMEOUT
]);
1182 priv
->size
= nla_get_u8(tb
[NFTA_CT_EXPECT_SIZE
]);
1184 return nf_ct_netns_get(ctx
->net
, ctx
->family
);
1187 static void nft_ct_expect_obj_destroy(const struct nft_ctx
*ctx
,
1188 struct nft_object
*obj
)
1190 nf_ct_netns_put(ctx
->net
, ctx
->family
);
1193 static int nft_ct_expect_obj_dump(struct sk_buff
*skb
,
1194 struct nft_object
*obj
, bool reset
)
1196 const struct nft_ct_expect_obj
*priv
= nft_obj_data(obj
);
1198 if (nla_put_be16(skb
, NFTA_CT_EXPECT_L3PROTO
, htons(priv
->l3num
)) ||
1199 nla_put_u8(skb
, NFTA_CT_EXPECT_L4PROTO
, priv
->l4proto
) ||
1200 nla_put_be16(skb
, NFTA_CT_EXPECT_DPORT
, priv
->dport
) ||
1201 nla_put_u32(skb
, NFTA_CT_EXPECT_TIMEOUT
, priv
->timeout
) ||
1202 nla_put_u8(skb
, NFTA_CT_EXPECT_SIZE
, priv
->size
))
1208 static void nft_ct_expect_obj_eval(struct nft_object
*obj
,
1209 struct nft_regs
*regs
,
1210 const struct nft_pktinfo
*pkt
)
1212 const struct nft_ct_expect_obj
*priv
= nft_obj_data(obj
);
1213 struct nf_conntrack_expect
*exp
;
1214 enum ip_conntrack_info ctinfo
;
1215 struct nf_conn_help
*help
;
1216 enum ip_conntrack_dir dir
;
1217 u16 l3num
= priv
->l3num
;
1220 ct
= nf_ct_get(pkt
->skb
, &ctinfo
);
1221 if (!ct
|| ctinfo
== IP_CT_UNTRACKED
) {
1222 regs
->verdict
.code
= NFT_BREAK
;
1225 dir
= CTINFO2DIR(ctinfo
);
1227 help
= nfct_help(ct
);
1229 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1231 regs
->verdict
.code
= NF_DROP
;
1235 if (help
->expecting
[NF_CT_EXPECT_CLASS_DEFAULT
] >= priv
->size
) {
1236 regs
->verdict
.code
= NFT_BREAK
;
1239 if (l3num
== NFPROTO_INET
)
1240 l3num
= nf_ct_l3num(ct
);
1242 exp
= nf_ct_expect_alloc(ct
);
1244 regs
->verdict
.code
= NF_DROP
;
1247 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, l3num
,
1248 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
1249 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
1250 priv
->l4proto
, NULL
, &priv
->dport
);
1251 exp
->timeout
.expires
= jiffies
+ priv
->timeout
* HZ
;
1253 if (nf_ct_expect_related(exp
, 0) != 0)
1254 regs
->verdict
.code
= NF_DROP
;
1257 static const struct nla_policy nft_ct_expect_policy
[NFTA_CT_EXPECT_MAX
+ 1] = {
1258 [NFTA_CT_EXPECT_L3PROTO
] = { .type
= NLA_U16
},
1259 [NFTA_CT_EXPECT_L4PROTO
] = { .type
= NLA_U8
},
1260 [NFTA_CT_EXPECT_DPORT
] = { .type
= NLA_U16
},
1261 [NFTA_CT_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1262 [NFTA_CT_EXPECT_SIZE
] = { .type
= NLA_U8
},
1265 static struct nft_object_type nft_ct_expect_obj_type
;
1267 static const struct nft_object_ops nft_ct_expect_obj_ops
= {
1268 .type
= &nft_ct_expect_obj_type
,
1269 .size
= sizeof(struct nft_ct_expect_obj
),
1270 .eval
= nft_ct_expect_obj_eval
,
1271 .init
= nft_ct_expect_obj_init
,
1272 .destroy
= nft_ct_expect_obj_destroy
,
1273 .dump
= nft_ct_expect_obj_dump
,
1276 static struct nft_object_type nft_ct_expect_obj_type __read_mostly
= {
1277 .type
= NFT_OBJECT_CT_EXPECT
,
1278 .ops
= &nft_ct_expect_obj_ops
,
1279 .maxattr
= NFTA_CT_EXPECT_MAX
,
1280 .policy
= nft_ct_expect_policy
,
1281 .owner
= THIS_MODULE
,
1284 static int __init
nft_ct_module_init(void)
1288 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE
> NFT_REG_SIZE
);
1290 err
= nft_register_expr(&nft_ct_type
);
1294 err
= nft_register_expr(&nft_notrack_type
);
1298 err
= nft_register_obj(&nft_ct_helper_obj_type
);
1302 err
= nft_register_obj(&nft_ct_expect_obj_type
);
1305 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1306 err
= nft_register_obj(&nft_ct_timeout_obj_type
);
1312 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1314 nft_unregister_obj(&nft_ct_expect_obj_type
);
1317 nft_unregister_obj(&nft_ct_helper_obj_type
);
1319 nft_unregister_expr(&nft_notrack_type
);
1321 nft_unregister_expr(&nft_ct_type
);
1325 static void __exit
nft_ct_module_exit(void)
1327 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1328 nft_unregister_obj(&nft_ct_timeout_obj_type
);
1330 nft_unregister_obj(&nft_ct_expect_obj_type
);
1331 nft_unregister_obj(&nft_ct_helper_obj_type
);
1332 nft_unregister_expr(&nft_notrack_type
);
1333 nft_unregister_expr(&nft_ct_type
);
1336 module_init(nft_ct_module_init
);
1337 module_exit(nft_ct_module_exit
);
1339 MODULE_LICENSE("GPL");
1340 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1341 MODULE_ALIAS_NFT_EXPR("ct");
1342 MODULE_ALIAS_NFT_EXPR("notrack");
1343 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_HELPER
);
1344 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_TIMEOUT
);
1345 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_EXPECT
);
1346 MODULE_DESCRIPTION("Netfilter nf_tables conntrack module");