blob: 7e5d51386f5686a42d121612a8855961c1a11275 [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,
Patrick McHardyfe1cb102006-08-22 00:35:47 -070025 const void *targinfo)
Harald Welte0ac4f892005-08-27 22:37:30 -070026{
27 struct ipv6hdr *ip6h;
28 const struct ip6t_HL_info *info = targinfo;
Harald Welte0ac4f892005-08-27 22:37:30 -070029 int new_hl;
30
31 if (!skb_make_writable(pskb, (*pskb)->len))
32 return NF_DROP;
33
34 ip6h = (*pskb)->nh.ipv6h;
35
36 switch (info->mode) {
37 case IP6T_HL_SET:
38 new_hl = info->hop_limit;
39 break;
40 case IP6T_HL_INC:
41 new_hl = ip6h->hop_limit + info->hop_limit;
42 if (new_hl > 255)
43 new_hl = 255;
44 break;
45 case IP6T_HL_DEC:
46 new_hl = ip6h->hop_limit - info->hop_limit;
47 if (new_hl < 0)
48 new_hl = 0;
49 break;
50 default:
51 new_hl = ip6h->hop_limit;
52 break;
53 }
54
Jan Engelhardt2822b0d2007-02-07 15:06:43 -080055 ip6h->hop_limit = new_hl;
Harald Welte0ac4f892005-08-27 22:37:30 -070056
57 return IP6T_CONTINUE;
58}
59
60static int ip6t_hl_checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080061 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -080062 const struct xt_target *target,
Harald Welte0ac4f892005-08-27 22:37:30 -070063 void *targinfo,
Harald Welte0ac4f892005-08-27 22:37:30 -070064 unsigned int hook_mask)
65{
66 struct ip6t_HL_info *info = targinfo;
67
Harald Welte0ac4f892005-08-27 22:37:30 -070068 if (info->mode > IP6T_HL_MAXMODE) {
69 printk(KERN_WARNING "ip6t_HL: invalid or unknown Mode %u\n",
70 info->mode);
71 return 0;
72 }
Harald Welte0ac4f892005-08-27 22:37:30 -070073 if ((info->mode != IP6T_HL_SET) && (info->hop_limit == 0)) {
74 printk(KERN_WARNING "ip6t_HL: increment/decrement doesn't "
75 "make sense with value 0\n");
76 return 0;
77 }
Harald Welte0ac4f892005-08-27 22:37:30 -070078 return 1;
79}
80
81static struct ip6t_target ip6t_HL = {
82 .name = "HL",
83 .target = ip6t_hl_target,
Patrick McHardy7f939712006-03-20 18:01:43 -080084 .targetsize = sizeof(struct ip6t_HL_info),
85 .table = "mangle",
Harald Welte0ac4f892005-08-27 22:37:30 -070086 .checkentry = ip6t_hl_checkentry,
87 .me = THIS_MODULE
88};
89
Andrew Morton65b4b4e2006-03-28 16:37:06 -080090static int __init ip6t_hl_init(void)
Harald Welte0ac4f892005-08-27 22:37:30 -070091{
92 return ip6t_register_target(&ip6t_HL);
93}
94
Andrew Morton65b4b4e2006-03-28 16:37:06 -080095static void __exit ip6t_hl_fini(void)
Harald Welte0ac4f892005-08-27 22:37:30 -070096{
97 ip6t_unregister_target(&ip6t_HL);
98}
99
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800100module_init(ip6t_hl_init);
101module_exit(ip6t_hl_fini);