net_sched: act: hide struct tcf_common from API
[pandora-kernel.git] / net / sched / act_ipt.c
1 /*
2  * net/sched/ipt.c     iptables target interface
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  *
11  * Copyright:   Jamal Hadi Salim (2002-13)
12  */
13
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/skbuff.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <net/netlink.h>
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
30
31 #define IPT_TAB_MASK     15
32 static struct tcf_hashinfo ipt_hash_info;
33
34 static int ipt_init_target(struct xt_entry_target *t, char *table, unsigned int hook)
35 {
36         struct xt_tgchk_param par;
37         struct xt_target *target;
38         int ret = 0;
39
40         target = xt_request_find_target(AF_INET, t->u.user.name,
41                                         t->u.user.revision);
42         if (IS_ERR(target))
43                 return PTR_ERR(target);
44
45         t->u.kernel.target = target;
46         par.table     = table;
47         par.entryinfo = NULL;
48         par.target    = target;
49         par.targinfo  = t->data;
50         par.hook_mask = hook;
51         par.family    = NFPROTO_IPV4;
52
53         ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
54         if (ret < 0) {
55                 module_put(t->u.kernel.target->me);
56                 return ret;
57         }
58         return 0;
59 }
60
61 static void ipt_destroy_target(struct xt_entry_target *t)
62 {
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);
70 }
71
72 static int tcf_ipt_release(struct tc_action *a, int bind)
73 {
74         struct tcf_ipt *ipt = to_ipt(a);
75         int ret = 0;
76         if (ipt) {
77                 if (bind)
78                         ipt->tcf_bindcnt--;
79                 ipt->tcf_refcnt--;
80                 if (ipt->tcf_bindcnt <= 0 && ipt->tcf_refcnt <= 0) {
81                         ipt_destroy_target(ipt->tcfi_t);
82                         kfree(ipt->tcfi_tname);
83                         kfree(ipt->tcfi_t);
84                         tcf_hash_destroy(a);
85                         ret = ACT_P_DELETED;
86                 }
87         }
88         return ret;
89 }
90
91 static 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 },
95         [TCA_IPT_TARG]  = { .len = sizeof(struct xt_entry_target) },
96 };
97
98 static int tcf_ipt_init(struct net *net, struct nlattr *nla, struct nlattr *est,
99                         struct tc_action *a, int ovr, int bind)
100 {
101         struct nlattr *tb[TCA_IPT_MAX + 1];
102         struct tcf_ipt *ipt;
103         struct xt_entry_target *td, *t;
104         char *tname;
105         int ret = 0, err;
106         u32 hook = 0;
107         u32 index = 0;
108
109         if (nla == NULL)
110                 return -EINVAL;
111
112         err = nla_parse_nested(tb, TCA_IPT_MAX, nla, ipt_policy);
113         if (err < 0)
114                 return err;
115
116         if (tb[TCA_IPT_HOOK] == NULL)
117                 return -EINVAL;
118         if (tb[TCA_IPT_TARG] == NULL)
119                 return -EINVAL;
120
121         td = (struct xt_entry_target *)nla_data(tb[TCA_IPT_TARG]);
122         if (nla_len(tb[TCA_IPT_TARG]) < td->u.target_size)
123                 return -EINVAL;
124
125         if (tb[TCA_IPT_INDEX] != NULL)
126                 index = nla_get_u32(tb[TCA_IPT_INDEX]);
127
128         if (!tcf_hash_check(index, a, bind) ) {
129                 ret = tcf_hash_create(index, est, a, sizeof(*ipt), bind);
130                 if (ret)
131                         return ret;
132                 ret = ACT_P_CREATED;
133         } else {
134                 if (bind)/* dont override defaults */
135                         return 0;
136                 tcf_ipt_release(a, bind);
137
138                 if (!ovr)
139                         return -EEXIST;
140         }
141         ipt = to_ipt(a);
142
143         hook = nla_get_u32(tb[TCA_IPT_HOOK]);
144
145         err = -ENOMEM;
146         tname = kmalloc(IFNAMSIZ, GFP_KERNEL);
147         if (unlikely(!tname))
148                 goto err1;
149         if (tb[TCA_IPT_TABLE] == NULL ||
150             nla_strlcpy(tname, tb[TCA_IPT_TABLE], IFNAMSIZ) >= IFNAMSIZ)
151                 strcpy(tname, "mangle");
152
153         t = kmemdup(td, td->u.target_size, GFP_KERNEL);
154         if (unlikely(!t))
155                 goto err2;
156
157         err = ipt_init_target(t, tname, hook);
158         if (err < 0)
159                 goto err3;
160
161         spin_lock_bh(&ipt->tcf_lock);
162         if (ret != ACT_P_CREATED) {
163                 ipt_destroy_target(ipt->tcfi_t);
164                 kfree(ipt->tcfi_tname);
165                 kfree(ipt->tcfi_t);
166         }
167         ipt->tcfi_tname = tname;
168         ipt->tcfi_t     = t;
169         ipt->tcfi_hook  = hook;
170         spin_unlock_bh(&ipt->tcf_lock);
171         if (ret == ACT_P_CREATED)
172                 tcf_hash_insert(a);
173         return ret;
174
175 err3:
176         kfree(t);
177 err2:
178         kfree(tname);
179 err1:
180         if (ret == ACT_P_CREATED)
181                 tcf_hash_cleanup(a, est);
182         return err;
183 }
184
185 static int tcf_ipt(struct sk_buff *skb, const struct tc_action *a,
186                    struct tcf_result *res)
187 {
188         int ret = 0, result = 0;
189         struct tcf_ipt *ipt = a->priv;
190         struct xt_action_param par;
191
192         if (skb_unclone(skb, GFP_ATOMIC))
193                 return TC_ACT_UNSPEC;
194
195         spin_lock(&ipt->tcf_lock);
196
197         ipt->tcf_tm.lastuse = jiffies;
198         bstats_update(&ipt->tcf_bstats, skb);
199
200         /* yes, we have to worry about both in and out dev
201          * worry later - danger - this API seems to have changed
202          * from earlier kernels
203          */
204         par.in       = skb->dev;
205         par.out      = NULL;
206         par.hooknum  = ipt->tcfi_hook;
207         par.target   = ipt->tcfi_t->u.kernel.target;
208         par.targinfo = ipt->tcfi_t->data;
209         ret = par.target->target(skb, &par);
210
211         switch (ret) {
212         case NF_ACCEPT:
213                 result = TC_ACT_OK;
214                 break;
215         case NF_DROP:
216                 result = TC_ACT_SHOT;
217                 ipt->tcf_qstats.drops++;
218                 break;
219         case XT_CONTINUE:
220                 result = TC_ACT_PIPE;
221                 break;
222         default:
223                 net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT\n",
224                                        ret);
225                 result = TC_POLICE_OK;
226                 break;
227         }
228         spin_unlock(&ipt->tcf_lock);
229         return result;
230
231 }
232
233 static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
234 {
235         unsigned char *b = skb_tail_pointer(skb);
236         struct tcf_ipt *ipt = a->priv;
237         struct xt_entry_target *t;
238         struct tcf_t tm;
239         struct tc_cnt c;
240
241         /* for simple targets kernel size == user size
242          * user name = target name
243          * for foolproof you need to not assume this
244          */
245
246         t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC);
247         if (unlikely(!t))
248                 goto nla_put_failure;
249
250         c.bindcnt = ipt->tcf_bindcnt - bind;
251         c.refcnt = ipt->tcf_refcnt - ref;
252         strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name);
253
254         if (nla_put(skb, TCA_IPT_TARG, ipt->tcfi_t->u.user.target_size, t) ||
255             nla_put_u32(skb, TCA_IPT_INDEX, ipt->tcf_index) ||
256             nla_put_u32(skb, TCA_IPT_HOOK, ipt->tcfi_hook) ||
257             nla_put(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c) ||
258             nla_put_string(skb, TCA_IPT_TABLE, ipt->tcfi_tname))
259                 goto nla_put_failure;
260         tm.install = jiffies_to_clock_t(jiffies - ipt->tcf_tm.install);
261         tm.lastuse = jiffies_to_clock_t(jiffies - ipt->tcf_tm.lastuse);
262         tm.expires = jiffies_to_clock_t(ipt->tcf_tm.expires);
263         if (nla_put(skb, TCA_IPT_TM, sizeof (tm), &tm))
264                 goto nla_put_failure;
265         kfree(t);
266         return skb->len;
267
268 nla_put_failure:
269         nlmsg_trim(skb, b);
270         kfree(t);
271         return -1;
272 }
273
274 static struct tc_action_ops act_ipt_ops = {
275         .kind           =       "ipt",
276         .hinfo          =       &ipt_hash_info,
277         .type           =       TCA_ACT_IPT,
278         .owner          =       THIS_MODULE,
279         .act            =       tcf_ipt,
280         .dump           =       tcf_ipt_dump,
281         .cleanup        =       tcf_ipt_release,
282         .init           =       tcf_ipt_init,
283 };
284
285 static struct tc_action_ops act_xt_ops = {
286         .kind           =       "xt",
287         .hinfo          =       &ipt_hash_info,
288         .type           =       TCA_ACT_XT,
289         .owner          =       THIS_MODULE,
290         .act            =       tcf_ipt,
291         .dump           =       tcf_ipt_dump,
292         .cleanup        =       tcf_ipt_release,
293         .init           =       tcf_ipt_init,
294 };
295
296 MODULE_AUTHOR("Jamal Hadi Salim(2002-13)");
297 MODULE_DESCRIPTION("Iptables target actions");
298 MODULE_LICENSE("GPL");
299 MODULE_ALIAS("act_xt");
300
301 static int __init ipt_init_module(void)
302 {
303         int ret1, ret2, err;
304         err = tcf_hashinfo_init(&ipt_hash_info, IPT_TAB_MASK);
305         if (err)
306                 return err;
307
308         ret1 = tcf_register_action(&act_xt_ops);
309         if (ret1 < 0)
310                 printk("Failed to load xt action\n");
311         ret2 = tcf_register_action(&act_ipt_ops);
312         if (ret2 < 0)
313                 printk("Failed to load ipt action\n");
314
315         if (ret1 < 0 && ret2 < 0) {
316                 tcf_hashinfo_destroy(&ipt_hash_info);
317                 return ret1;
318         } else
319                 return 0;
320 }
321
322 static void __exit ipt_cleanup_module(void)
323 {
324         tcf_unregister_action(&act_xt_ops);
325         tcf_unregister_action(&act_ipt_ops);
326         tcf_hashinfo_destroy(&ipt_hash_info);
327 }
328
329 module_init(ipt_init_module);
330 module_exit(ipt_cleanup_module);