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