1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kernel module to match IPComp parameters for IPv4 and IPv6
4 * Copyright (C) 2013 WindRiver
7 * Fan Du <fan.du@windriver.com>
10 * net/netfilter/xt_esp.c
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/skbuff.h>
19 #include <linux/netfilter/xt_ipcomp.h>
20 #include <linux/netfilter/x_tables.h>
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Fan Du <fan.du@windriver.com>");
24 MODULE_DESCRIPTION("Xtables: IPv4/6 IPsec-IPComp SPI match");
25 MODULE_ALIAS("ipt_ipcomp");
26 MODULE_ALIAS("ip6t_ipcomp");
28 /* Returns 1 if the spi is matched by the range, 0 otherwise */
30 spi_match(u_int32_t min
, u_int32_t max
, u_int32_t spi
, bool invert
)
33 pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
34 invert
? '!' : ' ', min
, spi
, max
);
35 r
= (spi
>= min
&& spi
<= max
) ^ invert
;
36 pr_debug(" result %s\n", r
? "PASS" : "FAILED");
40 static bool comp_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
42 struct ip_comp_hdr _comphdr
;
43 const struct ip_comp_hdr
*chdr
;
44 const struct xt_ipcomp
*compinfo
= par
->matchinfo
;
46 /* Must not be a fragment. */
47 if (par
->fragoff
!= 0)
50 chdr
= skb_header_pointer(skb
, par
->thoff
, sizeof(_comphdr
), &_comphdr
);
52 /* We've been asked to examine this packet, and we
53 * can't. Hence, no choice but to drop.
55 pr_debug("Dropping evil IPComp tinygram.\n");
60 return spi_match(compinfo
->spis
[0], compinfo
->spis
[1],
62 !!(compinfo
->invflags
& XT_IPCOMP_INV_SPI
));
65 static int comp_mt_check(const struct xt_mtchk_param
*par
)
67 const struct xt_ipcomp
*compinfo
= par
->matchinfo
;
69 /* Must specify no unknown invflags */
70 if (compinfo
->invflags
& ~XT_IPCOMP_INV_MASK
) {
71 pr_info_ratelimited("unknown flags %X\n", compinfo
->invflags
);
77 static struct xt_match comp_mt_reg
[] __read_mostly
= {
80 .family
= NFPROTO_IPV4
,
82 .matchsize
= sizeof(struct xt_ipcomp
),
83 .proto
= IPPROTO_COMP
,
84 .checkentry
= comp_mt_check
,
89 .family
= NFPROTO_IPV6
,
91 .matchsize
= sizeof(struct xt_ipcomp
),
92 .proto
= IPPROTO_COMP
,
93 .checkentry
= comp_mt_check
,
98 static int __init
comp_mt_init(void)
100 return xt_register_matches(comp_mt_reg
, ARRAY_SIZE(comp_mt_reg
));
103 static void __exit
comp_mt_exit(void)
105 xt_unregister_matches(comp_mt_reg
, ARRAY_SIZE(comp_mt_reg
));
108 module_init(comp_mt_init
);
109 module_exit(comp_mt_exit
);