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>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 18 | MODULE_DESCRIPTION("Xtables: Packet length (Layer3,4,5) match"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 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 |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 24 | length_mt(const struct sk_buff *skb, const struct net_device *in, |
| 25 | const struct net_device *out, const struct xt_match *match, |
| 26 | const void *matchinfo, int offset, unsigned int protoff, |
| 27 | bool *hotdrop) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 28 | { |
| 29 | const struct xt_length_info *info = matchinfo; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 30 | u_int16_t pktlen = ntohs(ip_hdr(skb)->tot_len); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 31 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 32 | return (pktlen >= info->min && pktlen <= info->max) ^ info->invert; |
| 33 | } |
| 34 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 35 | static bool |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 36 | length_mt6(const struct sk_buff *skb, const struct net_device *in, |
| 37 | const struct net_device *out, const struct xt_match *match, |
| 38 | const void *matchinfo, int offset, unsigned int protoff, |
| 39 | bool *hotdrop) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 40 | { |
| 41 | const struct xt_length_info *info = matchinfo; |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 42 | const u_int16_t pktlen = ntohs(ipv6_hdr(skb)->payload_len) + |
| 43 | sizeof(struct ipv6hdr); |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 44 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 45 | return (pktlen >= info->min && pktlen <= info->max) ^ info->invert; |
| 46 | } |
| 47 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 48 | static struct xt_match length_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 49 | { |
| 50 | .name = "length", |
| 51 | .family = AF_INET, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 52 | .match = length_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 53 | .matchsize = sizeof(struct xt_length_info), |
| 54 | .me = THIS_MODULE, |
| 55 | }, |
| 56 | { |
| 57 | .name = "length", |
| 58 | .family = AF_INET6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 59 | .match = length_mt6, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 60 | .matchsize = sizeof(struct xt_length_info), |
| 61 | .me = THIS_MODULE, |
| 62 | }, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 65 | static int __init length_mt_init(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 66 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 67 | return xt_register_matches(length_mt_reg, ARRAY_SIZE(length_mt_reg)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 70 | static void __exit length_mt_exit(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 71 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 72 | xt_unregister_matches(length_mt_reg, ARRAY_SIZE(length_mt_reg)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 75 | module_init(length_mt_init); |
| 76 | module_exit(length_mt_exit); |