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