1 // SPDX-License-Identifier: GPL-2.0-only
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_core.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>
26 struct nft_ct_helper_obj {
27 struct nf_conntrack_helper *helper4;
28 struct nf_conntrack_helper *helper6;
32 #ifdef CONFIG_NF_CONNTRACK_ZONES
33 static DEFINE_PER_CPU(struct nf_conn *, nft_ct_pcpu_template);
34 static unsigned int nft_ct_pcpu_template_refcnt __read_mostly;
35 static DEFINE_MUTEX(nft_ct_pcpu_mutex);
38 static u64 nft_ct_get_eval_counter(const struct nf_conn_counter *c,
40 enum ip_conntrack_dir d)
42 if (d < IP_CT_DIR_MAX)
43 return k == NFT_CT_BYTES ? atomic64_read(&c[d].bytes) :
44 atomic64_read(&c[d].packets);
46 return nft_ct_get_eval_counter(c, k, IP_CT_DIR_ORIGINAL) +
47 nft_ct_get_eval_counter(c, k, IP_CT_DIR_REPLY);
50 static void nft_ct_get_eval(const struct nft_expr *expr,
51 struct nft_regs *regs,
52 const struct nft_pktinfo *pkt)
54 const struct nft_ct *priv = nft_expr_priv(expr);
55 u32 *dest = ®s->data[priv->dreg];
56 enum ip_conntrack_info ctinfo;
57 const struct nf_conn *ct;
58 const struct nf_conn_help *help;
59 const struct nf_conntrack_tuple *tuple;
60 const struct nf_conntrack_helper *helper;
63 ct = nf_ct_get(pkt->skb, &ctinfo);
68 state = NF_CT_STATE_BIT(ctinfo);
69 else if (ctinfo == IP_CT_UNTRACKED)
70 state = NF_CT_STATE_UNTRACKED_BIT;
72 state = NF_CT_STATE_INVALID_BIT;
83 case NFT_CT_DIRECTION:
84 nft_reg_store8(dest, CTINFO2DIR(ctinfo));
89 #ifdef CONFIG_NF_CONNTRACK_MARK
91 *dest = READ_ONCE(ct->mark);
94 #ifdef CONFIG_NF_CONNTRACK_SECMARK
99 case NFT_CT_EXPIRATION:
100 *dest = jiffies_to_msecs(nf_ct_expires(ct));
103 if (ct->master == NULL)
105 help = nfct_help(ct->master);
108 helper = rcu_dereference(help->helper);
111 strncpy((char *)dest, helper->name, NF_CT_HELPER_NAME_LEN);
113 #ifdef CONFIG_NF_CONNTRACK_LABELS
114 case NFT_CT_LABELS: {
115 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
118 memcpy(dest, labels->bits, NF_CT_LABELS_MAX_SIZE);
120 memset(dest, 0, NF_CT_LABELS_MAX_SIZE);
126 const struct nf_conn_acct *acct = nf_conn_acct_find(ct);
130 count = nft_ct_get_eval_counter(acct->counter,
131 priv->key, priv->dir);
132 memcpy(dest, &count, sizeof(count));
135 case NFT_CT_AVGPKT: {
136 const struct nf_conn_acct *acct = nf_conn_acct_find(ct);
137 u64 avgcnt = 0, bcnt = 0, pcnt = 0;
140 pcnt = nft_ct_get_eval_counter(acct->counter,
141 NFT_CT_PKTS, priv->dir);
142 bcnt = nft_ct_get_eval_counter(acct->counter,
143 NFT_CT_BYTES, priv->dir);
145 avgcnt = div64_u64(bcnt, pcnt);
148 memcpy(dest, &avgcnt, sizeof(avgcnt));
151 case NFT_CT_L3PROTOCOL:
152 nft_reg_store8(dest, nf_ct_l3num(ct));
154 case NFT_CT_PROTOCOL:
155 nft_reg_store8(dest, nf_ct_protonum(ct));
157 #ifdef CONFIG_NF_CONNTRACK_ZONES
159 const struct nf_conntrack_zone *zone = nf_ct_zone(ct);
162 if (priv->dir < IP_CT_DIR_MAX)
163 zoneid = nf_ct_zone_id(zone, priv->dir);
167 nft_reg_store16(dest, zoneid);
172 *dest = nf_ct_get_id(ct);
178 tuple = &ct->tuplehash[priv->dir].tuple;
181 memcpy(dest, tuple->src.u3.all,
182 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
185 memcpy(dest, tuple->dst.u3.all,
186 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
188 case NFT_CT_PROTO_SRC:
189 nft_reg_store16(dest, (__force u16)tuple->src.u.all);
191 case NFT_CT_PROTO_DST:
192 nft_reg_store16(dest, (__force u16)tuple->dst.u.all);
195 if (nf_ct_l3num(ct) != NFPROTO_IPV4)
197 *dest = (__force __u32)tuple->src.u3.ip;
200 if (nf_ct_l3num(ct) != NFPROTO_IPV4)
202 *dest = (__force __u32)tuple->dst.u3.ip;
205 if (nf_ct_l3num(ct) != NFPROTO_IPV6)
207 memcpy(dest, tuple->src.u3.ip6, sizeof(struct in6_addr));
210 if (nf_ct_l3num(ct) != NFPROTO_IPV6)
212 memcpy(dest, tuple->dst.u3.ip6, sizeof(struct in6_addr));
219 regs->verdict.code = NFT_BREAK;
222 #ifdef CONFIG_NF_CONNTRACK_ZONES
223 static void nft_ct_set_zone_eval(const struct nft_expr *expr,
224 struct nft_regs *regs,
225 const struct nft_pktinfo *pkt)
227 struct nf_conntrack_zone zone = { .dir = NF_CT_DEFAULT_ZONE_DIR };
228 const struct nft_ct *priv = nft_expr_priv(expr);
229 struct sk_buff *skb = pkt->skb;
230 enum ip_conntrack_info ctinfo;
231 u16 value = nft_reg_load16(®s->data[priv->sreg]);
234 ct = nf_ct_get(skb, &ctinfo);
235 if (ct) /* already tracked */
241 case IP_CT_DIR_ORIGINAL:
242 zone.dir = NF_CT_ZONE_DIR_ORIG;
244 case IP_CT_DIR_REPLY:
245 zone.dir = NF_CT_ZONE_DIR_REPL;
251 ct = this_cpu_read(nft_ct_pcpu_template);
253 if (likely(refcount_read(&ct->ct_general.use) == 1)) {
254 refcount_inc(&ct->ct_general.use);
255 nf_ct_zone_add(ct, &zone);
257 /* previous skb got queued to userspace, allocate temporary
258 * one until percpu template can be reused.
260 ct = nf_ct_tmpl_alloc(nft_net(pkt), &zone, GFP_ATOMIC);
262 regs->verdict.code = NF_DROP;
267 nf_ct_set(skb, ct, IP_CT_NEW);
271 static void nft_ct_set_eval(const struct nft_expr *expr,
272 struct nft_regs *regs,
273 const struct nft_pktinfo *pkt)
275 const struct nft_ct *priv = nft_expr_priv(expr);
276 struct sk_buff *skb = pkt->skb;
277 #if defined(CONFIG_NF_CONNTRACK_MARK) || defined(CONFIG_NF_CONNTRACK_SECMARK)
278 u32 value = regs->data[priv->sreg];
280 enum ip_conntrack_info ctinfo;
283 ct = nf_ct_get(skb, &ctinfo);
284 if (ct == NULL || nf_ct_is_template(ct))
288 #ifdef CONFIG_NF_CONNTRACK_MARK
290 if (READ_ONCE(ct->mark) != value) {
291 WRITE_ONCE(ct->mark, value);
292 nf_conntrack_event_cache(IPCT_MARK, ct);
296 #ifdef CONFIG_NF_CONNTRACK_SECMARK
298 if (ct->secmark != value) {
300 nf_conntrack_event_cache(IPCT_SECMARK, ct);
304 #ifdef CONFIG_NF_CONNTRACK_LABELS
306 nf_connlabels_replace(ct,
307 ®s->data[priv->sreg],
308 ®s->data[priv->sreg],
309 NF_CT_LABELS_MAX_SIZE / sizeof(u32));
312 #ifdef CONFIG_NF_CONNTRACK_EVENTS
313 case NFT_CT_EVENTMASK: {
314 struct nf_conntrack_ecache *e = nf_ct_ecache_find(ct);
315 u32 ctmask = regs->data[priv->sreg];
318 if (e->ctmask != ctmask)
323 if (ctmask && !nf_ct_is_confirmed(ct))
324 nf_ct_ecache_ext_add(ct, ctmask, 0, GFP_ATOMIC);
333 static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
334 [NFTA_CT_DREG] = { .type = NLA_U32 },
335 [NFTA_CT_KEY] = { .type = NLA_U32 },
336 [NFTA_CT_DIRECTION] = { .type = NLA_U8 },
337 [NFTA_CT_SREG] = { .type = NLA_U32 },
340 #ifdef CONFIG_NF_CONNTRACK_ZONES
341 static void nft_ct_tmpl_put_pcpu(void)
346 for_each_possible_cpu(cpu) {
347 ct = per_cpu(nft_ct_pcpu_template, cpu);
351 per_cpu(nft_ct_pcpu_template, cpu) = NULL;
355 static bool nft_ct_tmpl_alloc_pcpu(void)
357 struct nf_conntrack_zone zone = { .id = 0 };
361 if (nft_ct_pcpu_template_refcnt)
364 for_each_possible_cpu(cpu) {
365 tmp = nf_ct_tmpl_alloc(&init_net, &zone, GFP_KERNEL);
367 nft_ct_tmpl_put_pcpu();
371 per_cpu(nft_ct_pcpu_template, cpu) = tmp;
378 static int nft_ct_get_init(const struct nft_ctx *ctx,
379 const struct nft_expr *expr,
380 const struct nlattr * const tb[])
382 struct nft_ct *priv = nft_expr_priv(expr);
386 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
387 priv->dir = IP_CT_DIR_MAX;
389 case NFT_CT_DIRECTION:
390 if (tb[NFTA_CT_DIRECTION] != NULL)
396 #ifdef CONFIG_NF_CONNTRACK_MARK
399 #ifdef CONFIG_NF_CONNTRACK_SECMARK
402 case NFT_CT_EXPIRATION:
403 if (tb[NFTA_CT_DIRECTION] != NULL)
407 #ifdef CONFIG_NF_CONNTRACK_LABELS
409 if (tb[NFTA_CT_DIRECTION] != NULL)
411 len = NF_CT_LABELS_MAX_SIZE;
415 if (tb[NFTA_CT_DIRECTION] != NULL)
417 len = NF_CT_HELPER_NAME_LEN;
420 case NFT_CT_L3PROTOCOL:
421 case NFT_CT_PROTOCOL:
422 /* For compatibility, do not report error if NFTA_CT_DIRECTION
423 * attribute is specified.
429 if (tb[NFTA_CT_DIRECTION] == NULL)
432 switch (ctx->family) {
434 len = sizeof_field(struct nf_conntrack_tuple,
439 len = sizeof_field(struct nf_conntrack_tuple,
443 return -EAFNOSUPPORT;
448 if (tb[NFTA_CT_DIRECTION] == NULL)
451 len = sizeof_field(struct nf_conntrack_tuple, src.u3.ip);
455 if (tb[NFTA_CT_DIRECTION] == NULL)
458 len = sizeof_field(struct nf_conntrack_tuple, src.u3.ip6);
460 case NFT_CT_PROTO_SRC:
461 case NFT_CT_PROTO_DST:
462 if (tb[NFTA_CT_DIRECTION] == NULL)
464 len = sizeof_field(struct nf_conntrack_tuple, src.u.all);
471 #ifdef CONFIG_NF_CONNTRACK_ZONES
483 if (tb[NFTA_CT_DIRECTION] != NULL) {
484 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
486 case IP_CT_DIR_ORIGINAL:
487 case IP_CT_DIR_REPLY:
495 err = nft_parse_register_store(ctx, tb[NFTA_CT_DREG], &priv->dreg, NULL,
496 NFT_DATA_VALUE, len);
500 err = nf_ct_netns_get(ctx->net, ctx->family);
504 if (priv->key == NFT_CT_BYTES ||
505 priv->key == NFT_CT_PKTS ||
506 priv->key == NFT_CT_AVGPKT)
507 nf_ct_set_acct(ctx->net, true);
512 static void __nft_ct_set_destroy(const struct nft_ctx *ctx, struct nft_ct *priv)
515 #ifdef CONFIG_NF_CONNTRACK_LABELS
517 nf_connlabels_put(ctx->net);
520 #ifdef CONFIG_NF_CONNTRACK_ZONES
522 mutex_lock(&nft_ct_pcpu_mutex);
523 if (--nft_ct_pcpu_template_refcnt == 0)
524 nft_ct_tmpl_put_pcpu();
525 mutex_unlock(&nft_ct_pcpu_mutex);
533 static int nft_ct_set_init(const struct nft_ctx *ctx,
534 const struct nft_expr *expr,
535 const struct nlattr * const tb[])
537 struct nft_ct *priv = nft_expr_priv(expr);
541 priv->dir = IP_CT_DIR_MAX;
542 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
544 #ifdef CONFIG_NF_CONNTRACK_MARK
546 if (tb[NFTA_CT_DIRECTION])
548 len = sizeof_field(struct nf_conn, mark);
551 #ifdef CONFIG_NF_CONNTRACK_LABELS
553 if (tb[NFTA_CT_DIRECTION])
555 len = NF_CT_LABELS_MAX_SIZE;
556 err = nf_connlabels_get(ctx->net, (len * BITS_PER_BYTE) - 1);
561 #ifdef CONFIG_NF_CONNTRACK_ZONES
563 mutex_lock(&nft_ct_pcpu_mutex);
564 if (!nft_ct_tmpl_alloc_pcpu()) {
565 mutex_unlock(&nft_ct_pcpu_mutex);
568 nft_ct_pcpu_template_refcnt++;
569 mutex_unlock(&nft_ct_pcpu_mutex);
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:
604 err = nft_parse_register_load(tb[NFTA_CT_SREG], &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,
635 const struct nft_expr *expr, bool reset)
637 const struct nft_ct *priv = nft_expr_priv(expr);
639 if (nft_dump_register(skb, NFTA_CT_DREG, priv->dreg))
640 goto nla_put_failure;
641 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
642 goto nla_put_failure;
651 case NFT_CT_PROTO_SRC:
652 case NFT_CT_PROTO_DST:
653 if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
654 goto nla_put_failure;
660 if (priv->dir < IP_CT_DIR_MAX &&
661 nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
662 goto nla_put_failure;
674 static bool nft_ct_get_reduce(struct nft_regs_track *track,
675 const struct nft_expr *expr)
677 const struct nft_ct *priv = nft_expr_priv(expr);
678 const struct nft_ct *ct;
680 if (!nft_reg_track_cmp(track, expr, priv->dreg)) {
681 nft_reg_track_update(track, expr, priv->dreg, priv->len);
685 ct = nft_expr_priv(track->regs[priv->dreg].selector);
686 if (priv->key != ct->key) {
687 nft_reg_track_update(track, expr, priv->dreg, priv->len);
691 if (!track->regs[priv->dreg].bitwise)
694 return nft_expr_reduce_bitwise(track, expr);
697 static int nft_ct_set_dump(struct sk_buff *skb,
698 const struct nft_expr *expr, bool reset)
700 const struct nft_ct *priv = nft_expr_priv(expr);
702 if (nft_dump_register(skb, NFTA_CT_SREG, priv->sreg))
703 goto nla_put_failure;
704 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
705 goto nla_put_failure;
709 if (priv->dir < IP_CT_DIR_MAX &&
710 nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
711 goto nla_put_failure;
723 static struct nft_expr_type nft_ct_type;
724 static const struct nft_expr_ops nft_ct_get_ops = {
725 .type = &nft_ct_type,
726 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
727 .eval = nft_ct_get_eval,
728 .init = nft_ct_get_init,
729 .destroy = nft_ct_get_destroy,
730 .dump = nft_ct_get_dump,
731 .reduce = nft_ct_get_reduce,
734 static bool nft_ct_set_reduce(struct nft_regs_track *track,
735 const struct nft_expr *expr)
739 for (i = 0; i < NFT_REG32_NUM; i++) {
740 if (!track->regs[i].selector)
743 if (track->regs[i].selector->ops != &nft_ct_get_ops)
746 __nft_reg_track_cancel(track, i);
752 #ifdef CONFIG_RETPOLINE
753 static const struct nft_expr_ops nft_ct_get_fast_ops = {
754 .type = &nft_ct_type,
755 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
756 .eval = nft_ct_get_fast_eval,
757 .init = nft_ct_get_init,
758 .destroy = nft_ct_get_destroy,
759 .dump = nft_ct_get_dump,
760 .reduce = nft_ct_set_reduce,
764 static const struct nft_expr_ops nft_ct_set_ops = {
765 .type = &nft_ct_type,
766 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
767 .eval = nft_ct_set_eval,
768 .init = nft_ct_set_init,
769 .destroy = nft_ct_set_destroy,
770 .dump = nft_ct_set_dump,
771 .reduce = nft_ct_set_reduce,
774 #ifdef CONFIG_NF_CONNTRACK_ZONES
775 static const struct nft_expr_ops nft_ct_set_zone_ops = {
776 .type = &nft_ct_type,
777 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
778 .eval = nft_ct_set_zone_eval,
779 .init = nft_ct_set_init,
780 .destroy = nft_ct_set_destroy,
781 .dump = nft_ct_set_dump,
782 .reduce = nft_ct_set_reduce,
786 static const struct nft_expr_ops *
787 nft_ct_select_ops(const struct nft_ctx *ctx,
788 const struct nlattr * const tb[])
790 if (tb[NFTA_CT_KEY] == NULL)
791 return ERR_PTR(-EINVAL);
793 if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
794 return ERR_PTR(-EINVAL);
796 if (tb[NFTA_CT_DREG]) {
797 #ifdef CONFIG_RETPOLINE
798 u32 k = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
802 case NFT_CT_DIRECTION:
806 return &nft_ct_get_fast_ops;
809 return &nft_ct_get_ops;
812 if (tb[NFTA_CT_SREG]) {
813 #ifdef CONFIG_NF_CONNTRACK_ZONES
814 if (nla_get_be32(tb[NFTA_CT_KEY]) == htonl(NFT_CT_ZONE))
815 return &nft_ct_set_zone_ops;
817 return &nft_ct_set_ops;
820 return ERR_PTR(-EINVAL);
823 static struct nft_expr_type nft_ct_type __read_mostly = {
825 .select_ops = nft_ct_select_ops,
826 .policy = nft_ct_policy,
827 .maxattr = NFTA_CT_MAX,
828 .owner = THIS_MODULE,
831 static void nft_notrack_eval(const struct nft_expr *expr,
832 struct nft_regs *regs,
833 const struct nft_pktinfo *pkt)
835 struct sk_buff *skb = pkt->skb;
836 enum ip_conntrack_info ctinfo;
839 ct = nf_ct_get(pkt->skb, &ctinfo);
840 /* Previously seen (loopback or untracked)? Ignore. */
841 if (ct || ctinfo == IP_CT_UNTRACKED)
844 nf_ct_set(skb, ct, IP_CT_UNTRACKED);
847 static struct nft_expr_type nft_notrack_type;
848 static const struct nft_expr_ops nft_notrack_ops = {
849 .type = &nft_notrack_type,
850 .size = NFT_EXPR_SIZE(0),
851 .eval = nft_notrack_eval,
852 .reduce = NFT_REDUCE_READONLY,
855 static struct nft_expr_type nft_notrack_type __read_mostly = {
857 .ops = &nft_notrack_ops,
858 .owner = THIS_MODULE,
861 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
863 nft_ct_timeout_parse_policy(void *timeouts,
864 const struct nf_conntrack_l4proto *l4proto,
865 struct net *net, const struct nlattr *attr)
870 tb = kcalloc(l4proto->ctnl_timeout.nlattr_max + 1, sizeof(*tb),
876 ret = nla_parse_nested_deprecated(tb,
877 l4proto->ctnl_timeout.nlattr_max,
879 l4proto->ctnl_timeout.nla_policy,
884 ret = l4proto->ctnl_timeout.nlattr_to_obj(tb, net, timeouts);
891 struct nft_ct_timeout_obj {
892 struct nf_ct_timeout *timeout;
896 static void nft_ct_timeout_obj_eval(struct nft_object *obj,
897 struct nft_regs *regs,
898 const struct nft_pktinfo *pkt)
900 const struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
901 struct nf_conn *ct = (struct nf_conn *)skb_nfct(pkt->skb);
902 struct nf_conn_timeout *timeout;
903 const unsigned int *values;
905 if (priv->l4proto != pkt->tprot)
908 if (!ct || nf_ct_is_template(ct) || nf_ct_is_confirmed(ct))
911 timeout = nf_ct_timeout_find(ct);
913 timeout = nf_ct_timeout_ext_add(ct, priv->timeout, GFP_ATOMIC);
915 regs->verdict.code = NF_DROP;
920 rcu_assign_pointer(timeout->timeout, priv->timeout);
922 /* adjust the timeout as per 'new' state. ct is unconfirmed,
923 * so the current timestamp must not be added.
925 values = nf_ct_timeout_data(timeout);
927 nf_ct_refresh(ct, pkt->skb, values[0]);
930 static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx,
931 const struct nlattr * const tb[],
932 struct nft_object *obj)
934 struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
935 const struct nf_conntrack_l4proto *l4proto;
936 struct nf_ct_timeout *timeout;
937 int l3num = ctx->family;
941 if (!tb[NFTA_CT_TIMEOUT_L4PROTO] ||
942 !tb[NFTA_CT_TIMEOUT_DATA])
945 if (tb[NFTA_CT_TIMEOUT_L3PROTO])
946 l3num = ntohs(nla_get_be16(tb[NFTA_CT_TIMEOUT_L3PROTO]));
948 l4num = nla_get_u8(tb[NFTA_CT_TIMEOUT_L4PROTO]);
949 priv->l4proto = l4num;
951 l4proto = nf_ct_l4proto_find(l4num);
953 if (l4proto->l4proto != l4num) {
958 timeout = kzalloc(sizeof(struct nf_ct_timeout) +
959 l4proto->ctnl_timeout.obj_size, GFP_KERNEL);
960 if (timeout == NULL) {
965 ret = nft_ct_timeout_parse_policy(&timeout->data, l4proto, ctx->net,
966 tb[NFTA_CT_TIMEOUT_DATA]);
968 goto err_free_timeout;
970 timeout->l3num = l3num;
971 timeout->l4proto = l4proto;
973 ret = nf_ct_netns_get(ctx->net, ctx->family);
975 goto err_free_timeout;
977 priv->timeout = timeout;
986 static void nft_ct_timeout_obj_destroy(const struct nft_ctx *ctx,
987 struct nft_object *obj)
989 struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
990 struct nf_ct_timeout *timeout = priv->timeout;
992 nf_ct_untimeout(ctx->net, timeout);
993 nf_ct_netns_put(ctx->net, ctx->family);
994 kfree(priv->timeout);
997 static int nft_ct_timeout_obj_dump(struct sk_buff *skb,
998 struct nft_object *obj, bool reset)
1000 const struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
1001 const struct nf_ct_timeout *timeout = priv->timeout;
1002 struct nlattr *nest_params;
1005 if (nla_put_u8(skb, NFTA_CT_TIMEOUT_L4PROTO, timeout->l4proto->l4proto) ||
1006 nla_put_be16(skb, NFTA_CT_TIMEOUT_L3PROTO, htons(timeout->l3num)))
1009 nest_params = nla_nest_start(skb, NFTA_CT_TIMEOUT_DATA);
1013 ret = timeout->l4proto->ctnl_timeout.obj_to_nlattr(skb, &timeout->data);
1016 nla_nest_end(skb, nest_params);
1020 static const struct nla_policy nft_ct_timeout_policy[NFTA_CT_TIMEOUT_MAX + 1] = {
1021 [NFTA_CT_TIMEOUT_L3PROTO] = {.type = NLA_U16 },
1022 [NFTA_CT_TIMEOUT_L4PROTO] = {.type = NLA_U8 },
1023 [NFTA_CT_TIMEOUT_DATA] = {.type = NLA_NESTED },
1026 static struct nft_object_type nft_ct_timeout_obj_type;
1028 static const struct nft_object_ops nft_ct_timeout_obj_ops = {
1029 .type = &nft_ct_timeout_obj_type,
1030 .size = sizeof(struct nft_ct_timeout_obj),
1031 .eval = nft_ct_timeout_obj_eval,
1032 .init = nft_ct_timeout_obj_init,
1033 .destroy = nft_ct_timeout_obj_destroy,
1034 .dump = nft_ct_timeout_obj_dump,
1037 static struct nft_object_type nft_ct_timeout_obj_type __read_mostly = {
1038 .type = NFT_OBJECT_CT_TIMEOUT,
1039 .ops = &nft_ct_timeout_obj_ops,
1040 .maxattr = NFTA_CT_TIMEOUT_MAX,
1041 .policy = nft_ct_timeout_policy,
1042 .owner = THIS_MODULE,
1044 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
1046 static int nft_ct_helper_obj_init(const struct nft_ctx *ctx,
1047 const struct nlattr * const tb[],
1048 struct nft_object *obj)
1050 struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1051 struct nf_conntrack_helper *help4, *help6;
1052 char name[NF_CT_HELPER_NAME_LEN];
1053 int family = ctx->family;
1056 if (!tb[NFTA_CT_HELPER_NAME] || !tb[NFTA_CT_HELPER_L4PROTO])
1059 priv->l4proto = nla_get_u8(tb[NFTA_CT_HELPER_L4PROTO]);
1063 nla_strscpy(name, tb[NFTA_CT_HELPER_NAME], sizeof(name));
1065 if (tb[NFTA_CT_HELPER_L3PROTO])
1066 family = ntohs(nla_get_be16(tb[NFTA_CT_HELPER_L3PROTO]));
1073 if (ctx->family == NFPROTO_IPV6)
1076 help4 = nf_conntrack_helper_try_module_get(name, family,
1080 if (ctx->family == NFPROTO_IPV4)
1083 help6 = nf_conntrack_helper_try_module_get(name, family,
1086 case NFPROTO_NETDEV:
1087 case NFPROTO_BRIDGE:
1089 help4 = nf_conntrack_helper_try_module_get(name, NFPROTO_IPV4,
1091 help6 = nf_conntrack_helper_try_module_get(name, NFPROTO_IPV6,
1095 return -EAFNOSUPPORT;
1098 /* && is intentional; only error if INET found neither ipv4 or ipv6 */
1099 if (!help4 && !help6)
1102 priv->helper4 = help4;
1103 priv->helper6 = help6;
1105 err = nf_ct_netns_get(ctx->net, ctx->family);
1107 goto err_put_helper;
1113 nf_conntrack_helper_put(priv->helper4);
1115 nf_conntrack_helper_put(priv->helper6);
1119 static void nft_ct_helper_obj_destroy(const struct nft_ctx *ctx,
1120 struct nft_object *obj)
1122 struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1125 nf_conntrack_helper_put(priv->helper4);
1127 nf_conntrack_helper_put(priv->helper6);
1129 nf_ct_netns_put(ctx->net, ctx->family);
1132 static void nft_ct_helper_obj_eval(struct nft_object *obj,
1133 struct nft_regs *regs,
1134 const struct nft_pktinfo *pkt)
1136 const struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1137 struct nf_conn *ct = (struct nf_conn *)skb_nfct(pkt->skb);
1138 struct nf_conntrack_helper *to_assign = NULL;
1139 struct nf_conn_help *help;
1142 nf_ct_is_confirmed(ct) ||
1143 nf_ct_is_template(ct) ||
1144 priv->l4proto != nf_ct_protonum(ct))
1147 switch (nf_ct_l3num(ct)) {
1149 to_assign = priv->helper4;
1152 to_assign = priv->helper6;
1162 if (test_bit(IPS_HELPER_BIT, &ct->status))
1165 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1167 rcu_assign_pointer(help->helper, to_assign);
1168 set_bit(IPS_HELPER_BIT, &ct->status);
1172 static int nft_ct_helper_obj_dump(struct sk_buff *skb,
1173 struct nft_object *obj, bool reset)
1175 const struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1176 const struct nf_conntrack_helper *helper;
1179 if (priv->helper4 && priv->helper6) {
1180 family = NFPROTO_INET;
1181 helper = priv->helper4;
1182 } else if (priv->helper6) {
1183 family = NFPROTO_IPV6;
1184 helper = priv->helper6;
1186 family = NFPROTO_IPV4;
1187 helper = priv->helper4;
1190 if (nla_put_string(skb, NFTA_CT_HELPER_NAME, helper->name))
1193 if (nla_put_u8(skb, NFTA_CT_HELPER_L4PROTO, priv->l4proto))
1196 if (nla_put_be16(skb, NFTA_CT_HELPER_L3PROTO, htons(family)))
1202 static const struct nla_policy nft_ct_helper_policy[NFTA_CT_HELPER_MAX + 1] = {
1203 [NFTA_CT_HELPER_NAME] = { .type = NLA_STRING,
1204 .len = NF_CT_HELPER_NAME_LEN - 1 },
1205 [NFTA_CT_HELPER_L3PROTO] = { .type = NLA_U16 },
1206 [NFTA_CT_HELPER_L4PROTO] = { .type = NLA_U8 },
1209 static struct nft_object_type nft_ct_helper_obj_type;
1210 static const struct nft_object_ops nft_ct_helper_obj_ops = {
1211 .type = &nft_ct_helper_obj_type,
1212 .size = sizeof(struct nft_ct_helper_obj),
1213 .eval = nft_ct_helper_obj_eval,
1214 .init = nft_ct_helper_obj_init,
1215 .destroy = nft_ct_helper_obj_destroy,
1216 .dump = nft_ct_helper_obj_dump,
1219 static struct nft_object_type nft_ct_helper_obj_type __read_mostly = {
1220 .type = NFT_OBJECT_CT_HELPER,
1221 .ops = &nft_ct_helper_obj_ops,
1222 .maxattr = NFTA_CT_HELPER_MAX,
1223 .policy = nft_ct_helper_policy,
1224 .owner = THIS_MODULE,
1227 struct nft_ct_expect_obj {
1235 static int nft_ct_expect_obj_init(const struct nft_ctx *ctx,
1236 const struct nlattr * const tb[],
1237 struct nft_object *obj)
1239 struct nft_ct_expect_obj *priv = nft_obj_data(obj);
1241 if (!tb[NFTA_CT_EXPECT_L4PROTO] ||
1242 !tb[NFTA_CT_EXPECT_DPORT] ||
1243 !tb[NFTA_CT_EXPECT_TIMEOUT] ||
1244 !tb[NFTA_CT_EXPECT_SIZE])
1247 priv->l3num = ctx->family;
1248 if (tb[NFTA_CT_EXPECT_L3PROTO])
1249 priv->l3num = ntohs(nla_get_be16(tb[NFTA_CT_EXPECT_L3PROTO]));
1251 priv->l4proto = nla_get_u8(tb[NFTA_CT_EXPECT_L4PROTO]);
1252 priv->dport = nla_get_be16(tb[NFTA_CT_EXPECT_DPORT]);
1253 priv->timeout = nla_get_u32(tb[NFTA_CT_EXPECT_TIMEOUT]);
1254 priv->size = nla_get_u8(tb[NFTA_CT_EXPECT_SIZE]);
1256 return nf_ct_netns_get(ctx->net, ctx->family);
1259 static void nft_ct_expect_obj_destroy(const struct nft_ctx *ctx,
1260 struct nft_object *obj)
1262 nf_ct_netns_put(ctx->net, ctx->family);
1265 static int nft_ct_expect_obj_dump(struct sk_buff *skb,
1266 struct nft_object *obj, bool reset)
1268 const struct nft_ct_expect_obj *priv = nft_obj_data(obj);
1270 if (nla_put_be16(skb, NFTA_CT_EXPECT_L3PROTO, htons(priv->l3num)) ||
1271 nla_put_u8(skb, NFTA_CT_EXPECT_L4PROTO, priv->l4proto) ||
1272 nla_put_be16(skb, NFTA_CT_EXPECT_DPORT, priv->dport) ||
1273 nla_put_u32(skb, NFTA_CT_EXPECT_TIMEOUT, priv->timeout) ||
1274 nla_put_u8(skb, NFTA_CT_EXPECT_SIZE, priv->size))
1280 static void nft_ct_expect_obj_eval(struct nft_object *obj,
1281 struct nft_regs *regs,
1282 const struct nft_pktinfo *pkt)
1284 const struct nft_ct_expect_obj *priv = nft_obj_data(obj);
1285 struct nf_conntrack_expect *exp;
1286 enum ip_conntrack_info ctinfo;
1287 struct nf_conn_help *help;
1288 enum ip_conntrack_dir dir;
1289 u16 l3num = priv->l3num;
1292 ct = nf_ct_get(pkt->skb, &ctinfo);
1293 if (!ct || nf_ct_is_confirmed(ct) || nf_ct_is_template(ct)) {
1294 regs->verdict.code = NFT_BREAK;
1297 dir = CTINFO2DIR(ctinfo);
1299 help = nfct_help(ct);
1301 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1303 regs->verdict.code = NF_DROP;
1307 if (help->expecting[NF_CT_EXPECT_CLASS_DEFAULT] >= priv->size) {
1308 regs->verdict.code = NFT_BREAK;
1311 if (l3num == NFPROTO_INET)
1312 l3num = nf_ct_l3num(ct);
1314 exp = nf_ct_expect_alloc(ct);
1316 regs->verdict.code = NF_DROP;
1319 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, l3num,
1320 &ct->tuplehash[!dir].tuple.src.u3,
1321 &ct->tuplehash[!dir].tuple.dst.u3,
1322 priv->l4proto, NULL, &priv->dport);
1323 exp->timeout.expires = jiffies + priv->timeout * HZ;
1325 if (nf_ct_expect_related(exp, 0) != 0)
1326 regs->verdict.code = NF_DROP;
1329 static const struct nla_policy nft_ct_expect_policy[NFTA_CT_EXPECT_MAX + 1] = {
1330 [NFTA_CT_EXPECT_L3PROTO] = { .type = NLA_U16 },
1331 [NFTA_CT_EXPECT_L4PROTO] = { .type = NLA_U8 },
1332 [NFTA_CT_EXPECT_DPORT] = { .type = NLA_U16 },
1333 [NFTA_CT_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1334 [NFTA_CT_EXPECT_SIZE] = { .type = NLA_U8 },
1337 static struct nft_object_type nft_ct_expect_obj_type;
1339 static const struct nft_object_ops nft_ct_expect_obj_ops = {
1340 .type = &nft_ct_expect_obj_type,
1341 .size = sizeof(struct nft_ct_expect_obj),
1342 .eval = nft_ct_expect_obj_eval,
1343 .init = nft_ct_expect_obj_init,
1344 .destroy = nft_ct_expect_obj_destroy,
1345 .dump = nft_ct_expect_obj_dump,
1348 static struct nft_object_type nft_ct_expect_obj_type __read_mostly = {
1349 .type = NFT_OBJECT_CT_EXPECT,
1350 .ops = &nft_ct_expect_obj_ops,
1351 .maxattr = NFTA_CT_EXPECT_MAX,
1352 .policy = nft_ct_expect_policy,
1353 .owner = THIS_MODULE,
1356 static int __init nft_ct_module_init(void)
1360 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE > NFT_REG_SIZE);
1362 err = nft_register_expr(&nft_ct_type);
1366 err = nft_register_expr(&nft_notrack_type);
1370 err = nft_register_obj(&nft_ct_helper_obj_type);
1374 err = nft_register_obj(&nft_ct_expect_obj_type);
1377 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1378 err = nft_register_obj(&nft_ct_timeout_obj_type);
1384 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1386 nft_unregister_obj(&nft_ct_expect_obj_type);
1389 nft_unregister_obj(&nft_ct_helper_obj_type);
1391 nft_unregister_expr(&nft_notrack_type);
1393 nft_unregister_expr(&nft_ct_type);
1397 static void __exit nft_ct_module_exit(void)
1399 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1400 nft_unregister_obj(&nft_ct_timeout_obj_type);
1402 nft_unregister_obj(&nft_ct_expect_obj_type);
1403 nft_unregister_obj(&nft_ct_helper_obj_type);
1404 nft_unregister_expr(&nft_notrack_type);
1405 nft_unregister_expr(&nft_ct_type);
1408 module_init(nft_ct_module_init);
1409 module_exit(nft_ct_module_exit);
1411 MODULE_LICENSE("GPL");
1413 MODULE_ALIAS_NFT_EXPR("ct");
1414 MODULE_ALIAS_NFT_EXPR("notrack");
1415 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_HELPER);
1416 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_TIMEOUT);
1417 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_EXPECT);
1418 MODULE_DESCRIPTION("Netfilter nf_tables conntrack module");