Blame view
net/netfilter/xt_esp.c
2.64 KB
1da177e4c
|
1 2 3 4 5 6 7 8 |
/* Kernel module to match ESP parameters. */ /* (C) 1999-2000 Yon Uriarte <yon@astaro.de> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ |
be91fd5e3
|
9 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4c
|
10 11 |
#include <linux/module.h> #include <linux/skbuff.h> |
dc5ab2fae
|
12 |
#include <linux/in.h> |
1da177e4c
|
13 |
#include <linux/ip.h> |
dc5ab2fae
|
14 15 |
#include <linux/netfilter/xt_esp.h> #include <linux/netfilter/x_tables.h> |
1da177e4c
|
16 |
#include <linux/netfilter_ipv4/ip_tables.h> |
dc5ab2fae
|
17 |
#include <linux/netfilter_ipv6/ip6_tables.h> |
1da177e4c
|
18 19 20 |
MODULE_LICENSE("GPL"); MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>"); |
2ae15b64e
|
21 |
MODULE_DESCRIPTION("Xtables: IPsec-ESP packet match"); |
dc5ab2fae
|
22 23 |
MODULE_ALIAS("ipt_esp"); MODULE_ALIAS("ip6t_esp"); |
1da177e4c
|
24 |
|
1da177e4c
|
25 |
/* Returns 1 if the spi is matched by the range, 0 otherwise */ |
1d93a9cba
|
26 27 |
static inline bool spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert) |
1da177e4c
|
28 |
{ |
1d93a9cba
|
29 |
bool r; |
ff67e4e42
|
30 31 |
pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x ", |
be91fd5e3
|
32 |
invert ? '!' : ' ', min, spi, max); |
dc5ab2fae
|
33 |
r = (spi >= min && spi <= max) ^ invert; |
be91fd5e3
|
34 35 |
pr_debug(" result %s ", r ? "PASS" : "FAILED"); |
1da177e4c
|
36 37 |
return r; } |
62fc80510
|
38 |
static bool esp_mt(const struct sk_buff *skb, struct xt_action_param *par) |
1da177e4c
|
39 |
{ |
3cf93c96a
|
40 41 |
const struct ip_esp_hdr *eh; struct ip_esp_hdr _esp; |
f7108a20d
|
42 |
const struct xt_esp *espinfo = par->matchinfo; |
1da177e4c
|
43 44 |
/* Must not be a fragment. */ |
f7108a20d
|
45 |
if (par->fragoff != 0) |
1d93a9cba
|
46 |
return false; |
1da177e4c
|
47 |
|
f7108a20d
|
48 |
eh = skb_header_pointer(skb, par->thoff, sizeof(_esp), &_esp); |
1da177e4c
|
49 50 51 52 |
if (eh == NULL) { /* We've been asked to examine this packet, and we * can't. Hence, no choice but to drop. */ |
be91fd5e3
|
53 54 |
pr_debug("Dropping evil ESP tinygram. "); |
b4ba26119
|
55 |
par->hotdrop = true; |
1d93a9cba
|
56 |
return false; |
1da177e4c
|
57 |
} |
dc5ab2fae
|
58 59 |
return spi_match(espinfo->spis[0], espinfo->spis[1], ntohl(eh->spi), !!(espinfo->invflags & XT_ESP_INV_SPI)); |
1da177e4c
|
60 |
} |
b0f38452f
|
61 |
static int esp_mt_check(const struct xt_mtchk_param *par) |
1da177e4c
|
62 |
{ |
9b4fce7a3
|
63 |
const struct xt_esp *espinfo = par->matchinfo; |
1da177e4c
|
64 |
|
dc5ab2fae
|
65 |
if (espinfo->invflags & ~XT_ESP_INV_MASK) { |
be91fd5e3
|
66 67 |
pr_debug("unknown flags %X ", espinfo->invflags); |
bd414ee60
|
68 |
return -EINVAL; |
1da177e4c
|
69 |
} |
dc5ab2fae
|
70 |
|
bd414ee60
|
71 |
return 0; |
1da177e4c
|
72 |
} |
d3c5ee6d5
|
73 |
static struct xt_match esp_mt_reg[] __read_mostly = { |
4470bbc74
|
74 75 |
{ .name = "esp", |
ee999d8b9
|
76 |
.family = NFPROTO_IPV4, |
d3c5ee6d5
|
77 78 |
.checkentry = esp_mt_check, .match = esp_mt, |
4470bbc74
|
79 80 81 82 83 84 |
.matchsize = sizeof(struct xt_esp), .proto = IPPROTO_ESP, .me = THIS_MODULE, }, { .name = "esp", |
ee999d8b9
|
85 |
.family = NFPROTO_IPV6, |
d3c5ee6d5
|
86 87 |
.checkentry = esp_mt_check, .match = esp_mt, |
4470bbc74
|
88 89 90 91 |
.matchsize = sizeof(struct xt_esp), .proto = IPPROTO_ESP, .me = THIS_MODULE, }, |
dc5ab2fae
|
92 |
}; |
d3c5ee6d5
|
93 |
static int __init esp_mt_init(void) |
1da177e4c
|
94 |
{ |
d3c5ee6d5
|
95 |
return xt_register_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg)); |
1da177e4c
|
96 |
} |
d3c5ee6d5
|
97 |
static void __exit esp_mt_exit(void) |
1da177e4c
|
98 |
{ |
d3c5ee6d5
|
99 |
xt_unregister_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg)); |
1da177e4c
|
100 |
} |
d3c5ee6d5
|
101 102 |
module_init(esp_mt_init); module_exit(esp_mt_exit); |