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
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>
15 #include <linux/slab.h>
17 #include <net/route.h>
21 MODULE_LICENSE("GPL");
23 MODULE_DESCRIPTION("iptables mangle table");
25 #define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
26 (1 << NF_INET_LOCAL_IN) | \
27 (1 << NF_INET_FORWARD) | \
28 (1 << NF_INET_LOCAL_OUT) | \
29 (1 << NF_INET_POST_ROUTING))
31 static int __net_init iptable_mangle_table_init(struct net *net);
33 static const struct xt_table packet_mangler = {
35 .valid_hooks = MANGLE_VALID_HOOKS,
38 .priority = NF_IP_PRI_MANGLE,
39 .table_init = iptable_mangle_table_init,
43 ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
46 const struct iphdr *iph;
52 /* Save things which could affect route */
59 ret = ipt_do_table(skb, state, state->net->ipv4.iptable_mangle);
60 /* Reroute for ANY change. */
61 if (ret != NF_DROP && ret != NF_STOLEN) {
64 if (iph->saddr != saddr ||
65 iph->daddr != daddr ||
68 err = ip_route_me_harder(state->net, skb, RTN_UNSPEC);
70 ret = NF_DROP_ERR(err);
77 /* The work comes in here from netfilter.c. */
79 iptable_mangle_hook(void *priv,
81 const struct nf_hook_state *state)
83 if (state->hook == NF_INET_LOCAL_OUT)
84 return ipt_mangle_out(skb, state);
85 return ipt_do_table(skb, state, state->net->ipv4.iptable_mangle);
88 static struct nf_hook_ops *mangle_ops __read_mostly;
89 static int __net_init iptable_mangle_table_init(struct net *net)
91 struct ipt_replace *repl;
94 if (net->ipv4.iptable_mangle)
97 repl = ipt_alloc_initial_table(&packet_mangler);
100 ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops,
101 &net->ipv4.iptable_mangle);
106 static void __net_exit iptable_mangle_net_exit(struct net *net)
108 if (!net->ipv4.iptable_mangle)
110 ipt_unregister_table(net, net->ipv4.iptable_mangle, mangle_ops);
111 net->ipv4.iptable_mangle = NULL;
114 static struct pernet_operations iptable_mangle_net_ops = {
115 .exit = iptable_mangle_net_exit,
118 static int __init iptable_mangle_init(void)
122 mangle_ops = xt_hook_ops_alloc(&packet_mangler, iptable_mangle_hook);
123 if (IS_ERR(mangle_ops)) {
124 ret = PTR_ERR(mangle_ops);
128 ret = register_pernet_subsys(&iptable_mangle_net_ops);
134 ret = iptable_mangle_table_init(&init_net);
136 unregister_pernet_subsys(&iptable_mangle_net_ops);
143 static void __exit iptable_mangle_fini(void)
145 unregister_pernet_subsys(&iptable_mangle_net_ops);
149 module_init(iptable_mangle_init);
150 module_exit(iptable_mangle_fini);