12 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/netfilter.h>
17 #include <linux/rtnetlink.h>
19 #include <linux/slab.h>
21 #include <linux/string.h>
32 #define NAT_TAB_MASK 15
34 static u32 nat_idx_gen;
48 struct tc_action *
a,
int ovr,
int bind)
70 &nat_idx_gen, &nat_hash_info);
83 spin_lock_bh(&p->tcf_lock);
89 p->tcf_action = parm->action;
90 spin_unlock_bh(&p->tcf_lock);
92 if (ret == ACT_P_CREATED)
98 static int tcf_nat_cleanup(
struct tc_action *a,
int bind)
119 spin_lock(&p->tcf_lock);
126 action = p->tcf_action;
128 bstats_update(&p->tcf_bstats, skb);
130 spin_unlock(&p->tcf_lock);
135 noff = skb_network_offset(skb);
136 if (!pskb_may_pull(skb,
sizeof(*iph) + noff))
146 if (!((old_addr ^ addr) & mask)) {
147 if (skb_cloned(skb) &&
148 !skb_clone_writable(skb,
sizeof(*iph) + noff) &&
153 new_addr |= addr & ~mask;
158 iph->
saddr = new_addr;
160 iph->
daddr = new_addr;
162 csum_replace4(&iph->
check, addr, new_addr);
176 if (!pskb_may_pull(skb, ihl +
sizeof(*tcph) + noff) ||
178 !skb_clone_writable(skb, ihl +
sizeof(*tcph) + noff) &&
182 tcph = (
void *)(skb_network_header(skb) + ihl);
190 if (!pskb_may_pull(skb, ihl +
sizeof(*udph) + noff) ||
192 !skb_clone_writable(skb, ihl +
sizeof(*udph) + noff) &&
196 udph = (
void *)(skb_network_header(skb) + ihl);
209 if (!pskb_may_pull(skb, ihl +
sizeof(*icmph) + noff))
212 icmph = (
void *)(skb_network_header(skb) + ihl);
219 if (!pskb_may_pull(skb, ihl +
sizeof(*icmph) +
sizeof(*iph) +
223 icmph = (
void *)(skb_network_header(skb) + ihl);
224 iph = (
void *)(icmph + 1);
230 if ((old_addr ^ addr) &
mask)
233 if (skb_cloned(skb) &&
234 !skb_clone_writable(skb, ihl +
sizeof(*icmph) +
235 sizeof(*iph) + noff) &&
239 icmph = (
void *)(skb_network_header(skb) + ihl);
240 iph = (
void *)(icmph + 1);
243 new_addr |= addr & ~mask;
247 iph->
daddr = new_addr;
249 iph->
saddr = new_addr;
263 spin_lock(&p->tcf_lock);
264 p->tcf_qstats.drops++;
265 spin_unlock(&p->tcf_lock);
269 static int tcf_nat_dump(
struct sk_buff *skb,
struct tc_action *a,
272 unsigned char *
b = skb_tail_pointer(skb);
280 .index = p->tcf_index,
281 .action = p->tcf_action,
282 .refcnt = p->tcf_refcnt - ref,
283 .bindcnt = p->tcf_bindcnt - bind,
288 goto nla_put_failure;
293 goto nla_put_failure;
302 static struct tc_action_ops act_nat_ops = {
304 .hinfo = &nat_hash_info,
306 .capab = TCA_CAP_NONE,
309 .dump = tcf_nat_dump,
310 .cleanup = tcf_nat_cleanup,
312 .init = tcf_nat_init,
319 static int __init nat_init_module(
void)
324 static void __exit nat_cleanup_module(
void)