Commit | Line | Data |
---|---|---|
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 | |
e9ce1cd3 | 31 | #define IPT_TAB_MASK 15 |
1da177e4 | 32 | |
ddf97ccd WC |
33 | static int ipt_net_id; |
34 | ||
35 | static int xt_net_id; | |
36 | ||
87a2e70d | 37 | static int ipt_init_target(struct xt_entry_target *t, char *table, unsigned int hook) |
1da177e4 | 38 | { |
af5d6dc2 | 39 | struct xt_tgchk_param par; |
e60a13e0 | 40 | struct xt_target *target; |
1da177e4 LT |
41 | int ret = 0; |
42 | ||
239a87c8 PM |
43 | target = xt_request_find_target(AF_INET, t->u.user.name, |
44 | t->u.user.revision); | |
d2a7b6ba JE |
45 | if (IS_ERR(target)) |
46 | return PTR_ERR(target); | |
1da177e4 | 47 | |
1da177e4 | 48 | t->u.kernel.target = target; |
af5d6dc2 JE |
49 | par.table = table; |
50 | par.entryinfo = NULL; | |
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 | ||
87a2e70d | 64 | static void ipt_destroy_target(struct xt_entry_target *t) |
1da177e4 | 65 | { |
a2df1648 JE |
66 | struct xt_tgdtor_param par = { |
67 | .target = t->u.kernel.target, | |
68 | .targinfo = t->data, | |
69 | }; | |
70 | if (par.target->destroy != NULL) | |
71 | par.target->destroy(&par); | |
72 | module_put(par.target->me); | |
1da177e4 LT |
73 | } |
74 | ||
a5b5c958 | 75 | static void tcf_ipt_release(struct tc_action *a, int bind) |
1da177e4 | 76 | { |
86062033 | 77 | struct tcf_ipt *ipt = to_ipt(a); |
a5b5c958 WC |
78 | ipt_destroy_target(ipt->tcfi_t); |
79 | kfree(ipt->tcfi_tname); | |
80 | kfree(ipt->tcfi_t); | |
1da177e4 LT |
81 | } |
82 | ||
53b2bf3f PM |
83 | static const struct nla_policy ipt_policy[TCA_IPT_MAX + 1] = { |
84 | [TCA_IPT_TABLE] = { .type = NLA_STRING, .len = IFNAMSIZ }, | |
85 | [TCA_IPT_HOOK] = { .type = NLA_U32 }, | |
86 | [TCA_IPT_INDEX] = { .type = NLA_U32 }, | |
87a2e70d | 87 | [TCA_IPT_TARG] = { .len = sizeof(struct xt_entry_target) }, |
53b2bf3f PM |
88 | }; |
89 | ||
ddf97ccd WC |
90 | static int __tcf_ipt_init(struct tc_action_net *tn, struct nlattr *nla, |
91 | struct nlattr *est, struct tc_action *a, int ovr, | |
92 | int bind) | |
1da177e4 | 93 | { |
7ba699c6 | 94 | struct nlattr *tb[TCA_IPT_MAX + 1]; |
e9ce1cd3 | 95 | struct tcf_ipt *ipt; |
87a2e70d | 96 | struct xt_entry_target *td, *t; |
1da177e4 LT |
97 | char *tname; |
98 | int ret = 0, err; | |
99 | u32 hook = 0; | |
100 | u32 index = 0; | |
101 | ||
cee63723 | 102 | if (nla == NULL) |
1da177e4 LT |
103 | return -EINVAL; |
104 | ||
53b2bf3f | 105 | err = nla_parse_nested(tb, TCA_IPT_MAX, nla, ipt_policy); |
cee63723 PM |
106 | if (err < 0) |
107 | return err; | |
108 | ||
53b2bf3f | 109 | if (tb[TCA_IPT_HOOK] == NULL) |
1da177e4 | 110 | return -EINVAL; |
53b2bf3f | 111 | if (tb[TCA_IPT_TARG] == NULL) |
1da177e4 | 112 | return -EINVAL; |
53b2bf3f | 113 | |
87a2e70d | 114 | td = (struct xt_entry_target *)nla_data(tb[TCA_IPT_TARG]); |
7ba699c6 | 115 | if (nla_len(tb[TCA_IPT_TARG]) < td->u.target_size) |
1da177e4 LT |
116 | return -EINVAL; |
117 | ||
53b2bf3f | 118 | if (tb[TCA_IPT_INDEX] != NULL) |
1587bac4 | 119 | index = nla_get_u32(tb[TCA_IPT_INDEX]); |
1da177e4 | 120 | |
ddf97ccd WC |
121 | if (!tcf_hash_check(tn, index, a, bind)) { |
122 | ret = tcf_hash_create(tn, index, est, a, sizeof(*ipt), bind, | |
123 | false); | |
86062033 WC |
124 | if (ret) |
125 | return ret; | |
1da177e4 LT |
126 | ret = ACT_P_CREATED; |
127 | } else { | |
1a29321e JHS |
128 | if (bind)/* dont override defaults */ |
129 | return 0; | |
a5b5c958 | 130 | tcf_hash_release(a, bind); |
1a29321e JHS |
131 | |
132 | if (!ovr) | |
1da177e4 | 133 | return -EEXIST; |
1da177e4 | 134 | } |
86062033 | 135 | ipt = to_ipt(a); |
1da177e4 | 136 | |
1587bac4 | 137 | hook = nla_get_u32(tb[TCA_IPT_HOOK]); |
1da177e4 LT |
138 | |
139 | err = -ENOMEM; | |
140 | tname = kmalloc(IFNAMSIZ, GFP_KERNEL); | |
e9ce1cd3 | 141 | if (unlikely(!tname)) |
1da177e4 | 142 | goto err1; |
7ba699c6 PM |
143 | if (tb[TCA_IPT_TABLE] == NULL || |
144 | nla_strlcpy(tname, tb[TCA_IPT_TABLE], IFNAMSIZ) >= IFNAMSIZ) | |
1da177e4 LT |
145 | strcpy(tname, "mangle"); |
146 | ||
c7b1b249 | 147 | t = kmemdup(td, td->u.target_size, GFP_KERNEL); |
e9ce1cd3 | 148 | if (unlikely(!t)) |
1da177e4 | 149 | goto err2; |
1da177e4 | 150 | |
cc7ec456 ED |
151 | err = ipt_init_target(t, tname, hook); |
152 | if (err < 0) | |
1da177e4 LT |
153 | goto err3; |
154 | ||
e9ce1cd3 | 155 | spin_lock_bh(&ipt->tcf_lock); |
1da177e4 | 156 | if (ret != ACT_P_CREATED) { |
e9ce1cd3 DM |
157 | ipt_destroy_target(ipt->tcfi_t); |
158 | kfree(ipt->tcfi_tname); | |
159 | kfree(ipt->tcfi_t); | |
1da177e4 | 160 | } |
e9ce1cd3 DM |
161 | ipt->tcfi_tname = tname; |
162 | ipt->tcfi_t = t; | |
163 | ipt->tcfi_hook = hook; | |
164 | spin_unlock_bh(&ipt->tcf_lock); | |
1da177e4 | 165 | if (ret == ACT_P_CREATED) |
ddf97ccd | 166 | tcf_hash_insert(tn, a); |
1da177e4 LT |
167 | return ret; |
168 | ||
169 | err3: | |
170 | kfree(t); | |
171 | err2: | |
172 | kfree(tname); | |
173 | err1: | |
86062033 WC |
174 | if (ret == ACT_P_CREATED) |
175 | tcf_hash_cleanup(a, est); | |
1da177e4 LT |
176 | return err; |
177 | } | |
178 | ||
ddf97ccd WC |
179 | static int tcf_ipt_init(struct net *net, struct nlattr *nla, |
180 | struct nlattr *est, struct tc_action *a, int ovr, | |
181 | int bind) | |
182 | { | |
183 | struct tc_action_net *tn = net_generic(net, ipt_net_id); | |
184 | ||
185 | return __tcf_ipt_init(tn, nla, est, a, ovr, bind); | |
186 | } | |
187 | ||
188 | static int tcf_xt_init(struct net *net, struct nlattr *nla, | |
189 | struct nlattr *est, struct tc_action *a, int ovr, | |
190 | int bind) | |
191 | { | |
192 | struct tc_action_net *tn = net_generic(net, xt_net_id); | |
193 | ||
194 | return __tcf_ipt_init(tn, nla, est, a, ovr, bind); | |
195 | } | |
196 | ||
dc7f9f6e | 197 | static int tcf_ipt(struct sk_buff *skb, const struct tc_action *a, |
e9ce1cd3 | 198 | struct tcf_result *res) |
1da177e4 LT |
199 | { |
200 | int ret = 0, result = 0; | |
e9ce1cd3 | 201 | struct tcf_ipt *ipt = a->priv; |
4b560b44 | 202 | struct xt_action_param par; |
1da177e4 | 203 | |
14bbd6a5 PS |
204 | if (skb_unclone(skb, GFP_ATOMIC)) |
205 | return TC_ACT_UNSPEC; | |
1da177e4 | 206 | |
e9ce1cd3 | 207 | spin_lock(&ipt->tcf_lock); |
1da177e4 | 208 | |
e9ce1cd3 | 209 | ipt->tcf_tm.lastuse = jiffies; |
bfe0d029 | 210 | bstats_update(&ipt->tcf_bstats, skb); |
1da177e4 LT |
211 | |
212 | /* yes, we have to worry about both in and out dev | |
cc7ec456 ED |
213 | * worry later - danger - this API seems to have changed |
214 | * from earlier kernels | |
215 | */ | |
156c196f | 216 | par.net = dev_net(skb->dev); |
7eb35586 JE |
217 | par.in = skb->dev; |
218 | par.out = NULL; | |
219 | par.hooknum = ipt->tcfi_hook; | |
220 | par.target = ipt->tcfi_t->u.kernel.target; | |
221 | par.targinfo = ipt->tcfi_t->data; | |
222 | ret = par.target->target(skb, &par); | |
223 | ||
1da177e4 LT |
224 | switch (ret) { |
225 | case NF_ACCEPT: | |
226 | result = TC_ACT_OK; | |
227 | break; | |
228 | case NF_DROP: | |
229 | result = TC_ACT_SHOT; | |
e9ce1cd3 | 230 | ipt->tcf_qstats.drops++; |
1da177e4 | 231 | break; |
243bf6e2 | 232 | case XT_CONTINUE: |
1da177e4 LT |
233 | result = TC_ACT_PIPE; |
234 | break; | |
235 | default: | |
e87cc472 JP |
236 | net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT\n", |
237 | ret); | |
1da177e4 LT |
238 | result = TC_POLICE_OK; |
239 | break; | |
240 | } | |
e9ce1cd3 | 241 | spin_unlock(&ipt->tcf_lock); |
1da177e4 LT |
242 | return result; |
243 | ||
244 | } | |
245 | ||
e9ce1cd3 | 246 | static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
1da177e4 | 247 | { |
27a884dc | 248 | unsigned char *b = skb_tail_pointer(skb); |
e9ce1cd3 | 249 | struct tcf_ipt *ipt = a->priv; |
87a2e70d | 250 | struct xt_entry_target *t; |
1da177e4 LT |
251 | struct tcf_t tm; |
252 | struct tc_cnt c; | |
1da177e4 LT |
253 | |
254 | /* for simple targets kernel size == user size | |
cc7ec456 ED |
255 | * user name = target name |
256 | * for foolproof you need to not assume this | |
257 | */ | |
1da177e4 | 258 | |
c7b1b249 | 259 | t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC); |
e9ce1cd3 | 260 | if (unlikely(!t)) |
7ba699c6 | 261 | goto nla_put_failure; |
1da177e4 | 262 | |
e9ce1cd3 DM |
263 | c.bindcnt = ipt->tcf_bindcnt - bind; |
264 | c.refcnt = ipt->tcf_refcnt - ref; | |
e9ce1cd3 DM |
265 | strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name); |
266 | ||
1b34ec43 DM |
267 | if (nla_put(skb, TCA_IPT_TARG, ipt->tcfi_t->u.user.target_size, t) || |
268 | nla_put_u32(skb, TCA_IPT_INDEX, ipt->tcf_index) || | |
269 | nla_put_u32(skb, TCA_IPT_HOOK, ipt->tcfi_hook) || | |
270 | nla_put(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c) || | |
271 | nla_put_string(skb, TCA_IPT_TABLE, ipt->tcfi_tname)) | |
272 | goto nla_put_failure; | |
e9ce1cd3 DM |
273 | tm.install = jiffies_to_clock_t(jiffies - ipt->tcf_tm.install); |
274 | tm.lastuse = jiffies_to_clock_t(jiffies - ipt->tcf_tm.lastuse); | |
275 | tm.expires = jiffies_to_clock_t(ipt->tcf_tm.expires); | |
1b34ec43 DM |
276 | if (nla_put(skb, TCA_IPT_TM, sizeof (tm), &tm)) |
277 | goto nla_put_failure; | |
1da177e4 LT |
278 | kfree(t); |
279 | return skb->len; | |
280 | ||
7ba699c6 | 281 | nla_put_failure: |
dc5fc579 | 282 | nlmsg_trim(skb, b); |
1da177e4 LT |
283 | kfree(t); |
284 | return -1; | |
285 | } | |
286 | ||
ddf97ccd WC |
287 | static int tcf_ipt_walker(struct net *net, struct sk_buff *skb, |
288 | struct netlink_callback *cb, int type, | |
289 | struct tc_action *a) | |
290 | { | |
291 | struct tc_action_net *tn = net_generic(net, ipt_net_id); | |
292 | ||
293 | return tcf_generic_walker(tn, skb, cb, type, a); | |
294 | } | |
295 | ||
296 | static int tcf_ipt_search(struct net *net, struct tc_action *a, u32 index) | |
297 | { | |
298 | struct tc_action_net *tn = net_generic(net, ipt_net_id); | |
299 | ||
300 | return tcf_hash_search(tn, a, index); | |
301 | } | |
302 | ||
1da177e4 LT |
303 | static struct tc_action_ops act_ipt_ops = { |
304 | .kind = "ipt", | |
305 | .type = TCA_ACT_IPT, | |
1da177e4 LT |
306 | .owner = THIS_MODULE, |
307 | .act = tcf_ipt, | |
308 | .dump = tcf_ipt_dump, | |
86062033 | 309 | .cleanup = tcf_ipt_release, |
1da177e4 | 310 | .init = tcf_ipt_init, |
ddf97ccd WC |
311 | .walk = tcf_ipt_walker, |
312 | .lookup = tcf_ipt_search, | |
313 | }; | |
314 | ||
315 | static __net_init int ipt_init_net(struct net *net) | |
316 | { | |
317 | struct tc_action_net *tn = net_generic(net, ipt_net_id); | |
318 | ||
319 | return tc_action_net_init(tn, &act_ipt_ops, IPT_TAB_MASK); | |
320 | } | |
321 | ||
322 | static void __net_exit ipt_exit_net(struct net *net) | |
323 | { | |
324 | struct tc_action_net *tn = net_generic(net, ipt_net_id); | |
325 | ||
326 | tc_action_net_exit(tn); | |
327 | } | |
328 | ||
329 | static struct pernet_operations ipt_net_ops = { | |
330 | .init = ipt_init_net, | |
331 | .exit = ipt_exit_net, | |
332 | .id = &ipt_net_id, | |
333 | .size = sizeof(struct tc_action_net), | |
1da177e4 LT |
334 | }; |
335 | ||
ddf97ccd WC |
336 | static int tcf_xt_walker(struct net *net, struct sk_buff *skb, |
337 | struct netlink_callback *cb, int type, | |
338 | struct tc_action *a) | |
339 | { | |
340 | struct tc_action_net *tn = net_generic(net, xt_net_id); | |
341 | ||
342 | return tcf_generic_walker(tn, skb, cb, type, a); | |
343 | } | |
344 | ||
345 | static int tcf_xt_search(struct net *net, struct tc_action *a, u32 index) | |
346 | { | |
347 | struct tc_action_net *tn = net_generic(net, xt_net_id); | |
348 | ||
349 | return tcf_hash_search(tn, a, index); | |
350 | } | |
351 | ||
0dcffd09 JHS |
352 | static struct tc_action_ops act_xt_ops = { |
353 | .kind = "xt", | |
6c80563c | 354 | .type = TCA_ACT_XT, |
0dcffd09 JHS |
355 | .owner = THIS_MODULE, |
356 | .act = tcf_ipt, | |
357 | .dump = tcf_ipt_dump, | |
86062033 | 358 | .cleanup = tcf_ipt_release, |
ddf97ccd WC |
359 | .init = tcf_xt_init, |
360 | .walk = tcf_xt_walker, | |
361 | .lookup = tcf_xt_search, | |
362 | }; | |
363 | ||
364 | static __net_init int xt_init_net(struct net *net) | |
365 | { | |
366 | struct tc_action_net *tn = net_generic(net, xt_net_id); | |
367 | ||
368 | return tc_action_net_init(tn, &act_xt_ops, IPT_TAB_MASK); | |
369 | } | |
370 | ||
371 | static void __net_exit xt_exit_net(struct net *net) | |
372 | { | |
373 | struct tc_action_net *tn = net_generic(net, xt_net_id); | |
374 | ||
375 | tc_action_net_exit(tn); | |
376 | } | |
377 | ||
378 | static struct pernet_operations xt_net_ops = { | |
379 | .init = xt_init_net, | |
380 | .exit = xt_exit_net, | |
381 | .id = &xt_net_id, | |
382 | .size = sizeof(struct tc_action_net), | |
0dcffd09 JHS |
383 | }; |
384 | ||
385 | MODULE_AUTHOR("Jamal Hadi Salim(2002-13)"); | |
1da177e4 LT |
386 | MODULE_DESCRIPTION("Iptables target actions"); |
387 | MODULE_LICENSE("GPL"); | |
0dcffd09 | 388 | MODULE_ALIAS("act_xt"); |
1da177e4 | 389 | |
e9ce1cd3 | 390 | static int __init ipt_init_module(void) |
1da177e4 | 391 | { |
4f1e9d89 | 392 | int ret1, ret2; |
369ba567 | 393 | |
ddf97ccd | 394 | ret1 = tcf_register_action(&act_xt_ops, &xt_net_ops); |
0dcffd09 | 395 | if (ret1 < 0) |
ddf97ccd WC |
396 | pr_err("Failed to load xt action\n"); |
397 | ||
398 | ret2 = tcf_register_action(&act_ipt_ops, &ipt_net_ops); | |
0dcffd09 | 399 | if (ret2 < 0) |
ddf97ccd | 400 | pr_err("Failed to load ipt action\n"); |
0dcffd09 | 401 | |
369ba567 | 402 | if (ret1 < 0 && ret2 < 0) { |
0dcffd09 | 403 | return ret1; |
369ba567 | 404 | } else |
0dcffd09 | 405 | return 0; |
1da177e4 LT |
406 | } |
407 | ||
e9ce1cd3 | 408 | static void __exit ipt_cleanup_module(void) |
1da177e4 | 409 | { |
ddf97ccd WC |
410 | tcf_unregister_action(&act_ipt_ops, &ipt_net_ops); |
411 | tcf_unregister_action(&act_xt_ops, &xt_net_ops); | |
1da177e4 LT |
412 | } |
413 | ||
414 | module_init(ipt_init_module); | |
415 | module_exit(ipt_cleanup_module); |