blob: b11378e001b6acb639873491606a62e16de8217d [file] [log] [blame]
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -08001/* 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#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 Engelhardt1d93a9c2007-07-07 22:15:35 -070034static inline bool
35spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080036{
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070037 bool r;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080038 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 Engelhardt1d93a9c2007-07-07 22:15:35 -070045static bool
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080046match(const struct sk_buff *skb,
47 const struct net_device *in,
48 const struct net_device *out,
49 const struct xt_match *match,
50 const void *matchinfo,
51 int offset,
52 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070053 bool *hotdrop)
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080054{
55 struct ip_esp_hdr _esp, *eh;
56 const struct xt_esp *espinfo = matchinfo;
57
58 /* Must not be a fragment. */
59 if (offset)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070060 return false;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080061
62 eh = skb_header_pointer(skb, protoff, sizeof(_esp), &_esp);
63 if (eh == NULL) {
64 /* We've been asked to examine this packet, and we
65 * can't. Hence, no choice but to drop.
66 */
67 duprintf("Dropping evil ESP tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -070068 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070069 return false;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080070 }
71
72 return spi_match(espinfo->spis[0], espinfo->spis[1], ntohl(eh->spi),
73 !!(espinfo->invflags & XT_ESP_INV_SPI));
74}
75
76/* Called when user tries to insert an entry of this type. */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070077static bool
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080078checkentry(const char *tablename,
79 const void *ip_void,
80 const struct xt_match *match,
81 void *matchinfo,
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080082 unsigned int hook_mask)
83{
84 const struct xt_esp *espinfo = matchinfo;
85
86 if (espinfo->invflags & ~XT_ESP_INV_MASK) {
87 duprintf("xt_esp: unknown flags %X\n", espinfo->invflags);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070088 return false;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080089 }
90
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070091 return true;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080092}
93
Patrick McHardy9f15c532007-07-07 22:22:02 -070094static struct xt_match xt_esp_match[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070095 {
96 .name = "esp",
97 .family = AF_INET,
98 .checkentry = checkentry,
99 .match = match,
100 .matchsize = sizeof(struct xt_esp),
101 .proto = IPPROTO_ESP,
102 .me = THIS_MODULE,
103 },
104 {
105 .name = "esp",
106 .family = AF_INET6,
107 .checkentry = checkentry,
108 .match = match,
109 .matchsize = sizeof(struct xt_esp),
110 .proto = IPPROTO_ESP,
111 .me = THIS_MODULE,
112 },
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -0800113};
114
115static int __init xt_esp_init(void)
116{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700117 return xt_register_matches(xt_esp_match, ARRAY_SIZE(xt_esp_match));
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -0800118}
119
120static void __exit xt_esp_cleanup(void)
121{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700122 xt_unregister_matches(xt_esp_match, ARRAY_SIZE(xt_esp_match));
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -0800123}
124
125module_init(xt_esp_init);
126module_exit(xt_esp_cleanup);