[NETFILTER]: ipt_TTL: fix checksum update bug
[deliverable/linux.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
15#include <linux/netfilter_ipv4/ip_tables.h>
16#include <linux/netfilter_ipv4/ipt_TTL.h>
17
18MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
19MODULE_DESCRIPTION("IP tables TTL modification module");
20MODULE_LICENSE("GPL");
21
22static unsigned int
c4986734
PM
23ipt_ttl_target(struct sk_buff **pskb,
24 const struct net_device *in, const struct net_device *out,
25 unsigned int hooknum, const struct xt_target *target,
fe1cb108 26 const void *targinfo)
5f2c3b91
HW
27{
28 struct iphdr *iph;
29 const struct ipt_TTL_info *info = targinfo;
5f2c3b91
HW
30 int new_ttl;
31
32 if (!skb_make_writable(pskb, (*pskb)->len))
33 return NF_DROP;
34
35 iph = (*pskb)->nh.iph;
36
37 switch (info->mode) {
38 case IPT_TTL_SET:
39 new_ttl = info->ttl;
40 break;
41 case IPT_TTL_INC:
42 new_ttl = iph->ttl + info->ttl;
43 if (new_ttl > 255)
44 new_ttl = 255;
45 break;
46 case IPT_TTL_DEC:
47 new_ttl = iph->ttl - info->ttl;
48 if (new_ttl < 0)
49 new_ttl = 0;
50 break;
51 default:
52 new_ttl = iph->ttl;
53 break;
54 }
55
56 if (new_ttl != iph->ttl) {
ca39df6c
PM
57 iph->check = nf_csum_update(ntohs((iph->ttl << 8)) ^ 0xFFFF,
58 ntohs(new_ttl << 8),
da878c8e 59 iph->check);
5f2c3b91 60 iph->ttl = new_ttl;
5f2c3b91
HW
61 }
62
63 return IPT_CONTINUE;
64}
65
66static int ipt_ttl_checkentry(const char *tablename,
2e4e6a17 67 const void *e,
c4986734 68 const struct xt_target *target,
5f2c3b91 69 void *targinfo,
5f2c3b91
HW
70 unsigned int hook_mask)
71{
72 struct ipt_TTL_info *info = targinfo;
73
5f2c3b91
HW
74 if (info->mode > IPT_TTL_MAXMODE) {
75 printk(KERN_WARNING "ipt_TTL: invalid or unknown Mode %u\n",
76 info->mode);
77 return 0;
78 }
5f2c3b91
HW
79 if ((info->mode != IPT_TTL_SET) && (info->ttl == 0))
80 return 0;
5f2c3b91
HW
81 return 1;
82}
83
84static struct ipt_target ipt_TTL = {
85 .name = "TTL",
86 .target = ipt_ttl_target,
1d5cd909
PM
87 .targetsize = sizeof(struct ipt_TTL_info),
88 .table = "mangle",
5f2c3b91
HW
89 .checkentry = ipt_ttl_checkentry,
90 .me = THIS_MODULE,
91};
92
65b4b4e8 93static int __init ipt_ttl_init(void)
5f2c3b91
HW
94{
95 return ipt_register_target(&ipt_TTL);
96}
97
65b4b4e8 98static void __exit ipt_ttl_fini(void)
5f2c3b91
HW
99{
100 ipt_unregister_target(&ipt_TTL);
101}
102
65b4b4e8
AM
103module_init(ipt_ttl_init);
104module_exit(ipt_ttl_fini);
This page took 0.254 seconds and 5 git commands to generate.