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>
24 #include <net/pkt_cls.h>
27 #include <net/netlink.h>
28 #include <net/tc_act/tc_nat.h>
33 static unsigned int nat_net_id
;
34 static struct tc_action_ops act_nat_ops
;
36 static const struct nla_policy nat_policy
[TCA_NAT_MAX
+ 1] = {
37 [TCA_NAT_PARMS
] = { .len
= sizeof(struct tc_nat
) },
40 static 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 struct netlink_ext_ack
*extack
)
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
;
55 err
= nla_parse_nested(tb
, TCA_NAT_MAX
, nla
, nat_policy
, NULL
);
59 if (tb
[TCA_NAT_PARMS
] == NULL
)
61 parm
= nla_data(tb
[TCA_NAT_PARMS
]);
63 err
= tcf_idr_check_alloc(tn
, &parm
->index
, a
, bind
);
65 ret
= tcf_idr_create(tn
, parm
->index
, est
, a
,
66 &act_nat_ops
, bind
, false);
68 tcf_idr_cleanup(tn
, parm
->index
);
76 tcf_idr_release(*a
, bind
);
82 err
= tcf_action_check_ctrlact(parm
->action
, tp
, &goto_ch
, extack
);
87 spin_lock_bh(&p
->tcf_lock
);
88 p
->old_addr
= parm
->old_addr
;
89 p
->new_addr
= parm
->new_addr
;
91 p
->flags
= parm
->flags
;
93 goto_ch
= tcf_action_set_ctrlact(*a
, parm
->action
, goto_ch
);
94 spin_unlock_bh(&p
->tcf_lock
);
96 tcf_chain_put_by_act(goto_ch
);
98 if (ret
== ACT_P_CREATED
)
99 tcf_idr_insert(tn
, *a
);
103 tcf_idr_release(*a
, bind
);
107 static int tcf_nat_act(struct sk_buff
*skb
, const struct tc_action
*a
,
108 struct tcf_result
*res
)
110 struct tcf_nat
*p
= to_tcf_nat(a
);
121 spin_lock(&p
->tcf_lock
);
123 tcf_lastuse_update(&p
->tcf_tm
);
124 old_addr
= p
->old_addr
;
125 new_addr
= p
->new_addr
;
127 egress
= p
->flags
& TCA_NAT_FLAG_EGRESS
;
128 action
= p
->tcf_action
;
130 bstats_update(&p
->tcf_bstats
, skb
);
132 spin_unlock(&p
->tcf_lock
);
134 if (unlikely(action
== TC_ACT_SHOT
))
137 noff
= skb_network_offset(skb
);
138 if (!pskb_may_pull(skb
, sizeof(*iph
) + noff
))
148 if (!((old_addr
^ addr
) & mask
)) {
149 if (skb_try_make_writable(skb
, sizeof(*iph
) + noff
))
153 new_addr
|= addr
& ~mask
;
155 /* Rewrite IP header */
158 iph
->saddr
= new_addr
;
160 iph
->daddr
= new_addr
;
162 csum_replace4(&iph
->check
, addr
, new_addr
);
163 } else if ((iph
->frag_off
& htons(IP_OFFSET
)) ||
164 iph
->protocol
!= IPPROTO_ICMP
) {
170 /* It would be nice to share code with stateful NAT. */
171 switch (iph
->frag_off
& htons(IP_OFFSET
) ? 0 : iph
->protocol
) {
176 if (!pskb_may_pull(skb
, ihl
+ sizeof(*tcph
) + noff
) ||
177 skb_try_make_writable(skb
, ihl
+ sizeof(*tcph
) + noff
))
180 tcph
= (void *)(skb_network_header(skb
) + ihl
);
181 inet_proto_csum_replace4(&tcph
->check
, skb
, addr
, new_addr
,
189 if (!pskb_may_pull(skb
, ihl
+ sizeof(*udph
) + noff
) ||
190 skb_try_make_writable(skb
, ihl
+ sizeof(*udph
) + noff
))
193 udph
= (void *)(skb_network_header(skb
) + ihl
);
194 if (udph
->check
|| skb
->ip_summed
== CHECKSUM_PARTIAL
) {
195 inet_proto_csum_replace4(&udph
->check
, skb
, addr
,
198 udph
->check
= CSUM_MANGLED_0
;
204 struct icmphdr
*icmph
;
206 if (!pskb_may_pull(skb
, ihl
+ sizeof(*icmph
) + noff
))
209 icmph
= (void *)(skb_network_header(skb
) + ihl
);
211 if ((icmph
->type
!= ICMP_DEST_UNREACH
) &&
212 (icmph
->type
!= ICMP_TIME_EXCEEDED
) &&
213 (icmph
->type
!= ICMP_PARAMETERPROB
))
216 if (!pskb_may_pull(skb
, ihl
+ sizeof(*icmph
) + sizeof(*iph
) +
220 icmph
= (void *)(skb_network_header(skb
) + ihl
);
221 iph
= (void *)(icmph
+ 1);
227 if ((old_addr
^ addr
) & mask
)
230 if (skb_try_make_writable(skb
, ihl
+ sizeof(*icmph
) +
231 sizeof(*iph
) + noff
))
234 icmph
= (void *)(skb_network_header(skb
) + ihl
);
235 iph
= (void *)(icmph
+ 1);
238 new_addr
|= addr
& ~mask
;
240 /* XXX Fix up the inner checksums. */
242 iph
->daddr
= new_addr
;
244 iph
->saddr
= new_addr
;
246 inet_proto_csum_replace4(&icmph
->checksum
, skb
, addr
, new_addr
,
258 spin_lock(&p
->tcf_lock
);
259 p
->tcf_qstats
.drops
++;
260 spin_unlock(&p
->tcf_lock
);
264 static int tcf_nat_dump(struct sk_buff
*skb
, struct tc_action
*a
,
267 unsigned char *b
= skb_tail_pointer(skb
);
268 struct tcf_nat
*p
= to_tcf_nat(a
);
269 struct tc_nat opt
= {
270 .index
= p
->tcf_index
,
271 .refcnt
= refcount_read(&p
->tcf_refcnt
) - ref
,
272 .bindcnt
= atomic_read(&p
->tcf_bindcnt
) - bind
,
276 spin_lock_bh(&p
->tcf_lock
);
277 opt
.old_addr
= p
->old_addr
;
278 opt
.new_addr
= p
->new_addr
;
280 opt
.flags
= p
->flags
;
281 opt
.action
= p
->tcf_action
;
283 if (nla_put(skb
, TCA_NAT_PARMS
, sizeof(opt
), &opt
))
284 goto nla_put_failure
;
286 tcf_tm_dump(&t
, &p
->tcf_tm
);
287 if (nla_put_64bit(skb
, TCA_NAT_TM
, sizeof(t
), &t
, TCA_NAT_PAD
))
288 goto nla_put_failure
;
289 spin_unlock_bh(&p
->tcf_lock
);
294 spin_unlock_bh(&p
->tcf_lock
);
299 static int tcf_nat_walker(struct net
*net
, struct sk_buff
*skb
,
300 struct netlink_callback
*cb
, int type
,
301 const struct tc_action_ops
*ops
,
302 struct netlink_ext_ack
*extack
)
304 struct tc_action_net
*tn
= net_generic(net
, nat_net_id
);
306 return tcf_generic_walker(tn
, skb
, cb
, type
, ops
, extack
);
309 static int tcf_nat_search(struct net
*net
, struct tc_action
**a
, u32 index
)
311 struct tc_action_net
*tn
= net_generic(net
, nat_net_id
);
313 return tcf_idr_search(tn
, a
, index
);
316 static struct tc_action_ops act_nat_ops
= {
319 .owner
= THIS_MODULE
,
321 .dump
= tcf_nat_dump
,
322 .init
= tcf_nat_init
,
323 .walk
= tcf_nat_walker
,
324 .lookup
= tcf_nat_search
,
325 .size
= sizeof(struct tcf_nat
),
328 static __net_init
int nat_init_net(struct net
*net
)
330 struct tc_action_net
*tn
= net_generic(net
, nat_net_id
);
332 return tc_action_net_init(tn
, &act_nat_ops
);
335 static void __net_exit
nat_exit_net(struct list_head
*net_list
)
337 tc_action_net_exit(net_list
, nat_net_id
);
340 static struct pernet_operations nat_net_ops
= {
341 .init
= nat_init_net
,
342 .exit_batch
= nat_exit_net
,
344 .size
= sizeof(struct tc_action_net
),
347 MODULE_DESCRIPTION("Stateless NAT actions");
348 MODULE_LICENSE("GPL");
350 static int __init
nat_init_module(void)
352 return tcf_register_action(&act_nat_ops
, &nat_net_ops
);
355 static void __exit
nat_cleanup_module(void)
357 tcf_unregister_action(&act_nat_ops
, &nat_net_ops
);
360 module_init(nat_init_module
);
361 module_exit(nat_cleanup_module
);