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