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];
53 err
= nla_parse_nested(tb
, TCA_NAT_MAX
, nla
, nat_policy
, NULL
);
57 if (tb
[TCA_NAT_PARMS
] == NULL
)
59 parm
= nla_data(tb
[TCA_NAT_PARMS
]);
61 err
= tcf_idr_check_alloc(tn
, &index
, a
, bind
);
63 ret
= tcf_idr_create(tn
, index
, est
, a
,
64 &act_nat_ops
, bind
, false);
66 tcf_idr_cleanup(tn
, index
);
74 tcf_idr_release(*a
, bind
);
82 spin_lock_bh(&p
->tcf_lock
);
83 p
->old_addr
= parm
->old_addr
;
84 p
->new_addr
= parm
->new_addr
;
86 p
->flags
= parm
->flags
;
88 p
->tcf_action
= parm
->action
;
89 spin_unlock_bh(&p
->tcf_lock
);
91 if (ret
== ACT_P_CREATED
)
92 tcf_idr_insert(tn
, *a
);
97 static int tcf_nat_act(struct sk_buff
*skb
, const struct tc_action
*a
,
98 struct tcf_result
*res
)
100 struct tcf_nat
*p
= to_tcf_nat(a
);
111 spin_lock(&p
->tcf_lock
);
113 tcf_lastuse_update(&p
->tcf_tm
);
114 old_addr
= p
->old_addr
;
115 new_addr
= p
->new_addr
;
117 egress
= p
->flags
& TCA_NAT_FLAG_EGRESS
;
118 action
= p
->tcf_action
;
120 bstats_update(&p
->tcf_bstats
, skb
);
122 spin_unlock(&p
->tcf_lock
);
124 if (unlikely(action
== TC_ACT_SHOT
))
127 noff
= skb_network_offset(skb
);
128 if (!pskb_may_pull(skb
, sizeof(*iph
) + noff
))
138 if (!((old_addr
^ addr
) & mask
)) {
139 if (skb_try_make_writable(skb
, sizeof(*iph
) + noff
))
143 new_addr
|= addr
& ~mask
;
145 /* Rewrite IP header */
148 iph
->saddr
= new_addr
;
150 iph
->daddr
= new_addr
;
152 csum_replace4(&iph
->check
, addr
, new_addr
);
153 } else if ((iph
->frag_off
& htons(IP_OFFSET
)) ||
154 iph
->protocol
!= IPPROTO_ICMP
) {
160 /* It would be nice to share code with stateful NAT. */
161 switch (iph
->frag_off
& htons(IP_OFFSET
) ? 0 : iph
->protocol
) {
166 if (!pskb_may_pull(skb
, ihl
+ sizeof(*tcph
) + noff
) ||
167 skb_try_make_writable(skb
, ihl
+ sizeof(*tcph
) + noff
))
170 tcph
= (void *)(skb_network_header(skb
) + ihl
);
171 inet_proto_csum_replace4(&tcph
->check
, skb
, addr
, new_addr
,
179 if (!pskb_may_pull(skb
, ihl
+ sizeof(*udph
) + noff
) ||
180 skb_try_make_writable(skb
, ihl
+ sizeof(*udph
) + noff
))
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
,
188 udph
->check
= CSUM_MANGLED_0
;
194 struct icmphdr
*icmph
;
196 if (!pskb_may_pull(skb
, ihl
+ sizeof(*icmph
) + noff
))
199 icmph
= (void *)(skb_network_header(skb
) + ihl
);
201 if ((icmph
->type
!= ICMP_DEST_UNREACH
) &&
202 (icmph
->type
!= ICMP_TIME_EXCEEDED
) &&
203 (icmph
->type
!= ICMP_PARAMETERPROB
))
206 if (!pskb_may_pull(skb
, ihl
+ sizeof(*icmph
) + sizeof(*iph
) +
210 icmph
= (void *)(skb_network_header(skb
) + ihl
);
211 iph
= (void *)(icmph
+ 1);
217 if ((old_addr
^ addr
) & mask
)
220 if (skb_try_make_writable(skb
, ihl
+ sizeof(*icmph
) +
221 sizeof(*iph
) + noff
))
224 icmph
= (void *)(skb_network_header(skb
) + ihl
);
225 iph
= (void *)(icmph
+ 1);
228 new_addr
|= addr
& ~mask
;
230 /* XXX Fix up the inner checksums. */
232 iph
->daddr
= new_addr
;
234 iph
->saddr
= new_addr
;
236 inet_proto_csum_replace4(&icmph
->checksum
, skb
, addr
, new_addr
,
248 spin_lock(&p
->tcf_lock
);
249 p
->tcf_qstats
.drops
++;
250 spin_unlock(&p
->tcf_lock
);
254 static int tcf_nat_dump(struct sk_buff
*skb
, struct tc_action
*a
,
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
,
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
,
272 if (nla_put(skb
, TCA_NAT_PARMS
, sizeof(opt
), &opt
))
273 goto nla_put_failure
;
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
;
286 static 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
)
291 struct tc_action_net
*tn
= net_generic(net
, nat_net_id
);
293 return tcf_generic_walker(tn
, skb
, cb
, type
, ops
, extack
);
296 static int tcf_nat_search(struct net
*net
, struct tc_action
**a
, u32 index
,
297 struct netlink_ext_ack
*extack
)
299 struct tc_action_net
*tn
= net_generic(net
, nat_net_id
);
301 return tcf_idr_search(tn
, a
, index
);
304 static struct tc_action_ops act_nat_ops
= {
307 .owner
= THIS_MODULE
,
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
),
316 static __net_init
int nat_init_net(struct net
*net
)
318 struct tc_action_net
*tn
= net_generic(net
, nat_net_id
);
320 return tc_action_net_init(net
, tn
, &act_nat_ops
);
323 static void __net_exit
nat_exit_net(struct list_head
*net_list
)
325 tc_action_net_exit(net_list
, nat_net_id
);
328 static struct pernet_operations nat_net_ops
= {
329 .init
= nat_init_net
,
330 .exit_batch
= nat_exit_net
,
332 .size
= sizeof(struct tc_action_net
),
335 MODULE_DESCRIPTION("Stateless NAT actions");
336 MODULE_LICENSE("GPL");
338 static int __init
nat_init_module(void)
340 return tcf_register_action(&act_nat_ops
, &nat_net_ops
);
343 static void __exit
nat_cleanup_module(void)
345 tcf_unregister_action(&act_nat_ops
, &nat_net_ops
);
348 module_init(nat_init_module
);
349 module_exit(nat_cleanup_module
);