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 | */ |
| 9 | |
| 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 | |
| 27 | #if 0 |
| 28 | #define duprintf(format, args...) printk(format , ## args) |
| 29 | #else |
| 30 | #define duprintf(format, args...) |
| 31 | #endif |
| 32 | |
| 33 | /* Returns 1 if the spi is matched by the range, 0 otherwise */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 34 | static inline bool |
| 35 | 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] | 36 | { |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 37 | bool r; |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 38 | duprintf("esp spi_match:%c 0x%x <= 0x%x <= 0x%x", invert ? '!' : ' ', |
| 39 | min, spi, max); |
| 40 | r = (spi >= min && spi <= max) ^ invert; |
| 41 | duprintf(" result %s\n", r ? "PASS" : "FAILED"); |
| 42 | return r; |
| 43 | } |
| 44 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 45 | static bool |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 46 | esp_mt(const struct sk_buff *skb, const struct net_device *in, |
| 47 | const struct net_device *out, const struct xt_match *match, |
| 48 | const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop) |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 49 | { |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 50 | const struct ip_esp_hdr *eh; |
| 51 | struct ip_esp_hdr _esp; |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 52 | const struct xt_esp *espinfo = matchinfo; |
| 53 | |
| 54 | /* Must not be a fragment. */ |
| 55 | if (offset) |
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 | eh = skb_header_pointer(skb, protoff, sizeof(_esp), &_esp); |
| 59 | if (eh == NULL) { |
| 60 | /* We've been asked to examine this packet, and we |
| 61 | * can't. Hence, no choice but to drop. |
| 62 | */ |
| 63 | duprintf("Dropping evil ESP tinygram.\n"); |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 64 | *hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 65 | return false; |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | return spi_match(espinfo->spis[0], espinfo->spis[1], ntohl(eh->spi), |
| 69 | !!(espinfo->invflags & XT_ESP_INV_SPI)); |
| 70 | } |
| 71 | |
| 72 | /* Called when user tries to insert an entry of this type. */ |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 73 | static bool |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 74 | esp_mt_check(const char *tablename, const void *ip_void, |
| 75 | const struct xt_match *match, void *matchinfo, |
| 76 | unsigned int hook_mask) |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 77 | { |
| 78 | const struct xt_esp *espinfo = matchinfo; |
| 79 | |
| 80 | if (espinfo->invflags & ~XT_ESP_INV_MASK) { |
| 81 | duprintf("xt_esp: unknown flags %X\n", espinfo->invflags); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 82 | return false; |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 85 | return true; |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 88 | static struct xt_match esp_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 89 | { |
| 90 | .name = "esp", |
| 91 | .family = AF_INET, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 92 | .checkentry = esp_mt_check, |
| 93 | .match = esp_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 94 | .matchsize = sizeof(struct xt_esp), |
| 95 | .proto = IPPROTO_ESP, |
| 96 | .me = THIS_MODULE, |
| 97 | }, |
| 98 | { |
| 99 | .name = "esp", |
| 100 | .family = AF_INET6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 101 | .checkentry = esp_mt_check, |
| 102 | .match = esp_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 103 | .matchsize = sizeof(struct xt_esp), |
| 104 | .proto = IPPROTO_ESP, |
| 105 | .me = THIS_MODULE, |
| 106 | }, |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 109 | static int __init esp_mt_init(void) |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 110 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 111 | return xt_register_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg)); |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 114 | static void __exit esp_mt_exit(void) |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 115 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 116 | xt_unregister_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg)); |
Yasuyuki Kozakai | dc5ab2f | 2006-04-01 02:22:30 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 119 | module_init(esp_mt_init); |
| 120 | module_exit(esp_mt_exit); |