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