Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1 | /* Kernel module to match packet length. */ |
| 2 | /* (C) 1999-2001 James Morris <jmorros@intercode.com.au> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/skbuff.h> |
David S. Miller | 37d8dc8 | 2006-01-13 16:19:44 -0800 | [diff] [blame] | 11 | #include <linux/ipv6.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 12 | #include <net/ip.h> |
| 13 | |
| 14 | #include <linux/netfilter/xt_length.h> |
| 15 | #include <linux/netfilter/x_tables.h> |
| 16 | |
| 17 | MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>"); |
| 18 | MODULE_DESCRIPTION("IP tables packet length matching module"); |
| 19 | MODULE_LICENSE("GPL"); |
| 20 | MODULE_ALIAS("ipt_length"); |
| 21 | MODULE_ALIAS("ip6t_length"); |
| 22 | |
| 23 | static int |
| 24 | match(const struct sk_buff *skb, |
| 25 | const struct net_device *in, |
| 26 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame^] | 27 | const struct xt_match *match, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 28 | const void *matchinfo, |
| 29 | int offset, |
| 30 | unsigned int protoff, |
| 31 | int *hotdrop) |
| 32 | { |
| 33 | const struct xt_length_info *info = matchinfo; |
| 34 | u_int16_t pktlen = ntohs(skb->nh.iph->tot_len); |
| 35 | |
| 36 | return (pktlen >= info->min && pktlen <= info->max) ^ info->invert; |
| 37 | } |
| 38 | |
| 39 | static int |
| 40 | match6(const struct sk_buff *skb, |
| 41 | const struct net_device *in, |
| 42 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame^] | 43 | const struct xt_match *match, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 44 | const void *matchinfo, |
| 45 | int offset, |
| 46 | unsigned int protoff, |
| 47 | int *hotdrop) |
| 48 | { |
| 49 | const struct xt_length_info *info = matchinfo; |
| 50 | u_int16_t pktlen = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr); |
| 51 | |
| 52 | return (pktlen >= info->min && pktlen <= info->max) ^ info->invert; |
| 53 | } |
| 54 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 55 | static struct xt_match length_match = { |
| 56 | .name = "length", |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 57 | .match = match, |
| 58 | .matchsize = sizeof(struct xt_length_info), |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 59 | .me = THIS_MODULE, |
| 60 | }; |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 61 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 62 | static struct xt_match length6_match = { |
| 63 | .name = "length", |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 64 | .match = match6, |
| 65 | .matchsize = sizeof(struct xt_length_info), |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 66 | .me = THIS_MODULE, |
| 67 | }; |
| 68 | |
| 69 | static int __init init(void) |
| 70 | { |
| 71 | int ret; |
| 72 | ret = xt_register_match(AF_INET, &length_match); |
| 73 | if (ret) |
| 74 | return ret; |
| 75 | ret = xt_register_match(AF_INET6, &length6_match); |
| 76 | if (ret) |
| 77 | xt_unregister_match(AF_INET, &length_match); |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | static void __exit fini(void) |
| 83 | { |
| 84 | xt_unregister_match(AF_INET, &length_match); |
| 85 | xt_unregister_match(AF_INET6, &length6_match); |
| 86 | } |
| 87 | |
| 88 | module_init(init); |
| 89 | module_exit(fini); |