blob: 03d66f1c5e69069077fa65b76339554d7c56ee6d [file] [log] [blame]
Florian Westphalc539f012013-01-11 06:30:44 +00001/*
2 * (C) 2013 Astaro GmbH & Co KG
3 *
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.
7 */
8
9#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <net/netfilter/nf_conntrack.h>
Florian Westphal857ed312016-07-21 12:51:17 +020012#include <net/netfilter/nf_conntrack_ecache.h>
Florian Westphalc539f012013-01-11 06:30:44 +000013#include <net/netfilter/nf_conntrack_labels.h>
14#include <linux/netfilter/x_tables.h>
15
16MODULE_LICENSE("GPL");
17MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
18MODULE_DESCRIPTION("Xtables: add/match connection trackling labels");
19MODULE_ALIAS("ipt_connlabel");
20MODULE_ALIAS("ip6t_connlabel");
21
22static bool
23connlabel_mt(const struct sk_buff *skb, struct xt_action_param *par)
24{
25 const struct xt_connlabel_mtinfo *info = par->matchinfo;
26 enum ip_conntrack_info ctinfo;
Florian Westphal857ed312016-07-21 12:51:17 +020027 struct nf_conn_labels *labels;
Florian Westphalc539f012013-01-11 06:30:44 +000028 struct nf_conn *ct;
29 bool invert = info->options & XT_CONNLABEL_OP_INVERT;
30
31 ct = nf_ct_get(skb, &ctinfo);
32 if (ct == NULL || nf_ct_is_untracked(ct))
33 return invert;
34
Florian Westphal857ed312016-07-21 12:51:17 +020035 labels = nf_ct_labels_find(ct);
36 if (!labels)
37 return invert;
Florian Westphalc539f012013-01-11 06:30:44 +000038
Florian Westphal857ed312016-07-21 12:51:17 +020039 if (test_bit(info->bit, labels->bits))
40 return !invert;
41
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);
45
46 return !invert;
47 }
48
49 return invert;
Florian Westphalc539f012013-01-11 06:30:44 +000050}
51
52static int connlabel_mt_check(const struct xt_mtchk_param *par)
53{
54 const int options = XT_CONNLABEL_OP_INVERT |
55 XT_CONNLABEL_OP_SET;
56 struct xt_connlabel_mtinfo *info = par->matchinfo;
57 int ret;
Florian Westphalc539f012013-01-11 06:30:44 +000058
59 if (info->options & ~options) {
60 pr_err("Unknown options in mask %x\n", info->options);
61 return -EINVAL;
62 }
63
64 ret = nf_ct_l3proto_try_module_get(par->family);
65 if (ret < 0) {
66 pr_info("cannot load conntrack support for proto=%u\n",
67 par->family);
68 return ret;
69 }
70
Florian Westphaladff6c62016-04-12 18:14:25 +020071 ret = nf_connlabels_get(par->net, info->bit);
Joe Stringer86ca02e2015-08-26 11:31:51 -070072 if (ret < 0)
73 nf_ct_l3proto_module_put(par->family);
Florian Westphalc539f012013-01-11 06:30:44 +000074 return ret;
75}
76
77static void connlabel_mt_destroy(const struct xt_mtdtor_param *par)
78{
Joe Stringer86ca02e2015-08-26 11:31:51 -070079 nf_connlabels_put(par->net);
Florian Westphalc539f012013-01-11 06:30:44 +000080 nf_ct_l3proto_module_put(par->family);
81}
82
83static struct xt_match connlabels_mt_reg __read_mostly = {
84 .name = "connlabel",
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,
90 .me = THIS_MODULE,
91};
92
93static int __init connlabel_mt_init(void)
94{
95 return xt_register_match(&connlabels_mt_reg);
96}
97
98static void __exit connlabel_mt_exit(void)
99{
100 xt_unregister_match(&connlabels_mt_reg);
101}
102
103module_init(connlabel_mt_init);
104module_exit(connlabel_mt_exit);