blob: 109132c9a1462727267a4aab426215ed97495a00 [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
23static int
24match(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,
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
39static int
40match6(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,
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 Welte2e4e6a12006-01-12 13:30:04 -080055static struct xt_match length_match = {
56 .name = "length",
Patrick McHardy5d04bff2006-03-20 18:01:58 -080057 .match = match,
58 .matchsize = sizeof(struct xt_length_info),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080059 .family = AF_INET,
Harald Welte2e4e6a12006-01-12 13:30:04 -080060 .me = THIS_MODULE,
61};
Patrick McHardy5d04bff2006-03-20 18:01:58 -080062
Harald Welte2e4e6a12006-01-12 13:30:04 -080063static struct xt_match length6_match = {
64 .name = "length",
Patrick McHardy5d04bff2006-03-20 18:01:58 -080065 .match = match6,
66 .matchsize = sizeof(struct xt_length_info),
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080067 .family = AF_INET6,
Harald Welte2e4e6a12006-01-12 13:30:04 -080068 .me = THIS_MODULE,
69};
70
Andrew Morton65b4b4e2006-03-28 16:37:06 -080071static int __init xt_length_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -080072{
73 int ret;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080074 ret = xt_register_match(&length_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -080075 if (ret)
76 return ret;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080077 ret = xt_register_match(&length6_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -080078 if (ret)
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080079 xt_unregister_match(&length_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -080080
81 return ret;
82}
83
Andrew Morton65b4b4e2006-03-28 16:37:06 -080084static void __exit xt_length_fini(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -080085{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080086 xt_unregister_match(&length_match);
87 xt_unregister_match(&length6_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -080088}
89
Andrew Morton65b4b4e2006-03-28 16:37:06 -080090module_init(xt_length_init);
91module_exit(xt_length_fini);