blob: 7b858c11b1b5ffd34f57706ee5631229b45f65a3 [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 <[email protected]>
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,
39 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 Berg8cb081742019-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,
Chris Mi65a206c2017-08-30 02:31:59 -040064 &act_nat_ops, bind, false);
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
96 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -040097 tcf_idr_insert(tn, *a);
Herbert Xub4219952007-09-27 12:48:05 -070098
99 return ret;
Davide Caratti1e45d042019-03-20 15:00:06 +0100100release_idr:
101 tcf_idr_release(*a, bind);
102 return err;
Herbert Xub4219952007-09-27 12:48:05 -0700103}
104
Jamal Hadi Salim03905142018-08-12 09:34:54 -0400105static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a,
106 struct tcf_result *res)
Herbert Xub4219952007-09-27 12:48:05 -0700107{
WANG Conga85a9702016-07-25 16:09:41 -0700108 struct tcf_nat *p = to_tcf_nat(a);
Herbert Xub4219952007-09-27 12:48:05 -0700109 struct iphdr *iph;
110 __be32 old_addr;
111 __be32 new_addr;
112 __be32 mask;
113 __be32 addr;
114 int egress;
115 int action;
116 int ihl;
Changli Gao36d12692010-08-03 17:39:18 +0000117 int noff;
Herbert Xub4219952007-09-27 12:48:05 -0700118
119 spin_lock(&p->tcf_lock);
120
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400121 tcf_lastuse_update(&p->tcf_tm);
Herbert Xub4219952007-09-27 12:48:05 -0700122 old_addr = p->old_addr;
123 new_addr = p->new_addr;
124 mask = p->mask;
125 egress = p->flags & TCA_NAT_FLAG_EGRESS;
126 action = p->tcf_action;
127
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000128 bstats_update(&p->tcf_bstats, skb);
Herbert Xub4219952007-09-27 12:48:05 -0700129
130 spin_unlock(&p->tcf_lock);
131
132 if (unlikely(action == TC_ACT_SHOT))
133 goto drop;
134
Changli Gao36d12692010-08-03 17:39:18 +0000135 noff = skb_network_offset(skb);
136 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700137 goto drop;
138
139 iph = ip_hdr(skb);
140
141 if (egress)
142 addr = iph->saddr;
143 else
144 addr = iph->daddr;
145
146 if (!((old_addr ^ addr) & mask)) {
Daniel Borkmann36976492016-02-19 23:05:25 +0100147 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700148 goto drop;
149
150 new_addr &= mask;
151 new_addr |= addr & ~mask;
152
153 /* Rewrite IP header */
154 iph = ip_hdr(skb);
155 if (egress)
156 iph->saddr = new_addr;
157 else
158 iph->daddr = new_addr;
159
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100160 csum_replace4(&iph->check, addr, new_addr);
Changli Gao33c29dd2010-05-29 14:26:59 +0000161 } else if ((iph->frag_off & htons(IP_OFFSET)) ||
162 iph->protocol != IPPROTO_ICMP) {
163 goto out;
Herbert Xub4219952007-09-27 12:48:05 -0700164 }
165
166 ihl = iph->ihl * 4;
167
168 /* It would be nice to share code with stateful NAT. */
169 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
170 case IPPROTO_TCP:
171 {
172 struct tcphdr *tcph;
173
Changli Gao36d12692010-08-03 17:39:18 +0000174 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100175 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700176 goto drop;
177
178 tcph = (void *)(skb_network_header(skb) + ihl);
Tom Herbert4b048d62015-08-17 13:42:25 -0700179 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
180 true);
Herbert Xub4219952007-09-27 12:48:05 -0700181 break;
182 }
183 case IPPROTO_UDP:
184 {
185 struct udphdr *udph;
186
Changli Gao36d12692010-08-03 17:39:18 +0000187 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100188 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700189 goto drop;
190
191 udph = (void *)(skb_network_header(skb) + ihl);
192 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100193 inet_proto_csum_replace4(&udph->check, skb, addr,
Tom Herbert4b048d62015-08-17 13:42:25 -0700194 new_addr, true);
Herbert Xub4219952007-09-27 12:48:05 -0700195 if (!udph->check)
196 udph->check = CSUM_MANGLED_0;
197 }
198 break;
199 }
200 case IPPROTO_ICMP:
201 {
202 struct icmphdr *icmph;
203
Changli Gao36d12692010-08-03 17:39:18 +0000204 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700205 goto drop;
206
207 icmph = (void *)(skb_network_header(skb) + ihl);
208
209 if ((icmph->type != ICMP_DEST_UNREACH) &&
210 (icmph->type != ICMP_TIME_EXCEEDED) &&
211 (icmph->type != ICMP_PARAMETERPROB))
212 break;
213
Changli Gao36d12692010-08-03 17:39:18 +0000214 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
215 noff))
Changli Gao70c2efa2010-07-09 15:33:25 +0000216 goto drop;
217
Changli Gao072d79a2010-07-29 13:41:46 +0000218 icmph = (void *)(skb_network_header(skb) + ihl);
Herbert Xub4219952007-09-27 12:48:05 -0700219 iph = (void *)(icmph + 1);
220 if (egress)
221 addr = iph->daddr;
222 else
223 addr = iph->saddr;
224
225 if ((old_addr ^ addr) & mask)
226 break;
227
Daniel Borkmann36976492016-02-19 23:05:25 +0100228 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
229 sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700230 goto drop;
231
232 icmph = (void *)(skb_network_header(skb) + ihl);
233 iph = (void *)(icmph + 1);
234
235 new_addr &= mask;
236 new_addr |= addr & ~mask;
237
238 /* XXX Fix up the inner checksums. */
239 if (egress)
240 iph->daddr = new_addr;
241 else
242 iph->saddr = new_addr;
243
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100244 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
Tom Herbert4b048d62015-08-17 13:42:25 -0700245 false);
Herbert Xub4219952007-09-27 12:48:05 -0700246 break;
247 }
248 default:
249 break;
250 }
251
Changli Gao33c29dd2010-05-29 14:26:59 +0000252out:
Herbert Xub4219952007-09-27 12:48:05 -0700253 return action;
254
255drop:
256 spin_lock(&p->tcf_lock);
257 p->tcf_qstats.drops++;
258 spin_unlock(&p->tcf_lock);
259 return TC_ACT_SHOT;
260}
261
262static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
263 int bind, int ref)
264{
265 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700266 struct tcf_nat *p = to_tcf_nat(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000267 struct tc_nat opt = {
Eric Dumazet1c40be12010-08-16 20:04:22 +0000268 .index = p->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300269 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
270 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000271 };
Herbert Xub4219952007-09-27 12:48:05 -0700272 struct tcf_t t;
Herbert Xub4219952007-09-27 12:48:05 -0700273
Vlad Buslovf20a4d02018-09-03 10:09:20 +0300274 spin_lock_bh(&p->tcf_lock);
275 opt.old_addr = p->old_addr;
276 opt.new_addr = p->new_addr;
277 opt.mask = p->mask;
278 opt.flags = p->flags;
279 opt.action = p->tcf_action;
280
David S. Miller1b34ec42012-03-29 05:11:39 -0400281 if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
282 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400283
284 tcf_tm_dump(&t, &p->tcf_tm);
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200285 if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400286 goto nla_put_failure;
Vlad Buslovf20a4d02018-09-03 10:09:20 +0300287 spin_unlock_bh(&p->tcf_lock);
Herbert Xub4219952007-09-27 12:48:05 -0700288
Herbert Xub4219952007-09-27 12:48:05 -0700289 return skb->len;
290
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800291nla_put_failure:
Vlad Buslovf20a4d02018-09-03 10:09:20 +0300292 spin_unlock_bh(&p->tcf_lock);
Herbert Xub4219952007-09-27 12:48:05 -0700293 nlmsg_trim(skb, b);
Herbert Xub4219952007-09-27 12:48:05 -0700294 return -1;
295}
296
WANG Congddf97cc2016-02-22 15:57:53 -0800297static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
298 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500299 const struct tc_action_ops *ops,
300 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800301{
302 struct tc_action_net *tn = net_generic(net, nat_net_id);
303
Alexander Aringb3620142018-02-15 10:54:59 -0500304 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800305}
306
Cong Wangf061b482018-08-29 10:15:35 -0700307static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800308{
309 struct tc_action_net *tn = net_generic(net, nat_net_id);
310
Chris Mi65a206c2017-08-30 02:31:59 -0400311 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800312}
313
Herbert Xub4219952007-09-27 12:48:05 -0700314static struct tc_action_ops act_nat_ops = {
315 .kind = "nat",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200316 .id = TCA_ID_NAT,
Herbert Xub4219952007-09-27 12:48:05 -0700317 .owner = THIS_MODULE,
Jamal Hadi Salim03905142018-08-12 09:34:54 -0400318 .act = tcf_nat_act,
Herbert Xub4219952007-09-27 12:48:05 -0700319 .dump = tcf_nat_dump,
Herbert Xub4219952007-09-27 12:48:05 -0700320 .init = tcf_nat_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800321 .walk = tcf_nat_walker,
322 .lookup = tcf_nat_search,
WANG Conga85a9702016-07-25 16:09:41 -0700323 .size = sizeof(struct tcf_nat),
WANG Congddf97cc2016-02-22 15:57:53 -0800324};
325
326static __net_init int nat_init_net(struct net *net)
327{
328 struct tc_action_net *tn = net_generic(net, nat_net_id);
329
Cong Wangc7e460c2017-11-06 13:47:18 -0800330 return tc_action_net_init(tn, &act_nat_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800331}
332
Cong Wang039af9c2017-12-11 15:35:03 -0800333static void __net_exit nat_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800334{
Cong Wang039af9c2017-12-11 15:35:03 -0800335 tc_action_net_exit(net_list, nat_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800336}
337
338static struct pernet_operations nat_net_ops = {
339 .init = nat_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800340 .exit_batch = nat_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800341 .id = &nat_net_id,
342 .size = sizeof(struct tc_action_net),
Herbert Xub4219952007-09-27 12:48:05 -0700343};
344
345MODULE_DESCRIPTION("Stateless NAT actions");
346MODULE_LICENSE("GPL");
347
348static int __init nat_init_module(void)
349{
WANG Congddf97cc2016-02-22 15:57:53 -0800350 return tcf_register_action(&act_nat_ops, &nat_net_ops);
Herbert Xub4219952007-09-27 12:48:05 -0700351}
352
353static void __exit nat_cleanup_module(void)
354{
WANG Congddf97cc2016-02-22 15:57:53 -0800355 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
Herbert Xub4219952007-09-27 12:48:05 -0700356}
357
358module_init(nat_init_module);
359module_exit(nat_cleanup_module);