2 * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
5 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/netfilter_ipv4/ip_tables.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
16 #include <net/route.h>
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
22 MODULE_DESCRIPTION("iptables mangle table");
24 #define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
25 (1 << NF_INET_LOCAL_IN) | \
26 (1 << NF_INET_FORWARD) | \
27 (1 << NF_INET_LOCAL_OUT) | \
28 (1 << NF_INET_POST_ROUTING))
30 static const struct xt_table packet_mangler = {
32 .valid_hooks = MANGLE_VALID_HOOKS,
35 .priority = NF_IP_PRI_MANGLE,
39 ipt_mangle_out(struct sk_buff *skb, const struct net_device *out)
42 const struct iphdr *iph;
47 /* root is playing with raw sockets. */
48 if (skb->len < sizeof(struct iphdr) ||
49 ip_hdrlen(skb) < sizeof(struct iphdr))
52 /* Save things which could affect route */
59 ret = ipt_do_table(skb, NF_INET_LOCAL_OUT, NULL, out,
60 dev_net(out)->ipv4.iptable_mangle);
61 /* Reroute for ANY change. */
62 if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) {
65 if (iph->saddr != saddr ||
66 iph->daddr != daddr ||
69 if (ip_route_me_harder(skb, RTN_UNSPEC))
76 /* The work comes in here from netfilter.c. */
78 iptable_mangle_hook(unsigned int hook,
80 const struct net_device *in,
81 const struct net_device *out,
82 int (*okfn)(struct sk_buff *))
84 if (hook == NF_INET_LOCAL_OUT)
85 return ipt_mangle_out(skb, out);
86 if (hook == NF_INET_POST_ROUTING)
87 return ipt_do_table(skb, hook, in, out,
88 dev_net(out)->ipv4.iptable_mangle);
89 /* PREROUTING/INPUT/FORWARD: */
90 return ipt_do_table(skb, hook, in, out,
91 dev_net(in)->ipv4.iptable_mangle);
94 static struct nf_hook_ops *mangle_ops __read_mostly;
96 static int __net_init iptable_mangle_net_init(struct net *net)
98 struct ipt_replace *repl;
100 repl = ipt_alloc_initial_table(&packet_mangler);
103 net->ipv4.iptable_mangle =
104 ipt_register_table(net, &packet_mangler, repl);
106 if (IS_ERR(net->ipv4.iptable_mangle))
107 return PTR_ERR(net->ipv4.iptable_mangle);
111 static void __net_exit iptable_mangle_net_exit(struct net *net)
113 ipt_unregister_table(net, net->ipv4.iptable_mangle);
116 static struct pernet_operations iptable_mangle_net_ops = {
117 .init = iptable_mangle_net_init,
118 .exit = iptable_mangle_net_exit,
121 static int __init iptable_mangle_init(void)
125 ret = register_pernet_subsys(&iptable_mangle_net_ops);
130 mangle_ops = xt_hook_link(&packet_mangler, iptable_mangle_hook);
131 if (IS_ERR(mangle_ops)) {
132 ret = PTR_ERR(mangle_ops);
139 unregister_pernet_subsys(&iptable_mangle_net_ops);
143 static void __exit iptable_mangle_fini(void)
145 xt_hook_unlink(&packet_mangler, mangle_ops);
146 unregister_pernet_subsys(&iptable_mangle_net_ops);
149 module_init(iptable_mangle_init);
150 module_exit(iptable_mangle_fini);