Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Kernel module to match ESP parameters. */ |
| 2 | /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/skbuff.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 12 | #include <linux/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/ipv6.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <net/checksum.h> |
| 16 | #include <net/ipv6.h> |
| 17 | |
| 18 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 19 | #include <linux/netfilter_ipv6/ip6t_esp.h> |
| 20 | |
| 21 | MODULE_LICENSE("GPL"); |
| 22 | MODULE_DESCRIPTION("IPv6 ESP match"); |
| 23 | MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>"); |
| 24 | |
| 25 | #if 0 |
| 26 | #define DEBUGP printk |
| 27 | #else |
| 28 | #define DEBUGP(format, args...) |
| 29 | #endif |
| 30 | |
| 31 | /* Returns 1 if the spi is matched by the range, 0 otherwise */ |
| 32 | static inline int |
| 33 | spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, int invert) |
| 34 | { |
| 35 | int r=0; |
| 36 | DEBUGP("esp spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ', |
| 37 | min,spi,max); |
| 38 | r=(spi >= min && spi <= max) ^ invert; |
| 39 | DEBUGP(" result %s\n",r? "PASS\n" : "FAILED\n"); |
| 40 | return r; |
| 41 | } |
| 42 | |
| 43 | static int |
| 44 | match(const struct sk_buff *skb, |
| 45 | const struct net_device *in, |
| 46 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 47 | const struct xt_match *match, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | const void *matchinfo, |
| 49 | int offset, |
| 50 | unsigned int protoff, |
| 51 | int *hotdrop) |
| 52 | { |
Yasuyuki Kozakai | e674d0f | 2005-09-19 15:34:40 -0700 | [diff] [blame] | 53 | struct ip_esp_hdr _esp, *eh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | const struct ip6t_esp *espinfo = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | unsigned int ptr; |
| 56 | |
| 57 | /* Make sure this isn't an evil packet */ |
| 58 | /*DEBUGP("ipv6_esp entered \n");*/ |
| 59 | |
Patrick McHardy | b777e0c | 2006-01-05 12:21:16 -0800 | [diff] [blame] | 60 | if (ipv6_find_hdr(skb, &ptr, NEXTHDR_ESP, NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return 0; |
| 62 | |
Yasuyuki Kozakai | e674d0f | 2005-09-19 15:34:40 -0700 | [diff] [blame] | 63 | eh = skb_header_pointer(skb, ptr, sizeof(_esp), &_esp); |
| 64 | if (eh == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | *hotdrop = 1; |
| 66 | return 0; |
| 67 | } |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | DEBUGP("IPv6 ESP SPI %u %08X\n", ntohl(eh->spi), ntohl(eh->spi)); |
| 70 | |
| 71 | return (eh != NULL) |
| 72 | && spi_match(espinfo->spis[0], espinfo->spis[1], |
| 73 | ntohl(eh->spi), |
| 74 | !!(espinfo->invflags & IP6T_ESP_INV_SPI)); |
| 75 | } |
| 76 | |
| 77 | /* Called when user tries to insert an entry of this type. */ |
| 78 | static int |
| 79 | checkentry(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 80 | const void *ip, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 81 | const struct xt_match *match, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | void *matchinfo, |
| 83 | unsigned int matchinfosize, |
| 84 | unsigned int hook_mask) |
| 85 | { |
| 86 | const struct ip6t_esp *espinfo = matchinfo; |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (espinfo->invflags & ~IP6T_ESP_INV_MASK) { |
| 89 | DEBUGP("ip6t_esp: unknown flags %X\n", |
| 90 | espinfo->invflags); |
| 91 | return 0; |
| 92 | } |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | static struct ip6t_match esp_match = { |
| 97 | .name = "esp", |
Patrick McHardy | 7f93971 | 2006-03-20 18:01:43 -0800 | [diff] [blame] | 98 | .match = match, |
| 99 | .matchsize = sizeof(struct ip6t_esp), |
| 100 | .checkentry = checkentry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | .me = THIS_MODULE, |
| 102 | }; |
| 103 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 104 | static int __init ip6t_esp_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | return ip6t_register_match(&esp_match); |
| 107 | } |
| 108 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 109 | static void __exit ip6t_esp_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | ip6t_unregister_match(&esp_match); |
| 112 | } |
| 113 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 114 | module_init(ip6t_esp_init); |
| 115 | module_exit(ip6t_esp_fini); |