1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
4 *
5 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
6 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
7 */
8 #include <linux/module.h>
9 #include <linux/netfilter_ipv4/ip_tables.h>
10 #include <linux/netdevice.h>
11 #include <linux/skbuff.h>
12 #include <linux/slab.h>
13 #include <net/sock.h>
14 #include <net/route.h>
15 #include <linux/ip.h>
16 #include <net/ip.h>
17
18 MODULE_LICENSE("GPL");
19 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
20 MODULE_DESCRIPTION("iptables mangle table");
21
22 #define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
23 (1 << NF_INET_LOCAL_IN) | \
24 (1 << NF_INET_FORWARD) | \
25 (1 << NF_INET_LOCAL_OUT) | \
26 (1 << NF_INET_POST_ROUTING))
27
28 static int __net_init iptable_mangle_table_init(struct net *net);
29
30 static const struct xt_table packet_mangler = {
31 .name = "mangle",
32 .valid_hooks = MANGLE_VALID_HOOKS,
33 .me = THIS_MODULE,
34 .af = NFPROTO_IPV4,
35 .priority = NF_IP_PRI_MANGLE,
36 .table_init = iptable_mangle_table_init,
37 };
38
39 static unsigned int
ipt_mangle_out(struct sk_buff * skb,const struct nf_hook_state * state)40 ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
41 {
42 unsigned int ret;
43 const struct iphdr *iph;
44 u_int8_t tos;
45 __be32 saddr, daddr;
46 u_int32_t mark;
47 int err;
48
49 /* Save things which could affect route */
50 mark = skb->mark;
51 iph = ip_hdr(skb);
52 saddr = iph->saddr;
53 daddr = iph->daddr;
54 tos = iph->tos;
55
56 ret = ipt_do_table(skb, state, state->net->ipv4.iptable_mangle);
57 /* Reroute for ANY change. */
58 if (ret != NF_DROP && ret != NF_STOLEN) {
59 iph = ip_hdr(skb);
60
61 if (iph->saddr != saddr ||
62 iph->daddr != daddr ||
63 skb->mark != mark ||
64 iph->tos != tos) {
65 err = ip_route_me_harder(state->net, state->sk, skb, RTN_UNSPEC);
66 if (err < 0)
67 ret = NF_DROP_ERR(err);
68 }
69 }
70
71 return ret;
72 }
73
74 /* The work comes in here from netfilter.c. */
75 static unsigned int
iptable_mangle_hook(void * priv,struct sk_buff * skb,const struct nf_hook_state * state)76 iptable_mangle_hook(void *priv,
77 struct sk_buff *skb,
78 const struct nf_hook_state *state)
79 {
80 if (state->hook == NF_INET_LOCAL_OUT)
81 return ipt_mangle_out(skb, state);
82 return ipt_do_table(skb, state, state->net->ipv4.iptable_mangle);
83 }
84
85 static struct nf_hook_ops *mangle_ops __read_mostly;
iptable_mangle_table_init(struct net * net)86 static int __net_init iptable_mangle_table_init(struct net *net)
87 {
88 struct ipt_replace *repl;
89 int ret;
90
91 if (net->ipv4.iptable_mangle)
92 return 0;
93
94 repl = ipt_alloc_initial_table(&packet_mangler);
95 if (repl == NULL)
96 return -ENOMEM;
97 ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops,
98 &net->ipv4.iptable_mangle);
99 kfree(repl);
100 return ret;
101 }
102
iptable_mangle_net_pre_exit(struct net * net)103 static void __net_exit iptable_mangle_net_pre_exit(struct net *net)
104 {
105 if (net->ipv4.iptable_mangle)
106 ipt_unregister_table_pre_exit(net, net->ipv4.iptable_mangle,
107 mangle_ops);
108 }
109
iptable_mangle_net_exit(struct net * net)110 static void __net_exit iptable_mangle_net_exit(struct net *net)
111 {
112 if (!net->ipv4.iptable_mangle)
113 return;
114 ipt_unregister_table_exit(net, net->ipv4.iptable_mangle);
115 net->ipv4.iptable_mangle = NULL;
116 }
117
118 static struct pernet_operations iptable_mangle_net_ops = {
119 .pre_exit = iptable_mangle_net_pre_exit,
120 .exit = iptable_mangle_net_exit,
121 };
122
iptable_mangle_init(void)123 static int __init iptable_mangle_init(void)
124 {
125 int ret;
126
127 mangle_ops = xt_hook_ops_alloc(&packet_mangler, iptable_mangle_hook);
128 if (IS_ERR(mangle_ops)) {
129 ret = PTR_ERR(mangle_ops);
130 return ret;
131 }
132
133 ret = register_pernet_subsys(&iptable_mangle_net_ops);
134 if (ret < 0) {
135 kfree(mangle_ops);
136 return ret;
137 }
138
139 ret = iptable_mangle_table_init(&init_net);
140 if (ret) {
141 unregister_pernet_subsys(&iptable_mangle_net_ops);
142 kfree(mangle_ops);
143 }
144
145 return ret;
146 }
147
iptable_mangle_fini(void)148 static void __exit iptable_mangle_fini(void)
149 {
150 unregister_pernet_subsys(&iptable_mangle_net_ops);
151 kfree(mangle_ops);
152 }
153
154 module_init(iptable_mangle_init);
155 module_exit(iptable_mangle_fini);
156