2 * (C) 2013 Astaro GmbH & Co KG
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <net/netfilter/nf_conntrack.h>
12 #include <net/netfilter/nf_conntrack_ecache.h>
13 #include <net/netfilter/nf_conntrack_labels.h>
14 #include <linux/netfilter/x_tables.h>
16 MODULE_LICENSE("GPL");
17 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
18 MODULE_DESCRIPTION("Xtables: add/match connection trackling labels");
19 MODULE_ALIAS("ipt_connlabel");
20 MODULE_ALIAS("ip6t_connlabel");
23 connlabel_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
25 const struct xt_connlabel_mtinfo
*info
= par
->matchinfo
;
26 enum ip_conntrack_info ctinfo
;
27 struct nf_conn_labels
*labels
;
29 bool invert
= info
->options
& XT_CONNLABEL_OP_INVERT
;
31 ct
= nf_ct_get(skb
, &ctinfo
);
35 labels
= nf_ct_labels_find(ct
);
39 if (test_bit(info
->bit
, labels
->bits
))
42 if (info
->options
& XT_CONNLABEL_OP_SET
) {
43 if (!test_and_set_bit(info
->bit
, labels
->bits
))
44 nf_conntrack_event_cache(IPCT_LABEL
, ct
);
52 static int connlabel_mt_check(const struct xt_mtchk_param
*par
)
54 const int options
= XT_CONNLABEL_OP_INVERT
|
56 struct xt_connlabel_mtinfo
*info
= par
->matchinfo
;
59 if (info
->options
& ~options
) {
60 pr_err("Unknown options in mask %x\n", info
->options
);
64 ret
= nf_ct_netns_get(par
->net
, par
->family
);
66 pr_info("cannot load conntrack support for proto=%u\n",
71 ret
= nf_connlabels_get(par
->net
, info
->bit
);
73 nf_ct_netns_put(par
->net
, par
->family
);
77 static void connlabel_mt_destroy(const struct xt_mtdtor_param
*par
)
79 nf_connlabels_put(par
->net
);
80 nf_ct_netns_put(par
->net
, par
->family
);
83 static struct xt_match connlabels_mt_reg __read_mostly
= {
85 .family
= NFPROTO_UNSPEC
,
86 .checkentry
= connlabel_mt_check
,
87 .match
= connlabel_mt
,
88 .matchsize
= sizeof(struct xt_connlabel_mtinfo
),
89 .destroy
= connlabel_mt_destroy
,
93 static int __init
connlabel_mt_init(void)
95 return xt_register_match(&connlabels_mt_reg
);
98 static void __exit
connlabel_mt_exit(void)
100 xt_unregister_match(&connlabels_mt_reg
);
103 module_init(connlabel_mt_init
);
104 module_exit(connlabel_mt_exit
);