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_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 strscpy_pad((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
;
265 __set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
268 nf_ct_set(skb
, ct
, IP_CT_NEW
);
272 static void nft_ct_set_eval(const struct nft_expr
*expr
,
273 struct nft_regs
*regs
,
274 const struct nft_pktinfo
*pkt
)
276 const struct nft_ct
*priv
= nft_expr_priv(expr
);
277 struct sk_buff
*skb
= pkt
->skb
;
278 #if defined(CONFIG_NF_CONNTRACK_MARK) || defined(CONFIG_NF_CONNTRACK_SECMARK)
279 u32 value
= regs
->data
[priv
->sreg
];
281 enum ip_conntrack_info ctinfo
;
284 ct
= nf_ct_get(skb
, &ctinfo
);
285 if (ct
== NULL
|| nf_ct_is_template(ct
))
289 #ifdef CONFIG_NF_CONNTRACK_MARK
291 if (READ_ONCE(ct
->mark
) != value
) {
292 WRITE_ONCE(ct
->mark
, value
);
293 nf_conntrack_event_cache(IPCT_MARK
, ct
);
297 #ifdef CONFIG_NF_CONNTRACK_SECMARK
299 if (ct
->secmark
!= value
) {
301 nf_conntrack_event_cache(IPCT_SECMARK
, ct
);
305 #ifdef CONFIG_NF_CONNTRACK_LABELS
307 nf_connlabels_replace(ct
,
308 ®s
->data
[priv
->sreg
],
309 ®s
->data
[priv
->sreg
],
310 NF_CT_LABELS_MAX_SIZE
/ sizeof(u32
));
313 #ifdef CONFIG_NF_CONNTRACK_EVENTS
314 case NFT_CT_EVENTMASK
: {
315 struct nf_conntrack_ecache
*e
= nf_ct_ecache_find(ct
);
316 u32 ctmask
= regs
->data
[priv
->sreg
];
319 if (e
->ctmask
!= ctmask
)
324 if (ctmask
&& !nf_ct_is_confirmed(ct
))
325 nf_ct_ecache_ext_add(ct
, ctmask
, 0, GFP_ATOMIC
);
334 static const struct nla_policy nft_ct_policy
[NFTA_CT_MAX
+ 1] = {
335 [NFTA_CT_DREG
] = { .type
= NLA_U32
},
336 [NFTA_CT_KEY
] = NLA_POLICY_MAX(NLA_BE32
, 255),
337 [NFTA_CT_DIRECTION
] = { .type
= NLA_U8
},
338 [NFTA_CT_SREG
] = { .type
= NLA_U32
},
341 #ifdef CONFIG_NF_CONNTRACK_ZONES
342 static void nft_ct_tmpl_put_pcpu(void)
347 for_each_possible_cpu(cpu
) {
348 ct
= per_cpu(nft_ct_pcpu_template
, cpu
);
352 per_cpu(nft_ct_pcpu_template
, cpu
) = NULL
;
356 static bool nft_ct_tmpl_alloc_pcpu(void)
358 struct nf_conntrack_zone zone
= { .id
= 0 };
362 if (nft_ct_pcpu_template_refcnt
)
365 for_each_possible_cpu(cpu
) {
366 tmp
= nf_ct_tmpl_alloc(&init_net
, &zone
, GFP_KERNEL
);
368 nft_ct_tmpl_put_pcpu();
372 __set_bit(IPS_CONFIRMED_BIT
, &tmp
->status
);
373 per_cpu(nft_ct_pcpu_template
, cpu
) = tmp
;
380 static int nft_ct_get_init(const struct nft_ctx
*ctx
,
381 const struct nft_expr
*expr
,
382 const struct nlattr
* const tb
[])
384 struct nft_ct
*priv
= nft_expr_priv(expr
);
388 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
389 priv
->dir
= IP_CT_DIR_MAX
;
391 case NFT_CT_DIRECTION
:
392 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
398 #ifdef CONFIG_NF_CONNTRACK_MARK
401 #ifdef CONFIG_NF_CONNTRACK_SECMARK
404 case NFT_CT_EXPIRATION
:
405 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
409 #ifdef CONFIG_NF_CONNTRACK_LABELS
411 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
413 len
= NF_CT_LABELS_MAX_SIZE
;
417 if (tb
[NFTA_CT_DIRECTION
] != NULL
)
419 len
= NF_CT_HELPER_NAME_LEN
;
422 case NFT_CT_L3PROTOCOL
:
423 case NFT_CT_PROTOCOL
:
424 /* For compatibility, do not report error if NFTA_CT_DIRECTION
425 * attribute is specified.
431 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
434 switch (ctx
->family
) {
436 len
= sizeof_field(struct nf_conntrack_tuple
,
441 len
= sizeof_field(struct nf_conntrack_tuple
,
445 return -EAFNOSUPPORT
;
450 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
453 len
= sizeof_field(struct nf_conntrack_tuple
, src
.u3
.ip
);
457 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
460 len
= sizeof_field(struct nf_conntrack_tuple
, src
.u3
.ip6
);
462 case NFT_CT_PROTO_SRC
:
463 case NFT_CT_PROTO_DST
:
464 if (tb
[NFTA_CT_DIRECTION
] == NULL
)
466 len
= sizeof_field(struct nf_conntrack_tuple
, src
.u
.all
);
473 #ifdef CONFIG_NF_CONNTRACK_ZONES
479 if (tb
[NFTA_CT_DIRECTION
])
488 if (tb
[NFTA_CT_DIRECTION
] != NULL
) {
489 priv
->dir
= nla_get_u8(tb
[NFTA_CT_DIRECTION
]);
491 case IP_CT_DIR_ORIGINAL
:
492 case IP_CT_DIR_REPLY
:
500 err
= nft_parse_register_store(ctx
, tb
[NFTA_CT_DREG
], &priv
->dreg
, NULL
,
501 NFT_DATA_VALUE
, len
);
505 err
= nf_ct_netns_get(ctx
->net
, ctx
->family
);
509 if (priv
->key
== NFT_CT_BYTES
||
510 priv
->key
== NFT_CT_PKTS
||
511 priv
->key
== NFT_CT_AVGPKT
)
512 nf_ct_set_acct(ctx
->net
, true);
517 static void __nft_ct_set_destroy(const struct nft_ctx
*ctx
, struct nft_ct
*priv
)
520 #ifdef CONFIG_NF_CONNTRACK_LABELS
522 nf_connlabels_put(ctx
->net
);
525 #ifdef CONFIG_NF_CONNTRACK_ZONES
527 mutex_lock(&nft_ct_pcpu_mutex
);
528 if (--nft_ct_pcpu_template_refcnt
== 0)
529 nft_ct_tmpl_put_pcpu();
530 mutex_unlock(&nft_ct_pcpu_mutex
);
538 static int nft_ct_set_init(const struct nft_ctx
*ctx
,
539 const struct nft_expr
*expr
,
540 const struct nlattr
* const tb
[])
542 struct nft_ct
*priv
= nft_expr_priv(expr
);
546 priv
->dir
= IP_CT_DIR_MAX
;
547 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
549 #ifdef CONFIG_NF_CONNTRACK_MARK
551 if (tb
[NFTA_CT_DIRECTION
])
553 len
= sizeof_field(struct nf_conn
, mark
);
556 #ifdef CONFIG_NF_CONNTRACK_LABELS
558 if (tb
[NFTA_CT_DIRECTION
])
560 len
= NF_CT_LABELS_MAX_SIZE
;
561 err
= nf_connlabels_get(ctx
->net
, (len
* BITS_PER_BYTE
) - 1);
566 #ifdef CONFIG_NF_CONNTRACK_ZONES
568 mutex_lock(&nft_ct_pcpu_mutex
);
569 if (!nft_ct_tmpl_alloc_pcpu()) {
570 mutex_unlock(&nft_ct_pcpu_mutex
);
573 nft_ct_pcpu_template_refcnt
++;
574 mutex_unlock(&nft_ct_pcpu_mutex
);
578 #ifdef CONFIG_NF_CONNTRACK_EVENTS
579 case NFT_CT_EVENTMASK
:
580 if (tb
[NFTA_CT_DIRECTION
])
585 #ifdef CONFIG_NF_CONNTRACK_SECMARK
587 if (tb
[NFTA_CT_DIRECTION
])
596 if (tb
[NFTA_CT_DIRECTION
]) {
597 priv
->dir
= nla_get_u8(tb
[NFTA_CT_DIRECTION
]);
599 case IP_CT_DIR_ORIGINAL
:
600 case IP_CT_DIR_REPLY
:
609 err
= nft_parse_register_load(ctx
, tb
[NFTA_CT_SREG
], &priv
->sreg
, len
);
613 err
= nf_ct_netns_get(ctx
->net
, ctx
->family
);
620 __nft_ct_set_destroy(ctx
, priv
);
624 static void nft_ct_get_destroy(const struct nft_ctx
*ctx
,
625 const struct nft_expr
*expr
)
627 nf_ct_netns_put(ctx
->net
, ctx
->family
);
630 static void nft_ct_set_destroy(const struct nft_ctx
*ctx
,
631 const struct nft_expr
*expr
)
633 struct nft_ct
*priv
= nft_expr_priv(expr
);
635 __nft_ct_set_destroy(ctx
, priv
);
636 nf_ct_netns_put(ctx
->net
, ctx
->family
);
639 static int nft_ct_get_dump(struct sk_buff
*skb
,
640 const struct nft_expr
*expr
, bool reset
)
642 const struct nft_ct
*priv
= nft_expr_priv(expr
);
644 if (nft_dump_register(skb
, NFTA_CT_DREG
, priv
->dreg
))
645 goto nla_put_failure
;
646 if (nla_put_be32(skb
, NFTA_CT_KEY
, htonl(priv
->key
)))
647 goto nla_put_failure
;
656 case NFT_CT_PROTO_SRC
:
657 case NFT_CT_PROTO_DST
:
658 if (nla_put_u8(skb
, NFTA_CT_DIRECTION
, priv
->dir
))
659 goto nla_put_failure
;
665 if (priv
->dir
< IP_CT_DIR_MAX
&&
666 nla_put_u8(skb
, NFTA_CT_DIRECTION
, priv
->dir
))
667 goto nla_put_failure
;
679 static bool nft_ct_get_reduce(struct nft_regs_track
*track
,
680 const struct nft_expr
*expr
)
682 const struct nft_ct
*priv
= nft_expr_priv(expr
);
683 const struct nft_ct
*ct
;
685 if (!nft_reg_track_cmp(track
, expr
, priv
->dreg
)) {
686 nft_reg_track_update(track
, expr
, priv
->dreg
, priv
->len
);
690 ct
= nft_expr_priv(track
->regs
[priv
->dreg
].selector
);
691 if (priv
->key
!= ct
->key
) {
692 nft_reg_track_update(track
, expr
, priv
->dreg
, priv
->len
);
696 if (!track
->regs
[priv
->dreg
].bitwise
)
699 return nft_expr_reduce_bitwise(track
, expr
);
702 static int nft_ct_set_dump(struct sk_buff
*skb
,
703 const struct nft_expr
*expr
, bool reset
)
705 const struct nft_ct
*priv
= nft_expr_priv(expr
);
707 if (nft_dump_register(skb
, NFTA_CT_SREG
, priv
->sreg
))
708 goto nla_put_failure
;
709 if (nla_put_be32(skb
, NFTA_CT_KEY
, htonl(priv
->key
)))
710 goto nla_put_failure
;
714 if (priv
->dir
< IP_CT_DIR_MAX
&&
715 nla_put_u8(skb
, NFTA_CT_DIRECTION
, priv
->dir
))
716 goto nla_put_failure
;
728 static struct nft_expr_type nft_ct_type
;
729 static const struct nft_expr_ops nft_ct_get_ops
= {
730 .type
= &nft_ct_type
,
731 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
732 .eval
= nft_ct_get_eval
,
733 .init
= nft_ct_get_init
,
734 .destroy
= nft_ct_get_destroy
,
735 .dump
= nft_ct_get_dump
,
736 .reduce
= nft_ct_get_reduce
,
739 static bool nft_ct_set_reduce(struct nft_regs_track
*track
,
740 const struct nft_expr
*expr
)
744 for (i
= 0; i
< NFT_REG32_NUM
; i
++) {
745 if (!track
->regs
[i
].selector
)
748 if (track
->regs
[i
].selector
->ops
!= &nft_ct_get_ops
)
751 __nft_reg_track_cancel(track
, i
);
757 #ifdef CONFIG_MITIGATION_RETPOLINE
758 static const struct nft_expr_ops nft_ct_get_fast_ops
= {
759 .type
= &nft_ct_type
,
760 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
761 .eval
= nft_ct_get_fast_eval
,
762 .init
= nft_ct_get_init
,
763 .destroy
= nft_ct_get_destroy
,
764 .dump
= nft_ct_get_dump
,
765 .reduce
= nft_ct_set_reduce
,
769 static const struct nft_expr_ops nft_ct_set_ops
= {
770 .type
= &nft_ct_type
,
771 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
772 .eval
= nft_ct_set_eval
,
773 .init
= nft_ct_set_init
,
774 .destroy
= nft_ct_set_destroy
,
775 .dump
= nft_ct_set_dump
,
776 .reduce
= nft_ct_set_reduce
,
779 #ifdef CONFIG_NF_CONNTRACK_ZONES
780 static const struct nft_expr_ops nft_ct_set_zone_ops
= {
781 .type
= &nft_ct_type
,
782 .size
= NFT_EXPR_SIZE(sizeof(struct nft_ct
)),
783 .eval
= nft_ct_set_zone_eval
,
784 .init
= nft_ct_set_init
,
785 .destroy
= nft_ct_set_destroy
,
786 .dump
= nft_ct_set_dump
,
787 .reduce
= nft_ct_set_reduce
,
791 static const struct nft_expr_ops
*
792 nft_ct_select_ops(const struct nft_ctx
*ctx
,
793 const struct nlattr
* const tb
[])
795 if (tb
[NFTA_CT_KEY
] == NULL
)
796 return ERR_PTR(-EINVAL
);
798 if (tb
[NFTA_CT_DREG
] && tb
[NFTA_CT_SREG
])
799 return ERR_PTR(-EINVAL
);
801 if (tb
[NFTA_CT_DREG
]) {
802 #ifdef CONFIG_MITIGATION_RETPOLINE
803 u32 k
= ntohl(nla_get_be32(tb
[NFTA_CT_KEY
]));
807 case NFT_CT_DIRECTION
:
811 return &nft_ct_get_fast_ops
;
814 return &nft_ct_get_ops
;
817 if (tb
[NFTA_CT_SREG
]) {
818 #ifdef CONFIG_NF_CONNTRACK_ZONES
819 if (nla_get_be32(tb
[NFTA_CT_KEY
]) == htonl(NFT_CT_ZONE
))
820 return &nft_ct_set_zone_ops
;
822 return &nft_ct_set_ops
;
825 return ERR_PTR(-EINVAL
);
828 static struct nft_expr_type nft_ct_type __read_mostly
= {
830 .select_ops
= nft_ct_select_ops
,
831 .policy
= nft_ct_policy
,
832 .maxattr
= NFTA_CT_MAX
,
833 .owner
= THIS_MODULE
,
836 static void nft_notrack_eval(const struct nft_expr
*expr
,
837 struct nft_regs
*regs
,
838 const struct nft_pktinfo
*pkt
)
840 struct sk_buff
*skb
= pkt
->skb
;
841 enum ip_conntrack_info ctinfo
;
844 ct
= nf_ct_get(pkt
->skb
, &ctinfo
);
845 /* Previously seen (loopback or untracked)? Ignore. */
846 if (ct
|| ctinfo
== IP_CT_UNTRACKED
)
849 nf_ct_set(skb
, ct
, IP_CT_UNTRACKED
);
852 static struct nft_expr_type nft_notrack_type
;
853 static const struct nft_expr_ops nft_notrack_ops
= {
854 .type
= &nft_notrack_type
,
855 .size
= NFT_EXPR_SIZE(0),
856 .eval
= nft_notrack_eval
,
857 .reduce
= NFT_REDUCE_READONLY
,
860 static struct nft_expr_type nft_notrack_type __read_mostly
= {
862 .ops
= &nft_notrack_ops
,
863 .owner
= THIS_MODULE
,
866 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
868 nft_ct_timeout_parse_policy(void *timeouts
,
869 const struct nf_conntrack_l4proto
*l4proto
,
870 struct net
*net
, const struct nlattr
*attr
)
875 tb
= kcalloc(l4proto
->ctnl_timeout
.nlattr_max
+ 1, sizeof(*tb
),
881 ret
= nla_parse_nested_deprecated(tb
,
882 l4proto
->ctnl_timeout
.nlattr_max
,
884 l4proto
->ctnl_timeout
.nla_policy
,
889 ret
= l4proto
->ctnl_timeout
.nlattr_to_obj(tb
, net
, timeouts
);
896 struct nft_ct_timeout_obj
{
897 struct nf_ct_timeout
*timeout
;
901 static void nft_ct_timeout_obj_eval(struct nft_object
*obj
,
902 struct nft_regs
*regs
,
903 const struct nft_pktinfo
*pkt
)
905 const struct nft_ct_timeout_obj
*priv
= nft_obj_data(obj
);
906 struct nf_conn
*ct
= (struct nf_conn
*)skb_nfct(pkt
->skb
);
907 struct nf_conn_timeout
*timeout
;
908 const unsigned int *values
;
910 if (priv
->l4proto
!= pkt
->tprot
)
913 if (!ct
|| nf_ct_is_template(ct
) || nf_ct_is_confirmed(ct
))
916 timeout
= nf_ct_timeout_find(ct
);
918 timeout
= nf_ct_timeout_ext_add(ct
, priv
->timeout
, GFP_ATOMIC
);
920 regs
->verdict
.code
= NF_DROP
;
925 rcu_assign_pointer(timeout
->timeout
, priv
->timeout
);
927 /* adjust the timeout as per 'new' state. ct is unconfirmed,
928 * so the current timestamp must not be added.
930 values
= nf_ct_timeout_data(timeout
);
932 nf_ct_refresh(ct
, pkt
->skb
, values
[0]);
935 static int nft_ct_timeout_obj_init(const struct nft_ctx
*ctx
,
936 const struct nlattr
* const tb
[],
937 struct nft_object
*obj
)
939 struct nft_ct_timeout_obj
*priv
= nft_obj_data(obj
);
940 const struct nf_conntrack_l4proto
*l4proto
;
941 struct nf_ct_timeout
*timeout
;
942 int l3num
= ctx
->family
;
946 if (!tb
[NFTA_CT_TIMEOUT_L4PROTO
] ||
947 !tb
[NFTA_CT_TIMEOUT_DATA
])
950 if (tb
[NFTA_CT_TIMEOUT_L3PROTO
])
951 l3num
= ntohs(nla_get_be16(tb
[NFTA_CT_TIMEOUT_L3PROTO
]));
953 l4num
= nla_get_u8(tb
[NFTA_CT_TIMEOUT_L4PROTO
]);
954 priv
->l4proto
= l4num
;
956 l4proto
= nf_ct_l4proto_find(l4num
);
958 if (l4proto
->l4proto
!= l4num
) {
963 timeout
= kzalloc(sizeof(struct nf_ct_timeout
) +
964 l4proto
->ctnl_timeout
.obj_size
, GFP_KERNEL
);
965 if (timeout
== NULL
) {
970 ret
= nft_ct_timeout_parse_policy(&timeout
->data
, l4proto
, ctx
->net
,
971 tb
[NFTA_CT_TIMEOUT_DATA
]);
973 goto err_free_timeout
;
975 timeout
->l3num
= l3num
;
976 timeout
->l4proto
= l4proto
;
978 ret
= nf_ct_netns_get(ctx
->net
, ctx
->family
);
980 goto err_free_timeout
;
982 priv
->timeout
= timeout
;
991 static void nft_ct_timeout_obj_destroy(const struct nft_ctx
*ctx
,
992 struct nft_object
*obj
)
994 struct nft_ct_timeout_obj
*priv
= nft_obj_data(obj
);
995 struct nf_ct_timeout
*timeout
= priv
->timeout
;
997 nf_ct_untimeout(ctx
->net
, timeout
);
998 nf_ct_netns_put(ctx
->net
, ctx
->family
);
999 kfree(priv
->timeout
);
1002 static int nft_ct_timeout_obj_dump(struct sk_buff
*skb
,
1003 struct nft_object
*obj
, bool reset
)
1005 const struct nft_ct_timeout_obj
*priv
= nft_obj_data(obj
);
1006 const struct nf_ct_timeout
*timeout
= priv
->timeout
;
1007 struct nlattr
*nest_params
;
1010 if (nla_put_u8(skb
, NFTA_CT_TIMEOUT_L4PROTO
, timeout
->l4proto
->l4proto
) ||
1011 nla_put_be16(skb
, NFTA_CT_TIMEOUT_L3PROTO
, htons(timeout
->l3num
)))
1014 nest_params
= nla_nest_start(skb
, NFTA_CT_TIMEOUT_DATA
);
1018 ret
= timeout
->l4proto
->ctnl_timeout
.obj_to_nlattr(skb
, &timeout
->data
);
1021 nla_nest_end(skb
, nest_params
);
1025 static const struct nla_policy nft_ct_timeout_policy
[NFTA_CT_TIMEOUT_MAX
+ 1] = {
1026 [NFTA_CT_TIMEOUT_L3PROTO
] = {.type
= NLA_U16
},
1027 [NFTA_CT_TIMEOUT_L4PROTO
] = {.type
= NLA_U8
},
1028 [NFTA_CT_TIMEOUT_DATA
] = {.type
= NLA_NESTED
},
1031 static struct nft_object_type nft_ct_timeout_obj_type
;
1033 static const struct nft_object_ops nft_ct_timeout_obj_ops
= {
1034 .type
= &nft_ct_timeout_obj_type
,
1035 .size
= sizeof(struct nft_ct_timeout_obj
),
1036 .eval
= nft_ct_timeout_obj_eval
,
1037 .init
= nft_ct_timeout_obj_init
,
1038 .destroy
= nft_ct_timeout_obj_destroy
,
1039 .dump
= nft_ct_timeout_obj_dump
,
1042 static struct nft_object_type nft_ct_timeout_obj_type __read_mostly
= {
1043 .type
= NFT_OBJECT_CT_TIMEOUT
,
1044 .ops
= &nft_ct_timeout_obj_ops
,
1045 .maxattr
= NFTA_CT_TIMEOUT_MAX
,
1046 .policy
= nft_ct_timeout_policy
,
1047 .owner
= THIS_MODULE
,
1049 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
1051 static int nft_ct_helper_obj_init(const struct nft_ctx
*ctx
,
1052 const struct nlattr
* const tb
[],
1053 struct nft_object
*obj
)
1055 struct nft_ct_helper_obj
*priv
= nft_obj_data(obj
);
1056 struct nf_conntrack_helper
*help4
, *help6
;
1057 char name
[NF_CT_HELPER_NAME_LEN
];
1058 int family
= ctx
->family
;
1061 if (!tb
[NFTA_CT_HELPER_NAME
] || !tb
[NFTA_CT_HELPER_L4PROTO
])
1064 priv
->l4proto
= nla_get_u8(tb
[NFTA_CT_HELPER_L4PROTO
]);
1068 nla_strscpy(name
, tb
[NFTA_CT_HELPER_NAME
], sizeof(name
));
1070 if (tb
[NFTA_CT_HELPER_L3PROTO
])
1071 family
= ntohs(nla_get_be16(tb
[NFTA_CT_HELPER_L3PROTO
]));
1078 if (ctx
->family
== NFPROTO_IPV6
)
1081 help4
= nf_conntrack_helper_try_module_get(name
, family
,
1085 if (ctx
->family
== NFPROTO_IPV4
)
1088 help6
= nf_conntrack_helper_try_module_get(name
, family
,
1091 case NFPROTO_NETDEV
:
1092 case NFPROTO_BRIDGE
:
1094 help4
= nf_conntrack_helper_try_module_get(name
, NFPROTO_IPV4
,
1096 help6
= nf_conntrack_helper_try_module_get(name
, NFPROTO_IPV6
,
1100 return -EAFNOSUPPORT
;
1103 /* && is intentional; only error if INET found neither ipv4 or ipv6 */
1104 if (!help4
&& !help6
)
1107 priv
->helper4
= help4
;
1108 priv
->helper6
= help6
;
1110 err
= nf_ct_netns_get(ctx
->net
, ctx
->family
);
1112 goto err_put_helper
;
1118 nf_conntrack_helper_put(priv
->helper4
);
1120 nf_conntrack_helper_put(priv
->helper6
);
1124 static void nft_ct_helper_obj_destroy(const struct nft_ctx
*ctx
,
1125 struct nft_object
*obj
)
1127 struct nft_ct_helper_obj
*priv
= nft_obj_data(obj
);
1130 nf_conntrack_helper_put(priv
->helper4
);
1132 nf_conntrack_helper_put(priv
->helper6
);
1134 nf_ct_netns_put(ctx
->net
, ctx
->family
);
1137 static void nft_ct_helper_obj_eval(struct nft_object
*obj
,
1138 struct nft_regs
*regs
,
1139 const struct nft_pktinfo
*pkt
)
1141 const struct nft_ct_helper_obj
*priv
= nft_obj_data(obj
);
1142 struct nf_conn
*ct
= (struct nf_conn
*)skb_nfct(pkt
->skb
);
1143 struct nf_conntrack_helper
*to_assign
= NULL
;
1144 struct nf_conn_help
*help
;
1147 nf_ct_is_confirmed(ct
) ||
1148 nf_ct_is_template(ct
) ||
1149 priv
->l4proto
!= nf_ct_protonum(ct
))
1152 switch (nf_ct_l3num(ct
)) {
1154 to_assign
= priv
->helper4
;
1157 to_assign
= priv
->helper6
;
1167 if (test_bit(IPS_HELPER_BIT
, &ct
->status
))
1170 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1172 rcu_assign_pointer(help
->helper
, to_assign
);
1173 set_bit(IPS_HELPER_BIT
, &ct
->status
);
1177 static int nft_ct_helper_obj_dump(struct sk_buff
*skb
,
1178 struct nft_object
*obj
, bool reset
)
1180 const struct nft_ct_helper_obj
*priv
= nft_obj_data(obj
);
1181 const struct nf_conntrack_helper
*helper
;
1184 if (priv
->helper4
&& priv
->helper6
) {
1185 family
= NFPROTO_INET
;
1186 helper
= priv
->helper4
;
1187 } else if (priv
->helper6
) {
1188 family
= NFPROTO_IPV6
;
1189 helper
= priv
->helper6
;
1191 family
= NFPROTO_IPV4
;
1192 helper
= priv
->helper4
;
1195 if (nla_put_string(skb
, NFTA_CT_HELPER_NAME
, helper
->name
))
1198 if (nla_put_u8(skb
, NFTA_CT_HELPER_L4PROTO
, priv
->l4proto
))
1201 if (nla_put_be16(skb
, NFTA_CT_HELPER_L3PROTO
, htons(family
)))
1207 static const struct nla_policy nft_ct_helper_policy
[NFTA_CT_HELPER_MAX
+ 1] = {
1208 [NFTA_CT_HELPER_NAME
] = { .type
= NLA_STRING
,
1209 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
1210 [NFTA_CT_HELPER_L3PROTO
] = { .type
= NLA_U16
},
1211 [NFTA_CT_HELPER_L4PROTO
] = { .type
= NLA_U8
},
1214 static struct nft_object_type nft_ct_helper_obj_type
;
1215 static const struct nft_object_ops nft_ct_helper_obj_ops
= {
1216 .type
= &nft_ct_helper_obj_type
,
1217 .size
= sizeof(struct nft_ct_helper_obj
),
1218 .eval
= nft_ct_helper_obj_eval
,
1219 .init
= nft_ct_helper_obj_init
,
1220 .destroy
= nft_ct_helper_obj_destroy
,
1221 .dump
= nft_ct_helper_obj_dump
,
1224 static struct nft_object_type nft_ct_helper_obj_type __read_mostly
= {
1225 .type
= NFT_OBJECT_CT_HELPER
,
1226 .ops
= &nft_ct_helper_obj_ops
,
1227 .maxattr
= NFTA_CT_HELPER_MAX
,
1228 .policy
= nft_ct_helper_policy
,
1229 .owner
= THIS_MODULE
,
1232 struct nft_ct_expect_obj
{
1240 static int nft_ct_expect_obj_init(const struct nft_ctx
*ctx
,
1241 const struct nlattr
* const tb
[],
1242 struct nft_object
*obj
)
1244 struct nft_ct_expect_obj
*priv
= nft_obj_data(obj
);
1246 if (!tb
[NFTA_CT_EXPECT_L4PROTO
] ||
1247 !tb
[NFTA_CT_EXPECT_DPORT
] ||
1248 !tb
[NFTA_CT_EXPECT_TIMEOUT
] ||
1249 !tb
[NFTA_CT_EXPECT_SIZE
])
1252 priv
->l3num
= ctx
->family
;
1253 if (tb
[NFTA_CT_EXPECT_L3PROTO
])
1254 priv
->l3num
= ntohs(nla_get_be16(tb
[NFTA_CT_EXPECT_L3PROTO
]));
1256 switch (priv
->l3num
) {
1259 if (priv
->l3num
== ctx
->family
|| ctx
->family
== NFPROTO_INET
)
1263 case NFPROTO_INET
: /* tuple.src.l3num supports NFPROTO_IPV4/6 only */
1265 return -EAFNOSUPPORT
;
1268 priv
->l4proto
= nla_get_u8(tb
[NFTA_CT_EXPECT_L4PROTO
]);
1269 switch (priv
->l4proto
) {
1272 case IPPROTO_UDPLITE
:
1280 priv
->dport
= nla_get_be16(tb
[NFTA_CT_EXPECT_DPORT
]);
1281 priv
->timeout
= nla_get_u32(tb
[NFTA_CT_EXPECT_TIMEOUT
]);
1282 priv
->size
= nla_get_u8(tb
[NFTA_CT_EXPECT_SIZE
]);
1284 return nf_ct_netns_get(ctx
->net
, ctx
->family
);
1287 static void nft_ct_expect_obj_destroy(const struct nft_ctx
*ctx
,
1288 struct nft_object
*obj
)
1290 nf_ct_netns_put(ctx
->net
, ctx
->family
);
1293 static int nft_ct_expect_obj_dump(struct sk_buff
*skb
,
1294 struct nft_object
*obj
, bool reset
)
1296 const struct nft_ct_expect_obj
*priv
= nft_obj_data(obj
);
1298 if (nla_put_be16(skb
, NFTA_CT_EXPECT_L3PROTO
, htons(priv
->l3num
)) ||
1299 nla_put_u8(skb
, NFTA_CT_EXPECT_L4PROTO
, priv
->l4proto
) ||
1300 nla_put_be16(skb
, NFTA_CT_EXPECT_DPORT
, priv
->dport
) ||
1301 nla_put_u32(skb
, NFTA_CT_EXPECT_TIMEOUT
, priv
->timeout
) ||
1302 nla_put_u8(skb
, NFTA_CT_EXPECT_SIZE
, priv
->size
))
1308 static void nft_ct_expect_obj_eval(struct nft_object
*obj
,
1309 struct nft_regs
*regs
,
1310 const struct nft_pktinfo
*pkt
)
1312 const struct nft_ct_expect_obj
*priv
= nft_obj_data(obj
);
1313 struct nf_conntrack_expect
*exp
;
1314 enum ip_conntrack_info ctinfo
;
1315 struct nf_conn_help
*help
;
1316 enum ip_conntrack_dir dir
;
1317 u16 l3num
= priv
->l3num
;
1320 ct
= nf_ct_get(pkt
->skb
, &ctinfo
);
1321 if (!ct
|| nf_ct_is_confirmed(ct
) || nf_ct_is_template(ct
)) {
1322 regs
->verdict
.code
= NFT_BREAK
;
1325 dir
= CTINFO2DIR(ctinfo
);
1327 help
= nfct_help(ct
);
1329 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
1331 regs
->verdict
.code
= NF_DROP
;
1335 if (help
->expecting
[NF_CT_EXPECT_CLASS_DEFAULT
] >= priv
->size
) {
1336 regs
->verdict
.code
= NFT_BREAK
;
1339 if (l3num
== NFPROTO_INET
)
1340 l3num
= nf_ct_l3num(ct
);
1342 exp
= nf_ct_expect_alloc(ct
);
1344 regs
->verdict
.code
= NF_DROP
;
1347 nf_ct_expect_init(exp
, NF_CT_EXPECT_CLASS_DEFAULT
, l3num
,
1348 &ct
->tuplehash
[!dir
].tuple
.src
.u3
,
1349 &ct
->tuplehash
[!dir
].tuple
.dst
.u3
,
1350 priv
->l4proto
, NULL
, &priv
->dport
);
1351 exp
->timeout
.expires
= jiffies
+ priv
->timeout
* HZ
;
1353 if (nf_ct_expect_related(exp
, 0) != 0)
1354 regs
->verdict
.code
= NF_DROP
;
1357 static const struct nla_policy nft_ct_expect_policy
[NFTA_CT_EXPECT_MAX
+ 1] = {
1358 [NFTA_CT_EXPECT_L3PROTO
] = { .type
= NLA_U16
},
1359 [NFTA_CT_EXPECT_L4PROTO
] = { .type
= NLA_U8
},
1360 [NFTA_CT_EXPECT_DPORT
] = { .type
= NLA_U16
},
1361 [NFTA_CT_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
1362 [NFTA_CT_EXPECT_SIZE
] = { .type
= NLA_U8
},
1365 static struct nft_object_type nft_ct_expect_obj_type
;
1367 static const struct nft_object_ops nft_ct_expect_obj_ops
= {
1368 .type
= &nft_ct_expect_obj_type
,
1369 .size
= sizeof(struct nft_ct_expect_obj
),
1370 .eval
= nft_ct_expect_obj_eval
,
1371 .init
= nft_ct_expect_obj_init
,
1372 .destroy
= nft_ct_expect_obj_destroy
,
1373 .dump
= nft_ct_expect_obj_dump
,
1376 static struct nft_object_type nft_ct_expect_obj_type __read_mostly
= {
1377 .type
= NFT_OBJECT_CT_EXPECT
,
1378 .ops
= &nft_ct_expect_obj_ops
,
1379 .maxattr
= NFTA_CT_EXPECT_MAX
,
1380 .policy
= nft_ct_expect_policy
,
1381 .owner
= THIS_MODULE
,
1384 static int __init
nft_ct_module_init(void)
1388 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE
> NFT_REG_SIZE
);
1390 err
= nft_register_expr(&nft_ct_type
);
1394 err
= nft_register_expr(&nft_notrack_type
);
1398 err
= nft_register_obj(&nft_ct_helper_obj_type
);
1402 err
= nft_register_obj(&nft_ct_expect_obj_type
);
1405 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1406 err
= nft_register_obj(&nft_ct_timeout_obj_type
);
1412 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1414 nft_unregister_obj(&nft_ct_expect_obj_type
);
1417 nft_unregister_obj(&nft_ct_helper_obj_type
);
1419 nft_unregister_expr(&nft_notrack_type
);
1421 nft_unregister_expr(&nft_ct_type
);
1425 static void __exit
nft_ct_module_exit(void)
1427 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1428 nft_unregister_obj(&nft_ct_timeout_obj_type
);
1430 nft_unregister_obj(&nft_ct_expect_obj_type
);
1431 nft_unregister_obj(&nft_ct_helper_obj_type
);
1432 nft_unregister_expr(&nft_notrack_type
);
1433 nft_unregister_expr(&nft_ct_type
);
1436 module_init(nft_ct_module_init
);
1437 module_exit(nft_ct_module_exit
);
1439 MODULE_LICENSE("GPL");
1440 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1441 MODULE_ALIAS_NFT_EXPR("ct");
1442 MODULE_ALIAS_NFT_EXPR("notrack");
1443 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_HELPER
);
1444 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_TIMEOUT
);
1445 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_EXPECT
);
1446 MODULE_DESCRIPTION("Netfilter nf_tables conntrack module");