2 * xt_connmark - Netfilter module to operate on connection marks
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>
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.
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.
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/>.
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>
30 MODULE_AUTHOR("Henrik Nordstrom <hno@marasystems.com>");
31 MODULE_DESCRIPTION("Xtables: connection mark operations");
32 MODULE_LICENSE("GPL");
33 MODULE_ALIAS("ipt_CONNMARK");
34 MODULE_ALIAS("ip6t_CONNMARK");
35 MODULE_ALIAS("ipt_connmark");
36 MODULE_ALIAS("ip6t_connmark");
39 connmark_tg_shift(struct sk_buff *skb,
40 const struct xt_connmark_tginfo1 *info,
41 u8 shift_bits, u8 shift_dir)
43 enum ip_conntrack_info ctinfo;
47 ct = nf_ct_get(skb, &ctinfo);
53 newmark = (ct->mark & ~info->ctmask) ^ info->ctmark;
54 if (shift_dir == D_SHIFT_RIGHT)
55 newmark >>= shift_bits;
57 newmark <<= shift_bits;
58 if (ct->mark != newmark) {
60 nf_conntrack_event_cache(IPCT_MARK, ct);
63 case XT_CONNMARK_SAVE:
64 newmark = (ct->mark & ~info->ctmask) ^
65 (skb->mark & info->nfmask);
66 if (shift_dir == D_SHIFT_RIGHT)
67 newmark >>= shift_bits;
69 newmark <<= shift_bits;
70 if (ct->mark != newmark) {
72 nf_conntrack_event_cache(IPCT_MARK, ct);
75 case XT_CONNMARK_RESTORE:
76 newmark = (skb->mark & ~info->nfmask) ^
77 (ct->mark & info->ctmask);
78 if (shift_dir == D_SHIFT_RIGHT)
79 newmark >>= shift_bits;
81 newmark <<= shift_bits;
89 connmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
91 const struct xt_connmark_tginfo1 *info = par->targinfo;
93 return connmark_tg_shift(skb, info, 0, 0);
97 connmark_tg_v2(struct sk_buff *skb, const struct xt_action_param *par)
99 const struct xt_connmark_tginfo2 *info = par->targinfo;
101 return connmark_tg_shift(skb, (const struct xt_connmark_tginfo1 *)info,
102 info->shift_bits, info->shift_dir);
105 static int connmark_tg_check(const struct xt_tgchk_param *par)
109 ret = nf_ct_netns_get(par->net, par->family);
111 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
116 static void connmark_tg_destroy(const struct xt_tgdtor_param *par)
118 nf_ct_netns_put(par->net, par->family);
122 connmark_mt(const struct sk_buff *skb, struct xt_action_param *par)
124 const struct xt_connmark_mtinfo1 *info = par->matchinfo;
125 enum ip_conntrack_info ctinfo;
126 const struct nf_conn *ct;
128 ct = nf_ct_get(skb, &ctinfo);
132 return ((ct->mark & info->mask) == info->mark) ^ info->invert;
135 static int connmark_mt_check(const struct xt_mtchk_param *par)
139 ret = nf_ct_netns_get(par->net, par->family);
141 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
146 static void connmark_mt_destroy(const struct xt_mtdtor_param *par)
148 nf_ct_netns_put(par->net, par->family);
151 static struct xt_target connmark_tg_reg[] __read_mostly = {
155 .family = NFPROTO_UNSPEC,
156 .checkentry = connmark_tg_check,
157 .target = connmark_tg,
158 .targetsize = sizeof(struct xt_connmark_tginfo1),
159 .destroy = connmark_tg_destroy,
165 .family = NFPROTO_UNSPEC,
166 .checkentry = connmark_tg_check,
167 .target = connmark_tg_v2,
168 .targetsize = sizeof(struct xt_connmark_tginfo2),
169 .destroy = connmark_tg_destroy,
174 static struct xt_match connmark_mt_reg __read_mostly = {
177 .family = NFPROTO_UNSPEC,
178 .checkentry = connmark_mt_check,
179 .match = connmark_mt,
180 .matchsize = sizeof(struct xt_connmark_mtinfo1),
181 .destroy = connmark_mt_destroy,
185 static int __init connmark_mt_init(void)
189 ret = xt_register_targets(connmark_tg_reg,
190 ARRAY_SIZE(connmark_tg_reg));
193 ret = xt_register_match(&connmark_mt_reg);
195 xt_unregister_targets(connmark_tg_reg,
196 ARRAY_SIZE(connmark_tg_reg));
202 static void __exit connmark_mt_exit(void)
204 xt_unregister_match(&connmark_mt_reg);
205 xt_unregister_target(connmark_tg_reg);
208 module_init(connmark_mt_init);
209 module_exit(connmark_mt_exit);