Blame view
net/sched/act_ipt.c
10.6 KB
2874c5fd2
|
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4c
|
2 |
/* |
0c6965dd3
|
3 |
* net/sched/act_ipt.c iptables target interface |
1da177e4c
|
4 5 6 |
* *TODO: Add other tables. For now we only support the ipv4 table targets * |
0dcffd096
|
7 |
* Copyright: Jamal Hadi Salim (2002-13) |
1da177e4c
|
8 |
*/ |
1da177e4c
|
9 10 |
#include <linux/types.h> #include <linux/kernel.h> |
1da177e4c
|
11 |
#include <linux/string.h> |
1da177e4c
|
12 |
#include <linux/errno.h> |
1da177e4c
|
13 14 15 16 |
#include <linux/skbuff.h> #include <linux/rtnetlink.h> #include <linux/module.h> #include <linux/init.h> |
5a0e3ad6a
|
17 |
#include <linux/slab.h> |
dc5fc579b
|
18 |
#include <net/netlink.h> |
1da177e4c
|
19 20 21 22 23 |
#include <net/pkt_sched.h> #include <linux/tc_act/tc_ipt.h> #include <net/tc_act/tc_ipt.h> #include <linux/netfilter_ipv4/ip_tables.h> |
1da177e4c
|
24 |
|
c7d03a00b
|
25 |
static unsigned int ipt_net_id; |
a85a970af
|
26 |
static struct tc_action_ops act_ipt_ops; |
ddf97ccdd
|
27 |
|
c7d03a00b
|
28 |
static unsigned int xt_net_id; |
a85a970af
|
29 |
static struct tc_action_ops act_xt_ops; |
ddf97ccdd
|
30 |
|
ec0acb093
|
31 32 |
static int ipt_init_target(struct net *net, struct xt_entry_target *t, char *table, unsigned int hook) |
1da177e4c
|
33 |
{ |
af5d6dc20
|
34 |
struct xt_tgchk_param par; |
e60a13e03
|
35 |
struct xt_target *target; |
4f8a881ac
|
36 |
struct ipt_entry e = {}; |
1da177e4c
|
37 |
int ret = 0; |
239a87c87
|
38 39 |
target = xt_request_find_target(AF_INET, t->u.user.name, t->u.user.revision); |
d2a7b6bad
|
40 41 |
if (IS_ERR(target)) return PTR_ERR(target); |
1da177e4c
|
42 |
|
1da177e4c
|
43 |
t->u.kernel.target = target; |
96d970305
|
44 |
memset(&par, 0, sizeof(par)); |
ec0acb093
|
45 |
par.net = net; |
af5d6dc20
|
46 |
par.table = table; |
4f8a881ac
|
47 |
par.entryinfo = &e; |
af5d6dc20
|
48 49 50 |
par.target = target; par.targinfo = t->data; par.hook_mask = hook; |
916a917df
|
51 |
par.family = NFPROTO_IPV4; |
1da177e4c
|
52 |
|
916a917df
|
53 |
ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false); |
367c67900
|
54 |
if (ret < 0) { |
239a87c87
|
55 |
module_put(t->u.kernel.target->me); |
18118cdbf
|
56 |
return ret; |
239a87c87
|
57 |
} |
367c67900
|
58 |
return 0; |
1da177e4c
|
59 |
} |
981471bd3
|
60 |
static void ipt_destroy_target(struct xt_entry_target *t, struct net *net) |
1da177e4c
|
61 |
{ |
a2df1648b
|
62 63 64 |
struct xt_tgdtor_param par = { .target = t->u.kernel.target, .targinfo = t->data, |
44ef548f4
|
65 |
.family = NFPROTO_IPV4, |
981471bd3
|
66 |
.net = net, |
a2df1648b
|
67 68 69 70 |
}; if (par.target->destroy != NULL) par.target->destroy(&par); module_put(par.target->me); |
1da177e4c
|
71 |
} |
9a63b255d
|
72 |
static void tcf_ipt_release(struct tc_action *a) |
1da177e4c
|
73 |
{ |
86062033f
|
74 |
struct tcf_ipt *ipt = to_ipt(a); |
1e46ef176
|
75 76 |
if (ipt->tcfi_t) { |
981471bd3
|
77 |
ipt_destroy_target(ipt->tcfi_t, a->idrinfo->net); |
1e46ef176
|
78 79 |
kfree(ipt->tcfi_t); } |
a5b5c958f
|
80 |
kfree(ipt->tcfi_tname); |
1da177e4c
|
81 |
} |
53b2bf3f8
|
82 83 84 85 |
static const struct nla_policy ipt_policy[TCA_IPT_MAX + 1] = { [TCA_IPT_TABLE] = { .type = NLA_STRING, .len = IFNAMSIZ }, [TCA_IPT_HOOK] = { .type = NLA_U32 }, [TCA_IPT_INDEX] = { .type = NLA_U32 }, |
87a2e70db
|
86 |
[TCA_IPT_TARG] = { .len = sizeof(struct xt_entry_target) }, |
53b2bf3f8
|
87 |
}; |
ec0acb093
|
88 |
static int __tcf_ipt_init(struct net *net, unsigned int id, struct nlattr *nla, |
a85a970af
|
89 |
struct nlattr *est, struct tc_action **a, |
85d0966fa
|
90 |
const struct tc_action_ops *ops, int ovr, int bind, |
abbb0d336
|
91 |
struct tcf_proto *tp, u32 flags) |
1da177e4c
|
92 |
{ |
ec0acb093
|
93 |
struct tc_action_net *tn = net_generic(net, id); |
7ba699c60
|
94 |
struct nlattr *tb[TCA_IPT_MAX + 1]; |
e9ce1cd3c
|
95 |
struct tcf_ipt *ipt; |
87a2e70db
|
96 |
struct xt_entry_target *td, *t; |
1da177e4c
|
97 |
char *tname; |
b2313077e
|
98 99 |
bool exists = false; int ret = 0, err; |
1da177e4c
|
100 101 |
u32 hook = 0; u32 index = 0; |
cee63723b
|
102 |
if (nla == NULL) |
1da177e4c
|
103 |
return -EINVAL; |
8cb081746
|
104 105 |
err = nla_parse_nested_deprecated(tb, TCA_IPT_MAX, nla, ipt_policy, NULL); |
cee63723b
|
106 107 |
if (err < 0) return err; |
a57f19d30
|
108 109 |
if (tb[TCA_IPT_INDEX] != NULL) index = nla_get_u32(tb[TCA_IPT_INDEX]); |
0190c1d45
|
110 111 112 113 |
err = tcf_idr_check_alloc(tn, &index, a, bind); if (err < 0) return err; exists = err; |
a57f19d30
|
114 115 116 117 118 |
if (exists && bind) return 0; if (tb[TCA_IPT_HOOK] == NULL || tb[TCA_IPT_TARG] == NULL) { if (exists) |
65a206c01
|
119 |
tcf_idr_release(*a, bind); |
0190c1d45
|
120 121 |
else tcf_idr_cleanup(tn, index); |
1da177e4c
|
122 |
return -EINVAL; |
a57f19d30
|
123 |
} |
53b2bf3f8
|
124 |
|
87a2e70db
|
125 |
td = (struct xt_entry_target *)nla_data(tb[TCA_IPT_TARG]); |
aeadd93f2
|
126 |
if (nla_len(tb[TCA_IPT_TARG]) != td->u.target_size) { |
d15eccea6
|
127 |
if (exists) |
65a206c01
|
128 |
tcf_idr_release(*a, bind); |
0190c1d45
|
129 130 |
else tcf_idr_cleanup(tn, index); |
1da177e4c
|
131 |
return -EINVAL; |
d15eccea6
|
132 |
} |
1da177e4c
|
133 |
|
d15eccea6
|
134 |
if (!exists) { |
65a206c01
|
135 |
ret = tcf_idr_create(tn, index, est, a, ops, bind, |
e38226786
|
136 |
false, 0); |
0190c1d45
|
137 138 |
if (ret) { tcf_idr_cleanup(tn, index); |
86062033f
|
139 |
return ret; |
0190c1d45
|
140 |
} |
1da177e4c
|
141 142 |
ret = ACT_P_CREATED; } else { |
1a29321ed
|
143 144 |
if (bind)/* dont override defaults */ return 0; |
1a29321ed
|
145 |
|
4e8ddd7f1
|
146 147 |
if (!ovr) { tcf_idr_release(*a, bind); |
1da177e4c
|
148 |
return -EEXIST; |
4e8ddd7f1
|
149 |
} |
1da177e4c
|
150 |
} |
1587bac49
|
151 |
hook = nla_get_u32(tb[TCA_IPT_HOOK]); |
1da177e4c
|
152 153 154 |
err = -ENOMEM; tname = kmalloc(IFNAMSIZ, GFP_KERNEL); |
e9ce1cd3c
|
155 |
if (unlikely(!tname)) |
1da177e4c
|
156 |
goto err1; |
7ba699c60
|
157 158 |
if (tb[TCA_IPT_TABLE] == NULL || nla_strlcpy(tname, tb[TCA_IPT_TABLE], IFNAMSIZ) >= IFNAMSIZ) |
1da177e4c
|
159 |
strcpy(tname, "mangle"); |
c7b1b2497
|
160 |
t = kmemdup(td, td->u.target_size, GFP_KERNEL); |
e9ce1cd3c
|
161 |
if (unlikely(!t)) |
1da177e4c
|
162 |
goto err2; |
1da177e4c
|
163 |
|
ec0acb093
|
164 |
err = ipt_init_target(net, t, tname, hook); |
cc7ec456f
|
165 |
if (err < 0) |
1da177e4c
|
166 |
goto err3; |
a85a970af
|
167 |
ipt = to_ipt(*a); |
e9ce1cd3c
|
168 |
spin_lock_bh(&ipt->tcf_lock); |
1da177e4c
|
169 |
if (ret != ACT_P_CREATED) { |
981471bd3
|
170 |
ipt_destroy_target(ipt->tcfi_t, net); |
e9ce1cd3c
|
171 172 |
kfree(ipt->tcfi_tname); kfree(ipt->tcfi_t); |
1da177e4c
|
173 |
} |
e9ce1cd3c
|
174 175 176 177 |
ipt->tcfi_tname = tname; ipt->tcfi_t = t; ipt->tcfi_hook = hook; spin_unlock_bh(&ipt->tcf_lock); |
1da177e4c
|
178 |
if (ret == ACT_P_CREATED) |
65a206c01
|
179 |
tcf_idr_insert(tn, *a); |
1da177e4c
|
180 181 182 183 184 185 186 |
return ret; err3: kfree(t); err2: kfree(tname); err1: |
8f67c90ee
|
187 |
tcf_idr_release(*a, bind); |
1da177e4c
|
188 189 |
return err; } |
ddf97ccdd
|
190 |
static int tcf_ipt_init(struct net *net, struct nlattr *nla, |
a85a970af
|
191 |
struct nlattr *est, struct tc_action **a, int ovr, |
85d0966fa
|
192 |
int bind, bool rtnl_held, struct tcf_proto *tp, |
abbb0d336
|
193 |
u32 flags, struct netlink_ext_ack *extack) |
ddf97ccdd
|
194 |
{ |
ec0acb093
|
195 |
return __tcf_ipt_init(net, ipt_net_id, nla, est, a, &act_ipt_ops, ovr, |
abbb0d336
|
196 |
bind, tp, flags); |
ddf97ccdd
|
197 198 199 |
} static int tcf_xt_init(struct net *net, struct nlattr *nla, |
a85a970af
|
200 |
struct nlattr *est, struct tc_action **a, int ovr, |
85d0966fa
|
201 |
int bind, bool unlocked, struct tcf_proto *tp, |
abbb0d336
|
202 |
u32 flags, struct netlink_ext_ack *extack) |
ddf97ccdd
|
203 |
{ |
ec0acb093
|
204 |
return __tcf_ipt_init(net, xt_net_id, nla, est, a, &act_xt_ops, ovr, |
abbb0d336
|
205 |
bind, tp, flags); |
ddf97ccdd
|
206 |
} |
11b9695b3
|
207 208 |
static int tcf_ipt_act(struct sk_buff *skb, const struct tc_action *a, struct tcf_result *res) |
1da177e4c
|
209 210 |
{ int ret = 0, result = 0; |
a85a970af
|
211 |
struct tcf_ipt *ipt = to_ipt(a); |
4b560b447
|
212 |
struct xt_action_param par; |
613dbd957
|
213 214 215 216 217 218 |
struct nf_hook_state state = { .net = dev_net(skb->dev), .in = skb->dev, .hook = ipt->tcfi_hook, .pf = NFPROTO_IPV4, }; |
1da177e4c
|
219 |
|
14bbd6a56
|
220 221 |
if (skb_unclone(skb, GFP_ATOMIC)) return TC_ACT_UNSPEC; |
1da177e4c
|
222 |
|
e9ce1cd3c
|
223 |
spin_lock(&ipt->tcf_lock); |
1da177e4c
|
224 |
|
9c4a4e488
|
225 |
tcf_lastuse_update(&ipt->tcf_tm); |
bfe0d0298
|
226 |
bstats_update(&ipt->tcf_bstats, skb); |
1da177e4c
|
227 228 |
/* yes, we have to worry about both in and out dev |
cc7ec456f
|
229 230 231 |
* worry later - danger - this API seems to have changed * from earlier kernels */ |
613dbd957
|
232 |
par.state = &state; |
7eb355865
|
233 234 235 |
par.target = ipt->tcfi_t->u.kernel.target; par.targinfo = ipt->tcfi_t->data; ret = par.target->target(skb, &par); |
1da177e4c
|
236 237 238 239 240 241 |
switch (ret) { case NF_ACCEPT: result = TC_ACT_OK; break; case NF_DROP: result = TC_ACT_SHOT; |
e9ce1cd3c
|
242 |
ipt->tcf_qstats.drops++; |
1da177e4c
|
243 |
break; |
243bf6e29
|
244 |
case XT_CONTINUE: |
1da177e4c
|
245 246 247 |
result = TC_ACT_PIPE; break; default: |
e87cc4728
|
248 249 250 |
net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT ", ret); |
95df1b160
|
251 |
result = TC_ACT_OK; |
1da177e4c
|
252 253 |
break; } |
e9ce1cd3c
|
254 |
spin_unlock(&ipt->tcf_lock); |
1da177e4c
|
255 256 257 |
return result; } |
0b0f43fe2
|
258 259 |
static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
1da177e4c
|
260 |
{ |
27a884dc3
|
261 |
unsigned char *b = skb_tail_pointer(skb); |
a85a970af
|
262 |
struct tcf_ipt *ipt = to_ipt(a); |
87a2e70db
|
263 |
struct xt_entry_target *t; |
1da177e4c
|
264 265 |
struct tcf_t tm; struct tc_cnt c; |
1da177e4c
|
266 267 |
/* for simple targets kernel size == user size |
cc7ec456f
|
268 269 270 |
* user name = target name * for foolproof you need to not assume this */ |
1da177e4c
|
271 |
|
ff25276de
|
272 |
spin_lock_bh(&ipt->tcf_lock); |
c7b1b2497
|
273 |
t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC); |
e9ce1cd3c
|
274 |
if (unlikely(!t)) |
7ba699c60
|
275 |
goto nla_put_failure; |
1da177e4c
|
276 |
|
036bb4432
|
277 278 |
c.bindcnt = atomic_read(&ipt->tcf_bindcnt) - bind; c.refcnt = refcount_read(&ipt->tcf_refcnt) - ref; |
e9ce1cd3c
|
279 |
strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name); |
1b34ec43c
|
280 281 282 283 284 285 |
if (nla_put(skb, TCA_IPT_TARG, ipt->tcfi_t->u.user.target_size, t) || nla_put_u32(skb, TCA_IPT_INDEX, ipt->tcf_index) || nla_put_u32(skb, TCA_IPT_HOOK, ipt->tcfi_hook) || nla_put(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c) || nla_put_string(skb, TCA_IPT_TABLE, ipt->tcfi_tname)) goto nla_put_failure; |
48d8ee169
|
286 287 |
tcf_tm_dump(&tm, &ipt->tcf_tm); |
9854518ea
|
288 |
if (nla_put_64bit(skb, TCA_IPT_TM, sizeof(tm), &tm, TCA_IPT_PAD)) |
1b34ec43c
|
289 |
goto nla_put_failure; |
48d8ee169
|
290 |
|
ff25276de
|
291 |
spin_unlock_bh(&ipt->tcf_lock); |
1da177e4c
|
292 293 |
kfree(t); return skb->len; |
7ba699c60
|
294 |
nla_put_failure: |
ff25276de
|
295 |
spin_unlock_bh(&ipt->tcf_lock); |
dc5fc579b
|
296 |
nlmsg_trim(skb, b); |
1da177e4c
|
297 298 299 |
kfree(t); return -1; } |
ddf97ccdd
|
300 301 |
static int tcf_ipt_walker(struct net *net, struct sk_buff *skb, struct netlink_callback *cb, int type, |
417801055
|
302 303 |
const struct tc_action_ops *ops, struct netlink_ext_ack *extack) |
ddf97ccdd
|
304 305 |
{ struct tc_action_net *tn = net_generic(net, ipt_net_id); |
b36201455
|
306 |
return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
ddf97ccdd
|
307 |
} |
f061b48c1
|
308 |
static int tcf_ipt_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccdd
|
309 310 |
{ struct tc_action_net *tn = net_generic(net, ipt_net_id); |
65a206c01
|
311 |
return tcf_idr_search(tn, a, index); |
ddf97ccdd
|
312 |
} |
1da177e4c
|
313 314 |
static struct tc_action_ops act_ipt_ops = { .kind = "ipt", |
eddd2cf19
|
315 |
.id = TCA_ID_IPT, |
1da177e4c
|
316 |
.owner = THIS_MODULE, |
11b9695b3
|
317 |
.act = tcf_ipt_act, |
1da177e4c
|
318 |
.dump = tcf_ipt_dump, |
86062033f
|
319 |
.cleanup = tcf_ipt_release, |
1da177e4c
|
320 |
.init = tcf_ipt_init, |
ddf97ccdd
|
321 322 |
.walk = tcf_ipt_walker, .lookup = tcf_ipt_search, |
a85a970af
|
323 |
.size = sizeof(struct tcf_ipt), |
ddf97ccdd
|
324 325 326 327 328 |
}; static __net_init int ipt_init_net(struct net *net) { struct tc_action_net *tn = net_generic(net, ipt_net_id); |
981471bd3
|
329 |
return tc_action_net_init(net, tn, &act_ipt_ops); |
ddf97ccdd
|
330 |
} |
039af9c66
|
331 |
static void __net_exit ipt_exit_net(struct list_head *net_list) |
ddf97ccdd
|
332 |
{ |
039af9c66
|
333 |
tc_action_net_exit(net_list, ipt_net_id); |
ddf97ccdd
|
334 335 336 337 |
} static struct pernet_operations ipt_net_ops = { .init = ipt_init_net, |
039af9c66
|
338 |
.exit_batch = ipt_exit_net, |
ddf97ccdd
|
339 340 |
.id = &ipt_net_id, .size = sizeof(struct tc_action_net), |
1da177e4c
|
341 |
}; |
ddf97ccdd
|
342 343 |
static int tcf_xt_walker(struct net *net, struct sk_buff *skb, struct netlink_callback *cb, int type, |
417801055
|
344 345 |
const struct tc_action_ops *ops, struct netlink_ext_ack *extack) |
ddf97ccdd
|
346 347 |
{ struct tc_action_net *tn = net_generic(net, xt_net_id); |
b36201455
|
348 |
return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
ddf97ccdd
|
349 |
} |
f061b48c1
|
350 |
static int tcf_xt_search(struct net *net, struct tc_action **a, u32 index) |
ddf97ccdd
|
351 352 |
{ struct tc_action_net *tn = net_generic(net, xt_net_id); |
65a206c01
|
353 |
return tcf_idr_search(tn, a, index); |
ddf97ccdd
|
354 |
} |
0dcffd096
|
355 356 |
static struct tc_action_ops act_xt_ops = { .kind = "xt", |
eddd2cf19
|
357 |
.id = TCA_ID_XT, |
0dcffd096
|
358 |
.owner = THIS_MODULE, |
11b9695b3
|
359 |
.act = tcf_ipt_act, |
0dcffd096
|
360 |
.dump = tcf_ipt_dump, |
86062033f
|
361 |
.cleanup = tcf_ipt_release, |
ddf97ccdd
|
362 363 364 |
.init = tcf_xt_init, .walk = tcf_xt_walker, .lookup = tcf_xt_search, |
a85a970af
|
365 |
.size = sizeof(struct tcf_ipt), |
ddf97ccdd
|
366 367 368 369 370 |
}; static __net_init int xt_init_net(struct net *net) { struct tc_action_net *tn = net_generic(net, xt_net_id); |
981471bd3
|
371 |
return tc_action_net_init(net, tn, &act_xt_ops); |
ddf97ccdd
|
372 |
} |
039af9c66
|
373 |
static void __net_exit xt_exit_net(struct list_head *net_list) |
ddf97ccdd
|
374 |
{ |
039af9c66
|
375 |
tc_action_net_exit(net_list, xt_net_id); |
ddf97ccdd
|
376 377 378 379 |
} static struct pernet_operations xt_net_ops = { .init = xt_init_net, |
039af9c66
|
380 |
.exit_batch = xt_exit_net, |
ddf97ccdd
|
381 382 |
.id = &xt_net_id, .size = sizeof(struct tc_action_net), |
0dcffd096
|
383 384 385 |
}; MODULE_AUTHOR("Jamal Hadi Salim(2002-13)"); |
1da177e4c
|
386 387 |
MODULE_DESCRIPTION("Iptables target actions"); MODULE_LICENSE("GPL"); |
0dcffd096
|
388 |
MODULE_ALIAS("act_xt"); |
1da177e4c
|
389 |
|
e9ce1cd3c
|
390 |
static int __init ipt_init_module(void) |
1da177e4c
|
391 |
{ |
4f1e9d894
|
392 |
int ret1, ret2; |
369ba5678
|
393 |
|
ddf97ccdd
|
394 |
ret1 = tcf_register_action(&act_xt_ops, &xt_net_ops); |
0dcffd096
|
395 |
if (ret1 < 0) |
ddf97ccdd
|
396 397 398 399 |
pr_err("Failed to load xt action "); ret2 = tcf_register_action(&act_ipt_ops, &ipt_net_ops); |
0dcffd096
|
400 |
if (ret2 < 0) |
ddf97ccdd
|
401 402 |
pr_err("Failed to load ipt action "); |
0dcffd096
|
403 |
|
369ba5678
|
404 |
if (ret1 < 0 && ret2 < 0) { |
0dcffd096
|
405 |
return ret1; |
369ba5678
|
406 |
} else |
0dcffd096
|
407 |
return 0; |
1da177e4c
|
408 |
} |
e9ce1cd3c
|
409 |
static void __exit ipt_cleanup_module(void) |
1da177e4c
|
410 |
{ |
ddf97ccdd
|
411 412 |
tcf_unregister_action(&act_ipt_ops, &ipt_net_ops); tcf_unregister_action(&act_xt_ops, &xt_net_ops); |
1da177e4c
|
413 414 415 416 |
} module_init(ipt_init_module); module_exit(ipt_cleanup_module); |