1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Kernel module to match AH parameters. */
4 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
10 #include <linux/ipv6.h>
11 #include <linux/types.h>
12 #include <net/checksum.h>
15 #include <linux/netfilter/x_tables.h>
16 #include <linux/netfilter_ipv6/ip6_tables.h>
17 #include <linux/netfilter_ipv6/ip6t_ah.h>
19 MODULE_LICENSE("GPL");
20 MODULE_DESCRIPTION("Xtables: IPv6 IPsec-AH match");
21 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
23 /* Returns 1 if the spi is matched by the range, 0 otherwise */
25 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
29 pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
30 invert ? '!' : ' ', min, spi, max);
31 r = (spi >= min && spi <= max) ^ invert;
32 pr_debug(" result %s\n", r ? "PASS" : "FAILED");
36 static bool ah_mt6(const struct sk_buff *skb, struct xt_action_param *par)
38 struct ip_auth_hdr _ah;
39 const struct ip_auth_hdr *ah;
40 const struct ip6t_ah *ahinfo = par->matchinfo;
42 unsigned int hdrlen = 0;
45 err = ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH, NULL, NULL);
52 ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah);
58 hdrlen = ipv6_authlen(ah);
60 pr_debug("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen);
61 pr_debug("RES %04X ", ah->reserved);
62 pr_debug("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi));
64 pr_debug("IPv6 AH spi %02X ",
65 spi_match(ahinfo->spis[0], ahinfo->spis[1],
67 !!(ahinfo->invflags & IP6T_AH_INV_SPI)));
68 pr_debug("len %02X %04X %02X ",
69 ahinfo->hdrlen, hdrlen,
71 (ahinfo->hdrlen == hdrlen) ^
72 !!(ahinfo->invflags & IP6T_AH_INV_LEN)));
73 pr_debug("res %02X %04X %02X\n",
74 ahinfo->hdrres, ah->reserved,
75 !(ahinfo->hdrres && ah->reserved));
77 return spi_match(ahinfo->spis[0], ahinfo->spis[1],
79 !!(ahinfo->invflags & IP6T_AH_INV_SPI)) &&
81 (ahinfo->hdrlen == hdrlen) ^
82 !!(ahinfo->invflags & IP6T_AH_INV_LEN)) &&
83 !(ahinfo->hdrres && ah->reserved);
86 static int ah_mt6_check(const struct xt_mtchk_param *par)
88 const struct ip6t_ah *ahinfo = par->matchinfo;
90 if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
91 pr_debug("unknown flags %X\n", ahinfo->invflags);
97 static struct xt_match ah_mt6_reg __read_mostly = {
99 .family = NFPROTO_IPV6,
101 .matchsize = sizeof(struct ip6t_ah),
102 .checkentry = ah_mt6_check,
106 static int __init ah_mt6_init(void)
108 return xt_register_match(&ah_mt6_reg);
111 static void __exit ah_mt6_exit(void)
113 xt_unregister_match(&ah_mt6_reg);
116 module_init(ah_mt6_init);
117 module_exit(ah_mt6_exit);