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 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 23 | static bool |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 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, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 31 | bool *hotdrop) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 32 | { |
| 33 | const struct xt_length_info *info = matchinfo; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 34 | u_int16_t pktlen = ntohs(ip_hdr(skb)->tot_len); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 35 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 36 | return (pktlen >= info->min && pktlen <= info->max) ^ info->invert; |
| 37 | } |
| 38 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 39 | static bool |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 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, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 47 | bool *hotdrop) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 48 | { |
| 49 | const struct xt_length_info *info = matchinfo; |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 50 | const u_int16_t pktlen = ntohs(ipv6_hdr(skb)->payload_len) + |
| 51 | sizeof(struct ipv6hdr); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 52 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 53 | return (pktlen >= info->min && pktlen <= info->max) ^ info->invert; |
| 54 | } |
| 55 | |
Patrick McHardy | 9f15c53 | 2007-07-07 22:22:02 -0700 | [diff] [blame] | 56 | static struct xt_match xt_length_match[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 57 | { |
| 58 | .name = "length", |
| 59 | .family = AF_INET, |
| 60 | .match = match, |
| 61 | .matchsize = sizeof(struct xt_length_info), |
| 62 | .me = THIS_MODULE, |
| 63 | }, |
| 64 | { |
| 65 | .name = "length", |
| 66 | .family = AF_INET6, |
| 67 | .match = match6, |
| 68 | .matchsize = sizeof(struct xt_length_info), |
| 69 | .me = THIS_MODULE, |
| 70 | }, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 73 | static int __init xt_length_init(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 74 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 75 | return xt_register_matches(xt_length_match, |
| 76 | ARRAY_SIZE(xt_length_match)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 79 | static void __exit xt_length_fini(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 80 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 81 | xt_unregister_matches(xt_length_match, ARRAY_SIZE(xt_length_match)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 84 | module_init(xt_length_init); |
| 85 | module_exit(xt_length_fini); |