[NETFILTER]: xt_policy: use the new union nf_inet_addr
[linux-2.6-block.git] / net / netfilter / xt_esp.c
CommitLineData
1da177e4
LT
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>
dc5ab2fa 12#include <linux/in.h>
1da177e4
LT
13#include <linux/ip.h>
14
dc5ab2fa
YK
15#include <linux/netfilter/xt_esp.h>
16#include <linux/netfilter/x_tables.h>
17
1da177e4 18#include <linux/netfilter_ipv4/ip_tables.h>
dc5ab2fa 19#include <linux/netfilter_ipv6/ip6_tables.h>
1da177e4
LT
20
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
dc5ab2fa
YK
23MODULE_DESCRIPTION("x_tables ESP SPI match module");
24MODULE_ALIAS("ipt_esp");
25MODULE_ALIAS("ip6t_esp");
1da177e4 26
dc5ab2fa 27#if 0
1da177e4
LT
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 */
1d93a9cb
JE
34static inline bool
35spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
1da177e4 36{
1d93a9cb 37 bool r;
dc5ab2fa
YK
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");
1da177e4
LT
42 return r;
43}
44
1d93a9cb 45static bool
d3c5ee6d
JE
46esp_mt(const struct sk_buff *skb, const struct net_device *in,
47 const struct net_device *out, const struct xt_match *match,
48 const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop)
1da177e4
LT
49{
50 struct ip_esp_hdr _esp, *eh;
dc5ab2fa 51 const struct xt_esp *espinfo = matchinfo;
1da177e4
LT
52
53 /* Must not be a fragment. */
54 if (offset)
1d93a9cb 55 return false;
1da177e4 56
dc5ab2fa 57 eh = skb_header_pointer(skb, protoff, sizeof(_esp), &_esp);
1da177e4
LT
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");
cff533ac 63 *hotdrop = true;
1d93a9cb 64 return false;
1da177e4
LT
65 }
66
dc5ab2fa
YK
67 return spi_match(espinfo->spis[0], espinfo->spis[1], ntohl(eh->spi),
68 !!(espinfo->invflags & XT_ESP_INV_SPI));
1da177e4
LT
69}
70
71/* Called when user tries to insert an entry of this type. */
ccb79bdc 72static bool
d3c5ee6d
JE
73esp_mt_check(const char *tablename, const void *ip_void,
74 const struct xt_match *match, void *matchinfo,
75 unsigned int hook_mask)
1da177e4 76{
dc5ab2fa 77 const struct xt_esp *espinfo = matchinfo;
1da177e4 78
dc5ab2fa
YK
79 if (espinfo->invflags & ~XT_ESP_INV_MASK) {
80 duprintf("xt_esp: unknown flags %X\n", espinfo->invflags);
ccb79bdc 81 return false;
1da177e4 82 }
dc5ab2fa 83
ccb79bdc 84 return true;
1da177e4
LT
85}
86
d3c5ee6d 87static struct xt_match esp_mt_reg[] __read_mostly = {
4470bbc7
PM
88 {
89 .name = "esp",
90 .family = AF_INET,
d3c5ee6d
JE
91 .checkentry = esp_mt_check,
92 .match = esp_mt,
4470bbc7
PM
93 .matchsize = sizeof(struct xt_esp),
94 .proto = IPPROTO_ESP,
95 .me = THIS_MODULE,
96 },
97 {
98 .name = "esp",
99 .family = AF_INET6,
d3c5ee6d
JE
100 .checkentry = esp_mt_check,
101 .match = esp_mt,
4470bbc7
PM
102 .matchsize = sizeof(struct xt_esp),
103 .proto = IPPROTO_ESP,
104 .me = THIS_MODULE,
105 },
dc5ab2fa
YK
106};
107
d3c5ee6d 108static int __init esp_mt_init(void)
1da177e4 109{
d3c5ee6d 110 return xt_register_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg));
1da177e4
LT
111}
112
d3c5ee6d 113static void __exit esp_mt_exit(void)
1da177e4 114{
d3c5ee6d 115 xt_unregister_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg));
1da177e4
LT
116}
117
d3c5ee6d
JE
118module_init(esp_mt_init);
119module_exit(esp_mt_exit);