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>"); 23 MODULE_DESCRIPTION("Xtables: IPsec-ESP packet match"); 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 */ 34 static inline bool 35 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert) 36 { 37 bool r; 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 45 static bool esp_mt(const struct sk_buff *skb, const struct xt_match_param *par) 46 { 47 const struct ip_esp_hdr *eh; 48 struct ip_esp_hdr _esp; 49 const struct xt_esp *espinfo = par->matchinfo; 50 51 /* Must not be a fragment. */ 52 if (par->fragoff != 0) 53 return false; 54 55 eh = skb_header_pointer(skb, par->thoff, sizeof(_esp), &_esp); 56 if (eh == NULL) { 57 /* We've been asked to examine this packet, and we 58 * can't. Hence, no choice but to drop. 59 */ 60 duprintf("Dropping evil ESP tinygram.\n"); 61 *par->hotdrop = true; 62 return false; 63 } 64 65 return spi_match(espinfo->spis[0], espinfo->spis[1], ntohl(eh->spi), 66 !!(espinfo->invflags & XT_ESP_INV_SPI)); 67 } 68 69 /* Called when user tries to insert an entry of this type. */ 70 static bool 71 esp_mt_check(const char *tablename, const void *ip_void, 72 const struct xt_match *match, void *matchinfo, 73 unsigned int hook_mask) 74 { 75 const struct xt_esp *espinfo = matchinfo; 76 77 if (espinfo->invflags & ~XT_ESP_INV_MASK) { 78 duprintf("xt_esp: unknown flags %X\n", espinfo->invflags); 79 return false; 80 } 81 82 return true; 83 } 84 85 static struct xt_match esp_mt_reg[] __read_mostly = { 86 { 87 .name = "esp", 88 .family = NFPROTO_IPV4, 89 .checkentry = esp_mt_check, 90 .match = esp_mt, 91 .matchsize = sizeof(struct xt_esp), 92 .proto = IPPROTO_ESP, 93 .me = THIS_MODULE, 94 }, 95 { 96 .name = "esp", 97 .family = NFPROTO_IPV6, 98 .checkentry = esp_mt_check, 99 .match = esp_mt, 100 .matchsize = sizeof(struct xt_esp), 101 .proto = IPPROTO_ESP, 102 .me = THIS_MODULE, 103 }, 104 }; 105 106 static int __init esp_mt_init(void) 107 { 108 return xt_register_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg)); 109 } 110 111 static void __exit esp_mt_exit(void) 112 { 113 xt_unregister_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg)); 114 } 115 116 module_init(esp_mt_init); 117 module_exit(esp_mt_exit); 118