]>
Commit | Line | Data |
---|---|---|
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 | 32 | static struct tcf_hashinfo ipt_hash_info; |
1da177e4 | 33 | |
87a2e70d | 34 | static 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 | 61 | static 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 | ||
86062033 | 72 | static int tcf_ipt_release(struct tc_action *a, int bind) |
1da177e4 | 73 | { |
86062033 | 74 | struct tcf_ipt *ipt = to_ipt(a); |
1da177e4 | 75 | int ret = 0; |
e9ce1cd3 | 76 | if (ipt) { |
1da177e4 | 77 | if (bind) |
e9ce1cd3 DM |
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); | |
86062033 | 84 | tcf_hash_destroy(a); |
1da177e4 LT |
85 | ret = ACT_P_DELETED; |
86 | } | |
87 | } | |
88 | return ret; | |
89 | } | |
90 | ||
53b2bf3f PM |
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 }, | |
87a2e70d | 95 | [TCA_IPT_TARG] = { .len = sizeof(struct xt_entry_target) }, |
53b2bf3f PM |
96 | }; |
97 | ||
c1b52739 | 98 | static int tcf_ipt_init(struct net *net, struct nlattr *nla, struct nlattr *est, |
e9ce1cd3 | 99 | struct tc_action *a, int ovr, int bind) |
1da177e4 | 100 | { |
7ba699c6 | 101 | struct nlattr *tb[TCA_IPT_MAX + 1]; |
e9ce1cd3 | 102 | struct tcf_ipt *ipt; |
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 | |
86062033 WC |
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; | |
1da177e4 LT |
132 | ret = ACT_P_CREATED; |
133 | } else { | |
1a29321e JHS |
134 | if (bind)/* dont override defaults */ |
135 | return 0; | |
86062033 | 136 | tcf_ipt_release(a, bind); |
1a29321e JHS |
137 | |
138 | if (!ovr) | |
1da177e4 | 139 | return -EEXIST; |
1da177e4 | 140 | } |
86062033 | 141 | ipt = to_ipt(a); |
1da177e4 | 142 | |
1587bac4 | 143 | hook = nla_get_u32(tb[TCA_IPT_HOOK]); |
1da177e4 LT |
144 | |
145 | err = -ENOMEM; | |
146 | tname = kmalloc(IFNAMSIZ, GFP_KERNEL); | |
e9ce1cd3 | 147 | if (unlikely(!tname)) |
1da177e4 | 148 | goto err1; |
7ba699c6 PM |
149 | if (tb[TCA_IPT_TABLE] == NULL || |
150 | nla_strlcpy(tname, tb[TCA_IPT_TABLE], IFNAMSIZ) >= IFNAMSIZ) | |
1da177e4 LT |
151 | strcpy(tname, "mangle"); |
152 | ||
c7b1b249 | 153 | t = kmemdup(td, td->u.target_size, GFP_KERNEL); |
e9ce1cd3 | 154 | if (unlikely(!t)) |
1da177e4 | 155 | goto err2; |
1da177e4 | 156 | |
cc7ec456 ED |
157 | err = ipt_init_target(t, tname, hook); |
158 | if (err < 0) | |
1da177e4 LT |
159 | goto err3; |
160 | ||
e9ce1cd3 | 161 | spin_lock_bh(&ipt->tcf_lock); |
1da177e4 | 162 | if (ret != ACT_P_CREATED) { |
e9ce1cd3 DM |
163 | ipt_destroy_target(ipt->tcfi_t); |
164 | kfree(ipt->tcfi_tname); | |
165 | kfree(ipt->tcfi_t); | |
1da177e4 | 166 | } |
e9ce1cd3 DM |
167 | ipt->tcfi_tname = tname; |
168 | ipt->tcfi_t = t; | |
169 | ipt->tcfi_hook = hook; | |
170 | spin_unlock_bh(&ipt->tcf_lock); | |
1da177e4 | 171 | if (ret == ACT_P_CREATED) |
86062033 | 172 | tcf_hash_insert(a); |
1da177e4 LT |
173 | return ret; |
174 | ||
175 | err3: | |
176 | kfree(t); | |
177 | err2: | |
178 | kfree(tname); | |
179 | err1: | |
86062033 WC |
180 | if (ret == ACT_P_CREATED) |
181 | tcf_hash_cleanup(a, est); | |
1da177e4 LT |
182 | return err; |
183 | } | |
184 | ||
dc7f9f6e | 185 | static int tcf_ipt(struct sk_buff *skb, const struct tc_action *a, |
e9ce1cd3 | 186 | struct tcf_result *res) |
1da177e4 LT |
187 | { |
188 | int ret = 0, result = 0; | |
e9ce1cd3 | 189 | struct tcf_ipt *ipt = a->priv; |
4b560b44 | 190 | struct xt_action_param par; |
1da177e4 | 191 | |
14bbd6a5 PS |
192 | if (skb_unclone(skb, GFP_ATOMIC)) |
193 | return TC_ACT_UNSPEC; | |
1da177e4 | 194 | |
e9ce1cd3 | 195 | spin_lock(&ipt->tcf_lock); |
1da177e4 | 196 | |
e9ce1cd3 | 197 | ipt->tcf_tm.lastuse = jiffies; |
bfe0d029 | 198 | bstats_update(&ipt->tcf_bstats, skb); |
1da177e4 LT |
199 | |
200 | /* yes, we have to worry about both in and out dev | |
cc7ec456 ED |
201 | * worry later - danger - this API seems to have changed |
202 | * from earlier kernels | |
203 | */ | |
7eb35586 JE |
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 | ||
1da177e4 LT |
211 | switch (ret) { |
212 | case NF_ACCEPT: | |
213 | result = TC_ACT_OK; | |
214 | break; | |
215 | case NF_DROP: | |
216 | result = TC_ACT_SHOT; | |
e9ce1cd3 | 217 | ipt->tcf_qstats.drops++; |
1da177e4 | 218 | break; |
243bf6e2 | 219 | case XT_CONTINUE: |
1da177e4 LT |
220 | result = TC_ACT_PIPE; |
221 | break; | |
222 | default: | |
e87cc472 JP |
223 | net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT\n", |
224 | ret); | |
1da177e4 LT |
225 | result = TC_POLICE_OK; |
226 | break; | |
227 | } | |
e9ce1cd3 | 228 | spin_unlock(&ipt->tcf_lock); |
1da177e4 LT |
229 | return result; |
230 | ||
231 | } | |
232 | ||
e9ce1cd3 | 233 | static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
1da177e4 | 234 | { |
27a884dc | 235 | unsigned char *b = skb_tail_pointer(skb); |
e9ce1cd3 | 236 | struct tcf_ipt *ipt = a->priv; |
87a2e70d | 237 | struct xt_entry_target *t; |
1da177e4 LT |
238 | struct tcf_t tm; |
239 | struct tc_cnt c; | |
1da177e4 LT |
240 | |
241 | /* for simple targets kernel size == user size | |
cc7ec456 ED |
242 | * user name = target name |
243 | * for foolproof you need to not assume this | |
244 | */ | |
1da177e4 | 245 | |
c7b1b249 | 246 | t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC); |
e9ce1cd3 | 247 | if (unlikely(!t)) |
7ba699c6 | 248 | goto nla_put_failure; |
1da177e4 | 249 | |
e9ce1cd3 DM |
250 | c.bindcnt = ipt->tcf_bindcnt - bind; |
251 | c.refcnt = ipt->tcf_refcnt - ref; | |
e9ce1cd3 DM |
252 | strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name); |
253 | ||
1b34ec43 DM |
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; | |
e9ce1cd3 DM |
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); | |
1b34ec43 DM |
263 | if (nla_put(skb, TCA_IPT_TM, sizeof (tm), &tm)) |
264 | goto nla_put_failure; | |
1da177e4 LT |
265 | kfree(t); |
266 | return skb->len; | |
267 | ||
7ba699c6 | 268 | nla_put_failure: |
dc5fc579 | 269 | nlmsg_trim(skb, b); |
1da177e4 LT |
270 | kfree(t); |
271 | return -1; | |
272 | } | |
273 | ||
274 | static struct tc_action_ops act_ipt_ops = { | |
275 | .kind = "ipt", | |
e9ce1cd3 | 276 | .hinfo = &ipt_hash_info, |
1da177e4 | 277 | .type = TCA_ACT_IPT, |
1da177e4 LT |
278 | .owner = THIS_MODULE, |
279 | .act = tcf_ipt, | |
280 | .dump = tcf_ipt_dump, | |
86062033 | 281 | .cleanup = tcf_ipt_release, |
1da177e4 | 282 | .init = tcf_ipt_init, |
1da177e4 LT |
283 | }; |
284 | ||
0dcffd09 JHS |
285 | static struct tc_action_ops act_xt_ops = { |
286 | .kind = "xt", | |
287 | .hinfo = &ipt_hash_info, | |
6c80563c | 288 | .type = TCA_ACT_XT, |
0dcffd09 JHS |
289 | .owner = THIS_MODULE, |
290 | .act = tcf_ipt, | |
291 | .dump = tcf_ipt_dump, | |
86062033 | 292 | .cleanup = tcf_ipt_release, |
0dcffd09 | 293 | .init = tcf_ipt_init, |
0dcffd09 JHS |
294 | }; |
295 | ||
296 | MODULE_AUTHOR("Jamal Hadi Salim(2002-13)"); | |
1da177e4 LT |
297 | MODULE_DESCRIPTION("Iptables target actions"); |
298 | MODULE_LICENSE("GPL"); | |
0dcffd09 | 299 | MODULE_ALIAS("act_xt"); |
1da177e4 | 300 | |
e9ce1cd3 | 301 | static int __init ipt_init_module(void) |
1da177e4 | 302 | { |
369ba567 | 303 | int ret1, ret2, err; |
568a153a | 304 | err = tcf_hashinfo_init(&ipt_hash_info, IPT_TAB_MASK); |
369ba567 WC |
305 | if (err) |
306 | return err; | |
307 | ||
0dcffd09 JHS |
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 | ||
369ba567 WC |
315 | if (ret1 < 0 && ret2 < 0) { |
316 | tcf_hashinfo_destroy(&ipt_hash_info); | |
0dcffd09 | 317 | return ret1; |
369ba567 | 318 | } else |
0dcffd09 | 319 | return 0; |
1da177e4 LT |
320 | } |
321 | ||
e9ce1cd3 | 322 | static void __exit ipt_cleanup_module(void) |
1da177e4 | 323 | { |
0dcffd09 | 324 | tcf_unregister_action(&act_xt_ops); |
1da177e4 | 325 | tcf_unregister_action(&act_ipt_ops); |
369ba567 | 326 | tcf_hashinfo_destroy(&ipt_hash_info); |
1da177e4 LT |
327 | } |
328 | ||
329 | module_init(ipt_init_module); | |
330 | module_exit(ipt_cleanup_module); |