blob: 87505cdad5f15ee9d69ea3dd35d1f6b803aa7510 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Florian Westphalc539f012013-01-11 06:30:44 +00002/*
3 * (C) 2013 Astaro GmbH & Co KG
Florian Westphalc539f012013-01-11 06:30:44 +00004 */
5
6#include <linux/module.h>
7#include <linux/skbuff.h>
8#include <net/netfilter/nf_conntrack.h>
Florian Westphal857ed312016-07-21 12:51:17 +02009#include <net/netfilter/nf_conntrack_ecache.h>
Florian Westphalc539f012013-01-11 06:30:44 +000010#include <net/netfilter/nf_conntrack_labels.h>
11#include <linux/netfilter/x_tables.h>
12
13MODULE_LICENSE("GPL");
14MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
Colin Ian King1de6f332019-04-18 18:00:56 +010015MODULE_DESCRIPTION("Xtables: add/match connection tracking labels");
Florian Westphalc539f012013-01-11 06:30:44 +000016MODULE_ALIAS("ipt_connlabel");
17MODULE_ALIAS("ip6t_connlabel");
18
19static bool
20connlabel_mt(const struct sk_buff *skb, struct xt_action_param *par)
21{
22 const struct xt_connlabel_mtinfo *info = par->matchinfo;
23 enum ip_conntrack_info ctinfo;
Florian Westphal857ed312016-07-21 12:51:17 +020024 struct nf_conn_labels *labels;
Florian Westphalc539f012013-01-11 06:30:44 +000025 struct nf_conn *ct;
26 bool invert = info->options & XT_CONNLABEL_OP_INVERT;
27
28 ct = nf_ct_get(skb, &ctinfo);
Florian Westphalab8bc7e2017-04-14 20:31:09 +020029 if (ct == NULL)
Florian Westphalc539f012013-01-11 06:30:44 +000030 return invert;
31
Florian Westphal857ed312016-07-21 12:51:17 +020032 labels = nf_ct_labels_find(ct);
33 if (!labels)
34 return invert;
Florian Westphalc539f012013-01-11 06:30:44 +000035
Florian Westphal857ed312016-07-21 12:51:17 +020036 if (test_bit(info->bit, labels->bits))
37 return !invert;
38
39 if (info->options & XT_CONNLABEL_OP_SET) {
40 if (!test_and_set_bit(info->bit, labels->bits))
41 nf_conntrack_event_cache(IPCT_LABEL, ct);
42
43 return !invert;
44 }
45
46 return invert;
Florian Westphalc539f012013-01-11 06:30:44 +000047}
48
49static int connlabel_mt_check(const struct xt_mtchk_param *par)
50{
51 const int options = XT_CONNLABEL_OP_INVERT |
52 XT_CONNLABEL_OP_SET;
53 struct xt_connlabel_mtinfo *info = par->matchinfo;
54 int ret;
Florian Westphalc539f012013-01-11 06:30:44 +000055
56 if (info->options & ~options) {
Florian Westphalb2606642018-02-09 15:52:07 +010057 pr_info_ratelimited("Unknown options in mask %x\n",
58 info->options);
Florian Westphalc539f012013-01-11 06:30:44 +000059 return -EINVAL;
60 }
61
Florian Westphalecb24212016-11-15 21:36:40 +010062 ret = nf_ct_netns_get(par->net, par->family);
Florian Westphalc539f012013-01-11 06:30:44 +000063 if (ret < 0) {
Florian Westphalb2606642018-02-09 15:52:07 +010064 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
65 par->family);
Florian Westphalc539f012013-01-11 06:30:44 +000066 return ret;
67 }
68
Florian Westphaladff6c62016-04-12 18:14:25 +020069 ret = nf_connlabels_get(par->net, info->bit);
Joe Stringer86ca02e2015-08-26 11:31:51 -070070 if (ret < 0)
Florian Westphalecb24212016-11-15 21:36:40 +010071 nf_ct_netns_put(par->net, par->family);
Florian Westphalc539f012013-01-11 06:30:44 +000072 return ret;
73}
74
75static void connlabel_mt_destroy(const struct xt_mtdtor_param *par)
76{
Joe Stringer86ca02e2015-08-26 11:31:51 -070077 nf_connlabels_put(par->net);
Florian Westphalecb24212016-11-15 21:36:40 +010078 nf_ct_netns_put(par->net, par->family);
Florian Westphalc539f012013-01-11 06:30:44 +000079}
80
81static struct xt_match connlabels_mt_reg __read_mostly = {
82 .name = "connlabel",
83 .family = NFPROTO_UNSPEC,
84 .checkentry = connlabel_mt_check,
85 .match = connlabel_mt,
86 .matchsize = sizeof(struct xt_connlabel_mtinfo),
87 .destroy = connlabel_mt_destroy,
88 .me = THIS_MODULE,
89};
90
91static int __init connlabel_mt_init(void)
92{
93 return xt_register_match(&connlabels_mt_reg);
94}
95
96static void __exit connlabel_mt_exit(void)
97{
98 xt_unregister_match(&connlabels_mt_reg);
99}
100
101module_init(connlabel_mt_init);
102module_exit(connlabel_mt_exit);