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 21MODULE_LICENSE("GPL"); 22MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>"); 23MODULE_DESCRIPTION("x_tables ESP SPI match module"); 24MODULE_ALIAS("ipt_esp"); 25MODULE_ALIAS("ip6t_esp"); 26 27#define duprintf(format, args...) 28 29/* Returns 1 if the spi is matched by the range, 0 otherwise */ 30static inline int 31spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, int invert) 32{ 33 int r = 0; 34 duprintf("esp spi_match:%c 0x%x <= 0x%x <= 0x%x", invert ? '!' : ' ', 35 min, spi, max); 36 r = (spi >= min && spi <= max) ^ invert; 37 duprintf(" result %s\n", r ? "PASS" : "FAILED"); 38 return r; 39} 40 41static int 42match(const struct sk_buff *skb, 43 const struct net_device *in, 44 const struct net_device *out, 45 const struct xt_match *match, 46 const void *matchinfo, 47 int offset, 48 unsigned int protoff, 49 int *hotdrop) 50{ 51 struct ip_esp_hdr _esp, *eh; 52 const struct xt_esp *espinfo = matchinfo; 53 54 /* Must not be a fragment. */ 55 if (offset) 56 return 0; 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"); 64 *hotdrop = 1; 65 return 0; 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. */ 73static int 74checkentry(const char *tablename, 75 const void *ip_void, 76 const struct xt_match *match, 77 void *matchinfo, 78 unsigned int hook_mask) 79{ 80 const struct xt_esp *espinfo = matchinfo; 81 82 if (espinfo->invflags & ~XT_ESP_INV_MASK) { 83 duprintf("xt_esp: unknown flags %X\n", espinfo->invflags); 84 return 0; 85 } 86 87 return 1; 88} 89 90static struct xt_match xt_esp_match[] = { 91 { 92 .name = "esp", 93 .family = AF_INET, 94 .checkentry = checkentry, 95 .match = match, 96 .matchsize = sizeof(struct xt_esp), 97 .proto = IPPROTO_ESP, 98 .me = THIS_MODULE, 99 }, 100 { 101 .name = "esp", 102 .family = AF_INET6, 103 .checkentry = checkentry, 104 .match = match, 105 .matchsize = sizeof(struct xt_esp), 106 .proto = IPPROTO_ESP, 107 .me = THIS_MODULE, 108 }, 109}; 110 111static int __init xt_esp_init(void) 112{ 113 return xt_register_matches(xt_esp_match, ARRAY_SIZE(xt_esp_match)); 114} 115 116static void __exit xt_esp_cleanup(void) 117{ 118 xt_unregister_matches(xt_esp_match, ARRAY_SIZE(xt_esp_match)); 119} 120 121module_init(xt_esp_init); 122module_exit(xt_esp_cleanup); 123