blob: 3840b417a3c51c7b669eaa6121bd28d2cfaa458e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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/ip.h>
13
14#include <linux/netfilter_ipv4/ipt_esp.h>
15#include <linux/netfilter_ipv4/ip_tables.h>
16
17MODULE_LICENSE("GPL");
18MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
19MODULE_DESCRIPTION("iptables ESP SPI match module");
20
21#ifdef DEBUG_CONNTRACK
22#define duprintf(format, args...) printk(format , ## args)
23#else
24#define duprintf(format, args...)
25#endif
26
27/* Returns 1 if the spi is matched by the range, 0 otherwise */
28static inline int
29spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, int invert)
30{
31 int r=0;
32 duprintf("esp spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ',
33 min,spi,max);
34 r=(spi >= min && spi <= max) ^ invert;
35 duprintf(" result %s\n",r? "PASS" : "FAILED");
36 return r;
37}
38
39static int
40match(const struct sk_buff *skb,
41 const struct net_device *in,
42 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080043 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 const void *matchinfo,
45 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080046 unsigned int protoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int *hotdrop)
48{
49 struct ip_esp_hdr _esp, *eh;
50 const struct ipt_esp *espinfo = matchinfo;
51
52 /* Must not be a fragment. */
53 if (offset)
54 return 0;
55
Harald Welte2e4e6a12006-01-12 13:30:04 -080056 eh = skb_header_pointer(skb, protoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 sizeof(_esp), &_esp);
58 if (eh == NULL) {
59 /* We've been asked to examine this packet, and we
60 * can't. Hence, no choice but to drop.
61 */
62 duprintf("Dropping evil ESP tinygram.\n");
63 *hotdrop = 1;
64 return 0;
65 }
66
67 return spi_match(espinfo->spis[0], espinfo->spis[1],
68 ntohl(eh->spi),
69 !!(espinfo->invflags & IPT_ESP_INV_SPI));
70}
71
72/* Called when user tries to insert an entry of this type. */
73static int
74checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080075 const void *ip_void,
Patrick McHardyc4986732006-03-20 18:02:56 -080076 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 void *matchinfo,
78 unsigned int matchinfosize,
79 unsigned int hook_mask)
80{
81 const struct ipt_esp *espinfo = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Patrick McHardy1d5cd902006-03-20 18:01:14 -080083 /* Must specify no unknown invflags */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (espinfo->invflags & ~IPT_ESP_INV_MASK) {
Patrick McHardy1d5cd902006-03-20 18:01:14 -080085 duprintf("ipt_esp: unknown flags %X\n", espinfo->invflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 0;
87 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 1;
89}
90
91static struct ipt_match esp_match = {
92 .name = "esp",
Patrick McHardy1d5cd902006-03-20 18:01:14 -080093 .match = match,
94 .matchsize = sizeof(struct ipt_esp),
95 .proto = IPPROTO_ESP,
96 .checkentry = checkentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .me = THIS_MODULE,
98};
99
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800100static int __init ipt_esp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 return ipt_register_match(&esp_match);
103}
104
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800105static void __exit ipt_esp_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 ipt_unregister_match(&esp_match);
108}
109
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800110module_init(ipt_esp_init);
111module_exit(ipt_esp_fini);