1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * xt_connmark - Netfilter module to operate on connection marks
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>
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>
18 MODULE_AUTHOR("Henrik Nordstrom <hno@marasystems.com>");
19 MODULE_DESCRIPTION("Xtables: connection mark operations");
20 MODULE_LICENSE("GPL");
21 MODULE_ALIAS("ipt_CONNMARK");
22 MODULE_ALIAS("ip6t_CONNMARK");
23 MODULE_ALIAS("ipt_connmark");
24 MODULE_ALIAS("ip6t_connmark");
27 connmark_tg_shift(struct sk_buff
*skb
, const struct xt_connmark_tginfo2
*info
)
29 enum ip_conntrack_info ctinfo
;
30 u_int32_t new_targetmark
;
35 ct
= nf_ct_get(skb
, &ctinfo
);
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
;
46 newmark
<<= info
->shift_bits
;
48 if (READ_ONCE(ct
->mark
) != newmark
) {
49 WRITE_ONCE(ct
->mark
, newmark
);
50 nf_conntrack_event_cache(IPCT_MARK
, ct
);
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
;
58 new_targetmark
<<= info
->shift_bits
;
60 newmark
= (READ_ONCE(ct
->mark
) & ~info
->ctmask
) ^
62 if (READ_ONCE(ct
->mark
) != newmark
) {
63 WRITE_ONCE(ct
->mark
, newmark
);
64 nf_conntrack_event_cache(IPCT_MARK
, ct
);
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
;
72 new_targetmark
<<= info
->shift_bits
;
74 newmark
= (skb
->mark
& ~info
->nfmask
) ^
83 connmark_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
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
,
93 return connmark_tg_shift(skb
, &info2
);
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
, info
);
104 static int connmark_tg_check(const struct xt_tgchk_param
*par
)
108 ret
= nf_ct_netns_get(par
->net
, par
->family
);
110 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
115 static void connmark_tg_destroy(const struct xt_tgdtor_param
*par
)
117 nf_ct_netns_put(par
->net
, par
->family
);
121 connmark_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
123 const struct xt_connmark_mtinfo1
*info
= par
->matchinfo
;
124 enum ip_conntrack_info ctinfo
;
125 const struct nf_conn
*ct
;
127 ct
= nf_ct_get(skb
, &ctinfo
);
131 return ((READ_ONCE(ct
->mark
) & info
->mask
) == info
->mark
) ^ info
->invert
;
134 static int connmark_mt_check(const struct xt_mtchk_param
*par
)
138 ret
= nf_ct_netns_get(par
->net
, par
->family
);
140 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
145 static void connmark_mt_destroy(const struct xt_mtdtor_param
*par
)
147 nf_ct_netns_put(par
->net
, par
->family
);
150 static struct xt_target connmark_tg_reg
[] __read_mostly
= {
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
,
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
,
171 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
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
,
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
,
195 static struct xt_match connmark_mt_reg __read_mostly
= {
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
,
206 static int __init
connmark_mt_init(void)
210 ret
= xt_register_targets(connmark_tg_reg
,
211 ARRAY_SIZE(connmark_tg_reg
));
214 ret
= xt_register_match(&connmark_mt_reg
);
216 xt_unregister_targets(connmark_tg_reg
,
217 ARRAY_SIZE(connmark_tg_reg
));
223 static void __exit
connmark_mt_exit(void)
225 xt_unregister_match(&connmark_mt_reg
);
226 xt_unregister_targets(connmark_tg_reg
, ARRAY_SIZE(connmark_tg_reg
));
229 module_init(connmark_mt_init
);
230 module_exit(connmark_mt_exit
);