NETFILTER: remove unnecessary goto statement for error recovery
[tomato.git] / release / src-rt / linux / linux-2.6 / net / ipv4 / netfilter / iptable_mangle.c
blobce7b6bac71cd54fe6a3649446a9d740b5d1f8f6b
1 /*
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>
15 #include <net/sock.h>
16 #include <net/route.h>
17 #include <linux/ip.h>
18 #include <net/ip.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_IP_PRE_ROUTING) | \
25 (1 << NF_IP_LOCAL_IN) | \
26 (1 << NF_IP_FORWARD) | \
27 (1 << NF_IP_LOCAL_OUT) | \
28 (1 << NF_IP_POST_ROUTING))
30 /* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
31 static struct
33 struct ipt_replace repl;
34 struct ipt_standard entries[5];
35 struct ipt_error term;
36 } initial_table __initdata = {
37 .repl = {
38 .name = "mangle",
39 .valid_hooks = MANGLE_VALID_HOOKS,
40 .num_entries = 6,
41 .size = sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error),
42 .hook_entry = {
43 [NF_IP_PRE_ROUTING] = 0,
44 [NF_IP_LOCAL_IN] = sizeof(struct ipt_standard),
45 [NF_IP_FORWARD] = sizeof(struct ipt_standard) * 2,
46 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
47 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard) * 4,
49 .underflow = {
50 [NF_IP_PRE_ROUTING] = 0,
51 [NF_IP_LOCAL_IN] = sizeof(struct ipt_standard),
52 [NF_IP_FORWARD] = sizeof(struct ipt_standard) * 2,
53 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
54 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard) * 4,
57 .entries = {
58 IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */
59 IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */
60 IPT_STANDARD_INIT(NF_ACCEPT), /* FORWARD */
61 IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
62 IPT_STANDARD_INIT(NF_ACCEPT), /* POST_ROUTING */
64 .term = IPT_ERROR_INIT, /* ERROR */
67 static struct xt_table packet_mangler = {
68 .name = "mangle",
69 .valid_hooks = MANGLE_VALID_HOOKS,
70 .me = THIS_MODULE,
71 .af = AF_INET,
74 /* The work comes in here from netfilter.c. */
75 static unsigned int
76 ipt_route_hook(unsigned int hook,
77 struct sk_buff *skb,
78 const struct net_device *in,
79 const struct net_device *out,
80 int (*okfn)(struct sk_buff *))
82 return ipt_do_table(skb, hook, in, out, &packet_mangler);
85 static unsigned int
86 ipt_local_hook(unsigned int hook,
87 struct sk_buff *skb,
88 const struct net_device *in,
89 const struct net_device *out,
90 int (*okfn)(struct sk_buff *))
92 unsigned int ret;
93 const struct iphdr *iph;
94 u_int8_t tos;
95 __be32 saddr, daddr;
96 u_int32_t mark;
98 /* root is playing with raw sockets. */
99 if (skb->len < sizeof(struct iphdr)
100 || ip_hdrlen(skb) < sizeof(struct iphdr)) {
101 if (net_ratelimit())
102 printk("iptable_mangle: ignoring short SOCK_RAW "
103 "packet.\n");
104 return NF_ACCEPT;
107 /* Save things which could affect route */
108 mark = skb->mark;
109 iph = ip_hdr(skb);
110 saddr = iph->saddr;
111 daddr = iph->daddr;
112 tos = iph->tos;
114 ret = ipt_do_table(skb, hook, in, out, &packet_mangler);
115 /* Reroute for ANY change. */
116 if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) {
117 iph = ip_hdr(skb);
119 if (iph->saddr != saddr ||
120 iph->daddr != daddr ||
121 skb->mark != mark ||
122 iph->tos != tos)
123 if (ip_route_me_harder(skb, RTN_UNSPEC))
124 ret = NF_DROP;
127 return ret;
130 static struct nf_hook_ops ipt_ops[] = {
132 .hook = ipt_route_hook,
133 .owner = THIS_MODULE,
134 .pf = PF_INET,
135 .hooknum = NF_IP_PRE_ROUTING,
136 .priority = NF_IP_PRI_MANGLE,
139 .hook = ipt_route_hook,
140 .owner = THIS_MODULE,
141 .pf = PF_INET,
142 .hooknum = NF_IP_LOCAL_IN,
143 .priority = NF_IP_PRI_MANGLE,
146 .hook = ipt_route_hook,
147 .owner = THIS_MODULE,
148 .pf = PF_INET,
149 .hooknum = NF_IP_FORWARD,
150 .priority = NF_IP_PRI_MANGLE,
153 .hook = ipt_local_hook,
154 .owner = THIS_MODULE,
155 .pf = PF_INET,
156 .hooknum = NF_IP_LOCAL_OUT,
157 .priority = NF_IP_PRI_MANGLE,
160 .hook = ipt_route_hook,
161 .owner = THIS_MODULE,
162 .pf = PF_INET,
163 .hooknum = NF_IP_POST_ROUTING,
164 .priority = NF_IP_PRI_MANGLE,
168 static int __init iptable_mangle_init(void)
170 int ret;
172 /* Register table */
173 ret = ipt_register_table(&packet_mangler, &initial_table.repl);
174 if (ret < 0)
175 return ret;
177 /* Register hooks */
178 ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
179 if (ret < 0) {
180 ipt_unregister_table(&packet_mangler);
182 return ret;
185 static void __exit iptable_mangle_fini(void)
187 nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
188 ipt_unregister_table(&packet_mangler);
191 module_init(iptable_mangle_init);
192 module_exit(iptable_mangle_fini);