1
2
3
4
5
6
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9#include <linux/in.h>
10#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/ip.h>
13
14#include <linux/netfilter_ipv4/ipt_ah.h>
15#include <linux/netfilter/x_tables.h>
16
17MODULE_LICENSE("GPL");
18MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
19MODULE_DESCRIPTION("Xtables: IPv4 IPsec-AH SPI match");
20
21
22static inline bool
23spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
24{
25 bool r;
26 pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
27 invert ? '!' : ' ', min, spi, max);
28 r = (spi >= min && spi <= max) ^ invert;
29 pr_debug(" result %s\n", r ? "PASS" : "FAILED");
30 return r;
31}
32
33static bool ah_mt(const struct sk_buff *skb, struct xt_action_param *par)
34{
35 struct ip_auth_hdr _ahdr;
36 const struct ip_auth_hdr *ah;
37 const struct ipt_ah *ahinfo = par->matchinfo;
38
39
40 if (par->fragoff != 0)
41 return false;
42
43 ah = skb_header_pointer(skb, par->thoff, sizeof(_ahdr), &_ahdr);
44 if (ah == NULL) {
45
46
47
48 pr_debug("Dropping evil AH tinygram.\n");
49 par->hotdrop = true;
50 return false;
51 }
52
53 return spi_match(ahinfo->spis[0], ahinfo->spis[1],
54 ntohl(ah->spi),
55 !!(ahinfo->invflags & IPT_AH_INV_SPI));
56}
57
58static int ah_mt_check(const struct xt_mtchk_param *par)
59{
60 const struct ipt_ah *ahinfo = par->matchinfo;
61
62
63 if (ahinfo->invflags & ~IPT_AH_INV_MASK) {
64 pr_debug("unknown flags %X\n", ahinfo->invflags);
65 return -EINVAL;
66 }
67 return 0;
68}
69
70static struct xt_match ah_mt_reg __read_mostly = {
71 .name = "ah",
72 .family = NFPROTO_IPV4,
73 .match = ah_mt,
74 .matchsize = sizeof(struct ipt_ah),
75 .proto = IPPROTO_AH,
76 .checkentry = ah_mt_check,
77 .me = THIS_MODULE,
78};
79
80static int __init ah_mt_init(void)
81{
82 return xt_register_match(&ah_mt_reg);
83}
84
85static void __exit ah_mt_exit(void)
86{
87 xt_unregister_match(&ah_mt_reg);
88}
89
90module_init(ah_mt_init);
91module_exit(ah_mt_exit);
92