]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
b4219952 HX |
2 | /* |
3 | * Stateless NAT actions | |
4 | * | |
5 | * Copyright (c) 2007 Herbert Xu <[email protected]> | |
b4219952 HX |
6 | */ |
7 | ||
8 | #include <linux/errno.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/kernel.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/netfilter.h> | |
13 | #include <linux/rtnetlink.h> | |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/spinlock.h> | |
17 | #include <linux/string.h> | |
18 | #include <linux/tc_act/tc_nat.h> | |
19 | #include <net/act_api.h> | |
1e45d043 | 20 | #include <net/pkt_cls.h> |
b4219952 HX |
21 | #include <net/icmp.h> |
22 | #include <net/ip.h> | |
23 | #include <net/netlink.h> | |
24 | #include <net/tc_act/tc_nat.h> | |
25 | #include <net/tcp.h> | |
26 | #include <net/udp.h> | |
27 | ||
28 | ||
a85a970a | 29 | static struct tc_action_ops act_nat_ops; |
ddf97ccd | 30 | |
53b2bf3f PM |
31 | static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = { |
32 | [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) }, | |
33 | }; | |
34 | ||
c1b52739 | 35 | static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est, |
695176bf | 36 | struct tc_action **a, struct tcf_proto *tp, |
abbb0d33 | 37 | u32 flags, struct netlink_ext_ack *extack) |
b4219952 | 38 | { |
acd0a7ab | 39 | struct tc_action_net *tn = net_generic(net, act_nat_ops.net_id); |
695176bf | 40 | bool bind = flags & TCA_ACT_FLAGS_BIND; |
7ba699c6 | 41 | struct nlattr *tb[TCA_NAT_MAX + 1]; |
1e45d043 | 42 | struct tcf_chain *goto_ch = NULL; |
b4219952 | 43 | struct tc_nat *parm; |
cee63723 | 44 | int ret = 0, err; |
b4219952 | 45 | struct tcf_nat *p; |
7be8ef2c | 46 | u32 index; |
b4219952 | 47 | |
cee63723 | 48 | if (nla == NULL) |
b4219952 HX |
49 | return -EINVAL; |
50 | ||
8cb08174 JB |
51 | err = nla_parse_nested_deprecated(tb, TCA_NAT_MAX, nla, nat_policy, |
52 | NULL); | |
cee63723 PM |
53 | if (err < 0) |
54 | return err; | |
55 | ||
53b2bf3f | 56 | if (tb[TCA_NAT_PARMS] == NULL) |
b4219952 | 57 | return -EINVAL; |
7ba699c6 | 58 | parm = nla_data(tb[TCA_NAT_PARMS]); |
7be8ef2c DL |
59 | index = parm->index; |
60 | err = tcf_idr_check_alloc(tn, &index, a, bind); | |
0190c1d4 | 61 | if (!err) { |
7be8ef2c | 62 | ret = tcf_idr_create(tn, index, est, a, |
40bd094d | 63 | &act_nat_ops, bind, false, flags); |
0190c1d4 | 64 | if (ret) { |
7be8ef2c | 65 | tcf_idr_cleanup(tn, index); |
86062033 | 66 | return ret; |
0190c1d4 | 67 | } |
b4219952 | 68 | ret = ACT_P_CREATED; |
0190c1d4 | 69 | } else if (err > 0) { |
1a29321e JHS |
70 | if (bind) |
71 | return 0; | |
695176bf | 72 | if (!(flags & TCA_ACT_FLAGS_REPLACE)) { |
4e8ddd7f | 73 | tcf_idr_release(*a, bind); |
b4219952 | 74 | return -EEXIST; |
4e8ddd7f | 75 | } |
0190c1d4 VB |
76 | } else { |
77 | return err; | |
b4219952 | 78 | } |
1e45d043 DC |
79 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
80 | if (err < 0) | |
81 | goto release_idr; | |
a85a970a | 82 | p = to_tcf_nat(*a); |
b4219952 HX |
83 | |
84 | spin_lock_bh(&p->tcf_lock); | |
85 | p->old_addr = parm->old_addr; | |
86 | p->new_addr = parm->new_addr; | |
87 | p->mask = parm->mask; | |
88 | p->flags = parm->flags; | |
89 | ||
1e45d043 | 90 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
b4219952 | 91 | spin_unlock_bh(&p->tcf_lock); |
1e45d043 DC |
92 | if (goto_ch) |
93 | tcf_chain_put_by_act(goto_ch); | |
b4219952 | 94 | |
b4219952 | 95 | return ret; |
1e45d043 DC |
96 | release_idr: |
97 | tcf_idr_release(*a, bind); | |
98 | return err; | |
b4219952 HX |
99 | } |
100 | ||
0390514f JHS |
101 | static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a, |
102 | struct tcf_result *res) | |
b4219952 | 103 | { |
a85a970a | 104 | struct tcf_nat *p = to_tcf_nat(a); |
b4219952 HX |
105 | struct iphdr *iph; |
106 | __be32 old_addr; | |
107 | __be32 new_addr; | |
108 | __be32 mask; | |
109 | __be32 addr; | |
110 | int egress; | |
111 | int action; | |
112 | int ihl; | |
36d12690 | 113 | int noff; |
b4219952 HX |
114 | |
115 | spin_lock(&p->tcf_lock); | |
116 | ||
9c4a4e48 | 117 | tcf_lastuse_update(&p->tcf_tm); |
b4219952 HX |
118 | old_addr = p->old_addr; |
119 | new_addr = p->new_addr; | |
120 | mask = p->mask; | |
121 | egress = p->flags & TCA_NAT_FLAG_EGRESS; | |
122 | action = p->tcf_action; | |
123 | ||
bfe0d029 | 124 | bstats_update(&p->tcf_bstats, skb); |
b4219952 HX |
125 | |
126 | spin_unlock(&p->tcf_lock); | |
127 | ||
128 | if (unlikely(action == TC_ACT_SHOT)) | |
129 | goto drop; | |
130 | ||
36d12690 CG |
131 | noff = skb_network_offset(skb); |
132 | if (!pskb_may_pull(skb, sizeof(*iph) + noff)) | |
b4219952 HX |
133 | goto drop; |
134 | ||
135 | iph = ip_hdr(skb); | |
136 | ||
137 | if (egress) | |
138 | addr = iph->saddr; | |
139 | else | |
140 | addr = iph->daddr; | |
141 | ||
142 | if (!((old_addr ^ addr) & mask)) { | |
3697649f | 143 | if (skb_try_make_writable(skb, sizeof(*iph) + noff)) |
b4219952 HX |
144 | goto drop; |
145 | ||
146 | new_addr &= mask; | |
147 | new_addr |= addr & ~mask; | |
148 | ||
149 | /* Rewrite IP header */ | |
150 | iph = ip_hdr(skb); | |
151 | if (egress) | |
152 | iph->saddr = new_addr; | |
153 | else | |
154 | iph->daddr = new_addr; | |
155 | ||
be0ea7d5 | 156 | csum_replace4(&iph->check, addr, new_addr); |
33c29dde CG |
157 | } else if ((iph->frag_off & htons(IP_OFFSET)) || |
158 | iph->protocol != IPPROTO_ICMP) { | |
159 | goto out; | |
b4219952 HX |
160 | } |
161 | ||
162 | ihl = iph->ihl * 4; | |
163 | ||
164 | /* It would be nice to share code with stateful NAT. */ | |
165 | switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) { | |
166 | case IPPROTO_TCP: | |
167 | { | |
168 | struct tcphdr *tcph; | |
169 | ||
36d12690 | 170 | if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) || |
3697649f | 171 | skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff)) |
b4219952 HX |
172 | goto drop; |
173 | ||
174 | tcph = (void *)(skb_network_header(skb) + ihl); | |
4b048d6d TH |
175 | inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, |
176 | true); | |
b4219952 HX |
177 | break; |
178 | } | |
179 | case IPPROTO_UDP: | |
180 | { | |
181 | struct udphdr *udph; | |
182 | ||
36d12690 | 183 | if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) || |
3697649f | 184 | skb_try_make_writable(skb, ihl + sizeof(*udph) + noff)) |
b4219952 HX |
185 | goto drop; |
186 | ||
187 | udph = (void *)(skb_network_header(skb) + ihl); | |
188 | if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) { | |
be0ea7d5 | 189 | inet_proto_csum_replace4(&udph->check, skb, addr, |
4b048d6d | 190 | new_addr, true); |
b4219952 HX |
191 | if (!udph->check) |
192 | udph->check = CSUM_MANGLED_0; | |
193 | } | |
194 | break; | |
195 | } | |
196 | case IPPROTO_ICMP: | |
197 | { | |
198 | struct icmphdr *icmph; | |
199 | ||
36d12690 | 200 | if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff)) |
b4219952 HX |
201 | goto drop; |
202 | ||
203 | icmph = (void *)(skb_network_header(skb) + ihl); | |
204 | ||
54074f1d | 205 | if (!icmp_is_err(icmph->type)) |
b4219952 HX |
206 | break; |
207 | ||
36d12690 CG |
208 | if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) + |
209 | noff)) | |
70c2efa5 CG |
210 | goto drop; |
211 | ||
072d79a3 | 212 | icmph = (void *)(skb_network_header(skb) + ihl); |
b4219952 HX |
213 | iph = (void *)(icmph + 1); |
214 | if (egress) | |
215 | addr = iph->daddr; | |
216 | else | |
217 | addr = iph->saddr; | |
218 | ||
219 | if ((old_addr ^ addr) & mask) | |
220 | break; | |
221 | ||
3697649f DB |
222 | if (skb_try_make_writable(skb, ihl + sizeof(*icmph) + |
223 | sizeof(*iph) + noff)) | |
b4219952 HX |
224 | goto drop; |
225 | ||
226 | icmph = (void *)(skb_network_header(skb) + ihl); | |
227 | iph = (void *)(icmph + 1); | |
228 | ||
229 | new_addr &= mask; | |
230 | new_addr |= addr & ~mask; | |
231 | ||
232 | /* XXX Fix up the inner checksums. */ | |
233 | if (egress) | |
234 | iph->daddr = new_addr; | |
235 | else | |
236 | iph->saddr = new_addr; | |
237 | ||
be0ea7d5 | 238 | inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr, |
4b048d6d | 239 | false); |
b4219952 HX |
240 | break; |
241 | } | |
242 | default: | |
243 | break; | |
244 | } | |
245 | ||
33c29dde | 246 | out: |
b4219952 HX |
247 | return action; |
248 | ||
249 | drop: | |
250 | spin_lock(&p->tcf_lock); | |
251 | p->tcf_qstats.drops++; | |
252 | spin_unlock(&p->tcf_lock); | |
253 | return TC_ACT_SHOT; | |
254 | } | |
255 | ||
256 | static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a, | |
257 | int bind, int ref) | |
258 | { | |
259 | unsigned char *b = skb_tail_pointer(skb); | |
a85a970a | 260 | struct tcf_nat *p = to_tcf_nat(a); |
1c40be12 | 261 | struct tc_nat opt = { |
1c40be12 | 262 | .index = p->tcf_index, |
036bb443 VB |
263 | .refcnt = refcount_read(&p->tcf_refcnt) - ref, |
264 | .bindcnt = atomic_read(&p->tcf_bindcnt) - bind, | |
1c40be12 | 265 | }; |
b4219952 | 266 | struct tcf_t t; |
b4219952 | 267 | |
f20a4d01 VB |
268 | spin_lock_bh(&p->tcf_lock); |
269 | opt.old_addr = p->old_addr; | |
270 | opt.new_addr = p->new_addr; | |
271 | opt.mask = p->mask; | |
272 | opt.flags = p->flags; | |
273 | opt.action = p->tcf_action; | |
274 | ||
1b34ec43 DM |
275 | if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt)) |
276 | goto nla_put_failure; | |
48d8ee16 JHS |
277 | |
278 | tcf_tm_dump(&t, &p->tcf_tm); | |
9854518e | 279 | if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD)) |
1b34ec43 | 280 | goto nla_put_failure; |
f20a4d01 | 281 | spin_unlock_bh(&p->tcf_lock); |
b4219952 | 282 | |
b4219952 HX |
283 | return skb->len; |
284 | ||
7ba699c6 | 285 | nla_put_failure: |
f20a4d01 | 286 | spin_unlock_bh(&p->tcf_lock); |
b4219952 | 287 | nlmsg_trim(skb, b); |
b4219952 HX |
288 | return -1; |
289 | } | |
290 | ||
291 | static struct tc_action_ops act_nat_ops = { | |
292 | .kind = "nat", | |
eddd2cf1 | 293 | .id = TCA_ID_NAT, |
b4219952 | 294 | .owner = THIS_MODULE, |
0390514f | 295 | .act = tcf_nat_act, |
b4219952 | 296 | .dump = tcf_nat_dump, |
b4219952 | 297 | .init = tcf_nat_init, |
a85a970a | 298 | .size = sizeof(struct tcf_nat), |
ddf97ccd WC |
299 | }; |
300 | ||
301 | static __net_init int nat_init_net(struct net *net) | |
302 | { | |
acd0a7ab | 303 | struct tc_action_net *tn = net_generic(net, act_nat_ops.net_id); |
ddf97ccd | 304 | |
981471bd | 305 | return tc_action_net_init(net, tn, &act_nat_ops); |
ddf97ccd WC |
306 | } |
307 | ||
039af9c6 | 308 | static void __net_exit nat_exit_net(struct list_head *net_list) |
ddf97ccd | 309 | { |
acd0a7ab | 310 | tc_action_net_exit(net_list, act_nat_ops.net_id); |
ddf97ccd WC |
311 | } |
312 | ||
313 | static struct pernet_operations nat_net_ops = { | |
314 | .init = nat_init_net, | |
039af9c6 | 315 | .exit_batch = nat_exit_net, |
acd0a7ab | 316 | .id = &act_nat_ops.net_id, |
ddf97ccd | 317 | .size = sizeof(struct tc_action_net), |
b4219952 HX |
318 | }; |
319 | ||
320 | MODULE_DESCRIPTION("Stateless NAT actions"); | |
321 | MODULE_LICENSE("GPL"); | |
322 | ||
323 | static int __init nat_init_module(void) | |
324 | { | |
ddf97ccd | 325 | return tcf_register_action(&act_nat_ops, &nat_net_ops); |
b4219952 HX |
326 | } |
327 | ||
328 | static void __exit nat_cleanup_module(void) | |
329 | { | |
ddf97ccd | 330 | tcf_unregister_action(&act_nat_ops, &nat_net_ops); |
b4219952 HX |
331 | } |
332 | ||
333 | module_init(nat_init_module); | |
334 | module_exit(nat_cleanup_module); |