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.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;
36 struct nft_ct_helper_obj {
37 struct nf_conntrack_helper *helper4;
38 struct nf_conntrack_helper *helper6;
42 #ifdef CONFIG_NF_CONNTRACK_ZONES
43 static DEFINE_PER_CPU(struct nf_conn *, nft_ct_pcpu_template);
44 static unsigned int nft_ct_pcpu_template_refcnt __read_mostly;
45 static DEFINE_MUTEX(nft_ct_pcpu_mutex);
48 static u64 nft_ct_get_eval_counter(const struct nf_conn_counter *c,
50 enum ip_conntrack_dir d)
52 if (d < IP_CT_DIR_MAX)
53 return k == NFT_CT_BYTES ? atomic64_read(&c[d].bytes) :
54 atomic64_read(&c[d].packets);
56 return nft_ct_get_eval_counter(c, k, IP_CT_DIR_ORIGINAL) +
57 nft_ct_get_eval_counter(c, k, IP_CT_DIR_REPLY);
60 static void nft_ct_get_eval(const struct nft_expr *expr,
61 struct nft_regs *regs,
62 const struct nft_pktinfo *pkt)
64 const struct nft_ct *priv = nft_expr_priv(expr);
65 u32 *dest = ®s->data[priv->dreg];
66 enum ip_conntrack_info ctinfo;
67 const struct nf_conn *ct;
68 const struct nf_conn_help *help;
69 const struct nf_conntrack_tuple *tuple;
70 const struct nf_conntrack_helper *helper;
73 ct = nf_ct_get(pkt->skb, &ctinfo);
78 state = NF_CT_STATE_BIT(ctinfo);
79 else if (ctinfo == IP_CT_UNTRACKED)
80 state = NF_CT_STATE_UNTRACKED_BIT;
82 state = NF_CT_STATE_INVALID_BIT;
93 case NFT_CT_DIRECTION:
94 nft_reg_store8(dest, CTINFO2DIR(ctinfo));
99 #ifdef CONFIG_NF_CONNTRACK_MARK
101 *dest = READ_ONCE(ct->mark);
104 #ifdef CONFIG_NF_CONNTRACK_SECMARK
109 case NFT_CT_EXPIRATION:
110 *dest = jiffies_to_msecs(nf_ct_expires(ct));
113 if (ct->master == NULL)
115 help = nfct_help(ct->master);
118 helper = rcu_dereference(help->helper);
121 strncpy((char *)dest, helper->name, NF_CT_HELPER_NAME_LEN);
123 #ifdef CONFIG_NF_CONNTRACK_LABELS
124 case NFT_CT_LABELS: {
125 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
128 memcpy(dest, labels->bits, NF_CT_LABELS_MAX_SIZE);
130 memset(dest, 0, NF_CT_LABELS_MAX_SIZE);
136 const struct nf_conn_acct *acct = nf_conn_acct_find(ct);
140 count = nft_ct_get_eval_counter(acct->counter,
141 priv->key, priv->dir);
142 memcpy(dest, &count, sizeof(count));
145 case NFT_CT_AVGPKT: {
146 const struct nf_conn_acct *acct = nf_conn_acct_find(ct);
147 u64 avgcnt = 0, bcnt = 0, pcnt = 0;
150 pcnt = nft_ct_get_eval_counter(acct->counter,
151 NFT_CT_PKTS, priv->dir);
152 bcnt = nft_ct_get_eval_counter(acct->counter,
153 NFT_CT_BYTES, priv->dir);
155 avgcnt = div64_u64(bcnt, pcnt);
158 memcpy(dest, &avgcnt, sizeof(avgcnt));
161 case NFT_CT_L3PROTOCOL:
162 nft_reg_store8(dest, nf_ct_l3num(ct));
164 case NFT_CT_PROTOCOL:
165 nft_reg_store8(dest, nf_ct_protonum(ct));
167 #ifdef CONFIG_NF_CONNTRACK_ZONES
169 const struct nf_conntrack_zone *zone = nf_ct_zone(ct);
172 if (priv->dir < IP_CT_DIR_MAX)
173 zoneid = nf_ct_zone_id(zone, priv->dir);
177 nft_reg_store16(dest, zoneid);
182 *dest = nf_ct_get_id(ct);
188 tuple = &ct->tuplehash[priv->dir].tuple;
191 memcpy(dest, tuple->src.u3.all,
192 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
195 memcpy(dest, tuple->dst.u3.all,
196 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
198 case NFT_CT_PROTO_SRC:
199 nft_reg_store16(dest, (__force u16)tuple->src.u.all);
201 case NFT_CT_PROTO_DST:
202 nft_reg_store16(dest, (__force u16)tuple->dst.u.all);
205 if (nf_ct_l3num(ct) != NFPROTO_IPV4)
207 *dest = (__force __u32)tuple->src.u3.ip;
210 if (nf_ct_l3num(ct) != NFPROTO_IPV4)
212 *dest = (__force __u32)tuple->dst.u3.ip;
215 if (nf_ct_l3num(ct) != NFPROTO_IPV6)
217 memcpy(dest, tuple->src.u3.ip6, sizeof(struct in6_addr));
220 if (nf_ct_l3num(ct) != NFPROTO_IPV6)
222 memcpy(dest, tuple->dst.u3.ip6, sizeof(struct in6_addr));
229 regs->verdict.code = NFT_BREAK;
232 #ifdef CONFIG_NF_CONNTRACK_ZONES
233 static void nft_ct_set_zone_eval(const struct nft_expr *expr,
234 struct nft_regs *regs,
235 const struct nft_pktinfo *pkt)
237 struct nf_conntrack_zone zone = { .dir = NF_CT_DEFAULT_ZONE_DIR };
238 const struct nft_ct *priv = nft_expr_priv(expr);
239 struct sk_buff *skb = pkt->skb;
240 enum ip_conntrack_info ctinfo;
241 u16 value = nft_reg_load16(®s->data[priv->sreg]);
244 ct = nf_ct_get(skb, &ctinfo);
245 if (ct) /* already tracked */
251 case IP_CT_DIR_ORIGINAL:
252 zone.dir = NF_CT_ZONE_DIR_ORIG;
254 case IP_CT_DIR_REPLY:
255 zone.dir = NF_CT_ZONE_DIR_REPL;
261 ct = this_cpu_read(nft_ct_pcpu_template);
263 if (likely(refcount_read(&ct->ct_general.use) == 1)) {
264 refcount_inc(&ct->ct_general.use);
265 nf_ct_zone_add(ct, &zone);
267 /* previous skb got queued to userspace, allocate temporary
268 * one until percpu template can be reused.
270 ct = nf_ct_tmpl_alloc(nft_net(pkt), &zone, GFP_ATOMIC);
272 regs->verdict.code = NF_DROP;
277 nf_ct_set(skb, ct, IP_CT_NEW);
281 static void nft_ct_set_eval(const struct nft_expr *expr,
282 struct nft_regs *regs,
283 const struct nft_pktinfo *pkt)
285 const struct nft_ct *priv = nft_expr_priv(expr);
286 struct sk_buff *skb = pkt->skb;
287 #if defined(CONFIG_NF_CONNTRACK_MARK) || defined(CONFIG_NF_CONNTRACK_SECMARK)
288 u32 value = regs->data[priv->sreg];
290 enum ip_conntrack_info ctinfo;
293 ct = nf_ct_get(skb, &ctinfo);
294 if (ct == NULL || nf_ct_is_template(ct))
298 #ifdef CONFIG_NF_CONNTRACK_MARK
300 if (READ_ONCE(ct->mark) != value) {
301 WRITE_ONCE(ct->mark, value);
302 nf_conntrack_event_cache(IPCT_MARK, ct);
306 #ifdef CONFIG_NF_CONNTRACK_SECMARK
308 if (ct->secmark != value) {
310 nf_conntrack_event_cache(IPCT_SECMARK, ct);
314 #ifdef CONFIG_NF_CONNTRACK_LABELS
316 nf_connlabels_replace(ct,
317 ®s->data[priv->sreg],
318 ®s->data[priv->sreg],
319 NF_CT_LABELS_MAX_SIZE / sizeof(u32));
322 #ifdef CONFIG_NF_CONNTRACK_EVENTS
323 case NFT_CT_EVENTMASK: {
324 struct nf_conntrack_ecache *e = nf_ct_ecache_find(ct);
325 u32 ctmask = regs->data[priv->sreg];
328 if (e->ctmask != ctmask)
333 if (ctmask && !nf_ct_is_confirmed(ct))
334 nf_ct_ecache_ext_add(ct, ctmask, 0, GFP_ATOMIC);
343 static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
344 [NFTA_CT_DREG] = { .type = NLA_U32 },
345 [NFTA_CT_KEY] = { .type = NLA_U32 },
346 [NFTA_CT_DIRECTION] = { .type = NLA_U8 },
347 [NFTA_CT_SREG] = { .type = NLA_U32 },
350 #ifdef CONFIG_NF_CONNTRACK_ZONES
351 static void nft_ct_tmpl_put_pcpu(void)
356 for_each_possible_cpu(cpu) {
357 ct = per_cpu(nft_ct_pcpu_template, cpu);
361 per_cpu(nft_ct_pcpu_template, cpu) = NULL;
365 static bool nft_ct_tmpl_alloc_pcpu(void)
367 struct nf_conntrack_zone zone = { .id = 0 };
371 if (nft_ct_pcpu_template_refcnt)
374 for_each_possible_cpu(cpu) {
375 tmp = nf_ct_tmpl_alloc(&init_net, &zone, GFP_KERNEL);
377 nft_ct_tmpl_put_pcpu();
381 per_cpu(nft_ct_pcpu_template, cpu) = tmp;
388 static int nft_ct_get_init(const struct nft_ctx *ctx,
389 const struct nft_expr *expr,
390 const struct nlattr * const tb[])
392 struct nft_ct *priv = nft_expr_priv(expr);
396 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
397 priv->dir = IP_CT_DIR_MAX;
399 case NFT_CT_DIRECTION:
400 if (tb[NFTA_CT_DIRECTION] != NULL)
406 #ifdef CONFIG_NF_CONNTRACK_MARK
409 #ifdef CONFIG_NF_CONNTRACK_SECMARK
412 case NFT_CT_EXPIRATION:
413 if (tb[NFTA_CT_DIRECTION] != NULL)
417 #ifdef CONFIG_NF_CONNTRACK_LABELS
419 if (tb[NFTA_CT_DIRECTION] != NULL)
421 len = NF_CT_LABELS_MAX_SIZE;
425 if (tb[NFTA_CT_DIRECTION] != NULL)
427 len = NF_CT_HELPER_NAME_LEN;
430 case NFT_CT_L3PROTOCOL:
431 case NFT_CT_PROTOCOL:
432 /* For compatibility, do not report error if NFTA_CT_DIRECTION
433 * attribute is specified.
439 if (tb[NFTA_CT_DIRECTION] == NULL)
442 switch (ctx->family) {
444 len = sizeof_field(struct nf_conntrack_tuple,
449 len = sizeof_field(struct nf_conntrack_tuple,
453 return -EAFNOSUPPORT;
458 if (tb[NFTA_CT_DIRECTION] == NULL)
461 len = sizeof_field(struct nf_conntrack_tuple, src.u3.ip);
465 if (tb[NFTA_CT_DIRECTION] == NULL)
468 len = sizeof_field(struct nf_conntrack_tuple, src.u3.ip6);
470 case NFT_CT_PROTO_SRC:
471 case NFT_CT_PROTO_DST:
472 if (tb[NFTA_CT_DIRECTION] == NULL)
474 len = sizeof_field(struct nf_conntrack_tuple, src.u.all);
481 #ifdef CONFIG_NF_CONNTRACK_ZONES
493 if (tb[NFTA_CT_DIRECTION] != NULL) {
494 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
496 case IP_CT_DIR_ORIGINAL:
497 case IP_CT_DIR_REPLY:
505 err = nft_parse_register_store(ctx, tb[NFTA_CT_DREG], &priv->dreg, NULL,
506 NFT_DATA_VALUE, len);
510 err = nf_ct_netns_get(ctx->net, ctx->family);
514 if (priv->key == NFT_CT_BYTES ||
515 priv->key == NFT_CT_PKTS ||
516 priv->key == NFT_CT_AVGPKT)
517 nf_ct_set_acct(ctx->net, true);
522 static void __nft_ct_set_destroy(const struct nft_ctx *ctx, struct nft_ct *priv)
525 #ifdef CONFIG_NF_CONNTRACK_LABELS
527 nf_connlabels_put(ctx->net);
530 #ifdef CONFIG_NF_CONNTRACK_ZONES
532 mutex_lock(&nft_ct_pcpu_mutex);
533 if (--nft_ct_pcpu_template_refcnt == 0)
534 nft_ct_tmpl_put_pcpu();
535 mutex_unlock(&nft_ct_pcpu_mutex);
543 static int nft_ct_set_init(const struct nft_ctx *ctx,
544 const struct nft_expr *expr,
545 const struct nlattr * const tb[])
547 struct nft_ct *priv = nft_expr_priv(expr);
551 priv->dir = IP_CT_DIR_MAX;
552 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
554 #ifdef CONFIG_NF_CONNTRACK_MARK
556 if (tb[NFTA_CT_DIRECTION])
558 len = sizeof_field(struct nf_conn, mark);
561 #ifdef CONFIG_NF_CONNTRACK_LABELS
563 if (tb[NFTA_CT_DIRECTION])
565 len = NF_CT_LABELS_MAX_SIZE;
566 err = nf_connlabels_get(ctx->net, (len * BITS_PER_BYTE) - 1);
571 #ifdef CONFIG_NF_CONNTRACK_ZONES
573 mutex_lock(&nft_ct_pcpu_mutex);
574 if (!nft_ct_tmpl_alloc_pcpu()) {
575 mutex_unlock(&nft_ct_pcpu_mutex);
578 nft_ct_pcpu_template_refcnt++;
579 mutex_unlock(&nft_ct_pcpu_mutex);
583 #ifdef CONFIG_NF_CONNTRACK_EVENTS
584 case NFT_CT_EVENTMASK:
585 if (tb[NFTA_CT_DIRECTION])
590 #ifdef CONFIG_NF_CONNTRACK_SECMARK
592 if (tb[NFTA_CT_DIRECTION])
601 if (tb[NFTA_CT_DIRECTION]) {
602 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
604 case IP_CT_DIR_ORIGINAL:
605 case IP_CT_DIR_REPLY:
614 err = nft_parse_register_load(tb[NFTA_CT_SREG], &priv->sreg, len);
618 err = nf_ct_netns_get(ctx->net, ctx->family);
625 __nft_ct_set_destroy(ctx, priv);
629 static void nft_ct_get_destroy(const struct nft_ctx *ctx,
630 const struct nft_expr *expr)
632 nf_ct_netns_put(ctx->net, ctx->family);
635 static void nft_ct_set_destroy(const struct nft_ctx *ctx,
636 const struct nft_expr *expr)
638 struct nft_ct *priv = nft_expr_priv(expr);
640 __nft_ct_set_destroy(ctx, priv);
641 nf_ct_netns_put(ctx->net, ctx->family);
644 static int nft_ct_get_dump(struct sk_buff *skb,
645 const struct nft_expr *expr, bool reset)
647 const struct nft_ct *priv = nft_expr_priv(expr);
649 if (nft_dump_register(skb, NFTA_CT_DREG, priv->dreg))
650 goto nla_put_failure;
651 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
652 goto nla_put_failure;
661 case NFT_CT_PROTO_SRC:
662 case NFT_CT_PROTO_DST:
663 if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
664 goto nla_put_failure;
670 if (priv->dir < IP_CT_DIR_MAX &&
671 nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
672 goto nla_put_failure;
684 static bool nft_ct_get_reduce(struct nft_regs_track *track,
685 const struct nft_expr *expr)
687 const struct nft_ct *priv = nft_expr_priv(expr);
688 const struct nft_ct *ct;
690 if (!nft_reg_track_cmp(track, expr, priv->dreg)) {
691 nft_reg_track_update(track, expr, priv->dreg, priv->len);
695 ct = nft_expr_priv(track->regs[priv->dreg].selector);
696 if (priv->key != ct->key) {
697 nft_reg_track_update(track, expr, priv->dreg, priv->len);
701 if (!track->regs[priv->dreg].bitwise)
704 return nft_expr_reduce_bitwise(track, expr);
707 static int nft_ct_set_dump(struct sk_buff *skb,
708 const struct nft_expr *expr, bool reset)
710 const struct nft_ct *priv = nft_expr_priv(expr);
712 if (nft_dump_register(skb, NFTA_CT_SREG, priv->sreg))
713 goto nla_put_failure;
714 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
715 goto nla_put_failure;
719 if (priv->dir < IP_CT_DIR_MAX &&
720 nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
721 goto nla_put_failure;
733 static struct nft_expr_type nft_ct_type;
734 static const struct nft_expr_ops nft_ct_get_ops = {
735 .type = &nft_ct_type,
736 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
737 .eval = nft_ct_get_eval,
738 .init = nft_ct_get_init,
739 .destroy = nft_ct_get_destroy,
740 .dump = nft_ct_get_dump,
741 .reduce = nft_ct_get_reduce,
744 static bool nft_ct_set_reduce(struct nft_regs_track *track,
745 const struct nft_expr *expr)
749 for (i = 0; i < NFT_REG32_NUM; i++) {
750 if (!track->regs[i].selector)
753 if (track->regs[i].selector->ops != &nft_ct_get_ops)
756 __nft_reg_track_cancel(track, i);
762 static const struct nft_expr_ops nft_ct_set_ops = {
763 .type = &nft_ct_type,
764 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
765 .eval = nft_ct_set_eval,
766 .init = nft_ct_set_init,
767 .destroy = nft_ct_set_destroy,
768 .dump = nft_ct_set_dump,
769 .reduce = nft_ct_set_reduce,
772 #ifdef CONFIG_NF_CONNTRACK_ZONES
773 static const struct nft_expr_ops nft_ct_set_zone_ops = {
774 .type = &nft_ct_type,
775 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
776 .eval = nft_ct_set_zone_eval,
777 .init = nft_ct_set_init,
778 .destroy = nft_ct_set_destroy,
779 .dump = nft_ct_set_dump,
780 .reduce = nft_ct_set_reduce,
784 static const struct nft_expr_ops *
785 nft_ct_select_ops(const struct nft_ctx *ctx,
786 const struct nlattr * const tb[])
788 if (tb[NFTA_CT_KEY] == NULL)
789 return ERR_PTR(-EINVAL);
791 if (tb[NFTA_CT_DREG] && tb[NFTA_CT_SREG])
792 return ERR_PTR(-EINVAL);
794 if (tb[NFTA_CT_DREG])
795 return &nft_ct_get_ops;
797 if (tb[NFTA_CT_SREG]) {
798 #ifdef CONFIG_NF_CONNTRACK_ZONES
799 if (nla_get_be32(tb[NFTA_CT_KEY]) == htonl(NFT_CT_ZONE))
800 return &nft_ct_set_zone_ops;
802 return &nft_ct_set_ops;
805 return ERR_PTR(-EINVAL);
808 static struct nft_expr_type nft_ct_type __read_mostly = {
810 .select_ops = nft_ct_select_ops,
811 .policy = nft_ct_policy,
812 .maxattr = NFTA_CT_MAX,
813 .owner = THIS_MODULE,
816 static void nft_notrack_eval(const struct nft_expr *expr,
817 struct nft_regs *regs,
818 const struct nft_pktinfo *pkt)
820 struct sk_buff *skb = pkt->skb;
821 enum ip_conntrack_info ctinfo;
824 ct = nf_ct_get(pkt->skb, &ctinfo);
825 /* Previously seen (loopback or untracked)? Ignore. */
826 if (ct || ctinfo == IP_CT_UNTRACKED)
829 nf_ct_set(skb, ct, IP_CT_UNTRACKED);
832 static struct nft_expr_type nft_notrack_type;
833 static const struct nft_expr_ops nft_notrack_ops = {
834 .type = &nft_notrack_type,
835 .size = NFT_EXPR_SIZE(0),
836 .eval = nft_notrack_eval,
837 .reduce = NFT_REDUCE_READONLY,
840 static struct nft_expr_type nft_notrack_type __read_mostly = {
842 .ops = &nft_notrack_ops,
843 .owner = THIS_MODULE,
846 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
848 nft_ct_timeout_parse_policy(void *timeouts,
849 const struct nf_conntrack_l4proto *l4proto,
850 struct net *net, const struct nlattr *attr)
855 tb = kcalloc(l4proto->ctnl_timeout.nlattr_max + 1, sizeof(*tb),
861 ret = nla_parse_nested_deprecated(tb,
862 l4proto->ctnl_timeout.nlattr_max,
864 l4proto->ctnl_timeout.nla_policy,
869 ret = l4proto->ctnl_timeout.nlattr_to_obj(tb, net, timeouts);
876 struct nft_ct_timeout_obj {
877 struct nf_ct_timeout *timeout;
881 static void nft_ct_timeout_obj_eval(struct nft_object *obj,
882 struct nft_regs *regs,
883 const struct nft_pktinfo *pkt)
885 const struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
886 struct nf_conn *ct = (struct nf_conn *)skb_nfct(pkt->skb);
887 struct nf_conn_timeout *timeout;
888 const unsigned int *values;
890 if (priv->l4proto != pkt->tprot)
893 if (!ct || nf_ct_is_template(ct) || nf_ct_is_confirmed(ct))
896 timeout = nf_ct_timeout_find(ct);
898 timeout = nf_ct_timeout_ext_add(ct, priv->timeout, GFP_ATOMIC);
900 regs->verdict.code = NF_DROP;
905 rcu_assign_pointer(timeout->timeout, priv->timeout);
907 /* adjust the timeout as per 'new' state. ct is unconfirmed,
908 * so the current timestamp must not be added.
910 values = nf_ct_timeout_data(timeout);
912 nf_ct_refresh(ct, pkt->skb, values[0]);
915 static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx,
916 const struct nlattr * const tb[],
917 struct nft_object *obj)
919 struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
920 const struct nf_conntrack_l4proto *l4proto;
921 struct nf_ct_timeout *timeout;
922 int l3num = ctx->family;
926 if (!tb[NFTA_CT_TIMEOUT_L4PROTO] ||
927 !tb[NFTA_CT_TIMEOUT_DATA])
930 if (tb[NFTA_CT_TIMEOUT_L3PROTO])
931 l3num = ntohs(nla_get_be16(tb[NFTA_CT_TIMEOUT_L3PROTO]));
933 l4num = nla_get_u8(tb[NFTA_CT_TIMEOUT_L4PROTO]);
934 priv->l4proto = l4num;
936 l4proto = nf_ct_l4proto_find(l4num);
938 if (l4proto->l4proto != l4num) {
943 timeout = kzalloc(sizeof(struct nf_ct_timeout) +
944 l4proto->ctnl_timeout.obj_size, GFP_KERNEL);
945 if (timeout == NULL) {
950 ret = nft_ct_timeout_parse_policy(&timeout->data, l4proto, ctx->net,
951 tb[NFTA_CT_TIMEOUT_DATA]);
953 goto err_free_timeout;
955 timeout->l3num = l3num;
956 timeout->l4proto = l4proto;
958 ret = nf_ct_netns_get(ctx->net, ctx->family);
960 goto err_free_timeout;
962 priv->timeout = timeout;
971 static void nft_ct_timeout_obj_destroy(const struct nft_ctx *ctx,
972 struct nft_object *obj)
974 struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
975 struct nf_ct_timeout *timeout = priv->timeout;
977 nf_ct_untimeout(ctx->net, timeout);
978 nf_ct_netns_put(ctx->net, ctx->family);
979 kfree(priv->timeout);
982 static int nft_ct_timeout_obj_dump(struct sk_buff *skb,
983 struct nft_object *obj, bool reset)
985 const struct nft_ct_timeout_obj *priv = nft_obj_data(obj);
986 const struct nf_ct_timeout *timeout = priv->timeout;
987 struct nlattr *nest_params;
990 if (nla_put_u8(skb, NFTA_CT_TIMEOUT_L4PROTO, timeout->l4proto->l4proto) ||
991 nla_put_be16(skb, NFTA_CT_TIMEOUT_L3PROTO, htons(timeout->l3num)))
994 nest_params = nla_nest_start(skb, NFTA_CT_TIMEOUT_DATA);
998 ret = timeout->l4proto->ctnl_timeout.obj_to_nlattr(skb, &timeout->data);
1001 nla_nest_end(skb, nest_params);
1005 static const struct nla_policy nft_ct_timeout_policy[NFTA_CT_TIMEOUT_MAX + 1] = {
1006 [NFTA_CT_TIMEOUT_L3PROTO] = {.type = NLA_U16 },
1007 [NFTA_CT_TIMEOUT_L4PROTO] = {.type = NLA_U8 },
1008 [NFTA_CT_TIMEOUT_DATA] = {.type = NLA_NESTED },
1011 static struct nft_object_type nft_ct_timeout_obj_type;
1013 static const struct nft_object_ops nft_ct_timeout_obj_ops = {
1014 .type = &nft_ct_timeout_obj_type,
1015 .size = sizeof(struct nft_ct_timeout_obj),
1016 .eval = nft_ct_timeout_obj_eval,
1017 .init = nft_ct_timeout_obj_init,
1018 .destroy = nft_ct_timeout_obj_destroy,
1019 .dump = nft_ct_timeout_obj_dump,
1022 static struct nft_object_type nft_ct_timeout_obj_type __read_mostly = {
1023 .type = NFT_OBJECT_CT_TIMEOUT,
1024 .ops = &nft_ct_timeout_obj_ops,
1025 .maxattr = NFTA_CT_TIMEOUT_MAX,
1026 .policy = nft_ct_timeout_policy,
1027 .owner = THIS_MODULE,
1029 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
1031 static int nft_ct_helper_obj_init(const struct nft_ctx *ctx,
1032 const struct nlattr * const tb[],
1033 struct nft_object *obj)
1035 struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1036 struct nf_conntrack_helper *help4, *help6;
1037 char name[NF_CT_HELPER_NAME_LEN];
1038 int family = ctx->family;
1041 if (!tb[NFTA_CT_HELPER_NAME] || !tb[NFTA_CT_HELPER_L4PROTO])
1044 priv->l4proto = nla_get_u8(tb[NFTA_CT_HELPER_L4PROTO]);
1048 nla_strscpy(name, tb[NFTA_CT_HELPER_NAME], sizeof(name));
1050 if (tb[NFTA_CT_HELPER_L3PROTO])
1051 family = ntohs(nla_get_be16(tb[NFTA_CT_HELPER_L3PROTO]));
1058 if (ctx->family == NFPROTO_IPV6)
1061 help4 = nf_conntrack_helper_try_module_get(name, family,
1065 if (ctx->family == NFPROTO_IPV4)
1068 help6 = nf_conntrack_helper_try_module_get(name, family,
1071 case NFPROTO_NETDEV:
1072 case NFPROTO_BRIDGE:
1074 help4 = nf_conntrack_helper_try_module_get(name, NFPROTO_IPV4,
1076 help6 = nf_conntrack_helper_try_module_get(name, NFPROTO_IPV6,
1080 return -EAFNOSUPPORT;
1083 /* && is intentional; only error if INET found neither ipv4 or ipv6 */
1084 if (!help4 && !help6)
1087 priv->helper4 = help4;
1088 priv->helper6 = help6;
1090 err = nf_ct_netns_get(ctx->net, ctx->family);
1092 goto err_put_helper;
1098 nf_conntrack_helper_put(priv->helper4);
1100 nf_conntrack_helper_put(priv->helper6);
1104 static void nft_ct_helper_obj_destroy(const struct nft_ctx *ctx,
1105 struct nft_object *obj)
1107 struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1110 nf_conntrack_helper_put(priv->helper4);
1112 nf_conntrack_helper_put(priv->helper6);
1114 nf_ct_netns_put(ctx->net, ctx->family);
1117 static void nft_ct_helper_obj_eval(struct nft_object *obj,
1118 struct nft_regs *regs,
1119 const struct nft_pktinfo *pkt)
1121 const struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1122 struct nf_conn *ct = (struct nf_conn *)skb_nfct(pkt->skb);
1123 struct nf_conntrack_helper *to_assign = NULL;
1124 struct nf_conn_help *help;
1127 nf_ct_is_confirmed(ct) ||
1128 nf_ct_is_template(ct) ||
1129 priv->l4proto != nf_ct_protonum(ct))
1132 switch (nf_ct_l3num(ct)) {
1134 to_assign = priv->helper4;
1137 to_assign = priv->helper6;
1147 if (test_bit(IPS_HELPER_BIT, &ct->status))
1150 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1152 rcu_assign_pointer(help->helper, to_assign);
1153 set_bit(IPS_HELPER_BIT, &ct->status);
1157 static int nft_ct_helper_obj_dump(struct sk_buff *skb,
1158 struct nft_object *obj, bool reset)
1160 const struct nft_ct_helper_obj *priv = nft_obj_data(obj);
1161 const struct nf_conntrack_helper *helper;
1164 if (priv->helper4 && priv->helper6) {
1165 family = NFPROTO_INET;
1166 helper = priv->helper4;
1167 } else if (priv->helper6) {
1168 family = NFPROTO_IPV6;
1169 helper = priv->helper6;
1171 family = NFPROTO_IPV4;
1172 helper = priv->helper4;
1175 if (nla_put_string(skb, NFTA_CT_HELPER_NAME, helper->name))
1178 if (nla_put_u8(skb, NFTA_CT_HELPER_L4PROTO, priv->l4proto))
1181 if (nla_put_be16(skb, NFTA_CT_HELPER_L3PROTO, htons(family)))
1187 static const struct nla_policy nft_ct_helper_policy[NFTA_CT_HELPER_MAX + 1] = {
1188 [NFTA_CT_HELPER_NAME] = { .type = NLA_STRING,
1189 .len = NF_CT_HELPER_NAME_LEN - 1 },
1190 [NFTA_CT_HELPER_L3PROTO] = { .type = NLA_U16 },
1191 [NFTA_CT_HELPER_L4PROTO] = { .type = NLA_U8 },
1194 static struct nft_object_type nft_ct_helper_obj_type;
1195 static const struct nft_object_ops nft_ct_helper_obj_ops = {
1196 .type = &nft_ct_helper_obj_type,
1197 .size = sizeof(struct nft_ct_helper_obj),
1198 .eval = nft_ct_helper_obj_eval,
1199 .init = nft_ct_helper_obj_init,
1200 .destroy = nft_ct_helper_obj_destroy,
1201 .dump = nft_ct_helper_obj_dump,
1204 static struct nft_object_type nft_ct_helper_obj_type __read_mostly = {
1205 .type = NFT_OBJECT_CT_HELPER,
1206 .ops = &nft_ct_helper_obj_ops,
1207 .maxattr = NFTA_CT_HELPER_MAX,
1208 .policy = nft_ct_helper_policy,
1209 .owner = THIS_MODULE,
1212 struct nft_ct_expect_obj {
1220 static int nft_ct_expect_obj_init(const struct nft_ctx *ctx,
1221 const struct nlattr * const tb[],
1222 struct nft_object *obj)
1224 struct nft_ct_expect_obj *priv = nft_obj_data(obj);
1226 if (!tb[NFTA_CT_EXPECT_L4PROTO] ||
1227 !tb[NFTA_CT_EXPECT_DPORT] ||
1228 !tb[NFTA_CT_EXPECT_TIMEOUT] ||
1229 !tb[NFTA_CT_EXPECT_SIZE])
1232 priv->l3num = ctx->family;
1233 if (tb[NFTA_CT_EXPECT_L3PROTO])
1234 priv->l3num = ntohs(nla_get_be16(tb[NFTA_CT_EXPECT_L3PROTO]));
1236 priv->l4proto = nla_get_u8(tb[NFTA_CT_EXPECT_L4PROTO]);
1237 priv->dport = nla_get_be16(tb[NFTA_CT_EXPECT_DPORT]);
1238 priv->timeout = nla_get_u32(tb[NFTA_CT_EXPECT_TIMEOUT]);
1239 priv->size = nla_get_u8(tb[NFTA_CT_EXPECT_SIZE]);
1241 return nf_ct_netns_get(ctx->net, ctx->family);
1244 static void nft_ct_expect_obj_destroy(const struct nft_ctx *ctx,
1245 struct nft_object *obj)
1247 nf_ct_netns_put(ctx->net, ctx->family);
1250 static int nft_ct_expect_obj_dump(struct sk_buff *skb,
1251 struct nft_object *obj, bool reset)
1253 const struct nft_ct_expect_obj *priv = nft_obj_data(obj);
1255 if (nla_put_be16(skb, NFTA_CT_EXPECT_L3PROTO, htons(priv->l3num)) ||
1256 nla_put_u8(skb, NFTA_CT_EXPECT_L4PROTO, priv->l4proto) ||
1257 nla_put_be16(skb, NFTA_CT_EXPECT_DPORT, priv->dport) ||
1258 nla_put_u32(skb, NFTA_CT_EXPECT_TIMEOUT, priv->timeout) ||
1259 nla_put_u8(skb, NFTA_CT_EXPECT_SIZE, priv->size))
1265 static void nft_ct_expect_obj_eval(struct nft_object *obj,
1266 struct nft_regs *regs,
1267 const struct nft_pktinfo *pkt)
1269 const struct nft_ct_expect_obj *priv = nft_obj_data(obj);
1270 struct nf_conntrack_expect *exp;
1271 enum ip_conntrack_info ctinfo;
1272 struct nf_conn_help *help;
1273 enum ip_conntrack_dir dir;
1274 u16 l3num = priv->l3num;
1277 ct = nf_ct_get(pkt->skb, &ctinfo);
1278 if (!ct || nf_ct_is_confirmed(ct) || nf_ct_is_template(ct)) {
1279 regs->verdict.code = NFT_BREAK;
1282 dir = CTINFO2DIR(ctinfo);
1284 help = nfct_help(ct);
1286 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1288 regs->verdict.code = NF_DROP;
1292 if (help->expecting[NF_CT_EXPECT_CLASS_DEFAULT] >= priv->size) {
1293 regs->verdict.code = NFT_BREAK;
1296 if (l3num == NFPROTO_INET)
1297 l3num = nf_ct_l3num(ct);
1299 exp = nf_ct_expect_alloc(ct);
1301 regs->verdict.code = NF_DROP;
1304 nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, l3num,
1305 &ct->tuplehash[!dir].tuple.src.u3,
1306 &ct->tuplehash[!dir].tuple.dst.u3,
1307 priv->l4proto, NULL, &priv->dport);
1308 exp->timeout.expires = jiffies + priv->timeout * HZ;
1310 if (nf_ct_expect_related(exp, 0) != 0)
1311 regs->verdict.code = NF_DROP;
1314 static const struct nla_policy nft_ct_expect_policy[NFTA_CT_EXPECT_MAX + 1] = {
1315 [NFTA_CT_EXPECT_L3PROTO] = { .type = NLA_U16 },
1316 [NFTA_CT_EXPECT_L4PROTO] = { .type = NLA_U8 },
1317 [NFTA_CT_EXPECT_DPORT] = { .type = NLA_U16 },
1318 [NFTA_CT_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1319 [NFTA_CT_EXPECT_SIZE] = { .type = NLA_U8 },
1322 static struct nft_object_type nft_ct_expect_obj_type;
1324 static const struct nft_object_ops nft_ct_expect_obj_ops = {
1325 .type = &nft_ct_expect_obj_type,
1326 .size = sizeof(struct nft_ct_expect_obj),
1327 .eval = nft_ct_expect_obj_eval,
1328 .init = nft_ct_expect_obj_init,
1329 .destroy = nft_ct_expect_obj_destroy,
1330 .dump = nft_ct_expect_obj_dump,
1333 static struct nft_object_type nft_ct_expect_obj_type __read_mostly = {
1334 .type = NFT_OBJECT_CT_EXPECT,
1335 .ops = &nft_ct_expect_obj_ops,
1336 .maxattr = NFTA_CT_EXPECT_MAX,
1337 .policy = nft_ct_expect_policy,
1338 .owner = THIS_MODULE,
1341 static int __init nft_ct_module_init(void)
1345 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE > NFT_REG_SIZE);
1347 err = nft_register_expr(&nft_ct_type);
1351 err = nft_register_expr(&nft_notrack_type);
1355 err = nft_register_obj(&nft_ct_helper_obj_type);
1359 err = nft_register_obj(&nft_ct_expect_obj_type);
1362 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1363 err = nft_register_obj(&nft_ct_timeout_obj_type);
1369 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1371 nft_unregister_obj(&nft_ct_expect_obj_type);
1374 nft_unregister_obj(&nft_ct_helper_obj_type);
1376 nft_unregister_expr(&nft_notrack_type);
1378 nft_unregister_expr(&nft_ct_type);
1382 static void __exit nft_ct_module_exit(void)
1384 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
1385 nft_unregister_obj(&nft_ct_timeout_obj_type);
1387 nft_unregister_obj(&nft_ct_expect_obj_type);
1388 nft_unregister_obj(&nft_ct_helper_obj_type);
1389 nft_unregister_expr(&nft_notrack_type);
1390 nft_unregister_expr(&nft_ct_type);
1393 module_init(nft_ct_module_init);
1394 module_exit(nft_ct_module_exit);
1396 MODULE_LICENSE("GPL");
1398 MODULE_ALIAS_NFT_EXPR("ct");
1399 MODULE_ALIAS_NFT_EXPR("notrack");
1400 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_HELPER);
1401 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_TIMEOUT);
1402 MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_CT_EXPECT);
1403 MODULE_DESCRIPTION("Netfilter nf_tables conntrack module");