blob: 3dad173d9735706608d47b01edf6006a2589c621 [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001/* 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. Miller37d8dc82006-01-13 16:19:44 -080011#include <linux/ipv6.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080012#include <net/ip.h>
13
14#include <linux/netfilter/xt_length.h>
15#include <linux/netfilter/x_tables.h>
16
17MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
18MODULE_DESCRIPTION("IP tables packet length matching module");
19MODULE_LICENSE("GPL");
20MODULE_ALIAS("ipt_length");
21MODULE_ALIAS("ip6t_length");
22
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070023static bool
Harald Welte2e4e6a12006-01-12 13:30:04 -080024match(const struct sk_buff *skb,
25 const struct net_device *in,
26 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080027 const struct xt_match *match,
Harald Welte2e4e6a12006-01-12 13:30:04 -080028 const void *matchinfo,
29 int offset,
30 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070031 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080032{
33 const struct xt_length_info *info = matchinfo;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070034 u_int16_t pktlen = ntohs(ip_hdr(skb)->tot_len);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080035
Harald Welte2e4e6a12006-01-12 13:30:04 -080036 return (pktlen >= info->min && pktlen <= info->max) ^ info->invert;
37}
38
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070039static bool
Harald Welte2e4e6a12006-01-12 13:30:04 -080040match6(const struct sk_buff *skb,
41 const struct net_device *in,
42 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080043 const struct xt_match *match,
Harald Welte2e4e6a12006-01-12 13:30:04 -080044 const void *matchinfo,
45 int offset,
46 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070047 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080048{
49 const struct xt_length_info *info = matchinfo;
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070050 const u_int16_t pktlen = ntohs(ipv6_hdr(skb)->payload_len) +
51 sizeof(struct ipv6hdr);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080052
Harald Welte2e4e6a12006-01-12 13:30:04 -080053 return (pktlen >= info->min && pktlen <= info->max) ^ info->invert;
54}
55
Patrick McHardy9f15c532007-07-07 22:22:02 -070056static struct xt_match xt_length_match[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070057 {
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 Welte2e4e6a12006-01-12 13:30:04 -080071};
72
Andrew Morton65b4b4e2006-03-28 16:37:06 -080073static int __init xt_length_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -080074{
Patrick McHardy4470bbc2006-08-22 00:34:04 -070075 return xt_register_matches(xt_length_match,
76 ARRAY_SIZE(xt_length_match));
Harald Welte2e4e6a12006-01-12 13:30:04 -080077}
78
Andrew Morton65b4b4e2006-03-28 16:37:06 -080079static void __exit xt_length_fini(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -080080{
Patrick McHardy4470bbc2006-08-22 00:34:04 -070081 xt_unregister_matches(xt_length_match, ARRAY_SIZE(xt_length_match));
Harald Welte2e4e6a12006-01-12 13:30:04 -080082}
83
Andrew Morton65b4b4e2006-03-28 16:37:06 -080084module_init(xt_length_init);
85module_exit(xt_length_fini);