blob: 1ebd2a86d980fc631a5f970543993f37aebdf3b1 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xub4219952007-09-27 12:48:05 -07002/*
3 * Stateless NAT actions
4 *
5 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xub4219952007-09-27 12:48:05 -07006 */
7
8#include <linux/errno.h>
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/netfilter.h>
13#include <linux/rtnetlink.h>
14#include <linux/skbuff.h>
15#include <linux/slab.h>
16#include <linux/spinlock.h>
17#include <linux/string.h>
18#include <linux/tc_act/tc_nat.h>
19#include <net/act_api.h>
Davide Caratti1e45d042019-03-20 15:00:06 +010020#include <net/pkt_cls.h>
Herbert Xub4219952007-09-27 12:48:05 -070021#include <net/icmp.h>
22#include <net/ip.h>
23#include <net/netlink.h>
24#include <net/tc_act/tc_nat.h>
25#include <net/tcp.h>
26#include <net/udp.h>
27
28
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030029static unsigned int nat_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070030static struct tc_action_ops act_nat_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080031
Patrick McHardy53b2bf32008-01-23 20:36:30 -080032static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
33 [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) },
34};
35
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000036static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
Alexander Aring589dad62018-02-15 10:54:56 -050037 struct tc_action **a, int ovr, int bind,
Davide Caratti85d09662019-03-20 14:59:59 +010038 bool rtnl_held, struct tcf_proto *tp,
Vlad Buslovabbb0d32019-10-30 16:09:05 +020039 u32 flags, struct netlink_ext_ack *extack)
Herbert Xub4219952007-09-27 12:48:05 -070040{
WANG Congddf97cc2016-02-22 15:57:53 -080041 struct tc_action_net *tn = net_generic(net, nat_net_id);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080042 struct nlattr *tb[TCA_NAT_MAX + 1];
Davide Caratti1e45d042019-03-20 15:00:06 +010043 struct tcf_chain *goto_ch = NULL;
Herbert Xub4219952007-09-27 12:48:05 -070044 struct tc_nat *parm;
Patrick McHardycee63722008-01-23 20:33:32 -080045 int ret = 0, err;
Herbert Xub4219952007-09-27 12:48:05 -070046 struct tcf_nat *p;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000047 u32 index;
Herbert Xub4219952007-09-27 12:48:05 -070048
Patrick McHardycee63722008-01-23 20:33:32 -080049 if (nla == NULL)
Herbert Xub4219952007-09-27 12:48:05 -070050 return -EINVAL;
51
Johannes Berg8cb08172019-04-26 14:07:28 +020052 err = nla_parse_nested_deprecated(tb, TCA_NAT_MAX, nla, nat_policy,
53 NULL);
Patrick McHardycee63722008-01-23 20:33:32 -080054 if (err < 0)
55 return err;
56
Patrick McHardy53b2bf32008-01-23 20:36:30 -080057 if (tb[TCA_NAT_PARMS] == NULL)
Herbert Xub4219952007-09-27 12:48:05 -070058 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080059 parm = nla_data(tb[TCA_NAT_PARMS]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000060 index = parm->index;
61 err = tcf_idr_check_alloc(tn, &index, a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030062 if (!err) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000063 ret = tcf_idr_create(tn, index, est, a,
Vlad Buslove3822672019-10-30 16:09:06 +020064 &act_nat_ops, bind, false, 0);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030065 if (ret) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000066 tcf_idr_cleanup(tn, index);
WANG Cong86062032014-02-11 17:07:31 -080067 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030068 }
Herbert Xub4219952007-09-27 12:48:05 -070069 ret = ACT_P_CREATED;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030070 } else if (err > 0) {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050071 if (bind)
72 return 0;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030073 if (!ovr) {
74 tcf_idr_release(*a, bind);
Herbert Xub4219952007-09-27 12:48:05 -070075 return -EEXIST;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030076 }
Vlad Buslov0190c1d2018-07-05 17:24:32 +030077 } else {
78 return err;
Herbert Xub4219952007-09-27 12:48:05 -070079 }
Davide Caratti1e45d042019-03-20 15:00:06 +010080 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
81 if (err < 0)
82 goto release_idr;
WANG Conga85a9702016-07-25 16:09:41 -070083 p = to_tcf_nat(*a);
Herbert Xub4219952007-09-27 12:48:05 -070084
85 spin_lock_bh(&p->tcf_lock);
86 p->old_addr = parm->old_addr;
87 p->new_addr = parm->new_addr;
88 p->mask = parm->mask;
89 p->flags = parm->flags;
90
Davide Caratti1e45d042019-03-20 15:00:06 +010091 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Herbert Xub4219952007-09-27 12:48:05 -070092 spin_unlock_bh(&p->tcf_lock);
Davide Caratti1e45d042019-03-20 15:00:06 +010093 if (goto_ch)
94 tcf_chain_put_by_act(goto_ch);
Herbert Xub4219952007-09-27 12:48:05 -070095
Herbert Xub4219952007-09-27 12:48:05 -070096 return ret;
Davide Caratti1e45d042019-03-20 15:00:06 +010097release_idr:
98 tcf_idr_release(*a, bind);
99 return err;
Herbert Xub4219952007-09-27 12:48:05 -0700100}
101
Jamal Hadi Salim03905142018-08-12 09:34:54 -0400102static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a,
103 struct tcf_result *res)
Herbert Xub4219952007-09-27 12:48:05 -0700104{
WANG Conga85a9702016-07-25 16:09:41 -0700105 struct tcf_nat *p = to_tcf_nat(a);
Herbert Xub4219952007-09-27 12:48:05 -0700106 struct iphdr *iph;
107 __be32 old_addr;
108 __be32 new_addr;
109 __be32 mask;
110 __be32 addr;
111 int egress;
112 int action;
113 int ihl;
Changli Gao36d12692010-08-03 17:39:18 +0000114 int noff;
Herbert Xub4219952007-09-27 12:48:05 -0700115
116 spin_lock(&p->tcf_lock);
117
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400118 tcf_lastuse_update(&p->tcf_tm);
Herbert Xub4219952007-09-27 12:48:05 -0700119 old_addr = p->old_addr;
120 new_addr = p->new_addr;
121 mask = p->mask;
122 egress = p->flags & TCA_NAT_FLAG_EGRESS;
123 action = p->tcf_action;
124
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000125 bstats_update(&p->tcf_bstats, skb);
Herbert Xub4219952007-09-27 12:48:05 -0700126
127 spin_unlock(&p->tcf_lock);
128
129 if (unlikely(action == TC_ACT_SHOT))
130 goto drop;
131
Changli Gao36d12692010-08-03 17:39:18 +0000132 noff = skb_network_offset(skb);
133 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700134 goto drop;
135
136 iph = ip_hdr(skb);
137
138 if (egress)
139 addr = iph->saddr;
140 else
141 addr = iph->daddr;
142
143 if (!((old_addr ^ addr) & mask)) {
Daniel Borkmann36976492016-02-19 23:05:25 +0100144 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700145 goto drop;
146
147 new_addr &= mask;
148 new_addr |= addr & ~mask;
149
150 /* Rewrite IP header */
151 iph = ip_hdr(skb);
152 if (egress)
153 iph->saddr = new_addr;
154 else
155 iph->daddr = new_addr;
156
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100157 csum_replace4(&iph->check, addr, new_addr);
Changli Gao33c29dd2010-05-29 14:26:59 +0000158 } else if ((iph->frag_off & htons(IP_OFFSET)) ||
159 iph->protocol != IPPROTO_ICMP) {
160 goto out;
Herbert Xub4219952007-09-27 12:48:05 -0700161 }
162
163 ihl = iph->ihl * 4;
164
165 /* It would be nice to share code with stateful NAT. */
166 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
167 case IPPROTO_TCP:
168 {
169 struct tcphdr *tcph;
170
Changli Gao36d12692010-08-03 17:39:18 +0000171 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100172 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700173 goto drop;
174
175 tcph = (void *)(skb_network_header(skb) + ihl);
Tom Herbert4b048d62015-08-17 13:42:25 -0700176 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
177 true);
Herbert Xub4219952007-09-27 12:48:05 -0700178 break;
179 }
180 case IPPROTO_UDP:
181 {
182 struct udphdr *udph;
183
Changli Gao36d12692010-08-03 17:39:18 +0000184 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100185 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700186 goto drop;
187
188 udph = (void *)(skb_network_header(skb) + ihl);
189 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100190 inet_proto_csum_replace4(&udph->check, skb, addr,
Tom Herbert4b048d62015-08-17 13:42:25 -0700191 new_addr, true);
Herbert Xub4219952007-09-27 12:48:05 -0700192 if (!udph->check)
193 udph->check = CSUM_MANGLED_0;
194 }
195 break;
196 }
197 case IPPROTO_ICMP:
198 {
199 struct icmphdr *icmph;
200
Changli Gao36d12692010-08-03 17:39:18 +0000201 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700202 goto drop;
203
204 icmph = (void *)(skb_network_header(skb) + ihl);
205
Matteo Croce54074f12019-11-02 01:12:04 +0100206 if (!icmp_is_err(icmph->type))
Herbert Xub4219952007-09-27 12:48:05 -0700207 break;
208
Changli Gao36d12692010-08-03 17:39:18 +0000209 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
210 noff))
Changli Gao70c2efa2010-07-09 15:33:25 +0000211 goto drop;
212
Changli Gao072d79a2010-07-29 13:41:46 +0000213 icmph = (void *)(skb_network_header(skb) + ihl);
Herbert Xub4219952007-09-27 12:48:05 -0700214 iph = (void *)(icmph + 1);
215 if (egress)
216 addr = iph->daddr;
217 else
218 addr = iph->saddr;
219
220 if ((old_addr ^ addr) & mask)
221 break;
222
Daniel Borkmann36976492016-02-19 23:05:25 +0100223 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
224 sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700225 goto drop;
226
227 icmph = (void *)(skb_network_header(skb) + ihl);
228 iph = (void *)(icmph + 1);
229
230 new_addr &= mask;
231 new_addr |= addr & ~mask;
232
233 /* XXX Fix up the inner checksums. */
234 if (egress)
235 iph->daddr = new_addr;
236 else
237 iph->saddr = new_addr;
238
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100239 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
Tom Herbert4b048d62015-08-17 13:42:25 -0700240 false);
Herbert Xub4219952007-09-27 12:48:05 -0700241 break;
242 }
243 default:
244 break;
245 }
246
Changli Gao33c29dd2010-05-29 14:26:59 +0000247out:
Herbert Xub4219952007-09-27 12:48:05 -0700248 return action;
249
250drop:
251 spin_lock(&p->tcf_lock);
252 p->tcf_qstats.drops++;
253 spin_unlock(&p->tcf_lock);
254 return TC_ACT_SHOT;
255}
256
257static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
258 int bind, int ref)
259{
260 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700261 struct tcf_nat *p = to_tcf_nat(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000262 struct tc_nat opt = {
Eric Dumazet1c40be12010-08-16 20:04:22 +0000263 .index = p->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300264 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
265 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000266 };
Herbert Xub4219952007-09-27 12:48:05 -0700267 struct tcf_t t;
Herbert Xub4219952007-09-27 12:48:05 -0700268
Vlad Buslovf20a4d02018-09-03 10:09:20 +0300269 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;
275
David S. Miller1b34ec42012-03-29 05:11:39 -0400276 if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
277 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400278
279 tcf_tm_dump(&t, &p->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200280 if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400281 goto nla_put_failure;
Vlad Buslovf20a4d02018-09-03 10:09:20 +0300282 spin_unlock_bh(&p->tcf_lock);
Herbert Xub4219952007-09-27 12:48:05 -0700283
Herbert Xub4219952007-09-27 12:48:05 -0700284 return skb->len;
285
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800286nla_put_failure:
Vlad Buslovf20a4d02018-09-03 10:09:20 +0300287 spin_unlock_bh(&p->tcf_lock);
Herbert Xub4219952007-09-27 12:48:05 -0700288 nlmsg_trim(skb, b);
Herbert Xub4219952007-09-27 12:48:05 -0700289 return -1;
290}
291
WANG Congddf97cc2016-02-22 15:57:53 -0800292static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
293 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500294 const struct tc_action_ops *ops,
295 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800296{
297 struct tc_action_net *tn = net_generic(net, nat_net_id);
298
Alexander Aringb3620142018-02-15 10:54:59 -0500299 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800300}
301
Cong Wangf061b482018-08-29 10:15:35 -0700302static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800303{
304 struct tc_action_net *tn = net_generic(net, nat_net_id);
305
Chris Mi65a206c2017-08-30 02:31:59 -0400306 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800307}
308
Herbert Xub4219952007-09-27 12:48:05 -0700309static struct tc_action_ops act_nat_ops = {
310 .kind = "nat",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200311 .id = TCA_ID_NAT,
Herbert Xub4219952007-09-27 12:48:05 -0700312 .owner = THIS_MODULE,
Jamal Hadi Salim03905142018-08-12 09:34:54 -0400313 .act = tcf_nat_act,
Herbert Xub4219952007-09-27 12:48:05 -0700314 .dump = tcf_nat_dump,
Herbert Xub4219952007-09-27 12:48:05 -0700315 .init = tcf_nat_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800316 .walk = tcf_nat_walker,
317 .lookup = tcf_nat_search,
WANG Conga85a9702016-07-25 16:09:41 -0700318 .size = sizeof(struct tcf_nat),
WANG Congddf97cc2016-02-22 15:57:53 -0800319};
320
321static __net_init int nat_init_net(struct net *net)
322{
323 struct tc_action_net *tn = net_generic(net, nat_net_id);
324
Cong Wang981471b2019-08-25 10:01:32 -0700325 return tc_action_net_init(net, tn, &act_nat_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800326}
327
Cong Wang039af9c2017-12-11 15:35:03 -0800328static void __net_exit nat_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800329{
Cong Wang039af9c2017-12-11 15:35:03 -0800330 tc_action_net_exit(net_list, nat_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800331}
332
333static struct pernet_operations nat_net_ops = {
334 .init = nat_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800335 .exit_batch = nat_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800336 .id = &nat_net_id,
337 .size = sizeof(struct tc_action_net),
Herbert Xub4219952007-09-27 12:48:05 -0700338};
339
340MODULE_DESCRIPTION("Stateless NAT actions");
341MODULE_LICENSE("GPL");
342
343static int __init nat_init_module(void)
344{
WANG Congddf97cc2016-02-22 15:57:53 -0800345 return tcf_register_action(&act_nat_ops, &nat_net_ops);
Herbert Xub4219952007-09-27 12:48:05 -0700346}
347
348static void __exit nat_cleanup_module(void)
349{
WANG Congddf97cc2016-02-22 15:57:53 -0800350 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
Herbert Xub4219952007-09-27 12:48:05 -0700351}
352
353module_init(nat_init_module);
354module_exit(nat_cleanup_module);