Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netfilter: ctnetlink: support kernel-space dump filtering by ctmark

This patch adds CTA_MARK_MASK which, together with CTA_MARK, allows
you to selectively send conntrack entries to user-space by
returning those that match mark & mask.

With this, we can save cycles in the building and the parsing of
the entries that may be later on filtered out in user-space by using
the ctmark & mask.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Pablo Neira Ayuso and committed by
David S. Miller
0f298a28 7175c883

+35 -1
+34 -1
net/netfilter/nf_conntrack_netlink.c
··· 691 691 { 692 692 if (cb->args[1]) 693 693 nf_ct_put((struct nf_conn *)cb->args[1]); 694 + if (cb->data) 695 + kfree(cb->data); 694 696 return 0; 695 697 } 698 + 699 + struct ctnetlink_dump_filter { 700 + struct { 701 + u_int32_t val; 702 + u_int32_t mask; 703 + } mark; 704 + }; 696 705 697 706 static int 698 707 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) ··· 712 703 struct hlist_nulls_node *n; 713 704 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); 714 705 u_int8_t l3proto = nfmsg->nfgen_family; 715 - 706 + #ifdef CONFIG_NF_CONNTRACK_MARK 707 + const struct ctnetlink_dump_filter *filter = cb->data; 708 + #endif 716 709 spin_lock_bh(&nf_conntrack_lock); 717 710 last = (struct nf_conn *)cb->args[1]; 718 711 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) { ··· 734 723 continue; 735 724 cb->args[1] = 0; 736 725 } 726 + #ifdef CONFIG_NF_CONNTRACK_MARK 727 + if (filter && !((ct->mark & filter->mark.mask) == 728 + filter->mark.val)) { 729 + continue; 730 + } 731 + #endif 737 732 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, 738 733 cb->nlh->nlmsg_seq, 739 734 NFNL_MSG_TYPE( ··· 911 894 [CTA_NAT_DST] = { .type = NLA_NESTED }, 912 895 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED }, 913 896 [CTA_ZONE] = { .type = NLA_U16 }, 897 + [CTA_MARK_MASK] = { .type = NLA_U32 }, 914 898 }; 915 899 916 900 static int ··· 1000 982 .dump = ctnetlink_dump_table, 1001 983 .done = ctnetlink_done, 1002 984 }; 985 + #ifdef CONFIG_NF_CONNTRACK_MARK 986 + if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) { 987 + struct ctnetlink_dump_filter *filter; 988 + 989 + filter = kzalloc(sizeof(struct ctnetlink_dump_filter), 990 + GFP_ATOMIC); 991 + if (filter == NULL) 992 + return -ENOMEM; 993 + 994 + filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK])); 995 + filter->mark.mask = 996 + ntohl(nla_get_be32(cda[CTA_MARK_MASK])); 997 + c.data = filter; 998 + } 999 + #endif 1003 1000 return netlink_dump_start(ctnl, skb, nlh, &c); 1004 1001 } 1005 1002