blob: d1b47a1b145c40f0acd98ca0ab30f9ecce4f364a [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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
32static unsigned int nat_net_id;
33static struct tc_action_ops act_nat_ops;
34
35static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
36 [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) },
37};
38
39static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
40 struct tc_action **a, int ovr, int bind,
41 bool rtnl_held, struct netlink_ext_ack *extack)
42{
43 struct tc_action_net *tn = net_generic(net, nat_net_id);
44 struct nlattr *tb[TCA_NAT_MAX + 1];
45 struct tc_nat *parm;
46 int ret = 0, err;
47 struct tcf_nat *p;
48 u32 index;
49
50 if (nla == NULL)
51 return -EINVAL;
52
53 err = nla_parse_nested(tb, TCA_NAT_MAX, nla, nat_policy, NULL);
54 if (err < 0)
55 return err;
56
57 if (tb[TCA_NAT_PARMS] == NULL)
58 return -EINVAL;
59 parm = nla_data(tb[TCA_NAT_PARMS]);
60 index = parm->index;
61 err = tcf_idr_check_alloc(tn, &index, a, bind);
62 if (!err) {
63 ret = tcf_idr_create(tn, index, est, a,
64 &act_nat_ops, bind, false);
65 if (ret) {
66 tcf_idr_cleanup(tn, index);
67 return ret;
68 }
69 ret = ACT_P_CREATED;
70 } else if (err > 0) {
71 if (bind)
72 return 0;
73 if (!ovr) {
74 tcf_idr_release(*a, bind);
75 return -EEXIST;
76 }
77 } else {
78 return err;
79 }
80 p = to_tcf_nat(*a);
81
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)
92 tcf_idr_insert(tn, *a);
93
94 return ret;
95}
96
97static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a,
98 struct tcf_result *res)
99{
100 struct tcf_nat *p = to_tcf_nat(a);
101 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;
109 int noff;
110
111 spin_lock(&p->tcf_lock);
112
113 tcf_lastuse_update(&p->tcf_tm);
114 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
120 bstats_update(&p->tcf_bstats, skb);
121
122 spin_unlock(&p->tcf_lock);
123
124 if (unlikely(action == TC_ACT_SHOT))
125 goto drop;
126
127 noff = skb_network_offset(skb);
128 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
129 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)) {
139 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
140 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
152 csum_replace4(&iph->check, addr, new_addr);
153 } else if ((iph->frag_off & htons(IP_OFFSET)) ||
154 iph->protocol != IPPROTO_ICMP) {
155 goto out;
156 }
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
166 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
167 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
168 goto drop;
169
170 tcph = (void *)(skb_network_header(skb) + ihl);
171 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
172 true);
173 break;
174 }
175 case IPPROTO_UDP:
176 {
177 struct udphdr *udph;
178
179 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
180 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
181 goto drop;
182
183 udph = (void *)(skb_network_header(skb) + ihl);
184 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
185 inet_proto_csum_replace4(&udph->check, skb, addr,
186 new_addr, true);
187 if (!udph->check)
188 udph->check = CSUM_MANGLED_0;
189 }
190 break;
191 }
192 case IPPROTO_ICMP:
193 {
194 struct icmphdr *icmph;
195
196 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
197 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
206 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
207 noff))
208 goto drop;
209
210 icmph = (void *)(skb_network_header(skb) + ihl);
211 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
220 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
221 sizeof(*iph) + noff))
222 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
236 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
237 false);
238 break;
239 }
240 default:
241 break;
242 }
243
244out:
245 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);
258 struct tcf_nat *p = to_tcf_nat(a);
259 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,
267 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
268 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
269 };
270 struct tcf_t t;
271
272 if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
273 goto nla_put_failure;
274
275 tcf_tm_dump(&t, &p->tcf_tm);
276 if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
277 goto nla_put_failure;
278
279 return skb->len;
280
281nla_put_failure:
282 nlmsg_trim(skb, b);
283 return -1;
284}
285
286static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
287 struct netlink_callback *cb, int type,
288 const struct tc_action_ops *ops,
289 struct netlink_ext_ack *extack)
290{
291 struct tc_action_net *tn = net_generic(net, nat_net_id);
292
293 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
294}
295
296static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index,
297 struct netlink_ext_ack *extack)
298{
299 struct tc_action_net *tn = net_generic(net, nat_net_id);
300
301 return tcf_idr_search(tn, a, index);
302}
303
304static struct tc_action_ops act_nat_ops = {
305 .kind = "nat",
306 .type = TCA_ACT_NAT,
307 .owner = THIS_MODULE,
308 .act = tcf_nat_act,
309 .dump = tcf_nat_dump,
310 .init = tcf_nat_init,
311 .walk = tcf_nat_walker,
312 .lookup = tcf_nat_search,
313 .size = sizeof(struct tcf_nat),
314};
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
320 return tc_action_net_init(net, tn, &act_nat_ops);
321}
322
323static void __net_exit nat_exit_net(struct list_head *net_list)
324{
325 tc_action_net_exit(net_list, nat_net_id);
326}
327
328static struct pernet_operations nat_net_ops = {
329 .init = nat_init_net,
330 .exit_batch = nat_exit_net,
331 .id = &nat_net_id,
332 .size = sizeof(struct tc_action_net),
333};
334
335MODULE_DESCRIPTION("Stateless NAT actions");
336MODULE_LICENSE("GPL");
337
338static int __init nat_init_module(void)
339{
340 return tcf_register_action(&act_nat_ops, &nat_net_ops);
341}
342
343static void __exit nat_cleanup_module(void)
344{
345 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
346}
347
348module_init(nat_init_module);
349module_exit(nat_cleanup_module);