2 * IPv6 packet mangling table, a port of the IPv4 mangle table to IPv6
4 * Copyright (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
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 * Extended to all five netfilter hooks by Brad Chapman & Harald Welte
13 #include <linux/module.h>
14 #include <linux/netfilter_ipv6/ip6_tables.h>
16 MODULE_LICENSE("GPL");
17 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
18 MODULE_DESCRIPTION("ip6tables mangle table");
20 #define MANGLE_VALID_HOOKS ((1 << NF_IP6_PRE_ROUTING) | \
21 (1 << NF_IP6_LOCAL_IN) | \
22 (1 << NF_IP6_FORWARD) | \
23 (1 << NF_IP6_LOCAL_OUT) | \
24 (1 << NF_IP6_POST_ROUTING))
27 #define DEBUGP(x, args...) printk(KERN_DEBUG x, ## args)
29 #define DEBUGP(x, args...)
34 struct ip6t_replace repl
;
35 struct ip6t_standard entries
[5];
36 struct ip6t_error term
;
37 } initial_table __initdata
38 = { { "mangle", MANGLE_VALID_HOOKS
, 6,
39 sizeof(struct ip6t_standard
) * 5 + sizeof(struct ip6t_error
),
40 { [NF_IP6_PRE_ROUTING
] = 0,
41 [NF_IP6_LOCAL_IN
] = sizeof(struct ip6t_standard
),
42 [NF_IP6_FORWARD
] = sizeof(struct ip6t_standard
) * 2,
43 [NF_IP6_LOCAL_OUT
] = sizeof(struct ip6t_standard
) * 3,
44 [NF_IP6_POST_ROUTING
] = sizeof(struct ip6t_standard
) * 4},
45 { [NF_IP6_PRE_ROUTING
] = 0,
46 [NF_IP6_LOCAL_IN
] = sizeof(struct ip6t_standard
),
47 [NF_IP6_FORWARD
] = sizeof(struct ip6t_standard
) * 2,
48 [NF_IP6_LOCAL_OUT
] = sizeof(struct ip6t_standard
) * 3,
49 [NF_IP6_POST_ROUTING
] = sizeof(struct ip6t_standard
) * 4},
53 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
55 sizeof(struct ip6t_entry
),
56 sizeof(struct ip6t_standard
),
58 { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target
)), "" } }, { } },
61 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
63 sizeof(struct ip6t_entry
),
64 sizeof(struct ip6t_standard
),
66 { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target
)), "" } }, { } },
69 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
71 sizeof(struct ip6t_entry
),
72 sizeof(struct ip6t_standard
),
74 { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target
)), "" } }, { } },
77 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
79 sizeof(struct ip6t_entry
),
80 sizeof(struct ip6t_standard
),
82 { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target
)), "" } }, { } },
85 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
87 sizeof(struct ip6t_entry
),
88 sizeof(struct ip6t_standard
),
90 { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target
)), "" } }, { } },
94 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
96 sizeof(struct ip6t_entry
),
97 sizeof(struct ip6t_error
),
99 { { { { IP6T_ALIGN(sizeof(struct ip6t_error_target
)), IP6T_ERROR_TARGET
} },
106 static struct xt_table packet_mangler
= {
108 .valid_hooks
= MANGLE_VALID_HOOKS
,
109 .lock
= RW_LOCK_UNLOCKED
,
114 /* The work comes in here from netfilter.c. */
116 ip6t_route_hook(unsigned int hook
,
117 struct sk_buff
**pskb
,
118 const struct net_device
*in
,
119 const struct net_device
*out
,
120 int (*okfn
)(struct sk_buff
*))
122 return ip6t_do_table(pskb
, hook
, in
, out
, &packet_mangler
);
126 ip6t_local_hook(unsigned int hook
,
127 struct sk_buff
**pskb
,
128 const struct net_device
*in
,
129 const struct net_device
*out
,
130 int (*okfn
)(struct sk_buff
*))
134 struct in6_addr saddr
, daddr
;
136 u_int32_t flowlabel
, mark
;
139 /* root is playing with raw sockets. */
140 if ((*pskb
)->len
< sizeof(struct iphdr
)
141 || (*pskb
)->nh
.iph
->ihl
* 4 < sizeof(struct iphdr
)) {
143 printk("ip6t_hook: happy cracking.\n");
148 /* save source/dest address, mark, hoplimit, flowlabel, priority, */
149 memcpy(&saddr
, &(*pskb
)->nh
.ipv6h
->saddr
, sizeof(saddr
));
150 memcpy(&daddr
, &(*pskb
)->nh
.ipv6h
->daddr
, sizeof(daddr
));
151 mark
= (*pskb
)->mark
;
152 hop_limit
= (*pskb
)->nh
.ipv6h
->hop_limit
;
154 /* flowlabel and prio (includes version, which shouldn't change either */
155 flowlabel
= *((u_int32_t
*) (*pskb
)->nh
.ipv6h
);
157 ret
= ip6t_do_table(pskb
, hook
, in
, out
, &packet_mangler
);
159 if (ret
!= NF_DROP
&& ret
!= NF_STOLEN
160 && (memcmp(&(*pskb
)->nh
.ipv6h
->saddr
, &saddr
, sizeof(saddr
))
161 || memcmp(&(*pskb
)->nh
.ipv6h
->daddr
, &daddr
, sizeof(daddr
))
162 || (*pskb
)->mark
!= mark
163 || (*pskb
)->nh
.ipv6h
->hop_limit
!= hop_limit
))
164 return ip6_route_me_harder(*pskb
) == 0 ? ret
: NF_DROP
;
169 static struct nf_hook_ops ip6t_ops
[] = {
171 .hook
= ip6t_route_hook
,
172 .owner
= THIS_MODULE
,
174 .hooknum
= NF_IP6_PRE_ROUTING
,
175 .priority
= NF_IP6_PRI_MANGLE
,
178 .hook
= ip6t_local_hook
,
179 .owner
= THIS_MODULE
,
181 .hooknum
= NF_IP6_LOCAL_IN
,
182 .priority
= NF_IP6_PRI_MANGLE
,
185 .hook
= ip6t_route_hook
,
186 .owner
= THIS_MODULE
,
188 .hooknum
= NF_IP6_FORWARD
,
189 .priority
= NF_IP6_PRI_MANGLE
,
192 .hook
= ip6t_local_hook
,
193 .owner
= THIS_MODULE
,
195 .hooknum
= NF_IP6_LOCAL_OUT
,
196 .priority
= NF_IP6_PRI_MANGLE
,
199 .hook
= ip6t_route_hook
,
200 .owner
= THIS_MODULE
,
202 .hooknum
= NF_IP6_POST_ROUTING
,
203 .priority
= NF_IP6_PRI_MANGLE
,
207 static int __init
ip6table_mangle_init(void)
212 ret
= ip6t_register_table(&packet_mangler
, &initial_table
.repl
);
217 ret
= nf_register_hooks(ip6t_ops
, ARRAY_SIZE(ip6t_ops
));
224 ip6t_unregister_table(&packet_mangler
);
228 static void __exit
ip6table_mangle_fini(void)
230 nf_unregister_hooks(ip6t_ops
, ARRAY_SIZE(ip6t_ops
));
231 ip6t_unregister_table(&packet_mangler
);
234 module_init(ip6table_mangle_init
);
235 module_exit(ip6table_mangle_fini
);