1
2
3
4
5
6
7
8
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#ifdef DEBUG_CONNTRACK
22#define duprintf(format, args...) printk(format , ## args)
23#else
24#define duprintf(format, args...)
25#endif
26
27
28static inline bool
29spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
30{
31 bool r;
32 duprintf("ah spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ',
33 min,spi,max);
34 r=(spi >= min && spi <= max) ^ invert;
35 duprintf(" result %s\n",r? "PASS" : "FAILED");
36 return r;
37}
38
39static bool ah_mt(const struct sk_buff *skb, const struct xt_match_param *par)
40{
41 struct ip_auth_hdr _ahdr;
42 const struct ip_auth_hdr *ah;
43 const struct ipt_ah *ahinfo = par->matchinfo;
44
45
46 if (par->fragoff != 0)
47 return false;
48
49 ah = skb_header_pointer(skb, par->thoff, sizeof(_ahdr), &_ahdr);
50 if (ah == NULL) {
51
52
53
54 duprintf("Dropping evil AH tinygram.\n");
55 *par->hotdrop = true;
56 return 0;
57 }
58
59 return spi_match(ahinfo->spis[0], ahinfo->spis[1],
60 ntohl(ah->spi),
61 !!(ahinfo->invflags & IPT_AH_INV_SPI));
62}
63
64static bool ah_mt_check(const struct xt_mtchk_param *par)
65{
66 const struct ipt_ah *ahinfo = par->matchinfo;
67
68
69 if (ahinfo->invflags & ~IPT_AH_INV_MASK) {
70 duprintf("ipt_ah: unknown flags %X\n", ahinfo->invflags);
71 return false;
72 }
73 return true;
74}
75
76static struct xt_match ah_mt_reg __read_mostly = {
77 .name = "ah",
78 .family = NFPROTO_IPV4,
79 .match = ah_mt,
80 .matchsize = sizeof(struct ipt_ah),
81 .proto = IPPROTO_AH,
82 .checkentry = ah_mt_check,
83 .me = THIS_MODULE,
84};
85
86static int __init ah_mt_init(void)
87{
88 return xt_register_match(&ah_mt_reg);
89}
90
91static void __exit ah_mt_exit(void)
92{
93 xt_unregister_match(&ah_mt_reg);
94}
95
96module_init(ah_mt_init);
97module_exit(ah_mt_exit);
98