Merge branch 'x86-x32-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[deliverable/linux.git] / net / sched / act_ipt.c
CommitLineData
1da177e4 1/*
e87cc472 2 * net/sched/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
e9ce1cd3 31#define IPT_TAB_MASK 15
369ba567 32static struct tcf_hashinfo ipt_hash_info;
1da177e4 33
87a2e70d 34static int ipt_init_target(struct xt_entry_target *t, char *table, unsigned int hook)
1da177e4 35{
af5d6dc2 36 struct xt_tgchk_param par;
e60a13e0 37 struct xt_target *target;
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;
af5d6dc2
JE
46 par.table = table;
47 par.entryinfo = NULL;
48 par.target = target;
49 par.targinfo = t->data;
50 par.hook_mask = hook;
916a917d 51 par.family = NFPROTO_IPV4;
1da177e4 52
916a917d 53 ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
367c6790 54 if (ret < 0) {
239a87c8 55 module_put(t->u.kernel.target->me);
18118cdb 56 return ret;
239a87c8 57 }
367c6790 58 return 0;
1da177e4
LT
59}
60
87a2e70d 61static void ipt_destroy_target(struct xt_entry_target *t)
1da177e4 62{
a2df1648
JE
63 struct xt_tgdtor_param par = {
64 .target = t->u.kernel.target,
65 .targinfo = t->data,
66 };
67 if (par.target->destroy != NULL)
68 par.target->destroy(&par);
69 module_put(par.target->me);
1da177e4
LT
70}
71
e9ce1cd3 72static int tcf_ipt_release(struct tcf_ipt *ipt, int bind)
1da177e4
LT
73{
74 int ret = 0;
e9ce1cd3 75 if (ipt) {
1da177e4 76 if (bind)
e9ce1cd3
DM
77 ipt->tcf_bindcnt--;
78 ipt->tcf_refcnt--;
79 if (ipt->tcf_bindcnt <= 0 && ipt->tcf_refcnt <= 0) {
80 ipt_destroy_target(ipt->tcfi_t);
81 kfree(ipt->tcfi_tname);
82 kfree(ipt->tcfi_t);
83 tcf_hash_destroy(&ipt->common, &ipt_hash_info);
1da177e4
LT
84 ret = ACT_P_DELETED;
85 }
86 }
87 return ret;
88}
89
53b2bf3f
PM
90static const struct nla_policy ipt_policy[TCA_IPT_MAX + 1] = {
91 [TCA_IPT_TABLE] = { .type = NLA_STRING, .len = IFNAMSIZ },
92 [TCA_IPT_HOOK] = { .type = NLA_U32 },
93 [TCA_IPT_INDEX] = { .type = NLA_U32 },
87a2e70d 94 [TCA_IPT_TARG] = { .len = sizeof(struct xt_entry_target) },
53b2bf3f
PM
95};
96
c1b52739 97static int tcf_ipt_init(struct net *net, struct nlattr *nla, struct nlattr *est,
e9ce1cd3 98 struct tc_action *a, int ovr, int bind)
1da177e4 99{
7ba699c6 100 struct nlattr *tb[TCA_IPT_MAX + 1];
e9ce1cd3
DM
101 struct tcf_ipt *ipt;
102 struct tcf_common *pc;
87a2e70d 103 struct xt_entry_target *td, *t;
1da177e4
LT
104 char *tname;
105 int ret = 0, err;
106 u32 hook = 0;
107 u32 index = 0;
108
cee63723 109 if (nla == NULL)
1da177e4
LT
110 return -EINVAL;
111
53b2bf3f 112 err = nla_parse_nested(tb, TCA_IPT_MAX, nla, ipt_policy);
cee63723
PM
113 if (err < 0)
114 return err;
115
53b2bf3f 116 if (tb[TCA_IPT_HOOK] == NULL)
1da177e4 117 return -EINVAL;
53b2bf3f 118 if (tb[TCA_IPT_TARG] == NULL)
1da177e4 119 return -EINVAL;
53b2bf3f 120
87a2e70d 121 td = (struct xt_entry_target *)nla_data(tb[TCA_IPT_TARG]);
7ba699c6 122 if (nla_len(tb[TCA_IPT_TARG]) < td->u.target_size)
1da177e4
LT
123 return -EINVAL;
124
53b2bf3f 125 if (tb[TCA_IPT_INDEX] != NULL)
1587bac4 126 index = nla_get_u32(tb[TCA_IPT_INDEX]);
1da177e4 127
c779f7af 128 pc = tcf_hash_check(index, a, bind);
e9ce1cd3 129 if (!pc) {
c779f7af 130 pc = tcf_hash_create(index, est, a, sizeof(*ipt), bind);
0e991ec6 131 if (IS_ERR(pc))
cc7ec456 132 return PTR_ERR(pc);
1da177e4
LT
133 ret = ACT_P_CREATED;
134 } else {
1a29321e
JHS
135 if (bind)/* dont override defaults */
136 return 0;
137 tcf_ipt_release(to_ipt(pc), bind);
138
139 if (!ovr)
1da177e4 140 return -EEXIST;
1da177e4 141 }
e9ce1cd3 142 ipt = to_ipt(pc);
1da177e4 143
1587bac4 144 hook = nla_get_u32(tb[TCA_IPT_HOOK]);
1da177e4
LT
145
146 err = -ENOMEM;
147 tname = kmalloc(IFNAMSIZ, GFP_KERNEL);
e9ce1cd3 148 if (unlikely(!tname))
1da177e4 149 goto err1;
7ba699c6
PM
150 if (tb[TCA_IPT_TABLE] == NULL ||
151 nla_strlcpy(tname, tb[TCA_IPT_TABLE], IFNAMSIZ) >= IFNAMSIZ)
1da177e4
LT
152 strcpy(tname, "mangle");
153
c7b1b249 154 t = kmemdup(td, td->u.target_size, GFP_KERNEL);
e9ce1cd3 155 if (unlikely(!t))
1da177e4 156 goto err2;
1da177e4 157
cc7ec456
ED
158 err = ipt_init_target(t, tname, hook);
159 if (err < 0)
1da177e4
LT
160 goto err3;
161
e9ce1cd3 162 spin_lock_bh(&ipt->tcf_lock);
1da177e4 163 if (ret != ACT_P_CREATED) {
e9ce1cd3
DM
164 ipt_destroy_target(ipt->tcfi_t);
165 kfree(ipt->tcfi_tname);
166 kfree(ipt->tcfi_t);
1da177e4 167 }
e9ce1cd3
DM
168 ipt->tcfi_tname = tname;
169 ipt->tcfi_t = t;
170 ipt->tcfi_hook = hook;
171 spin_unlock_bh(&ipt->tcf_lock);
1da177e4 172 if (ret == ACT_P_CREATED)
c779f7af 173 tcf_hash_insert(pc, a->ops->hinfo);
1da177e4
LT
174 return ret;
175
176err3:
177 kfree(t);
178err2:
179 kfree(tname);
180err1:
47fd92f5
HS
181 if (ret == ACT_P_CREATED) {
182 if (est)
183 gen_kill_estimator(&pc->tcfc_bstats,
184 &pc->tcfc_rate_est);
185 kfree_rcu(pc, tcfc_rcu);
186 }
1da177e4
LT
187 return err;
188}
189
e9ce1cd3 190static int tcf_ipt_cleanup(struct tc_action *a, int bind)
1da177e4 191{
e9ce1cd3
DM
192 struct tcf_ipt *ipt = a->priv;
193 return tcf_ipt_release(ipt, bind);
1da177e4
LT
194}
195
dc7f9f6e 196static int tcf_ipt(struct sk_buff *skb, const struct tc_action *a,
e9ce1cd3 197 struct tcf_result *res)
1da177e4
LT
198{
199 int ret = 0, result = 0;
e9ce1cd3 200 struct tcf_ipt *ipt = a->priv;
4b560b44 201 struct xt_action_param par;
1da177e4 202
14bbd6a5
PS
203 if (skb_unclone(skb, GFP_ATOMIC))
204 return TC_ACT_UNSPEC;
1da177e4 205
e9ce1cd3 206 spin_lock(&ipt->tcf_lock);
1da177e4 207
e9ce1cd3 208 ipt->tcf_tm.lastuse = jiffies;
bfe0d029 209 bstats_update(&ipt->tcf_bstats, skb);
1da177e4
LT
210
211 /* yes, we have to worry about both in and out dev
cc7ec456
ED
212 * worry later - danger - this API seems to have changed
213 * from earlier kernels
214 */
7eb35586
JE
215 par.in = skb->dev;
216 par.out = NULL;
217 par.hooknum = ipt->tcfi_hook;
218 par.target = ipt->tcfi_t->u.kernel.target;
219 par.targinfo = ipt->tcfi_t->data;
220 ret = par.target->target(skb, &par);
221
1da177e4
LT
222 switch (ret) {
223 case NF_ACCEPT:
224 result = TC_ACT_OK;
225 break;
226 case NF_DROP:
227 result = TC_ACT_SHOT;
e9ce1cd3 228 ipt->tcf_qstats.drops++;
1da177e4 229 break;
243bf6e2 230 case XT_CONTINUE:
1da177e4
LT
231 result = TC_ACT_PIPE;
232 break;
233 default:
e87cc472
JP
234 net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT\n",
235 ret);
1da177e4
LT
236 result = TC_POLICE_OK;
237 break;
238 }
e9ce1cd3 239 spin_unlock(&ipt->tcf_lock);
1da177e4
LT
240 return result;
241
242}
243
e9ce1cd3 244static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
1da177e4 245{
27a884dc 246 unsigned char *b = skb_tail_pointer(skb);
e9ce1cd3 247 struct tcf_ipt *ipt = a->priv;
87a2e70d 248 struct xt_entry_target *t;
1da177e4
LT
249 struct tcf_t tm;
250 struct tc_cnt c;
1da177e4
LT
251
252 /* for simple targets kernel size == user size
cc7ec456
ED
253 * user name = target name
254 * for foolproof you need to not assume this
255 */
1da177e4 256
c7b1b249 257 t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC);
e9ce1cd3 258 if (unlikely(!t))
7ba699c6 259 goto nla_put_failure;
1da177e4 260
e9ce1cd3
DM
261 c.bindcnt = ipt->tcf_bindcnt - bind;
262 c.refcnt = ipt->tcf_refcnt - ref;
e9ce1cd3
DM
263 strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name);
264
1b34ec43
DM
265 if (nla_put(skb, TCA_IPT_TARG, ipt->tcfi_t->u.user.target_size, t) ||
266 nla_put_u32(skb, TCA_IPT_INDEX, ipt->tcf_index) ||
267 nla_put_u32(skb, TCA_IPT_HOOK, ipt->tcfi_hook) ||
268 nla_put(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c) ||
269 nla_put_string(skb, TCA_IPT_TABLE, ipt->tcfi_tname))
270 goto nla_put_failure;
e9ce1cd3
DM
271 tm.install = jiffies_to_clock_t(jiffies - ipt->tcf_tm.install);
272 tm.lastuse = jiffies_to_clock_t(jiffies - ipt->tcf_tm.lastuse);
273 tm.expires = jiffies_to_clock_t(ipt->tcf_tm.expires);
1b34ec43
DM
274 if (nla_put(skb, TCA_IPT_TM, sizeof (tm), &tm))
275 goto nla_put_failure;
1da177e4
LT
276 kfree(t);
277 return skb->len;
278
7ba699c6 279nla_put_failure:
dc5fc579 280 nlmsg_trim(skb, b);
1da177e4
LT
281 kfree(t);
282 return -1;
283}
284
285static struct tc_action_ops act_ipt_ops = {
286 .kind = "ipt",
e9ce1cd3 287 .hinfo = &ipt_hash_info,
1da177e4 288 .type = TCA_ACT_IPT,
1da177e4
LT
289 .owner = THIS_MODULE,
290 .act = tcf_ipt,
291 .dump = tcf_ipt_dump,
292 .cleanup = tcf_ipt_cleanup,
1da177e4 293 .init = tcf_ipt_init,
1da177e4
LT
294};
295
0dcffd09
JHS
296static struct tc_action_ops act_xt_ops = {
297 .kind = "xt",
298 .hinfo = &ipt_hash_info,
6c80563c 299 .type = TCA_ACT_XT,
0dcffd09
JHS
300 .owner = THIS_MODULE,
301 .act = tcf_ipt,
302 .dump = tcf_ipt_dump,
303 .cleanup = tcf_ipt_cleanup,
0dcffd09 304 .init = tcf_ipt_init,
0dcffd09
JHS
305};
306
307MODULE_AUTHOR("Jamal Hadi Salim(2002-13)");
1da177e4
LT
308MODULE_DESCRIPTION("Iptables target actions");
309MODULE_LICENSE("GPL");
0dcffd09 310MODULE_ALIAS("act_xt");
1da177e4 311
e9ce1cd3 312static int __init ipt_init_module(void)
1da177e4 313{
369ba567 314 int ret1, ret2, err;
568a153a 315 err = tcf_hashinfo_init(&ipt_hash_info, IPT_TAB_MASK);
369ba567
WC
316 if (err)
317 return err;
318
0dcffd09
JHS
319 ret1 = tcf_register_action(&act_xt_ops);
320 if (ret1 < 0)
321 printk("Failed to load xt action\n");
322 ret2 = tcf_register_action(&act_ipt_ops);
323 if (ret2 < 0)
324 printk("Failed to load ipt action\n");
325
369ba567
WC
326 if (ret1 < 0 && ret2 < 0) {
327 tcf_hashinfo_destroy(&ipt_hash_info);
0dcffd09 328 return ret1;
369ba567 329 } else
0dcffd09 330 return 0;
1da177e4
LT
331}
332
e9ce1cd3 333static void __exit ipt_cleanup_module(void)
1da177e4 334{
0dcffd09 335 tcf_unregister_action(&act_xt_ops);
1da177e4 336 tcf_unregister_action(&act_ipt_ops);
369ba567 337 tcf_hashinfo_destroy(&ipt_hash_info);
1da177e4
LT
338}
339
340module_init(ipt_init_module);
341module_exit(ipt_cleanup_module);
This page took 0.767035 seconds and 5 git commands to generate.