hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/net/sched/act_nat.c
....@@ -1,12 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Stateless NAT actions
34 *
45 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
5
- *
6
- * This program is free software; you can redistribute it and/or modify it
7
- * under the terms of the GNU General Public License as published by the Free
8
- * Software Foundation; either version 2 of the License, or (at your option)
9
- * any later version.
106 */
117
128 #include <linux/errno.h>
....@@ -21,6 +17,7 @@
2117 #include <linux/string.h>
2218 #include <linux/tc_act/tc_nat.h>
2319 #include <net/act_api.h>
20
+#include <net/pkt_cls.h>
2421 #include <net/icmp.h>
2522 #include <net/ip.h>
2623 #include <net/netlink.h>
....@@ -38,10 +35,12 @@
3835
3936 static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
4037 struct tc_action **a, int ovr, int bind,
41
- bool rtnl_held, struct netlink_ext_ack *extack)
38
+ bool rtnl_held, struct tcf_proto *tp,
39
+ u32 flags, struct netlink_ext_ack *extack)
4240 {
4341 struct tc_action_net *tn = net_generic(net, nat_net_id);
4442 struct nlattr *tb[TCA_NAT_MAX + 1];
43
+ struct tcf_chain *goto_ch = NULL;
4544 struct tc_nat *parm;
4645 int ret = 0, err;
4746 struct tcf_nat *p;
....@@ -50,7 +49,8 @@
5049 if (nla == NULL)
5150 return -EINVAL;
5251
53
- err = nla_parse_nested(tb, TCA_NAT_MAX, nla, nat_policy, NULL);
52
+ err = nla_parse_nested_deprecated(tb, TCA_NAT_MAX, nla, nat_policy,
53
+ NULL);
5454 if (err < 0)
5555 return err;
5656
....@@ -61,7 +61,7 @@
6161 err = tcf_idr_check_alloc(tn, &index, a, bind);
6262 if (!err) {
6363 ret = tcf_idr_create(tn, index, est, a,
64
- &act_nat_ops, bind, false);
64
+ &act_nat_ops, bind, false, flags);
6565 if (ret) {
6666 tcf_idr_cleanup(tn, index);
6767 return ret;
....@@ -77,6 +77,9 @@
7777 } else {
7878 return err;
7979 }
80
+ err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
81
+ if (err < 0)
82
+ goto release_idr;
8083 p = to_tcf_nat(*a);
8184
8285 spin_lock_bh(&p->tcf_lock);
....@@ -85,13 +88,15 @@
8588 p->mask = parm->mask;
8689 p->flags = parm->flags;
8790
88
- p->tcf_action = parm->action;
91
+ goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
8992 spin_unlock_bh(&p->tcf_lock);
90
-
91
- if (ret == ACT_P_CREATED)
92
- tcf_idr_insert(tn, *a);
93
+ if (goto_ch)
94
+ tcf_chain_put_by_act(goto_ch);
9395
9496 return ret;
97
+release_idr:
98
+ tcf_idr_release(*a, bind);
99
+ return err;
95100 }
96101
97102 static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a,
....@@ -198,9 +203,7 @@
198203
199204 icmph = (void *)(skb_network_header(skb) + ihl);
200205
201
- if ((icmph->type != ICMP_DEST_UNREACH) &&
202
- (icmph->type != ICMP_TIME_EXCEEDED) &&
203
- (icmph->type != ICMP_PARAMETERPROB))
206
+ if (!icmp_is_err(icmph->type))
204207 break;
205208
206209 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
....@@ -257,17 +260,18 @@
257260 unsigned char *b = skb_tail_pointer(skb);
258261 struct tcf_nat *p = to_tcf_nat(a);
259262 struct tc_nat opt = {
260
- .old_addr = p->old_addr,
261
- .new_addr = p->new_addr,
262
- .mask = p->mask,
263
- .flags = p->flags,
264
-
265263 .index = p->tcf_index,
266
- .action = p->tcf_action,
267264 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
268265 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
269266 };
270267 struct tcf_t t;
268
+
269
+ spin_lock_bh(&p->tcf_lock);
270
+ opt.old_addr = p->old_addr;
271
+ opt.new_addr = p->new_addr;
272
+ opt.mask = p->mask;
273
+ opt.flags = p->flags;
274
+ opt.action = p->tcf_action;
271275
272276 if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
273277 goto nla_put_failure;
....@@ -275,10 +279,12 @@
275279 tcf_tm_dump(&t, &p->tcf_tm);
276280 if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
277281 goto nla_put_failure;
282
+ spin_unlock_bh(&p->tcf_lock);
278283
279284 return skb->len;
280285
281286 nla_put_failure:
287
+ spin_unlock_bh(&p->tcf_lock);
282288 nlmsg_trim(skb, b);
283289 return -1;
284290 }
....@@ -293,8 +299,7 @@
293299 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
294300 }
295301
296
-static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index,
297
- struct netlink_ext_ack *extack)
302
+static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index)
298303 {
299304 struct tc_action_net *tn = net_generic(net, nat_net_id);
300305
....@@ -303,7 +308,7 @@
303308
304309 static struct tc_action_ops act_nat_ops = {
305310 .kind = "nat",
306
- .type = TCA_ACT_NAT,
311
+ .id = TCA_ID_NAT,
307312 .owner = THIS_MODULE,
308313 .act = tcf_nat_act,
309314 .dump = tcf_nat_dump,