Blame view
net/netfilter/xt_connmark.c
5.13 KB
1ccea77e2 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
96e322726 [NETFILTER]: xt_c... |
2 |
/* |
b8f00ba27 netfilter: xtable... |
3 |
* xt_connmark - Netfilter module to operate on connection marks |
1da177e4c Linux-2.6.12-rc2 |
4 |
* |
50935339c netfilter: Replac... |
5 |
* Copyright (C) 2002,2004 MARA Systems AB <https://www.marasystems.com> |
96e322726 [NETFILTER]: xt_c... |
6 7 |
* by Henrik Nordstrom <hno@marasystems.com> * Copyright © CC Computer Consultants GmbH, 2007 - 2008 |
408ffaa4a netfilter: update... |
8 |
* Jan Engelhardt <jengelh@medozas.de> |
1da177e4c Linux-2.6.12-rc2 |
9 10 11 12 |
*/ #include <linux/module.h> #include <linux/skbuff.h> |
587aa6416 [NETFILTER]: Remo... |
13 |
#include <net/netfilter/nf_conntrack.h> |
b8f00ba27 netfilter: xtable... |
14 |
#include <net/netfilter/nf_conntrack_ecache.h> |
587aa6416 [NETFILTER]: Remo... |
15 16 |
#include <linux/netfilter/x_tables.h> #include <linux/netfilter/xt_connmark.h> |
1da177e4c Linux-2.6.12-rc2 |
17 |
|
3a4fa0a25 Fix misspellings ... |
18 |
MODULE_AUTHOR("Henrik Nordstrom <hno@marasystems.com>"); |
b8f00ba27 netfilter: xtable... |
19 |
MODULE_DESCRIPTION("Xtables: connection mark operations"); |
1da177e4c Linux-2.6.12-rc2 |
20 |
MODULE_LICENSE("GPL"); |
b8f00ba27 netfilter: xtable... |
21 22 |
MODULE_ALIAS("ipt_CONNMARK"); MODULE_ALIAS("ip6t_CONNMARK"); |
2e4e6a17a [NETFILTER] x_tab... |
23 |
MODULE_ALIAS("ipt_connmark"); |
73aaf9355 [NETFILTER]: x_ta... |
24 |
MODULE_ALIAS("ip6t_connmark"); |
1da177e4c Linux-2.6.12-rc2 |
25 |
|
b8f00ba27 netfilter: xtable... |
26 |
static unsigned int |
5a786232e netfilter: xt_con... |
27 |
connmark_tg_shift(struct sk_buff *skb, const struct xt_connmark_tginfo2 *info) |
b8f00ba27 netfilter: xtable... |
28 |
{ |
b8f00ba27 netfilter: xtable... |
29 |
enum ip_conntrack_info ctinfo; |
cf43ae63c netfilter: xt_con... |
30 |
u_int32_t new_targetmark; |
b8f00ba27 netfilter: xtable... |
31 32 33 34 |
struct nf_conn *ct; u_int32_t newmark; ct = nf_ct_get(skb, &ctinfo); |
ab8bc7ed8 netfilter: remove... |
35 |
if (ct == NULL) |
b8f00ba27 netfilter: xtable... |
36 37 38 39 40 |
return XT_CONTINUE; switch (info->mode) { case XT_CONNMARK_SET: newmark = (ct->mark & ~info->ctmask) ^ info->ctmark; |
5a786232e netfilter: xt_con... |
41 42 |
if (info->shift_dir == D_SHIFT_RIGHT) newmark >>= info->shift_bits; |
472a73e00 netfilter: xt_con... |
43 |
else |
5a786232e netfilter: xt_con... |
44 |
newmark <<= info->shift_bits; |
b8f00ba27 netfilter: xtable... |
45 46 47 48 49 50 |
if (ct->mark != newmark) { ct->mark = newmark; nf_conntrack_event_cache(IPCT_MARK, ct); } break; case XT_CONNMARK_SAVE: |
cf43ae63c netfilter: xt_con... |
51 |
new_targetmark = (skb->mark & info->nfmask); |
5a786232e netfilter: xt_con... |
52 53 |
if (info->shift_dir == D_SHIFT_RIGHT) new_targetmark >>= info->shift_bits; |
472a73e00 netfilter: xt_con... |
54 |
else |
5a786232e netfilter: xt_con... |
55 |
new_targetmark <<= info->shift_bits; |
cf43ae63c netfilter: xt_con... |
56 57 |
newmark = (ct->mark & ~info->ctmask) ^ new_targetmark; |
b8f00ba27 netfilter: xtable... |
58 59 60 61 62 63 |
if (ct->mark != newmark) { ct->mark = newmark; nf_conntrack_event_cache(IPCT_MARK, ct); } break; case XT_CONNMARK_RESTORE: |
cf43ae63c netfilter: xt_con... |
64 |
new_targetmark = (ct->mark & info->ctmask); |
5a786232e netfilter: xt_con... |
65 66 |
if (info->shift_dir == D_SHIFT_RIGHT) new_targetmark >>= info->shift_bits; |
472a73e00 netfilter: xt_con... |
67 |
else |
5a786232e netfilter: xt_con... |
68 |
new_targetmark <<= info->shift_bits; |
cf43ae63c netfilter: xt_con... |
69 70 |
newmark = (skb->mark & ~info->nfmask) ^ new_targetmark; |
b8f00ba27 netfilter: xtable... |
71 72 73 |
skb->mark = newmark; break; } |
b8f00ba27 netfilter: xtable... |
74 75 |
return XT_CONTINUE; } |
472a73e00 netfilter: xt_con... |
76 77 78 79 |
static unsigned int connmark_tg(struct sk_buff *skb, const struct xt_action_param *par) { const struct xt_connmark_tginfo1 *info = par->targinfo; |
5a786232e netfilter: xt_con... |
80 81 82 83 84 85 |
const struct xt_connmark_tginfo2 info2 = { .ctmark = info->ctmark, .ctmask = info->ctmask, .nfmask = info->nfmask, .mode = info->mode, }; |
472a73e00 netfilter: xt_con... |
86 |
|
5a786232e netfilter: xt_con... |
87 |
return connmark_tg_shift(skb, &info2); |
472a73e00 netfilter: xt_con... |
88 89 90 91 92 93 |
} static unsigned int connmark_tg_v2(struct sk_buff *skb, const struct xt_action_param *par) { const struct xt_connmark_tginfo2 *info = par->targinfo; |
5a786232e netfilter: xt_con... |
94 |
return connmark_tg_shift(skb, info); |
472a73e00 netfilter: xt_con... |
95 |
} |
135367b8f netfilter: xtable... |
96 |
static int connmark_tg_check(const struct xt_tgchk_param *par) |
b8f00ba27 netfilter: xtable... |
97 |
{ |
4a5a5c73b netfilter: xtable... |
98 |
int ret; |
ecb2421b5 netfilter: add an... |
99 |
ret = nf_ct_netns_get(par->net, par->family); |
f95c74e33 netfilter: xtable... |
100 |
if (ret < 0) |
b26066447 netfilter: x_tabl... |
101 102 103 |
pr_info_ratelimited("cannot load conntrack support for proto=%u ", par->family); |
f95c74e33 netfilter: xtable... |
104 |
return ret; |
b8f00ba27 netfilter: xtable... |
105 106 107 108 |
} static void connmark_tg_destroy(const struct xt_tgdtor_param *par) { |
ecb2421b5 netfilter: add an... |
109 |
nf_ct_netns_put(par->net, par->family); |
b8f00ba27 netfilter: xtable... |
110 |
} |
1d93a9cba [NETFILTER]: x_ta... |
111 |
static bool |
62fc80510 netfilter: xtable... |
112 |
connmark_mt(const struct sk_buff *skb, struct xt_action_param *par) |
1da177e4c Linux-2.6.12-rc2 |
113 |
{ |
f7108a20d netfilter: xtable... |
114 |
const struct xt_connmark_mtinfo1 *info = par->matchinfo; |
96e322726 [NETFILTER]: xt_c... |
115 116 117 118 |
enum ip_conntrack_info ctinfo; const struct nf_conn *ct; ct = nf_ct_get(skb, &ctinfo); |
ab8bc7ed8 netfilter: remove... |
119 |
if (ct == NULL) |
96e322726 [NETFILTER]: xt_c... |
120 121 122 123 |
return false; return ((ct->mark & info->mask) == info->mark) ^ info->invert; } |
b0f38452f netfilter: xtable... |
124 |
static int connmark_mt_check(const struct xt_mtchk_param *par) |
96e322726 [NETFILTER]: xt_c... |
125 |
{ |
4a5a5c73b netfilter: xtable... |
126 |
int ret; |
ecb2421b5 netfilter: add an... |
127 |
ret = nf_ct_netns_get(par->net, par->family); |
f95c74e33 netfilter: xtable... |
128 |
if (ret < 0) |
b26066447 netfilter: x_tabl... |
129 130 131 |
pr_info_ratelimited("cannot load conntrack support for proto=%u ", par->family); |
f95c74e33 netfilter: xtable... |
132 |
return ret; |
96e322726 [NETFILTER]: xt_c... |
133 |
} |
6be3d8598 netfilter: xtable... |
134 |
static void connmark_mt_destroy(const struct xt_mtdtor_param *par) |
b9f78f9fc [NETFILTER]: nf_c... |
135 |
{ |
ecb2421b5 netfilter: add an... |
136 |
nf_ct_netns_put(par->net, par->family); |
b9f78f9fc [NETFILTER]: nf_c... |
137 |
} |
472a73e00 netfilter: xt_con... |
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
static struct xt_target connmark_tg_reg[] __read_mostly = { { .name = "CONNMARK", .revision = 1, .family = NFPROTO_UNSPEC, .checkentry = connmark_tg_check, .target = connmark_tg, .targetsize = sizeof(struct xt_connmark_tginfo1), .destroy = connmark_tg_destroy, .me = THIS_MODULE, }, { .name = "CONNMARK", .revision = 2, .family = NFPROTO_UNSPEC, .checkentry = connmark_tg_check, .target = connmark_tg_v2, .targetsize = sizeof(struct xt_connmark_tginfo2), .destroy = connmark_tg_destroy, .me = THIS_MODULE, } |
b8f00ba27 netfilter: xtable... |
159 |
}; |
84899a2b9 netfilter: xtable... |
160 161 162 163 164 165 166 167 168 |
static struct xt_match connmark_mt_reg __read_mostly = { .name = "connmark", .revision = 1, .family = NFPROTO_UNSPEC, .checkentry = connmark_mt_check, .match = connmark_mt, .matchsize = sizeof(struct xt_connmark_mtinfo1), .destroy = connmark_mt_destroy, .me = THIS_MODULE, |
1da177e4c Linux-2.6.12-rc2 |
169 |
}; |
d3c5ee6d5 [NETFILTER]: x_ta... |
170 |
static int __init connmark_mt_init(void) |
1da177e4c Linux-2.6.12-rc2 |
171 |
{ |
b8f00ba27 netfilter: xtable... |
172 |
int ret; |
472a73e00 netfilter: xt_con... |
173 174 |
ret = xt_register_targets(connmark_tg_reg, ARRAY_SIZE(connmark_tg_reg)); |
b8f00ba27 netfilter: xtable... |
175 176 177 178 |
if (ret < 0) return ret; ret = xt_register_match(&connmark_mt_reg); if (ret < 0) { |
472a73e00 netfilter: xt_con... |
179 180 |
xt_unregister_targets(connmark_tg_reg, ARRAY_SIZE(connmark_tg_reg)); |
b8f00ba27 netfilter: xtable... |
181 182 183 |
return ret; } return 0; |
1da177e4c Linux-2.6.12-rc2 |
184 |
} |
d3c5ee6d5 [NETFILTER]: x_ta... |
185 |
static void __exit connmark_mt_exit(void) |
1da177e4c Linux-2.6.12-rc2 |
186 |
{ |
84899a2b9 netfilter: xtable... |
187 |
xt_unregister_match(&connmark_mt_reg); |
fc6ddbecc netfilter: xt_con... |
188 |
xt_unregister_targets(connmark_tg_reg, ARRAY_SIZE(connmark_tg_reg)); |
1da177e4c Linux-2.6.12-rc2 |
189 |
} |
d3c5ee6d5 [NETFILTER]: x_ta... |
190 191 |
module_init(connmark_mt_init); module_exit(connmark_mt_exit); |