netfilter: xtables: move extension arguments into compound structure (4/6)
[linux-2.6-block.git] / net / ipv4 / netfilter / ipt_TTL.c
CommitLineData
5f2c3b91
HW
1/* TTL modification target for IP tables
2 * (C) 2000,2005 by Harald Welte <laforge@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 */
9
10#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/ip.h>
13#include <net/checksum.h>
14
6709dbbb 15#include <linux/netfilter/x_tables.h>
5f2c3b91
HW
16#include <linux/netfilter_ipv4/ipt_TTL.h>
17
18MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
2ae15b64 19MODULE_DESCRIPTION("Xtables: IPv4 TTL field modification target");
5f2c3b91
HW
20MODULE_LICENSE("GPL");
21
e905a9ed 22static unsigned int
7eb35586 23ttl_tg(struct sk_buff *skb, const struct xt_target_param *par)
5f2c3b91
HW
24{
25 struct iphdr *iph;
7eb35586 26 const struct ipt_TTL_info *info = par->targinfo;
5f2c3b91
HW
27 int new_ttl;
28
3db05fea 29 if (!skb_make_writable(skb, skb->len))
5f2c3b91
HW
30 return NF_DROP;
31
3db05fea 32 iph = ip_hdr(skb);
5f2c3b91
HW
33
34 switch (info->mode) {
35 case IPT_TTL_SET:
36 new_ttl = info->ttl;
37 break;
38 case IPT_TTL_INC:
39 new_ttl = iph->ttl + info->ttl;
40 if (new_ttl > 255)
41 new_ttl = 255;
42 break;
43 case IPT_TTL_DEC:
44 new_ttl = iph->ttl - info->ttl;
45 if (new_ttl < 0)
46 new_ttl = 0;
47 break;
48 default:
49 new_ttl = iph->ttl;
50 break;
51 }
52
53 if (new_ttl != iph->ttl) {
be0ea7d5
PM
54 csum_replace2(&iph->check, htons(iph->ttl << 8),
55 htons(new_ttl << 8));
5f2c3b91 56 iph->ttl = new_ttl;
5f2c3b91
HW
57 }
58
6709dbbb 59 return XT_CONTINUE;
5f2c3b91
HW
60}
61
d3c5ee6d
JE
62static bool
63ttl_tg_check(const char *tablename, const void *e,
64 const struct xt_target *target, void *targinfo,
65 unsigned int hook_mask)
5f2c3b91 66{
a47362a2 67 const struct ipt_TTL_info *info = targinfo;
5f2c3b91 68
5f2c3b91 69 if (info->mode > IPT_TTL_MAXMODE) {
e905a9ed 70 printk(KERN_WARNING "ipt_TTL: invalid or unknown Mode %u\n",
5f2c3b91 71 info->mode);
e1931b78 72 return false;
5f2c3b91 73 }
7c4e36bc 74 if (info->mode != IPT_TTL_SET && info->ttl == 0)
e1931b78
JE
75 return false;
76 return true;
5f2c3b91
HW
77}
78
d3c5ee6d 79static struct xt_target ttl_tg_reg __read_mostly = {
5f2c3b91 80 .name = "TTL",
ee999d8b 81 .family = NFPROTO_IPV4,
d3c5ee6d 82 .target = ttl_tg,
1d5cd909
PM
83 .targetsize = sizeof(struct ipt_TTL_info),
84 .table = "mangle",
d3c5ee6d 85 .checkentry = ttl_tg_check,
5f2c3b91
HW
86 .me = THIS_MODULE,
87};
88
d3c5ee6d 89static int __init ttl_tg_init(void)
5f2c3b91 90{
d3c5ee6d 91 return xt_register_target(&ttl_tg_reg);
5f2c3b91
HW
92}
93
d3c5ee6d 94static void __exit ttl_tg_exit(void)
5f2c3b91 95{
d3c5ee6d 96 xt_unregister_target(&ttl_tg_reg);
5f2c3b91
HW
97}
98
d3c5ee6d
JE
99module_init(ttl_tg_init);
100module_exit(ttl_tg_exit);