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 | */ | |
be91fd5e | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
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 | 20 | |
1da177e4 | 21 | /* Returns 1 if the spi is matched by the range, 0 otherwise */ |
1d93a9cb JE |
22 | static inline bool |
23 | spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert) | |
1da177e4 | 24 | { |
1d93a9cb | 25 | bool r; |
be91fd5e JE |
26 | pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n", |
27 | invert ? '!' : ' ', min, spi, max); | |
1da177e4 | 28 | r=(spi >= min && spi <= max) ^ invert; |
be91fd5e | 29 | pr_debug(" result %s\n", r ? "PASS" : "FAILED"); |
1da177e4 LT |
30 | return r; |
31 | } | |
32 | ||
62fc8051 | 33 | static bool ah_mt(const struct sk_buff *skb, struct xt_action_param *par) |
1da177e4 | 34 | { |
a47362a2 JE |
35 | struct ip_auth_hdr _ahdr; |
36 | const struct ip_auth_hdr *ah; | |
f7108a20 | 37 | const struct ipt_ah *ahinfo = par->matchinfo; |
1da177e4 LT |
38 | |
39 | /* Must not be a fragment. */ | |
f7108a20 | 40 | if (par->fragoff != 0) |
1d93a9cb | 41 | return false; |
1da177e4 | 42 | |
f7108a20 | 43 | ah = skb_header_pointer(skb, par->thoff, sizeof(_ahdr), &_ahdr); |
1da177e4 LT |
44 | if (ah == NULL) { |
45 | /* We've been asked to examine this packet, and we | |
46 | * can't. Hence, no choice but to drop. | |
47 | */ | |
be91fd5e | 48 | pr_debug("Dropping evil AH tinygram.\n"); |
b4ba2611 | 49 | par->hotdrop = true; |
1da177e4 LT |
50 | return 0; |
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 | ||
b0f38452 | 58 | static int ah_mt_check(const struct xt_mtchk_param *par) |
1da177e4 | 59 | { |
9b4fce7a | 60 | const struct ipt_ah *ahinfo = par->matchinfo; |
1da177e4 | 61 | |
1d5cd909 | 62 | /* Must specify no unknown invflags */ |
1da177e4 | 63 | if (ahinfo->invflags & ~IPT_AH_INV_MASK) { |
be91fd5e | 64 | pr_debug("unknown flags %X\n", ahinfo->invflags); |
bd414ee6 | 65 | return -EINVAL; |
1da177e4 | 66 | } |
bd414ee6 | 67 | return 0; |
1da177e4 LT |
68 | } |
69 | ||
d3c5ee6d | 70 | static struct xt_match ah_mt_reg __read_mostly = { |
1da177e4 | 71 | .name = "ah", |
ee999d8b | 72 | .family = NFPROTO_IPV4, |
d3c5ee6d | 73 | .match = ah_mt, |
1d5cd909 PM |
74 | .matchsize = sizeof(struct ipt_ah), |
75 | .proto = IPPROTO_AH, | |
d3c5ee6d | 76 | .checkentry = ah_mt_check, |
1da177e4 LT |
77 | .me = THIS_MODULE, |
78 | }; | |
79 | ||
d3c5ee6d | 80 | static int __init ah_mt_init(void) |
1da177e4 | 81 | { |
d3c5ee6d | 82 | return xt_register_match(&ah_mt_reg); |
1da177e4 LT |
83 | } |
84 | ||
d3c5ee6d | 85 | static void __exit ah_mt_exit(void) |
1da177e4 | 86 | { |
d3c5ee6d | 87 | xt_unregister_match(&ah_mt_reg); |
1da177e4 LT |
88 | } |
89 | ||
d3c5ee6d JE |
90 | module_init(ah_mt_init); |
91 | module_exit(ah_mt_exit); |