blob: b8eff8ee69b1fb71ed044f47bae88f564c78d61a [file] [log] [blame]
Harald Welte0ac4f892005-08-27 22:37:30 -07001/*
2 * Hop Limit modification target for ip6tables
3 * Maciej Soltysiak <solt@dns.toxicfilms.tv>
4 * Based on HW's TTL module
5 *
6 * This software is distributed under the terms of GNU GPL
7 */
8
9#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <linux/ip.h>
12
13#include <linux/netfilter_ipv6/ip6_tables.h>
14#include <linux/netfilter_ipv6/ip6t_HL.h>
15
16MODULE_AUTHOR("Maciej Soltysiak <solt@dns.toxicfilms.tv>");
17MODULE_DESCRIPTION("IP tables Hop Limit modification module");
18MODULE_LICENSE("GPL");
19
20static unsigned int ip6t_hl_target(struct sk_buff **pskb,
21 const struct net_device *in,
22 const struct net_device *out,
23 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -080024 const struct xt_target *target,
Harald Welte0ac4f892005-08-27 22:37:30 -070025 const void *targinfo, void *userinfo)
26{
27 struct ipv6hdr *ip6h;
28 const struct ip6t_HL_info *info = targinfo;
29 u_int16_t diffs[2];
30 int new_hl;
31
32 if (!skb_make_writable(pskb, (*pskb)->len))
33 return NF_DROP;
34
35 ip6h = (*pskb)->nh.ipv6h;
36
37 switch (info->mode) {
38 case IP6T_HL_SET:
39 new_hl = info->hop_limit;
40 break;
41 case IP6T_HL_INC:
42 new_hl = ip6h->hop_limit + info->hop_limit;
43 if (new_hl > 255)
44 new_hl = 255;
45 break;
46 case IP6T_HL_DEC:
47 new_hl = ip6h->hop_limit - info->hop_limit;
48 if (new_hl < 0)
49 new_hl = 0;
50 break;
51 default:
52 new_hl = ip6h->hop_limit;
53 break;
54 }
55
56 if (new_hl != ip6h->hop_limit) {
57 diffs[0] = htons(((unsigned)ip6h->hop_limit) << 8) ^ 0xFFFF;
58 ip6h->hop_limit = new_hl;
59 diffs[1] = htons(((unsigned)ip6h->hop_limit) << 8);
60 }
61
62 return IP6T_CONTINUE;
63}
64
65static int ip6t_hl_checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080066 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -080067 const struct xt_target *target,
Harald Welte0ac4f892005-08-27 22:37:30 -070068 void *targinfo,
69 unsigned int targinfosize,
70 unsigned int hook_mask)
71{
72 struct ip6t_HL_info *info = targinfo;
73
Harald Welte0ac4f892005-08-27 22:37:30 -070074 if (info->mode > IP6T_HL_MAXMODE) {
75 printk(KERN_WARNING "ip6t_HL: invalid or unknown Mode %u\n",
76 info->mode);
77 return 0;
78 }
Harald Welte0ac4f892005-08-27 22:37:30 -070079 if ((info->mode != IP6T_HL_SET) && (info->hop_limit == 0)) {
80 printk(KERN_WARNING "ip6t_HL: increment/decrement doesn't "
81 "make sense with value 0\n");
82 return 0;
83 }
Harald Welte0ac4f892005-08-27 22:37:30 -070084 return 1;
85}
86
87static struct ip6t_target ip6t_HL = {
88 .name = "HL",
89 .target = ip6t_hl_target,
Patrick McHardy7f939712006-03-20 18:01:43 -080090 .targetsize = sizeof(struct ip6t_HL_info),
91 .table = "mangle",
Harald Welte0ac4f892005-08-27 22:37:30 -070092 .checkentry = ip6t_hl_checkentry,
93 .me = THIS_MODULE
94};
95
Andrew Morton65b4b4e2006-03-28 16:37:06 -080096static int __init ip6t_hl_init(void)
Harald Welte0ac4f892005-08-27 22:37:30 -070097{
98 return ip6t_register_target(&ip6t_HL);
99}
100
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800101static void __exit ip6t_hl_fini(void)
Harald Welte0ac4f892005-08-27 22:37:30 -0700102{
103 ip6t_unregister_target(&ip6t_HL);
104}
105
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800106module_init(ip6t_hl_init);
107module_exit(ip6t_hl_fini);