1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/act_ipt.c iptables target interface
5 *TODO: Add other tables. For now we only support the ipv4 table targets
7 * Copyright: Jamal Hadi Salim (2002-13)
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 #include <net/tc_wrapper.h>
26 #include <linux/netfilter_ipv4/ip_tables.h>
29 static struct tc_action_ops act_ipt_ops;
30 static struct tc_action_ops act_xt_ops;
32 static int ipt_init_target(struct net *net, struct xt_entry_target *t,
33 char *table, unsigned int hook)
35 struct xt_tgchk_param par;
36 struct xt_target *target;
37 struct ipt_entry e = {};
40 target = xt_request_find_target(AF_INET, t->u.user.name,
43 return PTR_ERR(target);
45 t->u.kernel.target = target;
46 memset(&par, 0, sizeof(par));
51 par.targinfo = t->data;
52 par.hook_mask = 1 << hook;
53 par.family = NFPROTO_IPV4;
55 ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
57 module_put(t->u.kernel.target->me);
63 static void ipt_destroy_target(struct xt_entry_target *t, struct net *net)
65 struct xt_tgdtor_param par = {
66 .target = t->u.kernel.target,
68 .family = NFPROTO_IPV4,
71 if (par.target->destroy != NULL)
72 par.target->destroy(&par);
73 module_put(par.target->me);
76 static void tcf_ipt_release(struct tc_action *a)
78 struct tcf_ipt *ipt = to_ipt(a);
81 ipt_destroy_target(ipt->tcfi_t, a->idrinfo->net);
84 kfree(ipt->tcfi_tname);
87 static const struct nla_policy ipt_policy[TCA_IPT_MAX + 1] = {
88 [TCA_IPT_TABLE] = { .type = NLA_STRING, .len = IFNAMSIZ },
89 [TCA_IPT_HOOK] = NLA_POLICY_RANGE(NLA_U32, NF_INET_PRE_ROUTING,
91 [TCA_IPT_INDEX] = { .type = NLA_U32 },
92 [TCA_IPT_TARG] = { .len = sizeof(struct xt_entry_target) },
95 static int __tcf_ipt_init(struct net *net, unsigned int id, struct nlattr *nla,
96 struct nlattr *est, struct tc_action **a,
97 const struct tc_action_ops *ops,
98 struct tcf_proto *tp, u32 flags)
100 struct tc_action_net *tn = net_generic(net, id);
101 bool bind = flags & TCA_ACT_FLAGS_BIND;
102 struct nlattr *tb[TCA_IPT_MAX + 1];
104 struct xt_entry_target *td, *t;
114 err = nla_parse_nested_deprecated(tb, TCA_IPT_MAX, nla, ipt_policy,
119 if (tb[TCA_IPT_INDEX] != NULL)
120 index = nla_get_u32(tb[TCA_IPT_INDEX]);
122 err = tcf_idr_check_alloc(tn, &index, a, bind);
129 if (tb[TCA_IPT_HOOK] == NULL || tb[TCA_IPT_TARG] == NULL) {
131 tcf_idr_release(*a, bind);
133 tcf_idr_cleanup(tn, index);
137 td = (struct xt_entry_target *)nla_data(tb[TCA_IPT_TARG]);
138 if (nla_len(tb[TCA_IPT_TARG]) != td->u.target_size) {
140 tcf_idr_release(*a, bind);
142 tcf_idr_cleanup(tn, index);
147 ret = tcf_idr_create(tn, index, est, a, ops, bind,
150 tcf_idr_cleanup(tn, index);
155 if (bind)/* dont override defaults */
158 if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
159 tcf_idr_release(*a, bind);
165 hook = nla_get_u32(tb[TCA_IPT_HOOK]);
167 case NF_INET_PRE_ROUTING:
169 case NF_INET_POST_ROUTING:
175 if (tb[TCA_IPT_TABLE]) {
176 /* mangle only for now */
177 if (nla_strcmp(tb[TCA_IPT_TABLE], "mangle"))
181 tname = kstrdup("mangle", GFP_KERNEL);
182 if (unlikely(!tname))
185 t = kmemdup(td, td->u.target_size, GFP_KERNEL);
189 err = ipt_init_target(net, t, tname, hook);
195 spin_lock_bh(&ipt->tcf_lock);
196 if (ret != ACT_P_CREATED) {
197 ipt_destroy_target(ipt->tcfi_t, net);
198 kfree(ipt->tcfi_tname);
201 ipt->tcfi_tname = tname;
203 ipt->tcfi_hook = hook;
204 spin_unlock_bh(&ipt->tcf_lock);
212 tcf_idr_release(*a, bind);
216 static int tcf_ipt_init(struct net *net, struct nlattr *nla,
217 struct nlattr *est, struct tc_action **a,
218 struct tcf_proto *tp,
219 u32 flags, struct netlink_ext_ack *extack)
221 return __tcf_ipt_init(net, act_ipt_ops.net_id, nla, est,
222 a, &act_ipt_ops, tp, flags);
225 static int tcf_xt_init(struct net *net, struct nlattr *nla,
226 struct nlattr *est, struct tc_action **a,
227 struct tcf_proto *tp,
228 u32 flags, struct netlink_ext_ack *extack)
230 return __tcf_ipt_init(net, act_xt_ops.net_id, nla, est,
231 a, &act_xt_ops, tp, flags);
234 static bool tcf_ipt_act_check(struct sk_buff *skb)
236 const struct iphdr *iph;
237 unsigned int nhoff, len;
239 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
242 nhoff = skb_network_offset(skb);
244 if (iph->ihl < 5 || iph->version != 4)
247 len = skb_ip_totlen(skb);
248 if (skb->len < nhoff + len || len < (iph->ihl * 4u))
251 return pskb_may_pull(skb, iph->ihl * 4u);
254 TC_INDIRECT_SCOPE int tcf_ipt_act(struct sk_buff *skb,
255 const struct tc_action *a,
256 struct tcf_result *res)
258 char saved_cb[sizeof_field(struct sk_buff, cb)];
259 int ret = 0, result = 0;
260 struct tcf_ipt *ipt = to_ipt(a);
261 struct xt_action_param par;
262 struct nf_hook_state state = {
263 .net = dev_net(skb->dev),
265 .hook = ipt->tcfi_hook,
269 if (skb_protocol(skb, false) != htons(ETH_P_IP))
270 return TC_ACT_UNSPEC;
272 if (skb_unclone(skb, GFP_ATOMIC))
273 return TC_ACT_UNSPEC;
275 if (!tcf_ipt_act_check(skb))
276 return TC_ACT_UNSPEC;
278 if (state.hook == NF_INET_POST_ROUTING) {
280 return TC_ACT_UNSPEC;
282 state.out = skb->dev;
285 memcpy(saved_cb, skb->cb, sizeof(saved_cb));
287 spin_lock(&ipt->tcf_lock);
289 tcf_lastuse_update(&ipt->tcf_tm);
290 bstats_update(&ipt->tcf_bstats, skb);
292 /* yes, we have to worry about both in and out dev
293 * worry later - danger - this API seems to have changed
294 * from earlier kernels
297 par.target = ipt->tcfi_t->u.kernel.target;
298 par.targinfo = ipt->tcfi_t->data;
300 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
302 ret = par.target->target(skb, &par);
309 result = TC_ACT_SHOT;
310 ipt->tcf_qstats.drops++;
313 result = TC_ACT_PIPE;
316 net_notice_ratelimited("tc filter: Bogus netfilter code %d assume ACCEPT\n",
321 spin_unlock(&ipt->tcf_lock);
323 memcpy(skb->cb, saved_cb, sizeof(skb->cb));
329 static int tcf_ipt_dump(struct sk_buff *skb, struct tc_action *a, int bind,
332 unsigned char *b = skb_tail_pointer(skb);
333 struct tcf_ipt *ipt = to_ipt(a);
334 struct xt_entry_target *t;
338 /* for simple targets kernel size == user size
339 * user name = target name
340 * for foolproof you need to not assume this
343 spin_lock_bh(&ipt->tcf_lock);
344 t = kmemdup(ipt->tcfi_t, ipt->tcfi_t->u.user.target_size, GFP_ATOMIC);
346 goto nla_put_failure;
348 c.bindcnt = atomic_read(&ipt->tcf_bindcnt) - bind;
349 c.refcnt = refcount_read(&ipt->tcf_refcnt) - ref;
350 strcpy(t->u.user.name, ipt->tcfi_t->u.kernel.target->name);
352 if (nla_put(skb, TCA_IPT_TARG, ipt->tcfi_t->u.user.target_size, t) ||
353 nla_put_u32(skb, TCA_IPT_INDEX, ipt->tcf_index) ||
354 nla_put_u32(skb, TCA_IPT_HOOK, ipt->tcfi_hook) ||
355 nla_put(skb, TCA_IPT_CNT, sizeof(struct tc_cnt), &c) ||
356 nla_put_string(skb, TCA_IPT_TABLE, ipt->tcfi_tname))
357 goto nla_put_failure;
359 tcf_tm_dump(&tm, &ipt->tcf_tm);
360 if (nla_put_64bit(skb, TCA_IPT_TM, sizeof(tm), &tm, TCA_IPT_PAD))
361 goto nla_put_failure;
363 spin_unlock_bh(&ipt->tcf_lock);
368 spin_unlock_bh(&ipt->tcf_lock);
374 static struct tc_action_ops act_ipt_ops = {
377 .owner = THIS_MODULE,
379 .dump = tcf_ipt_dump,
380 .cleanup = tcf_ipt_release,
381 .init = tcf_ipt_init,
382 .size = sizeof(struct tcf_ipt),
385 static __net_init int ipt_init_net(struct net *net)
387 struct tc_action_net *tn = net_generic(net, act_ipt_ops.net_id);
389 return tc_action_net_init(net, tn, &act_ipt_ops);
392 static void __net_exit ipt_exit_net(struct list_head *net_list)
394 tc_action_net_exit(net_list, act_ipt_ops.net_id);
397 static struct pernet_operations ipt_net_ops = {
398 .init = ipt_init_net,
399 .exit_batch = ipt_exit_net,
400 .id = &act_ipt_ops.net_id,
401 .size = sizeof(struct tc_action_net),
404 static struct tc_action_ops act_xt_ops = {
407 .owner = THIS_MODULE,
409 .dump = tcf_ipt_dump,
410 .cleanup = tcf_ipt_release,
412 .size = sizeof(struct tcf_ipt),
415 static __net_init int xt_init_net(struct net *net)
417 struct tc_action_net *tn = net_generic(net, act_xt_ops.net_id);
419 return tc_action_net_init(net, tn, &act_xt_ops);
422 static void __net_exit xt_exit_net(struct list_head *net_list)
424 tc_action_net_exit(net_list, act_xt_ops.net_id);
427 static struct pernet_operations xt_net_ops = {
429 .exit_batch = xt_exit_net,
430 .id = &act_xt_ops.net_id,
431 .size = sizeof(struct tc_action_net),
434 MODULE_AUTHOR("Jamal Hadi Salim(2002-13)");
435 MODULE_DESCRIPTION("Iptables target actions");
436 MODULE_LICENSE("GPL");
437 MODULE_ALIAS("act_xt");
439 static int __init ipt_init_module(void)
443 ret1 = tcf_register_action(&act_xt_ops, &xt_net_ops);
445 pr_err("Failed to load xt action\n");
447 ret2 = tcf_register_action(&act_ipt_ops, &ipt_net_ops);
449 pr_err("Failed to load ipt action\n");
451 if (ret1 < 0 && ret2 < 0) {
457 static void __exit ipt_cleanup_module(void)
459 tcf_unregister_action(&act_ipt_ops, &ipt_net_ops);
460 tcf_unregister_action(&act_xt_ops, &xt_net_ops);
463 module_init(ipt_init_module);
464 module_exit(ipt_cleanup_module);