1
2
3
4
5
6
7
8
9
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/pkt_cls.h>
25#include <net/icmp.h>
26#include <net/ip.h>
27#include <net/netlink.h>
28#include <net/tc_act/tc_nat.h>
29#include <net/tcp.h>
30#include <net/udp.h>
31
32
33static unsigned int nat_net_id;
34static struct tc_action_ops act_nat_ops;
35
36static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
37 [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) },
38};
39
40static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
41 struct tc_action **a, int ovr, int bind,
42 bool rtnl_held, struct tcf_proto *tp,
43 u32 flags, struct netlink_ext_ack *extack)
44{
45 struct tc_action_net *tn = net_generic(net, nat_net_id);
46 struct nlattr *tb[TCA_NAT_MAX + 1];
47 struct tcf_chain *goto_ch = NULL;
48 struct tc_nat *parm;
49 int ret = 0, err;
50 struct tcf_nat *p;
51 u32 index;
52
53 if (nla == NULL)
54 return -EINVAL;
55
56 err = nla_parse_nested_deprecated(tb, TCA_NAT_MAX, nla, nat_policy,
57 NULL);
58 if (err < 0)
59 return err;
60
61 if (tb[TCA_NAT_PARMS] == NULL)
62 return -EINVAL;
63 parm = nla_data(tb[TCA_NAT_PARMS]);
64 index = parm->index;
65 err = tcf_idr_check_alloc(tn, &index, a, bind);
66 if (!err) {
67 ret = tcf_idr_create(tn, index, est, a,
68 &act_nat_ops, bind, false, 0);
69 if (ret) {
70 tcf_idr_cleanup(tn, index);
71 return ret;
72 }
73 ret = ACT_P_CREATED;
74 } else if (err > 0) {
75 if (bind)
76 return 0;
77 if (!ovr) {
78 tcf_idr_release(*a, bind);
79 return -EEXIST;
80 }
81 } else {
82 return err;
83 }
84 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
85 if (err < 0)
86 goto release_idr;
87 p = to_tcf_nat(*a);
88
89 spin_lock_bh(&p->tcf_lock);
90 p->old_addr = parm->old_addr;
91 p->new_addr = parm->new_addr;
92 p->mask = parm->mask;
93 p->flags = parm->flags;
94
95 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
96 spin_unlock_bh(&p->tcf_lock);
97 if (goto_ch)
98 tcf_chain_put_by_act(goto_ch);
99
100 return ret;
101release_idr:
102 tcf_idr_release(*a, bind);
103 return err;
104}
105
106static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a,
107 struct tcf_result *res)
108{
109 struct tcf_nat *p = to_tcf_nat(a);
110 struct iphdr *iph;
111 __be32 old_addr;
112 __be32 new_addr;
113 __be32 mask;
114 __be32 addr;
115 int egress;
116 int action;
117 int ihl;
118 int noff;
119
120 spin_lock(&p->tcf_lock);
121
122 tcf_lastuse_update(&p->tcf_tm);
123 old_addr = p->old_addr;
124 new_addr = p->new_addr;
125 mask = p->mask;
126 egress = p->flags & TCA_NAT_FLAG_EGRESS;
127 action = p->tcf_action;
128
129 bstats_update(&p->tcf_bstats, skb);
130
131 spin_unlock(&p->tcf_lock);
132
133 if (unlikely(action == TC_ACT_SHOT))
134 goto drop;
135
136 noff = skb_network_offset(skb);
137 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
138 goto drop;
139
140 iph = ip_hdr(skb);
141
142 if (egress)
143 addr = iph->saddr;
144 else
145 addr = iph->daddr;
146
147 if (!((old_addr ^ addr) & mask)) {
148 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
149 goto drop;
150
151 new_addr &= mask;
152 new_addr |= addr & ~mask;
153
154
155 iph = ip_hdr(skb);
156 if (egress)
157 iph->saddr = new_addr;
158 else
159 iph->daddr = new_addr;
160
161 csum_replace4(&iph->check, addr, new_addr);
162 } else if ((iph->frag_off & htons(IP_OFFSET)) ||
163 iph->protocol != IPPROTO_ICMP) {
164 goto out;
165 }
166
167 ihl = iph->ihl * 4;
168
169
170 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
171 case IPPROTO_TCP:
172 {
173 struct tcphdr *tcph;
174
175 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
176 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
177 goto drop;
178
179 tcph = (void *)(skb_network_header(skb) + ihl);
180 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
181 true);
182 break;
183 }
184 case IPPROTO_UDP:
185 {
186 struct udphdr *udph;
187
188 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
189 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
190 goto drop;
191
192 udph = (void *)(skb_network_header(skb) + ihl);
193 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
194 inet_proto_csum_replace4(&udph->check, skb, addr,
195 new_addr, true);
196 if (!udph->check)
197 udph->check = CSUM_MANGLED_0;
198 }
199 break;
200 }
201 case IPPROTO_ICMP:
202 {
203 struct icmphdr *icmph;
204
205 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
206 goto drop;
207
208 icmph = (void *)(skb_network_header(skb) + ihl);
209
210 if (!icmp_is_err(icmph->type))
211 break;
212
213 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
214 noff))
215 goto drop;
216
217 icmph = (void *)(skb_network_header(skb) + ihl);
218 iph = (void *)(icmph + 1);
219 if (egress)
220 addr = iph->daddr;
221 else
222 addr = iph->saddr;
223
224 if ((old_addr ^ addr) & mask)
225 break;
226
227 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
228 sizeof(*iph) + noff))
229 goto drop;
230
231 icmph = (void *)(skb_network_header(skb) + ihl);
232 iph = (void *)(icmph + 1);
233
234 new_addr &= mask;
235 new_addr |= addr & ~mask;
236
237
238 if (egress)
239 iph->daddr = new_addr;
240 else
241 iph->saddr = new_addr;
242
243 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
244 false);
245 break;
246 }
247 default:
248 break;
249 }
250
251out:
252 return action;
253
254drop:
255 spin_lock(&p->tcf_lock);
256 p->tcf_qstats.drops++;
257 spin_unlock(&p->tcf_lock);
258 return TC_ACT_SHOT;
259}
260
261static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
262 int bind, int ref)
263{
264 unsigned char *b = skb_tail_pointer(skb);
265 struct tcf_nat *p = to_tcf_nat(a);
266 struct tc_nat opt = {
267 .index = p->tcf_index,
268 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
269 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
270 };
271 struct tcf_t t;
272
273 spin_lock_bh(&p->tcf_lock);
274 opt.old_addr = p->old_addr;
275 opt.new_addr = p->new_addr;
276 opt.mask = p->mask;
277 opt.flags = p->flags;
278 opt.action = p->tcf_action;
279
280 if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
281 goto nla_put_failure;
282
283 tcf_tm_dump(&t, &p->tcf_tm);
284 if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
285 goto nla_put_failure;
286 spin_unlock_bh(&p->tcf_lock);
287
288 return skb->len;
289
290nla_put_failure:
291 spin_unlock_bh(&p->tcf_lock);
292 nlmsg_trim(skb, b);
293 return -1;
294}
295
296static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
297 struct netlink_callback *cb, int type,
298 const struct tc_action_ops *ops,
299 struct netlink_ext_ack *extack)
300{
301 struct tc_action_net *tn = net_generic(net, nat_net_id);
302
303 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
304}
305
306static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index)
307{
308 struct tc_action_net *tn = net_generic(net, nat_net_id);
309
310 return tcf_idr_search(tn, a, index);
311}
312
313static struct tc_action_ops act_nat_ops = {
314 .kind = "nat",
315 .id = TCA_ID_NAT,
316 .owner = THIS_MODULE,
317 .act = tcf_nat_act,
318 .dump = tcf_nat_dump,
319 .init = tcf_nat_init,
320 .walk = tcf_nat_walker,
321 .lookup = tcf_nat_search,
322 .size = sizeof(struct tcf_nat),
323};
324
325static __net_init int nat_init_net(struct net *net)
326{
327 struct tc_action_net *tn = net_generic(net, nat_net_id);
328
329 return tc_action_net_init(net, tn, &act_nat_ops);
330}
331
332static void __net_exit nat_exit_net(struct list_head *net_list)
333{
334 tc_action_net_exit(net_list, nat_net_id);
335}
336
337static struct pernet_operations nat_net_ops = {
338 .init = nat_init_net,
339 .exit_batch = nat_exit_net,
340 .id = &nat_net_id,
341 .size = sizeof(struct tc_action_net),
342};
343
344MODULE_DESCRIPTION("Stateless NAT actions");
345MODULE_LICENSE("GPL");
346
347static int __init nat_init_module(void)
348{
349 return tcf_register_action(&act_nat_ops, &nat_net_ops);
350}
351
352static void __exit nat_cleanup_module(void)
353{
354 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
355}
356
357module_init(nat_init_module);
358module_exit(nat_cleanup_module);
359