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, |
| 27 | const void *matchinfo, |
| 28 | int offset, |
| 29 | unsigned int protoff, |
| 30 | int *hotdrop) |
| 31 | { |
| 32 | const struct xt_length_info *info = matchinfo; |
| 33 | u_int16_t pktlen = ntohs(skb->nh.iph->tot_len); |
| 34 | |
| 35 | return (pktlen >= info->min && pktlen <= info->max) ^ info->invert; |
| 36 | } |
| 37 | |
| 38 | static int |
| 39 | match6(const struct sk_buff *skb, |
| 40 | const struct net_device *in, |
| 41 | const struct net_device *out, |
| 42 | const void *matchinfo, |
| 43 | int offset, |
| 44 | unsigned int protoff, |
| 45 | int *hotdrop) |
| 46 | { |
| 47 | const struct xt_length_info *info = matchinfo; |
| 48 | u_int16_t pktlen = ntohs(skb->nh.ipv6h->payload_len) + sizeof(struct ipv6hdr); |
| 49 | |
| 50 | return (pktlen >= info->min && pktlen <= info->max) ^ info->invert; |
| 51 | } |
| 52 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 53 | static struct xt_match length_match = { |
| 54 | .name = "length", |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame^] | 55 | .match = match, |
| 56 | .matchsize = sizeof(struct xt_length_info), |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 57 | .me = THIS_MODULE, |
| 58 | }; |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame^] | 59 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 60 | static struct xt_match length6_match = { |
| 61 | .name = "length", |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame^] | 62 | .match = match6, |
| 63 | .matchsize = sizeof(struct xt_length_info), |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 64 | .me = THIS_MODULE, |
| 65 | }; |
| 66 | |
| 67 | static int __init init(void) |
| 68 | { |
| 69 | int ret; |
| 70 | ret = xt_register_match(AF_INET, &length_match); |
| 71 | if (ret) |
| 72 | return ret; |
| 73 | ret = xt_register_match(AF_INET6, &length6_match); |
| 74 | if (ret) |
| 75 | xt_unregister_match(AF_INET, &length_match); |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | static void __exit fini(void) |
| 81 | { |
| 82 | xt_unregister_match(AF_INET, &length_match); |
| 83 | xt_unregister_match(AF_INET6, &length6_match); |
| 84 | } |
| 85 | |
| 86 | module_init(init); |
| 87 | module_exit(fini); |