blob: 8e8b0cc30704e7bbf69975932d4678f2e8caf9a1 [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
32#define NAT_TAB_MASK 15
Herbert Xub4219952007-09-27 12:48:05 -070033
WANG Congddf97cc2016-02-22 15:57:53 -080034static int nat_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070035static struct tc_action_ops act_nat_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080036
Patrick McHardy53b2bf32008-01-23 20:36:30 -080037static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
38 [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) },
39};
40
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000041static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
WANG Conga85a9702016-07-25 16:09:41 -070042 struct tc_action **a, int ovr, int bind)
Herbert Xub4219952007-09-27 12:48:05 -070043{
WANG Congddf97cc2016-02-22 15:57:53 -080044 struct tc_action_net *tn = net_generic(net, nat_net_id);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080045 struct nlattr *tb[TCA_NAT_MAX + 1];
Herbert Xub4219952007-09-27 12:48:05 -070046 struct tc_nat *parm;
Patrick McHardycee63722008-01-23 20:33:32 -080047 int ret = 0, err;
Herbert Xub4219952007-09-27 12:48:05 -070048 struct tcf_nat *p;
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
Patrick McHardy53b2bf32008-01-23 20:36:30 -080053 err = nla_parse_nested(tb, TCA_NAT_MAX, nla, nat_policy);
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]);
Herbert Xub4219952007-09-27 12:48:05 -070060
WANG Congddf97cc2016-02-22 15:57:53 -080061 if (!tcf_hash_check(tn, parm->index, a, bind)) {
62 ret = tcf_hash_create(tn, parm->index, est, a,
WANG Conga85a9702016-07-25 16:09:41 -070063 &act_nat_ops, bind, false);
WANG Cong86062032014-02-11 17:07:31 -080064 if (ret)
65 return ret;
Herbert Xub4219952007-09-27 12:48:05 -070066 ret = ACT_P_CREATED;
67 } else {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050068 if (bind)
69 return 0;
WANG Conga85a9702016-07-25 16:09:41 -070070 tcf_hash_release(*a, bind);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050071 if (!ovr)
Herbert Xub4219952007-09-27 12:48:05 -070072 return -EEXIST;
Herbert Xub4219952007-09-27 12:48:05 -070073 }
WANG Conga85a9702016-07-25 16:09:41 -070074 p = to_tcf_nat(*a);
Herbert Xub4219952007-09-27 12:48:05 -070075
76 spin_lock_bh(&p->tcf_lock);
77 p->old_addr = parm->old_addr;
78 p->new_addr = parm->new_addr;
79 p->mask = parm->mask;
80 p->flags = parm->flags;
81
82 p->tcf_action = parm->action;
83 spin_unlock_bh(&p->tcf_lock);
84
85 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -070086 tcf_hash_insert(tn, *a);
Herbert Xub4219952007-09-27 12:48:05 -070087
88 return ret;
89}
90
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000091static int tcf_nat(struct sk_buff *skb, const struct tc_action *a,
Herbert Xub4219952007-09-27 12:48:05 -070092 struct tcf_result *res)
93{
WANG Conga85a9702016-07-25 16:09:41 -070094 struct tcf_nat *p = to_tcf_nat(a);
Herbert Xub4219952007-09-27 12:48:05 -070095 struct iphdr *iph;
96 __be32 old_addr;
97 __be32 new_addr;
98 __be32 mask;
99 __be32 addr;
100 int egress;
101 int action;
102 int ihl;
Changli Gao36d12692010-08-03 17:39:18 +0000103 int noff;
Herbert Xub4219952007-09-27 12:48:05 -0700104
105 spin_lock(&p->tcf_lock);
106
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400107 tcf_lastuse_update(&p->tcf_tm);
Herbert Xub4219952007-09-27 12:48:05 -0700108 old_addr = p->old_addr;
109 new_addr = p->new_addr;
110 mask = p->mask;
111 egress = p->flags & TCA_NAT_FLAG_EGRESS;
112 action = p->tcf_action;
113
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000114 bstats_update(&p->tcf_bstats, skb);
Herbert Xub4219952007-09-27 12:48:05 -0700115
116 spin_unlock(&p->tcf_lock);
117
118 if (unlikely(action == TC_ACT_SHOT))
119 goto drop;
120
Changli Gao36d12692010-08-03 17:39:18 +0000121 noff = skb_network_offset(skb);
122 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700123 goto drop;
124
125 iph = ip_hdr(skb);
126
127 if (egress)
128 addr = iph->saddr;
129 else
130 addr = iph->daddr;
131
132 if (!((old_addr ^ addr) & mask)) {
Daniel Borkmann36976492016-02-19 23:05:25 +0100133 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700134 goto drop;
135
136 new_addr &= mask;
137 new_addr |= addr & ~mask;
138
139 /* Rewrite IP header */
140 iph = ip_hdr(skb);
141 if (egress)
142 iph->saddr = new_addr;
143 else
144 iph->daddr = new_addr;
145
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100146 csum_replace4(&iph->check, addr, new_addr);
Changli Gao33c29dd2010-05-29 14:26:59 +0000147 } else if ((iph->frag_off & htons(IP_OFFSET)) ||
148 iph->protocol != IPPROTO_ICMP) {
149 goto out;
Herbert Xub4219952007-09-27 12:48:05 -0700150 }
151
152 ihl = iph->ihl * 4;
153
154 /* It would be nice to share code with stateful NAT. */
155 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
156 case IPPROTO_TCP:
157 {
158 struct tcphdr *tcph;
159
Changli Gao36d12692010-08-03 17:39:18 +0000160 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100161 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700162 goto drop;
163
164 tcph = (void *)(skb_network_header(skb) + ihl);
Tom Herbert4b048d62015-08-17 13:42:25 -0700165 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
166 true);
Herbert Xub4219952007-09-27 12:48:05 -0700167 break;
168 }
169 case IPPROTO_UDP:
170 {
171 struct udphdr *udph;
172
Changli Gao36d12692010-08-03 17:39:18 +0000173 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
Daniel Borkmann36976492016-02-19 23:05:25 +0100174 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700175 goto drop;
176
177 udph = (void *)(skb_network_header(skb) + ihl);
178 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100179 inet_proto_csum_replace4(&udph->check, skb, addr,
Tom Herbert4b048d62015-08-17 13:42:25 -0700180 new_addr, true);
Herbert Xub4219952007-09-27 12:48:05 -0700181 if (!udph->check)
182 udph->check = CSUM_MANGLED_0;
183 }
184 break;
185 }
186 case IPPROTO_ICMP:
187 {
188 struct icmphdr *icmph;
189
Changli Gao36d12692010-08-03 17:39:18 +0000190 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700191 goto drop;
192
193 icmph = (void *)(skb_network_header(skb) + ihl);
194
195 if ((icmph->type != ICMP_DEST_UNREACH) &&
196 (icmph->type != ICMP_TIME_EXCEEDED) &&
197 (icmph->type != ICMP_PARAMETERPROB))
198 break;
199
Changli Gao36d12692010-08-03 17:39:18 +0000200 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
201 noff))
Changli Gao70c2efa2010-07-09 15:33:25 +0000202 goto drop;
203
Changli Gao072d79a2010-07-29 13:41:46 +0000204 icmph = (void *)(skb_network_header(skb) + ihl);
Herbert Xub4219952007-09-27 12:48:05 -0700205 iph = (void *)(icmph + 1);
206 if (egress)
207 addr = iph->daddr;
208 else
209 addr = iph->saddr;
210
211 if ((old_addr ^ addr) & mask)
212 break;
213
Daniel Borkmann36976492016-02-19 23:05:25 +0100214 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
215 sizeof(*iph) + noff))
Herbert Xub4219952007-09-27 12:48:05 -0700216 goto drop;
217
218 icmph = (void *)(skb_network_header(skb) + ihl);
219 iph = (void *)(icmph + 1);
220
221 new_addr &= mask;
222 new_addr |= addr & ~mask;
223
224 /* XXX Fix up the inner checksums. */
225 if (egress)
226 iph->daddr = new_addr;
227 else
228 iph->saddr = new_addr;
229
Patrick McHardybe0ea7d2007-11-30 01:17:11 +1100230 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
Tom Herbert4b048d62015-08-17 13:42:25 -0700231 false);
Herbert Xub4219952007-09-27 12:48:05 -0700232 break;
233 }
234 default:
235 break;
236 }
237
Changli Gao33c29dd2010-05-29 14:26:59 +0000238out:
Herbert Xub4219952007-09-27 12:48:05 -0700239 return action;
240
241drop:
242 spin_lock(&p->tcf_lock);
243 p->tcf_qstats.drops++;
244 spin_unlock(&p->tcf_lock);
245 return TC_ACT_SHOT;
246}
247
248static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
249 int bind, int ref)
250{
251 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700252 struct tcf_nat *p = to_tcf_nat(a);
Eric Dumazet1c40be12010-08-16 20:04:22 +0000253 struct tc_nat opt = {
254 .old_addr = p->old_addr,
255 .new_addr = p->new_addr,
256 .mask = p->mask,
257 .flags = p->flags,
258
259 .index = p->tcf_index,
260 .action = p->tcf_action,
261 .refcnt = p->tcf_refcnt - ref,
262 .bindcnt = p->tcf_bindcnt - bind,
263 };
Herbert Xub4219952007-09-27 12:48:05 -0700264 struct tcf_t t;
Herbert Xub4219952007-09-27 12:48:05 -0700265
David S. Miller1b34ec42012-03-29 05:11:39 -0400266 if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
267 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400268
269 tcf_tm_dump(&t, &p->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200270 if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400271 goto nla_put_failure;
Herbert Xub4219952007-09-27 12:48:05 -0700272
Herbert Xub4219952007-09-27 12:48:05 -0700273 return skb->len;
274
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800275nla_put_failure:
Herbert Xub4219952007-09-27 12:48:05 -0700276 nlmsg_trim(skb, b);
Herbert Xub4219952007-09-27 12:48:05 -0700277 return -1;
278}
279
WANG Congddf97cc2016-02-22 15:57:53 -0800280static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
281 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700282 const struct tc_action_ops *ops)
WANG Congddf97cc2016-02-22 15:57:53 -0800283{
284 struct tc_action_net *tn = net_generic(net, nat_net_id);
285
WANG Conga85a9702016-07-25 16:09:41 -0700286 return tcf_generic_walker(tn, skb, cb, type, ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800287}
288
WANG Conga85a9702016-07-25 16:09:41 -0700289static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800290{
291 struct tc_action_net *tn = net_generic(net, nat_net_id);
292
293 return tcf_hash_search(tn, a, index);
294}
295
Herbert Xub4219952007-09-27 12:48:05 -0700296static struct tc_action_ops act_nat_ops = {
297 .kind = "nat",
Herbert Xub4219952007-09-27 12:48:05 -0700298 .type = TCA_ACT_NAT,
Herbert Xub4219952007-09-27 12:48:05 -0700299 .owner = THIS_MODULE,
300 .act = tcf_nat,
301 .dump = tcf_nat_dump,
Herbert Xub4219952007-09-27 12:48:05 -0700302 .init = tcf_nat_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800303 .walk = tcf_nat_walker,
304 .lookup = tcf_nat_search,
WANG Conga85a9702016-07-25 16:09:41 -0700305 .size = sizeof(struct tcf_nat),
WANG Congddf97cc2016-02-22 15:57:53 -0800306};
307
308static __net_init int nat_init_net(struct net *net)
309{
310 struct tc_action_net *tn = net_generic(net, nat_net_id);
311
312 return tc_action_net_init(tn, &act_nat_ops, NAT_TAB_MASK);
313}
314
315static void __net_exit nat_exit_net(struct net *net)
316{
317 struct tc_action_net *tn = net_generic(net, nat_net_id);
318
319 tc_action_net_exit(tn);
320}
321
322static struct pernet_operations nat_net_ops = {
323 .init = nat_init_net,
324 .exit = nat_exit_net,
325 .id = &nat_net_id,
326 .size = sizeof(struct tc_action_net),
Herbert Xub4219952007-09-27 12:48:05 -0700327};
328
329MODULE_DESCRIPTION("Stateless NAT actions");
330MODULE_LICENSE("GPL");
331
332static int __init nat_init_module(void)
333{
WANG Congddf97cc2016-02-22 15:57:53 -0800334 return tcf_register_action(&act_nat_ops, &nat_net_ops);
Herbert Xub4219952007-09-27 12:48:05 -0700335}
336
337static void __exit nat_cleanup_module(void)
338{
WANG Congddf97cc2016-02-22 15:57:53 -0800339 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
Herbert Xub4219952007-09-27 12:48:05 -0700340}
341
342module_init(nat_init_module);
343module_exit(nat_cleanup_module);