blob: 543eab9193f17ca94756bff060fee937078a1e21 [file] [log] [blame]
Herbert Xub4219952007-09-27 12:48:05 -07001/*
2 * Stateless NAT actions
3 *
4 * Copyright (c) 2007 Herbert Xu <[email protected]>
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.
10 */
11
12#include <linux/errno.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/netfilter.h>
17#include <linux/rtnetlink.h>
18#include <linux/skbuff.h>
19#include <linux/slab.h>
20#include <linux/spinlock.h>
21#include <linux/string.h>
22#include <linux/tc_act/tc_nat.h>
23#include <net/act_api.h>
24#include <net/icmp.h>
25#include <net/ip.h>
26#include <net/netlink.h>
27#include <net/tc_act/tc_nat.h>
28#include <net/tcp.h>
29#include <net/udp.h>
30
31
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030032static unsigned int nat_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070033static struct tc_action_ops act_nat_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080034
Patrick McHardy53b2bf32008-01-23 20:36:30 -080035static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
36 [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) },
37};
38
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000039static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
Alexander Aring589dad62018-02-15 10:54:56 -050040 struct tc_action **a, int ovr, int bind,
Vlad Buslov789871b2018-07-05 17:24:25 +030041 bool rtnl_held, struct netlink_ext_ack *extack)
Herbert Xub4219952007-09-27 12:48:05 -070042{
WANG Congddf97cc2016-02-22 15:57:53 -080043 struct tc_action_net *tn = net_generic(net, nat_net_id);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080044 struct nlattr *tb[TCA_NAT_MAX + 1];
Herbert Xub4219952007-09-27 12:48:05 -070045 struct tc_nat *parm;
Patrick McHardycee63722008-01-23 20:33:32 -080046 int ret = 0, err;
Herbert Xub4219952007-09-27 12:48:05 -070047 struct tcf_nat *p;
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 Bergfceb6432017-04-12 14:34:07 +020052 err = nla_parse_nested(tb, TCA_NAT_MAX, nla, nat_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -080053 if (err < 0)
54 return err;
55
Patrick McHardy53b2bf32008-01-23 20:36:30 -080056 if (tb[TCA_NAT_PARMS] == NULL)
Herbert Xub4219952007-09-27 12:48:05 -070057 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080058 parm = nla_data(tb[TCA_NAT_PARMS]);
Herbert Xub4219952007-09-27 12:48:05 -070059
Vlad Buslov0190c1d2018-07-05 17:24:32 +030060 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
61 if (!err) {
Chris Mi65a206c2017-08-30 02:31:59 -040062 ret = tcf_idr_create(tn, parm->index, est, a,
63 &act_nat_ops, bind, false);
Vlad Buslov0190c1d2018-07-05 17:24:32 +030064 if (ret) {
65 tcf_idr_cleanup(tn, parm->index);
WANG Cong86062032014-02-11 17:07:31 -080066 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030067 }
Herbert Xub4219952007-09-27 12:48:05 -070068 ret = ACT_P_CREATED;
Vlad Buslov0190c1d2018-07-05 17:24:32 +030069 } else if (err > 0) {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050070 if (bind)
71 return 0;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030072 if (!ovr) {
73 tcf_idr_release(*a, bind);
Herbert Xub4219952007-09-27 12:48:05 -070074 return -EEXIST;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +030075 }
Vlad Buslov0190c1d2018-07-05 17:24:32 +030076 } else {
77 return err;
Herbert Xub4219952007-09-27 12:48:05 -070078 }
WANG Conga85a9702016-07-25 16:09:41 -070079 p = to_tcf_nat(*a);
Herbert Xub4219952007-09-27 12:48:05 -070080
81 spin_lock_bh(&p->tcf_lock);
82 p->old_addr = parm->old_addr;
83 p->new_addr = parm->new_addr;
84 p->mask = parm->mask;
85 p->flags = parm->flags;
86
87 p->tcf_action = parm->action;
88 spin_unlock_bh(&p->tcf_lock);
89
90 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -040091 tcf_idr_insert(tn, *a);
Herbert Xub4219952007-09-27 12:48:05 -070092
93 return ret;
94}
95
Jamal Hadi Salim03905142018-08-12 09:34:54 -040096static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a,
97 struct tcf_result *res)
Herbert Xub4219952007-09-27 12:48:05 -070098{
WANG Conga85a9702016-07-25 16:09:41 -070099 struct tcf_nat *p = to_tcf_nat(a);
Herbert Xub4219952007-09-27 12:48:05 -0700100 struct iphdr *iph;
101 __be32 old_addr;
102 __be32 new_addr;
103 __be32 mask;
104 __be32 addr;
105 int egress;
106 int action;
107 int ihl;
Changli Gao36d12692010-08-03 17:39:18 +0000108 int noff;
Herbert Xub4219952007-09-27 12:48:05 -0700109
110 spin_lock(&p->tcf_lock);
111
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400112 tcf_lastuse_update(&p->tcf_tm);
Herbert Xub4219952007-09-27 12:48:05 -0700113 old_addr = p->old_addr;
114 new_addr = p->new_addr;
115 mask = p->mask;
116 egress = p->flags & TCA_NAT_FLAG_EGRESS;
117 action = p->tcf_action;
118
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000119 bstats_update(&p->tcf_bstats, skb);
Herbert Xub4219952007-09-27 12:48:05 -0700120
121 spin_unlock(&p->tcf_lock);
122
123 if (unlikely(action == TC_ACT_SHOT))
124 goto drop;
125
Changli Gao36d12692010-08-03 17:39:18 +0000126 noff = skb_network_offset(skb);
127 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700128 goto drop;
129
130 iph = ip_hdr(skb);
131
132 if (egress)
133 addr = iph->saddr;
134 else
135 addr = iph->daddr;
136
137 if (!((old_addr ^ addr) & mask)) {
Daniel Borkmann36976492016-02-19 23:05:25 +0100138 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700139 goto drop;
140
141 new_addr &= mask;
142 new_addr |= addr & ~mask;
143
144 /* Rewrite IP header */
145 iph = ip_hdr(skb);
146 if (egress)
147 iph->saddr = new_addr;
148 else
149 iph->daddr = new_addr;
150
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100151 csum_replace4(&iph->check, addr, new_addr);
Changli Gao33c29dd2010-05-29 14:26:59 +0000152 } else if ((iph->frag_off & htons(IP_OFFSET)) ||
153 iph->protocol != IPPROTO_ICMP) {
154 goto out;
Herbert Xub4219952007-09-27 12:48:05 -0700155 }
156
157 ihl = iph->ihl * 4;
158
159 /* It would be nice to share code with stateful NAT. */
160 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
161 case IPPROTO_TCP:
162 {
163 struct tcphdr *tcph;
164
Changli Gao36d12692010-08-03 17:39:18 +0000165 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100166 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700167 goto drop;
168
169 tcph = (void *)(skb_network_header(skb) + ihl);
Tom Herbert4b048d62015-08-17 13:42:25 -0700170 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
171 true);
Herbert Xub4219952007-09-27 12:48:05 -0700172 break;
173 }
174 case IPPROTO_UDP:
175 {
176 struct udphdr *udph;
177
Changli Gao36d12692010-08-03 17:39:18 +0000178 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100179 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700180 goto drop;
181
182 udph = (void *)(skb_network_header(skb) + ihl);
183 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100184 inet_proto_csum_replace4(&udph->check, skb, addr,
Tom Herbert4b048d62015-08-17 13:42:25 -0700185 new_addr, true);
Herbert Xub4219952007-09-27 12:48:05 -0700186 if (!udph->check)
187 udph->check = CSUM_MANGLED_0;
188 }
189 break;
190 }
191 case IPPROTO_ICMP:
192 {
193 struct icmphdr *icmph;
194
Changli Gao36d12692010-08-03 17:39:18 +0000195 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700196 goto drop;
197
198 icmph = (void *)(skb_network_header(skb) + ihl);
199
200 if ((icmph->type != ICMP_DEST_UNREACH) &&
201 (icmph->type != ICMP_TIME_EXCEEDED) &&
202 (icmph->type != ICMP_PARAMETERPROB))
203 break;
204
Changli Gao36d12692010-08-03 17:39:18 +0000205 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
206 noff))
Changli Gao70c2efa2010-07-09 15:33:25 +0000207 goto drop;
208
Changli Gao072d79a2010-07-29 13:41:46 +0000209 icmph = (void *)(skb_network_header(skb) + ihl);
Herbert Xub4219952007-09-27 12:48:05 -0700210 iph = (void *)(icmph + 1);
211 if (egress)
212 addr = iph->daddr;
213 else
214 addr = iph->saddr;
215
216 if ((old_addr ^ addr) & mask)
217 break;
218
Daniel Borkmann36976492016-02-19 23:05:25 +0100219 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
220 sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700221 goto drop;
222
223 icmph = (void *)(skb_network_header(skb) + ihl);
224 iph = (void *)(icmph + 1);
225
226 new_addr &= mask;
227 new_addr |= addr & ~mask;
228
229 /* XXX Fix up the inner checksums. */
230 if (egress)
231 iph->daddr = new_addr;
232 else
233 iph->saddr = new_addr;
234
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100235 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
Tom Herbert4b048d62015-08-17 13:42:25 -0700236 false);
Herbert Xub4219952007-09-27 12:48:05 -0700237 break;
238 }
239 default:
240 break;
241 }
242
Changli Gao33c29dd2010-05-29 14:26:59 +0000243out:
Herbert Xub4219952007-09-27 12:48:05 -0700244 return action;
245
246drop:
247 spin_lock(&p->tcf_lock);
248 p->tcf_qstats.drops++;
249 spin_unlock(&p->tcf_lock);
250 return TC_ACT_SHOT;
251}
252
253static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
254 int bind, int ref)
255{
256 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700257 struct tcf_nat *p = to_tcf_nat(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000258 struct tc_nat opt = {
Eric Dumazet1c40be12010-08-16 20:04:22 +0000259 .index = p->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300260 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
261 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000262 };
Herbert Xub4219952007-09-27 12:48:05 -0700263 struct tcf_t t;
Herbert Xub4219952007-09-27 12:48:05 -0700264
Vlad Buslovf20a4d02018-09-03 10:09:20 +0300265 spin_lock_bh(&p->tcf_lock);
266 opt.old_addr = p->old_addr;
267 opt.new_addr = p->new_addr;
268 opt.mask = p->mask;
269 opt.flags = p->flags;
270 opt.action = p->tcf_action;
271
David S. Miller1b34ec42012-03-29 05:11:39 -0400272 if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
273 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400274
275 tcf_tm_dump(&t, &p->tcf_tm);
Nicolas Dichtel9854518e2016-04-26 10:06:18 +0200276 if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400277 goto nla_put_failure;
Vlad Buslovf20a4d02018-09-03 10:09:20 +0300278 spin_unlock_bh(&p->tcf_lock);
Herbert Xub4219952007-09-27 12:48:05 -0700279
Herbert Xub4219952007-09-27 12:48:05 -0700280 return skb->len;
281
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800282nla_put_failure:
Vlad Buslovf20a4d02018-09-03 10:09:20 +0300283 spin_unlock_bh(&p->tcf_lock);
Herbert Xub4219952007-09-27 12:48:05 -0700284 nlmsg_trim(skb, b);
Herbert Xub4219952007-09-27 12:48:05 -0700285 return -1;
286}
287
WANG Congddf97cc2016-02-22 15:57:53 -0800288static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
289 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500290 const struct tc_action_ops *ops,
291 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800292{
293 struct tc_action_net *tn = net_generic(net, nat_net_id);
294
Alexander Aringb3620142018-02-15 10:54:59 -0500295 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800296}
297
Cong Wangf061b482018-08-29 10:15:35 -0700298static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800299{
300 struct tc_action_net *tn = net_generic(net, nat_net_id);
301
Chris Mi65a206c2017-08-30 02:31:59 -0400302 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800303}
304
Herbert Xub4219952007-09-27 12:48:05 -0700305static struct tc_action_ops act_nat_ops = {
306 .kind = "nat",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200307 .id = TCA_ID_NAT,
Herbert Xub4219952007-09-27 12:48:05 -0700308 .owner = THIS_MODULE,
Jamal Hadi Salim03905142018-08-12 09:34:54 -0400309 .act = tcf_nat_act,
Herbert Xub4219952007-09-27 12:48:05 -0700310 .dump = tcf_nat_dump,
Herbert Xub4219952007-09-27 12:48:05 -0700311 .init = tcf_nat_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800312 .walk = tcf_nat_walker,
313 .lookup = tcf_nat_search,
WANG Conga85a9702016-07-25 16:09:41 -0700314 .size = sizeof(struct tcf_nat),
WANG Congddf97cc2016-02-22 15:57:53 -0800315};
316
317static __net_init int nat_init_net(struct net *net)
318{
319 struct tc_action_net *tn = net_generic(net, nat_net_id);
320
Cong Wangc7e460c2017-11-06 13:47:18 -0800321 return tc_action_net_init(tn, &act_nat_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800322}
323
Cong Wang039af9c2017-12-11 15:35:03 -0800324static void __net_exit nat_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800325{
Cong Wang039af9c2017-12-11 15:35:03 -0800326 tc_action_net_exit(net_list, nat_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800327}
328
329static struct pernet_operations nat_net_ops = {
330 .init = nat_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800331 .exit_batch = nat_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800332 .id = &nat_net_id,
333 .size = sizeof(struct tc_action_net),
Herbert Xub4219952007-09-27 12:48:05 -0700334};
335
336MODULE_DESCRIPTION("Stateless NAT actions");
337MODULE_LICENSE("GPL");
338
339static int __init nat_init_module(void)
340{
WANG Congddf97cc2016-02-22 15:57:53 -0800341 return tcf_register_action(&act_nat_ops, &nat_net_ops);
Herbert Xub4219952007-09-27 12:48:05 -0700342}
343
344static void __exit nat_cleanup_module(void)
345{
WANG Congddf97cc2016-02-22 15:57:53 -0800346 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
Herbert Xub4219952007-09-27 12:48:05 -0700347}
348
349module_init(nat_init_module);
350module_exit(nat_cleanup_module);