allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / net / netfilter / xt_HL.c
blob869eb451ea0fb0f5e6701e146cf3d91fab4a388b
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,
30 const struct net_device *in, const struct net_device *out,
31 unsigned int hooknum, const struct xt_target *target,
32 const void *targinfo)
34 struct iphdr *iph;
35 const struct ipt_TTL_info *info = targinfo;
36 int new_ttl;
38 if (!skb_make_writable(skb, skb->len))
39 return NF_DROP;
41 iph = ip_hdr(skb);
43 switch (info->mode) {
44 case IPT_TTL_SET:
45 new_ttl = info->ttl;
46 break;
47 case IPT_TTL_INC:
48 new_ttl = iph->ttl + info->ttl;
49 if (new_ttl > 255)
50 new_ttl = 255;
51 break;
52 case IPT_TTL_DEC:
53 new_ttl = iph->ttl - info->ttl;
54 if (new_ttl < 0)
55 new_ttl = 0;
56 break;
57 default:
58 new_ttl = iph->ttl;
59 break;
62 if (new_ttl != iph->ttl) {
63 nf_csum_replace2(&iph->check, htons(iph->ttl << 8),
64 htons(new_ttl << 8));
65 iph->ttl = new_ttl;
68 return XT_CONTINUE;
71 static unsigned int
72 hl_tg6(struct sk_buff *skb,
73 const struct net_device *in, const struct net_device *out,
74 unsigned int hooknum, const struct xt_target *target,
75 const void *targinfo)
77 struct ipv6hdr *ip6h;
78 const struct ip6t_HL_info *info = targinfo;
79 int new_hl;
81 if (!skb_make_writable(skb, skb->len))
82 return NF_DROP;
84 ip6h = ipv6_hdr(skb);
86 switch (info->mode) {
87 case IP6T_HL_SET:
88 new_hl = info->hop_limit;
89 break;
90 case IP6T_HL_INC:
91 new_hl = ip6h->hop_limit + info->hop_limit;
92 if (new_hl > 255)
93 new_hl = 255;
94 break;
95 case IP6T_HL_DEC:
96 new_hl = ip6h->hop_limit - info->hop_limit;
97 if (new_hl < 0)
98 new_hl = 0;
99 break;
100 default:
101 new_hl = ip6h->hop_limit;
102 break;
105 ip6h->hop_limit = new_hl;
107 return XT_CONTINUE;
110 static int ttl_tg_check(const char *tablename,
111 const void *e,
112 const struct xt_target *target,
113 void *targinfo,
114 unsigned int hook_mask)
116 const struct ipt_TTL_info *info = targinfo;
118 if (info->mode > IPT_TTL_MAXMODE) {
119 printk(KERN_WARNING "ipt_TTL: invalid or unknown Mode %u\n",
120 info->mode);
121 return 0;
123 if (info->mode != IPT_TTL_SET && info->ttl == 0)
124 return 0;
125 return 1;
128 static int hl_tg6_check(const char *tablename,
129 const void *e,
130 const struct xt_target *target,
131 void *targinfo,
132 unsigned int hook_mask)
134 const struct ip6t_HL_info *info = targinfo;
136 if (info->mode > IP6T_HL_MAXMODE) {
137 printk(KERN_WARNING "ip6t_HL: invalid or unknown Mode %u\n",
138 info->mode);
139 return 0;
141 if (info->mode != IP6T_HL_SET && info->hop_limit == 0) {
142 printk(KERN_WARNING "ip6t_HL: increment/decrement doesn't "
143 "make sense with value 0\n");
144 return 0;
146 return 1;
149 static struct xt_target hl_tg_reg[] __read_mostly = {
151 .name = "TTL",
152 .revision = 0,
153 .family = AF_INET,
154 .target = ttl_tg,
155 .targetsize = sizeof(struct ipt_TTL_info),
156 .table = "mangle",
157 .checkentry = ttl_tg_check,
158 .me = THIS_MODULE,
161 .name = "HL",
162 .revision = 0,
163 .family = AF_INET6,
164 .target = hl_tg6,
165 .targetsize = sizeof(struct ip6t_HL_info),
166 .table = "mangle",
167 .checkentry = hl_tg6_check,
168 .me = THIS_MODULE,
172 static int __init hl_tg_init(void)
174 return xt_register_targets(hl_tg_reg, ARRAY_SIZE(hl_tg_reg));
177 static void __exit hl_tg_exit(void)
179 xt_unregister_targets(hl_tg_reg, ARRAY_SIZE(hl_tg_reg));
182 module_init(hl_tg_init);
183 module_exit(hl_tg_exit);
184 MODULE_ALIAS("ipt_TTL");
185 MODULE_ALIAS("ip6t_HL");