Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6/mini2440.git] / net / netfilter / xt_HL.c
blob10e789e2d12af24d5f526e0feea8c6414d7069bc
1 /*
2 * TTL modification target for IP tables
3 * (C) 2000,2005 by Harald Welte <laforge@netfilter.org>
5 * Hop Limit modification target for ip6tables
6 * Maciej Soltysiak <solt@dns.toxicfilms.tv>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/ip.h>
16 #include <linux/ipv6.h>
17 #include <net/checksum.h>
19 #include <linux/netfilter/x_tables.h>
20 #include <linux/netfilter_ipv4/ipt_TTL.h>
21 #include <linux/netfilter_ipv6/ip6t_HL.h>
23 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
24 MODULE_AUTHOR("Maciej Soltysiak <solt@dns.toxicfilms.tv>");
25 MODULE_DESCRIPTION("Xtables: Hoplimit/TTL Limit field modification target");
26 MODULE_LICENSE("GPL");
28 static unsigned int
29 ttl_tg(struct sk_buff *skb, const struct xt_target_param *par)
31 struct iphdr *iph;
32 const struct ipt_TTL_info *info = par->targinfo;
33 int new_ttl;
35 if (!skb_make_writable(skb, skb->len))
36 return NF_DROP;
38 iph = ip_hdr(skb);
40 switch (info->mode) {
41 case IPT_TTL_SET:
42 new_ttl = info->ttl;
43 break;
44 case IPT_TTL_INC:
45 new_ttl = iph->ttl + info->ttl;
46 if (new_ttl > 255)
47 new_ttl = 255;
48 break;
49 case IPT_TTL_DEC:
50 new_ttl = iph->ttl - info->ttl;
51 if (new_ttl < 0)
52 new_ttl = 0;
53 break;
54 default:
55 new_ttl = iph->ttl;
56 break;
59 if (new_ttl != iph->ttl) {
60 csum_replace2(&iph->check, htons(iph->ttl << 8),
61 htons(new_ttl << 8));
62 iph->ttl = new_ttl;
65 return XT_CONTINUE;
68 static unsigned int
69 hl_tg6(struct sk_buff *skb, const struct xt_target_param *par)
71 struct ipv6hdr *ip6h;
72 const struct ip6t_HL_info *info = par->targinfo;
73 int new_hl;
75 if (!skb_make_writable(skb, skb->len))
76 return NF_DROP;
78 ip6h = ipv6_hdr(skb);
80 switch (info->mode) {
81 case IP6T_HL_SET:
82 new_hl = info->hop_limit;
83 break;
84 case IP6T_HL_INC:
85 new_hl = ip6h->hop_limit + info->hop_limit;
86 if (new_hl > 255)
87 new_hl = 255;
88 break;
89 case IP6T_HL_DEC:
90 new_hl = ip6h->hop_limit - info->hop_limit;
91 if (new_hl < 0)
92 new_hl = 0;
93 break;
94 default:
95 new_hl = ip6h->hop_limit;
96 break;
99 ip6h->hop_limit = new_hl;
101 return XT_CONTINUE;
104 static bool ttl_tg_check(const struct xt_tgchk_param *par)
106 const struct ipt_TTL_info *info = par->targinfo;
108 if (info->mode > IPT_TTL_MAXMODE) {
109 printk(KERN_WARNING "ipt_TTL: invalid or unknown Mode %u\n",
110 info->mode);
111 return false;
113 if (info->mode != IPT_TTL_SET && info->ttl == 0)
114 return false;
115 return true;
118 static bool hl_tg6_check(const struct xt_tgchk_param *par)
120 const struct ip6t_HL_info *info = par->targinfo;
122 if (info->mode > IP6T_HL_MAXMODE) {
123 printk(KERN_WARNING "ip6t_HL: invalid or unknown Mode %u\n",
124 info->mode);
125 return false;
127 if (info->mode != IP6T_HL_SET && info->hop_limit == 0) {
128 printk(KERN_WARNING "ip6t_HL: increment/decrement doesn't "
129 "make sense with value 0\n");
130 return false;
132 return true;
135 static struct xt_target hl_tg_reg[] __read_mostly = {
137 .name = "TTL",
138 .revision = 0,
139 .family = NFPROTO_IPV4,
140 .target = ttl_tg,
141 .targetsize = sizeof(struct ipt_TTL_info),
142 .table = "mangle",
143 .checkentry = ttl_tg_check,
144 .me = THIS_MODULE,
147 .name = "HL",
148 .revision = 0,
149 .family = NFPROTO_IPV6,
150 .target = hl_tg6,
151 .targetsize = sizeof(struct ip6t_HL_info),
152 .table = "mangle",
153 .checkentry = hl_tg6_check,
154 .me = THIS_MODULE,
158 static int __init hl_tg_init(void)
160 return xt_register_targets(hl_tg_reg, ARRAY_SIZE(hl_tg_reg));
163 static void __exit hl_tg_exit(void)
165 xt_unregister_targets(hl_tg_reg, ARRAY_SIZE(hl_tg_reg));
168 module_init(hl_tg_init);
169 module_exit(hl_tg_exit);
170 MODULE_ALIAS("ipt_TTL");
171 MODULE_ALIAS("ip6t_HL");