Merge tag 'for-linus-2023100502' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / net / sched / act_ipt.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
0c6965dd 3 * net/sched/act_ipt.c iptables target interface
1da177e4
LT
4 *
5 *TODO: Add other tables. For now we only support the ipv4 table targets
6 *
0dcffd09 7 * Copyright: Jamal Hadi Salim (2002-13)
1da177e4
LT
8 */
9
1da177e4
LT
10#include <linux/types.h>
11#include <linux/kernel.h>
1da177e4 12#include <linux/string.h>
1da177e4 13#include <linux/errno.h>
1da177e4
LT
14#include <linux/skbuff.h>
15#include <linux/rtnetlink.h>
16#include <linux/module.h>
17#include <linux/init.h>
5a0e3ad6 18#include <linux/slab.h>
dc5fc579 19#include <net/netlink.h>
1da177e4
LT
20#include <net/pkt_sched.h>
21#include <linux/tc_act/tc_ipt.h>
22#include <net/tc_act/tc_ipt.h>
871cf386 23#include <net/tc_wrapper.h>
93d75d47 24#include <net/ip.h>
1da177e4
LT
25
26#include <linux/netfilter_ipv4/ip_tables.h>
27
1da177e4 28
a85a970a 29static struct tc_action_ops act_ipt_ops;
a85a970a 30static struct tc_action_ops act_xt_ops;
ddf97ccd 31
ec0acb09
XL
32static int ipt_init_target(struct net *net, struct xt_entry_target *t,
33 char *table, unsigned int hook)
1da177e4 34{
af5d6dc2 35 struct xt_tgchk_param par;
e60a13e0 36 struct xt_target *target;
4f8a881a 37 struct ipt_entry e = {};
1da177e4
LT
38 int ret = 0;
39
239a87c8
PM
40 target = xt_request_find_target(AF_INET, t->u.user.name,
41 t->u.user.revision);
d2a7b6ba
JE
42 if (IS_ERR(target))
43 return PTR_ERR(target);
1da177e4 44
1da177e4 45 t->u.kernel.target = target;
96d97030 46 memset(&par, 0, sizeof(par));
ec0acb09 47 par.net = net;
af5d6dc2 48 par.table = table;
4f8a881a 49 par.entryinfo = &e;
af5d6dc2
JE
50 par.target = target;
51 par.targinfo = t->data;
b4ee9338 52 par.hook_mask = 1 << hook;
916a917d 53 par.family = NFPROTO_IPV4;
1da177e4 54
916a917d 55 ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
367c6790 56 if (ret < 0) {
239a87c8 57 module_put(t->u.kernel.target->me);
18118cdb 58 return ret;
239a87c8 59 }
367c6790 60 return 0;
1da177e4
LT
61}
62
981471bd 63static void ipt_destroy_target(struct xt_entry_target *t, struct net *net)
1da177e4 64{
a2df1648
JE
65 struct xt_tgdtor_param par = {
66 .target = t->u.kernel.target,
67 .targinfo = t->data,
44ef548f 68 .family = NFPROTO_IPV4,
981471bd 69 .net = net,
a2df1648
JE
70 };
71 if (par.target->destroy != NULL)
72 par.target->destroy(&par);
73 module_put(par.target->me);
1da177e4
LT
74}
75
9a63b255 76static void tcf_ipt_release(struct tc_action *a)
1da177e4 77{
86062033 78 struct tcf_ipt *ipt = to_ipt(a);
1e46ef17
DC
79
80 if (ipt->tcfi_t) {
981471bd 81 ipt_destroy_target(ipt->tcfi_t, a->idrinfo->net);
1e46ef17
DC
82 kfree(ipt->tcfi_t);
83 }
a5b5c958 84 kfree(ipt->tcfi_tname);
1da177e4
LT
85}
86
53b2bf3f
PM
87static const struct nla_policy ipt_policy[TCA_IPT_MAX + 1] = {
88 [TCA_IPT_TABLE] = { .type = NLA_STRING, .len = IFNAMSIZ },
b4ee9338
FW
89 [TCA_IPT_HOOK] = NLA_POLICY_RANGE(NLA_U32, NF_INET_PRE_ROUTING,
90 NF_INET_NUMHOOKS),
53b2bf3f 91 [TCA_IPT_INDEX] = { .type = NLA_U32 },
87a2e70d 92 [TCA_IPT_TARG] = { .len = sizeof(struct xt_entry_target) },
53b2bf3f
PM
93};
94
ec0acb09 95static int __tcf_ipt_init(struct net *net, unsigned int id, struct nlattr *nla,
a85a970a 96 struct nlattr *est, struct tc_action **a,
695176bf 97 const struct tc_action_ops *ops,
abbb0d33 98 struct tcf_proto *tp, u32 flags)
1da177e4 99{
ec0acb09 100 struct tc_action_net *tn = net_generic(net, id);
695176bf 101 bool bind = flags & TCA_ACT_FLAGS_BIND;
7ba699c6 102 struct nlattr *tb[TCA_IPT_MAX + 1];
e9ce1cd3 103 struct tcf_ipt *ipt;
87a2e70d 104 struct xt_entry_target *td, *t;
1da177e4 105 char *tname;
b2313077
WC
106 bool exists = false;
107 int ret = 0, err;
1da177e4
LT
108 u32 hook = 0;
109 u32 index = 0;
110
cee63723 111 if (nla == NULL)
1da177e4
LT
112 return -EINVAL;
113
8cb08174
JB
114 err = nla_parse_nested_deprecated(tb, TCA_IPT_MAX, nla, ipt_policy,
115 NULL);
cee63723
PM
116 if (err < 0)
117 return err;
118
a57f19d3
JHS
119 if (tb[TCA_IPT_INDEX] != NULL)
120 index = nla_get_u32(tb[TCA_IPT_INDEX]);
121
0190c1d4
VB
122 err = tcf_idr_check_alloc(tn, &index, a, bind);
123 if (err < 0)
124 return err;
125 exists = err;
a57f19d3
JHS
126 if (exists && bind)
127 return 0;
128
129 if (tb[TCA_IPT_HOOK] == NULL || tb[TCA_IPT_TARG] == NULL) {
130 if (exists)
65a206c0 131 tcf_idr_release(*a, bind);
0190c1d4
VB
132 else
133 tcf_idr_cleanup(tn, index);
1da177e4 134 return -EINVAL;
a57f19d3 135 }
53b2bf3f 136
87a2e70d 137 td = (struct xt_entry_target *)nla_data(tb[TCA_IPT_TARG]);
aeadd93f 138 if (nla_len(tb[TCA_IPT_TARG]) != td->u.target_size) {
d15eccea 139 if (exists)
65a206c0 140 tcf_idr_release(*a, bind);
0190c1d4
VB
141 else
142 tcf_idr_cleanup(tn, index);
1da177e4 143 return -EINVAL;
d15eccea 144 }
1da177e4 145
d15eccea 146 if (!exists) {
65a206c0 147 ret = tcf_idr_create(tn, index, est, a, ops, bind,
40bd094d 148 false, flags);
0190c1d4
VB
149 if (ret) {
150 tcf_idr_cleanup(tn, index);
86062033 151 return ret;
0190c1d4 152 }
1da177e4
LT
153 ret = ACT_P_CREATED;
154 } else {
1a29321e
JHS
155 if (bind)/* dont override defaults */
156 return 0;
1a29321e 157
695176bf 158 if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
4e8ddd7f 159 tcf_idr_release(*a, bind);
1da177e4 160 return -EEXIST;
4e8ddd7f 161 }
1da177e4 162 }
b4ee9338
FW
163
164 err = -EINVAL;
1587bac4 165 hook = nla_get_u32(tb[TCA_IPT_HOOK]);
b4ee9338
FW
166 switch (hook) {
167 case NF_INET_PRE_ROUTING:
168 break;
169 case NF_INET_POST_ROUTING:
170 break;
171 default:
172 goto err1;
173 }
174
175 if (tb[TCA_IPT_TABLE]) {
176 /* mangle only for now */
177 if (nla_strcmp(tb[TCA_IPT_TABLE], "mangle"))
178 goto err1;
179 }
1da177e4 180
b4ee9338 181 tname = kstrdup("mangle", GFP_KERNEL);
e9ce1cd3 182 if (unlikely(!tname))
1da177e4 183 goto err1;
1da177e4 184
c7b1b249 185 t = kmemdup(td, td->u.target_size, GFP_KERNEL);
e9ce1cd3 186 if (unlikely(!t))
1da177e4 187 goto err2;
1da177e4 188
ec0acb09 189 err = ipt_init_target(net, t, tname, hook);
cc7ec456 190 if (err < 0)
1da177e4
LT
191 goto err3;
192
a85a970a
WC
193 ipt = to_ipt(*a);
194
e9ce1cd3 195 spin_lock_bh(&ipt->tcf_lock);
1da177e4 196 if (ret != ACT_P_CREATED) {
981471bd 197 ipt_destroy_target(ipt->tcfi_t, net);
e9ce1cd3
DM
198 kfree(ipt->tcfi_tname);
199 kfree(ipt->tcfi_t);
1da177e4 200 }
e9ce1cd3
DM
201 ipt->tcfi_tname = tname;
202 ipt->tcfi_t = t;
203 ipt->tcfi_hook = hook;
204 spin_unlock_bh(&ipt->tcf_lock);
1da177e4
LT
205 return ret;
206
207err3:
208 kfree(t);
209err2:
210 kfree(tname);
211err1:
8f67c90e 212 tcf_idr_release(*a, bind);
1da177e4
LT
213 return err;
214}
215
ddf97ccd 216static int tcf_ipt_init(struct net *net, struct nlattr *nla,
695176bf
CW
217 struct nlattr *est, struct tc_action **a,
218 struct tcf_proto *tp,
abbb0d33 219 u32 flags, struct netlink_ext_ack *extack)
ddf97ccd 220{
acd0a7ab
ZS
221 return __tcf_ipt_init(net, act_ipt_ops.net_id, nla, est,
222 a, &act_ipt_ops, tp, flags);
ddf97ccd
WC
223}
224
225static int tcf_xt_init(struct net *net, struct nlattr *nla,
695176bf
CW
226 struct nlattr *est, struct tc_action **a,
227 struct tcf_proto *tp,
abbb0d33 228 u32 flags, struct netlink_ext_ack *extack)
ddf97ccd 229{
acd0a7ab
ZS
230 return __tcf_ipt_init(net, act_xt_ops.net_id, nla, est,
231 a, &act_xt_ops, tp, flags);
ddf97ccd
WC
232}
233
b2dc32dc
FW
234static bool tcf_ipt_act_check(struct sk_buff *skb)
235{
236 const struct iphdr *iph;
237 unsigned int nhoff, len;
238
239 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
240 return false;
241
242 nhoff = skb_network_offset(skb);
243 iph = ip_hdr(skb);
244 if (iph->ihl < 5 || iph->version != 4)
245 return false;
246
247 len = skb_ip_totlen(skb);
248 if (skb->len < nhoff + len || len < (iph->ihl * 4u))
249 return false;
250
251 return pskb_may_pull(skb, iph->ihl * 4u);
252}
253
871cf386
PT
254TC_INDIRECT_SCOPE int tcf_ipt_act(struct sk_buff *skb,
255 const struct tc_action *a,
256 struct tcf_result *res)
1da177e4 257{
93d75d47 258 char saved_cb[sizeof_field(struct sk_buff, cb)];
1da177e4 259 int ret = 0, result = 0;
a85a970a 260 struct tcf_ipt *ipt = to_ipt(a);
4b560b44 261 struct xt_action_param par;
613dbd95
PNA
262 struct nf_hook_state state = {
263 .net = dev_net(skb->dev),
264 .in = skb->dev,
265 .hook = ipt->tcfi_hook,
266 .pf = NFPROTO_IPV4,
267 };
1da177e4 268
b2dc32dc
FW
269 if (skb_protocol(skb, false) != htons(ETH_P_IP))
270 return TC_ACT_UNSPEC;
271
14bbd6a5
PS
272 if (skb_unclone(skb, GFP_ATOMIC))
273 return TC_ACT_UNSPEC;
1da177e4 274
b2dc32dc
FW
275 if (!tcf_ipt_act_check(skb))
276 return TC_ACT_UNSPEC;
277
278 if (state.hook == NF_INET_POST_ROUTING) {
279 if (!skb_dst(skb))
280 return TC_ACT_UNSPEC;
281
282 state.out = skb->dev;
283 }
284
93d75d47
FW
285 memcpy(saved_cb, skb->cb, sizeof(saved_cb));
286
e9ce1cd3 287 spin_lock(&ipt->tcf_lock);
1da177e4 288
9c4a4e48 289 tcf_lastuse_update(&ipt->tcf_tm);
bfe0d029 290 bstats_update(&ipt->tcf_bstats, skb);
1da177e4
LT
291
292 /* yes, we have to worry about both in and out dev
cc7ec456
ED
293 * worry later - danger - this API seems to have changed
294 * from earlier kernels
295 */
613dbd95 296 par.state = &state;
7eb35586
JE
297 par.target = ipt->tcfi_t->u.kernel.target;
298 par.targinfo = ipt->tcfi_t->data;
93d75d47
FW
299
300 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
301
7eb35586
JE
302 ret = par.target->target(skb, &par);
303
1da177e4
LT
304 switch (ret) {
305 case NF_ACCEPT:
306 result = TC_ACT_OK;
307 break;
308 case NF_DROP:
309 result = TC_ACT_SHOT;
e9ce1cd3 310 ipt->tcf_qstats.drops++;
1da177e4 311 break;
243bf6e2 312 case XT_CONTINUE:
1da177e4
LT
313 result = TC_ACT_PIPE;
314 break;
315 default:
e87cc472
JP
316 net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT\n",
317 ret);
95df1b16 318 result = TC_ACT_OK;
1da177e4
LT
319 break;
320 }
e9ce1cd3 321 spin_unlock(&ipt->tcf_lock);
93d75d47
FW
322
323 memcpy(skb->cb, saved_cb, sizeof(skb->cb));
324
1da177e4
LT
325 return result;
326
327}
328
0b0f43fe
JHS
329static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind,
330 int ref)
1da177e4 331{
27a884dc 332 unsigned char *b = skb_tail_pointer(skb);
a85a970a 333 struct tcf_ipt *ipt = to_ipt(a);
87a2e70d 334 struct xt_entry_target *t;
1da177e4
LT
335 struct tcf_t tm;
336 struct tc_cnt c;
1da177e4
LT
337
338 /* for simple targets kernel size == user size
cc7ec456
ED
339 * user name = target name
340 * for foolproof you need to not assume this
341 */
1da177e4 342
ff25276d 343 spin_lock_bh(&ipt->tcf_lock);
c7b1b249 344 t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC);
e9ce1cd3 345 if (unlikely(!t))
7ba699c6 346 goto nla_put_failure;
1da177e4 347
036bb443
VB
348 c.bindcnt = atomic_read(&ipt->tcf_bindcnt) - bind;
349 c.refcnt = refcount_read(&ipt->tcf_refcnt) - ref;
e9ce1cd3
DM
350 strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name);
351
1b34ec43
DM
352 if (nla_put(skb, TCA_IPT_TARG, ipt->tcfi_t->u.user.target_size, t) ||
353 nla_put_u32(skb, TCA_IPT_INDEX, ipt->tcf_index) ||
354 nla_put_u32(skb, TCA_IPT_HOOK, ipt->tcfi_hook) ||
355 nla_put(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c) ||
356 nla_put_string(skb, TCA_IPT_TABLE, ipt->tcfi_tname))
357 goto nla_put_failure;
48d8ee16
JHS
358
359 tcf_tm_dump(&tm, &ipt->tcf_tm);
9854518e 360 if (nla_put_64bit(skb, TCA_IPT_TM, sizeof(tm), &tm, TCA_IPT_PAD))
1b34ec43 361 goto nla_put_failure;
48d8ee16 362
ff25276d 363 spin_unlock_bh(&ipt->tcf_lock);
1da177e4
LT
364 kfree(t);
365 return skb->len;
366
7ba699c6 367nla_put_failure:
ff25276d 368 spin_unlock_bh(&ipt->tcf_lock);
dc5fc579 369 nlmsg_trim(skb, b);
1da177e4
LT
370 kfree(t);
371 return -1;
372}
373
374static struct tc_action_ops act_ipt_ops = {
375 .kind = "ipt",
eddd2cf1 376 .id = TCA_ID_IPT,
1da177e4 377 .owner = THIS_MODULE,
11b9695b 378 .act = tcf_ipt_act,
1da177e4 379 .dump = tcf_ipt_dump,
86062033 380 .cleanup = tcf_ipt_release,
1da177e4 381 .init = tcf_ipt_init,
a85a970a 382 .size = sizeof(struct tcf_ipt),
ddf97ccd
WC
383};
384
385static __net_init int ipt_init_net(struct net *net)
386{
acd0a7ab 387 struct tc_action_net *tn = net_generic(net, act_ipt_ops.net_id);
ddf97ccd 388
981471bd 389 return tc_action_net_init(net, tn, &act_ipt_ops);
ddf97ccd
WC
390}
391
039af9c6 392static void __net_exit ipt_exit_net(struct list_head *net_list)
ddf97ccd 393{
acd0a7ab 394 tc_action_net_exit(net_list, act_ipt_ops.net_id);
ddf97ccd
WC
395}
396
397static struct pernet_operations ipt_net_ops = {
398 .init = ipt_init_net,
039af9c6 399 .exit_batch = ipt_exit_net,
acd0a7ab 400 .id = &act_ipt_ops.net_id,
ddf97ccd 401 .size = sizeof(struct tc_action_net),
1da177e4
LT
402};
403
0dcffd09
JHS
404static struct tc_action_ops act_xt_ops = {
405 .kind = "xt",
eddd2cf1 406 .id = TCA_ID_XT,
0dcffd09 407 .owner = THIS_MODULE,
11b9695b 408 .act = tcf_ipt_act,
0dcffd09 409 .dump = tcf_ipt_dump,
86062033 410 .cleanup = tcf_ipt_release,
ddf97ccd 411 .init = tcf_xt_init,
a85a970a 412 .size = sizeof(struct tcf_ipt),
ddf97ccd
WC
413};
414
415static __net_init int xt_init_net(struct net *net)
416{
acd0a7ab 417 struct tc_action_net *tn = net_generic(net, act_xt_ops.net_id);
ddf97ccd 418
981471bd 419 return tc_action_net_init(net, tn, &act_xt_ops);
ddf97ccd
WC
420}
421
039af9c6 422static void __net_exit xt_exit_net(struct list_head *net_list)
ddf97ccd 423{
acd0a7ab 424 tc_action_net_exit(net_list, act_xt_ops.net_id);
ddf97ccd
WC
425}
426
427static struct pernet_operations xt_net_ops = {
428 .init = xt_init_net,
039af9c6 429 .exit_batch = xt_exit_net,
acd0a7ab 430 .id = &act_xt_ops.net_id,
ddf97ccd 431 .size = sizeof(struct tc_action_net),
0dcffd09
JHS
432};
433
434MODULE_AUTHOR("Jamal Hadi Salim(2002-13)");
1da177e4
LT
435MODULE_DESCRIPTION("Iptables target actions");
436MODULE_LICENSE("GPL");
0dcffd09 437MODULE_ALIAS("act_xt");
1da177e4 438
e9ce1cd3 439static int __init ipt_init_module(void)
1da177e4 440{
4f1e9d89 441 int ret1, ret2;
369ba567 442
ddf97ccd 443 ret1 = tcf_register_action(&act_xt_ops, &xt_net_ops);
0dcffd09 444 if (ret1 < 0)
ddf97ccd
WC
445 pr_err("Failed to load xt action\n");
446
447 ret2 = tcf_register_action(&act_ipt_ops, &ipt_net_ops);
0dcffd09 448 if (ret2 < 0)
ddf97ccd 449 pr_err("Failed to load ipt action\n");
0dcffd09 450
369ba567 451 if (ret1 < 0 && ret2 < 0) {
0dcffd09 452 return ret1;
369ba567 453 } else
0dcffd09 454 return 0;
1da177e4
LT
455}
456
e9ce1cd3 457static void __exit ipt_cleanup_module(void)
1da177e4 458{
ddf97ccd
WC
459 tcf_unregister_action(&act_ipt_ops, &ipt_net_ops);
460 tcf_unregister_action(&act_xt_ops, &xt_net_ops);
1da177e4
LT
461}
462
463module_init(ipt_init_module);
464module_exit(ipt_cleanup_module);