]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/ipv4/netfilter/iptable_mangle.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[mirror_ubuntu-zesty-kernel.git] / net / ipv4 / netfilter / iptable_mangle.c
CommitLineData
1da177e4
LT
1/*
2 * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
3 *
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
5 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
6 *
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.
1da177e4 10 */
1da177e4
LT
11#include <linux/module.h>
12#include <linux/netfilter_ipv4/ip_tables.h>
13#include <linux/netdevice.h>
14#include <linux/skbuff.h>
5a0e3ad6 15#include <linux/slab.h>
1da177e4
LT
16#include <net/sock.h>
17#include <net/route.h>
18#include <linux/ip.h>
c9bdd4b5 19#include <net/ip.h>
1da177e4
LT
20
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
23MODULE_DESCRIPTION("iptables mangle table");
24
6e23ae2a
PM
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))
1da177e4 30
35aad0ff 31static const struct xt_table packet_mangler = {
1da177e4
LT
32 .name = "mangle",
33 .valid_hooks = MANGLE_VALID_HOOKS,
1da177e4 34 .me = THIS_MODULE,
f88e6a8a 35 .af = NFPROTO_IPV4,
2b95efe7 36 .priority = NF_IP_PRI_MANGLE,
1da177e4
LT
37};
38
1da177e4 39static unsigned int
fa96a0e2 40ipt_mangle_out(struct sk_buff *skb, const struct net_device *out)
1da177e4
LT
41{
42 unsigned int ret;
eddc9ec5 43 const struct iphdr *iph;
1da177e4 44 u_int8_t tos;
6a19d614 45 __be32 saddr, daddr;
82e91ffe 46 u_int32_t mark;
1da177e4
LT
47
48 /* root is playing with raw sockets. */
3666ed1c
JP
49 if (skb->len < sizeof(struct iphdr) ||
50 ip_hdrlen(skb) < sizeof(struct iphdr))
1da177e4 51 return NF_ACCEPT;
1da177e4
LT
52
53 /* Save things which could affect route */
3db05fea
HX
54 mark = skb->mark;
55 iph = ip_hdr(skb);
eddc9ec5
ACM
56 saddr = iph->saddr;
57 daddr = iph->daddr;
58 tos = iph->tos;
1da177e4 59
fa96a0e2 60 ret = ipt_do_table(skb, NF_INET_LOCAL_OUT, NULL, out,
48dc7865 61 dev_net(out)->ipv4.iptable_mangle);
1da177e4 62 /* Reroute for ANY change. */
eddc9ec5 63 if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) {
3db05fea 64 iph = ip_hdr(skb);
eddc9ec5
ACM
65
66 if (iph->saddr != saddr ||
67 iph->daddr != daddr ||
3db05fea 68 skb->mark != mark ||
eddc9ec5 69 iph->tos != tos)
3db05fea 70 if (ip_route_me_harder(skb, RTN_UNSPEC))
eddc9ec5
ACM
71 ret = NF_DROP;
72 }
1da177e4
LT
73
74 return ret;
75}
76
737535c5
JE
77/* The work comes in here from netfilter.c. */
78static unsigned int
79iptable_mangle_hook(unsigned int hook,
80 struct sk_buff *skb,
81 const struct net_device *in,
82 const struct net_device *out,
83 int (*okfn)(struct sk_buff *))
84{
85 if (hook == NF_INET_LOCAL_OUT)
fa96a0e2 86 return ipt_mangle_out(skb, out);
b2907e50
AD
87 if (hook == NF_INET_POST_ROUTING)
88 return ipt_do_table(skb, hook, in, out,
89 dev_net(out)->ipv4.iptable_mangle);
737535c5
JE
90 /* PREROUTING/INPUT/FORWARD: */
91 return ipt_do_table(skb, hook, in, out,
92 dev_net(in)->ipv4.iptable_mangle);
93}
94
2b95efe7 95static struct nf_hook_ops *mangle_ops __read_mostly;
1da177e4 96
9335f047
AD
97static int __net_init iptable_mangle_net_init(struct net *net)
98{
e3eaa991
JE
99 struct ipt_replace *repl;
100
101 repl = ipt_alloc_initial_table(&packet_mangler);
102 if (repl == NULL)
103 return -ENOMEM;
9335f047 104 net->ipv4.iptable_mangle =
e3eaa991
JE
105 ipt_register_table(net, &packet_mangler, repl);
106 kfree(repl);
9335f047
AD
107 if (IS_ERR(net->ipv4.iptable_mangle))
108 return PTR_ERR(net->ipv4.iptable_mangle);
109 return 0;
110}
111
112static void __net_exit iptable_mangle_net_exit(struct net *net)
113{
f54e9367 114 ipt_unregister_table(net, net->ipv4.iptable_mangle);
9335f047
AD
115}
116
117static struct pernet_operations iptable_mangle_net_ops = {
118 .init = iptable_mangle_net_init,
119 .exit = iptable_mangle_net_exit,
120};
121
65b4b4e8 122static int __init iptable_mangle_init(void)
1da177e4
LT
123{
124 int ret;
125
9335f047
AD
126 ret = register_pernet_subsys(&iptable_mangle_net_ops);
127 if (ret < 0)
128 return ret;
1da177e4
LT
129
130 /* Register hooks */
2b95efe7
JE
131 mangle_ops = xt_hook_link(&packet_mangler, iptable_mangle_hook);
132 if (IS_ERR(mangle_ops)) {
133 ret = PTR_ERR(mangle_ops);
1da177e4 134 goto cleanup_table;
2b95efe7 135 }
1da177e4 136
1da177e4
LT
137 return ret;
138
1da177e4 139 cleanup_table:
9335f047 140 unregister_pernet_subsys(&iptable_mangle_net_ops);
1da177e4
LT
141 return ret;
142}
143
65b4b4e8 144static void __exit iptable_mangle_fini(void)
1da177e4 145{
2b95efe7 146 xt_hook_unlink(&packet_mangler, mangle_ops);
9335f047 147 unregister_pernet_subsys(&iptable_mangle_net_ops);
1da177e4
LT
148}
149
65b4b4e8
AM
150module_init(iptable_mangle_init);
151module_exit(iptable_mangle_fini);