Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel module to match ESP parameters. */ |
2 | ||
3 | /* (C) 1999-2000 Yon Uriarte <yon@astaro.de> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | */ | |
be91fd5e | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4 LT |
10 | #include <linux/module.h> |
11 | #include <linux/skbuff.h> | |
dc5ab2fa | 12 | #include <linux/in.h> |
1da177e4 LT |
13 | #include <linux/ip.h> |
14 | ||
dc5ab2fa YK |
15 | #include <linux/netfilter/xt_esp.h> |
16 | #include <linux/netfilter/x_tables.h> | |
17 | ||
1da177e4 | 18 | #include <linux/netfilter_ipv4/ip_tables.h> |
dc5ab2fa | 19 | #include <linux/netfilter_ipv6/ip6_tables.h> |
1da177e4 LT |
20 | |
21 | MODULE_LICENSE("GPL"); | |
22 | MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>"); | |
2ae15b64 | 23 | MODULE_DESCRIPTION("Xtables: IPsec-ESP packet match"); |
dc5ab2fa YK |
24 | MODULE_ALIAS("ipt_esp"); |
25 | MODULE_ALIAS("ip6t_esp"); | |
1da177e4 | 26 | |
1da177e4 | 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; |
ff67e4e4 | 32 | pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n", |
be91fd5e | 33 | invert ? '!' : ' ', min, spi, max); |
dc5ab2fa | 34 | r = (spi >= min && spi <= max) ^ invert; |
be91fd5e | 35 | pr_debug(" result %s\n", r ? "PASS" : "FAILED"); |
1da177e4 LT |
36 | return r; |
37 | } | |
38 | ||
4b560b44 JE |
39 | static bool esp_mt(const struct sk_buff *skb, |
40 | const struct xt_action_param *par) | |
1da177e4 | 41 | { |
3cf93c96 JE |
42 | const struct ip_esp_hdr *eh; |
43 | struct ip_esp_hdr _esp; | |
f7108a20 | 44 | const struct xt_esp *espinfo = par->matchinfo; |
1da177e4 LT |
45 | |
46 | /* Must not be a fragment. */ | |
f7108a20 | 47 | if (par->fragoff != 0) |
1d93a9cb | 48 | return false; |
1da177e4 | 49 | |
f7108a20 | 50 | eh = skb_header_pointer(skb, par->thoff, sizeof(_esp), &_esp); |
1da177e4 LT |
51 | if (eh == NULL) { |
52 | /* We've been asked to examine this packet, and we | |
53 | * can't. Hence, no choice but to drop. | |
54 | */ | |
be91fd5e | 55 | pr_debug("Dropping evil ESP tinygram.\n"); |
f7108a20 | 56 | *par->hotdrop = true; |
1d93a9cb | 57 | return false; |
1da177e4 LT |
58 | } |
59 | ||
dc5ab2fa YK |
60 | return spi_match(espinfo->spis[0], espinfo->spis[1], ntohl(eh->spi), |
61 | !!(espinfo->invflags & XT_ESP_INV_SPI)); | |
1da177e4 LT |
62 | } |
63 | ||
b0f38452 | 64 | static int esp_mt_check(const struct xt_mtchk_param *par) |
1da177e4 | 65 | { |
9b4fce7a | 66 | const struct xt_esp *espinfo = par->matchinfo; |
1da177e4 | 67 | |
dc5ab2fa | 68 | if (espinfo->invflags & ~XT_ESP_INV_MASK) { |
be91fd5e | 69 | pr_debug("unknown flags %X\n", espinfo->invflags); |
bd414ee6 | 70 | return -EINVAL; |
1da177e4 | 71 | } |
dc5ab2fa | 72 | |
bd414ee6 | 73 | return 0; |
1da177e4 LT |
74 | } |
75 | ||
d3c5ee6d | 76 | static struct xt_match esp_mt_reg[] __read_mostly = { |
4470bbc7 PM |
77 | { |
78 | .name = "esp", | |
ee999d8b | 79 | .family = NFPROTO_IPV4, |
d3c5ee6d JE |
80 | .checkentry = esp_mt_check, |
81 | .match = esp_mt, | |
4470bbc7 PM |
82 | .matchsize = sizeof(struct xt_esp), |
83 | .proto = IPPROTO_ESP, | |
84 | .me = THIS_MODULE, | |
85 | }, | |
86 | { | |
87 | .name = "esp", | |
ee999d8b | 88 | .family = NFPROTO_IPV6, |
d3c5ee6d JE |
89 | .checkentry = esp_mt_check, |
90 | .match = esp_mt, | |
4470bbc7 PM |
91 | .matchsize = sizeof(struct xt_esp), |
92 | .proto = IPPROTO_ESP, | |
93 | .me = THIS_MODULE, | |
94 | }, | |
dc5ab2fa YK |
95 | }; |
96 | ||
d3c5ee6d | 97 | static int __init esp_mt_init(void) |
1da177e4 | 98 | { |
d3c5ee6d | 99 | return xt_register_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg)); |
1da177e4 LT |
100 | } |
101 | ||
d3c5ee6d | 102 | static void __exit esp_mt_exit(void) |
1da177e4 | 103 | { |
d3c5ee6d | 104 | xt_unregister_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg)); |
1da177e4 LT |
105 | } |
106 | ||
d3c5ee6d JE |
107 | module_init(esp_mt_init); |
108 | module_exit(esp_mt_exit); |