2 * Stateless NAT actions
4 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
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)
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>
26 #include <net/netlink.h>
27 #include <net/tc_act/tc_nat.h>
32 static unsigned int nat_net_id
;
33 static struct tc_action_ops act_nat_ops
;
35 static const struct nla_policy nat_policy
[TCA_NAT_MAX
+ 1] = {
36 [TCA_NAT_PARMS
] = { .len
= sizeof(struct tc_nat
) },
39 static 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
)
43 struct tc_action_net
*tn
= net_generic(net
, nat_net_id
);
44 struct nlattr
*tb
[TCA_NAT_MAX
+ 1];
52 err
= nla_parse_nested(tb
, TCA_NAT_MAX
, nla
, nat_policy
, NULL
);
56 if (tb
[TCA_NAT_PARMS
] == NULL
)
58 parm
= nla_data(tb
[TCA_NAT_PARMS
]);
60 err
= tcf_idr_check_alloc(tn
, &parm
->index
, a
, bind
);
62 ret
= tcf_idr_create(tn
, parm
->index
, est
, a
,
63 &act_nat_ops
, bind
, false);
65 tcf_idr_cleanup(tn
, parm
->index
);
73 tcf_idr_release(*a
, bind
);
81 spin_lock_bh(&p
->tcf_lock
);
82 p
->old_addr
= parm
->old_addr
;
83 p
->new_addr
= parm
->new_addr
;
85 p
->flags
= parm
->flags
;
87 p
->tcf_action
= parm
->action
;
88 spin_unlock_bh(&p
->tcf_lock
);
90 if (ret
== ACT_P_CREATED
)
91 tcf_idr_insert(tn
, *a
);
96 static int tcf_nat_act(struct sk_buff
*skb
, const struct tc_action
*a
,
97 struct tcf_result
*res
)
99 struct tcf_nat
*p
= to_tcf_nat(a
);
110 spin_lock(&p
->tcf_lock
);
112 tcf_lastuse_update(&p
->tcf_tm
);
113 old_addr
= p
->old_addr
;
114 new_addr
= p
->new_addr
;
116 egress
= p
->flags
& TCA_NAT_FLAG_EGRESS
;
117 action
= p
->tcf_action
;
119 bstats_update(&p
->tcf_bstats
, skb
);
121 spin_unlock(&p
->tcf_lock
);
123 if (unlikely(action
== TC_ACT_SHOT
))
126 noff
= skb_network_offset(skb
);
127 if (!pskb_may_pull(skb
, sizeof(*iph
) + noff
))
137 if (!((old_addr
^ addr
) & mask
)) {
138 if (skb_try_make_writable(skb
, sizeof(*iph
) + noff
))
142 new_addr
|= addr
& ~mask
;
144 /* Rewrite IP header */
147 iph
->saddr
= new_addr
;
149 iph
->daddr
= new_addr
;
151 csum_replace4(&iph
->check
, addr
, new_addr
);
152 } else if ((iph
->frag_off
& htons(IP_OFFSET
)) ||
153 iph
->protocol
!= IPPROTO_ICMP
) {
159 /* It would be nice to share code with stateful NAT. */
160 switch (iph
->frag_off
& htons(IP_OFFSET
) ? 0 : iph
->protocol
) {
165 if (!pskb_may_pull(skb
, ihl
+ sizeof(*tcph
) + noff
) ||
166 skb_try_make_writable(skb
, ihl
+ sizeof(*tcph
) + noff
))
169 tcph
= (void *)(skb_network_header(skb
) + ihl
);
170 inet_proto_csum_replace4(&tcph
->check
, skb
, addr
, new_addr
,
178 if (!pskb_may_pull(skb
, ihl
+ sizeof(*udph
) + noff
) ||
179 skb_try_make_writable(skb
, ihl
+ sizeof(*udph
) + noff
))
182 udph
= (void *)(skb_network_header(skb
) + ihl
);
183 if (udph
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
184 inet_proto_csum_replace4(&udph
->check
, skb
, addr
,
187 udph
->check
= CSUM_MANGLED_0
;
193 struct icmphdr
*icmph
;
195 if (!pskb_may_pull(skb
, ihl
+ sizeof(*icmph
) + noff
))
198 icmph
= (void *)(skb_network_header(skb
) + ihl
);
200 if ((icmph
->type
!= ICMP_DEST_UNREACH
) &&
201 (icmph
->type
!= ICMP_TIME_EXCEEDED
) &&
202 (icmph
->type
!= ICMP_PARAMETERPROB
))
205 if (!pskb_may_pull(skb
, ihl
+ sizeof(*icmph
) + sizeof(*iph
) +
209 icmph
= (void *)(skb_network_header(skb
) + ihl
);
210 iph
= (void *)(icmph
+ 1);
216 if ((old_addr
^ addr
) & mask
)
219 if (skb_try_make_writable(skb
, ihl
+ sizeof(*icmph
) +
220 sizeof(*iph
) + noff
))
223 icmph
= (void *)(skb_network_header(skb
) + ihl
);
224 iph
= (void *)(icmph
+ 1);
227 new_addr
|= addr
& ~mask
;
229 /* XXX Fix up the inner checksums. */
231 iph
->daddr
= new_addr
;
233 iph
->saddr
= new_addr
;
235 inet_proto_csum_replace4(&icmph
->checksum
, skb
, addr
, new_addr
,
247 spin_lock(&p
->tcf_lock
);
248 p
->tcf_qstats
.drops
++;
249 spin_unlock(&p
->tcf_lock
);
253 static int tcf_nat_dump(struct sk_buff
*skb
, struct tc_action
*a
,
256 unsigned char *b
= skb_tail_pointer(skb
);
257 struct tcf_nat
*p
= to_tcf_nat(a
);
258 struct tc_nat opt
= {
259 .old_addr
= p
->old_addr
,
260 .new_addr
= p
->new_addr
,
264 .index
= p
->tcf_index
,
265 .action
= p
->tcf_action
,
266 .refcnt
= refcount_read(&p
->tcf_refcnt
) - ref
,
267 .bindcnt
= atomic_read(&p
->tcf_bindcnt
) - bind
,
271 if (nla_put(skb
, TCA_NAT_PARMS
, sizeof(opt
), &opt
))
272 goto nla_put_failure
;
274 tcf_tm_dump(&t
, &p
->tcf_tm
);
275 if (nla_put_64bit(skb
, TCA_NAT_TM
, sizeof(t
), &t
, TCA_NAT_PAD
))
276 goto nla_put_failure
;
285 static int tcf_nat_walker(struct net
*net
, struct sk_buff
*skb
,
286 struct netlink_callback
*cb
, int type
,
287 const struct tc_action_ops
*ops
,
288 struct netlink_ext_ack
*extack
)
290 struct tc_action_net
*tn
= net_generic(net
, nat_net_id
);
292 return tcf_generic_walker(tn
, skb
, cb
, type
, ops
, extack
);
295 static int tcf_nat_search(struct net
*net
, struct tc_action
**a
, u32 index
,
296 struct netlink_ext_ack
*extack
)
298 struct tc_action_net
*tn
= net_generic(net
, nat_net_id
);
300 return tcf_idr_search(tn
, a
, index
);
303 static struct tc_action_ops act_nat_ops
= {
306 .owner
= THIS_MODULE
,
308 .dump
= tcf_nat_dump
,
309 .init
= tcf_nat_init
,
310 .walk
= tcf_nat_walker
,
311 .lookup
= tcf_nat_search
,
312 .size
= sizeof(struct tcf_nat
),
315 static __net_init
int nat_init_net(struct net
*net
)
317 struct tc_action_net
*tn
= net_generic(net
, nat_net_id
);
319 return tc_action_net_init(tn
, &act_nat_ops
);
322 static void __net_exit
nat_exit_net(struct list_head
*net_list
)
324 tc_action_net_exit(net_list
, nat_net_id
);
327 static struct pernet_operations nat_net_ops
= {
328 .init
= nat_init_net
,
329 .exit_batch
= nat_exit_net
,
331 .size
= sizeof(struct tc_action_net
),
334 MODULE_DESCRIPTION("Stateless NAT actions");
335 MODULE_LICENSE("GPL");
337 static int __init
nat_init_module(void)
339 return tcf_register_action(&act_nat_ops
, &nat_net_ops
);
342 static void __exit
nat_cleanup_module(void)
344 tcf_unregister_action(&act_nat_ops
, &nat_net_ops
);
347 module_init(nat_init_module
);
348 module_exit(nat_cleanup_module
);