Loading...
1/*
2 * xt_connmark - Netfilter module to operate on connection marks
3 *
4 * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
5 * by Henrik Nordstrom <hno@marasystems.com>
6 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
7 * Jan Engelhardt <jengelh@medozas.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <linux/module.h>
24#include <linux/skbuff.h>
25#include <net/netfilter/nf_conntrack.h>
26#include <net/netfilter/nf_conntrack_ecache.h>
27#include <linux/netfilter/x_tables.h>
28#include <linux/netfilter/xt_connmark.h>
29
30MODULE_AUTHOR("Henrik Nordstrom <hno@marasystems.com>");
31MODULE_DESCRIPTION("Xtables: connection mark operations");
32MODULE_LICENSE("GPL");
33MODULE_ALIAS("ipt_CONNMARK");
34MODULE_ALIAS("ip6t_CONNMARK");
35MODULE_ALIAS("ipt_connmark");
36MODULE_ALIAS("ip6t_connmark");
37
38static unsigned int
39connmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
40{
41 const struct xt_connmark_tginfo1 *info = par->targinfo;
42 enum ip_conntrack_info ctinfo;
43 struct nf_conn *ct;
44 u_int32_t newmark;
45
46 ct = nf_ct_get(skb, &ctinfo);
47 if (ct == NULL || nf_ct_is_untracked(ct))
48 return XT_CONTINUE;
49
50 switch (info->mode) {
51 case XT_CONNMARK_SET:
52 newmark = (ct->mark & ~info->ctmask) ^ info->ctmark;
53 if (ct->mark != newmark) {
54 ct->mark = newmark;
55 nf_conntrack_event_cache(IPCT_MARK, ct);
56 }
57 break;
58 case XT_CONNMARK_SAVE:
59 newmark = (ct->mark & ~info->ctmask) ^
60 (skb->mark & info->nfmask);
61 if (ct->mark != newmark) {
62 ct->mark = newmark;
63 nf_conntrack_event_cache(IPCT_MARK, ct);
64 }
65 break;
66 case XT_CONNMARK_RESTORE:
67 newmark = (skb->mark & ~info->nfmask) ^
68 (ct->mark & info->ctmask);
69 skb->mark = newmark;
70 break;
71 }
72
73 return XT_CONTINUE;
74}
75
76static int connmark_tg_check(const struct xt_tgchk_param *par)
77{
78 int ret;
79
80 ret = nf_ct_netns_get(par->net, par->family);
81 if (ret < 0)
82 pr_info("cannot load conntrack support for proto=%u\n",
83 par->family);
84 return ret;
85}
86
87static void connmark_tg_destroy(const struct xt_tgdtor_param *par)
88{
89 nf_ct_netns_put(par->net, par->family);
90}
91
92static bool
93connmark_mt(const struct sk_buff *skb, struct xt_action_param *par)
94{
95 const struct xt_connmark_mtinfo1 *info = par->matchinfo;
96 enum ip_conntrack_info ctinfo;
97 const struct nf_conn *ct;
98
99 ct = nf_ct_get(skb, &ctinfo);
100 if (ct == NULL || nf_ct_is_untracked(ct))
101 return false;
102
103 return ((ct->mark & info->mask) == info->mark) ^ info->invert;
104}
105
106static int connmark_mt_check(const struct xt_mtchk_param *par)
107{
108 int ret;
109
110 ret = nf_ct_netns_get(par->net, par->family);
111 if (ret < 0)
112 pr_info("cannot load conntrack support for proto=%u\n",
113 par->family);
114 return ret;
115}
116
117static void connmark_mt_destroy(const struct xt_mtdtor_param *par)
118{
119 nf_ct_netns_put(par->net, par->family);
120}
121
122static struct xt_target connmark_tg_reg __read_mostly = {
123 .name = "CONNMARK",
124 .revision = 1,
125 .family = NFPROTO_UNSPEC,
126 .checkentry = connmark_tg_check,
127 .target = connmark_tg,
128 .targetsize = sizeof(struct xt_connmark_tginfo1),
129 .destroy = connmark_tg_destroy,
130 .me = THIS_MODULE,
131};
132
133static struct xt_match connmark_mt_reg __read_mostly = {
134 .name = "connmark",
135 .revision = 1,
136 .family = NFPROTO_UNSPEC,
137 .checkentry = connmark_mt_check,
138 .match = connmark_mt,
139 .matchsize = sizeof(struct xt_connmark_mtinfo1),
140 .destroy = connmark_mt_destroy,
141 .me = THIS_MODULE,
142};
143
144static int __init connmark_mt_init(void)
145{
146 int ret;
147
148 ret = xt_register_target(&connmark_tg_reg);
149 if (ret < 0)
150 return ret;
151 ret = xt_register_match(&connmark_mt_reg);
152 if (ret < 0) {
153 xt_unregister_target(&connmark_tg_reg);
154 return ret;
155 }
156 return 0;
157}
158
159static void __exit connmark_mt_exit(void)
160{
161 xt_unregister_match(&connmark_mt_reg);
162 xt_unregister_target(&connmark_tg_reg);
163}
164
165module_init(connmark_mt_init);
166module_exit(connmark_mt_exit);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * xt_connmark - Netfilter module to operate on connection marks
4 *
5 * Copyright (C) 2002,2004 MARA Systems AB <https://www.marasystems.com>
6 * by Henrik Nordstrom <hno@marasystems.com>
7 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
8 * Jan Engelhardt <jengelh@medozas.de>
9 */
10
11#include <linux/module.h>
12#include <linux/skbuff.h>
13#include <net/netfilter/nf_conntrack.h>
14#include <net/netfilter/nf_conntrack_ecache.h>
15#include <linux/netfilter/x_tables.h>
16#include <linux/netfilter/xt_connmark.h>
17
18MODULE_AUTHOR("Henrik Nordstrom <hno@marasystems.com>");
19MODULE_DESCRIPTION("Xtables: connection mark operations");
20MODULE_LICENSE("GPL");
21MODULE_ALIAS("ipt_CONNMARK");
22MODULE_ALIAS("ip6t_CONNMARK");
23MODULE_ALIAS("ipt_connmark");
24MODULE_ALIAS("ip6t_connmark");
25
26static unsigned int
27connmark_tg_shift(struct sk_buff *skb, const struct xt_connmark_tginfo2 *info)
28{
29 enum ip_conntrack_info ctinfo;
30 u_int32_t new_targetmark;
31 struct nf_conn *ct;
32 u_int32_t newmark;
33 u_int32_t oldmark;
34
35 ct = nf_ct_get(skb, &ctinfo);
36 if (ct == NULL)
37 return XT_CONTINUE;
38
39 switch (info->mode) {
40 case XT_CONNMARK_SET:
41 oldmark = READ_ONCE(ct->mark);
42 newmark = (oldmark & ~info->ctmask) ^ info->ctmark;
43 if (info->shift_dir == D_SHIFT_RIGHT)
44 newmark >>= info->shift_bits;
45 else
46 newmark <<= info->shift_bits;
47
48 if (READ_ONCE(ct->mark) != newmark) {
49 WRITE_ONCE(ct->mark, newmark);
50 nf_conntrack_event_cache(IPCT_MARK, ct);
51 }
52 break;
53 case XT_CONNMARK_SAVE:
54 new_targetmark = (skb->mark & info->nfmask);
55 if (info->shift_dir == D_SHIFT_RIGHT)
56 new_targetmark >>= info->shift_bits;
57 else
58 new_targetmark <<= info->shift_bits;
59
60 newmark = (READ_ONCE(ct->mark) & ~info->ctmask) ^
61 new_targetmark;
62 if (READ_ONCE(ct->mark) != newmark) {
63 WRITE_ONCE(ct->mark, newmark);
64 nf_conntrack_event_cache(IPCT_MARK, ct);
65 }
66 break;
67 case XT_CONNMARK_RESTORE:
68 new_targetmark = (READ_ONCE(ct->mark) & info->ctmask);
69 if (info->shift_dir == D_SHIFT_RIGHT)
70 new_targetmark >>= info->shift_bits;
71 else
72 new_targetmark <<= info->shift_bits;
73
74 newmark = (skb->mark & ~info->nfmask) ^
75 new_targetmark;
76 skb->mark = newmark;
77 break;
78 }
79 return XT_CONTINUE;
80}
81
82static unsigned int
83connmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
84{
85 const struct xt_connmark_tginfo1 *info = par->targinfo;
86 const struct xt_connmark_tginfo2 info2 = {
87 .ctmark = info->ctmark,
88 .ctmask = info->ctmask,
89 .nfmask = info->nfmask,
90 .mode = info->mode,
91 };
92
93 return connmark_tg_shift(skb, &info2);
94}
95
96static unsigned int
97connmark_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
98{
99 const struct xt_connmark_tginfo2 *info = par->targinfo;
100
101 return connmark_tg_shift(skb, info);
102}
103
104static int connmark_tg_check(const struct xt_tgchk_param *par)
105{
106 int ret;
107
108 ret = nf_ct_netns_get(par->net, par->family);
109 if (ret < 0)
110 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
111 par->family);
112 return ret;
113}
114
115static void connmark_tg_destroy(const struct xt_tgdtor_param *par)
116{
117 nf_ct_netns_put(par->net, par->family);
118}
119
120static bool
121connmark_mt(const struct sk_buff *skb, struct xt_action_param *par)
122{
123 const struct xt_connmark_mtinfo1 *info = par->matchinfo;
124 enum ip_conntrack_info ctinfo;
125 const struct nf_conn *ct;
126
127 ct = nf_ct_get(skb, &ctinfo);
128 if (ct == NULL)
129 return false;
130
131 return ((READ_ONCE(ct->mark) & info->mask) == info->mark) ^ info->invert;
132}
133
134static int connmark_mt_check(const struct xt_mtchk_param *par)
135{
136 int ret;
137
138 ret = nf_ct_netns_get(par->net, par->family);
139 if (ret < 0)
140 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
141 par->family);
142 return ret;
143}
144
145static void connmark_mt_destroy(const struct xt_mtdtor_param *par)
146{
147 nf_ct_netns_put(par->net, par->family);
148}
149
150static struct xt_target connmark_tg_reg[] __read_mostly = {
151 {
152 .name = "CONNMARK",
153 .revision = 1,
154 .family = NFPROTO_IPV4,
155 .checkentry = connmark_tg_check,
156 .target = connmark_tg,
157 .targetsize = sizeof(struct xt_connmark_tginfo1),
158 .destroy = connmark_tg_destroy,
159 .me = THIS_MODULE,
160 },
161 {
162 .name = "CONNMARK",
163 .revision = 2,
164 .family = NFPROTO_IPV4,
165 .checkentry = connmark_tg_check,
166 .target = connmark_tg_v2,
167 .targetsize = sizeof(struct xt_connmark_tginfo2),
168 .destroy = connmark_tg_destroy,
169 .me = THIS_MODULE,
170 },
171#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
172 {
173 .name = "CONNMARK",
174 .revision = 1,
175 .family = NFPROTO_IPV6,
176 .checkentry = connmark_tg_check,
177 .target = connmark_tg,
178 .targetsize = sizeof(struct xt_connmark_tginfo1),
179 .destroy = connmark_tg_destroy,
180 .me = THIS_MODULE,
181 },
182 {
183 .name = "CONNMARK",
184 .revision = 2,
185 .family = NFPROTO_IPV6,
186 .checkentry = connmark_tg_check,
187 .target = connmark_tg_v2,
188 .targetsize = sizeof(struct xt_connmark_tginfo2),
189 .destroy = connmark_tg_destroy,
190 .me = THIS_MODULE,
191 },
192#endif
193};
194
195static struct xt_match connmark_mt_reg __read_mostly = {
196 .name = "connmark",
197 .revision = 1,
198 .family = NFPROTO_UNSPEC,
199 .checkentry = connmark_mt_check,
200 .match = connmark_mt,
201 .matchsize = sizeof(struct xt_connmark_mtinfo1),
202 .destroy = connmark_mt_destroy,
203 .me = THIS_MODULE,
204};
205
206static int __init connmark_mt_init(void)
207{
208 int ret;
209
210 ret = xt_register_targets(connmark_tg_reg,
211 ARRAY_SIZE(connmark_tg_reg));
212 if (ret < 0)
213 return ret;
214 ret = xt_register_match(&connmark_mt_reg);
215 if (ret < 0) {
216 xt_unregister_targets(connmark_tg_reg,
217 ARRAY_SIZE(connmark_tg_reg));
218 return ret;
219 }
220 return 0;
221}
222
223static void __exit connmark_mt_exit(void)
224{
225 xt_unregister_match(&connmark_mt_reg);
226 xt_unregister_targets(connmark_tg_reg, ARRAY_SIZE(connmark_tg_reg));
227}
228
229module_init(connmark_mt_init);
230module_exit(connmark_mt_exit);