]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/netfilter/xt_connmark.c
netfilter: nf_tables: use WARN_ON_ONCE instead of BUG_ON in nft_do_chain()
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / xt_connmark.c
CommitLineData
96e32272 1/*
b8f00ba2 2 * xt_connmark - Netfilter module to operate on connection marks
1da177e4 3 *
96e32272
JE
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
408ffaa4 7 * Jan Engelhardt <jengelh@medozas.de>
1da177e4
LT
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
e664eabd 20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
1da177e4
LT
21 */
22
23#include <linux/module.h>
24#include <linux/skbuff.h>
587aa641 25#include <net/netfilter/nf_conntrack.h>
b8f00ba2 26#include <net/netfilter/nf_conntrack_ecache.h>
587aa641
PM
27#include <linux/netfilter/x_tables.h>
28#include <linux/netfilter/xt_connmark.h>
1da177e4 29
3a4fa0a2 30MODULE_AUTHOR("Henrik Nordstrom <hno@marasystems.com>");
b8f00ba2 31MODULE_DESCRIPTION("Xtables: connection mark operations");
1da177e4 32MODULE_LICENSE("GPL");
b8f00ba2
JE
33MODULE_ALIAS("ipt_CONNMARK");
34MODULE_ALIAS("ip6t_CONNMARK");
2e4e6a17 35MODULE_ALIAS("ipt_connmark");
73aaf935 36MODULE_ALIAS("ip6t_connmark");
1da177e4 37
b8f00ba2 38static unsigned int
4b560b44 39connmark_tg(struct sk_buff *skb, const struct xt_action_param *par)
b8f00ba2
JE
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);
ab8bc7ed 47 if (ct == NULL)
b8f00ba2
JE
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
135367b8 76static int connmark_tg_check(const struct xt_tgchk_param *par)
b8f00ba2 77{
4a5a5c73
JE
78 int ret;
79
ecb2421b 80 ret = nf_ct_netns_get(par->net, par->family);
f95c74e3 81 if (ret < 0)
8bee4bad
JE
82 pr_info("cannot load conntrack support for proto=%u\n",
83 par->family);
f95c74e3 84 return ret;
b8f00ba2
JE
85}
86
87static void connmark_tg_destroy(const struct xt_tgdtor_param *par)
88{
ecb2421b 89 nf_ct_netns_put(par->net, par->family);
b8f00ba2
JE
90}
91
1d93a9cb 92static bool
62fc8051 93connmark_mt(const struct sk_buff *skb, struct xt_action_param *par)
96e32272 94{
f7108a20 95 const struct xt_connmark_mtinfo1 *info = par->matchinfo;
96e32272
JE
96 enum ip_conntrack_info ctinfo;
97 const struct nf_conn *ct;
98
99 ct = nf_ct_get(skb, &ctinfo);
ab8bc7ed 100 if (ct == NULL)
96e32272
JE
101 return false;
102
103 return ((ct->mark & info->mask) == info->mark) ^ info->invert;
104}
105
b0f38452 106static int connmark_mt_check(const struct xt_mtchk_param *par)
96e32272 107{
4a5a5c73
JE
108 int ret;
109
ecb2421b 110 ret = nf_ct_netns_get(par->net, par->family);
f95c74e3 111 if (ret < 0)
8bee4bad
JE
112 pr_info("cannot load conntrack support for proto=%u\n",
113 par->family);
f95c74e3 114 return ret;
96e32272
JE
115}
116
6be3d859 117static void connmark_mt_destroy(const struct xt_mtdtor_param *par)
b9f78f9f 118{
ecb2421b 119 nf_ct_netns_put(par->net, par->family);
b9f78f9f
PNA
120}
121
b8f00ba2
JE
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
84899a2b
JE
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,
1da177e4
LT
142};
143
d3c5ee6d 144static int __init connmark_mt_init(void)
1da177e4 145{
b8f00ba2
JE
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;
1da177e4
LT
157}
158
d3c5ee6d 159static void __exit connmark_mt_exit(void)
1da177e4 160{
84899a2b 161 xt_unregister_match(&connmark_mt_reg);
b8f00ba2 162 xt_unregister_target(&connmark_tg_reg);
1da177e4
LT
163}
164
d3c5ee6d
JE
165module_init(connmark_mt_init);
166module_exit(connmark_mt_exit);