Harald Welte | 0ac4f89 | 2005-08-27 22:37:30 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | MODULE_AUTHOR("Maciej Soltysiak <solt@dns.toxicfilms.tv>"); |
| 17 | MODULE_DESCRIPTION("IP tables Hop Limit modification module"); |
| 18 | MODULE_LICENSE("GPL"); |
| 19 | |
| 20 | static 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, |
| 24 | const void *targinfo, void *userinfo) |
| 25 | { |
| 26 | struct ipv6hdr *ip6h; |
| 27 | const struct ip6t_HL_info *info = targinfo; |
| 28 | u_int16_t diffs[2]; |
| 29 | 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 | |
| 55 | if (new_hl != ip6h->hop_limit) { |
| 56 | diffs[0] = htons(((unsigned)ip6h->hop_limit) << 8) ^ 0xFFFF; |
| 57 | ip6h->hop_limit = new_hl; |
| 58 | diffs[1] = htons(((unsigned)ip6h->hop_limit) << 8); |
| 59 | } |
| 60 | |
| 61 | return IP6T_CONTINUE; |
| 62 | } |
| 63 | |
| 64 | static int ip6t_hl_checkentry(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 65 | const void *entry, |
Harald Welte | 0ac4f89 | 2005-08-27 22:37:30 -0700 | [diff] [blame] | 66 | void *targinfo, |
| 67 | unsigned int targinfosize, |
| 68 | unsigned int hook_mask) |
| 69 | { |
| 70 | struct ip6t_HL_info *info = targinfo; |
| 71 | |
Harald Welte | 0ac4f89 | 2005-08-27 22:37:30 -0700 | [diff] [blame] | 72 | if (info->mode > IP6T_HL_MAXMODE) { |
| 73 | printk(KERN_WARNING "ip6t_HL: invalid or unknown Mode %u\n", |
| 74 | info->mode); |
| 75 | return 0; |
| 76 | } |
Harald Welte | 0ac4f89 | 2005-08-27 22:37:30 -0700 | [diff] [blame] | 77 | if ((info->mode != IP6T_HL_SET) && (info->hop_limit == 0)) { |
| 78 | printk(KERN_WARNING "ip6t_HL: increment/decrement doesn't " |
| 79 | "make sense with value 0\n"); |
| 80 | return 0; |
| 81 | } |
Harald Welte | 0ac4f89 | 2005-08-27 22:37:30 -0700 | [diff] [blame] | 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | static struct ip6t_target ip6t_HL = { |
| 86 | .name = "HL", |
| 87 | .target = ip6t_hl_target, |
Patrick McHardy | 7f93971 | 2006-03-20 18:01:43 -0800 | [diff] [blame^] | 88 | .targetsize = sizeof(struct ip6t_HL_info), |
| 89 | .table = "mangle", |
Harald Welte | 0ac4f89 | 2005-08-27 22:37:30 -0700 | [diff] [blame] | 90 | .checkentry = ip6t_hl_checkentry, |
| 91 | .me = THIS_MODULE |
| 92 | }; |
| 93 | |
| 94 | static int __init init(void) |
| 95 | { |
| 96 | return ip6t_register_target(&ip6t_HL); |
| 97 | } |
| 98 | |
| 99 | static void __exit fini(void) |
| 100 | { |
| 101 | ip6t_unregister_target(&ip6t_HL); |
| 102 | } |
| 103 | |
| 104 | module_init(init); |
| 105 | module_exit(fini); |