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
;
34 ct
= nf_ct_get(skb
, &ctinfo
);
40 newmark
= (ct
->mark
& ~info
->ctmask
) ^ info
->ctmark
;
41 if (info
->shift_dir
== D_SHIFT_RIGHT
)
42 newmark
>>= info
->shift_bits
;
44 newmark
<<= info
->shift_bits
;
46 if (ct
->mark
!= newmark
) {
48 nf_conntrack_event_cache(IPCT_MARK
, ct
);
51 case XT_CONNMARK_SAVE
:
52 new_targetmark
= (skb
->mark
& info
->nfmask
);
53 if (info
->shift_dir
== D_SHIFT_RIGHT
)
54 new_targetmark
>>= info
->shift_bits
;
56 new_targetmark
<<= info
->shift_bits
;
58 newmark
= (ct
->mark
& ~info
->ctmask
) ^
60 if (ct
->mark
!= newmark
) {
62 nf_conntrack_event_cache(IPCT_MARK
, ct
);
65 case XT_CONNMARK_RESTORE
:
66 new_targetmark
= (ct
->mark
& info
->ctmask
);
67 if (info
->shift_dir
== D_SHIFT_RIGHT
)
68 new_targetmark
>>= info
->shift_bits
;
70 new_targetmark
<<= info
->shift_bits
;
72 newmark
= (skb
->mark
& ~info
->nfmask
) ^
81 connmark_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
83 const struct xt_connmark_tginfo1
*info
= par
->targinfo
;
84 const struct xt_connmark_tginfo2 info2
= {
85 .ctmark
= info
->ctmark
,
86 .ctmask
= info
->ctmask
,
87 .nfmask
= info
->nfmask
,
91 return connmark_tg_shift(skb
, &info2
);
95 connmark_tg_v2(struct sk_buff
*skb
, const struct xt_action_param
*par
)
97 const struct xt_connmark_tginfo2
*info
= par
->targinfo
;
99 return connmark_tg_shift(skb
, info
);
102 static int connmark_tg_check(const struct xt_tgchk_param
*par
)
106 ret
= nf_ct_netns_get(par
->net
, par
->family
);
108 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
113 static void connmark_tg_destroy(const struct xt_tgdtor_param
*par
)
115 nf_ct_netns_put(par
->net
, par
->family
);
119 connmark_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
121 const struct xt_connmark_mtinfo1
*info
= par
->matchinfo
;
122 enum ip_conntrack_info ctinfo
;
123 const struct nf_conn
*ct
;
125 ct
= nf_ct_get(skb
, &ctinfo
);
129 return ((ct
->mark
& info
->mask
) == info
->mark
) ^ info
->invert
;
132 static int connmark_mt_check(const struct xt_mtchk_param
*par
)
136 ret
= nf_ct_netns_get(par
->net
, par
->family
);
138 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
143 static void connmark_mt_destroy(const struct xt_mtdtor_param
*par
)
145 nf_ct_netns_put(par
->net
, par
->family
);
148 static struct xt_target connmark_tg_reg
[] __read_mostly
= {
152 .family
= NFPROTO_UNSPEC
,
153 .checkentry
= connmark_tg_check
,
154 .target
= connmark_tg
,
155 .targetsize
= sizeof(struct xt_connmark_tginfo1
),
156 .destroy
= connmark_tg_destroy
,
162 .family
= NFPROTO_UNSPEC
,
163 .checkentry
= connmark_tg_check
,
164 .target
= connmark_tg_v2
,
165 .targetsize
= sizeof(struct xt_connmark_tginfo2
),
166 .destroy
= connmark_tg_destroy
,
171 static struct xt_match connmark_mt_reg __read_mostly
= {
174 .family
= NFPROTO_UNSPEC
,
175 .checkentry
= connmark_mt_check
,
176 .match
= connmark_mt
,
177 .matchsize
= sizeof(struct xt_connmark_mtinfo1
),
178 .destroy
= connmark_mt_destroy
,
182 static int __init
connmark_mt_init(void)
186 ret
= xt_register_targets(connmark_tg_reg
,
187 ARRAY_SIZE(connmark_tg_reg
));
190 ret
= xt_register_match(&connmark_mt_reg
);
192 xt_unregister_targets(connmark_tg_reg
,
193 ARRAY_SIZE(connmark_tg_reg
));
199 static void __exit
connmark_mt_exit(void)
201 xt_unregister_match(&connmark_mt_reg
);
202 xt_unregister_targets(connmark_tg_reg
, ARRAY_SIZE(connmark_tg_reg
));
205 module_init(connmark_mt_init
);
206 module_exit(connmark_mt_exit
);