Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Kernel module to match packet length. */
0003 /* (C) 1999-2001 James Morris <jmorros@intercode.com.au>
0004  */
0005 
0006 #include <linux/module.h>
0007 #include <linux/skbuff.h>
0008 #include <linux/ipv6.h>
0009 #include <net/ip.h>
0010 
0011 #include <linux/netfilter/xt_length.h>
0012 #include <linux/netfilter/x_tables.h>
0013 
0014 MODULE_AUTHOR("James Morris <jmorris@intercode.com.au>");
0015 MODULE_DESCRIPTION("Xtables: Packet length (Layer3,4,5) match");
0016 MODULE_LICENSE("GPL");
0017 MODULE_ALIAS("ipt_length");
0018 MODULE_ALIAS("ip6t_length");
0019 
0020 static bool
0021 length_mt(const struct sk_buff *skb, struct xt_action_param *par)
0022 {
0023     const struct xt_length_info *info = par->matchinfo;
0024     u_int16_t pktlen = ntohs(ip_hdr(skb)->tot_len);
0025 
0026     return (pktlen >= info->min && pktlen <= info->max) ^ info->invert;
0027 }
0028 
0029 static bool
0030 length_mt6(const struct sk_buff *skb, struct xt_action_param *par)
0031 {
0032     const struct xt_length_info *info = par->matchinfo;
0033     const u_int16_t pktlen = ntohs(ipv6_hdr(skb)->payload_len) +
0034                  sizeof(struct ipv6hdr);
0035 
0036     return (pktlen >= info->min && pktlen <= info->max) ^ info->invert;
0037 }
0038 
0039 static struct xt_match length_mt_reg[] __read_mostly = {
0040     {
0041         .name       = "length",
0042         .family     = NFPROTO_IPV4,
0043         .match      = length_mt,
0044         .matchsize  = sizeof(struct xt_length_info),
0045         .me     = THIS_MODULE,
0046     },
0047     {
0048         .name       = "length",
0049         .family     = NFPROTO_IPV6,
0050         .match      = length_mt6,
0051         .matchsize  = sizeof(struct xt_length_info),
0052         .me     = THIS_MODULE,
0053     },
0054 };
0055 
0056 static int __init length_mt_init(void)
0057 {
0058     return xt_register_matches(length_mt_reg, ARRAY_SIZE(length_mt_reg));
0059 }
0060 
0061 static void __exit length_mt_exit(void)
0062 {
0063     xt_unregister_matches(length_mt_reg, ARRAY_SIZE(length_mt_reg));
0064 }
0065 
0066 module_init(length_mt_init);
0067 module_exit(length_mt_exit);