0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0014 #include <linux/in.h>
0015 #include <linux/module.h>
0016 #include <linux/skbuff.h>
0017 #include <linux/ip.h>
0018
0019 #include <linux/netfilter/xt_ipcomp.h>
0020 #include <linux/netfilter/x_tables.h>
0021
0022 MODULE_LICENSE("GPL");
0023 MODULE_AUTHOR("Fan Du <fan.du@windriver.com>");
0024 MODULE_DESCRIPTION("Xtables: IPv4/6 IPsec-IPComp SPI match");
0025 MODULE_ALIAS("ipt_ipcomp");
0026 MODULE_ALIAS("ip6t_ipcomp");
0027
0028
0029 static inline bool
0030 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
0031 {
0032 bool r;
0033 pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
0034 invert ? '!' : ' ', min, spi, max);
0035 r = (spi >= min && spi <= max) ^ invert;
0036 pr_debug(" result %s\n", r ? "PASS" : "FAILED");
0037 return r;
0038 }
0039
0040 static bool comp_mt(const struct sk_buff *skb, struct xt_action_param *par)
0041 {
0042 struct ip_comp_hdr _comphdr;
0043 const struct ip_comp_hdr *chdr;
0044 const struct xt_ipcomp *compinfo = par->matchinfo;
0045
0046
0047 if (par->fragoff != 0)
0048 return false;
0049
0050 chdr = skb_header_pointer(skb, par->thoff, sizeof(_comphdr), &_comphdr);
0051 if (chdr == NULL) {
0052
0053
0054
0055 pr_debug("Dropping evil IPComp tinygram.\n");
0056 par->hotdrop = true;
0057 return false;
0058 }
0059
0060 return spi_match(compinfo->spis[0], compinfo->spis[1],
0061 ntohs(chdr->cpi),
0062 !!(compinfo->invflags & XT_IPCOMP_INV_SPI));
0063 }
0064
0065 static int comp_mt_check(const struct xt_mtchk_param *par)
0066 {
0067 const struct xt_ipcomp *compinfo = par->matchinfo;
0068
0069
0070 if (compinfo->invflags & ~XT_IPCOMP_INV_MASK) {
0071 pr_info_ratelimited("unknown flags %X\n", compinfo->invflags);
0072 return -EINVAL;
0073 }
0074 return 0;
0075 }
0076
0077 static struct xt_match comp_mt_reg[] __read_mostly = {
0078 {
0079 .name = "ipcomp",
0080 .family = NFPROTO_IPV4,
0081 .match = comp_mt,
0082 .matchsize = sizeof(struct xt_ipcomp),
0083 .proto = IPPROTO_COMP,
0084 .checkentry = comp_mt_check,
0085 .me = THIS_MODULE,
0086 },
0087 {
0088 .name = "ipcomp",
0089 .family = NFPROTO_IPV6,
0090 .match = comp_mt,
0091 .matchsize = sizeof(struct xt_ipcomp),
0092 .proto = IPPROTO_COMP,
0093 .checkentry = comp_mt_check,
0094 .me = THIS_MODULE,
0095 },
0096 };
0097
0098 static int __init comp_mt_init(void)
0099 {
0100 return xt_register_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
0101 }
0102
0103 static void __exit comp_mt_exit(void)
0104 {
0105 xt_unregister_matches(comp_mt_reg, ARRAY_SIZE(comp_mt_reg));
0106 }
0107
0108 module_init(comp_mt_init);
0109 module_exit(comp_mt_exit);