Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel module to match AH parameters. */ |
2 | /* (C) 1999-2000 Yon Uriarte <yon@astaro.de> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | */ | |
8 | ||
6709dbbb | 9 | #include <linux/in.h> |
1da177e4 LT |
10 | #include <linux/module.h> |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/ip.h> | |
13 | ||
14 | #include <linux/netfilter_ipv4/ipt_ah.h> | |
6709dbbb | 15 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
16 | |
17 | MODULE_LICENSE("GPL"); | |
18 | MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>"); | |
2ae15b64 | 19 | MODULE_DESCRIPTION("Xtables: IPv4 IPsec-AH SPI match"); |
1da177e4 LT |
20 | |
21 | #ifdef DEBUG_CONNTRACK | |
22 | #define duprintf(format, args...) printk(format , ## args) | |
23 | #else | |
24 | #define duprintf(format, args...) | |
25 | #endif | |
26 | ||
27 | /* Returns 1 if the spi is matched by the range, 0 otherwise */ | |
1d93a9cb JE |
28 | static inline bool |
29 | spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert) | |
1da177e4 | 30 | { |
1d93a9cb | 31 | bool r; |
e905a9ed YH |
32 | duprintf("ah spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ', |
33 | min,spi,max); | |
1da177e4 LT |
34 | r=(spi >= min && spi <= max) ^ invert; |
35 | duprintf(" result %s\n",r? "PASS" : "FAILED"); | |
36 | return r; | |
37 | } | |
38 | ||
1d93a9cb | 39 | static bool |
d3c5ee6d JE |
40 | ah_mt(const struct sk_buff *skb, const struct net_device *in, |
41 | const struct net_device *out, const struct xt_match *match, | |
42 | const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop) | |
1da177e4 | 43 | { |
a47362a2 JE |
44 | struct ip_auth_hdr _ahdr; |
45 | const struct ip_auth_hdr *ah; | |
1da177e4 LT |
46 | const struct ipt_ah *ahinfo = matchinfo; |
47 | ||
48 | /* Must not be a fragment. */ | |
49 | if (offset) | |
1d93a9cb | 50 | return false; |
1da177e4 | 51 | |
2e4e6a17 | 52 | ah = skb_header_pointer(skb, protoff, |
1da177e4 LT |
53 | sizeof(_ahdr), &_ahdr); |
54 | if (ah == NULL) { | |
55 | /* We've been asked to examine this packet, and we | |
56 | * can't. Hence, no choice but to drop. | |
57 | */ | |
58 | duprintf("Dropping evil AH tinygram.\n"); | |
cff533ac | 59 | *hotdrop = true; |
1da177e4 LT |
60 | return 0; |
61 | } | |
62 | ||
63 | return spi_match(ahinfo->spis[0], ahinfo->spis[1], | |
64 | ntohl(ah->spi), | |
65 | !!(ahinfo->invflags & IPT_AH_INV_SPI)); | |
66 | } | |
67 | ||
68 | /* Called when user tries to insert an entry of this type. */ | |
ccb79bdc | 69 | static bool |
d3c5ee6d JE |
70 | ah_mt_check(const char *tablename, const void *ip_void, |
71 | const struct xt_match *match, void *matchinfo, | |
72 | unsigned int hook_mask) | |
1da177e4 LT |
73 | { |
74 | const struct ipt_ah *ahinfo = matchinfo; | |
75 | ||
1d5cd909 | 76 | /* Must specify no unknown invflags */ |
1da177e4 | 77 | if (ahinfo->invflags & ~IPT_AH_INV_MASK) { |
1d5cd909 | 78 | duprintf("ipt_ah: unknown flags %X\n", ahinfo->invflags); |
ccb79bdc | 79 | return false; |
1da177e4 | 80 | } |
ccb79bdc | 81 | return true; |
1da177e4 LT |
82 | } |
83 | ||
d3c5ee6d | 84 | static struct xt_match ah_mt_reg __read_mostly = { |
1da177e4 | 85 | .name = "ah", |
6709dbbb | 86 | .family = AF_INET, |
d3c5ee6d | 87 | .match = ah_mt, |
1d5cd909 PM |
88 | .matchsize = sizeof(struct ipt_ah), |
89 | .proto = IPPROTO_AH, | |
d3c5ee6d | 90 | .checkentry = ah_mt_check, |
1da177e4 LT |
91 | .me = THIS_MODULE, |
92 | }; | |
93 | ||
d3c5ee6d | 94 | static int __init ah_mt_init(void) |
1da177e4 | 95 | { |
d3c5ee6d | 96 | return xt_register_match(&ah_mt_reg); |
1da177e4 LT |
97 | } |
98 | ||
d3c5ee6d | 99 | static void __exit ah_mt_exit(void) |
1da177e4 | 100 | { |
d3c5ee6d | 101 | xt_unregister_match(&ah_mt_reg); |
1da177e4 LT |
102 | } |
103 | ||
d3c5ee6d JE |
104 | module_init(ah_mt_init); |
105 | module_exit(ah_mt_exit); |