xref: /linux/net/ipv4/netfilter/ipt_ah.c (revision 49bda4826843be0ef97a162009a29ea3a63f3935)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Kernel module to match AH parameters. */
3 /* (C) 1999-2000 Yon Uriarte <yon@astaro.de>
4  */
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/in.h>
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
9 #include <linux/ip.h>
10 
11 #include <linux/netfilter_ipv4/ipt_ah.h>
12 #include <linux/netfilter/x_tables.h>
13 
14 MODULE_LICENSE("GPL");
15 MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
16 MODULE_DESCRIPTION("Xtables: IPv4 IPsec-AH SPI match");
17 
18 /* Returns 1 if the spi is matched by the range, 0 otherwise */
19 static inline bool
spi_match(u_int32_t min,u_int32_t max,u_int32_t spi,bool invert)20 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
21 {
22 	return (spi >= min && spi <= max) ^ invert;
23 }
24 
ah_mt(const struct sk_buff * skb,struct xt_action_param * par)25 static bool ah_mt(const struct sk_buff *skb, struct xt_action_param *par)
26 {
27 	struct ip_auth_hdr _ahdr;
28 	const struct ip_auth_hdr *ah;
29 	const struct ipt_ah *ahinfo = par->matchinfo;
30 
31 	/* Must not be a fragment. */
32 	if (par->fragoff != 0)
33 		return false;
34 
35 	ah = skb_header_pointer(skb, par->thoff, sizeof(_ahdr), &_ahdr);
36 	if (ah == NULL) {
37 		/* We've been asked to examine this packet, and we
38 		 * can't.  Hence, no choice but to drop.
39 		 */
40 		par->hotdrop = true;
41 		return false;
42 	}
43 
44 	return spi_match(ahinfo->spis[0], ahinfo->spis[1],
45 			 ntohl(ah->spi),
46 			 !!(ahinfo->invflags & IPT_AH_INV_SPI));
47 }
48 
ah_mt_check(const struct xt_mtchk_param * par)49 static int ah_mt_check(const struct xt_mtchk_param *par)
50 {
51 	const struct ipt_ah *ahinfo = par->matchinfo;
52 
53 	/* Must specify no unknown invflags */
54 	if (ahinfo->invflags & ~IPT_AH_INV_MASK) {
55 		pr_info_ratelimited("unknown flags %X\n", ahinfo->invflags);
56 		return -EINVAL;
57 	}
58 	return 0;
59 }
60 
61 static struct xt_match ah_mt_reg __read_mostly = {
62 	.name		= "ah",
63 	.family		= NFPROTO_IPV4,
64 	.match		= ah_mt,
65 	.matchsize	= sizeof(struct ipt_ah),
66 	.proto		= IPPROTO_AH,
67 	.checkentry	= ah_mt_check,
68 	.me		= THIS_MODULE,
69 };
70 
ah_mt_init(void)71 static int __init ah_mt_init(void)
72 {
73 	return xt_register_match(&ah_mt_reg);
74 }
75 
ah_mt_exit(void)76 static void __exit ah_mt_exit(void)
77 {
78 	xt_unregister_match(&ah_mt_reg);
79 }
80 
81 module_init(ah_mt_init);
82 module_exit(ah_mt_exit);
83