Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 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 | */ |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/in.h> |
| 13 | #include <linux/ip.h> |
| 14 | |
| 15 | #include <linux/netfilter/xt_esp.h> |
| 16 | #include <linux/netfilter/x_tables.h> |
| 17 | |
| 18 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 19 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 20 | |
| 21 | MODULE_LICENSE("GPL"); |
| 22 | MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 23 | MODULE_DESCRIPTION("Xtables: IPsec-ESP packet match"); |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 24 | MODULE_ALIAS("ipt_esp"); |
| 25 | MODULE_ALIAS("ip6t_esp"); |
| 26 | |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 27 | /* Returns 1 if the spi is matched by the range, 0 otherwise */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 28 | static inline bool |
| 29 | spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert) |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 30 | { |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 31 | bool r; |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 32 | pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n", |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 33 | invert ? '!' : ' ', min, spi, max); |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 34 | r = (spi >= min && spi <= max) ^ invert; |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 35 | pr_debug(" result %s\n", r ? "PASS" : "FAILED"); |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 36 | return r; |
| 37 | } |
| 38 | |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 39 | static bool esp_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 40 | { |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 41 | const struct ip_esp_hdr *eh; |
| 42 | struct ip_esp_hdr _esp; |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 43 | const struct xt_esp *espinfo = par->matchinfo; |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 44 | |
| 45 | /* Must not be a fragment. */ |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 46 | if (par->fragoff != 0) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 47 | return false; |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 48 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 49 | eh = skb_header_pointer(skb, par->thoff, sizeof(_esp), &_esp); |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 50 | if (eh == NULL) { |
| 51 | /* We've been asked to examine this packet, and we |
| 52 | * can't. Hence, no choice but to drop. |
| 53 | */ |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 54 | pr_debug("Dropping evil ESP tinygram.\n"); |
Jan Engelhardt | b4ba261 | 2009-07-07 20:54:30 +0200 | [diff] [blame] | 55 | par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 56 | return false; |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | return spi_match(espinfo->spis[0], espinfo->spis[1], ntohl(eh->spi), |
| 60 | !!(espinfo->invflags & XT_ESP_INV_SPI)); |
| 61 | } |
| 62 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 63 | static int esp_mt_check(const struct xt_mtchk_param *par) |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 64 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 65 | const struct xt_esp *espinfo = par->matchinfo; |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 66 | |
| 67 | if (espinfo->invflags & ~XT_ESP_INV_MASK) { |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 68 | pr_debug("unknown flags %X\n", espinfo->invflags); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 69 | return -EINVAL; |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 72 | return 0; |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 75 | static struct xt_match esp_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 76 | { |
| 77 | .name = "esp", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 78 | .family = NFPROTO_IPV4, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 79 | .checkentry = esp_mt_check, |
| 80 | .match = esp_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 81 | .matchsize = sizeof(struct xt_esp), |
| 82 | .proto = IPPROTO_ESP, |
| 83 | .me = THIS_MODULE, |
| 84 | }, |
| 85 | { |
| 86 | .name = "esp", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 87 | .family = NFPROTO_IPV6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 88 | .checkentry = esp_mt_check, |
| 89 | .match = esp_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 90 | .matchsize = sizeof(struct xt_esp), |
| 91 | .proto = IPPROTO_ESP, |
| 92 | .me = THIS_MODULE, |
| 93 | }, |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 96 | static int __init esp_mt_init(void) |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 97 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 98 | return xt_register_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg)); |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 101 | static void __exit esp_mt_exit(void) |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 102 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 103 | xt_unregister_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg)); |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 106 | module_init(esp_mt_init); |
| 107 | module_exit(esp_mt_exit); |