blob: d1b47a1b145c40f0acd98ca0ab30f9ecce4f364a [file] [log] [blame]
Herbert Xub4219952007-09-27 12:48:05 -07001/*
2 * Stateless NAT actions
3 *
4 * 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.
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;
Dmytro Linkin51d240a2019-08-01 13:02:51 +000048 u32 index;
Herbert Xub4219952007-09-27 12:48:05 -070049
Patrick McHardycee63722008-01-23 20:33:32 -080050 if (nla == NULL)
Herbert Xub4219952007-09-27 12:48:05 -070051 return -EINVAL;
52
Johannes Bergfceb6432017-04-12 14:34:07 +020053 err = nla_parse_nested(tb, TCA_NAT_MAX, nla, nat_policy, 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 Linkin51d240a2019-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 Linkin51d240a2019-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 Linkin51d240a2019-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 }
WANG Conga85a9702016-07-25 16:09:41 -070080 p = to_tcf_nat(*a);
Herbert Xub4219952007-09-27 12:48:05 -070081
82 spin_lock_bh(&p->tcf_lock);
83 p->old_addr = parm->old_addr;
84 p->new_addr = parm->new_addr;
85 p->mask = parm->mask;
86 p->flags = parm->flags;
87
88 p->tcf_action = parm->action;
89 spin_unlock_bh(&p->tcf_lock);
90
91 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -040092 tcf_idr_insert(tn, *a);
Herbert Xub4219952007-09-27 12:48:05 -070093
94 return ret;
95}
96
Jamal Hadi Salim03905142018-08-12 09:34:54 -040097static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a,
98 struct tcf_result *res)
Herbert Xub4219952007-09-27 12:48:05 -070099{
WANG Conga85a9702016-07-25 16:09:41 -0700100 struct tcf_nat *p = to_tcf_nat(a);
Herbert Xub4219952007-09-27 12:48:05 -0700101 struct iphdr *iph;
102 __be32 old_addr;
103 __be32 new_addr;
104 __be32 mask;
105 __be32 addr;
106 int egress;
107 int action;
108 int ihl;
Changli Gao36d12692010-08-03 17:39:18 +0000109 int noff;
Herbert Xub4219952007-09-27 12:48:05 -0700110
111 spin_lock(&p->tcf_lock);
112
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400113 tcf_lastuse_update(&p->tcf_tm);
Herbert Xub4219952007-09-27 12:48:05 -0700114 old_addr = p->old_addr;
115 new_addr = p->new_addr;
116 mask = p->mask;
117 egress = p->flags & TCA_NAT_FLAG_EGRESS;
118 action = p->tcf_action;
119
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000120 bstats_update(&p->tcf_bstats, skb);
Herbert Xub4219952007-09-27 12:48:05 -0700121
122 spin_unlock(&p->tcf_lock);
123
124 if (unlikely(action == TC_ACT_SHOT))
125 goto drop;
126
Changli Gao36d12692010-08-03 17:39:18 +0000127 noff = skb_network_offset(skb);
128 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700129 goto drop;
130
131 iph = ip_hdr(skb);
132
133 if (egress)
134 addr = iph->saddr;
135 else
136 addr = iph->daddr;
137
138 if (!((old_addr ^ addr) & mask)) {
Daniel Borkmann36976492016-02-19 23:05:25 +0100139 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700140 goto drop;
141
142 new_addr &= mask;
143 new_addr |= addr & ~mask;
144
145 /* Rewrite IP header */
146 iph = ip_hdr(skb);
147 if (egress)
148 iph->saddr = new_addr;
149 else
150 iph->daddr = new_addr;
151
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100152 csum_replace4(&iph->check, addr, new_addr);
Changli Gao33c29dd2010-05-29 14:26:59 +0000153 } else if ((iph->frag_off & htons(IP_OFFSET)) ||
154 iph->protocol != IPPROTO_ICMP) {
155 goto out;
Herbert Xub4219952007-09-27 12:48:05 -0700156 }
157
158 ihl = iph->ihl * 4;
159
160 /* It would be nice to share code with stateful NAT. */
161 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
162 case IPPROTO_TCP:
163 {
164 struct tcphdr *tcph;
165
Changli Gao36d12692010-08-03 17:39:18 +0000166 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100167 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700168 goto drop;
169
170 tcph = (void *)(skb_network_header(skb) + ihl);
Tom Herbert4b048d62015-08-17 13:42:25 -0700171 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
172 true);
Herbert Xub4219952007-09-27 12:48:05 -0700173 break;
174 }
175 case IPPROTO_UDP:
176 {
177 struct udphdr *udph;
178
Changli Gao36d12692010-08-03 17:39:18 +0000179 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100180 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700181 goto drop;
182
183 udph = (void *)(skb_network_header(skb) + ihl);
184 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100185 inet_proto_csum_replace4(&udph->check, skb, addr,
Tom Herbert4b048d62015-08-17 13:42:25 -0700186 new_addr, true);
Herbert Xub4219952007-09-27 12:48:05 -0700187 if (!udph->check)
188 udph->check = CSUM_MANGLED_0;
189 }
190 break;
191 }
192 case IPPROTO_ICMP:
193 {
194 struct icmphdr *icmph;
195
Changli Gao36d12692010-08-03 17:39:18 +0000196 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700197 goto drop;
198
199 icmph = (void *)(skb_network_header(skb) + ihl);
200
201 if ((icmph->type != ICMP_DEST_UNREACH) &&
202 (icmph->type != ICMP_TIME_EXCEEDED) &&
203 (icmph->type != ICMP_PARAMETERPROB))
204 break;
205
Changli Gao36d12692010-08-03 17:39:18 +0000206 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
207 noff))
Changli Gao70c2efa2010-07-09 15:33:25 +0000208 goto drop;
209
Changli Gao072d79a2010-07-29 13:41:46 +0000210 icmph = (void *)(skb_network_header(skb) + ihl);
Herbert Xub4219952007-09-27 12:48:05 -0700211 iph = (void *)(icmph + 1);
212 if (egress)
213 addr = iph->daddr;
214 else
215 addr = iph->saddr;
216
217 if ((old_addr ^ addr) & mask)
218 break;
219
Daniel Borkmann36976492016-02-19 23:05:25 +0100220 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
221 sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700222 goto drop;
223
224 icmph = (void *)(skb_network_header(skb) + ihl);
225 iph = (void *)(icmph + 1);
226
227 new_addr &= mask;
228 new_addr |= addr & ~mask;
229
230 /* XXX Fix up the inner checksums. */
231 if (egress)
232 iph->daddr = new_addr;
233 else
234 iph->saddr = new_addr;
235
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100236 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
Tom Herbert4b048d62015-08-17 13:42:25 -0700237 false);
Herbert Xub4219952007-09-27 12:48:05 -0700238 break;
239 }
240 default:
241 break;
242 }
243
Changli Gao33c29dd2010-05-29 14:26:59 +0000244out:
Herbert Xub4219952007-09-27 12:48:05 -0700245 return action;
246
247drop:
248 spin_lock(&p->tcf_lock);
249 p->tcf_qstats.drops++;
250 spin_unlock(&p->tcf_lock);
251 return TC_ACT_SHOT;
252}
253
254static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
255 int bind, int ref)
256{
257 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700258 struct tcf_nat *p = to_tcf_nat(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000259 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
265 .index = p->tcf_index,
266 .action = p->tcf_action,
Vlad Buslov036bb442018-07-05 17:24:24 +0300267 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
268 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
Eric Dumazet1c40be12010-08-16 20:04:22 +0000269 };
Herbert Xub4219952007-09-27 12:48:05 -0700270 struct tcf_t t;
Herbert Xub4219952007-09-27 12:48:05 -0700271
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 Dichtel98545182016-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;
Herbert Xub4219952007-09-27 12:48:05 -0700278
Herbert Xub4219952007-09-27 12:48:05 -0700279 return skb->len;
280
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800281nla_put_failure:
Herbert Xub4219952007-09-27 12:48:05 -0700282 nlmsg_trim(skb, b);
Herbert Xub4219952007-09-27 12:48:05 -0700283 return -1;
284}
285
WANG Congddf97cc2016-02-22 15:57:53 -0800286static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
287 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500288 const struct tc_action_ops *ops,
289 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800290{
291 struct tc_action_net *tn = net_generic(net, nat_net_id);
292
Alexander Aringb3620142018-02-15 10:54:59 -0500293 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
WANG Congddf97cc2016-02-22 15:57:53 -0800294}
295
Alexander Aring331a9292018-02-15 10:54:57 -0500296static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index,
297 struct netlink_ext_ack *extack)
WANG Congddf97cc2016-02-22 15:57:53 -0800298{
299 struct tc_action_net *tn = net_generic(net, nat_net_id);
300
Chris Mi65a206c2017-08-30 02:31:59 -0400301 return tcf_idr_search(tn, a, index);
WANG Congddf97cc2016-02-22 15:57:53 -0800302}
303
Herbert Xub4219952007-09-27 12:48:05 -0700304static struct tc_action_ops act_nat_ops = {
305 .kind = "nat",
Herbert Xub4219952007-09-27 12:48:05 -0700306 .type = TCA_ACT_NAT,
Herbert Xub4219952007-09-27 12:48:05 -0700307 .owner = THIS_MODULE,
Jamal Hadi Salim03905142018-08-12 09:34:54 -0400308 .act = tcf_nat_act,
Herbert Xub4219952007-09-27 12:48:05 -0700309 .dump = tcf_nat_dump,
Herbert Xub4219952007-09-27 12:48:05 -0700310 .init = tcf_nat_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800311 .walk = tcf_nat_walker,
312 .lookup = tcf_nat_search,
WANG Conga85a9702016-07-25 16:09:41 -0700313 .size = sizeof(struct tcf_nat),
WANG Congddf97cc2016-02-22 15:57:53 -0800314};
315
316static __net_init int nat_init_net(struct net *net)
317{
318 struct tc_action_net *tn = net_generic(net, nat_net_id);
319
Cong Wang38166932019-08-25 10:01:32 -0700320 return tc_action_net_init(net, tn, &act_nat_ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800321}
322
Cong Wang039af9c2017-12-11 15:35:03 -0800323static void __net_exit nat_exit_net(struct list_head *net_list)
WANG Congddf97cc2016-02-22 15:57:53 -0800324{
Cong Wang039af9c2017-12-11 15:35:03 -0800325 tc_action_net_exit(net_list, nat_net_id);
WANG Congddf97cc2016-02-22 15:57:53 -0800326}
327
328static struct pernet_operations nat_net_ops = {
329 .init = nat_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800330 .exit_batch = nat_exit_net,
WANG Congddf97cc2016-02-22 15:57:53 -0800331 .id = &nat_net_id,
332 .size = sizeof(struct tc_action_net),
Herbert Xub4219952007-09-27 12:48:05 -0700333};
334
335MODULE_DESCRIPTION("Stateless NAT actions");
336MODULE_LICENSE("GPL");
337
338static int __init nat_init_module(void)
339{
WANG Congddf97cc2016-02-22 15:57:53 -0800340 return tcf_register_action(&act_nat_ops, &nat_net_ops);
Herbert Xub4219952007-09-27 12:48:05 -0700341}
342
343static void __exit nat_cleanup_module(void)
344{
WANG Congddf97cc2016-02-22 15:57:53 -0800345 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
Herbert Xub4219952007-09-27 12:48:05 -0700346}
347
348module_init(nat_init_module);
349module_exit(nat_cleanup_module);