]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/ipv4/netfilter/iptable_mangle.c
Merge tag 'efi-urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/mfleming...
[mirror_ubuntu-artful-kernel.git] / net / ipv4 / netfilter / iptable_mangle.c
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.
10 */
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>
16 #include <net/sock.h>
17 #include <net/route.h>
18 #include <linux/ip.h>
19 #include <net/ip.h>
20
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
23 MODULE_DESCRIPTION("iptables mangle table");
24
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))
30
31 static const struct xt_table packet_mangler = {
32 .name = "mangle",
33 .valid_hooks = MANGLE_VALID_HOOKS,
34 .me = THIS_MODULE,
35 .af = NFPROTO_IPV4,
36 .priority = NF_IP_PRI_MANGLE,
37 };
38
39 static unsigned int
40 ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
41 {
42 struct net_device *out = state->out;
43 unsigned int ret;
44 const struct iphdr *iph;
45 u_int8_t tos;
46 __be32 saddr, daddr;
47 u_int32_t mark;
48 int err;
49
50 /* root is playing with raw sockets. */
51 if (skb->len < sizeof(struct iphdr) ||
52 ip_hdrlen(skb) < sizeof(struct iphdr))
53 return NF_ACCEPT;
54
55 /* Save things which could affect route */
56 mark = skb->mark;
57 iph = ip_hdr(skb);
58 saddr = iph->saddr;
59 daddr = iph->daddr;
60 tos = iph->tos;
61
62 ret = ipt_do_table(skb, NF_INET_LOCAL_OUT, state,
63 dev_net(out)->ipv4.iptable_mangle);
64 /* Reroute for ANY change. */
65 if (ret != NF_DROP && ret != NF_STOLEN) {
66 iph = ip_hdr(skb);
67
68 if (iph->saddr != saddr ||
69 iph->daddr != daddr ||
70 skb->mark != mark ||
71 iph->tos != tos) {
72 err = ip_route_me_harder(skb, RTN_UNSPEC);
73 if (err < 0)
74 ret = NF_DROP_ERR(err);
75 }
76 }
77
78 return ret;
79 }
80
81 /* The work comes in here from netfilter.c. */
82 static unsigned int
83 iptable_mangle_hook(const struct nf_hook_ops *ops,
84 struct sk_buff *skb,
85 const struct nf_hook_state *state)
86 {
87 if (ops->hooknum == NF_INET_LOCAL_OUT)
88 return ipt_mangle_out(skb, state);
89 if (ops->hooknum == NF_INET_POST_ROUTING)
90 return ipt_do_table(skb, ops->hooknum, state,
91 dev_net(state->out)->ipv4.iptable_mangle);
92 /* PREROUTING/INPUT/FORWARD: */
93 return ipt_do_table(skb, ops->hooknum, state,
94 dev_net(state->in)->ipv4.iptable_mangle);
95 }
96
97 static struct nf_hook_ops *mangle_ops __read_mostly;
98
99 static int __net_init iptable_mangle_net_init(struct net *net)
100 {
101 struct ipt_replace *repl;
102
103 repl = ipt_alloc_initial_table(&packet_mangler);
104 if (repl == NULL)
105 return -ENOMEM;
106 net->ipv4.iptable_mangle =
107 ipt_register_table(net, &packet_mangler, repl);
108 kfree(repl);
109 return PTR_ERR_OR_ZERO(net->ipv4.iptable_mangle);
110 }
111
112 static void __net_exit iptable_mangle_net_exit(struct net *net)
113 {
114 ipt_unregister_table(net, net->ipv4.iptable_mangle);
115 }
116
117 static struct pernet_operations iptable_mangle_net_ops = {
118 .init = iptable_mangle_net_init,
119 .exit = iptable_mangle_net_exit,
120 };
121
122 static int __init iptable_mangle_init(void)
123 {
124 int ret;
125
126 ret = register_pernet_subsys(&iptable_mangle_net_ops);
127 if (ret < 0)
128 return ret;
129
130 /* Register hooks */
131 mangle_ops = xt_hook_link(&packet_mangler, iptable_mangle_hook);
132 if (IS_ERR(mangle_ops)) {
133 ret = PTR_ERR(mangle_ops);
134 unregister_pernet_subsys(&iptable_mangle_net_ops);
135 }
136
137 return ret;
138 }
139
140 static void __exit iptable_mangle_fini(void)
141 {
142 xt_hook_unlink(&packet_mangler, mangle_ops);
143 unregister_pernet_subsys(&iptable_mangle_net_ops);
144 }
145
146 module_init(iptable_mangle_init);
147 module_exit(iptable_mangle_fini);